Understanding how libvirt snapshots are stored

5,061

The xml files represents the snapshot metadata, if you open it you will see something like this (domain definition shortened for brevity):

your-snapshot.xml

<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
  virsh snapshot-edit
or other application using the libvirt API.
-->

<domainsnapshot>
  <name>your-snapshot</name>
  <state>shutoff</state>
  <creationTime>1567455358</creationTime>
  <memory snapshot='no'/>
  <disks>
    <disk name='hda' snapshot='internal'/>
  </disks>
  <domain type='kvm'>
    <name>centos76-client</name>
    [...]
  </domain>
  <active>1</active>
</domainsnapshot>

This file records the name of the snapshot, state of the domain when it was created, creation time, if memory was snapshot as well, and the disks, then the domain definition at that point as well.

This domain definition amounts to the same info you will get with virsh dumpxml your-domain. It's the configuration of your device, CPU, memory, etc. at the point of the snapshot. For example if after the snapshot you changed some device, memory size, etc. this allows you to go back to the exact setup you had back when the snapshot was taken.

Now on the disks section you can see which disk devices where actually snapshot. You expected a disk image to be created but with qcow2 files it works a bit differently.

qcow2 implements Copy-on-write images. On other Copy-on-write filesystems like btrfs this means that the files are not changed in place but rather when modified a copy of them with the changes will be stored somewhere else on the disk.

For qcow2 snapshots, the image is always modified in place but if a snapshot has been created, then within that qcow2 file whenever a file is modified after the snapshot, a copy before the modification is applied will be saved for the snapshot. This means that the image and the snapshots are both within the qcow2 file. (A more in-depth explanation of the qcow2 fileformat and how snapshots work can be found here.

You can list and interact with these disk snapshots (list, revert, create, delete) using the qemu-img command. For example you could list the snapshots like this:

# qemu-img snapshot -l /var/lib/libvirt/qemu/centos7-server.qcow2
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
1         your-snapshot             0 2019-09-02 22:15:58   00:00:00.000

The TAG here should match the name of your snapshot XML files and the name entry inside the XML itself, this is how libvirt knows what snapshot within the qcow2 image it should revert to.

Share:
5,061

Related videos on Youtube

Weezy
Author by

Weezy

Updated on September 18, 2022

Comments

  • Weezy
    Weezy over 1 year

    At first I thought it would be stored in /var/lib/libvirt/images/ but as I created snapshots for the centos7 domain, nothing changed in this directory:

    drwx--x--x 2 root         root       4096 Feb 29 21:28 .
    drwxr-xr-x 7 root         root       4096 Feb 28 23:47 ..
    -rw------- 1 libvirt-qemu kvm  5370216574 Feb 29 22:09 centos7-1.qcow2
    -rw------- 2 libvirt-qemu kvm  5931597824 Feb 29 22:12 centos7.qcow2
    -rw------- 1 root         root 1499267135 Feb 28 21:07 centos7-server.qcow2
    

    Next I checked /var/lib/libvirt/qemu/snapshot/centos7 which showed these xml files:

    client2.xml client.xml disks.xml

    Which are the names I gave to my snapshots.

    Can someone please tell me why snapshots are xml files and not disk images? What are these xml files storing and I'm guessing they need the original qcow2 image which is in my images directory to work and won't work with just any image - is that correct?

  • gstlouis
    gstlouis almost 3 years
    I like this explanation. Do you know if this applies for .img images as well? Basically same just different formats? qcow2 and img Because when I use the virsh commands it seems to have the same behaviours for xml, same storage location etc. so the snapshot is attached to the parent img file?