Centos7 使用kvm创建虚拟机
来自CloudWiki
1. KVM环境配置
(1)关闭Selinux [root@ localhost ~]#vim /etc/sysconfig/selinux # 修改SELINUX=disabled # This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=disabled # SELINUXTYPE= can take one of these two values: # targeted - Targeted processes are protected, # mls - Multi Level Security protection. SELINUXTYPE=targeted (2) [root@ localhost ~]#setenforce 0 #当次生效,getenforce 查看当前selinux状态 (3)关闭防火墙 [root@ localhost ~]#systemctl stop firewalld [root@ localhost ~]#systemctl disable firewalld (4)查看是否支持虚拟机 [root@localhost ~]# egrep -c '(vmx|svm)' /proc/cpuinfo 2 #如果执行这条命令的返回结果为0则表示CPU不支持虚拟化,如果返回结果为1或者大于1到数字则表示CPU支持虚拟。其中vmx为Intel的CPU指令集,svm为AMD的CPU指令集。
2. 安装KVM
(1)安装KVM软件包 [root@localhost ~]# yum install -y qemu-kvm libvirt virt-* bridge-utils (最后会报错,忽略就好,直接进行下一步) [root@localhost ~]# yum -y install libguestfs-tools (2)查看虚拟机环境 [root@localhost ~]#systemctl start libvirtd 启动libvirt服务 [root@localhost ~]#systemctl enable libvirtd [root@localhost ~]# ln -s /usr/libexec/qemu-kvm /usr/bin/qemu-kvm 虚拟机执行kvm模块命令必做
3. 创建NAT模式KVM虚拟机
将cirros-0.3.3-x86_64-disk.img镜像与qemu-ifup-NAT脚本文件上传到系统/root目录下。 [root@localhost ~]# ls cirros-0.3.3-x86_64-disk.img qemu-ifup-NAT 给脚本赋予执行权限。示例代码如下: [root@localhost ~]# chmod +x /root/qemu-ifup-NAT 通过qemu-kvm命令启动KVM虚拟机。示例代码如下: [root@localhost ~]# qemu-kvm -m 1024 -drive file=/root/cirros-0.3.3-x86_64-disk.img,if=virtio -net nic,model=virtio -net tap,script=/root/qemu-ifup-NAT -nographic -vnc :1 创建虚拟机完成后,cirros用户登录虚拟机,输入用户名为cirros,密码为cubswin:)。然后输入ip addr list命令查询IP地址,最后输入route -n命令查询路由表。示例代码如下: $ ip addr list 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000 link/ether 52:54:00:12:34:56 brd ff:ff:ff:ff:ff:ff inet 192.168.122.89/24 brd 192.168.122.255 scope global eth0 inet6 fe80::5054:ff:fe12:3456/64 scope link valid_lft forever preferred_lft forever $ route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.122.1 0.0.0.0 UG 0 0 0 eth0 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0