Skip to content

libvirt domain XML — the schema operators actually read

A walk through the libvirt domain XML — CPU, memory, devices, networking, security — so an operator can edit a guest definition by hand with confidence.

Every libvirt guest is described by a single XML document — the “domain” definition. virsh edits it, virt-manager edits it, your config-management tools render it. An operator who can read this XML can diagnose a guest without touching the GUI. This article walks the schema from outside in: the elements you set on every guest, the elements you set on some guests, and the elements you reach for when something is wrong.

How to verify

Dump the XML of a running guest and pipe it through xmllint for indentation. The full schema documentation lives in /usr/share/libvirt/schemas/ if you want the authoritative answer.

virsh -c qemu:///system dumpxml web01 | xmllint --format -
ls /usr/share/libvirt/schemas/ | head
xmllint --noout --schema /usr/share/libvirt/schemas/domain.rng /etc/libvirt/qemu/web01.xml
xmllint --xpath '//domain/devices/disk' <(virsh dumpxml web01)

The xmllint --schema validation is what virsh define runs under the hood — passing it locally means a virsh define will accept the file too.

What’s happening

The root element is <domain type='kvm'>. Inside, libvirt expects a small required set (name, uuid, memory, vcpu, os, devices) and accepts a long optional set (CPU model, NUMA topology, hugepages, security, hypervisor features). On virsh define, libvirt parses the XML, validates it against domain.rng, normalizes a few things (adds a UUID if absent, fills in default device addresses), and writes it to /etc/libvirt/qemu/<name>.xml. The on-disk file is canonical; what virsh dumpxml emits for a running guest may include runtime-only fields that don’t survive a restart.

The schema versions over time. New libvirt releases add elements; old ones stay backward compatible. Pinning the QEMU machine version (<os><type machine='q35-7.2'>) protects guests from device shuffling when QEMU is upgraded, the same way you’d pin a container base image.

The procedure

  1. The required spine. Every domain has these elements; if any are missing, virsh define rejects the XML:

    <domain type='kvm'>
      <name>web01</name>
      <uuid>f47ac10b-58cc-4372-a567-0e02b2c3d479</uuid>
      <memory unit='KiB'>2097152</memory>
      <currentMemory unit='KiB'>2097152</currentMemory>
      <vcpu placement='static'>2</vcpu>
      <os>
        <type arch='x86_64' machine='q35-7.2'>hvm</type>
        <boot dev='hd'/>
      </os>
      <devices>
        <emulator>/usr/bin/qemu-system-x86_64</emulator>
        <!-- disks, interfaces, console go here -->
      </devices>
    </domain>
  2. CPU model and features. Three modes matter: host-passthrough exposes the physical CPU as-is, host-model exposes a close-enough match, custom pins a named model. For production clusters that live-migrate, custom with a baseline shared across hosts is the safe choice:

    <cpu mode='custom' match='exact'>
      <model fallback='forbid'>Haswell-noTSX</model>
      <topology sockets='1' dies='1' cores='2' threads='1'/>
      <feature policy='require' name='vmx'/>
    </cpu>
  3. Memory and hugepages. memory is the maximum; currentMemory is the current allocation (ballooning). For workloads sensitive to TLB misses, request hugepages:

    <memoryBacking>
      <hugepages>
        <page size='2048' unit='KiB'/>
      </hugepages>
    </memoryBacking>
  4. Disks — virtio-blk, virtio-scsi, raw vs qcow2. virtio-scsi supports more devices and TRIM (discard); virtio-blk is simpler. qcow2 supports snapshots and backing files; raw is faster and easier to back up at the host level. Always set discard='unmap' so the guest’s TRIM operations reach the host filesystem:

    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2' cache='none' io='native' discard='unmap'/>
      <source file='/var/lib/libvirt/images/web01.qcow2'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x04' slot='0x00' function='0x0'/>
    </disk>
  5. Network interfaces — bridge, network, direct. bridge attaches to an existing Linux bridge; network attaches to a libvirt-managed network; direct (macvtap) bypasses the host bridge entirely. For most server VMs, bridge is what you want:

    <interface type='bridge'>
      <mac address='52:54:00:11:22:33'/>
      <source bridge='br0'/>
      <model type='virtio'/>
      <driver name='vhost' queues='4'/>
    </interface>
  6. Console, graphics, channels. Cloud images expect a serial console; Windows installers expect a framebuffer. The qemu-ga channel is what the guest agent uses for fsfreeze:

    <serial type='pty'><target type='isa-serial' port='0'/></serial>
    <console type='pty'><target type='serial' port='0'/></console>
    <graphics type='vnc' port='-1' autoport='yes' listen='127.0.0.1'/>
    <channel type='unix'>
      <source mode='bind' path='/var/lib/libvirt/qemu/channel/target/web01.org.qemu.guest_agent.0'/>
      <target type='virtio' name='org.qemu.guest_agent.0'/>
    </channel>
  7. Security — SELinux/AppArmor labels. The <seclabel> element decides who confines the QEMU process. dynamic lets libvirt allocate per-guest MCS categories — almost always what you want:

    <seclabel type='dynamic' model='selinux' relabel='yes'/>

Operational notes

  • virsh dumpxml on a running guest includes a <seclabel> with the active label and runtime address fields; what you want for version control is virsh dumpxml --inactive, which emits the persistent definition.
  • Editing the XML directly in /etc/libvirt/qemu/ and then SIGHUP’ing libvirt does NOT work — libvirt only re-reads on virsh define. Always go through virsh.
  • <address> elements (PCI slot/function, USB port) are usually auto-assigned; manually setting them is necessary when you live-migrate and need the guest’s PCI topology to remain identical.
  • A <cpu mode='host-passthrough'> guest cannot live-migrate to a host with a different CPU family — the receiving QEMU will refuse it. Clusters need custom with a common baseline.
  • The XML for a domain that fails to start is sometimes more useful than the error message — virsh dumpxml it, look for missing devices, mismatched paths, or a <source file> pointing at storage that isn’t mounted.

Stack Harbor treats the libvirt XML as code — every guest definition is rendered from a template, every change is reviewed, every host’s set of guests is reconstructable from the configuration repo. The clients who let virsh edits accumulate as untracked changes get that brought back under version control as part of the onboarding into Managed Operations.