KVM是kernel-based Virtual Machine的简称,目前已成为学术界的主流VMM之一。KVM的虚拟化需要硬件支持(如Intel VT技术或者AMD V技术),是基于硬件的完全虚拟化。KVM的安装和使用相对于XEN来说十分的简单和方便,并且功能强大,比较适用于高性能计算服务的云计算环境中作为IAAS层的虚拟化部署工作。
KVM提供了图像界面的管理接口(Virtual Machine Manager)和命令行式的管理接口(virsh)。可以根据使用的场景采用不同的方式,当然也可以使用Libvirt库进行管理虚拟机,并且使用libvirt库进行虚拟机的管理是业界很推崇的做法,这都源于libvirt库良好的移植性和强大的API,并且提供了多种语言接口(如C语言,python语言,JAVA语言,C#语言和PHP语言)能对Xen,KVM以及QEMU等多类虚拟机进行管理管理
实验环境
centos6.5_x64
桥接模式 br0-eth0-vm
nat模式 br0-eth-eth1-vm
软件安装
cat /proc/cpuinfo | egrep 'vmx|svm' 查看是否支持虚拟化
flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc up arch_perfmon xtopology tsc_reliable nonstop_tsc unfair_spinlock pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm arat xsaveopt tpr_shadow vnmi ept vpid fsgsbase bmi1 avx2 smep bmi2 invpcid
yum remove -y NetworkManager iptables-ipv6
yum install -y tigervnc tigervnc-server lsof wget curl acpid
yum install -y qemu-kvm.x86_64 qemu-kvm-tools.x86_64 qemu-img.x86_64
yum install -y python-virtinst.noarch bridge-utils.x86_64
yum install -y libvirt virt-manager virt-viewer
modprobe kvm
modprobe kvm_inter
lsmod | grep kvm
kvm_amd 40333 0
kvm 345038 1 kvm_amd
vim /etc/sysconfig/vncservers
#VNCSERVERS="2:myusername"
#VNCSERVERARGS[2]="-geometry 800x600 -nolisten tcp -localhost" 找到
VNCSERVERS="1:root"
VNCSERVERARGS[1]="-geometry 1024x768" 改为
vncpasswd
Password:
Verify:
/etc/rc.d/init.d/vncserver restart
vim ~/.vnc/xstartup
xterm -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" & 找到
twm &
#xterm -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" & 改为
#twm &
gnome-session & 添加配置
cp -pv /etc/libvirt/qemu.conf /etc/libvirt/qemu.conf.bak
sed -i 's/#user/user/g' /etc/libvirt/qemu.conf
sed -i 's/#group/group/g' /etc/libvirt/qemu.conf
sed -i 's/#dynamic_ownership = 1/dynamic_ownership = 0/g' /etc/libvirt/qemu.conf
sed -i 's/#vnc_listen/vnc_listen/g' /etc/libvirt/qemu.conf
service libvirtd start
service acpid restart
chkconfig --level 35 libvirtd on
chkconfig --level 35 acpid on
virsh list
Id Name State
netstat -tuplna | grep 5901
tcp 0 0 0.0.0.0:5901 0.0.0.0:* LISTEN 12740/Xvnc
ps -aux | grep libvirtd
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
root 5001 0.0 0.1 698280 7544 ? Sl 11:56 0:00 libvirtd --daemon
root 5196 0.0 0.0 103312 864 pts/0 S+ 12:01 0:00 grep libvirtd
ps -ef | grep acpid
root 90 2 0 Mar22 ? 00:00:00 [kacpid]
68 2467 2423 0 Mar22 ? 00:00:02 hald-addon-acpi: listening on acpid socket /var/run/acpid.socket
root 11408 1 0 15:08 ? 00:00:00 /usr/sbin/acpid
root 11505 7641 0 15:09 pts/0 00:00:00 grep acpid
cp -p /etc/sysctl.conf /etc/sysctl.conf.bak
sed -i "s/net.ipv4.ip_forward = 0/net.ipv4.ip_forward = 1/g" /etc/sysctl.conf 启用双网卡路由转发
sysctl -p
cp -p /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0.bak
vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BRIDGE=br0 添加配置桥接网卡
BOOTPROTO=static
IPADDR=192.168.1.104
NETMASK=255.255.255.0
GATEWAY=192.168.1.1 红笔配置可以删除,也可保留
cp -p /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-br0
vim /etc/sysconfig/network-scripts/ifcfg-br0
DEVICE=br0
TYPE=Bridge
ONBOOT=yes
BOOTPROTO=none
IPADDR=192.168.1.104
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
cp -pv /etc/sysconfig/network-scripts/ifcfg-em1 /etc/sysconfig/network-scripts/ifcfg-em1.30.bak nat网络设置 .30为vlan子接口 .30标准配置
DEVICE="em1.30"
VLAN="yes"
ONBOOT="yes"
BOOTPROTO="static"
IPADDR= 192.168.30.2
NETMASK=255.255.255.0
service network restart
Shutting down interface eth0: bridge br0 does not exist! [ OK ]
Bringing up loopback interface: [ OK ]
Bringing up interface eth0: [ OK ]
Bringing up interface eth1:
Determining IP information for eth1... done. [ OK ]
Bringing up interface br0: Determining if ip address 192.168.1.106 is already in use for device br0... [ OK ]
ip addr | grep br0
4: virbr0:
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
5: virbr0-nic:
9: br0:
inet 192.168.1.106/24 brd 192.168.1.255 scope global br0
brctl show
bridge namebridge idSTP enabledinterfaces
br08000.000c297d9c1dnoeth0
virbr08000.52540091cbf1yesvirbr0-nic
mv CentOS-6.8-x86_64-bin-DVD1.iso /var/lib/libvirt/images/ 上传镜像到系统
ll /var/lib/libvirt/images/
CentOS-6.5-x86_64-bin-DVD1.iso
virt-install --name=xp_aa --ram 4096 --vcpus=2
--disk path=/var/lib/libvirt/images/xp_aa.img,size=20,format=raw,bus=virtio
--cdrom /var/lib/libvirt/images/windows_xp_cd.iso
--network bridge=br0,model=virtio
--vnc --accelerate --force --autostart
--name 虚拟机名字 --ram 内存大小 --vcpus cpu数量
--cdrom 镜像文件目录 --bridge 指定桥接网卡
virsh list --all
11 centos6 running
virsh start kvm_name 开机
virsh reboot kvm_name 重启
virsh shutdown kvm_name 关机
virsh destroy kvm_name 强制关机
virsh autostart kvm_name 开机自动开机
virsh undefine kvm_name 删除虚拟机
virsh suspend kvm_name 暂停,挂起虚拟机
virsh resume kvm_name 暂停状态还原
virt-clone --original centos6 --name centos6-1 --file /var/lib/libvirt/images/centos6-1.img
克隆虚拟机 centos6运行虚拟机 centos6-1克隆虚拟机从命名
virsh save --bypass-cache centos6 /var/lib/libvirt/images/centos6_save --running 备份虚拟机
ll /var/lib/libvirt/images/centos6_save
/var/lib/libvirt/images/centos6_save
virsh destroy centos6 还原/克隆虚拟机之前必须关闭虚拟机
virsh restore /var/lib/libvirt/images/centos6_save --bypass-cache --running 还原虚拟机