OSETC TECH

How To Create/Restore/Delete VM Snapshot on KVM Hyperviser

This post will guide you how to create/restore/delete Virtual Machine Snapshot in KVM Hyperviser under Linux operating system. How do I create or delete VM snapshot using virsh command in your Linux system.

Virsh Command


The virsh program is the main interface for managing virsh guest domains. The program can be used to create, pause, and shutdown domains. It can also be used to list current domains. Libvirt is a C toolkit to interact with the virtualization capabilities of recent versions of Linux (and other OSes). It is free software available under the GNU Lesser General Public License. Virtualization of the Linux Operating System means the ability to run multiple instances of Operating Systems concurrently on a single hardware system where the basic resources are driven by a Linux instance. The library aims at providing a long term stable C API. It currently supports Xen, QEMU, KVM, LXC, OpenVZ, VirtualBox and VMware ESX.

The Syntax of the Virsh command are as followss:

virsh [OPTION]... [COMMAND_STRING]

virsh [OPTION]... COMMAND [ARG]...

List All KVM VMs


If you want to list all KVM VM guests in your KVM hypervisor, and you can use the following “virsh list -all” command, type:

$ virsh list --all

Outputs:

[root@mydevops ~]# virsh list --all
Id Name State
----------------------------------------------------
1 generic running
4 centos7.0 running

Create KVM VM Snapshot


If you want to create a snapshot of a kvm vm guest in your Kvm hypervisor, and you can use virsh command with snapshot-create-as subcommand and “–domain“, “–name” options. For example, you want to create the snapshot of “centos7.0” VM, and you can run the following command:

$ virsh snapshot-create-as --domain centos7.0 --name centos7.0_snapshot01

Outputs:

[root@mydevops ~]# virsh snapshot-create-as --domain centos7.0 --name centos7.0_snapshot01
Domain snapshot centos7.0_snapshot01 created

then you can run the following command to check if snapshot named centos7.0_snapshot01 for “centos7.0” domain is created or not, type:

$ virsh snapshot-list --domain centos7.0

Outputs:

[root@mydevops ~]# virsh snapshot-list --domain centos7.0
Name Creation Time State
------------------------------------------------------------
centos7.0_snapshot01 2019-10-02 10:58:49 -0400 running

If you want to see more detailed information for a VM domain called “centos7.0“, and you can use the following command:

$ virsh snapshot-info --domain centos7.0 --snapshotname centos7.0_snapshot01

Outputs:

[root@mydevops ~]# virsh snapshot-info --domain centos7.0 --snapshotname centos7.0_snapshot01
Name: centos7.0_snapshot01
Domain: centos7.0
Current: yes
State: running
Location: internal
Parent: -
Children: 0
Descendants: 0
Metadata: yes

Restore KVM VM Snapshot


If you want to revert or restore your KVM VM guest to the snapshot you created in the above step. and you can use the virsh command with “snapshot-revert” subcommand, and you also need to provide domain name and its snapshot name. type:

$ virsh snapshot-revert centos7.0 centos7.0_snapshot01

Outputs:

[root@mydevops ~]# virsh snapshot-revert centos7.0 centos7.0_snapshot01

[root@mydevops ~]#

Delete KVM VM Snapshot


If you want to delete KVM VM snapshots in your KVM hypervisor, and you need to use “virsh snapshot-list” command to list all snapshots of this domain, and then you can use virsh command with its snapshot-delete subcommand. type:

$ virsh snapshot-list --domain centos7.0 
$ virsh snapshot-delete --domain centos7.0 --snapshotname centos7.0_snapshot01

Outputs:

[root@mydevops ~]# virsh snapshot-list --domain centos7.0
Name Creation Time State
------------------------------------------------------------
centos7.0_snapshot01 2019-10-02 10:58:49 -0400 running

[root@mydevops ~]# virsh snapshot-delete --domain centos7.0 --snapshotname centos7.0_snapshot01
Domain snapshot centos7.0_snapshot01 deleted

If you want to get more help about virsh command, you can run the following command:

$ virsh --help

or

$ man virsh

Outputs:

[root@mydevops ~]# virsh --help

virsh [options]... [<command_string>]
virsh [options]... <command> [args...]

options:
-c | --connect=URI hypervisor connection URI
-d | --debug=NUM debug level [0-4]
-e | --escape <char> set escape sequence for console
-h | --help this help
-k | --keepalive-interval=NUM
keepalive interval in seconds, 0 for disable
-K | --keepalive-count=NUM
number of possible missed keepalive messages
-l | --log=FILE output logging to file
-q | --quiet quiet mode
-r | --readonly connect readonly
-t | --timing print timing information
-v short version
-V long version
--version[=TYPE] version, TYPE is short or long (default short)
......
Snapshot (help keyword 'snapshot')
snapshot-create Create a snapshot from XML
snapshot-create-as Create a snapshot from a set of args
snapshot-current Get or set the current snapshot
snapshot-delete Delete a domain snapshot
snapshot-dumpxml Dump XML for a domain snapshot
snapshot-edit edit XML for a snapshot
snapshot-info snapshot information
snapshot-list List snapshots for a domain
snapshot-parent Get the name of the parent of a snapshot
snapshot-revert Revert a domain to a snapshot
......

If you want to get more information about “virsh snapshot-create-as” command, you can type the following command:

$ virsh help snapshot-create-as

Outputs:

[root@mydevops ~]# virsh help snapshot-create-as
NAME
snapshot-create-as - Create a snapshot from a set of args

SYNOPSIS
snapshot-create-as <domain> [--name <string>] [--description <string>] [--print-xml] [--no-metadata] [--halt] [--disk-only] [--reuse-external] [--quiesce] [--atomic] [--live] [--memspec <string>] [[--diskspec] <string>]...

DESCRIPTION
Create a snapshot (disk and RAM) from arguments

OPTIONS
[--domain] <string> domain name, id or uuid
--name <string> name of snapshot
--description <string> description of snapshot
--print-xml print XML document rather than create
--no-metadata take snapshot but create no metadata
--halt halt domain after snapshot is created
--disk-only capture disk state but not vm state
--reuse-external reuse any existing external files
--quiesce quiesce guest's file systems
--atomic require atomic operation
--live take a live snapshot
--memspec <string> memory attributes: [file=]name[,snapshot=type]
[--diskspec] <string> disk attributes: disk[,snapshot=type][,driver=type][,file=name]

Conclusion


You should know that how to create/revert/delete VM snapshots using virsh command in your KVM hypervisor in your CentOS or RHEL or Ubuntu system.