Skip to content

Kvm_qemu

Chapter 56: KVM/QEMU Virtualization - Deep Dive

Section titled “Chapter 56: KVM/QEMU Virtualization - Deep Dive”

Mastering Kernel-Based Virtual Machine Technology

Section titled “Mastering Kernel-Based Virtual Machine Technology”

Virtualization is a technology that allows multiple isolated virtual machines to run on a single physical server. KVM (Kernel-based Virtual Machine) turns the Linux kernel into a hypervisor.

Virtualization Types
+------------------------------------------------------------------+
| |
| Full Virtualization |
| +----------------------------------------------------------+ |
| | - Complete emulation of hardware | |
| | - Guest OS doesn't know it's virtualized | |
| | - Uses: Legacy OS, OS testing | |
| | - Examples: VMware, QEMU (no KVM) | |
| +----------------------------------------------------------+ |
| |
| Paravirtualization |
| +----------------------------------------------------------+ |
| | - Guest OS is aware of virtualization | |
| | - Uses hypervisor APIs for I/O | |
| | - Better performance than full virtualization | |
| | - Examples: Xen, early KVM | |
| +----------------------------------------------------------+ |
| |
| Hardware-Assisted Virtualization (HVM) |
| +----------------------------------------------------------+ |
| | - CPU provides hardware support (Intel VT-x, AMD-V) | |
| | - Nearly native performance | |
| | - Can run unmodified OS | |
| | - Examples: KVM, VMware, Hyper-V | |
| +----------------------------------------------------------+ |
| |
| Container Virtualization |
| +----------------------------------------------------------+ |
| | - OS-level virtualization | |
| | - Containers share host kernel | |
| | - Lightweight, fast to start | |
| | - Examples: Docker, LXC, Podman | |
| +----------------------------------------------------------+ |
| |
+------------------------------------------------------------------+
KVM Architecture
+------------------------------------------------------------------+
| |
| QEMU/KVM Stack |
| |
| +-------------------------------------------------------------+ |
| | libvirt (Management API) | |
| | (virsh, virt-manager, OpenStack, etc.) | |
| +-----------------------------+-------------------------------+ |
| | |
| v |
| +-----------------------------+-------------------------------+ |
| | libvirt daemon (libvirtd) | |
| +-----------------------------+-------------------------------+ |
| | |
| v |
| +-------------------------------------------------------------+ |
| | QEMU Emulator | |
| | - Emulates hardware devices | |
| | - Handles I/O operations | |
| | - Process: qemu-system-x86_64 | |
| +-----------------------------+-------------------------------+ |
| | |
| v |
| +-----------------------------+-------------------------------+ |
| | KVM Kernel Module (kvm.ko, kvm-intel.ko) | |
| +-----------------------------+-------------------------------+ |
| | |
| v |
| +-----------------------------+-------------------------------+ |
| | Linux Kernel | |
| | - CPU scheduling | |
| | - Memory management | |
| | - Device drivers | |
| +-----------------------------+-------------------------------+ |
| |
| | |
| v |
| Physical Hardware |
| |
+------------------------------------------------------------------+

