While debugging a QEMU live migration related issue, I learned a setup to live migrate a virtual machine (VM) across two host machines. Migration here means to move a VM from one host machine to another. And live migration is, while a VM is running on the source machine, its state is copied to the destination machine, and once this copying is done, VM stops on the source machine and starts running on the destination machine. Applications running inside the VM are suppose to run as before, as if nothing changed.
Migrating a VM from one machine to another would entail moving its disk image and xml files as well. But copying (rsync(1)) files across machines could take long time, depending on file size and network bandwidth.
f39vm.qcow2 107.39G 100% 334.82MB/s 0:05:05
r91vm.qcow2 42.96G 100% 111.88MB/s 0:06:06
r93vm.qcow2 21.48G 100% 242.79MB/s 0:01:24
It is not feasible to stall a VM for such long times during migration. So migration requires that the source and destination machines are connected by a reliable high bandwidth network channel and the VM disk files are accessed on both machines via shared storage system. Accordingly, the source host machine is turned into a NFS server and destination machine is made NFS client:
source)# dnf install nfs-utils
source)# systemctl enable nfs-server
source)# systemctl enable rpcbind
source)# echo '/home/images 10.x.x.0/24(rw)' > /etc/exports
source)# mount -t nfs source.host.machine.com:/home/images/ /mnt/images/
destin)# dnf install nfs-utils
destin)# systemctl enable nfs-server
destin)# systemctl enable rpcbind
destin)# virsh pool-create-as --name=migrate \
--type=netfs --source-host=source.host.machine.com \
--source-path='/home/images' --source-format=nfs \
--target=/mnt/images/
The VM xml configuration file should access the disk image via /mnt/images path as
<disk type='file' device='disk'>
<source file='/mnt/images/r93vm.qcow2' index='1'/>
</disk>
If the NFS setup is incorrect, migrate command may throw errors like these:
error: Unsafe migration: Migration without shared storage is unsafe
Once NFS server & client setup is done, try to start the VM on both machines to confirm that it is working, before trying the migration.
source)# virsh start --console r93vm
When VM runs on both machines, we can try the migration between two machines with
# cat migrate-test.sh
#!/bin/sh
if [ $# -lt 2 ]; then
echo "Usage: $0 <guest> <destination-host>"
exit 1;
fi
GUEST=$1
DHOST=$2
while true;
do
ds=$(virsh domstate $GUEST | sed -ne '1p')
if [ "$ds" == "running" ]; then
id=$(virsh domid $GUEST | sed -ne '1p')
echo -n "migrate $GUEST.........$id"
sleep 3s
/bin/virsh migrate --verbose --persistent \
--timeout 3 --timeout-postcopy \
--live --postcopy \
$GUEST qemu+ssh://$DHOST/system
fi
sleep 3s
done
exit 0;
This script live migrates a VM between source and destination machines in an infinite loop using the postcopy method of migration. Next we’ll see more about these migration methods.


