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.
virt-sparsifycan convert a raw disk image to a thin-provisioned qcow2 image.
virt-sparsifywill work, but it will not be very effective.
ls -lh test1.img-rw-rw-r--. 1 rjones rjones 100M Aug 8 08:08 test1.img $
du -sh test1.img3.6M test1.img
lscommand shows the image size to be 100M. However, the
ducommand correctly shows the image size to be 3.6M.
- 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
virt-sparsify /dev/sda1 /dev/device
/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
virt-sparsify disk.raw --convert qcow2 disk.qcow2
virt-sparsify --ignore /dev/device
virt-sparsify --in-place disk.img
|--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.|| $ |