20.13. Working with Snapshots
20.13.1. Shortening a Backing Chain by Copying the Data
virsh blockcommit domain <path> [<bandwidth>] [<base>] [--shallow] [<top>] [--active] [--delete] [--wait] [--verbose] [--timeout <number>] [--pivot] [--keep-overlay] [--async] [--keep-relative]
command to shorten a backing chain. The command has many options, which are listed in the help menu or man page.
virsh blockcommit
command copies data from one part of the chain down into a backing file, allowing you to pivot the rest of the chain in order to bypass the committed portions. For example, suppose this is the current state:
base ← snap1 ← snap2 ← active
.
virsh blockcommit
moves the contents of snap2 into snap1, allowing you to delete snap2 from the chain, making backups much quicker.
Procedure 20.1. How to shorten a backing chain
- Enter the following command, replacing guest1 with the name of your guest virtual machine and disk1 with the name of your disk.
#
virsh blockcommit guest1 disk1 --base snap1 --top snap2
--wait
--verbose
The contents of snap2 are moved into snap1, resulting in:base ← snap1 ← active
. Snap2 is no longer valid and can be deletedWarning
virsh blockcommit
will corrupt any file that depends on the--base
argument (other than files that depended on the--top
argument, as those files now point to the base). To prevent this, do not commit changes into files shared by more than one guest. The--verbose
option will allow the progress to be printed on the screen.
20.13.2. Shortening a Backing Chain by Flattening the Image
virsh blockpull
can be used in in the following applications:
- 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 and looks like this:
- Before: base.img ← active
- After: base.img is no longer used by the guest and Active contains all of the data.
- Flattens part of the backing image chain. This can be used to flatten snapshots into the top-level image and looks like this:
- Before: base ← sn1 ←sn2 ← active
- After: base.img ← active. Note that active now contains all data from sn1 and sn2, and neither sn1 nor sn2 are used by the guest.
- Moves the disk image to a new file system on the host. This is allows image files to be moved while the guest is running and looks like this:
- Before (The original image file):
/fs1/base.vm.img
- After:
/fs2/active.vm.qcow2
is now the new file system and/fs1/base.vm.img
is no longer used.
- Useful in live migration with post-copy storage migration. The disk image is copied from the source host to the destination host after live migration completes.In short this is what happens: Before:
/source-host/base.vm.img
After:/destination-host/active.vm.qcow2
./source-host/base.vm.img
is no longer used.
Procedure 20.2. How to shorten a backing chain by flattening the data
- It may be helpful to create a snapshot prior to running
virsh blockpull
. To do so, use thevirsh snapshot-create-as
command. In the following example, replace guest1 with the name of your guest virtual machine, and snap1 with the name of your snapshot.#
virsh snapshot-create-as guest1 snap1
--disk-only
- If the chain looks like this:
base ← snap1 ← snap2 ← active
, enter the following command, replacing guest1 with the name of your guest virtual machine and path1 with the source path to your disk (/home/username/VirtualMachines/*
, for example).#
virsh blockpull guest1 path1
This command makes snap1 the backing file of active, by pulling data from snap2 into active resulting in:base ← snap1 ← active
. - Once the
virsh 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, replacing guest1 with the name of your guest virtual machine and snap1 with the name of your snapshot.#
virsh snapshot-delete guest1 snap1 --metadata
virsh blockpull
can be performed as follows:
Example 20.31. How to flatten a single image and populate it with data from its backing image chain
# virsh blockpull guest1 vda --wait
Example 20.32. How to flatten part of the backing image chain
# virsh blockpull guest1 vda /path/to/base.img --base
--wait
Example 20.33. How to move the disk image to a new file system on the host
# virsh snapshot-create guest1 --xmlfile /path/to/snap1.xml --disk-only
# virsh blockpull guest1 disk1 --wait
Example 20.34. How to use live migration with post-copy storage migration
# qemu-img create -f qcow2
-o
backing_file=/source-host/vm.img
/destination-host/vm.qcow2
# virsh migrate guest1
# virsh blockpull guest1 disk1 --wait
20.13.3. Changing the Size of a Guest Virtual Machine's Block Device
virsh blockresize
command can be used to resize a block device of a guest virtual machine while the guest virtual machine is running, using the absolute path of the block device, which also corresponds to a unique target name (<target dev="name"/>
) or source file (<source file="name"/>
). This can be applied to one of the disk devices attached to guest virtual machine (you can use the command virsh domblklist
to print a table showing the brief information of all block devices associated with a given guest virtual machine).
Note
echo > /sys/class/scsi_device/0:0:0:0/device/rescan
. In addition, with IDE it is required to reboot the guest before it picks up the new size.
Example 20.35. How to resize the guest virtual machine block device
# virsh blockresize guest1 90 B