KVM(全稱是Kernel-based Virtual Machine)是Linux下x86 硬件平台上的全功能虛擬化解決方案,包含一個可加載的內核模塊kvm.ko提供和虛擬化核心架構和處理器規范模塊。
使用KVM可允許多個包括Linux和Windows每個虛擬機有私有的硬件,包括網卡、磁盤以及圖形適配卡等。自Linux 2.6.20之後集成在Linux的各個主要發行版本中,它使用Linux自身的調度器進行管理,所以相對於Xen,其核心源碼很少。KVM目前已成為學 術界的主流VMM之一。
KVM的虛擬化需要硬件支持(如Intel VT技術或者AMD V技術)。是基於硬件的完全虛擬化。而Xen早期則是基於軟件模擬的Para-Virtualization,新版本則是基於硬件支持的完全虛擬化。但 Xen本身有自己到進程調度器,存儲管理模塊等,所以代碼較為龐大。廣為流傳的商業系統虛擬化軟件VMware ESX系列也是基於軟件模擬的Para-Virtualization。
KVM原理圖
一個典型的 KVM 安裝包括以下部件:
檢查CPU
和 Xen 不同,KVM 需要有 CPU 的支持(IntelVT 或 AMD SVM),在安裝 KVM之前檢查一下CPU是否提供了虛擬技術的支持:
# grep -E --color 'vmx|svm' /proc/cpuinfo flags :fpu vme de pse tsc msr pae mce cx8 apic mtrr pge mca cmov pat pse36 clflush dtsacpi mmx fxsr sse sse2 ss ht tm pbe syscall nx lm constant_tsc arch_perfmonpebs bts rep_good aperfmperf pni dtes64 monitor ds_cplvmxest tm2 ssse3 cx16 xtpr pdcm xsave lahf_lm tpr_shadow vnmi flexpriority flags :fpu vme de pse tsc msr pae mce cx8 apic mtrr pge mca cmov pat pse36 clflush dtsacpi mmx fxsr sse sse2 ss ht tm pbe syscall nx lm constant_tsc arch_perfmonpebs bts rep_good aperfmperf pni dtes64 monitor ds_cplvmxest tm2 ssse3 cx16 xtpr pdcm xsave lahf_lm tpr_shadow vnmi flexpriority
CentOS6系列安裝虛擬化工具可以在安裝系統的時候選擇安裝(這裡不作介紹),這裡主要介紹的是系統安裝之後使用YUM安裝虛擬化工具的過程。
yum安裝虛擬化
為了在CentOS6上使用虛擬化至少要安裝qemu-kvm和qemu-img包。這些安裝包提供用戶級別的KVM模擬器和RHEL6系統主機磁盤鏡像管理。
# yum install qemu-kvm qemu-img
# yum install virt-managerlibvirt libvirt-python python-virtinst libvirt-client virt-viewer
virt-manager: virt-manager, also known as Virtual Machine Manager, provides agraphical tool for administering virtual machines. It uses libvirt-clientlibrary as the management API.
libvirt: he libvirt package provides the server and host side librariesfor interacting with hypervisors and host systems. The libvirt package providesthe libvirtd daemon that handles the library calls, manages virtualized guestsand controls the hypervisor.
libvirt-python:The libvirt-python package contains a module thatpermits applications written in the Python programming language to use theinterface supplied by the libvirt API.
python-virtinst:Provides the virt-install command for creatingvirtual machines.(提供virt-install命令安裝虛擬機)
libvirt-client:The libvirt-client package provides theclient-side APIs and libraries for accessing libvirt servers. Thelibvirt-client package includes the virsh command line tool to manage andcontrol virtualized guests and hypervisors from the command line or a specialvirtualization shell.
virt-viewer:開啟圖形控制台安裝虛擬客戶機需要
也可以使用 yum groupinstall
# yum groupinstall -y Virtualization "Virtualization Client""Virtualization Platform" "Virtualization Tools"
birdge networking 橋接網絡
# vim /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 HWADDR=xxxxxxxxx ONBOOT=yes BRIDGE=br0
# vim /etc/sysconfig/network-scripts/ifcfg-br0 DEVICE=br0 TYPE=Bridge BOOTPROTO=static ONBOOT=yes IPADDR=10.1.1.X NETMASK=255.255.255.0 GATEWAY=x.x.x.x
# service network restart # brctl show bridge name bridge id STP enabled interfaces br0 8000.b8975a004d66 no eth0 virbr0 8000.52540077cd54 yes virbr0-nic
如果設置了防火牆,則使用以下命令放行
# iptables -I FORWARD -m physdev --physdev-is-bridged -j ACCEPT # service iptables save # service iptables restart
# /etc/init.d/libvirtd restart 正在關閉libvirtd 守護進程: [確定] 啟動libvirtd 守護進程: [確定] # chkconfig libvirtd on
安裝可以使用命令行實現,也可以使用KVM管理器安裝,這裡只介紹命令行的一般安裝方法
# virt-install --name=rhel6-1 --disk path=/kvm/rhel6-1/rhel6.img,size=10,sparse=true --graphics=vnc --network bridge=br0 --os-type=linux --os-variant=rhel6 --ram=1024 --cdrom=/media/rhel-server-6.1-i386-dvd.iso //本地安裝
# virt-install \ --name=rhel6-2 \ --file=/kvm/rhel6-2/rhel6-2.img \ --file-size=10 \ --nonsparse --graphics spice \ --vcpus=2 --ram=2048 \ --location=http://example1.com/installation_tree/RHEL6.1-Server-x86_64/os \ --network bridge=br0 \ --os-type=linux \ --os-variant=rhel6-2 //http方式安裝
網絡安裝方法
# virt-install --name=rhel6-3 --disk path=/kvm/rhel6-3/rhel6.img,size=10,sparse=true \