KVM
KVM,基于内核的虚拟机,是内置于 Linux 内核的 hypervisor。在功能上类似于 Xen,但运行起来更简便。与使用模拟的原生 QEMU 不同的是,KVM 是 QEMU 的一种特殊的运行模式,这种模式通过内核模块使用 CPU 扩展(HVM)进行虚拟化。
通过 KVM,可以运行多台未修改的 GNU/Linux、Windows 或任何其他操作系统的虚拟机(详情请参阅客户机支持状态)。每台虚拟机都有独享的虚拟硬件:网卡、硬盘、显卡等。
有关 KVM 与 Xen、VMware 和 QEMU 的区别,可查看 KVM FAQ。
This article does not cover features common to multiple emulators using KVM as a backend. 对于这些信息,你应查看相关的文档。
检查 KVM 支持[编辑 | 编辑源代码]
硬件支持[编辑 | 编辑源代码]
KVM 需要虚拟机宿主的处理器支持虚拟化(对于 Intel 处理器来说是 VT-x,对于 AMD 处理器来说是 AMD-V)。你可通过以下命令来检查你的处理器是否支持硬件虚拟化:
$ LC_ALL=C lscpu | grep Virtualization
或者:
$ grep -E --color=auto 'vmx|svm|0xc0f' /proc/cpuinfo
如果运行后没有显示,那么你的处理器不支持硬件虚拟化,你不能使用KVM。
内核支持[编辑 | 编辑源代码]
Arch Linux 内核提供了相应的内核模块来支持KVM。
- 你可以通过以下命令来检查内核中是否已包含必要的模块(
kvm
以及kvm_amd
和kvm_intel
中的一个):
$ zgrep CONFIG_KVM /proc/config.gz
只有当模块设置为 y
或 m
时才可用。
- 然后,确认这些内核模块已自动加载:
$ lsmod | grep kvm
kvm_intel 245760 0 kvmgt 28672 0 mdev 20480 2 kvmgt,vfio_mdev vfio 32768 3 kvmgt,vfio_mdev,vfio_iommu_type1 kvm 737280 2 kvmgt,kvm_intel irqbypass 16384 1 kvm
如果运行后没有显示,那么需要手动加载这些模块。
kvm_intel
或 kvm_amd
失败,但 modprobe kvm
成功,并且 lscpu
声称支持硬件加速,检查 BIOS 设置。某些厂商,特别是笔记本电脑厂商,默认禁用这些处理器扩展。modprobe 失败后,dmesg 的输出可以告诉你这些扩展是硬件不支持还是在 BIOS 中禁用。Virtio 准虚拟化[编辑 | 编辑源代码]
准虚拟化为客户机提供了一种使用主机上设备的快速有效的通信方式。KVM 使用 Virtio API 作为虚拟机管理程序和客户机之间的连接层,为虚拟机提供准虚拟化设备。
所有 Virtio 设备都包括两部分:主机设备和客户机驱动程序。
内核支持[编辑 | 编辑源代码]
用以下命令检查虚拟机中内核的 VIRTIO 模块是否可用:
$ zgrep VIRTIO /proc/config.gz
然后,检查这些内核模块是否已自动加载:
$ lsmod | grep virtio
如果运行后没有显示,那么需要手动加载这些模块。
准虚拟化设备列表[编辑 | 编辑源代码]
- 网络设备 (virtio-net)
- 块设备 (virtio-blk)
- 控制器设备 (virtio-scsi)
- 串口设备 (virtio-serial)
- 气球设备 (virtio-balloon)
如何使用 KVM[编辑 | 编辑源代码]
请参考QEMU。
小贴士与小技巧[编辑 | 编辑源代码]
嵌套虚拟化[编辑 | 编辑源代码]
Nested virtualization enables existing virtual machines to be run on third-party hypervisors and on other clouds without any modifications to the original virtual machines or their networking.
在宿主机上,启用 kvm_intel
模块的嵌套虚拟化功能:
intel
替换为 amd
# modprobe -r kvm_intel # modprobe kvm_intel nested=1
要使嵌套虚拟化永久生效(请参考 Kernel modules#Setting module options):
/etc/modprobe.d/kvm_intel.conf
options kvm_intel nested=1
确认嵌套虚拟化功能是否已激活:
$ systool -m kvm_intel -v | grep nested
nested = "Y"
Enable the "host passthrough" mode to forward all CPU features to the guest system:
- If using QEMU, run the guest virtual machine with the following command:
qemu-system-x86_64 -enable-kvm -cpu host
. - If using virt-manager, change the CPU model to
host-passthrough
. - If using virsh, use
virsh edit vm-name
and change the CPU line to<cpu mode='host-passthrough' check='partial'/>
Boot VM and check if vmx flag is present:
$ grep -E --color=auto 'vmx|svm' /proc/cpuinfo
Enabling huge pages[编辑 | 编辑源代码]
You may also want to enable hugepages to improve the performance of your virtual machine.
With an up to date Arch Linux and a running KVM you probably already have everything you need. Check if you have the directory /dev/hugepages
. If not, create it.
Now we need the right permissions to use this directory. The default permission is root's uid and gid with 0755, but we want anyone in the kvm group to have access to hugepages.
Add to your /etc/fstab
:
hugetlbfs /dev/hugepages hugetlbfs mode=01770,gid=78 0 0
Of course the gid must match that of the kvm
group or specify the group name directly with gid=kvm
. The mode of 1770
allows anyone in the group to create files but not unlink or rename each other's files. Make sure /dev/hugepages
is mounted properly:
# umount /dev/hugepages # mount /dev/hugepages $ mount | grep huge
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,mode=1770,gid=78)
Now you can calculate how many hugepages you need. Check how large your hugepages are:
$ grep Hugepagesize /proc/meminfo
Normally that should be 2048 kB ≙ 2 MB. Let us say you want to run your virtual machine with 1024 MB. 1024 / 2 = 512. Add a few extra so we can round this up to 550. Now tell your machine how many hugepages you want:
# echo 550 > /proc/sys/vm/nr_hugepages
If you had enough free memory you should see:
$ grep HugePages_Total /proc/meminfo
HugesPages_Total: 550
If the number is smaller, close some applications or start your virtual machine with less memory (number_of_pages x 2):
$ qemu-system-x86_64 -enable-kvm -m 1024 -mem-path /dev/hugepages -hda <disk_image> [...]
注意 -mem-path
参数。This will make use of the hugepages.
Now you can check, while your virtual machine is running, how many pages are used:
$ grep HugePages /proc/meminfo
HugePages_Total: 550 HugePages_Free: 48 HugePages_Rsvd: 6 HugePages_Surp: 0
Now that everything seems to work you can enable hugepages by default if you like. Add to your /etc/sysctl.d/40-hugepage.conf
:
vm.nr_hugepages = 550
参阅: