Virtualization can be defined as a way to run multiple operating systems(Linux, Windows, Unix, e.t.c) on the same physical hardware/computer server. The Virtualization software is responsible for the abstraction between the actual hardware and an instance of operating system being virtualized. Some common type 2 Virtualization softwares available for Linux users are VirtualBox, KVM, Qemu and VMware Player/Workstation.
In a Virtual Machine, you’re able to install and run any desktop and server applications meant for a different operating system without any issues since they are purely independent. The only limitation is running Virtualization within a Virtual Machine. This is where Nested Virtualization come in.
The Linux KVM (Kernel-based Virtual Machine) is an open source virtualization software for Linux systems. It can be installed on any system that runs Linux kernel to turn your computer into an Hypervisor.
What is nested Virtualization?
Nested virtualization enables you to run a virtual machine (VM) inside another Virtual Machine (Nested) with the same hardware acceleration from the host system. This is helpful for running applications such as Visual Studio phone emulator in a virtual machine, or testing configurations that ordinarily require several hosts.
Enable Nested Virtualization on KVM / Qemu
Before we can enable Nested Virtualization on KVM hypervisor host, we need to ensure the following requirements are met;
- Have installed and configured KVM virtualization – Ubuntu / Debian / CentOS / Oracle Linux
- Confirm KVM Kernel modules are loaded.
$ lsmod | grep kvm
kvm_intel 315392 60
kvm 847872 1 kvm_intel
irqbypass 16384 44 kvm
- Virtual Machine installed on KVM for testing nested Virtualization
$ virsh list
Id Name State
--------------------------------------
1 Fedora-38 running
- Access to virsh, Virt-viewer or WebVirtCloud for editing Virtual Machine xml file.
Enable nested Virtualization on KVM
You can check the following files if nested virtualization is supported:
- Intel processors:
$ cat /sys/module/kvm_intel/parameters/nested
1
If you see 1 or Y, it means nested virtualization is supported; if you see 0 or N, then nested virtualization is not supported in your system.
- AMD processors:
cat /sys/module/kvm_amd/parameters/nested
To enable nested virtualization on your KVM host if the results were N or 0, edit KVM modules loading file:
sudo vim /etc/modprobe.d/kvm.conf
Set like below for Intel processors:
options kvm_intel nested=1
For AMD Processors:
options kvm_amd nested=1
Shut down all running Virtual Machines on your host and reload the kernel module for KVM. Or optionally reboot the machine.
sudo modprobe -r kvm_intel
sudo modprobe kvm_intel
Confirm the setting after:
cat /sys/module/kvm_intel/parameters/nested
Edit configuration of Virtual Machine
Shutdown your Virtual Machine:
$ virsh shutdown Fedora-38
Domain Fedora-38 is being shutdown
$ virsh list --all | grep Fedora-38
- Fedora-38 shut off
Edit the configuration of your virtual machine to enabled Virtualization in it.
$ virsh edit Fedora-38
Update the [cpu mode] setting to host-model:
<cpu mode='host-model' check='partial'/>
You can also set to host-passthrough:
<cpu mode='host-passthrough'/>
On virt-manager.
Click on the Virtual Machine > Edit > Show virtual hardware details > CPUs > Configuration. Use either:
Type Model: host-passthrough
# OR
Select Copy host CPU configuration in check box
To understand more about CPU mode check the following libvirt documentation.
Test Virtualization on Guest OS.
Start the Virtual Machine.
$ virsh start Fedora-38
Domain Fedora-38 started
We’ll install KVM Virtualization stack on the Fedora OS.
sudo dnf -y install qemu-kvm libvirt virt-install
sudo dnf -y install guestfs-tools virt-top
sudo systemctl enable --now libvirtd
Creating Fedora 38 VM.
We’ll try to create another instance of Fedora on the host.
List available OS templates
sudo virt-builder -l
Create Fedora image
sudo virt-builder fedora-38 --format qcow2 --size 10G -o /var/lib/libvirt/images/fedora-38.qcow2 --root-password password:RootPassw0rd
Command execution output:
[ 0.7] Downloading: http://builder.libguestfs.org/fedora-38.xz
############################################################################################################################################################################################### 100.0%
[ 16.2] Planning how to build this image
[ 16.2] Uncompressing
[ 33.6] Resizing (using virt-resize) to expand the disk to 10.0G
Create Virtual Machine from image created
sudo virt-install \
--name fedora-38 \
--os-variant fedora38 \
--vcpus 1 \
--ram 1024 \
--disk path=/var/lib/libvirt/images/fedora-38.qcow2 \
--network bridge=virbr0 \
--graphics none \
--noautoconsole \
--boot hd \
--noreboot \
--import
VM importation should be successful within a few minutes:
Starting install...
Domain creation completed.
You can restart your domain by running:
virsh --connect qemu:///system start fedora-38
We’ve enabled Nested Virtualization feature on our KVM host and tested with virtual machine creation. From this point your Virtual machine can be used to run other instances. Just ensure it has enough compute resources – Memory, CPU and disk allocation.
More guides on Virtualization;