Skip to content

Nested virtualization with KVM — running a hypervisor inside a guest

How to enable nested KVM on Intel and AMD, the performance cost, and the scenarios where running a hypervisor inside a guest is the right call.

Nested virtualization is the ability to run a hypervisor inside a guest — KVM-in-KVM, ESXi-in-KVM, Hyper-V-in-KVM. The use cases are narrow but real: lab work that needs to test cluster behaviour without renting a second physical host, CI that builds a VM image and boots it for an integration test, training environments where students need their own private hypervisor. This article is the enablement procedure on Intel and AMD, the cost to expect, and the way an operator validates the result.

How to verify

Confirm the host supports nesting (every modern Intel and AMD CPU does at the silicon level) and that the kernel module is configured to expose it.

# Host check
egrep -c '(vmx|svm)' /proc/cpuinfo
cat /sys/module/kvm_intel/parameters/nested 2>/dev/null   # 'Y' or '1' = enabled
cat /sys/module/kvm_amd/parameters/nested 2>/dev/null

# After exposing to a guest, confirm in the guest:
egrep -c '(vmx|svm)' /proc/cpuinfo
ls /dev/kvm
sudo modprobe kvm_intel || sudo modprobe kvm_amd

If the guest’s /proc/cpuinfo lacks vmx or svm, the host kernel module hasn’t exposed nesting to that guest — verify the host parameter, then check that the guest XML sets a CPU mode that passes the virtualization features through.

What’s happening

Nested KVM works in two places. On the host, the kvm_intel or kvm_amd kernel module has a nested=Y parameter — when set, the host KVM exposes the VMX (Intel) or SVM (AMD) extensions to guests. Without that, no amount of guest config will expose virtualization. On the guest, the libvirt domain XML must select a CPU mode that passes the virtualization feature flags through; the cleanest way is <cpu mode='host-passthrough'> which exposes the physical CPU’s full feature set.

When the inner hypervisor (the L1 guest running KVM/ESXi/Hyper-V) issues a VMX/SVM instruction, the L0 (outer) hypervisor traps it and emulates the semantics. Modern Intel and AMD silicon has hardware help for this — “VMCS shadowing” on Intel, “AMD nested paging” on AMD — which makes nested performance acceptable but not free. Expect L2 guests to run 10-25% slower than the same workload on L1, depending on how I/O-bound it is.

The procedure

  1. Enable on the host kernel module. The simplest path is a modprobe config file that survives reboots:

    # Intel
    sudo tee /etc/modprobe.d/kvm-nested.conf <<'EOF'
    options kvm_intel nested=1
    EOF
    sudo modprobe -r kvm_intel
    sudo modprobe kvm_intel
    cat /sys/module/kvm_intel/parameters/nested
    
    # AMD
    sudo tee /etc/modprobe.d/kvm-nested.conf <<'EOF'
    options kvm_amd nested=1
    EOF
    sudo modprobe -r kvm_amd
    sudo modprobe kvm_amd

    modprobe -r requires no guests be running — schedule the change.

  2. Configure the guest XML to pass through. Edit the L1 guest’s libvirt XML so the CPU mode is host-passthrough (or host-model with vmx/svm explicitly required):

    <cpu mode='host-passthrough' check='partial'/>

    Or the more controlled variant:

    <cpu mode='custom' match='exact'>
      <model fallback='allow'>Haswell-noTSX</model>
      <feature policy='require' name='vmx'/>
    </cpu>

    See libvirt domain XML schema for the surrounding context.

  3. Restart the L1 guest and confirm. Inside the guest:

    sudo systemctl stop libvirtd 2>/dev/null
    sudo shutdown -r now
    # After reboot:
    egrep -c '(vmx|svm)' /proc/cpuinfo   # > 0 = good
    ls /dev/kvm                          # exists
    sudo modprobe kvm_intel || sudo modprobe kvm_amd
  4. Install a hypervisor inside the L1 guest. From here, you can install libvirt/qemu-kvm in the L1 guest and create L2 guests inside it, exactly as if it were a bare-metal host:

    sudo apt install -y qemu-kvm libvirt-daemon-system virtinst
    sudo virsh -c qemu:///system nodeinfo   # confirms the L1 sees itself as a hypervisor
  5. Performance test before relying on it. Nested I/O is the part that hurts most. Run a benchmark in an L2 guest representative of your real workload (sysbench, fio, your app) and compare to the same workload in an L1 guest. Decide whether the slowdown is acceptable for the use case.

  6. Live migration — limited. Live migration of L1 guests that have running L2 guests has historically been unreliable; recent QEMU/KVM has improved this but treat it as a “test for your version” matter, not an assumption.

Common pitfalls

  • A host with vmx in /proc/cpuinfo but nested=N in the module parameter is the most common case — the silicon supports it; the kernel module is configured to hide it.
  • Setting the guest CPU to <cpu mode='custom'> with a model that doesn’t include vmx/svm in its default feature set means even with host nesting enabled, the guest doesn’t see it. Use host-passthrough for nesting unless you have a specific reason not to.
  • Nested Hyper-V (Windows guest with Hyper-V role) requires extra host flags — <feature policy='require' name='vmx'/> plus exposing nested VMX MSRs; the AMD equivalent is different again.
  • Memory pressure is amplified — L0 reserves memory for L1, L1 reserves for L2, so guest sprawl inside a nested setup eats host RAM fast. Plan capacity.
  • Nested guests cannot use SR-IOV passthrough from the L0 — the device is already passed through (or not) to the L1; the L2 sees only what the L1 exposes.

Stack Harbor enables nested virtualization for narrow client use cases — primarily CI environments that build VM images and need to boot them for integration tests, or staging environments that mirror a customer hypervisor configuration. Nested virtualization is not a general production tool; we set it up where it solves a real problem, and we surface the performance cost honestly. The operating model is part of Managed Operations.