目前公司的测试环境使用Proxmox VE(PVE),PVE虚拟出来的主机CPU默认不支持vmx,即不支持嵌套虚拟化,在虚拟机中使用egrep “vmx|svm” /proc/cpuinfo验证,无输出,那么如何让他支持呢?其实PVE的内核还是采用了KVM+Qemu的方式模拟,那么参照如何让KVM支持嵌套虚拟化的方法操作,开启nested即可

nested是一个可通过内核参数来启用的功能。它能够使一台虚拟机具有物理机CPU特性,支持vmx或者svm(AMD)硬件虚拟化。Proxmox VE是运行于Debian操作系统上的,也是支持嵌套式虚拟nested的,但默认情况下并未开启。开启nested很容易,我们可以通过命令检验嵌套虚拟化功能是否已被开启(实验环境,PVE4.4):

cat /sys/module/kvm_intel/parameters/nested
N

可见默认状态下是未开启的,下面让我们来打开嵌套虚拟化,首先需要关闭所有虚拟机

列出所有虚拟机:

qm list

关闭虚拟机:

qm stop <vmid>

以上操作也可以在PVE网页控制台操作

开启内核支持:

modprobe -r kvm_intel  
modprobe kvm_intel nested=1

执行modprobe -r kvm_intel时如果报错Module kvm_intel is in use,请检查你的虚拟机是否全部关闭

现在再看看nested是否已开启:

cat /sys/module/kvm_intel/parameters/nested
Y

编辑配置文件:

echo "options kvm_intel nested=1" >> /etc/modprobe.d/modprobe.conf

这样系统重启也会自动加载netsted

查看虚拟机启动命令行,下面以我的环境中vmid为100的虚拟机为列:

# qm showcmd 100
/usr/bin/kvm -id 100 -chardev socket,id=qmp,path=/var/run/qemu-server/100.qmp,server,nowait -mon chardev=qmp,mode=control -pidfile /var/run/qemu-server/100.pid -daemonize -smbios type=1,uuid=a1f4fefc-3d14-41c7-aeb1-bf9b79ef03df -name ops-test-172.16.30.20 -smp 2,sockets=1,cores=2,maxcpus=2 -nodefaults -boot menu=on,strict=on,reboot-timeout=1000,splash=/usr/share/qemu-server/bootsplash.jpg -vga cirrus -vnc unix:/var/run/qemu-server/100.vnc,x509,password -cpu kvm64,+lahf_lm,+sep,+kvm_pv_unhalt,+kvm_pv_eoi,enforce -m 4096 -k en-us -device pci-bridge,id=pci.1,chassis_nr=1,bus=pci.0,addr=0x1e -device pci-bridge,id=pci.2,chassis_nr=2,bus=pci.0,addr=0x1f -device piix3-usb-uhci,id=uhci,bus=pci.0,addr=0x1.0x2 -device usb-tablet,id=tablet,bus=uhci.0,port=1 -device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x3 -iscsi initiator-name=iqn.1993-08.org.debian:01:6775cf970ba -drive file=/var/lib/vz/template/iso/CentOS-7-x86_64-DVD-1611.iso,if=none,id=drive-ide2,media=cdrom,aio=threads -device ide-cd,bus=ide.1,unit=0,drive=drive-ide2,id=ide2,bootindex=200 -device virtio-scsi-pci,id=scsihw0,bus=pci.0,addr=0x5 -drive file=/dev/pve/vm-100-disk-1,if=none,id=drive-scsi0,format=raw,cache=none,aio=native,detect-zeroes=on -device scsi-hd,bus=scsihw0.0,channel=0,scsi-id=0,lun=0,drive=drive-scsi0,id=scsi0,bootindex=100 -netdev type=tap,id=net0,ifname=tap100i0,script=/var/lib/qemu-server/pve-bridge,downscript=/var/lib/qemu-server/pve-bridgedown,vhost=on -device virtio-net-pci,mac=D2:15:25:81:73:BD,netdev=net0,bus=pci.0,addr=0x12,id=net0,bootindex=300

找到-cpu kvm64,+lahf_lm,+sep,+kvm_pv_unhalt,+kvm_pv_eoi,enforce

在后面加上+vmx,表示开启vmx

-cpu kvm64,+lahf_lm,+sep,+kvm_pv_unhalt,+kvm_pv_eoi,+vmx,enforce

关闭虚拟机:

qm stop 100

重新启动虚拟机:

/usr/bin/kvm -id 100 -chardev socket,id=qmp,path=/var/run/qemu-server/100.qmp,server,nowait -mon chardev=qmp,mode=control -pidfile /var/run/qemu-server/100.pid -daemonize -smbios type=1,uuid=a1f4fefc-3d14-41c7-aeb1-bf9b79ef03df -name ops-test-172.16.30.20 -smp 2,sockets=1,cores=2,maxcpus=2 -nodefaults -boot menu=on,strict=on,reboot-timeout=1000,splash=/usr/share/qemu-server/bootsplash.jpg -vga cirrus -vnc unix:/var/run/qemu-server/100.vnc,x509,password -cpu kvm64,+lahf_lm,+sep,+kvm_pv_unhalt,+kvm_pv_eoi,+vmx,enforce -m 4096 -k en-us -device pci-bridge,id=pci.1,chassis_nr=1,bus=pci.0,addr=0x1e -device pci-bridge,id=pci.2,chassis_nr=2,bus=pci.0,addr=0x1f -device piix3-usb-uhci,id=uhci,bus=pci.0,addr=0x1.0x2 -device usb-tablet,id=tablet,bus=uhci.0,port=1 -device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x3 -iscsi initiator-name=iqn.1993-08.org.debian:01:6775cf970ba -drive file=/var/lib/vz/template/iso/CentOS-7-x86_64-DVD-1611.iso,if=none,id=drive-ide2,media=cdrom,aio=threads -device ide-cd,bus=ide.1,unit=0,drive=drive-ide2,id=ide2,bootindex=200 -device virtio-scsi-pci,id=scsihw0,bus=pci.0,addr=0x5 -drive file=/dev/pve/vm-100-disk-1,if=none,id=drive-scsi0,format=raw,cache=none,aio=native,detect-zeroes=on -device scsi-hd,bus=scsihw0.0,channel=0,scsi-id=0,lun=0,drive=drive-scsi0,id=scsi0,bootindex=100 -netdev type=tap,id=net0,ifname=tap100i0,script=/var/lib/qemu-server/pve-bridge,downscript=/var/lib/qemu-server/pve-bridgedown,vhost=on -device virtio-net-pci,mac=D2:15:25:81:73:BD,netdev=net0,bus=pci.0,addr=0x12,id=net0,bootindex=300

列出虚拟机:

qm list

发现vmid为100的虚拟机状态已变更为running

进入该虚拟机,使用命令验证是否已开启vmx:

#egrep "vmx|svm" /proc/cpuinfo 
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx lm constant_tsc nopl pni vmx cx16 x2apic hypervisor lahf_lm tpr_shadow vnmi flexpriority ept vpid
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx lm constant_tsc nopl pni vmx cx16 x2apic hypervisor lahf_lm tpr_shadow vnmi flexpriority ept vpid

至此,成功在PVE中开启了嵌套虚拟化

https://blog.51cto.com/kusorz/1925172