| |

LFCS 155 ๐Ÿง KVM โ€” Virtualization Concepts

โฑ๏ธ Estimated read: 9 min | |

The previous chapters covered containerization with Docker, which isolates applications at the operating system level by sharing the host kernel. This chapter introduces the other major virtualization technology on Linux: KVM (Kernel-based Virtual Machine). KVM is a full virtualization solution that provides each guest with its own virtual hardware and its own kernel. The LFCS exam tests the conceptual distinction between the two models, the hardware requirements (Intel VT-x or AMD-V), and the role of QEMU in the KVM architecture. The critical trap is confusing KVM with a traditional hypervisor. KVM is not a separate software layer that runs on the host; it is a kernel module that turns the Linux kernel itself into a type-1 hypervisor .

Key point: KVM is a Linux kernel module that enables hardware-assisted full virtualization. It requires a CPU with virtualization extensions (Intel VT-x or AMD-V). KVM provides the kernel-level virtualization infrastructure, while QEMU provides the device emulation and the userspace management. Each virtual machine is a regular Linux process on the host, and each virtual CPU is a thread within that process .


Why KVM requires a specific mental model

The type-1 versus type-2 problem. A type-2 hypervisor (like VirtualBox) runs as an application on top of a host operating system. The host OS manages the hardware, and the hypervisor asks the host OS for resources. A type-1 hypervisor runs directly on the hardware. KVM is unusual: it is a type-1 hypervisor because the Linux kernel itself becomes the hypervisor. The KVM module plugs into the kernel, and the kernel manages the hardware directly. There is no separate hypervisor OS beneath the kernel .

The hardware extension problem. KVM does not emulate the CPU in software. It relies on the CPU’s hardware virtualization extensions: Intel VT-x or AMD-V. These extensions add a new execution mode to the CPU (VMX root mode on Intel, SVM on AMD) that allows the host kernel to run the guest’s code natively while retaining control. Without these extensions, KVM cannot function. The kvm-ok command checks whether the CPU supports them .

The QEMU problem. KVM provides the CPU virtualization, but it does not provide the disk, the network card, the display, or the BIOS. Those are provided by QEMU. QEMU is a userspace process that emulates the devices and manages the VM lifecycle. The KVM module exposes a device (/dev/kvm), and QEMU talks to it via ioctl system calls. When QEMU wants to run the guest’s CPU, it issues KVM_RUN, and the kernel switches the CPU into the guest mode. When the guest needs to access an emulated device, the CPU exits back to QEMU, which emulates the device and resumes the guest .

The process problem. Each VM is a QEMU process on the host. Each virtual CPU is a thread within that process. The host’s standard process management tools (ps, top, kill) work on the VM. The kill command on the QEMU process terminates the VM, which is the same behavior as killing any other process .

The isolation problem. The VM has its own kernel, its own memory space, and its own virtual devices. The isolation is stronger than a container’s, because a kernel vulnerability in the guest does not directly compromise the host. The trade-off is the overhead: the VM boots a full operating system, consumes more memory, and has a higher I/O latency for the emulated devices .

The trade-off. The container is lighter, faster to start, and denser. The VM is more isolated, supports different operating systems, and provides a stronger security boundary. The choice depends on the trust level of the workload and the requirement for kernel isolation.


a. The KVM architecture and the hardware requirements

KVM is a kernel module that turns the Linux kernel into a hypervisor. The hardware must support the virtualization extensions.

# Check the CPU virtualization support
egrep -c '(vmx|svm)' /proc/cpuinfo
# Output > 0 means the CPU supports VT-x (vmx) or AMD-V (svm)

# The kvm-ok tool (Ubuntu)
sudo apt install cpu-checker
kvm-ok
# INFO: /dev/kvm exists
# KVM acceleration can be used

The vmx flag indicates Intel VT-x. The svm flag indicates AMD-V. If neither is present, the CPU does not support hardware virtualization, and KVM cannot be used .

The KVM module is loaded into the kernel.

# Load the KVM module
sudo modprobe kvm_intel   # for Intel
sudo modprobe kvm_amd     # for AMD

# Verify the module is loaded
lsmod | grep kvm

The /dev/kvm character device is the interface to the userspace. The QEMU process opens the device and uses ioctl to create the VM, create the vCPUs, and run the guest .


b. The QEMU userspace and the device emulation

QEMU is the userspace process that provides the device emulation. It is not part of the KVM module.

# Install QEMU and the KVM tools
sudo apt install qemu-kvm libvirt-daemon-system libvirt-clients virtinst

# List the running VMs
virsh list --all

The libvirt is the management layer. The virsh is the CLI. The virt-manager is the GUI. The libvirt daemon communicates with the QEMU processes and provides a consistent API for the VM management .

