Exporting libvirt+KVM virtual machines to files

Solution 1:

If you need to backup your vm configuration using virsh you can use the following command

virsh dumpxml vmname > vmname.xml 

If you need to move your vm to other server, you can dump your vm config and transfer the xml, if you are using files as backend storage for your vm you can copy the files to other server using scp or rsync, when you copied the disk files of the vm, you can try to start the vm with virsh define /tmp/myvm.xml && virsh start myvm

Solution 2:

You should be able to image your disks from a snapshot:

First you wil need to make sure the Vm is not running:

virsh suspend vm

Now you can create a snapshot make sure to adjust the size 100M and the name vm-root-snapshot to your own needs.

lvcreate -L 100M -n vm-root-snapshot -s /dev/sysvg/vm-root

After this you can start the VM again.

virsh resume vm

Now that you have the snapshot you can create the image and delete the snapshot:

dd if=/dev/mapper/sysvg-vm--root--snapshot of=/tmp/vm-root.img bs=1M
lvremove /dev/mapper/sysvg-vm--root--snapshot

The image can be copied to the new server and imported to the LVM on the new server with dd

dd if=/tmp/vm-root.img of=/dev/mapper/sysvg-vm--root bs=1M

Also don't forget to import your XML file.

virsh define /path/to/vm.xml

Solution 3:

Besides virsh dumpxml which will export the VM settings into a XML file:

Copy the VM disk image(s), if they are sparse or on a block volume, the correct way is using qemu-img convert -O output_format source destination

To import, edit the XML to reflect the new location of disk images, and then run virsh define path/to/dumpXML