Creating Nested VMs with KVM
Objective
Install the KVM hypervisor stack on Linux, create and manage virtual machines using virsh and virt-install, and understand the libvirt architecture.
Tools & Technologies
KVMqemulibvirtvirshvirt-managervirt-install
Key Commands
egrep -c '(vmx|svm)' /proc/cpuinfoapt install qemu-kvm libvirt-daemon-systemvirsh list --allvirt-install --name vm1 --memory 1024 --disk size=10Architecture Overview
graph TD
HOST[Host Machine] --> KVM[KVM Kernel Module]
KVM --> QEMU[QEMU Emulator]
QEMU --> LV[libvirt daemon]
LV --> VM1[VM 1]
LV --> VM2[VM 2]
LV --> VM3[VM 3]
LV --> API[API / virsh / virt-manager]
style HOST fill:#1a1a2e,stroke:#00d4ff,color:#e0e0e0
style LV fill:#1a1a2e,stroke:#00ff88,color:#e0e0e0
style API fill:#1a1a2e,stroke:#ffd700,color:#ffd700
Step-by-Step Process
01
Verify CPU Support
KVM requires hardware virtualization extensions. Check before installing.
# Count virtualization-capable CPUs
egrep -c '(vmx|svm)' /proc/cpuinfo # >0 means supported
# More detail
lscpu | grep Virtualization
kvm-ok # ubuntu: apt install cpu-checker
02
Install KVM Stack
Install QEMU, KVM, libvirt, and management tools.
sudo apt install -y \
qemu-kvm libvirt-daemon-system \
libvirt-clients bridge-utils \
virt-manager virtinst
# Add user to libvirt group
sudo usermod -aG libvirt $USER
newgrp libvirt
# Start and enable libvirt
sudo systemctl enable --now libvirtd
03
Create a VM with virt-install
Use virt-install to create a VM from an ISO.
sudo virt-install \
--name testvm \
--ram 1024 \
--vcpus 1 \
--disk size=10 \
--location /path/to/ubuntu.iso \
--os-variant ubuntu22.04 \
--network default \
--graphics none \
--extra-args 'console=ttyS0'
04
Manage VMs with virsh
virsh is the command-line interface to libvirt.
virsh list --all # list all VMs
virsh start testvm # start VM
virsh shutdown testvm # graceful stop
virsh destroy testvm # force stop
virsh console testvm # serial console
virsh snapshot-create-as testvm snap1
virsh snapshot-revert testvm snap1
virsh undefine testvm --remove-all-storage
Challenges & Solutions
- Nested virtualization requires enabling in host BIOS and VMware/VBox settings
- Permission denied to virsh — user not in libvirt group, relogin required
Key Takeaways
- Use snapshots before major changes — virsh snapshot-create-as is instant
- virt-manager provides a GUI when a desktop is available