24.1. Planning a Virtualization Host

A virtualization host is a normal Debian server with three additional responsibilities: it must expose hardware virtualization to the kernel, it must have enough memory and I/O capacity for the guests it will carry, and it must run a management daemon that survives reboots. Planning those three things before the first guest avoids the common failure in which a host is sized for CPU count but runs out of memory, free space, or file descriptors under real load.

24.1.1. CPU Virtualization Support and Host Resource Requirements

KVM requires a processor with Intel VT-x or AMD-V and firmware that leaves the feature enabled. On a physical server this is normally a firmware setting; on a host that is itself virtual, the outer hypervisor must expose nested virtualization. Debian loads the appropriate kernel module automatically when the CPU advertises the feature. On virt1, as john:

$ lscpu | grep -i 'virtualization\|hypervisor'
Flags:                                   fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon rep_good nopl xtopology cpuid tsc_known_freq pni pclmulqdq vmx ssse3 fma cx16 pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch cpuid_fault pti ssbd ibrs ibpb stibp tpr_shadow flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx rdseed adx smap clflushopt xsaveopt xsavec xgetbv1 xsaves arat vnmi umip md_clear flush_l1d arch_capabilities
Virtualization:                          VT-x
Hypervisor vendor:                       KVM
Virtualization type:                     full
Vulnerability Srbds:                     Unknown: Dependent on hypervisor status

The Virtualization line reports the instruction-set extension, VT-x here. The Hypervisor vendor line shows that this particular host is itself a KVM guest with nested VT-x; a physical server leaves that line out and reports its processor vendor instead. The Flags line matches the filter because the CPU advertises a hypervisor flag, which the kernel sets when it runs under a hypervisor. Both a physical host and a nested one can run guests, but nested virtualization adds a second translation layer and is not a substitute for capacity planning on the physical machine.

The kernel exposes the device that QEMU uses for hardware acceleration. On virt1, as john:

$ ls -l /dev/kvm
crw-rw---- 1 root kvm 10, 232 Nov 23 08:02 /dev/kvm

The character device is owned by root and group kvm. An administrator who is not root needs membership in kvm to open it directly. Libvirt's system daemon runs as a privileged service and does not depend on the caller's group, but membership is still useful for local tools such as qemu-system-x86_64 and for the session connection described later.

The cpu-checker package provides a quick verdict. On virt1, as john:

$ sudo kvm-ok
INFO: /dev/kvm exists
KVM acceleration can be used

If the command reports that KVM is disabled in the BIOS or that the module is not loaded, fix the firmware or the outer hypervisor first. QEMU can run without KVM using software emulation, but the performance and CPU cost make that suitable only for recovery or for guests of a different architecture.

Memory is the resource that most often limits guest count. KVM allocates guest RAM from the host, and overcommitting memory works only when guests do not touch all of their pages at once. A host running several guests should keep enough unreserved memory for the kernel, page cache, and the libvirt daemon, and should not rely on swap to absorb sustained guest pressure. Check what the host has to divide up. On virt1, as john:

$ nproc
2
$ free -m | head -2
               total        used        free      shared  buff/cache   available
Mem:            2978        1213         913           2        1025        1765

head -2 keeps the column header and the Mem row, which are the lines that matter for planning. Only 1765 MiB is available even though 913 MiB is free, because the page cache counted in buff/cache can be reclaimed under pressure but the host's own working set cannot. Subtract the memory you intend to reserve for the host before dividing the remainder among guests. For a small lab, one or two 1 GiB guests fit on this machine; for production, treat available as an upper bound and leave a margin.

Storage needs both capacity and I/O. A qcow2 overlay grows as the guest writes, so a 10 GiB virtual disk does not consume 10 GiB immediately, but a thin pool can still be exhausted by snapshots, logs, or a runaway guest. Check the filesystem that backs the image directory. On virt1, as john:

$ df -h /var/lib/libvirt/images | tail -1
/dev/vda1        98G  2.6G   91G   3% /