Terminal window
# =============================================================================
# ARCH LINUX
# =============================================================================
# Install packages
sudo pacman -S \
qemu \
libvirt \
virt-manager \
bridge-utils \
dnsmasq \
iptables \
ovmf \
edk2-ovmf
# Enable nested virtualization (if needed)
sudo modprobe kvm_intel nested=1
# Verify KVM
kvm-ok
# =============================================================================
# UBUNTU/DEBIAN
# =============================================================================
# Install packages
sudo apt update
sudo apt install \
qemu-kvm \
libvirt-daemon-system \
libvirt-clients \
bridge-utils \
virt-manager
# Add user to groups
sudo usermod -aG libvirt $USER
sudo usermod -aG kvm $USER
# Start libvirtd
sudo systemctl enable --now libvirtd
# =============================================================================
# VERIFY INSTALLATION
# =============================================================================
# Check modules
lsmod | grep kvm
# Check KVM devices
ls -la /dev/kvm
# Verify libvirtd
sudo systemctl status libvirtd
virsh list --all
# Test with guest
virt-install --name test --os-variant detect --ram 1024 --disk path=/var/lib/libvirt/images/test.qcow2 --pxe --dry-run
Terminal window
# =============================================================================
# BRIDGE NETWORK
# =============================================================================
# Create bridge (using nmcli)
nmcli con add type bridge ifname br0
nmcli con add type ethernet ifname eth0 master br0
nmcli con down eth0
nmcli con up br0
# Using brctl (deprecated but still works)
sudo brctl addbr br0
sudo brctl addif br0 eth0
sudo brctl show
# =============================================================================
# LIBVIRT NETWORK
# =============================================================================
# Create network XML /tmp/bridge-network.xml
<network>
<name>bridge-network</name>
<forward mode='bridge'/>
<bridge name='br0'/>
</network>
# Define and start network
virsh net-define /tmp/bridge-network.xml
virsh net-start bridge-network
virsh net-autostart bridge-network
# =============================================================================
# NAT NETWORK (DEFAULT)
# =============================================================================
# Default NAT network comes with libvirt
virsh net-list
virsh net-start default

Terminal window
# =============================================================================
# BASIC VM CREATION
# =============================================================================
# Create VM with ISO
virt-install \
--name=webserver \
--vcpu=2 \
--memory=2048 \
--disk path=/var/lib/libvirt/images/webserver.qcow2,size=20 \
--os-variant=ubuntu22.04 \
--cdrom=/path/to/ubuntu.iso \
--network network=default \
--graphics=vnc
# Create VM with PXE boot
virt-install \
--name=pxe-server \
--vcpu=2 \
--memory=2048 \
--disk path=/var/lib/libvirt/images/pxe.qcow2,size=10 \
--os-variant=ubuntu22.04 \
--pxe \
--network network=default
# Create VM with cloud-init
virt-install \
--name=cloud-vm \
--vcpu=2 \
--memory=4096 \
--disk path=/var/lib/libvirt/images/cloud.qcow2,size=20 \
--os-variant=ubuntu22.04 \
--cloud-init root-password-generate=on \
--network network=default
# Create from existing image
virt-install \
--name=imported \
--vcpu=2 \
--memory=2048 \
--disk path=/var/lib/libvirt/images/existing.qcow2 \
--import \
--os-variant=auto
# =============================================================================
# CONSOLE ACCESS
# =============================================================================
# Connect to VM console
virt-viewer webserver
# Connect via serial console
virsh console webserver
# Exit serial console: Ctrl+]
# =============================================================================
# VM CLONING
# =============================================================================
# Clone VM
virt-clone --original=webserver --name=webserver-clone --auto-clone
# Clone to new storage
virt-clone --original=webserver \
--name=webserver-clone \
--file=/var/lib/libvirt/images/clone.qcow2
Terminal window
# =============================================================================
# VM LIFECYCLE
# =============================================================================
# List running VMs
virsh list
# List all VMs
virsh list --all
# Start VM
virsh start webserver
# Stop VM (graceful)
virsh shutdown webserver
# Force stop VM
virsh destroy webserver
# Reboot VM
virsh reboot webserver
# Pause/Resume
virsh suspend webserver
virsh resume webserver
# Autostart VM
virsh autostart webserver
virsh autostart --disable webserver
# =============================================================================
# VM INFORMATION
# =============================================================================
# VM details
virsh dominfo webserver
# VM state
virsh domstate webserver
# List VCPUs
virsh vcpucount webserver
# Get VNC port
virsh vncdisplay webserver
# =============================================================================
# VM MODIFICATION
# =============================================================================
# Resize memory (online)
virsh setmem webserver 4096 --current
virsh setmem webserver 4096 --config # persistent
# Resize VCPUs (online)
virsh setvcpus webserver 4 --current
virsh setvcpus webserver 4 --config # persistent
# Attach disk
virsh attach-disk webserver /var/lib/libvirt/images/newdisk.qcow2 vdb --persistent
# Detach disk
virsh detach-disk webserver vdb --persistent
# Attach network
virsh attach-interface webserver --type network --source default --persistent
# =============================================================================
# VM DELETION
# =============================================================================
# Undefine VM (remove definition)
virsh undefine webserver
# Undefine with storage
virsh undefine webserver --remove-all-storage
# Delete snapshots before undefine
virsh undefine webserver --snapshots-metadata

