Nested Virtualization
How Nested Virtualization Works
Nested virtualization means running a hypervisor inside a virtual machine, so that the inner hypervisor can start virtual machines of its own. The physical server's hypervisor is the first level, often called L0. The virtual machine that runs the inner hypervisor is L1, and the machines inside it are L2.
For this to work, the outer hypervisor must expose hardware virtualization support, the CPU features described in hardware-assisted virtualization, to the L1 machine. Privileged operations from an L2 guest may need to pass through both layers before they are handled, so the design is more complex than a single level of virtualization.
Common Uses and Performance Trade-Offs
Nested virtualization is useful when you need to run or test virtualization itself. Common uses include training labs, where each student runs a hypervisor and builds small test networks; continuous integration systems that test software which creates virtual machines; and developer tools that start a Linux virtual machine inside a Windows or macOS virtual machine.
Microsoft's Hyper-V hypervisor can host nested guests on suitable hardware, and the same idea applies to KVM and other hypervisors that expose virtualization features.
The trade-off is performance. Nested guests run slower than guests started directly on physical hardware, and the gap is larger for workloads that make many hardware requests, such as heavy disk or network activity. Each extra layer adds overhead, and memory and CPU must be sized for both layers.
A Training Lab Built on Nested Machines
A university gives each of 20 students a Linux virtual machine. Inside it, each student installs a hypervisor and creates two small virtual machines for a networking exercise. The lab host must provide enough CPU and memory for every outer machine plus its nested guests, and the exercise runs more slowly than it would on bare hardware, which is acceptable for the course.
Frequently asked questions
Can I use nested virtualization on a VPS?
It depends on the provider. Nested virtualization requires the VPS platform to pass hardware virtualization features through to the guest, and many providers do not. Some KVM-based platforms allow it. Ask before you buy if your work depends on running a hypervisor inside your server.
Is nested virtualization the same as running containers?
No. Containers share the host kernel and do not start a separate operating system. Nested virtual machines each run their own kernel, which gives stronger separation but costs more memory and CPU. The choice depends on whether you need a full second operating system or only an isolated application.