I'm wondering what is the correct way of moving a VM between two KVM hosts without using any kind of shared storage
Would copying the disk files and the XML dump from the source KVM machine to the destination one suffice? If so, what commands need to be run to import the vm on the destination?
OS is Ubuntu on both the Dom0's and DomU.
Thanks in advance
/var/lib/libvirt/images
on src host to the same dir on destination hostvirsh dumpxml VMNAME > domxml.xml
and copy this xml to the destination hostvirsh define domxml.xml
start the VM.
If the VM is attached to custom defined networks, you'll need to either edit them out of the xml on the destination host or redefine them as well
virsh net-dumpxml NETNAME > netxml.xml
virsh net-define netxml.xml && virsh net-start NETNAME & virsh net-autostart NETNAME
)Since I can't comment yet, I have to post this addendum to dyasny's answer this way.
If the VM has snapshots that you want to preserve, you should dump the snapshot xml-files on the source with
virsh snapshot-dumpxml $dom $name > file.xml
for each snapshot in the snapshot list of the VMvirsh snapshot-list --name $dom
.Then on the destination use
virsh snapshot-create --redefine $dom file.xml
to finish migrating the snapshots.If you also care about which snapshot is the current one, then additionally do on the source:
virsh snapshot-current --name $dom
and on the destination:
virsh snapshot-current $dom $name
Then you can use
virsh snapshot-delete --metadata $dom $name
for each snapshot to delete the xml files on the source, or you could just delete them from/var/lib/libvirt/qemu/snapshots/$guestname
Sources:
libvirt-users mailing list
http://kashyapc.com/2012/09/14/externaland-live-snapshots-with-libvirt/
Yes, just copying the XML file and the virtual disk images is sufficient, but this obviously precludes a "live" migration. The VM must be shut off during this procedure.
Once copied to the destination,
libvirtd
must be reloaded or restarted to recognize the new XML file.Detailed Instructions on Copying VMs using blocksync.py
These instructions apply to a VM using a LVM provided disk and assumes that Python is on each of the hosts
Download the blocksync.py script from https://gist.github.com/rcoup/1338263 and put on both source and destination host in your /home/user folder.
Precursor
You will also need to have 'sudo' access to 'root' on both machines.
Alternatively, you could do everything as root, but only if your ssh key gives you root access to at least the target machine. ** In this case, remove the user name from the command lines.
Example Settings
Procedure
Initial steps on the source host
Copy the dumped definition to the new machine (the "target" host), eg:
you can change the internal ip to your destination dom0 server name ** Note: it is best to use the ip address for the target, eg:If you cannot copy due to keys the cat larry.xml and copy it Then you can ssh into other machine and create file and paste it.
Find the size and name of the VM's disk using
.** The command above should show size exactly in bytes. ** The machine's disk name is in the first column of the listing, its volume group in the second, and size in the last. ** Determine the device name as /dev// ** Check it with a 'll' command For example, in this output: vm_larry vg1 -wi-ao---- 69793218560B
Initial steps on the target host
Create a volume definition file, eg:
or with the following lines: NOTE - You will need to take the size in bytes from the original VM and put into below script. The command on the source machine for size was sudo lvs --units BNote: this definition is for a 69793218560 Bytes disk for VM larry, change as necessary for the actual VM.
Note: the name and last part of the path should match and will be used as the new disk name.
Create the new disk from the definition, using
it will say Vol larry.domainname.com.au created from larry.domainname.com.au-vol.xml
Make the disk device file accessible:
Edit the xml definition copied over, eg:
Find the disk definition in the file (search for "source dev =") and replace the device with the one just created (you can ls /dev/centos/ to see vm), eg: /dev/drbd4 -> /dev/centos/larry.domainname.com.au
This bridge change was unique to our situation.
** Find any references to "br1" in the interface stanzas and change it to "br0" e.g. you are changing source bridge so line is like this
Final steps on the source host
Login to the source host, eg
The best practice would be to shutdown the VM on the source host before doing the final sync but doesn't need to be done. (virsh shutdown NameOfMachine)
If not already on the source host, download the blocksync.py script from https://gist.github.com/rcoup/1338263
If your username is user (for example) then copy the blocksync.py script into both machines into /home/user and chown user:user and chmod 755 the script.
Command that does the copying
Note: the first device name is for the source host, as determined from the 'lvs' command; this one is from a [[chewie]] source host.
Note: this will destroy the contents of the target disk, make sure that /dev/mapper/centos-larry.domainname.com.au is correct!
Note: the sync will take a long time - about 100 seconds per gigabyte, ie: 90 minutes for a 60 gigabyte disk.
However, you can do a sync while the VM is in use; subsequent syncs can be up to 25 percent faster
The script will print out the parameters that it is using (there may be a message about a deprecated module, this is okay). Next, it displays the ssh command that it is using and runs it (you will see the authorised staff only message when it does this). During its sync, it will display a running total of blocks copied and its average speed. Finally, it prints out a completion message with the number of seconds it took.
Things to Know
You can cancel the sync with CTRL C and restart it later by running the command again
Final steps on the target host
Note: it may be necessary to alter the details of the VM to suit the new environment.
I have run into this problem with a couple of my older KVM servers, but its really annoying when it happens, and can cause issues with any of the installed VM's. In my case it regularly pushed one of my VMs into the reset state, as disk space was slow exhausted. The instructions below are somewhat sensitive to KVM/Distro version. In my case, I have CentOS 7.5
By default the KVM images are located the location /var/lib/libvirt/images/
You need to find the Name of the VM, for this use virsh list
Stop the VM virsh stop VM-Name
For me I copy the file first, rather than moving. Copy the qcow file to the new location
Edit the VM xml file, to reference the new "source file" location virsh edit VM-Name
You will want to change the "source file" this file
Restart the libvirtd service
Then restart the VM and you should be good to go.