Chapter 24. Virtual Machines with KVM and Libvirt
KVM turns the Linux kernel into a hypervisor: a guest executes its own kernel directly on the CPU's virtualization extensions, while QEMU supplies the virtual chipset, devices, and firmware around it. Libvirt sits above those two programs and gives administrators one interface for defining, starting, inspecting, and moving guests, whether the connection is local or remote. This chapter builds a small virtualization service on two Debian hosts, covers both the installer and the cloud-image route to a Debian guest, connects the guest to NAT, bridged, and isolated networks, and then operates it: resizing resources, cloning, snapshotting, backing up, and migrating it.
The practice machines and their addresses:
| Host | Role | Practice interface | Administration |
|---|---|---|---|
virt1 |
primary KVM host; libvirt storage pool; NAT, bridge, and isolated networks; NFS export for migration | eth1 - 192.168.235.10/24 |
eth0 - management network, 192.168.121.0/24 |
virt2 |
second KVM host; migration target; NFS client | eth1 - 192.168.235.11/24 |
eth0 - management network |
handbook-web |
Debian 13 guest on virt1, later migrated to virt2 | enp1s0 - NAT, enp7s0 - bridge, enp8s0 - isolated |
reachable over the host's NAT network and SSH |
Figure 24.1. Two KVM hosts with a management network for SSH, a separate link for migration, and three libvirt networks attached to one guest. The NAT address is assigned by DHCP and differs between installations; the bridge and isolated addresses are configured explicitly in the exercises.
The management network carries every administrative SSH session. The
secondary eth1 link carries only migration and NFS traffic, so a mistaken
bridge or firewall change on the practice networks cannot lock you out of
the host. The virtual networks and addresses used in this chapter were
introduced with the tools of
Chapter 17; bridges and VLANs are discussed
in Chapter 19; remote access is covered in
Chapter 20.
Commands entered in john's ordinary shell begin with $ and commands
entered in a root shell begin with #; the prompt marker itself is not
typed. A single elevated step is written $ sudo command, which asks for
john's password the first time in a session. Sustained administrative work
can open a root shell with $ sudo -i, whose prompt is #, and exit
leaves it. john obtains these rights through the sudo policy of
Chapter 5; adapt the elevated commands if
your site delegates administration differently. Before each block the text
names the host and account.
Example files live under a named workspace in john's home. On both virt1 and virt2, as john:
$ mkdir -p ~/debian-handbook/virtualization
The command prints nothing on success. Unless a step changes directory,
commands run from that workspace. Guest disk images do not belong in a home
directory: they live in the host's libvirt storage pool under
/var/lib/libvirt/images, where the service account can reach them and
where backup and capacity policy can be applied. After sudo -i, ~ is
/root, not john's home, so paths into the workspace are always spelled
out in full when root needs them.
The Debian 13 packages in this chapter are the current archive versions on the practice hosts. Check the same versions on your own host before relying on a particular option; libvirt's XML schema and command-line options change between major releases.