Skip to content
Paolo Cozzi edited this page Dec 17, 2015 · 5 revisions

Create a snapshot

Check for requirements

Check the type of disk image

Before attempting a snapshot, ensure that the disks you want to backup up are in qcow2 format. You can check this by dumping the domain configuration file with:

$ virsh dumpxml <domain_name>

You can retrieve the list of all defined domains (activer of not) with:

$ virsh list --all

Here you can retrieve a list of the most used virsh commands. By inspecting the XML domain configuration file, you have to search type='qcow2' in the disk block under the devices tag:

  <devices>
    ...
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2' cache='none' discard='unmap'/>
      <source file='/var/lib/libvirt/images/1672ff62-ab9e-49d7-886b-c4ef0e2d2019-0.img'/>
      ...
    </disk>
    ...
  </devices>

Install and configure qemu agent inside Guest

You need to install qemu-guest-agent in order to ensure you have a consistent disk state during snapshot. You can install it via package manager:

# On ubuntu
$ sudo apt-get install qemu-guest-agent
# On centos
$ yum install qemu-guest-agent

Now you can exit from Guest and configure qemu-guest-agent socket. In order to edit Guest XML configuration file:

$ virsh edit <domain_name>

Then place this piece of code under devices section:

  <devices>
    ...
    <channel type="unix">
      <source mode="bind"/>
      <target type="virtio" name="org.qemu.guest_agent.0"/>
    </channel>
    ...
  </devices>

Then restart Guest using virsh:

$ virsh shutdown <domain_name>
$ virsh start <domain_name>

This will create a new virtual serial device within the VM and a new socket under /var/lib/libvirt/qemu/channel/target/. Ensure that Guest agent process is active on Guest. Then test channel on Host, by using

$ virsh qemu-agent-command <domain_name> '{"execute":"guest-info"}' | python -mjson.tool

You can found more information in installing qemu-guest-agent here and here

Create a Snapshot manually

This procedure is inspired from official libvirt documentation, and a guide found here and here. First of all, make a dump of xml configuration file (DockerNode2 is the domain name of these examples):

$ virsh dumpxml DockerNode2 > ~/DockerNode2.xml

Then explore virtual hard disk which we want to backup with:

$ virsh domblklist DockerNode2
Target     Source
------------------------------------------------
hda        /var/lib/libvirt/images/829cd357-8ae7-4d0d-9a3b-308fbcbc8e7b-0.img
hdc        /var/lib/kimchi/isos/CentOS-7.0-1406-x86_64-Minimal.iso

In the previous example, the disk we want to backup is hda. First of all, we have to create a disk snapshot in order to backup a running image:

$ virsh snapshot-create-as --domain DockerNode2 sn1 --diskspec hda,file=/var/lib/libvirt/images/sn1.img --disk-only --atomic --quiesce

the --quiesce parameter ensure a consistent state, but it need that QEMU guest agent is installed on Guest. Then, if you redo a domblklist command, you would see that the hda image now is pointing to the user defined file:

$ virsh domblklist DockerNode2
Target     Source
------------------------------------------------
hda        /var/lib/libvirt/images/sn1.img
hdc        /var/lib/kimchi/isos/CentOS-7.0-1406-x86_64-Minimal.iso

You can inspect snapshot by using:

$ virsh snapshot-list DockerNode2
Nome                 Creation Time             Stato
------------------------------------------------------------
sn1                  2015-12-17 13:55:25 +0100 disk-snapshot

All new data will be placed in hda snapshot, thus we can backup the original image by copying it in another location

$ cp -a /var/lib/libvirt/images/829cd357-8ae7-4d0d-9a3b-308fbcbc8e7b-0.img .

Once backup is finished, perform active blockcommit by live merging contents of sn1 into base image (this will write on disk all changes made during backup):

$ virsh  blockcommit DockerNode2 hda --active --verbose --pivot
Block Commit: [100 %]
Successfully pivoted

Ensure that hda image points to the original file with domblklist:

$ virsh domblklist DockerNode2
Target     Source
------------------------------------------------
hda        /var/lib/libvirt/images/829cd357-8ae7-4d0d-9a3b-308fbcbc8e7b-0.img
hdc        /var/lib/kimchi/isos/CentOS-7.0-1406-x86_64-Minimal.iso

Now we can delete snapshot metadata: snapshot image might be manually removed in older qemu package versions:

$ virsh snapshot-delete DockerNode2 --snapshotname sn1 --children --metadata
$ rm /var/lib/libvirt/images/sn1.img

Those are steps needed in order to do a KVM snapshot. In the next chapter we will automate those operation by running kvmBackup.py

References