Kernel-based Virtual Machine (KVM) has become the defacto hypervisor on GNU/Linux systems it works with a great performance as it utilizes the CPU virtualization extensions Intel VT-x or AMD-V). KVM doesn’t emulate hardware but uses QEMU for this.
Nested Virtual guest
It’s possible to use nested virtualization this makes it possible to run a hypervisor inside a KVM virtual machine.
Verify
To verify if nested virtualization is enabled on your system can check /sys/module/kvm_intel/parameters/nested on Intal systems or /sys/module/kvm_amd/parameters/nested
[staf@ak ~]$ cat /sys/module/kvm_intel/parameters/nested
N
[staf@ak ~]$
Enable
Shutdown all virtual machines
Make sure that there no virtual machines running.
[root@ak ~]# virsh
Welcome to virsh, the virtualization interactive terminal.
Type: 'help' for help with commands
'quit' to quit
virsh # list
Id Name State
----------------------------------------------------
virsh #
Unload KVM
Unload the KVM kernel module.
[root@ak ~]# modprobe -r kvm_intel
[root@ak ~]#
Load KVM and activate nested
Reload the KVM with the nested feature enabled.
[root@ak ~]# modprobe kvm_intel nested=1
[root@ak ~]#
Verify
[root@ak ~]# cat /sys/module/kvm_intel/parameters/nested
Y
[root@ak ~]#
To enable the nested feature permanently create /etc/modprobe.d/kvm_intel.conf
[root@ak ~]# vi /etc/modprobe.d/kvm_intel.conf
and enable the nested option.
options kvm_intel nested=1
Enabling nested virtualization in the virtual machine
When you logon to a virtual machine and verify the virtualization extensions on the cpu the flags aren’t available.
[staf@centos7 ~]$ cat /proc/cpuinfo | grep -i -E "vmx|svm"
[staf@centos7 ~]$
To enable nested virtualization in a vritual machine you can
- start
virshand and edit the the virtual machine and change the CPU line to<cpu mode='host-model' check='partial'/> - Open virt-manager and select Copy host CPU configuration on the CPU configuration
root@ak ~]# virsh
Welcome to virsh, the virtualization interactive terminal.
Type: 'help' for help with commands
'quit' to quit
virsh # list
Id Name State
----------------------------------------------------
1 centos7.0 running
virsh # edit centos7.0
Change the cpu settings
<features>
<acpi/>
<apic/>
<vmport state='off'/>
</features>
<cpu mode='host-model' check='partial'>
<model fallback='allow'/>
</cpu>
Shutdown the virtual machine
virsh # reboot centos7.0
Domain centos7.0 is being rebooted
virsh #
Start the virtual machine
virsh # start centos7.0
Domain centos7.0 started
While saving the virsh domain xml you might get an error as:
Extra element cpu in interleave
Press i for ignore and start the domain.
Logon to the virtual machine and verify the cpu flags;
[staf@centos7 ~]$ cat /proc/cpuinfo | grep -i vmx
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology eagerfpu pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt ibpb ibrs arat spec_ctrl
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology eagerfpu pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt ibpb ibrs arat spec_ctrl
[staf@centos7 ~]$ cat /proc/cpuinfo | grep -i "vmx|svm"
[staf@centos7 ~]$ cat /proc/cpuinfo | grep -i -E "vmx|svm"
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology eagerfpu pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt ibpb ibrs arat spec_ctrl
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology eagerfpu pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt ibpb ibrs arat spec_ctrl