21.14. virt-sparsify: Reclaiming Empty Disk Space
virt-sparsifycommand-line tool can be used to make a virtual machine disk (or any disk image) sparse. This is also known as thin-provisioning. Free disk space on the disk image is converted to free space on the host.
virt-sparsifycommand can work with most filesystems, such as ext2, ext3, ext4, btrfs, NTFS. It also works with LVM physical volumes.
virt-sparsifycan operate on any disk image, not just virtual machine disk images.
virt-sparsifyon live virtual machines, or concurrently with other disk editing tools can cause disk corruption. The virtual machine must be shut down before using this command. In addition, disk images should not be edited concurrently.
The command can also be used to convert between some disk formats. For example,
virt-sparsifycan convert a raw disk image to a thin-provisioned qcow2 image.
If a virtual machine has multiple disks and uses volume management,
virt-sparsifywill work, but it will not be very effective.
If the input is raw, then the default output is raw sparse. The size of the output image must be checked using a tool that understands sparseness.
ls -lh test1.img-rw-rw-r--. 1 rjones rjones 100M Aug 8 08:08 test1.img $
du -sh test1.img3.6M test1.img
Note that the
lscommand shows the image size to be 100M. However, the
ducommand correctly shows the image size to be 3.6M.
The following is a list of important limitations:
- The virtual machine must be shutdown before using
- In a worst case scenario,
virt-sparsifymay require up to twice the virtual size of the source disk image. One for the temporary copy and one for the destination image.If you use the
--in-placeoption, large amounts of temporary space are not needed.
virt-sparsifycannot be used to resize disk images. To resize disk images, use
virt-resize. For information about
virt-resize, see Section 21.8, “virt-resize: Resizing Guest Virtual Machines Offline”.
virt-sparsifydoes not work with encrypted disks, because encrypted disks cannot be sparsified.
virt-sparsifycannot sparsify the space between partitions. This space is often used for critical items like bootloaders, so it is not really unused space.
copymode, qcow2 internal snapshots are not copied to the destination image.
virt-sparsify, run one of the following commands:
yum install /usr/bin/virt-sparsify
yum install libguestfs-tools-c
To sparsify a disk:
virt-sparsify /dev/sda1 /dev/device
Copies the contents of
/dev/device, making the output sparse. If
/dev/devicealready exists, it is overwritten. The format of
/dev/sda1is detected and used as the format for
To convert between formats:
virt-sparsify disk.raw --convert qcow2 disk.qcow2
Tries to zero and sparsify free space on every filesystem it can find within the source disk image.
To prevent free space from being overwritten with zeros on certain filesystems:
virt-sparsify --ignore /dev/device
Creates sparsified disk images from all filesystems in the disk image, without overwriting free space on the filesystems with zeros.
To make a disk image sparse without creating a temporary copy:
virt-sparsify --in-place disk.img
Makes the specified disk image sparse, overwriting the image file.
The following command options are available to use with
|--help|| Displays a brief help entry about a particular command or about the || |
| --check-tmpdir || Estimates if tmpdir has enough space to complete the operation. The specified option determines the behavior if there is not enough space to complete the operation.
This option cannot be used with the
|--compress|| Compresses the output file. This only works if the output format is qcow2. This option cannot be used with the || |
Creates the sparse image using a specified format. If no format is specified, the input format is used.
The following output formats are supported and known to work: raw, qcow, vdi
You can use any format supported by the QEMU emulator.
It is recommended that you use the
This option cannot be used with the
|--format||Specifies the format of the input disk image. If not specified, the format is detected from the image. When working with untrusted raw-format guest disk images, ensure to specify the format.|| |
Ignores the specified file system or volume group.
When a filesystem is specified and the
When a volume group is specified, the volume group is ignored. The volume group name should be used without the
Makes an image sparse in-place, instead of making a temporary copy. Although in-place sparsification is more efficient than copying sparsification, it cannot recover quite as much disk space as copying sparsification. In-place sparsification works using discard (also known as trim or unmap) support.
To use in-place sparsification, specify a disk image that will be sparsified in-place.
When specifying in-place sparsification, the following options cannot be used:
|-x||Enables tracing of libguestfs API calls.|| |
For more information, including additional options, see libguestfs.org.