KVM Storage Types
+------------------------------------------------------------------+
| |
| Directory (dir) |
| +----------------------------------------------------------+ |
| | - File-based storage (qcow2, raw) | |
| | - Default for most setups | |
| | - Easiest to manage | |
| +----------------------------------------------------------+ |
| |
| LVM |
| +----------------------------------------------------------+ |
| | - LVM volumes as VM disks | |
| | - Better performance | |
| | - Requires LVM setup | |
| +----------------------------------------------------------+ |
| |
| iSCSI |
| +----------------------------------------------------------+ |
| | - Network storage | |
| | - For shared storage | |
| | - Requires iSCSI target | |
| +----------------------------------------------------------+ |
| |
| NFS |
| +----------------------------------------------------------+ |
| | - Network file system | |
| | - For live migration | |
| | - Requires NFS server | |
| +----------------------------------------------------------+ |
| |
| Ceph/RBD |
| +----------------------------------------------------------+ |
| | - Distributed storage | |
| | - Highly available | |
| | - For production clusters | |
| +----------------------------------------------------------+ |
| |
+------------------------------------------------------------------+
Terminal window
# =============================================================================
# STORAGE POOLS
# =============================================================================
# List pools
virsh pool-list
virsh pool-list --all
# Create directory pool
virsh pool-define-as default dir - - - - /var/lib/libvirt/images
virsh pool-start default
# Create LVM pool
virsh pool-define-as vgpool logical - /dev/vg_kvm /dev/empty
virsh pool-start vgpool
# Pool info
virsh pool-info default
# Build pool
virsh pool-build default
# Refresh pool
virsh pool-refresh default
# Delete pool
virsh pool-destroy default
virsh pool-undefine default
# =============================================================================
# STORAGE VOLUMES
# =============================================================================
# List volumes
virsh vol-list default
# Create volume
virsh vol-create-as default vmdisk.qcow2 20G
# Create volume with specific format
virsh vol-create-as default vmdisk.raw 10G --format raw
# Clone volume
virsh vol-clone --pool default vmdisk.qcow2 vmdisk-clone.qcow2
# Resize volume
virsh vol-resize default/vmdisk.qcow2 40G
# Delete volume
virsh vol-delete default/vmdisk.qcow2
# =============================================================================
# DISK FORMATS
# =============================================================================
# qcow2 (Copy on Write) - Recommended
# - Snapshot support
# - Compression
# - Encryption
# - Thin provisioning
# raw - Fastest
# - No features
# - Full allocation
# qcow2 creation
qemu-img create -f qcow2 /var/lib/libvirt/images/vm.qcow2 20G
# Convert raw to qcow2
qemu-img convert -f raw -O qcow2 input.raw output.qcow2
# Convert to raw
qemu-img convert -f qcow2 -O raw input.qcow2 output.raw
# Resize disk
qemu-img resize vm.qcow2 +10G
# Check disk
qemu-img check vm.qcow2

