How to Migrate a KVM/QEMU Virtual Machine to Another Server Without Shared Storage


2 views

When migrating KVM VMs between hosts without shared storage, you're essentially dealing with two main components:

  1. The virtual disk image (typically .img or .qcow2 files)
  2. The VM configuration XML (stored in /etc/libvirt/qemu/)

Here's the complete procedure I've successfully used in production environments:

# On source host
virsh dumpxml original-vm > vm-config.xml
rsync -avzP /var/lib/libvirt/images/original-vm.img user@newhost:/local/vm/cloned-vm.img

# On destination host
virsh define vm-config.xml
virsh edit cloned-vm  # Make necessary adjustments

After copying the files, you must modify these elements in the XML:

<uuid>NEW_UUID_HERE</uuid>
<mac address='NE:W_:MA:C_:AD:DR'/>
<source file='/local/vm/cloned-vm.img'/>

The "Connection reset by peer" error typically occurs due to:

  • Incorrect disk image permissions (ensure libvirt-qemu:kvm owns the file)
  • Undefined SELinux contexts (run restorecon -Rv /local/vm/)
  • Mismatched machine type (verify machine='rhel6.2.0' matches target hypervisor)

For large VMs or frequent migrations, consider these approaches:

# Method 1: Using virt-clone (requires temporary storage)
virt-clone --original original-vm --name cloned-vm --file /local/vm/cloned-vm.img
scp /local/vm/cloned-vm.img newhost:/local/vm/

# Method 2: Direct block device copy
dd if=/dev/vg/original-vm of=/mnt/nfs/cloned-vm.img bs=4M
ssh newhost "dd of=/local/vm/cloned-vm.img bs=4M" < /mnt/nfs/cloned-vm.img

Always verify these aspects after migration:

  1. Network connectivity (ping internal and external hosts)
  2. Storage integrity (run fsck on Linux guests)
  3. Service availability (check critical services are running)

When using raw copy methods, these factors affect migration speed:

Factor Impact
Disk image size Linear time increase
Network bandwidth Bottleneck for remote copies
Storage I/O Affects both source and destination

For optimal performance, compress during transfer:

ssh sourcehost "dd if=/var/lib/libvirt/images/vm.img | gzip -c" | \
gunzip -c | dd of=/local/vm/cloned-vm.img

When migrating KVM virtual machines between hosts without shared storage, we face unique challenges in disk image transfer and configuration adaptation. The error message you encountered (Connection reset by peer) typically indicates either permission issues or configuration mismatches in the XML definition.

First, let's properly transfer the disk image using rsync for efficient copying:

bash
# On destination server:
rsync -avzP --sparse source-server:/path/to/vm.img /local/vm/cloned-vm.img

Your XML configuration needs three essential modifications:

1. Generate a new UUID (mandatory for clones):
bash
uuidgen > /etc/libvirt/qemu/cloned-vm.xml

2. Update MAC address for the network interface:
xml

3. Verify storage paths match the new location:
xmlFor simpler cases, consider using virt-clone with remote storage:

bash
virt-clone --original vm-original --name cloned-vm \
--file /local/vm/cloned-vm.img \
--mac RANDOM \
--force

If you still encounter startup problems:

1. Check SELinux contexts:
bash
restorecon -Rv /local/vm/

2. Verify libvirt permissions:
bash
chown qemu:qemu /local/vm/cloned-vm.img

3. Examine detailed error logs:
bash
journalctl -xe -u libvirtd

For large VMs, consider these optimizations:

1. Use compression during transfer:
bash
rsync -avzP --sparse --compress-level=9 source:/path/vm.img /local/vm/

2. Convert to qcow2 format for better storage efficiency:
bash
qemu-img convert -O qcow2 /local/vm/cloned-vm.img /local/vm/cloned-vm.qcow2