knktc's Notes

python, cloud, linux...

0%

How to Duplicate a KVM Snapshot

KVM snapshots can preserve a virtual machine’s in-memory runtime state. After reverting to a snapshot, the VM continues from the exact state it was in when the snapshot was created, much like a save/load operation in a game.

That makes snapshots useful for several scenarios, such as restoring a sandbox quickly without cleaning the environment or waiting for services to reboot one by one.

The problem is that KVM snapshots are not especially easy to reuse at scale. When you try to duplicate them in bulk, the copied snapshots often fail after boot and cannot be restored properly. This article describes a lower-cost way to duplicate a VM snapshot so it can be reused for mass deployment.

Snapshot commands

The snapshot capability actually comes from QEMU, and it requires the VM image to use the qcow2 format. Formats such as raw do not support this kind of runtime snapshot.

Since libvirt is the management tool most of us use, it provides convenient commands for creating and reverting snapshots:

1
2
3
4
virsh snapshot-create kvm01
virsh snapshot-list kvm01
virsh snapshot-revert kvm01 --current
virsh snapshot-revert kvm01 --snapshotname 1515143552

OpenStack snapshots do not preserve runtime memory state. They only preserve disk state, so restoring them still requires the VM to reboot.

Where is the snapshot stored?

Snapshot metadata is stored inside the image file itself. You can inspect it with qemu-img info:

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@knktc snapshot]# qemu-img info source.img
image: source.img
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 3.1G
cluster_size: 65536
backing file: /opt/snapshot/base.img
Snapshot list:
ID TAG VM SIZE DATE VM CLOCK
1 1519990436 2.6G 2018-03-02 19:33:56 00:08:27.846
Format specific information:
compat: 1.1
lazy refcounts: false

That means duplicating the snapshot really means duplicating the image that contains it. Because full VM images are large, the best approach is to rely on qcow2 backing files so only incremental data is stored.

The basic idea is:

  1. Create a base VM image.
  2. Create a new VM from that base image and create a snapshot.
  3. Copy the new image to duplicate the snapshot.

Create a new VM and snapshot

Assume the base image is base.img. Create a derived image:

1
qemu-img create -b base.img -f qcow2 source.img

Start a VM from source.img, let it reach the state you want, then create the snapshot:

1
virsh snapshot-create --domain source

Example output:

1
Domain snapshot 1519990436 created

To keep the image from growing unnecessarily, force the VM off:

1
virsh destroy source

You can confirm the snapshot is there with either:

1
qemu-img info source.img

or:

1
virsh snapshot-list source

Export the snapshot XML

Export the snapshot definition:

1
virsh snapshot-dumpxml --domain source --snapshotname 1519990436 > snapshot.xml

The exported XML contains both the snapshot metadata and nearly the full VM definition. That is exactly what makes duplication possible.

Duplicate the snapshot

First copy the image:

1
cp source.img test01.img

Then create a new VM named test01 that uses test01.img.

After defining that VM, inspect its actual configuration:

1
virsh dumpxml test01

Use that information to edit the exported snapshot.xml. The fields that must be updated are:

  1. UUID
  2. VM name
  3. image path
  4. MAC address
  5. VNC port

If those values do not match the real configuration of test01, the duplicated snapshot will not work.

如果我的文字帮到了您,那么可不可以请我喝罐可乐?