The QEMU process allocates the guest memory via mmap on /dev/kvm. It creates one thread per virtual CPU. The thread runs a loop: ioctl(KVM_RUN) to enter the guest mode, handle the exit, and repeat .

The device emulation is the reason the VM can boot. The guest sees a virtual disk, a virtual network card, and a virtual display. QEMU emulates these devices. The hardware virtualization extensions can accelerate some of the I/O (with VT-d or virtio), but the basic device model is QEMU’s .


c. The VM lifecycle and the management tools

The VM is created from a disk image and a configuration. The virt-install command creates the VM. The virsh command manages the lifecycle.

# Create a VM
sudo virt-install \
  --name ubuntu-vm \
  --os-variant ubuntu22.04 \
  --vcpus 2 \
  --ram 2048 \
  --disk path=/var/lib/libvirt/images/ubuntu.qcow2,size=10 \
  --network bridge=virbr0 \
  --graphics none \
  --location http://ftp.ubuntu.com/ubuntu/dists/jammy/main/installer-amd64/

# Start the VM
virsh start ubuntu-vm

# List the running VMs
virsh list

# Shut down the VM
virsh shutdown ubuntu-vm

# Destroy (force stop) the VM
virsh destroy ubuntu-vm

# Delete the VM
virsh undefine ubuntu-vm

The virsh start starts the QEMU process. The virsh shutdown sends the ACPI shutdown signal. The virsh destroy kills the process. The virsh undefine removes the VM definition but not the disk image .

The disk image is the storage. The qcow2 format is the copy-on-write format that grows as the data is written. The raw format is the pre-allocated format. The qemu-img command manages the images.

# Create a qcow2 image
qemu-img create -f qcow2 /var/lib/libvirt/images/disk.qcow2 10G

# Inspect the image
qemu-img info /var/lib/libvirt/images/disk.qcow2

Complete Example Session

# ============================================
# PART 1: CHECK THE VIRTUALIZATION SUPPORT
# ============================================

egrep -c '(vmx|svm)' /proc/cpuinfo
# Output > 0
# ============================================
# PART 2: INSTALL THE KVM PACKAGES
# ============================================

sudo apt install qemu-kvm libvirt-daemon-system libvirt-clients virtinst
# ============================================
# PART 3: VERIFY THE KVM MODULE
# ============================================

lsmod | grep kvm
# kvm_intel  ...
# kvm        ...
# ============================================
# PART 4: CHECK THE /dev/kvm
# ============================================

ls -l /dev/kvm
# crw-rw---- 1 root kvm 10, 232 ...
# ============================================
# PART 5: CREATE A VM
# ============================================

sudo virt-install --name test-vm --vcpus 1 --ram 1024 --disk size=5 --os-variant ubuntu22.04 --graphics none --location http://ftp.ubuntu.com/ubuntu/dists/jammy/main/installer-amd64/
# ============================================
# PART 6: LIST THE VMs
# ============================================

virsh list --all
# Id   Name      State
# -------------------------
# 1    test-vm   running
# ============================================
# PART 7: SHUT DOWN THE VM
# ============================================

virsh shutdown test-vm
# ============================================
# PART 8: REMOVE THE VM
# ============================================

virsh undefine test-vm
# ============================================
# PART 9: INSPECT THE DISK IMAGE
# ============================================

qemu-img info /var/lib/libvirt/images/test-vm.qcow2

The nine parts trace the KVM workflow: CPU check, package install, module verification, device check, VM creation, VM list, shutdown, removal, and disk inspection.


Quick Reference

KVM Components

ComponentRole
kvm kernel moduleCPU virtualization
/dev/kvmUserspace interface
QEMUDevice emulation
libvirtManagement API
virshCLI
virt-managerGUI

Hardware Requirements

CPUFlag
Intel VT-xvmx
AMD-Vsvm

Management Commands

CommandPurpose
kvm-okCheck the support
virsh listList the VMs
virsh startStart the VM
virsh shutdownGraceful stop
virsh destroyForce stop
virsh undefineRemove the definition

Disk Images

FormatPurpose
qcow2Copy-on-write, grows
rawPre-allocated

Best Practices

โœ… Do This:

egrep -c '(vmx|svm)' /proc/cpuinfo
<!-- Check the CPU support first -->                 # โœ…
virsh shutdown test-vm
<!-- Use graceful shutdown -->                       # โœ…
qemu-img info disk.qcow2
<!-- Inspect the disk image -->                      # โœ…

โŒ Don’t Do This:

