KVM (正體中文)
KVM(Kernel-based Virtual Machine的縮寫)是於Linux內核內建的虛擬機。有點類似於 Xen ,但更追求更簡單的運作,比如執行此虛擬機器,只需要載入相應的 kvm
模組即可後台待命。和 Xen 的半虛擬不同的是,KVM 需要電腦支援虛擬化技術(Intel的 VT 扩展或者 AMD 的 AMD-V 擴充)。Xen的全虛擬化仍然要求電腦支援虛擬化技術。
在KVM中,可以執行各種未更改的GNU/Linux, Windows 或任何其他系統鏡像。(請看[1]),每個虛擬機器都可提供獨享的虛擬硬體:網路卡,硬碟,顯卡等。请看 KVM Howto
KVM, Xen, VMware, 和 QEMU 的不同之處,請看此處KVM FAQ.
Contents
檢查您的電腦是否支援KVM
硬體支援
KVM需要宿主(host)機的處理器支援虛擬化(對於Intel CPU來說是VT-x,對於AMD CPU來說是AMD-V)。你可以通過lscpu命令來檢查你的CPU是否支援虛擬化:
$ lscpu
如果你的CPU支援虛擬化,輸出結果中會有一行Virtualization的諮詢。
你也可以執行:
$ grep -E "(vmx|svm)" --color=always /proc/cpuinfo
如果執行後沒有結果,那麼你的CPU不支援硬體虛擬化,你不能使用KVM。
內核支援
Arch Linux的內核提供了相應的內核模組來支援KVM和VirtIO。
KVM模組
如果你的內核是用 CONFIG_IKCONFIG_PROC 這個選項編譯的話,你可以通過以下命令檢查你的內核是否已經包含了支援KVM所必須的模組(kvm
及kvm_amd
与kvm_intel
這両者中的任意一個):
$ zgrep KVM /proc/config.gz
如果模組設定不等於 y
或m
,則該模組不可用
Para-virtualized devices
Para-virtualization provides a fast and efficient means of communication for guests to use devices on the host machine. KVM provides para-virtualized devices to virtual machines using the Virtio API as a layer between the hypervisor and guest.
All virtio devices have two parts: the host device and the guest driver.
VirtIO modules
Use the following command to check if needed modules are available:
$ zgrep VIRTIO /proc/config.gz
Loading kernel modules
First, check if the kernel modules are automatically loaded. This should be the case with recent versions of udev.
$ lsmod | grep kvm $ lsmod | grep virtio
In case the above commands return nothing, you need to load[broken link: invalid section] kernel modules.
kvm_intel
or kvm_amd
fails but modprobing kvm
succeeds, (and lscpu
claims that hardware acceleration is supported), check your BIOS settings. Some vendors (especially laptop vendors) disable these processor extensions by default. To determine whether there's no hardware support or there is but the extensions are disabled in BIOS, the output from dmesg
after having failed to modprobe will tell.List of para-virtualized devices
- network device (virtio-net)
- block device (virtio-blk)
- controller device (virtio-scsi)
- serial device (virtio-serial)
- balloon device (virtio-balloon)
如何使用KVM
必須使用QEMU或Libvirt作為KVM的控制界面和前端,請參考Libvirt (正體中文)條目和QEMU條目。
小貼士与小技巧
嵌套虛擬化
在宿主機啟用kvm_intel
模組的嵌套虛擬化機能:
# modprobe -r kvm_intel # modprobe kvm_intel nested=1
使嵌套虛擬化永久生效(請參考Kernel modules#Setting module options):
/etc/modprobe.d/modprobe.conf
options kvm_intel nested=1
檢驗嵌套虛擬化功能是否已被激活:
$ systool -m kvm_intel -v | grep nested
nested = "Y"
使用以下命令來執行guest虛擬機器:
$ qemu-system-x86_64 -enable-kvm -cpu host
啟動虛擬機並檢查是否有vmx標誌:
$ grep vmx /proc/cpuinfo