How do I migrate a KVM guest from host to another?
You are right. I have done this several times. I used netcat to image the drives across the network. This is the method that I used:
- Create the logical volume on the target. It should be the same size as the source volume.
- Run
nc -l 1234 | dd of=/dev/<vgname>/<lvname>
on the target host. This tells netcat to listen on the network port and pipe everything to the logical volume. - Shutdown the source machine cleanly to write everything to disk.
- Run
dd if=/dev/<vgname>/<lvname> | nc <targetip> 1234
on the source host. This tells netcat to pipe everything over the network to the target host. - Go fetch a cup of coffee. This takes a few minutes depending on your volume size and network speed.
- Copy the xml definitions from
/etc/libvirt/qemu/
from the source to the target. - Edit the storage portion of the xml file and any other necessary bits such as network bridge name if they are different.
- Reload libvirt on the target to read the xml. An
/etc/init.d/libvirt-bin reload
should do the trick (orsystemctl restart libvirtd
for distributions that use systemd) - Start-up the machine on the target host.
KVM (since qemu 1.3) now includes storage migration functionality, so it's no longer necessary to do any of the hackery shown in the older answers. Just migrate the VM as you normally would, for instance virsh migrate "Guest Name" qemu+ssh://dest-hypervisor/system
or click the nice buttons in your GUI of choice.