Libvirt Basics

Libvirt is the management layer that sits between you and KVM/QEMU. It manages networking (virtual switches, bridges, NAT), storage (pools of disk images), and provides the virsh API. Understanding libvirt's network and storage models is necessary for anything beyond basic VM creation — particularly for making VMs accessible on the local network (bridged networking) and for organizing disk image storage.

Libvirt networking

Libvirt network modes:
  NAT (default):
    VMs → virbr0 (virtual bridge) → NAT → host's physical NIC → internet
    VMs can reach internet, but are NOT reachable from external hosts
    VMs get IPs from 192.168.122.0/24

  Bridged (production use):
    VMs → bridge (br0) → physical NIC → network
    VMs appear on same network as host, get IPs from DHCP
    VMs are directly reachable from local network and internet
# List networks:
virsh net-list --all

# Show NAT network details:
virsh net-info default
virsh net-dumpxml default    # Full XML config

# Create a bridged network (for VMs on LAN):
# First create bridge in /etc/netplan/01-network.conf:
# bridges:
#   br0:
#     interfaces: [eth0]
#     dhcp4: true
sudo netplan apply

# Then create libvirt bridge network:
cat > /tmp/bridge-network.xml << 'EOF'

  bridge-network
  
  

EOF
virsh net-define /tmp/bridge-network.xml
virsh net-start bridge-network
virsh net-autostart bridge-network

Storage pools and volumes

# List storage pools:
virsh pool-list --all

# Default pool is at /var/lib/libvirt/images/
virsh pool-info default

# List disk images (volumes) in the default pool:
virsh vol-list default

# Create a new disk image:
virsh vol-create-as default vm-disk.qcow2 20G --format qcow2

# Resize a volume:
virsh vol-resize --pool default vm-disk.qcow2 30G

# Show disk image info:
sudo qemu-img info /var/lib/libvirt/images/ubuntu-test.qcow2

qemu-img info output

image: /var/lib/libvirt/images/ubuntu-test.qcow2
file format: qcow2
virtual size: 20 GiB (21474836480 bytes)
disk size: 3.45 GiB     ← actual disk usage (QCOW2 is sparse)

VM XML configuration

# Every libvirt VM is defined by an XML file:
virsh dumpxml ubuntu-test > /backup/ubuntu-test.xml    # Export config

# Key XML sections:
# : RAM allocation
# : CPU count
# : boot firmware and device order
# : disks, NICs, console
# : each disk image attached

# Restore a VM from XML (if VM was deleted):
virsh define /backup/ubuntu-test.xml    # Recreate VM definition (not data)

Conclusion

For production VMs that need to be reachable from the local network, switch from the default NAT network to a bridged network. The default NAT works fine for isolated development VMs. Use virsh dumpxml to export VM configuration to XML — this is your VM configuration backup (separate from the disk image data backup). Store the XML alongside your disk image backups so a complete VM can be reconstructed.

FAQ

Why should administrators understand Libvirt Basics?+

Because this topic affects planning decisions, server lifecycle, compatibility, support expectations, or how you reason about Ubuntu systems before making operational changes.

Do I need a lab for this topic?+

A lab is useful for checking commands and seeing the concept on a real Ubuntu machine, but the main value is understanding the decision, tradeoff, or system behavior clearly.

How should I use this knowledge in production?+

Use it to make better choices, document why those choices were made, and avoid rushed changes that ignore support windows, compatibility, stability, or operational risk.

Need help with Ubuntu administration?

Work directly with Muhammad Irfan Aslam for Ubuntu Server, Linux, cloud, Docker, DevOps, CI/CD, or infrastructure troubleshooting support.

Hire Me for Support