tail -1 drops the header and shows the filesystem's row: the pool directory lives on the root filesystem of this host. Monitor the filesystem, not only the pool's reported free space. A separate filesystem or LVM volume for images makes this easier and prevents a full root filesystem from taking down libvirt. LVM and thin provisioning are covered in Chapter 13; filesystem-level capacity changes are covered in Chapter 14.

Network capacity matters as soon as guests are bridged or used for services. A single 1 GbE uplink shared by ten guests will show contention under backup or migration traffic. Plan a separate link for migration, as the practice hosts do, or accept that live migration will compete with guest traffic. CPU overcommit is usually safe for idle or bursty guests; memory overcommit and storage overcommit need monitoring.

Resource Practical planning check Failure symptom when underestimated
CPU Count physical cores; leave room for host work; confirm VT-x or AMD-V Guests run but are slow; high steal time; host load average rises
Memory Compare available with the sum of guest RAM plus host reserve Host swaps, the OOM killer selects a guest or libvirt process
Storage Check the pool's filesystem, not just virtual disk sizes qcow2 writes fail with No space left on device; guests pause
Network Separate management, guest, and migration traffic where possible Migration stalls; SSH becomes sluggish; packet loss
Entropy and time Use virtio-rng and a time source; keep host clock correct Slow key generation; clock drift in guests

Do not infer capacity from the number of vCPUs alone. A host with many cores but one memory channel or one saturated disk will fail differently from a host with fewer cores. Measure the workload you intend to run, and keep the host's own monitoring and backup paths independent of the guests.

24.1.2. Installing QEMU, KVM, Libvirt, and Management Tools

Debian splits the stack into packages so that a host can install only the architecture and tools it needs. The following set provides x86-64 virtualization, the libvirt daemon and client, the installation tools, and the helpers used in this chapter. On virt1, as john:

$ sudo apt install qemu-system-x86 qemu-utils libvirt-daemon-system libvirt-clients virtinst cloud-image-utils genisoimage cpu-checker bridge-utils libosinfo-bin libguestfs-tools nfs-kernel-server nftables

The command prompts for confirmation and then installs the packages and their dependencies. qemu-system-x86 contains the x86 system emulator; qemu-utils contains qemu-img and related disk tools; libvirt-daemon-system provides the system service, default networks, and system connection; libvirt-clients provides virsh; virtinst provides virt-install and virt-clone; cloud-image-utils provides cloud-localds, which uses genisoimage to build the seed ISO; cpu-checker provides kvm-ok; libosinfo-bin supplies the operating-system database used by virt-install; bridge-utils provides the legacy brctl helper and is not required on a host managed entirely with ip; libguestfs-tools provides virt-sysprep and virt-customize; nfs-kernel-server exports guest storage for the migration exercises; and nftables is used for the firewall exercises. The second host needs a smaller set. On virt2, as john:

$ sudo apt install qemu-system-x86 qemu-utils libvirt-daemon-system libvirt-clients virtinst nfs-common nftables

Confirm the installed versions. On virt1, as john:

$ dpkg-query -W -f='${Package} ${Version}\n' qemu-system-x86 libvirt-daemon-system libvirt-clients virtinst cloud-image-utils
cloud-image-utils 0.33-1
libvirt-clients 11.3.0-3+deb13u3
libvirt-daemon-system 11.3.0-3+deb13u3
qemu-system-x86 1:10.0.13+ds-0+deb13u1
virtinst 1:5.0.0-5+deb13u1

The epoch prefixes (1:) are part of the Debian version and matter when comparing upgrades. QEMU 10 and libvirt 11 are the versions used for the XML and command syntax in this chapter. A Debian 12 host has older libvirt and may not accept every element shown here.

Installation enables and starts libvirtd through its systemd unit. Check it directly rather than assuming the package did the right thing. On virt1, as john:

$ systemctl is-active libvirtd
active
$ systemctl is-enabled libvirtd
enabled

