Chapter 07 — 2007 — today

Linux becomes a hypervisor

One kernel module, one userspace device model, one management API. The KVM/QEMU/libvirt stack turned Linux into the default hypervisor of the open-source world.

KVM — Kernel-based Virtual Machine — began as a Qumranet project led by Avi Kivity and was merged into the Linux kernel in February 2007, in version 2.6.20. It did not add a new hypervisor binary. It turned the existing Linux kernel into one, using the CPU virtualization extensions from Chapter 6. Within a few years it became the default open-source virtualization stack, and today it underpins most of the public cloud.

A hypervisor made of ioctls

KVM exposes /dev/kvm. A userspace process opens the device, creates a virtual machine with an ioctl, maps guest memory, creates virtual CPUs as file descriptors, and then runs them with KVM_RUN. Each vCPU is a thread in the userspace process. When the guest executes a privileged operation that the kernel has configured to exit, KVM_RUN returns to userspace with the exit reason, and the virtual machine manager decides what to do.

The kernel component is deliberately small. It handles CPU state, memory slots, and the VM entry/exit machinery. It does not know what a virtio disk is, how to emulate a PS/2 keyboard, or how to present a VGA adapter. That is the job of the userspace device model: QEMU.

KVM and QEMU architecture libvirt manages a QEMU process with vCPU threads, which uses the KVM kernel module and hardware. libvirt / virsh XML · API · LIFECYCLE QEMU PROCESS — USERSPACE VMM DEVICE MODEL · MONITOR · MIGRATION VCPU THREADS + I/O THREADS each vCPU is a thread · KVM_RUN loop GUEST MEMORY = MMAP'D REGION VIRTIO BACK-ENDS · VHOST · VFIO LINUX KERNEL — KVM MODULE CPU VT-x / AMD-V · EPT · IOMMU IOCTL
FIG 7.1 — KVM is the in-kernel mechanism; QEMU is the userspace machine. libvirt manages the lifecycle.

QEMU: the machine around the CPU

QEMU was created by Fabrice Bellard in 2003 as a fast, portable machine emulator. It can emulate an entire x86 PC on an ARM board, a Raspberry Pi on a laptop, or a mainframe on x86 — all in software, using its Tiny Code Generator (TCG). With KVM, QEMU stops emulating the CPU and lets the guest run natively, but keeps its device models, firmware, monitor and migration machinery.

That division of labour is the source of the most common confusion in the field. KVM is not a complete hypervisor product. It is a kernel interface. QEMU is the program you actually launch. When people say "a KVM virtual machine," they usually mean "a QEMU virtual machine accelerated by KVM."

QEMU provides the virtual hardware: chipset, PCI bus, VGA, USB, SCSI, network cards, timers, ACPI tables and firmware. It also provides qcow2 disk images with snapshots and copy-on-write, the QEMU monitor for runtime control, and live migration. The same binary can run with -accel kvm, -accel hvf on macOS, or pure TCG emulation.

virtio: the fast path for I/O

Emulating a real NIC or disk controller means a VM exit for every register access. virtio, standardized starting in 2008, replaces emulated hardware with a shared-memory ring: the guest driver places buffers in a virtqueue, notifies the host, and the host processes them. The interface is simple enough to implement in any OS and fast enough to approach native I/O.

Modern KVM stacks push the data path further out of QEMU:

  • vhost moves virtio back-end processing into the kernel, avoiding a context switch to QEMU for every packet or block request.
  • vhost-user moves it into a separate userspace process, which is how Open vSwitch and DPDK-based networking integrate.
  • VFIO passes a physical device through to a guest with IOMMU protection; SR-IOV splits it into virtual functions.

This is why a well-tuned KVM guest can saturate a 10/25/100 GbE link while still being isolated by the hypervisor. The fast path avoids emulation, but not the boundary.

libvirt: one API for many hypervisors

libvirt started in 2005 as a management library for Xen and grew into a vendor-neutral API for QEMU/KVM, Xen, LXC, bhyve, VMware and Hyper-V. It runs a daemon (libvirtd) and represents each domain as XML. Tools such as virsh, virt-install and virt-manager speak to that API.

A minimal libvirt domain definition looks like this. It is not a complete production configuration — no firmware, no CPU tuning, no graphics — but it shows the shape.

libvirt — lab01.xml
<domain type='kvm'>
  <name>lab01</name>
  <memory unit='GiB'>4</memory>
  <vcpu>2</vcpu>
  <os>
    <type arch='x86_64'>hvm</type>
  </os>
  <devices>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/var/lib/libvirt/images/lab01.qcow2'/>
      <target dev='vda' bus='virtio'/>
    </disk>
    <interface type='network'>
      <source network='default'/>
      <model type='virtio'/>
    </interface>
    <graphics type='none'/>
    <console type='pty'/>
  </devices>
</domain>
bash — virsh lifecycle
$ sudo virsh define lab01.xml
Domain 'lab01' defined from lab01.xml

$ sudo virsh start lab01
$ sudo virsh console lab01

$ sudo virsh dominfo lab01
Id:             3
Name:           lab01
State:          running
CPU(s):         2
Max memory:     4194304 KiB
Used memory:    4194304 KiB
Autostart:      disable

$ sudo virsh shutdown lab01
$ sudo virsh undefine lab01 --remove-all-storage

Launching QEMU by hand

Skipping libvirt is useful for understanding what the stack actually does. This command boots an Ubuntu cloud image with KVM acceleration, virtio disk and network, user-mode networking, and SSH forwarded to port 2222 on the host. The image must have cloud-init or a pre-set password; cloud images typically do not have a default login.

bash — qemu-system-x86_64
$ qemu-system-x86_64 \
  -enable-kvm \
  -machine q35 \
  -cpu host \
  -smp 4 \
  -m 4096 \
  -drive file=ubuntu-24.04-server-cloudimg-amd64.img,if=virtio \
  -netdev user,id=net0,hostfwd=tcp::2222-:22 \
  -device virtio-net-pci,netdev=net0 \
  -nographic

-cpu host exposes the host CPU features (including vmx or svm if nested virtualization is enabled), which matters for workloads that check CPU flags. -machine q35 selects a modern chipset with PCIe. -nographic redirects the serial console to the terminal.

So is KVM Type 1 or Type 2?

The honest answer is that the labels break down. KVM is not a separate kernel running on bare metal like ESXi, so it is not Type 1 in the strict VMware sense. But it is also not an application on top of a general-purpose OS, because the hypervisor functionality lives inside the kernel that owns the hardware. Most practitioners call it Type 1 because Linux is the hypervisor kernel; purists call it a hosted or hybrid model. The useful question is not the label but the trusted computing base: with KVM, that includes the entire Linux kernel, its drivers and its security surface.

Where KVM runs

KVM is the default compute engine for OpenStack, oVirt, Proxmox VE and many private clouds. Google Compute Engine uses KVM. AWS's Nitro architecture uses KVM on the general-purpose CPU while offloading virtualization and I/O to dedicated hardware. KVM also runs on ARM (with the ARM virtualization extensions), s390 and POWER, which makes it the most portable hypervisor design in production.

The ecosystem around it — libvirt, QEMU, virtio, OVS, Ceph, Kubernetes — is why the open-source cloud stack converged on this combination. Each component is replaceable, but the interfaces between them are stable enough that an entire industry can build on top.

Field note

If you remember one thing: KVM accelerates the CPU, QEMU builds the machine, libvirt manages it. Most "KVM problems" are really QEMU device-model, firmware or libvirt configuration problems.

Sources & further reading