When I started to work with KVM, I used to ask myself a question How do I backup and restore a KVM VM?, or How do I revert my virtual machine to its old state, when things go wrong while I am making some changes.
In the beginning, I used to copy the image from default libvirt default directory /var/log/libvirt/images/vm.qcow2 to a backup folder, in the event of anything goes wrong I can quickly restore them by editing the XML file to use the original image.
However, the easiest and the best method of backing up and restoring VM is using snapshots.
What is a VM snapshot?
A virtual machine snapshot is a method to capture and store the VM’s current working state, which includes all its configurations in it. By preserving the VM’s current state, you can restore the VM to its working condition at any time.
For instance, you deployed a VM and configured all its services and everything, and it’s running smoothly for some time now. One day you have been asked to make a change on the VM, and you are worried that something could go wrong, then Snapshot can be very handy.
You can go ahead and take a snapshot before the change, and after the change, if things are not working well then you can revert to the Snapshot.
Where are KVM snapshots stored?
It depends on what types of Snapshot that you are performing.
The KVM snapshots are two types
Internal snapshots and External snapshots, let’s look at each of them now.
1. KVM Internal snapshot.
In an internal snapshot, you can take a snapshot of a virtual machine while running known as a live snapshot and it stores the snapshot data within the qcow2 image. Though you can take the Snapshot while the VM is running, it is advisable to shut down the VM and take Snapshot. Anyway, while taking a snapshot, the VM would be in paused, and you won’t be able to access any of its services, so it does make sense to shut the VM and take a snapshot.
I remember, when I took my first KVM snapshot, I received an email from people saying the VM was down. The VM wasn’t down. Instead, the libvirt was paused the VM. At this point, you cannot even ssh or ping the VM.
So it is better to have a maintenance window while taking the Snapshot.
And one more downside of the internal Snapshot is that it supports only the qcow2 image.
2. KVM External Snapshot.
External Snapshot makes an overlay image from the original image. The original image will become a read-only state, and any changes that you make would be on the overlay image. It also supports live snapshots.
When you wanted to restore the Snapshot, you need to point it back to the backing file or the overlay image.
backing file is the original image and the overlay image is the snapshot.
The advantage of using the External Snapshot is that it supports a variety of disk images, not just the qcow2.
The disadvantage of using the external Snapshot is that it doesn’t support direct restoring and deleting of Snapshot with the commands and it is a little tricky. Also, live restore is not supported hence you have to shut the VM off and restore it.
Let’s look at the internal Snapshot now.
How to manage KVM snapshots using CLI?
- How to perform internal snapshot in KVM ?
- How do I see the KVM snapshot list?
- How to restore snapshots in KVM?
- How to delete KVM VM snapshot?
How to manage KVM snapshots using GUI?
- How to take the Snapshot using a GUI ?
- How to restore the snapshots in the KVM GUI?
- How to delete the snapshots in the KVM GUI?
1. How to perform an internal snapshot in KVM ?
First, let’s list the VM’s that we have and identify what VM that we are going to take Snapshot from.
To list all the shutdown and running VM’s you can run the command
virsh list --all
As you can see, I have two VM’s, and I am going to take a snapshot of the win10 VM.
If you wanted to keep it short and take a snapshot, you could type the command below.
virsh snapshot-create-as –domain win10
Note : During the snapshot process the VM will be paused.
As you can see, the Snapshot has created with a unique name 1603602051
If you wanted to be more organized and give Snapshot a name and description as well, then you can add those too with the below commands.
virsh snapshot-create-as --domain VM --name "name of your choice" --description "description of your choice" and hit enter.
Let’s go ahead and take a snapshot with a name and a description.
But before that, let me shutdown the VM. You can shut down the VM with the command virsh shutdown win10
After a few seconds, the VM’s will be shut.
Note: You don’t really need to add the date in the snapshot description or the name, it will be added automatically to the Snapshot. See the example below where I have created a snapshot for win10 VM with a name and description
Awesome! We have created two snapshots, one with default name while the VM was running and the second one with a custom name while the VM was shut.
How do you see the list of snapshots we have taken? Let’s take a look.
2. How do I see the KVM snapshot list?
To see the snapshot list, you can type the command, virsh snapshot-list VM-name.
As you can see below, the list of Snapshot that we have taken.
You can see the Name – The first with a unique default name, second with the custom name we have given.
Creation Time – Here, you can see the date and time of the Snapshot taken.
Finally the state – First VM snapshot is in running state because we have taken the Snapshot while the VM was running and the second one is in shutoff state, of course, we had shut the VM down while taking the Snapshot.
You can also see the snapshot status in more details by using virsh snapshot-info –domain win10 –current command.
3. How to restore snapshots in KVM?
Now, if you were to restore the Snapshot in running state, during the revert process, there will be no downtime. After the restore process completed, the VM will be in running condition.
Similarly, if you try to restore Snapshot in shutoff state, the VM will be in shutdown state after the restore.
Alright, let’s take a look at the Snapshot restore process in action.
I am going to start the VM by typing virsh start win10, which was shut during the snapshot process.
You can check the VM current status by typing the command virsh list –all. You can see win10 is running at the moment.
I am going to restore the snapshot “update#123” which is in shutdown state.
To restore the Snapshot, you can enter the command below.
virsh snapshot-revert --doamin win10 --snapshotname "update#123"
As you can see after the Snapshot the VM which was in running state, went to shut off.
What if you wanted the VM to keep running even after you restore the Snapshot which is in a shut state, you could enter the command –running at the end.
See above. The VM state changed to running, all I did was added –running at the end.
Let’s restore the VM snapshot, which is in the running state
I have shut down the VM again.
As you can see, without any running command the VM which was shut, started again after the Snapshot that was restored.
It’s because, we have taken the live snapshot, meaning taking the snapshot while the VM was running.
4. How to delete KVM VM snapshot ?
Alright, we have taken a look at how to create an internal snapshot and how to restore them, now let’s see how we can delete the Snapshot that we had taken earlier.
To remove the Snapshot, you can enter the command virsh delete snapshot, see the example below.
[email protected]:~$ virsh snapshot-delete --domain win10 --snapshotname "update#123" Domain snapshot update#123 deleted [email protected]:~$ [email protected]:~$ virsh snapshot-delete --domain win10 --snapshotname "1603602051" Domain snapshot 1603602051 deleted [email protected]:~$
1. How to take the Snapshot using a GUI ?
To take a KVM snapshot in GUI, you will have to open virtualization manager window.
A window will pop up, now select the VM that you want to work with and then click on Open.
In the VM properties, click on the icon which is no the right, which says manage snapshots.
To create a snapshot, you can click on the plus icon on the bottom.
A create a new Snapshot. A window will pop up, provide the name and the description and click on Finish.
You can see the Snapshot is created successfully.
2. How to restore the snapshots in the KVM GUI?
To revert the Snapshot in KVM GUI, click on the play button which says Run selected snapshot.
Let’s see what happens if I try to revert the Snapshot to snapshot1 On the prompt warning click on Yes
After a few seconds, the Snapshot will be restored. And you will get a tick mark which indicates it is done.
3. How to delete the snapshots in the KVM GUI?
Deleting a Snapshot from KVM virt-manager also easy, you have to select the Snapshot and click on the delete button on the bottom right.