KVM是Kernel-based Virtual Machine的简称,是一个开源的虚拟化模块,今天我将在CentOS7的操作系统上安装KVM,以下是我的安装步骤。
一.环境信息
系统: CentOS 7.2 IP: 10.0.0.12/24
在虚拟机安装时,需要启用CPU的虚拟化功能
二. KVM安装步骤
1. 安装qemu-kvm和libvirt
[root@kvm-node1 ~]# yum install -y qemu-kvm libvirt ###qemu-kvm用来创建虚拟机硬盘,libvirt用来管理虚拟机
2. 安装virt-install
[root@kvm-node1 ~]# yum install -y virt-install ###用来创建虚拟机
3. 启动libvirtd,并将它设为开机启动,启动后使用ifconfig查看,发现会多出来一块virbr0的网卡,ip默认为192.168.122.1/24,说明libvirtd启动成功,如果默认没有ifconfig命令,使用yum install -y net-tools安装
[root@kvm-node1 ~]# systemctl start libvirtd && systemctl enable libvirtd [root@kvm-node1 ~]# ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.0.0.12 netmask 255.255.255.0 broadcast 10.0.0.255 inet6 fe80::5505:5aa:3f96:f3f8 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:51:09:70 txqueuelen 1000 (Ethernet) RX packets 22459 bytes 29147955 (27.7 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 5161 bytes 587757 (573.9 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1 (Local Loopback) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255 ether 52:54:00:fb:b1:55 txqueuelen 1000 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
经过以上三步,KVM安装成功,下面开始使用KVM创建虚拟机.
三. 使用KVM创建虚拟机(CentOS7)
1. 使用qemu命令创建一个10G的硬盘(最小10,G,可以更多),硬盘的名称为: CentOS-7-x86_64.raw
[root@kvm-node1 ~]# qemu-img create -f raw /opt/CentOS-7-x86_64.raw 10G Formatting '/opt/CentOS-7-x86_64.raw', fmt=raw size=10737418240 [root@kvm-node1 ~]# ll -h /opt total 0 -rw-r--r-- 1 root root 10G Aug 22 07:59 CentOS-7-x86_64.raw
2. 使用virt-install创建名称为CentOS-7-x86_64的虚拟机,在创建之前,先上传一个CentOS7的ISO镜像,最好在根目录下创建一个ISO的目录文件夹
[root@kvm-node1 ~]# virt-install --virt-type kvm --name CentOS-7-x86_64 --ram 1024 --cdrom=/ISO/CentOS-7-x86_64-Everything-1611.iso --disk path=/opt/CentOS-7-x86_64.raw --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole Starting install... Creating domain... | 0 B 00:03 Domain installation still in progress. You can reconnect to the console to complete the installation process.
这时候使用TightVNC工具,连接主机IP 10.0.0.12,设置安装操作系统的网卡名称为eth0,如图所示
安装步骤和我们平时安装操作系统的方法一样,安装完成后,可以使用virsh list --all显示KVM上所有的虚拟机