This short guide on How to Create VM snapshot on KVM aims at helping you use KVM virsh commands to take a snapshot of a VM. I’ll also include steps for restoring to an old snapshot of the VM.
I assume you have a working KVM server with Virsh Commands. If you’re new and would like to setup KVM, use the link:
Install KVM on CentOS 7 / Ubuntu 16.04 / Debian 9 / SLES 12 / Arch Linux
Create VM on KVM
Once you have KVM running, if you don’t have a test vm, you can install it using a virsh command like below:
sudo virt-install \
--name centos7 \
--description "Test VM with CentOS 7" \
--ram=1024 \
--vcpus=2 \
--os-type=Linux \
--os-variant=rhel7 \
--disk path=/var/lib/libvirt/images/centos7.qcow2,bus=virtio,size=10 \
--graphics none \
--location $HOME/iso/CentOS-7-x86_64-Everything-1611.iso \
--network bridge:virbr0 \
--console pty,target_type=serial -x 'console=ttyS0,115200n8 serial'
This will install CentOS VM with:
- named centos7
- 2gb ram
- 2vcpu
- 10gb virtual disk
- default bridge attached – virbr0
Answer all questions asked during normal installation of CentOS. VM will be autostarted once the installation is done.
Create VM snapshot on KVM
With your VM running, let’s proceed to create a snapshot. For a later demonstration on restoring VM state to snapshot, I’ll create two snapshots.
$ sudo virsh snapshot-create-as --domain centos7 \ --name "centos7_vm_snapshot1" \ --description "centos7 vm snapshot 1" Domain snapshot centos7_vm_snapshot1 created
Before we take a second snapshot, le’s install few packages:
$ sudo yum -y install vim elinks epel-release
I’ll then take the second snapshot:
$ sudo virsh snapshot-create-as --domain centos7 \ --name "centos7_vm_snapshot2" \ --description "centos7 vm snapshot 2" Domain snapshot centos7_vm_snapshot2 created
We’re now set to go.
List VM Snapshots on KVM
We have two snapshots taken initially, let’s check if we can see all these snapshots:
$ sudo virsh snapshot-list test Name Creation Time State ------------------------------------------------------- centos7_vm_snapshot1 2018-03-24 22:09:48 +0300 shutoff centos7_vm_snapshot2 2018-03-24 02:09:28 +0300 running
Revert to VM Snapshot on KVM
To revert to a snapshot on KVM, use the commands shown below:
$ sudo virsh snapshot-revert --domain centos7 --snapshotname \ centos7_vm_snapshot1 --running
The VM should be restored to a state that we took the first snapshot.
Delete VM Snapshot on KVM
If you no longer need the VM snapshot, you can delete it using the command snapshot-delete followed by the name of the snapshot.
$ sudo virsh snapshot-delete --domain centos7 --snapshotname centos7_vm_snapshot1 Domain snapshot centos7_vm_snapshot1 deleted
If you check a list of available snapshots now, you should see only one snapshot.
$ sudo virsh snapshot-list test
Display Snapshot details on KVM
Use the option snapshot-info with the virsh command to get more details about a given snapshot on KVM.
sudo virsh snapshot-info --domain "$1" "$2"
Where:
Argument 1: domain name
Argument 2: Snapshot name
This guide has covered most basic snapshot management commands for KVM.