Live snapshots on KVM

Live snapshots have long been a key feature of virtualisation technologies. VMware probably weren’t the first but they sold the feature to the world. If you want to be able to take live external snapshots (say, for backup purposes) and you’re on KVM, here’s what you need to do (using CentOS 7).

So, you have your KVM host set up with machines running. You need to add the oVirt repo to your system to get more recent versions of qemu and, well, everything KVM-related. Do this like so:

wget http://resources.ovirt.org/pub/yum-repo/ovirt-release36.rpm && yum localinstall ovirt-release36.rpm && yum update -y

Let it update then reboot the host. Yup, I know – rebooting Linux, what a crime. It’s what I had to do to get the VMs running on the newer version qemu. Fire up a VM.

To test live external snapshots, first of all you need to know what disk image is being used by a particular VM. Do this:

virsh domblklist vm1

Replace vm1 with the name of your VM. Returned is a list of block storage attached to it. Now take the live snapshot:

virsh snapshot-create-as --domain vm1 snapshot1 --diskspec vda,file=/var/lib/libvirt/images/vmsnapshot1.qcow2 --disk-only --atomic --no-metadata

You have a little thinking to do here. As before, replace vm1 with the name of your VM. The vda has to match the target in the domblklist output from before. The file= path can be arbitrary but I tend to stick with keeping images in the same place. The name of the snapshot is also arbitrary. Once you have finished thinking and input the command you’ll have a snapshot.

The active image is now this snapshot. Feel free to backup the original. Finished? Okay, on the assumption the snapshot was temporary (snapshots should ALWAYS be temporary, okay? I’ve seen em used as permanent ‘bookmarks’ in some settings and it’s just wrong), we now need to commit the snapshot back into the original, base image while the VM is powered on:

virsh blockcommit vm1 vda --active --verbose --pivot

This shouldn’t take long. Now emu is using the original disk image again. Delete the snapshot:

rm /var/lib/libvirt/images/vmsnapshot1.qcow2

You’re done. There is more to this, using the –quiesce option when creating the snapshot. That requires the emu guest agent being installed on the VM and set up correctly, which I hope to go through soon. It ensures consistent disk images.