Introuction

Snapshot is really a important part of virtualization. Example we need to upgrade system and before that we like to take a snapshot complete machine, so if something goes wrong with upgrade then we always have option to revert back old condition.
libvirt
Mostly Linux host machine, use KVM, libvirt and Xen virtulization platform to support these virtulization solution. So do we have any method in which we can take snapshot in libvirt.yes we have commands to take snapshots.

In this post we will try to cover various details how we could take snapshots and use them to revert back old conditions of virtual machine. For same we need Virtual machine image based on qcow2 type disk. Otherwise it will show you message like below

#virsh snapshot-create-as --domain srv7 --name srv7-Fresh-updated
error: unsupported configuration: internal snapshot for disk vda unsupported for storage type raw

We can find out image type. In case image is not qcow2 format type, in that can we can also convert image into qcow2 file format like in this post. So first we need to check file format in below way.

#qemu-img info /data/vmware_images/CentOS7/system.image
image: /data/vmware_images/CentOS7/system.image
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 1.9G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
    refcount bits: 16
    corrupt: false

#virsh dumpxml srv7 | grep driver
 <driver name='qemu' type='qcow2'/>

We can create snapshot of DISK and RAM with commands and arguments. Only KVM Virtual machine support snapshot feature.

Below are list Guest machine on my Host.

#virsh list --all
 Id    Name                           State
----------------------------------------------------
 -     srv6                           shut off
 -     srv7                           shut off
 -     srv7.1                         shut off
 -     ubuntu                         shut off

I would like to take snapshot of srv7 Guest, which we already checked has qcow2 file format image.Let check its present snapshot.

#virsh snapshot-list srv7
 Name                 Creation Time             State
------------------------------------------------------------

So there is no Snapshot present for this Guest machine.

Syntax

We can create KVM Guest Snapshot through virsh command using snapshot-create-as options, mentioned below syntax for same.

virsh snapshot-create-as --domain <VM-NAME> --name <SNAPSHOT-NAME> 

Example

There is no snaphost listed in above command as its mentioned. Let’s take a snapshot for same before upgrade.

#virsh snapshot-create-as --domain srv7 --name srv7-before-update
Domain snapshot srv7-Fresh-updated created

#virsh snapshot-list srv7
 Name                 Creation Time             State
------------------------------------------------------------
 srv7-before-update   2017-04-26 12:49:00 +0530 shutoff

We can check Snapshot size, with qemu-img command like below.

#qemu-img info system.image 
image: system.image
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 12G
cluster_size: 65536
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
1         srv7-Fresh-updated     329M 2017-04-26 12:49:00   00:00:00.000
Format specific information:
    compat: 1.1
    lazy refcounts: false
    refcount bits: 16
    corrupt: false

This time Guest machine was down, we can create snapshot when Guest machine is up and running.Below i tried to take another snapshot over earlier one in which Guest machine is up and running.

#qemu-img info system.image 
image: system.image
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 13G
cluster_size: 65536
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
1         srv7-Fresh-updated     329M 2017-04-26 12:49:00   00:00:00.000
2         srv7-Fresh-updated_1   336M 2017-04-26 15:57:01   00:33:20.085
Format specific information:
    compat: 1.1
    lazy refcounts: false
    refcount bits: 16
    corrupt: false

Now snapshot-list command shows.

#virsh snapshot-list srv7
 Name                 Creation Time             State
------------------------------------------------------------
 srv7-Fresh-updated   2017-04-26 12:49:00 +0530 running
 srv7-Fresh-updated_1 2017-04-26 15:57:01 +0530 running

We can also check snapshot status with snapshot-info command like below.

#virsh snapshot-info srv7 srv7-Fresh-updated 
Name:           srv7-Fresh-updated
Domain:         srv7
Current:        no
State:          running
Location:       internal
Parent:         -
Children:       1
Descendants:    1
Metadata:       yes

#virsh snapshot-info srv7 srv7-Fresh-updated_1
Name:           srv7-Fresh-updated_1
Domain:         srv7
Current:        yes
State:          running
Location:       internal
Parent:         srv7-Fresh-updated
Children:       0
Descendants:    0
Metadata:       yes

Revert Back of Snapshot

We can Revert back snapshot from present condition of Guest machine to earlier one that time we take snapshot. While reverting back snapshot Guest machine will automatically comes to that stage were it was at the time of taking snapshot.

Syntax

Syntax used to revert back Snapshot.

#virsh snapshot-revert <Domain-name> <Snapshot-name>

Example

In below example, i tried to revert back snapshot taken above

#virsh snapshot-revert srv7 srv7-Fresh-updated_1

Deletion of Snapshot

we can also delete Snapshot once usage of snapshot is complete, like below we delete snapshots taken above.

#virsh snapshot-delete srv7 srv7-Fresh-updated
Domain snapshot srv7-Fresh-updated deleted