How to Export libvirt/KVM Virtual Machines to Files When Network is Unavailable


1 views

When decommissioning a libvirt/KVM host with network connectivity issues, traditional VM migration methods won't work. Here's how to properly export complete VM configurations including disk images when you can't establish network connections.

First identify all VM components:

# List all VMs
virsh list --all

# Get VM XML definition
virsh dumpxml vm_name > vm_name.xml

# Find disk paths
grep -i "source file" vm_name.xml

The complete export requires two components:

# 1. Save VM definition
virsh dumpxml vm_name > vm_name.xml

# 2. Copy disk images (raw or qcow2)
cp /var/lib/libvirt/images/vm_name.qcow2 /backup_path/

For more complex scenarios:

Compressed Export

tar -czvf vm_export.tar.gz vm_name.xml /var/lib/libvirt/images/vm_name.qcow2

Exporting Running VMs

# Create external snapshot
virsh snapshot-create-as vm_name temp_snapshot --disk-only --atomic

# Export original disk (now read-only)
cp /var/lib/libvirt/images/vm_name.qcow2 /backup_path/

# Export snapshot disk
cp /var/lib/libvirt/images/vm_name.snapshot1.qcow2 /backup_path/

Reconstruction process:

# Copy disk images
cp vm_name.qcow2 /var/lib/libvirt/images/

# Define VM
virsh define vm_name.xml

# For snapshots:
virsh snapshot-create vm_name --xmlfile snapshot.xml --redefine

Always validate your exports:

# Check disk integrity
qemu-img check vm_name.qcow2

# Validate XML
virt-xml-validate vm_name.xml

When retiring a KVM host server with network connectivity issues, the standard migration methods like virsh migrate won't work. We need a local export solution that preserves the complete VM state including:

  • Virtual disk images (qcow2, raw, etc.)
  • XML configuration files
  • Metadata and snapshots

First, identify your VMs:

virsh list --all

1. Dump VM Configuration

For each VM, export its XML definition:

virsh dumpxml vm_name > vm_name.xml

2. Locate Disk Images

Check the XML file for disk paths:

grep "source file" vm_name.xml

3. Copy Disk Images

Use rsync (if available) or cp for better integrity:

rsync -avP /var/lib/libvirt/images/vm_disk.qcow2 /mnt/backup/

Exporting Running VMs

For minimal downtime, create external snapshots first:

virsh snapshot-create-as --domain vm_name \
--name "pre_export" \
--disk-only \
--atomic

Compressing Disk Images

Use qemu-img convert for space savings:

qemu-img convert -O qcow2 -c \
/path/original.qcow2 \
/path/compressed.qcow2

On the new host, validate the import:

virt-xml-validate vm_name.xml
virsh define vm_name.xml

Full export script for multiple VMs:

#!/bin/bash
BACKUP_DIR="/mnt/vm_backup/$(date +%Y%m%d)"
mkdir -p "$BACKUP_DIR"

for vm in $(virsh list --name --all); do
    echo "Exporting $vm..."
    virsh dumpxml "$vm" > "$BACKUP_DIR/$vm.xml"
    
    for disk in $(virsh domblklist "$vm" | awk 'NR>2 {print $2}'); do
        cp -v "$disk" "$BACKUP_DIR/"
    done
done

Remember to check SELinux contexts if enabled on the target system.