Terminal window
# =============================================================================
# SNAPSHOT TYPES
# =============================================================================
# Internal snapshots
# - Stored within qcow2 file
# - Cannot live merge
# - Limited performance
# External snapshots
# - Backing chain
# - Can live merge
# - Better performance
# =============================================================================
# CREATE SNAPSHOT
# =============================================================================
# Create internal snapshot
virsh snapshot-create webserver
virsh snapshot-create-as webserver --name "before-update"
# Create external snapshot
virsh snapshot-create-as webserver \
--name "backup-snapshot" \
--disk-only \
--diskspec vda,file=/var/lib/libvirt/images/webserver-backup.qcow2
# =============================================================================
# LIST SNAPSHOTS
# =============================================================================
# List all snapshots
virsh snapshot-list webserver
# Current snapshot
virsh snapshot-current webserver
# =============================================================================
# REVERT TO SNAPSHOT
# =============================================================================
# Revert to snapshot
virsh snapshot-revert webserver before-update
# =============================================================================
# DELETE SNAPSHOT
# =============================================================================
# Delete snapshot
virsh snapshot-delete webserver before-update
# =============================================================================
# MERGE SNAPSHOT
# =============================================================================
# Block commit (merge to base)
virsh blockcommit webserver vda --active --verbose --pivot
# Block pull (merge to top)
virsh blockpull webserver vda --verbose

Live Migration Types
+------------------------------------------------------------------+
| |
| Pre-copy Migration |
| +----------------------------------------------------------+ |
| | 1. Initial transfer (memory pages) | |
| | 2.迭代 memory transfer (dirty pages) | |
| | 3. Stop source VM | |
| | 4. Transfer remaining pages | |
| | 5. Start destination VM | |
| | | |
| | - Minimal downtime | |
| | - Requires shared storage | |
| +----------------------------------------------------------+ |
| |
| Post-copy Migration |
| +----------------------------------------------------------+ |
| | 1. Stop source VM | |
| | 2. Transfer memory | |
| | 3. Start destination VM | |
| | | |
| | - Faster transfer | |
| | - Risk of source failure | |
| +----------------------------------------------------------+ |
| |
+------------------------------------------------------------------+
Terminal window
# =============================================================================
# LIVE MIGRATION
# =============================================================================
# Basic live migration
virsh migrate --live webserver qemu+ssh://dest-host/system
# With compression
virsh migrate --live --compress webserver qemu+ssh://dest-host/system
# With tunneled migration
virsh migrate --live --tunnelled webserver qemu+ssh://dest-host/system
# =============================================================================
# MIGRATION WITH STORAGE
# =============================================================================
# Migrate with non-shared storage
virsh migrate --live \
--copy-storage-all \
webserver \
qemu+ssh://dest-host/system
# Migrate with incremental storage
virsh migrate --live \
--copy-storage-incremental \
webserver \
qemu+ssh://dest-host/system
# =============================================================================
# MIGRATION OPTIONS
# =============================================================================
# Set maximum downtime
virsh migrate-setmaxdowntime webserver 1000
# Auto-converge (reduce memory during migration)
virsh migrate --live --auto-converge webserver qemu+ssh://dest-host/system
# Bandwidth limit (Mbps)
virsh migrate --live --bandwidth 100 webserver qemu+ssh://dest-host/system
# =============================================================================
# VERIFY MIGRATION
# =============================================================================
# Check migration status
virsh migrate-getspeed webserver
# List completed migrations
virsh migrate-compeleted webserver

Important

  1. KVM vs QEMU: KVM = kernel module, QEMU = emulator
  2. virt-install: Create VMs from command line
  3. virsh: Main CLI for VM management
  4. Storage: qcow2 supports snapshots, raw is faster
  5. Networks: Bridge for external access, NAT for isolated
  6. Live migration: Requires shared storage or —copy-storage-all
  7. libvirt: API/daemon that powers KVM management
  8. snapshots: Internal vs external, understand tradeoffs
  9. Nested virtualization: Enable kvm_intel nested=1
  10. Performance: Use virtio drivers for disk/network

In this chapter, you learned:

  • ✅ Virtualization types and concepts
  • ✅ KVM architecture
  • ✅ KVM installation
  • ✅ VM creation with virt-install
  • ✅ VM management with virsh
  • ✅ Storage pools and volumes
  • ✅ Snapshots
  • ✅ Live migration
  • ✅ Performance tuning

Chapter 57: Docker Fundamentals


Last Updated: February 2026