The VM XML structure in libvirt defines how a virtual machine works. It controls CPU, memory, storage, and networking.
In this guide, we will use Debian GNU/Linux 12 (Bookworm). All future examples in this series also use this version.
If you manage virtual machines with libvirt, you must understand this XML file. Because of this, troubleshooting and customization become easier.
Table of Contents
What Is a Libvirt Domain XML?
In libvirt, each virtual machine is called a domain. Libvirt stores its configuration in an XML file.
You can view it using:
virsh dumpxml vm-name
You can edit it using:
virsh edit vm-name
However, always stop the VM before making major changes.
Basic Structure of VM XML Structure in Libvirt
Below is a simplified example:
<domain type='kvm'>
<name>debian12-vm</name>
<memory unit='MiB'>2048</memory>
<vcpu>2</vcpu> <os>
<type arch='x86_64'>hvm</type>
</os> <devices>
<disk type='file' device='disk'>
<source file='/var/lib/libvirt/images/debian12.qcow2'/>
<target dev='vda' bus='virtio'/>
</disk> <interface type='network'>
<source network='default'/>
<model type='virtio'/>
</interface>
</devices>
</domain>
Now let us break this down step by step.
Main Sections of VM XML Structure in Libvirt
1. <domain> – VM Type
The <domain> tag defines the hypervisor type.
For example:
type='kvm'for KVM accelerationtype='qemu'for software emulation
Most Debian 12 setups use KVM.
2. <name> – Virtual Machine Name
This defines the VM name shown in virsh list.
Keep it simple and descriptive.
3. <memory> – RAM Allocation
This section defines RAM.
Example:
<memory unit='MiB'>2048</memory>
This means the VM uses 2 GB RAM.
However, ensure your host has enough memory available.
4. <vcpu> – CPU Allocation
This tag defines how many virtual CPUs the VM uses.
Example:
<vcpu>2</vcpu>
Because CPU affects performance, assign wisely.
5. <os> – Operating System Type
This section defines architecture and virtualization mode.
Example:
<type arch='x86_64'>hvm</type>
x86_64means 64-bit architecturehvmmeans hardware virtualization
6. <devices> – Hardware Components
This section is very important. It defines disks, network, graphics, and more.
Disk Configuration in VM XML Structure in Libvirt
Example:
<disk type='file' device='disk'>
<source file='/var/lib/libvirt/images/debian12.qcow2'/>
<target dev='vda' bus='virtio'/>
</disk>
Here:
sourcedefines disk image locationqcow2is a common disk formatvirtioimproves disk performance
Because of virtio drivers, performance increases significantly.
Network Configuration in VM XML Structure in Libvirt
Example:
<interface type='network'>
<source network='default'/>
<model type='virtio'/>
</interface>
This connects the VM to the default libvirt NAT network.
You can also configure:
- Bridge networking
- Direct physical interface
- VLAN tagging
However, NAT works best for beginners.
Graphics and Console Section
Many VMs include:
<graphics type='vnc' port='-1'/>
This enables VNC console access.
You can also configure:
- SPICE
- Serial console
- Headless mode
Choose based on your use case.
How to Safely Edit VM XML
Follow these steps:
- Shut down the VM
- Run
virsh edit vm-name - Modify carefully
- Save and exit
- Start the VM
Because XML is strict, even one wrong character can break the configuration.
Therefore, always double-check before saving.
Why Understanding VM XML Structure in Libvirt Matters
When you understand the VM XML structure in libvirt, you gain full control.
For example, you can:
- Increase memory
- Add additional disks
- Change CPU model
- Modify network type
- Enable advanced features
As a result, you can fine-tune performance and behavior.
FAQ Section
Usually in /etc/libvirt/qemu/.
Yes, but using virsh edit is safer.
The VM will fail to start.
Yes, use virsh dumpxml vm-name > backup.xml
No. Changes apply after restart.
Conclusion
The VM XML structure in libvirt defines every part of a virtual machine. It controls CPU, memory, storage, networking, and devices.
If you use Debian GNU/Linux 12 (Bookworm), mastering this structure is essential. Because of this knowledge, you can confidently manage and optimize your virtual machines.
In the next article, we will modify VM XML to add disks and change network settings.





