Nested virtualization is very important feature if you’re using virtual machines for your daily tasks. If you’ve heard but not know much about it this blog post is for you.
You’ve probably watched the movie “Inception”. In this movie you dream in an other dream. This is a nested dream. Virtualization can also be nested like this. You have virtual machine but within the virtual machine you may have another virtual machine. This is not an easy thing (just like dream) since your CPU architecture and hypervisor must support this feature.
So we need to check 2 important features first:
- Does the CPU supports nested virtualization
- Does the Hypervisor supports nested virtualization
To check CPU to support it, you can simply run the following command (in linux of course), if you’re looking nested virtualization for Windows forgive my explanation that comes this much late.
For intel processors:
cat /sys/module/kvm_intel/parameters/nested
For AMD processors:
cat /sys/module/kvm_amd/parameters/nested
If you get “Y” in output you’re good to go.
Good news is KVM supports nested virtualization. Bad new is VirtualBox does NOT. To enable nested virtualization, virtual machnine needs to see some particular CPU flags enabled. So, simply enable check box “Copy host CPU configuration” in the virt-manager gui’s CPU settings. This will copy all CPU flags to virtual machine. If you’re having a fresh install to virtual machine this is good, but if you have an existing image, enabling this may result some undetermined behaviours, because image can contain some instructions that are not found in the current CPU configuration.
What happens if your CPU or Hypervisor does not support nested virtualization?
You will find your self in emulated, or very slow virtual machine.
Remember not two hypervisor can use the virtualization at the same time. So, KVM and VirtualBox can not run virtual machines at the same time. You can use only one at a time.
Nowadays, Android emulator also uses KVM as hypervisor. So, if you’ve ever tried to use VirtualBox while android emulator is running you’ve probably failed. This is the reason behind it.
So with this option enabled you can develop Android using its emulator within a virtual machine.
Some very critical configuration option to developers:
spice-vdaagent is very good tool (better than build in virt-manager python spice client gui) to use with VMs. Because it supports auto scale to real resolution in full-screen and custom screen sizes, and it supports multiple displays.
Last but not the least, devstack can be used with nested virtualization which can boost your devstack performance.