Move a virtual machine from one Centos machine to another
Revised: How to migrate a kvm guest from one host to another.
These instructions are for moving a guest machine from one host to another host where the storage is on the host (instead of external storage that can be re-assigned to another host)
- Verify you can ssh from the source system to the destination system.
- 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 on the source machine:
dd bs=4M if=/dev/mapper/VG_SOURCE | pv -s SIZEOFVOLUMEg | ssh -C destination.machine.ip.address 'dd bs=4M of=/dev/mapper/VG_DEST'
– set SIZEOFVOLUME to the size of the volume you are copying in gig (make sure you include the trailing “g”)
– if the destination or source storage system is an image file, enter the full path of the image file. - 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:<source dev='/dev/vgname/lvname'/>
(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 6service libvritd reload
On Centos 7 or 8
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!
Reference: http://strugglers.net/~andy/blog/2011/03/13/copying-block-devices-between-machines/
Old Method: 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.
For iptables:iptables -I INPUT -m tcp -p tcp --dport 4321 -j ACCEPT
For firewalld (Centos 7)
firewall-cmd --permanent --zone=public --add-port=4321/tcp
firewall-cmd --reload
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 volumenc -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:<source dev='/dev/vgname/lvname'/>
(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 6service 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!
Reference: http://serverfault.com/questions/156502/how-do-i-migrate-a-kvm-guest-from-host-to-another