(1)确认是否能够安装KVM
# grep -E 'svm|vmx' /proc/cpuinfo > /dev/null && echo OK # grep flags /proc/cpuinfo|grep lm > /dev/null && echo OK
(2)安装KVM
# yum -y install libguestfs libvirt libvirt-client python-virtinst qemu-kvm qemu-img virt-manager virt-top virt-viewer virt-who virt-install bridge-utils # systemctl start libvirtd # systemctl enable libvirtd
(3)配置网络Bridged Network
# systemctl disable NetworkManager # systemctl stop NetworkManager # vi /etc/sysconfig/network-scripts/ifcfg-br0 DEVICE=br0 BOOTRPOTO=none TYPE=Bridge ONBOOT=yes DEFROUTE=yes IPV6INIT=no NM_CONTROLLED=yes USERCTL=no IPADDR=192.168.21.176 NETMASK=255.255.255.0 GATEWAY=192.168.21.254 DNS1=219.149.6.99 # vi /etc/sysconfig/network-scripts/ifcfg-p4p1 TYPE=Ethernet BOOTPROTO=static IPV6INIT=no NAME=p4p1 UUID=4497ef0a-b570-49fc-bd41-cca65af5934d DEVICE=p4p1 ONBOOT=yes IPADDR=192.168.21.176 PREFIX=24 GATEWAY=192.168.21.254 DNS1=219.149.6.99 BRIDGE=br0 # systemctl restart network # ifconfig # brctl show
(4)安装虚拟机
# qemu-img create -f qcow2 /var/lib/libvirt/images/kvm01_centos7.img 20G # ls -l /var/lib/libvirt/images # virt-install \ --name=kvm01_centos7 \ --hvm \ --virt-type kvm \ --vcpus=2 --ram=1024 \ --network bridge=br0 \ --disk path=/var/lib/libvirt/images/kvm01_centos7.img,size=20 \ --location=/tmp/CentOS-7-x86_64-Minimal-1511.iso \ --os-type=linux --os-variant=rhel7 \ --accelerate \ --nographics \ --autostart \ --extra-args "console=ttyS0"
配置kvm的IP
# virsh start kvm01_centos7 # virsh console kvm01_centos7 # vi /etc/sysconfig/network-scripts/ifcfg-eth0 # shutdown -r now
(5)虚拟机操作
KVM的配置文件
# ll /etc/libvirt/qemu/
查看已安装的kvm
# virsh list --all
启动停止重启kvm
# virsh start kvm01_centos7 # virsh shutdown kvm01_centos7 # virsh reboot kvm01_centos7
配置kvm
# virsh dumpxml kvm01_centos7 # virsh edit kvm01_centos7
链接kvm
# virsh console kvm01_centos7
退出kvm
引用
# Ctrl+]
# Ctrl+5
# Ctrl+5
查看kvm
# virsh dominfo kvm01_centos7 # virt-top
快照kvm
# virsh snapshot-list kvm01_centos7 # virsh snapshot-create-as kvm01_centos7 kvm01_centos7-snapshot-2016-09-06-001 # virsh snapshot-revert kvm01_centos7 kvm01_centos7-snapshot-2016-09-06-001 # virsh snapshot-delete kvm01_centos7 kvm01_centos7-snapshot-2016-09-06-001
复制kvm
# virsh shutdown kvm01_centos7 # virt-clone -o kvm01_centos7 -n kvm02_centos7 -f /var/lib/libvirt/images/kvm02_centos7.img # ll /var/lib/libvirt/images/ # virsh list --all
# virsh start kvm02_centos7 # virsh console kvm02_centos7 # vi /etc/hosts # vi /etc/hostname (CentOS6是/etc/sysconfig/network) # vi /etc/sysconfig/network-scripts/ifcfg-eth0 # vi /etc/udev/rules.d/70-persistent-net.rules # shutdown -r now
删除kvm
# virsh autostart --disable kvm01_centos7 # virsh destroy kvm01_centos7 # virsh undefine kvm01_centos7