Understanding KVM, QEMU, and Libvirt: A Comprehensive Guide
Core Components
The Linux virtualization stack consists of three main components working together:
KVM (Kernel-based Virtual Machine)
- A kernel module providing hardware virtualization support
- Uses CPU virtualization extensions (Intel VT-x or AMD-V)
- Exposes /dev/kvm interface for userspace access
- Handles CPU and memory virtualization
QEMU
- Hardware emulator and virtual machine monitor
- Provides device emulation (network, storage, etc.)
- Communicates with KVM through ioctl calls
- Runs as a userspace process
Libvirt
- Management layer for virtualization platforms
- Provides unified API and tools (virsh)
- Handles security and resource isolation
- Manages VM lifecycle, storage, and networking
Practical Usage Examples
Basic VM Management
[code]
# List all VMs
virsh list –all
# Start a VM
virsh start vm_name
# Stop a VM
virsh shutdown vm_name
# Force stop
virsh destroy vm_name
[/code]
Creating a New VM
[code]
virt-install \
–name ubuntu20.04 \
–ram 2048 \
–disk path=/var/lib/libvirt/images/ubuntu.qcow2,size=20 \
–vcpus 2 \
–os-type linux \
–os-variant ubuntu20.04 \
–network bridge=virbr0 \
–graphics none \
–console pty,target_type=serial \
–location ‘http://archive.ubuntu.com/ubuntu/dists/focal/main/installer-amd64/’ \
–extra-args ‘console=ttyS0,115200n8 serial’
[/code]
Storage Management
[code]
# Create storage pool
virsh pool-define-as default dir –target /var/lib/libvirt/images
virsh pool-start default
virsh pool-autostart default
# Create volume
virsh vol-create-as default ubuntu.qcow2 20G –format qcow2
[/code]
Network Configuration
[code]
# Create network configuration file
cat > network.xml <
EOF
# Define and start network
virsh net-define network.xml
virsh net-start isolated
virsh net-autostart isolated
[/code]
Advanced Configuration
CPU Pinning
[code]
virsh vcpupin ubuntu20.04 0 0
virsh vcpupin ubuntu20.04 1 1
[/code]
Memory Management
[code]
# Set memory limits
virsh setmem ubuntu20.04 2G –config
virsh setmaxmem ubuntu20.04 4G –config
# Enable memory ballooning
virsh edit ubuntu20.04
# Add under
[/code]
Storage Pool Management
[code]
# Create LVM storage pool
virsh pool-define-as vmstorage logical –source-name vg_vms –target /dev/vg_vms
virsh pool-build vmstorage
virsh pool-start vmstorage
virsh pool-autostart vmstorage
[/code]
Network Bridge Configuration
[code] Key Components Overview Production Recommendations Common Pitfalls to Avoid Performance Optimization Tips Security Considerations Quick Reference Commands [code] # Verify KVM module # Check libvirt status # Monitor VM performance # Backup VM Additional Resources
# Create bridge interface configuration
cat > /etc/netplan/01-netcfg.yaml <
Component
Role
Level
KVM
Hardware virtualization
Kernel space
QEMU
Device emulation
User space
Libvirt
Management layer
User space
# Check virtualization support
egrep -c ‘(vmx|svm)’ /proc/cpuinfo
lsmod | grep kvm
systemctl status libvirtd
virt-top
virsh dumpxml VM_NAME > vm_config.xml
virsh snapshot-create-as VM_NAME snapshot1 “First snapshot” –disk-only
[/code]