24.5. Protecting and Moving Guests
A guest is recoverable when both its definition and its disks are recoverable. Backups must capture both, and the restore procedure must be tested while the original is still available. Migration moves a definition and, depending on storage, the disk data; it is not a backup, because a mistake or a failed migration can remove the source.
24.5.1. Backing Up Domain Definitions and Virtual Disks
The domain XML is small and should be backed up whenever it changes, not only when the disk changes. On virt1, in the root shell, create a backup directory:
# mkdir -p /var/lib/libvirt/images/handbook/backup
The command is silent. Write the definition into it:
# virsh -c qemu:///system dumpxml handbook-web > /var/lib/libvirt/images/handbook/backup/handbook-web.xml
The redirection writes the XML to the file; the terminal shows nothing.
dumpxml includes the current definition, including devices added with
--config or --persistent. It does not include the disk contents, the
NVRAM file for a UEFI guest, or external snapshot overlays unless they are
referenced in the XML.
Copy the disk while the guest is shut down. On virt1, as john, with the guest stopped:
$ sudo qemu-img convert -O qcow2 /var/lib/libvirt/images/handbook/handbook-web.qcow2 /var/lib/libvirt/images/handbook/backup/handbook-web.qcow2
The command is silent on success. qemu-img convert reads the source and
writes a new image with the requested format; it does not keep the backing
file, so the backup is self-contained. Check it:
$ sudo qemu-img check /var/lib/libvirt/images/handbook/backup/handbook-web.qcow2
No errors were found on the image.
20580/196608 = 10.47% allocated, 0.00% fragmented, 0.00% compressed clusters
Image end offset: 1349910528
A backup that has never been restored is only a hope. To restore, define a new domain from the saved XML with a new name, a fresh UUID, and the backup disk path. On virt1, in the root shell:
# sed -e '/<backingStore/,/<\/backingStore>/d' \
-e 's#<name>handbook-web</name>#<name>handbook-restore</name>#' \
-e "s#<uuid>[^<]*</uuid>#<uuid>$(uuidgen)</uuid>#" \
-e 's#/var/lib/libvirt/images/handbook/handbook-web.qcow2#/var/lib/libvirt/images/handbook/backup/handbook-web.qcow2#' \
/var/lib/libvirt/images/handbook/backup/handbook-web.xml \
> /var/lib/libvirt/images/handbook/backup/handbook-restore.xml
The sed command rewrites the name, replaces the UUID with a fresh one
from uuidgen - any value that is not already in use works - points the
disk at the backup, and drops the empty backingStore elements that
dumpxml emits, so the restored definition carries no chain references.
The redirection writes the file; nothing is printed. Define the domain:
# virsh -c qemu:///system define /var/lib/libvirt/images/handbook/backup/handbook-restore.xml
Domain 'handbook-restore' defined from /var/lib/libvirt/images/handbook/backup/handbook-restore.xml
Start the restored domain:
# virsh -c qemu:///system start handbook-restore
Domain 'handbook-restore' started
The guest boots with the backup's identity. If the original is still running on the same network, the two guests may conflict over IP addresses, hostnames, or service ports. Give the restored guest a different network or stop the original before starting it. The point of the restore is to prove the backup is complete, not to run two copies of the same service.
Verify the restored guest's data from the host. The restored disk carries the same SSH host key as the original, so its fingerprint is already known and can be recorded in a dedicated known-hosts file; strict checking stays on. On virt1, as john:
$ ssh -i ~/.ssh/ch24-lab -o UserKnownHostsFile=~/.ssh/known_hosts_ch24_restore -o StrictHostKeyChecking=yes john@192.168.236.117 'hostname; sqlite3 ~/debian-handbook/virtualization/app.db "select * from notes;"'
handbook-web
1|before snapshot
The hostname inside the guest is still handbook-web, because the name in
the domain XML and the guest's own hostname are separate identities; the
database row proves the disk contents survived the round trip. If the XML
has more than one disk, update every path; a restore that leaves one disk
pointing at the original is not isolated from the source.
Shut handbook-restore down once the restore is verified, using the
shutdown and state check from Section 24.4.1. The restored guest keeps the
source's MAC address, so it cannot run at the same time as handbook-web
on the same host, and the migration exercises in Section 24.5.2 bring the
original guest back to virt1. The restored domain stays defined and shut
off, ready to be started again for a later restore test.
| Backup component | Where it lives | Included by dumpxml? |
Restore action |
|---|---|---|---|
| Domain XML | libvirt's domain state | Yes, when dumped | virsh define |
| Disk images | Storage pool | No | Copy or convert, then update XML |
| NVRAM for UEFI | /var/lib/libvirt/qemu/nvram |
Referenced path only | Back up and restore the file |
| External snapshots | Storage pool | Referenced chain | Copy the full chain or commit it first |
| Network and pool definitions | libvirt's network/pool state | No | net-dumpxml/pool-dumpxml, then define |
| Application data | Inside the disk | Only as part of the disk | Application-level export |
Back up the network and pool definitions as well if the host itself is
being rebuilt. They are small XML files and can be dumped with
virsh net-dumpxml and virsh pool-dumpxml. Store them with the domain
XML and a note describing which pool paths they expect. The backup and
restore principles in Chapter 15 apply to the host
filesystem and to the guest data separately.
24.5.2. Offline and Live Migration Requirements
Migration moves a domain from one libvirt host to another. Offline migration copies the definition and requires the disk to be available on the target, usually through shared storage. Live migration keeps the guest running while memory is copied, with a brief pause at the switchover. Both need compatible libvirt and QEMU versions, a reachable migration transport, and a target that can satisfy the domain's devices and networks.
For the practice pair, the hosts are joined by 192.168.235.0/24 and both
run libvirt with root SSH access for migration. The target must resolve the
source's name or address, and the source must authenticate to the target.
The transport URI names the remote daemon:
qemu+ssh://root@192.168.235.11/system
The root account is used for the libvirt connection, not for the guest's
login. A non-root account can be used if it has system connection access
and the target's libvirt socket allows it, but SSH key setup and known-host
verification are still required. The target's host key must be verified
through a trusted channel before the first automatic connection; do not
disable host checking to make migration scripts work.
The target must also be able to satisfy the domain's devices. The practice
guest uses three libvirt networks, so virt2 needs the same three network
definitions, started before any migrated guest runs there. Copy the same
XML files to /etc/libvirt/qemu/networks/ on virt2. On virt2, in the root
shell, for each network - handbook-nat, handbook-bridge, and
handbook-isolated - run the pair:
# virsh -c qemu:///system net-define /etc/libvirt/qemu/networks/handbook-nat.xml
# virsh -c qemu:///system net-start handbook-nat
Each subcommand prints a one-line confirmation; repeat the pair for the other two networks. Storage is the main design choice:
| Storage arrangement | Offline migration | Live migration | Operational notes |
|---|---|---|---|
| Shared NFS, iSCSI, or SAN | Disk already visible on target | Supported; no disk copy | Requires consistent path names and permissions |
| Local disk on source | Copy disk to target first | --copy-storage-all copies while running |
Slow for large disks; target needs space |
| Local disk with block migration | Copy disk to target | --copy-storage-inc copies only changed blocks |
Sensitive to I/O load and network bandwidth |
| No shared storage, no copy | Not possible | Not possible | Fails with a storage error |
The practice NFS export makes the pool path available on both hosts. On
virt1, the file /etc/exports contains:
# Chapter 24 lab export for offline migration
/var/lib/libvirt/images/handbook 192.168.235.11(rw,sync,no_subtree_check,no_root_squash)
no_root_squash is required here because libvirt on the target writes as
root-owned QEMU processes; on a production export, restrict the client and
use a dedicated storage network, as discussed in
Chapter 21. Apply the export. On virt1, in
the root shell:
# systemctl restart nfs-server
The restart is silent and makes the NFS server read the new file. Confirm the active exports:
# exportfs -v
/var/lib/libvirt/images/handbook
192.168.235.11(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,no_root_squash,no_all_squash)
On virt2, in the root shell, create the mount point, mount the export at the same path so the XML's disk path resolves identically, and prepare the pool:
# install -d -m 755 /var/lib/libvirt/images/handbook
# mount -t nfs 192.168.235.10:/var/lib/libvirt/images/handbook /var/lib/libvirt/images/handbook
# virsh -c qemu:///system pool-define-as handbook dir --target /var/lib/libvirt/images/handbook
# virsh -c qemu:///system pool-build handbook
# virsh -c qemu:///system pool-start handbook
# virsh -c qemu:///system pool-refresh handbook
The mount is silent and the pool subcommands print one-line confirmations. Confirm the mount:
# mount | grep handbook
192.168.235.10:/var/lib/libvirt/images/handbook on /var/lib/libvirt/images/handbook type nfs4 (rw,relatime,vers=4.2,rsize=524288,wsize=524288,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=192.168.235.11,local_lock=none,addr=192.168.235.10)
With the guest shut down, migrate the definition and leave the source undefined. On virt1, in the root shell:
# virsh migrate --offline --persistent --undefinesource --verbose handbook-web qemu+ssh://root@192.168.235.11/system
Migration: [100.00 %]
--offline moves the definition without starting or copying a running
guest; --persistent keeps the target definition; --undefinesource
removes the source definition after success. --verbose prints the one
progress line. Verify the result from the source, using the target's URI;
on virt1, in the root shell:
# virsh -c qemu+ssh://root@192.168.235.11/system list --all
Id Name State
-------------------------------
- handbook-web shut off
The migrated domain appears on the target with its disk path resolving through the shared mount. Start it there. On virt2, in the root shell:
# virsh -c qemu:///system start handbook-web
Domain 'handbook-web' started
If the target cannot see the disk, the start fails with a storage error; fix the mount or the path before defining the domain again. Verify the migrated guest's data. On virt2, as john:
$ ssh -i ~/.ssh/ch24-lab -o UserKnownHostsFile=~/.ssh/known_hosts_ch24_mig -o StrictHostKeyChecking=yes john@192.168.236.117 'hostname; sqlite3 ~/debian-handbook/virtualization/app.db "select * from notes;"'
handbook-web
1|before snapshot
The same host key covers the connection, because the migrated guest is the
same machine with the same disk; the dedicated known-hosts file keeps the
exercise's records separate while strict checking stays on. Moving the
domain back is the same command with the source and target exchanged.
Shut the guest down on virt2 - virsh -c qemu:///system shutdown
handbook-web, as in Section 24.4.1 - and wait for shut off. On virt2,
in the root shell:
# virsh migrate --offline --persistent --undefinesource --verbose handbook-web qemu+ssh://root@192.168.235.10/system
Migration: [100.00 %]
The command prints the same progress line as the move to virt2 and returns to the prompt on success.
Live migration requires the guest to be running and the target to have
enough memory and CPU. When the storage is not shared, --copy-storage-all
streams the guest's disks to the target as part of the migration. The
target writes each disk to the path named in the domain XML, so a migration
into a different target pool needs an XML override that rewrites the paths.
On virt2, in the root shell, prepare a local pool for the incoming disk:
# install -d -m 755 /var/lib/libvirt/images/handbook-mig
# virsh -c qemu:///system pool-define-as handbook-mig dir --target /var/lib/libvirt/images/handbook-mig
# virsh -c qemu:///system pool-build handbook-mig
# virsh -c qemu:///system pool-start handbook-mig
Each pool subcommand prints a one-line confirmation. If the guest's disk chain references a backing file, that file must already exist at the corresponding target path, because the migration copies the disks the guest writes, not the read-only base. The practice guest's overlay records the cloud image as its backing file, so copy the base image into the target pool:
# cp /var/lib/libvirt/images/handbook/debian-13-genericcloud-amd64.qcow2 /var/lib/libvirt/images/handbook-mig/debian-13-genericcloud-amd64.qcow2
The command is silent on success. Prepare the XML override on the source. On virt1, in the root shell:
# virsh -c qemu:///system dumpxml handbook-web > /tmp/handbook-web-mig.xml
# sed -i 's#/var/lib/libvirt/images/handbook/handbook-web.qcow2#/var/lib/libvirt/images/handbook-mig/handbook-web.qcow2#; s#/var/lib/libvirt/images/handbook/handbook-data.qcow2#/var/lib/libvirt/images/handbook-mig/handbook-data.qcow2#; s#/var/lib/libvirt/images/handbook/debian-13-genericcloud-amd64.qcow2#/var/lib/libvirt/images/handbook-mig/debian-13-genericcloud-amd64.qcow2#' /tmp/handbook-web-mig.xml
The sed is silent; neither command prints anything - the redirection
saves the XML and sed -i edits it in place. The substitutions rewrite
the guest's disk, the data disk, and the
backing-file paths to the target pool in one pass. The practice domain has
one disk at this point, so the first substitution does the work and the
others are harmless - a domain that still carries a data disk or a chain
gets every path moved the same way. Start the guest on the source, where
the offline move left it shut off. On virt1, in the root shell:
# virsh -c qemu:///system start handbook-web
Domain 'handbook-web' started
Migrate it. On virt1, in the root shell:
# virsh migrate --live --persistent --undefinesource --copy-storage-all --xml /tmp/handbook-web-mig.xml --verbose handbook-web qemu+ssh://root@192.168.235.11/system
Migration: [100.00 %]
--live keeps the guest running during the memory copy; --copy-storage-all
copies the full disk to the paths in the XML override; --xml supplies that
override; and --persistent with --undefinesource moves the definition
as in the offline case. --verbose rewrites a single progress line in
place as memory and disk pages stream across; the line reads 100.00 %
when the switchover completes and the source releases the domain. Confirm
the outcome on both hosts. On virt1, in the root shell:
# virsh -c qemu:///system list --all
Id Name State
-----------------------------------
3 handbook-clone running
- handbook-restore shut off
On virt2, in the root shell:
# virsh -c qemu:///system list --all
Id Name State
------------------------------
2 handbook-web running
The source no longer defines the migrated domain; the target runs it. Verify the guest's data from the target host. On virt2, as john:
$ ssh -i ~/.ssh/ch24-lab -o UserKnownHostsFile=~/.ssh/known_hosts_ch24_mig -o StrictHostKeyChecking=yes john@192.168.236.117 'hostname; sqlite3 ~/debian-handbook/virtualization/app.db "select * from notes;"'
handbook-web
1|before snapshot
The migration options:
| Migration option | Effect | When to use |
|---|---|---|
--offline |
Moves definition only; guest stopped | Shared storage, planned host maintenance |
--live |
Keeps guest running during memory copy | Planned maintenance with minimal downtime |
--persistent |
Keeps target definition after migration | Normal server migration |
--undefinesource |
Removes source definition on success | Move, not copy |
--copy-storage-all |
Copies the full disk to the target | No shared storage, enough time and space |
--copy-storage-inc |
Copies changed blocks only | Repeated migrations with a common base |
--xml FILE |
Target-side XML with rewritten paths | Non-shared storage with different target paths |
--unsafe |
Allows migration without safety checks | Only when you understand the risk |
Live migration can fail for reasons that are visible only in the logs:
incompatible CPU features, a missing network on the target, insufficient
memory, a storage path that differs, or a firewall blocking the migration
port. libvirt reports the high-level error; the source's
/var/log/libvirt/qemu/<domain>.log and the target's libvirtd journal carry
the detail. Do not retry in a loop against a full target; it increases load
and can leave a paused domain.
24.5.3. Storage Exhaustion, Guest Boot Failures, and Recovery
Thin provisioning makes storage exhaustion a normal operational event, not an exceptional one. A qcow2 file grows cluster by cluster, a thin LVM pool allocates extents on write, and a full filesystem turns guest writes into I/O errors. The host may pause a guest when QEMU cannot write, which looks like a hang from inside the guest. Monitor the pool's filesystem and the guest's own free space, and leave headroom for snapshots and backups.
A small filesystem makes the failure reproducible without touching real guests. On virt1, in the root shell, create a 64 MiB image, format it, and mount it as a pool target:
# truncate -s 64M /var/lib/libvirt/images/tiny.img
# mkfs.ext4 -q -F /var/lib/libvirt/images/tiny.img
# mkdir -p /var/lib/libvirt/images/tinyfs
# mount -o loop /var/lib/libvirt/images/tiny.img /var/lib/libvirt/images/tinyfs
The four commands form one setup sequence and print nothing on success;
mount -o loop attaches the file as the loop device /dev/loop0, the
mechanism used for image mounts in Chapter 11.
Define a directory pool on it - this is the same command family used for
the handbook pool in Section 24.2.1, with its captured output:
# virsh -c qemu:///system pool-define-as tiny dir --target /var/lib/libvirt/images/tinyfs && virsh -c qemu:///system pool-build tiny && virsh -c qemu:///system pool-start tiny
Pool tiny defined
Pool tiny built
Pool tiny started
The three subcommands chained with && form one define-build-start
operation; each prints a one-line confirmation, and the sequence stops at
the first failure. Fill the filesystem:
# dd if=/dev/zero of=/var/lib/libvirt/images/tinyfs/fill bs=1M
dd: error writing '/var/lib/libvirt/images/tinyfs/fill': No space left on device
54+0 records in
53+0 records out
55992320 bytes (56 MB, 53 MiB) copied, 1.96936 s, 28.4 MB/s
dd writes from /dev/zero until the filesystem is full. It then reports
the failed write and stops with a nonzero exit status; the partially
written file remains. The error is the expected end of the fill, not a
failure to investigate. Confirm that the pool target is full:
# df -h /var/lib/libvirt/images/tinyfs | tail -1
/dev/loop0 55M 54M 0 100% /var/lib/libvirt/images/tinyfs
The loop filesystem behind the pool is at 100 percent. A volume creation now fails:
# virsh -c qemu:///system vol-create-as tiny too-large.qcow2 20M --format qcow2
error: Failed to create vol too-large.qcow2
error: internal error: Child process (/usr/bin/qemu-img create -f qcow2 -o compat=1.1 /var/lib/libvirt/images/tinyfs/too-large.qcow2 20480K) unexpected exit status 1: qemu-img: /var/lib/libvirt/images/tinyfs/too-large.qcow2: Could not write qcow2 header: No space left on device
libvirt reports the underlying qemu-img failure, and no usable volume is
left behind. Remove the filler and retry:
# rm -f /var/lib/libvirt/images/tinyfs/fill
The removal is silent. Retry the volume that failed:
# virsh -c qemu:///system vol-create-as tiny recovered.qcow2 8M --format qcow2
Vol recovered.qcow2 created
The retry succeeds because the pool has space again. The lesson is to alert
on the backing filesystem before it reaches 100 percent, and to keep
snapshot and backup space outside the pool that guests write to. A full
pool can also make snapshot-delete fail, because block commit needs
temporary space. When the exercise is finished, remove its fixtures: stop
and undefine the tiny pool, unmount the loop filesystem, and delete
tiny.img - on virt1, in the root shell:
# virsh -c qemu:///system pool-destroy tiny
# virsh -c qemu:///system pool-undefine tiny
# umount /var/lib/libvirt/images/tinyfs
# rm -f /var/lib/libvirt/images/tiny.img
The pool-destroy and pool-undefine subcommands print one-line
confirmations; the umount and rm are silent. Nothing outside the
exercise's own files is touched.
Guest boot failures usually come from a missing or corrupt disk, a changed UUID, a missing network, or a bootloader that no longer finds its root. Start with the domain state and the disk list. On virt1, as john:
$ virsh -c qemu:///system domstate handbook-web
shut off
$ virsh -c qemu:///system domblklist handbook-web
Target Source
---------------------------------------------------------------
vda /var/lib/libvirt/images/handbook/handbook-web.qcow2
If a path is missing, start fails before the guest boots. To see the
failure, define a practice domain whose disk points at a file that does not
exist. On virt1, in the root shell, make a copy of the backup XML with a
new name, a fresh UUID, and the disk rewritten to missing.qcow2:
# sed -e 's#<name>handbook-web</name>#<name>handbook-broken</name>#' \
-e "s#<uuid>[^<]*</uuid>#<uuid>$(uuidgen)</uuid>#" \
-e 's#/var/lib/libvirt/images/handbook/handbook-web.qcow2#/var/lib/libvirt/images/handbook/missing.qcow2#' \
/var/lib/libvirt/images/handbook/backup/handbook-web.xml \
> /var/lib/libvirt/images/handbook/backup/handbook-broken.xml
The redirection writes the transformed XML; nothing is printed. Define the domain from it:
# virsh -c qemu:///system define /var/lib/libvirt/images/handbook/backup/handbook-broken.xml
Domain 'handbook-broken' defined from /var/lib/libvirt/images/handbook/backup/handbook-broken.xml
The define confirms the new domain. Now start it:
# virsh -c qemu:///system start handbook-broken
error: Failed to start domain 'handbook-broken'
error: Cannot access storage file '/var/lib/libvirt/images/handbook/missing.qcow2' (as uid:64055, gid:64055): No such file or directory
The message names the missing path and the identity that tried to open it:
uid:64055 is the libvirt-qemu user, the identity QEMU runs as, so
libvirt's pre-start check catches the problem before the guest gets any
CPU time. The same check fails when a path exists but is not accessible to
that user, for example with restrictive ownership and libvirt's relabeling
disabled. Recover by restoring real data at the expected path -
never by creating an empty file at the missing path, which gives the guest
a blank disk and a more confusing failure. The backup from
Section 24.5.1 provides a genuine copy:
# cp /var/lib/libvirt/images/handbook/backup/handbook-web.qcow2 /var/lib/libvirt/images/handbook/missing.qcow2 && virsh -c qemu:///system start handbook-broken
Domain 'handbook-broken' started
The cp is silent; the start confirms that the domain now boots. When
the recovery is verified, shut the practice domain down and remove both the
domain and the copy. For a corrupt filesystem, boot the guest from rescue
media, run fsck on the affected filesystem, and check the guest's logs.
For a broken bootloader, chroot from rescue media or use virt-rescue on
the disk. The recovery techniques of Chapter 16 apply
inside the guest; the host-side difference is that you may need to attach
the disk to a rescue domain or use guestfish because the guest cannot
boot on its own.
Keep a known-good domain XML and disk copy before changing a bootloader, partition table, or encryption header. A snapshot can help with a short-lived change, but it is not a substitute for an offline backup when the guest will not boot.
| Symptom | Likely cause | First check | Recovery |
|---|---|---|---|
start fails immediately |
Missing disk or NVRAM file | domblklist, XML paths |
Restore file or correct path |
| Guest boots to emergency shell | Root filesystem or fstab problem | Guest console, rescue media | fsck, fix /etc/fstab |
| Guest pauses under load | Pool or filesystem full | Host df, QEMU log |
Free space, resume guest |
| Snapshot delete fails | Missing parent or permissions | qemu-img info -U, ls -l |
Repair chain, fix ownership |
| Network missing after clone | Stale MAC or interface name | ip link, netplan |
Regenerate netplan or udev rules |
24.5.4. Hardware Passthrough Requirements and Isolation Limits
PCI passthrough gives a guest direct control of a physical device, such as
a network card, NVMe controller, or GPU. The device is removed from the
host's normal driver and bound to vfio-pci; the guest then sees it as a
real PCI device. This provides near-native performance and access to
features that emulation cannot provide, but it reduces host isolation and
makes migration and snapshots much harder.
The host needs an IOMMU. On Intel systems, enable VT-d in firmware and add
intel_iommu=on iommu=pt to the kernel command line; on AMD, enable AMD-Vi
and use amd_iommu=on iommu=pt. The kernel must report IOMMU groups, and
the device must be in a group whose other members can also be passed
through or are safe to ignore. Check the inventory on the practice host.
On virt1, in the root shell:
# lspci -nnk | head -20
00:00.0 Host bridge [0600]: Intel Corporation 440FX - 82441FX PMC [Natoma] [8086:1237] (rev 02)
Subsystem: Red Hat, Inc. Qemu virtual machine [1af4:1100]
00:01.0 ISA bridge [0601]: Intel Corporation 82371SB PIIX3 ISA [Natoma/Triton II] [8086:7000]
Subsystem: Red Hat, Inc. Qemu virtual machine [1af4:1100]
00:01.1 IDE interface [0101]: Intel Corporation 82371SB PIIX3 IDE [Natoma/Triton II] [8086:7010]
Subsystem: Red Hat, Inc. Qemu virtual machine [1af4:1100]
Kernel driver in use: ata_piix
Kernel modules: ata_piix, ata_generic
[...]
Selected output; the pipe keeps the first 20 lines, and further lines are omitted here. The devices are the emulated chipset of a virtual machine, not physical hardware. The decisive check is the IOMMU group directory:
# ls /sys/kernel/iommu_groups 2>/dev/null | wc -l
0
The count is zero: this host has no IOMMU groups, because it is a nested
guest without a firmware-assigned IOMMU, so VFIO assignment is not
possible here. The 2>/dev/null in the pipeline is part of the tested
form: it keeps ls's error message off the terminal when the directory
does not exist, which is exactly the case here. On a physical host with
VT-d or AMD-Vi enabled, the
directory lists numbered groups and ls /sys/kernel/iommu_groups/<N>/devices/
shows each group's members. libvirt's own inventory works regardless:
# virsh -c qemu:///system nodedev-list --cap pci | head -10
pci_0000_00_00_0
pci_0000_00_01_0
pci_0000_00_01_1
pci_0000_00_01_2
pci_0000_00_01_3
pci_0000_00_02_0
pci_0000_00_03_0
pci_0000_00_04_0
pci_0000_00_05_0
pci_0000_00_06_0
nodedev-list enumerates the host's PCI devices as libvirt sees them; the
practice host offers only emulated devices. To assign a device on hardware
that supports it, identify its PCI address, unbind it from the host
driver, bind it to vfio-pci, and add a hostdev entry to the domain. A
representative domain fragment is:
<hostdev mode='subsystem' type='pci' managed='yes'>
<source>
<address domain='0x0000' bus='0x3b' slot='0x00' function='0x0'/>
</source>
</hostdev>
managed='yes' asks libvirt to detach the device from the host driver and
reattach it when the guest stops. The device must not be in use by the host
or another guest. For a GPU, the guest also needs the vendor driver and
firmware; a desktop guest may need the GPU as its primary display, which
requires additional configuration and is outside the scope of a headless
server. For a network card, the guest sees a physical NIC and manages it
with its own driver; the host loses that port.
SR-IOV is a different model: a physical function (PF) creates virtual
functions (VFs), and each VF can be assigned to a guest. The host keeps the
PF driver and configures the VFs; guests see a lighter-weight device. It
requires firmware and driver support and is not available on the virtual
NICs on the practice host. Mediated devices (mdev) provide a similar
sharing model for GPUs that support it, but the vendor stack and guest
driver must match.
| Approach | Performance | Host isolation | Migration | Hardware requirement |
|---|---|---|---|---|
| Virtio emulation | Good for most I/O | Strong; host controls device | Supported | None beyond virtio |
| PCI passthrough | Near-native | Weak; guest owns device | Usually unsupported | IOMMU, VFIO, spare device |
| SR-IOV VF | Near-native, shared PF | Moderate; PF remains on host | Often unsupported | SR-IOV-capable NIC/GPU |
| Mediated device | Vendor-dependent | Moderate | Vendor-dependent | Vendor GPU and driver |
| USB passthrough | Adequate for peripherals | Weak for that device | Unsupported while attached | Spare USB controller/device |
Passthrough is appropriate when a guest needs a device that cannot be virtualized well: a specific GPU for compute, a low-latency NIC, or a hardware security module. It is a poor default for ordinary disks and networks, where virtio is faster to manage, survives migration, and keeps the host in control. Before assigning a device, confirm that the host can lose it without losing management access, and keep a recovery path that does not depend on the guest.
Isolation is not binary. A guest with a passed-through device can perform DMA to memory the IOMMU allows; a firmware or driver bug can affect the host. Keep the host firmware and microcode updated, use ACS where the platform supports it, and do not pass through a device that shares an IOMMU group with a device the host needs. For untrusted workloads, containers or VMs with virtio devices and a confined AppArmor profile are safer than passthrough.
The chapter's virtualization service is complete when a guest can be created from a cloud image or the installer, reached over the network, resized, cloned without identity conflicts, snapshotted consistently, backed up with its definition and disk, and moved to a second host with a tested storage arrangement. The next chapter, Chapter 25, covers containers, which share the host kernel and therefore have a different isolation and operations model.