How to migrate a kvm guest from one host to another.
- On the destination system, permit connections to port 4321/TCP from the source system.
iptables -I INPUT -m tcp -p tcp --dport 4321 -j ACCEPT
For firewalld (Centos 7)
firewall-cmd --zone=public --add-port=4321/tcp
NOTE: This exception created in iptables or firewalld will be lost upon reboot of the system. If you want to keep the port open, you need to read up further on iptables or firewalld
- On the destination machine, create a logical volume (using the Virtual Machine Manager) that is as large as the drive space allocation on the source machine or, if you are going to use an image file, select a location that you have sufficient space to copy the image to. Then execute:
– If you are using a logical volume
nc -l 4321 | dd of=/dev/<vgname>/<lvname>
– if you are going to use an image file
nc -l 4321 | dd of=/path/to/image/file.img
Note: If you are using an image file on the source machine you can put it on a logical volume on the destination machine and vice versa.
- On the source machine, shut down the virtual machine. Using the Virtual Machine Manager and determine the Source Path of the Virtual Disk, then execute
pv -tpreb /source/path/to/virtual/disk | nc <destination machine ip> 4321
- Go take a break. Depending on the size of the drive, the speed of your connection between the machines, the speed of your drives, and the phase of the moon, this may take between minutes to hours.
- On the source machine, find the XML file in the /etc/libvirt/qemu that corresponds to the virtual machine you are moving. Copy that file to the same directory in the destination machine
- On the destination machine, verify the file you copied is owned by root.root and the permissions are set to 600
- Edit the file (using vim NOT “virsh edit” even though you will see a comment that tells you to do so) on the destination machine. Find the section for the disk storage. It will look something like this:
<disk type='file' device='disk'> <driver name='qemu' type='raw' cache='none'/> <source file='/mnt/virtimages/Win7.img'/> <target dev='hda' bus='ide'/> <address type='drive' controller='0' bus='0' target='0' unit='0'/> </disk>
The above configuration uses an image file. If the destination server is using an images file, update the line that starts with “<source file….” to point to the current source file.
NOTE: If you are switching from an image file to a logical volume, update the line that starts with “<source file=…..” to read:
(where /dev/vgname/lvname is the correct path the the logical volume)
and change the line that starts with “<disk type….” to read:
<disk type='block' device='disk'>
and the line that starts with “<driver name…”
<driver name='qemu' type='raw' cache='writeback' io='native'/>
- You will may also need to change the network interface. If you use bridging, you will find a section in the file that reads like this:
<interface type='bridge'> <mac address='52:54:00:f6:45:78'/> <source bridge='br0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> </interface>
replace the “br0” with the correct value, if needed
- Reload libvirtd using the command:
On Centos 6
service libvritd reload
On Centos 7
systemctl reload libvirtd
and the virtual machine will appear in the Virtual Machine Manager. If it does not appear, check the /var/log/messages file for errors and re-edit the XML file using vim. Look for typos. Then reload libvirtd
- Enjoy your relocated virtual machine!