How to Migrate a Virtual Machine
This page shall describe how to transfer a virtual machine from one hypervisor to another. The instructions below assume you have two hosts that are functioning hypervisors.
VM Images and .xml files
There are only two things we need to transfer from a hypervisor in order to get it functioning on the new machine: The VM disk image and the corresponding .xml file. VM disk images are typically stored in /var/lib/libvirt/images or in some subdirectory under there. .xml files are typically stored in /etc/libvirt/qemu.
In below example, I will transfer the VM nu from the hypervisor he to the new hypervisor vav:
# Transferring the .xml file for nu [root@he bulk]# cd /etc/libvirt/qemu/ [root@he qemu]# ls alpha.xml chi.xml gamma.xml nu.xml phi.xml sigma.xml zeta.xml autostart epsilon.xml kappa.xml omega.xml psi.xml tau.xml beta.xml from_aleph networks omicron.xml rho.xml testraid.xml
I should transfer this .xml file to /etc/libvirt/qemu but I transfer it first to scratch to avoid permission denials due to no PermitRootLogin.
[root@he qemu]# scp nu.xml s_bwong1@vav:/scratch [root@vav qemu]# cp /scratch/nu.xml /etc/libvirt/qemu/
Transferring the VM image for nu; Prior to VM image transfer, you must freeze or shutdown the VM because copying a live VM can cause the copy to have corruption.
If the VM is running, do the following command:
[root@he qemu]# virsh suspend nu
Ensure VM is off or suspended
[root@he qemu]# virsh list --all Id Name State ---------------------------------------------------- 13 alpha running 18 beta running 19 gamma running 20 epsilon running 21 tau running 23 zeta running 24 phi running 25 omega running 26 psi running 27 omicron running 28 chi running 31 rho running - kappa shut off - nu shut off - sigma shut off - testraid shut off
Find the disk image. Sometimes there are multiple disk images in the image directories. There are a few ways to tell which disk image is the one you need and whether you need just one or several.
[root@he from_aleph]# ls -lh total 555G -rwxr-----. 1 qemu qemu 445G May 23 04:12 alpha-share -rwxr-----. 1 qemu qemu 9.0G Nov 18 2014 beta-disk1 -rwxr-----. 1 qemu qemu 5.5G Nov 18 2014 delta-disk1 -rwxr-----. 1 qemu qemu 12G May 23 10:08 gamma-disk1 -rwxr-----. 1 qemu qemu 33G May 23 10:08 gamma-disk2 -rwxr-----. 1 qemu qemu 10G Nov 18 2014 nu-disk1 -rwxr-----. 1 root root 12G Feb 13 13:37 nu-disk1.qcow2 -rw-r-----. 1 qemu qemu 20G May 23 10:08 psi-disk1 -rwxr-----. 1 root root 9.6G Mar 31 2015 zeta-disk1
nu has two disk images but only one of these is vital for the VM transfer. Looking at the date, It seems the one with 12GB that was last used on Feb 13 of the current year is the correct one.
VMs are constantly updating when they are on, so the most recent date should correspond to the correct VM.
But if you need a surefire way of checking which VM image corresponds to the VM, then check the xml file for the vm with this command:
[root@he from_aleph]# virsh dumpxml nu | grep 'source\ file'
After we found the correct disk image, we'll copy this over to our new hypervisor. We want to copy this over to /var/lib/libvirt/images on the new host but we'll put it on an all-accessible directory first so we can avoid
permission denials due to no PermitRootLogin.
[root@he from_aleph]# scp /var/lib/libvirt/images/bulk/nu-disk1.qcow2 s_bwong1@vav:/scratch [root@vav scratch]# cp /scratch/nu-disk1.qcow2 /var/lib/libvirt/images
Defining then Running VM on New Hypervisor
With both our VM images and our .xml files in the places they should be, we can get started on reconfigurations for the new host.
[root@vav ~]# ls -lh /var/lib/libvirt/images/ total 165G -rwxr--r--. 1 qemu qemu 6.9G May 23 10:26 chronos-disk1 -rw-r--r--. 1 root root 14G May 9 10:50 kappa-disk1.qcow2 drwx------. 2 root root 16K Apr 24 15:52 lost+found -rwxr-----. 1 root root 12G May 22 14:10 nu-disk1.qcow2 -rw-r-----. 1 root root 16G May 22 14:32 omega-disk1.qcow2 -rw-r-----. 1 root root 15G May 22 14:47 omicron-disk1.qcow2 -rw-r-----. 1 root root 69G May 22 15:39 phi-disk1.qcow2 -rw-r-----. 1 root root 20G May 22 15:55 psi-disk1 -rwxr-----. 1 root root 15G May 22 16:06 zeta-disk1 [root@vav ~]# ls -lh /etc/libvirt/qemu total 36K -rw-------. 1 root root 2.4K May 9 14:19 chronos.xml -rw-------. 1 root root 2.4K May 8 14:39 kappa.xml drwx------. 3 root root 4.0K May 9 14:14 networks -rw-------. 1 root root 2.2K May 22 16:13 nu.xml -rw-------. 1 root root 2.4K May 22 16:13 omega.xml -rw-------. 1 root root 2.4K May 22 16:13 omicron.xml -rw-------. 1 root root 2.3K May 22 16:13 phi.xml -rw-------. 1 root root 2.2K May 22 16:13 psi.xml -rw-------. 1 root root 2.6K May 22 16:13 zeta.xml
Next step, we have to reconfigure the .xml file for our VM because it reflects configurations that were known on the old host but are foreign to our new host. First, define the host by providing the .xml file as an argument.
[root@vav ~]# virsh define /etc/libvirt/qemu/nu.xml Domain nu defined from /etc/libvirt/qemu/nu.xml
The VM nu previously belonged in the directory /var/lib/libvirt/images/bulk/ in the old host. But in the new host, it is in /var/lib/libvirt/images. The .xml file will have to be adjusted to reflect this change.
[root@vav ~]# virsh dumpxml nu | grep 'source\ file'
Edit the xml file with the following command:
[root@vav ~]# virsh edit nu
Change the line that contains the 'source file' to the directory where your disk image actually resides now and then save your changes:
Domain nu XML configuration edited.