KVM (简体中文)
KVM(Kernel-based Virtual Machine的英文缩写)是内核内建的虚拟机。有点类似于 Xen ,但更追求更简便的运作,比如运行此虚拟机,仅需要加载相应的 kvm
模块即可后台待命。和 Xen 的完整模拟不同的是,KVM 需要芯片支持虚拟化技术(英特尔的 VT 扩展或者 AMD 的 AMD-V 扩展)。
在KVM中,可以运行各种未更改的GNU/Linux, Windows 或任何其他系统镜像。(请看客户机支持状态),每个虚拟机都可提供独享的虚拟硬件:网卡,硬盘,显卡等。请看 KVM Howto
KVM, Xen, VMware, 和 QEMU 的不同,请看这里KVM FAQ.
Contents
检查是否支持KVM
硬件支持
KVM需要虚拟机宿主(host)的处理器带有虚拟化支持(对于Intel处理器来说是VT-x,对于AMD处理器来说是AMD-V)。你可以通过以下命令来检查你的处理器是否支持虚拟化:
$ lscpu
如果你的处理器支持虚拟化,输出结果中会有一行Virtualization的信息。
你也可以运行:
$ grep -E "(vmx|svm)" --color=always /proc/cpuinfo
如果运行后没有显示,那么你的处理器不支持硬件虚拟化,你不能使用KVM。
内核支持
Arch Linux的内核提供了相应的内核模块来支持KVM和VIRTIO
KVM模块
如果你的内核是用 CONFIG_IKCONFIG_PROC 这个选项编译的话,你可以通过以下命令来检查你的内核是否已经包含了支持虚拟化所必须的模块(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条目。
小贴士与小技巧
嵌套虚拟化
在宿主机启用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