If the unit is inactive, $ sudo systemctl enable --now libvirtd starts it and makes it start at boot. If it is active but not enabled, the host will lose its management daemon after the next reboot even though guests may have been running.

The system daemon listens on a local Unix socket by default. It does not need a TCP listener for local administration, and exposing one increases the attack surface. Remote management belongs over SSH or TLS with authentication, as described in Chapter 20. The virt-manager graphical console is a separate package and is installed on the administrator's desktop, not necessarily on the host:

Package Purpose Needed on the host?
qemu-system-x86 x86-64 guest emulator and KVM backend Yes
qemu-utils qemu-img, qemu-nbd, image inspection Yes for image work
libvirt-daemon-system libvirtd, default networks, system connection Yes
libvirt-clients virsh and remote client libraries Yes for administration
virtinst virt-install, virt-clone Yes for installation and cloning
cloud-image-utils cloud-localds for NoCloud seed images For cloud images
genisoimage ISO builder used by cloud-localds For cloud images
libguestfs-tools virt-sysprep, virt-customize, offline editing Optional but useful
virt-manager graphical management and console Usually on the desktop
qemu-guest-agent guest-side agent for shutdown, freeze, address reporting Inside guests
nfs-kernel-server / nfs-common shared guest storage for migration Server / target host

On a headless server, virt-manager can run on a workstation and connect to qemu+ssh://john@virt1/system. The host does not need a desktop, but the workstation needs the package and its GTK dependencies. The command line tools are sufficient for everything in this chapter; the graphical path is described where it differs.

24.1.3. System and Session Connections and Administrative Access

Libvirt identifies each management endpoint by a URI. The two local URIs behave differently, and confusing them is a common source of "my guest disappeared" reports. The system connection manages the host-wide daemon, its default networks, and guests that may run before any user logs in. The session connection runs a private daemon as the calling user and is convenient for unprivileged experiments, but it cannot use the host's system networks or storage policy in the same way.

On virt1, as john, the system connection lists the practice guest created in Section 24.2.2:

$ virsh -c qemu:///system list --all
 Id   Name           State
------------------------------
 1    handbook-web   running

The session connection is empty and independent:

$ virsh -c qemu:///session list --all
 Id   Name   State
--------------------

The blank table is the expected result: no guest was defined in john's session. A guest started with the session connection will not appear in the system connection, will not start at boot, and may not be visible to other administrators. Use qemu:///system for a server's guests and for anything that must survive logout.

URI Daemon and identity Networks and storage Typical use
qemu:///system Host-wide libvirtd, privileged service System networks, host bridges, system pools Servers, boot-time guests
qemu:///session Per-user daemon under the caller's UID User-mode networking; user-owned pools Unprivileged testing, desktop VMs
qemu+ssh://user@host/system Remote system connection tunneled over SSH Target host's system resources Administering a remote host
qemu+tls://host/system TLS-authenticated remote connection Target host's system resources Managed remote environments

Local access to the system connection is controlled by the socket's group and by polkit. On Debian, members of the libvirt group can use the system connection without an interactive polkit prompt for ordinary operations. On virt1, as john:

$ id
uid=1001(john) gid=1001(john) groups=1001(john),106(libvirt),991(kvm)

The libvirt and kvm groups are present. If they are missing, add john with $ sudo usermod -aG libvirt,kvm john, then start a new login session; group changes do not apply to existing shells. Do not add ordinary users to libvirt casually: membership is effectively equivalent to root on the host, because a member can define a guest that mounts host block devices or runs arbitrary QEMU arguments. Treat it as an administrative group.

For work that writes root-owned image files and system definitions, a root shell is the straightforward context. On virt1, as john:

$ sudo -i

The prompt changes to #. Setup sequences in this chapter that create pools, networks, and shared storage are entered there; exit returns to john's shell. Running virsh from this shell uses the system connection by default, but this chapter spells out -c qemu:///system in examples so that the target is unambiguous and so that the same command works from an unprivileged session.