How to create snapshots of QEMU/KVM guests

KVM (Kernel-based Virtual Machine) is the virtualization solution (type 1 hypervisor) included in the Linux kernel, which, by default, is used together with QEMU, the userspace software which actually performs the guest systems emulation (type 2 hypervisor). In a previous tutorial we saw how to create and manage KVM virtual machines from the command line; in this article, instead, we will learn how to create and manage guest systems snapshots using tools like virsh and virt-manager.

In this tutorial we learn how to create snapshots of guest systems emulated with QEMU/KVM, using virsh and virt-manager.

In this tutorial you will learn:

  • What is the difference between internal and external snapshots
  • How to create, delete and revert to internal snapshots using virsh or virt-manager
  • How to list snapshots associated with a domain
  • How to obtain information about a snapshot
  • How to delete an internal snapshot
How to create snapshots of QEMU/KVM guests
How to create snapshots of Qemu/KVM guests
Software requirements and conventions used
Category Requirements, Conventions or Software Version Used
System Distribution-independent
Software virsh, virt-manager
Other Administrative privileges if working in QEMU/KVM system session
Conventions # – requires given linux-commands to be executed with root privileges either directly as a root user or by use of sudo command $ – requires given linux-commands to be executed as a regular non-privileged user

Introduction

A snapshot is basically a “frozen picture” of the disk, the memory and the state of the devices associated with a domain, taken in a specific moment in time. Snapshots are very useful: they can be created, for example, before performing a potential dangerous task on the system, in order to have an easy way to switch back to a known well state.

To create snapshots of guest systems when using QEMU/KVM, we can either use virsh, (the main interface for managing domains from the command line) or graphically, with virt-manager. Two types of snapshot exist: internal and external. Internal snapshots are the default: they are embed in the disk image used for the guest system and use the COW (Copy On Write) technique: basically, when something needs to be changed, it is first copied to the snapshot, than the new data is written. External snapshot, on the other hand, are stored in separate files, and work in a different way. In this tutorial we will focus on the former.

Creating snapshots with virsh



A system snapshot can be created when a guest is running or when it is turned off. There are basically two ways we can create a snapshot with virsh: by using the snapshot-create or the snapshot-create-as commands. The difference between the two is that the former creates a snapshot with the properties specified in a XML file, while the latter creates a snapshot from arguments. For the sake of this tutorial we will use the latter approach.

Suppose we have a domain called “fedora36” up and running. To create a snapshot of this domain named “snapshot-0”, using “first snapshot” as a description, we would use the following command:

$ virsh snapshot-create-as fedora36 snapshot0 --description "first snapshot"

If everything goes as expected, the system should respond with:

Domain snapshot fedora36-snapshot0 created

We used the snapshot-create-as virsh command and passed the name of the domain we want to create a snapshot of, as first argument and the name we want to use for the snapshot as the second one. Finally, we used the --description option to provide a brief description for the snapshot. If a name for the snapshot is not provided, it is generated randomly; the description, instead, is optional. Since we created the snapshot when the system was running, both the disk and the memory state were included in it.

Creating a snapshot with virt-manager

An alternative method to create internal snapshots is to use the the virt-manager application. To use this method, in the virtual machine viewer, just click on the button highlighted in the screenshot below:

How to create snapshots of QEMU/KVM guests
The highlighted button let us access the snapshots section



Once we click on the button, a new window will be opened. In the interface we will able to visualize and manage the snapshots associated with the domain. To add a snapshot, we just click on the “plus” button located in the bottom left corner, provide a name and a description for the snapshot, and finally click on the “Finish” button:

virt-manager-snapshots-window
The virt-manager snapshots interface

In the above screenshot you can see there is a checkbox next to the existing “snapshot0” name. This is because it is the snapshot currently in use. Later in the tutorial we will see how to retrieve information about snapshots.

Reverting to an internal snapshot

Suppose we made come changes to our guest system and we are not satisfied with it. Since we created a snapshot, we can easily revert back to it, and all the changes we made since it was created will be lost. To revert to a snapshot state using virsh, we use snapshot-revert. To revert to the “snapshot0” we created in the previous example, we would run:

$ virsh snapshot-revert fedora36 snapshot0

We just provided the name of the guest domain as first argument, and the name of the snapshot as the second one. As an alternative, to revert to the current snapshot without specifying its name, we would run:

$ virsh snapshot-revert fedora36 --current

To revert to a snapshot using the virt-manager interface, we just select the snapshot we want to revert to in the list, than click on the “play” button:

virt-manager-revert-to-snapshot
Reverting to a snapshot using virt-manager

Deleting an internal snapshot

Deleting an internal snapshot is quite easy. All we have to do is to use the snapshot-delete virsh sub-command, specifying, again, the domain and the name of the snapshot we want to delete. To remove the “snapshot0”, for example, we would run:

$ virsh snapshot-delete fedora36 snapshot0

The system should respond with:

Domain snapshot snapshot0 deleted



To remove a snapshot using virt-manager, we select it in the list, and click on the “remove” button. We will be prompted to confirm our choice:

virt-manager-delete-snapshot
Deleting a snapshot with virt-manager

Retrieving a list of existing snapshots with virsh

As we saw in the previous examples, the list of the available snapshots for a certain guests is easily visible when using virt-manager, but how to retrieve it from the command line, when using virsh? The snapshot-list sub-command let us accomplish exactly that task. To list all snapshots associated with the “fedora36” domain, we would run:

$ virsh snapshot-list fedora36

In this case the output produced by the command is the following:

 Name        Creation Time               State
--------------------------------------------------
 snapshot0   2022-07-28 17:53:51 +0200   running

By default the output is composed of three columns reporting information about the snapshot name, its creation time and the state the domain had when the snapshot was taken.

Obtaining information about a snapshot

To obtain information about a specific snapshot we use snapshot-info and pass the name of the domain as first argument, and the name of the snapshot we want to examine as second. To obtain information about “snapshot0”, we would run:

$ virsh snapshot-info fedora36 snapshot0

The output produced by the command should look similar to the following:

Name:           snapshot0
Domain:         fedora36
Current:        yes
State:          running
Location:       internal
Parent:         -
Children:       0
Descendants:    0
Metadata:       yes

Among the other things we can see that the snapshot is the one currently in use,  its location is “internal” (as we previously said, snapshots can also be “external”), and it has no children nor descendants.



Another virsh command we can use to retrieve information about a snapshot is snapshot-dumpxml. It is used to display the XML representing the snapshot structure. As in the previous case, we need to specify the domain and the snapshot name as arguments to the command:

$ virsh snapshot-dumpxml fedora36 snapshot0

Deleting a snapshot

If we are sure we don’t need a snapshot and we want to delete it, we can use the snapshot-delete virsh command. As usual, we specify the name of the domain the snapshot belongs to, and name of the snapshot we want to delete. To delete the “snapshot0” snapshot we used in the previous examples we would run:

$ virsh snapshot-delete fedora36 snapshot0

If everything goes as expected we should receive a message like the following:

Domain snapshot snapshot0 deleted

Conclusions

Snapshot are a really important feature: they represent an image of the state of an emulated system in a specific point in time, and we can use it to restore the system back to a known well state in case something goes wrong. When using Qemu/KVM on Linux, two types of snapshots exists: internal and external. In this tutorial we focused on the former, soon we will talk about the latter, and we will see what are the differences between the two.



Comments and Discussions
Linux Forum