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 blockcommitcommand 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 blockcommitmoves 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
--verboseThe contents of snap2 are moved into snap1, resulting in:
base ← snap1 ← active. Snap2 is no longer valid and can be deleted
virsh blockcommitwill corrupt any file that depends on the
--baseargument (other than files that depended on the
--topargument, as those files now point to the base). To prevent this, do not commit changes into files shared by more than one guest. The
--verboseoption will allow the progress to be printed on the screen.
20.13.2. Shortening a Backing Chain by Flattening the Image
virsh blockpullcan 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):
/fs2/active.vm.qcow2is now the new file system and
/fs1/base.vm.imgis 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.imgis 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 the
virsh snapshot-create-ascommand. 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
- 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 path1This command makes snap1 the backing file of active, by pulling data from snap2 into active resulting in:
base ← snap1 ← active.
- Once the
virsh blockpullis 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 blockpullcan 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
Example 20.32. How to flatten part of the backing image chain
virsh blockpull guest1 vda /path/to/base.img
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
Example 20.34. How to use live migration with post-copy storage migration
qemu-img create -f
virsh migrate guest1
virsh blockpull guest1 disk1
20.13.3. Changing the Size of a Guest Virtual Machine's Block Device
virsh blockresizecommand 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 domblklistto print a table showing the brief information of all block devices associated with a given guest virtual machine).
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