Skip to content
systemcoding.dev
Virtualization

Run KVM Inside a VM With Nested Virtualization

Enable nested virtualization on a KVM host, pass the vmx or svm flag to a guest with -cpu host and start a second-level VM, with the checks that prove it works.

6 min read
Summarize in AI
Flat illustration of guest machines running on a hypervisor over hardware, with the QEMU, Docker and RISC-V logos.

Sometimes the machine in front of you is already a virtual machine, and you still need a hypervisor on it: a CI runner that boots QEMU guests, a cloud instance for kernel testing, a laptop VM used for a lab. That is nested virtualization, and it does not work by default in every setup. The outer hypervisor has to expose the CPU's virtualization feature to its guest and emulate the instructions that guest uses to run its own guests. This post shows how to enable it on a Linux KVM host (Linux 6.12, QEMU 9.2), how to prove it works from inside and what to expect when it does not. The general model is in the complete guide to virtualization for systems programmers.

Terms: L0, L1 and L2

Three layers are involved, and the names keep a confusing setup readable.

  • L0 is the physical host and its hypervisor. It owns the real hardware.

  • L1 is the guest that L0 runs. It will itself be a hypervisor.

  • L2 is the guest that L1 runs, the nested one.

The processor only knows one level of virtualization. When L2 does something that causes an exit, the hardware transfers control to L0, not to L1. L0 has to work out that the exit belongs to L1's hypervisor and forward it, building the illusion that L1 received the exit directly. To manage that, L0 keeps a shadow of the control structure L1 maintains for L2, merging the settings L1 requested with L0's own. That bookkeeping is why nested guests are slower than first-level ones: an L2 exit can cost several L0 operations.

Step 1: enable it on L0

On Intel, the kvm_intel module has a nested parameter. Check it on the host:

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

Recent kernels default this to Y. If it prints N or 0, turn it on by reloading the module with no guests running:

Shell
sudo modprobe -r kvm_intel
sudo modprobe kvm_intel nested=1

To make it permanent, write a modprobe configuration:

Text
options kvm_intel nested=1

Save that line in /etc/modprobe.d/kvm-nested.conf. On AMD the module is kvm_amd and the parameter is also nested; the same commands apply with the names swapped. If modprobe -r fails with Module kvm_intel is in use, a guest is still running, and you must shut it down first.

Step 2: start L1 with the feature exposed

The flag has to reach the guest. QEMU's default CPU model, qemu64, does not include vmx, so a guest started without -cpu never sees it. Use -cpu host to pass the host's features through:

Shell
qemu-system-x86_64 -enable-kvm -cpu host -smp 4 -m 4G -nographic \
  -drive file=l1.qcow2,if=virtio,format=qcow2 \
  -netdev user,id=n0,hostfwd=tcp::2222-:22 -device virtio-net-pci,netdev=n0

-cpu host ties the guest to the host's CPU model, which matters if you plan to migrate L1 later. If you need a stable model, name one and add the feature, for example -cpu Skylake-Server,+vmx, using a model your host actually supports. If you manage guests with libvirt, the equivalent is <cpu mode='host-passthrough'/> in the domain XML.

Step 3: check from inside L1

Log in to the L1 guest and run the checks you would run on any machine:

Shell
grep -c -w vmx /proc/cpuinfo
ls -l /dev/kvm
Text
4
crw-rw---- 1 root kvm 10, 232 Nov 23 10:02 /dev/kvm

The count equals the number of vCPUs, here four, and /dev/kvm exists because the L1 kernel loaded kvm_intel and found VMX. If the count is 0, the flag did not reach L1, which almost always means -cpu host is missing or L0's nested parameter is off. If the count is non-zero but /dev/kvm is missing, check sudo dmesg | grep -i kvm in L1 for the reason.

You can also ask lscpu inside L1. It will print both the virtualization feature and the hypervisor vendor, which is the visible sign of nesting:

Shell
lscpu | grep -E 'Virtualization|Hypervisor vendor'
Text
Virtualization:                  VT-x
Hypervisor vendor:               KVM

Step 4: start L2

Inside L1, run QEMU exactly as on any host. With the L1 guest ready, start a small second-level guest from a kernel and root file system:

Shell
qemu-system-x86_64 -enable-kvm -cpu host -m 512M -nographic \
  -kernel /boot/vmlinuz-6.12 -initrd /boot/initrd.img-6.12 \
  -append "console=ttyS0 quiet"

If -enable-kvm succeeds, QEMU starts silently and the L2 kernel boots on the serial console. If the L2 guest starts and boots much more slowly than an L1 guest, that is the expected cost of nesting, not a failure. If QEMU prints Could not access KVM kernel module: No such file or directory, L1 has no /dev/kvm, and you go back to step 3.

What to check when it fails

SymptomLikely causeFix
vmx count is 0 in L1Flag not exposedStart L1 with -cpu host, enable nested on L0
/dev/kvm missing in L1Module not loaded or failedmodprobe kvm_intel and read dmesg
kvm: disabled by bios in L1Outer hypervisor hides itUse a provider or instance type with nesting enabled
L2 hangs or crashes at bootUnsupported feature combinationTry a plain -cpu model, update kernel and QEMU
Live migration of L1 failsNested state not migratable on older versionsAvoid migrating L1 with running L2s

On public clouds, nesting is a provider feature and not something you control from inside the instance. Some providers offer it on selected instance families, and others only on bare-metal types. Check the provider's documentation for that exact instance type before you plan around it. Do not assume that because the physical CPU supports VT-x, your instance can use it.

Performance and what nesting is good for

Nested virtualization is for development and testing, not for performance. Every L2 exit goes to L0 first and then possibly to L1, and L0 must synthesize the state that L1 expects. Workloads that rarely exit, such as compute loops, run at close to native speed, and workloads that exit often, such as many small I/O operations, slow down a lot. We avoid quoting a ratio, because it depends on the CPU generation and the kernel version. Measure with your own workload.

Good uses: testing a hypervisor or VMM you are writing inside a disposable VM, running a CI job that boots QEMU guests on a hosted runner, learning KVM on a machine where you cannot touch the physical host, and reproducing a customer's setup. Poor uses: production database hosts, latency-sensitive services and anything where you could place the workload in a container or on bare metal instead.

A note on safety

An L2 guest escaping to L1 gets L1's privileges, and an L1 compromise reaches L0 only if L0 has a bug. Nesting also widens L0's attack surface, because L0 must emulate hypervisor instructions for L1, and that emulation is code that has had bugs. If you run untrusted workloads, think carefully before enabling nested virtualization for them.

Share

Keep reading

Flat illustration of user space above the system call boundary, the kernel and the hardware, with the C, FreeBSD and Linux logos.
Systems Programming

How argc and argv Reach main in a C Program

The kernel copies argc and argv onto the new process's stack on execve, then startup code hands them to main. See the layout and measure it with a program.

6 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.