# Assume the CPU supports virtualization
<!-- Check the vmx/svm flag -->                      # โŒ
virsh destroy test-vm
# For the routine shutdown
<!-- Use shutdown; destroy is force -->              # โŒ
# Forget the libvirt daemon
<!-- virsh needs the daemon running -->              # โŒ

Common Pitfalls

PitfallWhy It HappensFix
KVM not availableCPU lacks VT-x/AMD-VCheck the vmx/svm flag
virsh cannot connectlibvirt daemon not runningsystemctl start libvirtd
VM slowNo hardware accelerationEnsure /dev/kvm is used
Disk image fullqcow2 not pre-allocatedResize or use raw
Network not workingBridge misconfiguredCheck the virbr0 bridge

Real-World Examples

1. Check Support

egrep -c '(vmx|svm)' /proc/cpuinfo

2. Load Module

sudo modprobe kvm_intel

3. Check Device

ls -l /dev/kvm

4. List VMs

virsh list --all

5. Start VM

virsh start myvm

6. Shutdown VM

virsh shutdown myvm

7. Force Stop

virsh destroy myvm

8. Remove VM

virsh undefine myvm

9. Create Disk

qemu-img create -f qcow2 disk.qcow2 10G

10. Inspect Disk

qemu-img info disk.qcow2

Visual

KVM Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Guest VM (own kernel)                       โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                   โ”‚
                   โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  QEMU (userspace process)                    โ”‚
โ”‚  โ”œโ”€โ”€ Device emulation                        โ”‚
โ”‚  โ”œโ”€โ”€ Memory allocation                       โ”‚
โ”‚  โ””โ”€โ”€ vCPU threads                            โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                   โ”‚ ioctl
                   โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  /dev/kvm                                    โ”‚
โ”‚  โ†’ KVM kernel module                         โ”‚
โ”‚  โ†’ Hardware virtualization (VT-x/AMD-V)      โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                   โ”‚
                   โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Host Linux Kernel                           โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

VM vs Container

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  VM                                          โ”‚
โ”‚  โ”œโ”€โ”€ App                                     โ”‚
โ”‚  โ”œโ”€โ”€ Guest OS + Kernel                       โ”‚
โ”‚  โ””โ”€โ”€ QEMU + KVM                              โ”‚
โ”‚  โ””โ”€โ”€ Host OS                                 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                   โ”‚
                   โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Container                                   โ”‚
โ”‚  โ”œโ”€โ”€ App                                     โ”‚
โ”‚  โ””โ”€โ”€ Container runtime (namespaces)          โ”‚
โ”‚  โ””โ”€โ”€ Host OS Kernel (shared)                 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Summary

ItemValue
KVM typeType-1 hypervisor (kernel module)
Hardware requirementIntel VT-x (vmx) or AMD-V (svm)
CPU virtualizationKVM
Device emulationQEMU
Managementlibvirt (virsh)
Device interface/dev/kvm
Disk formatqcow2 or raw
VM processQEMU process
vCPUThread

Key takeaways:

  • KVM is a kernel module. It turns the Linux kernel into a type-1 hypervisor. It requires the CPU’s hardware virtualization extensions (Intel VT-x or AMD-V) .
  • QEMU provides the device emulation. KVM handles the CPU virtualization, and QEMU handles the disk, the network, the display, and the BIOS. The QEMU process is the VM on the host .
  • The VM is a process. Each VM is a QEMU process. Each virtual CPU is a thread. The standard process tools (ps, top, kill) work on the VM .
  • The isolation is stronger than a container. The VM has its own kernel. The container shares the host kernel. The VM is the correct choice for the untrusted workloads and the different operating systems .
  • The management is libvirt. The virsh CLI and the virt-manager GUI use the libvirt API to manage the VMs. The virt-install creates the VM .

Remember: KVM is the kernel module. QEMU is the device emulation. libvirt is the management. The VM is the process. The vCPU is the thread. The VT-x is the requirement. The exam tests the distinction from the container and the QEMU/KVM architecture.

Was this helpful?


Stop using slow, ad-bloated tool sites! ๐Ÿคฎ

๐Ÿ”Ž Search “KandZ Tools” on Google to use many professional utilities for free.

KandZ.me is the ultimate minimalist hub for:
โœ… Finance (Mortgage, Interest, Inflation)
โœ… Tech (Base64, JSON, Dev Suite, IP)
โœ… Health (BMI, BMR, TDEE)
โœ… Productivity (Timer, Workspace, QR)

โšก๏ธ Fast & Private
๐Ÿ”’ No data leaves your device
๐Ÿ’Ž 100% Free

๐Ÿ”— Use it now: https://tools.kandz.me
๐Ÿ”– Bookmark itโ€”youโ€™ll need it later!