Get started Bring yourself up to speed with our introductory content.

Getting a clear picture on KVM snapshot basics

There are two basic ways to create a KVM snapshot, but only one will allow you to take snapshots of a running virtual machine.

Creating a KVM snapshot has been a challenge for a long time. For years, there was no native solution, and creating...

snapshots was possible only through the Linux Logical Volume Manager (LVM) snapshot feature. This is still a reliable approach to make a snapshot, but libvirt offers a better alternative.

LVM snapshots

The purpose of LVM snapshots is to easily revert the state of files on a logical volume to a previous version. LVM volumes are created from a volume group, which represents the total amount of available storage. Before creating an LVM snapshot, you need to make sure that disk space is available in the volume group. Creating a snapshot is an easy procedure; just run the command lvcreate -s -n myvolume_snap -L 10G myoriginalvolume. Make sure to first shut down the virtual machine (VM) to create a reliable snapshot.

The libvirt advantage

For about two years, KVM has offered an alternative method to create snapshots, through libvirt. Libvirt is the management interface used for KVM. Using libvirt to create snapshots is a better approach, as it is a native KVM solution. It also takes a snapshot of the entire VM, as opposed to LVM snapshots that typically back up just one individual disk.

To create a libvirt snapshot, you can use the virsh snapshot-create command. This command is based on an XML file containing the definition of the original VM. The virsh command reads the contents of this file to find out which original VM (as specified in the <name> section of the XML file) and which disks should be used.

The XML file can be the file that defines the original VM; it can also be a custom-made XML file that just contains a part of the configuration of a VM. You might, for instance, create an XML file that does not contain all of the disk used by the VM. Use, for instance, the command virsh snapshot-create mydomain-snap mydomain.xml to create a snapshot with the name mudomain-snap that is based on the mydomain.xml configuration file.

The real advantage to using virsh for creating a KVM snapshot is the "--live" command modifier. With this option you can take a snapshot of a running VM. When using this option you should always test your snapshot, because some workloads don't support live snapshots. Also, be aware that a live KVM snapshot will be larger because it also includes the memory image.

After creating a KVM snapshot with the virsh command, you can later revert back to the snapshot with the snapshot-revert yourvmname command. For instance, the command snapshot-revert myvm --current reverts the VM to the last saved snapshot. You can also specify the name of a specific snapshot to revert to, using a command like virsh snapshot-revert myvm myvm-snapshot.

While the old approach of using LVM storage volumes still works, it requires the VM to be down when the snapshot is created and doesn't consider VM metadata. So, even if LVM still works in modern KVM environments, you're probably better off taking a KVM snapshot with virsh in libvert.

Dig Deeper on Open source virtualization