virsh - snapshot

最後更新: 2017-04-27


By default libvirt 的 snapshot 會保儲

  • memory
  • disk
  • device state




snapshot-list centos6.3

Name                 Creation Time             State

virsh # snapshot-current centos6.3

error: domain 'centos6.3' has no current snapshot


Default: disk and RAM    ( 加了 --disk-only 就只會 backup harddisk)

virsh # snapshot-create centos6.3

Domain snapshot 1345047528 created

virsh # snapshot-list centos6.3

 Name                 Creation Time             State
 1345047528           2012-08-16 00:18:48 +0800 shutoff
 1345047747           2012-08-16 00:22:27 +0800 shutoff

# 不是用數字做名的 snapshot:

virsh # snapshot-create-as debian7 clearos "clear state"

virsh # snapshot-list debian7

Name                 Creation Time             State
 clearos              2013-12-11 15:51:25 +0800 shutoff


snapshot-parent centos6.3 1345047747

1345047528       <-- 只顯示一層


snapshot-revert centos6.3 1345047528


virsh # snapshot-delete centos6.3 1345047528

Domain snapshot 1345047528 deleted


External snapshots with libvirt


 * At the moment external snapshots are a one-way operation

    (libvirt can create them but cannot do anything further with them)


# snapshot-create-as && --disk-only

virsh snapshot-create-as mydom snap --disk-only


take Internal snapshots 係比 External snapshots 慢的.


Snapshot By XML



    <description>type here </description>

virsh snapshot-create VM1 /var/tmp/VM1-snap1.xml



snapshot-dumpxml centos6.3 1345047956

  <domain type='kvm'>



Snapshot 的分類


Snapshot 的儲結構一共有種

  • Internal snapshot (qcow2) <-- Deafult
  • External snapshot (vdi)
  • VM State (ram)

查看 Image 檔的內容:

qemu-img info /mnt/vm/centos6.3.qcow2

file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 818M
cluster_size: 65536
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
3         1345047956                0 2012-08-16 00:25:56   00:00:00.000

External snapshot XML:

<disk name='vda' snapshot='external'>
  <source file='/path/to/new'/>


Active blockcommit


# 需要 Version

QEMU 2.1 (and above), libvirt-1.2.9 (and above).

qemu --version
libvirtd --version

It is a two phase operation:

(1) once an external snapshot (which results in a qcow2 overlay that tracks the new writes from then on) is taken, the content from the overlay can be copied into its backing file (e.g. 'base'); then

(2) pivot live QEMU to 'base'. All of this while the guest is running.


# List the current block device in use:
virsh domblklist win7


# Create an external disk snapshot:
virsh snapshot-create-as win7 guest-state1 \
    --diskspec vda,file=/data/vm/win7/overlay1.qcow2 \
    --disk-only --atomic

# Create a snapshot for domain <domain> with the given <name>
snapshot-create-as <domain> {[--print-xml] | [--no-metadata] [--halt]
       [--reuse-external]} [name] [description] [--disk-only [--quiesce] [--atomic]
       [[--diskspec] diskspec]...]
request a disk-only snapshot
the command can also take additional diskspec
then the snapshot data is created, but any metadata is immediately discarded
(that is, libvirt does not treat the snapshot as current, and cannot revert to the snapshot unless snapshot-create is later used to teach libvirt about the metadata again)
libvirt will guarantee that the snapshot either succeeds, or fails with no changes;

[base] (live QEMU)
[base] <-- [overlay1] (live QEMU)

NOTE-1: Above, if you have QEMU guest agent installed in your virtual machine, try '--quiesce' option with virsh snapshot-create-as[. . .] to ensure you have a consistent disk state.

NOTE-2: Optionally, you can also supply '--no-metadata' option to tell libvirt to not track the snapshot metadata -- this is useful currently, at a later point when you decide to merge snapshot files, you have to explicitly clean the libvirt metadata


virsh domblklist win7

Take a backup of the original disk in backround using your faviourite tool:

$ cp /export/images/base.img /export/images/copy.img


$ rsync -avh --progress /export/images/base.img \

[4] live mergning contents

virsh blockcommit win7 vda --active --verbose --pivot

--active: It performs a two stage operation:

first stage – it commits the contents from top images into base (i.e. sn1, sn2, current into base);

second stage, the block operation remains awake to synchronize any further changes (from top images into base), here the user can take two actions: cancel the job, or pivot the job, i.e. adjust the base image as the current active image.

--pivot: Once data is committed from sn1, sn2 and current into base, it pivots the live QEMU to use base as the active image.
--verbose: Displays a progress of block operation.

# checking

virsh domblklist vm1





Flattens an image by populating it with data from its backing image chain.
This makes the image file self-contained so that it no longer depends on backing images

looks like this:

Before: base.img ← Active

After: base.img is no longer used by the guest and Active contains all of the data.

# virsh snapshot-create-as $dom $name - disk-only

# Before: base ← snap1 ← snap2 ← active

virsh blockpull $dom $disk snap1

This command makes 'snap1' the backing file of active,
by pulling data from snap2 into active resulting in:

After: base ← snap1 ← active.

Once the blockpull is complete, the libvirt tracking of the snapshot that created the extra image in the chain is no longer useful. Delete the tracking on the outdated snapshot with this command:

virsh snapshot-delete $dom $name - metadata


blockpull domain path [bandwidth] [base] [--wait [--verbose]

path specifies fully-qualified path of the disk;

<source file='name'/>

By default, this command flattens the entire chain;

but if base is specified, containing the name of one of the
           backing files in the chain, then that file becomes the new backing file and
           only the intermediate portion of the chain is pulled.

By default, this command returns as soon as possible, and data for the entire
 disk is pulled in the background; the progress of the operation can be checked
 with blockjob.  However, if --wait is specified, then this command will block
 until the operation completes, or cancel the operation if the optional timeout
in seconds elapses or SIGINT is sent (usually with "Ctrl-C").

Using --verbose along with --wait will produce periodic status updates.





Creative Commons license icon Creative Commons license icon