virt-sparsify(1)

πŸ’… virt-sparsify(1) β€” Make a virtual machine disk sparse

🌸 NAME

virt-sparsify β€” make a virtual machine disk sparse. That's the whole vibe of the virt-sparsify(1) manual page: it takes a disk image and makes it sparse.

✨ SYNOPSIS

virt-sparsify [--options] indisk outdisk

virt-sparsify [--options] --in-place disk

🚨 WARNING

Bestie, please do not skip this part. Using virt-sparsify on live virtual machines, or concurrently with other disk editing tools, can be dangerous, potentially causing disk corruption. The virtual machine must be shut down before you use this command, and disk images must not be edited concurrently. No cap, this is the "do not test me" warning.

🌸 DESCRIPTION

virt-sparsify is a tool that can make a virtual machine disk β€” or literally any disk image β€” sparse, a.k.a. thin-provisioned. What that means: free space inside the disk image gets converted back into free space on the host. Slay.

It can locate and sparsify free space in most filesystems (eg. ext2/3/4, btrfs, NTFS, etc.), and also in LVM physical volumes.

It can also convert between some disk formats, for example converting a raw disk image to a thin-provisioned qcow2 image.

virt-sparsify can operate on any disk image, not just ones from virtual machines. However, if a virtual machine has multiple disks and uses volume management, then virt-sparsify will work but not be very effective (http://bugzilla.redhat.com/887826).

πŸ¦‹ IMPORTANT NOTE ABOUT SPARSE OUTPUT IMAGES

If the input is raw, then the default output is raw sparse. You must check the output size using a tool that understands sparseness such as du -sh. It can make a huge difference:

$ ls -lh test1.img
-rw-rw-r--. 1 rjones rjones 100M Aug  8 08:08 test1.img
$ du -sh test1.img
3.6M   test1.img

Compare the apparent size 100M vs the actual size 3.6M. Iconic behavior, honestly.

🧚 IMPORTANT LIMITATIONS

  • The virtual machine must be shut down before using this tool.
  • virt-sparsify may require up to 2x the virtual size of the source disk image (1 temporary copy + 1 destination image). This is in the worst case and usually much less space is required.

If you are using the --in-place option, then large amounts of temporary space are not required.

  • virt-sparsify cannot resize disk images. To do that, use virt-resize(1).
  • virt-sparsify cannot handle encrypted disks. Libguestfs supports encrypted disks, but encrypted disks themselves cannot be sparsified.
  • virt-sparsify cannot yet sparsify the space between partitions. Note that this space is often used for critical items like bootloaders so it's not really unused.
  • In copy mode, qcow2 internal snapshots are not copied over to the destination image.

You may also want to read the manual pages for the associated tools virt-filesystems(1) and virt-df(1) before starting.

πŸ’… EXAMPLES

Typical usage is:

virt-sparsify indisk outdisk

which copies indisk to outdisk, making the output sparse. outdisk is created, or overwritten if it already exists. The format of the input disk is detected (eg. qcow2) and the same format is used for the output disk.

To convert between formats, use the --convert option:

virt-sparsify disk.raw --convert qcow2 disk.qcow2

virt-sparsify tries to zero and sparsify free space on every filesystem it can find within the source disk image. You can get it to ignore (don't zero free space on) certain filesystems by doing:

virt-sparsify --ignore /dev/sda1 indisk outdisk

See virt-filesystems(1) to get a list of filesystems within a disk image.

Since virt-sparsify β‰₯ 1.26, you can now sparsify a disk image in place by doing:

virt-sparsify --in-place disk.img

πŸ‘‘ OPTIONS

  • --help β€” display help.
  • --check-tmpdir ignore, --check-tmpdir continue, --check-tmpdir warn, --check-tmpdir fail β€” check if TMPDIR or the --tmp directory has enough space to complete the operation. This is just an estimate.

If the check indicates a problem, then you can either:

  • ignore it,
  • print a warning and continue,
  • warn and wait for the user to press the Return key (this is the default), or
  • fail and exit.

You cannot use this option and --in-place together.

  • --colors, --colours β€” use ANSI colour sequences to colourize messages. This is the default when the output is a tty. If the output of the program is redirected to a file, ANSI colour sequences are disabled unless you use this option.
  • --compress β€” compress the output file. This only works if the output format is qcow2.

You cannot use this option and --in-place together.

  • --convert raw, --convert qcow2, --convert [other formats] β€” use output-format as the format for the destination image. If this is not specified, then the input format is used.

Supported and known-working output formats are: raw, qcow2, vdi.

You can also use any format supported by the qemu-img(1) program, eg. vmdk, but support for other formats is reliant on qemu.

Specifying the --convert option is usually a good idea, because then virt-sparsify doesn't need to try to guess the input format.

For fine-tuning the output format, see: --compress, -o.

You cannot use this option and --in-place together.

  • --echo-keys β€” when prompting for keys and passphrases, virt-sparsify normally turns echoing off so you cannot see what you are typing. If you are not worried about Tempest attacks and there is no one else in the room you can specify this flag to see what you are typing.
  • --format raw, --format qcow2 β€” specify the format of the input disk image. If this flag is not given then it is auto-detected from the image itself.

If working with untrusted raw-format guest disk images, you should ensure the format is always specified.

  • --ignore filesystem, --ignore volgroup β€” ignore the named filesystem.

When not using --in-place: free space on the filesystem will not be zeroed, but existing blocks of zeroes will still be sparsified.

When using --in-place, the filesystem is ignored completely.

In the second form, this ignores the named volume group. Use the volume group name without the /dev/ prefix, eg. --ignore vg_foo.

You can give this option multiple times.

  • --in-place β€” do in-place sparsification instead of copying sparsification. See IN-PLACE SPARSIFICATION below.
  • --key SELECTOR β€” specify a key for LUKS, to automatically open a LUKS device when using the inspection.
    • --key NAME:key:KEY_STRING, --key UUID:key:KEY_STRING, --key all:key:KEY_STRING β€” NAME is the libguestfs device name (eg. /dev/sda1). UUID is the device UUID. all means try the key against any encrypted device. Use the specified KEY_STRING as passphrase.
    • --key NAME:file:FILENAME, --key UUID:file:FILENAME, --key all:file:FILENAME β€” read the passphrase from FILENAME.
    • --key NAME:clevis, --key UUID:clevis, --key all:clevis β€” attempt passphrase-less unlocking for the device with Clevis, over the network. Please refer to ENCRYPTED DISKS in guestfs(3) for more information on network-bound disk encryption (NBDE).

Note that if any such option is present on the command line, QEMU user networking will be automatically enabled for the libguestfs appliance.

  • --key NAME:error, --key UUID:error, --key all:error β€” any encrypted device matching will cause an error. --key all:error can be used to reject any disk image that contains an encrypted partition.
  • --keys-from-stdin β€” read key or passphrase parameters from stdin. The default is to try to read passphrases from the user by opening /dev/tty.

If there are multiple encrypted devices then you may need to supply multiple keys on stdin, one per line.

  • --machine-readable, --machine-readable=format β€” this option is used to make the output more machine friendly when being parsed by other programs. See MACHINE READABLE OUTPUT below.
  • -o option[,option,...] β€” pass -o option(s) to the qemu-img(1) command to fine-tune the output format. Options available depend on the output format (see --convert) and the installed version of the qemu-img program.

You should use -o at most once. To pass multiple options, separate them with commas, eg:

virt-sparsify --convert qcow2 \
  -o cluster_size=512,preallocation=metadata ...

You cannot use this option and --in-place together.

  • -q, --quiet β€” this disables progress bars and other unnecessary output.
  • --tmp block_device, --tmp dir β€” in copying mode only, use the named device or directory as the location of the temporary overlay (see also TMPDIR below).

If the parameter given is a block device, then the block device is written to directly. Note this erases the existing contents of the block device.

If the parameter is a directory, then this is the same as setting the TMPDIR environment variable.

You cannot use this option and --in-place together.

  • --tmp prebuilt:file β€” in copying mode only, the specialized option --tmp prebuilt:file (where prebuilt: is a literal string) causes virt-sparsify to use the qcow2 file as temporary space.
    • The file must be freshly formatted as qcow2, with indisk as the backing file.
    • If you rerun virt-sparsify, you must recreate the file before each run.
    • Virt-sparsify does not delete the file.

This option is used by oVirt which requires a specially formatted temporary file.

  • -v, --verbose β€” enable verbose messages for debugging.
  • -V, --version β€” display version number and exit.
  • --wrap β€” wrap error, warning, and informative messages. This is the default when the output is a tty. If the output of the program is redirected to a file, wrapping is disabled unless you use this option.
  • -x β€” enable tracing of libguestfs API calls.
  • --zero partition, --zero logvol β€” zero the contents of the named partition or logical volume in the guest. All data on the device is lost, but sparsification is excellent! You can give this option multiple times.

✨ IN-PLACE SPARSIFICATION ✨

Okay so this is the glow-up era of virt-sparsify, bestie. Since virt-sparsify β‰₯ 1.26, the tool can do in-place sparsification β€” meaning instead of copying from an input disk to an output disk, it just… edits your disk where it sits. Iconic. So much more efficient, no cap.

But listen, there's a tradeoff and I'm gonna be honest with you: it's not able to recover quite as much space as copying sparsification does. So it's giving "good enough, fast, no extra disk needed" not "maximum shrinkage." Manage your expectations accordingly πŸ’…

To use this mode, you specify a disk image which will be modified in place:

virt-sparsify --in-place disk.img
Heads up: that disk is getting modified in place β€” no output file, no do-over. So like, know what you're doing.

Some options are just not compatible with this mode, and it's not personal, they just need different vibes:

  • --convert, --compress and -o β€” incompatible because they require wholesale disk format changes
  • --check-tmpdir β€” incompatible because large amounts of temporary space are not required

In-place sparsification works using discard (a.k.a trim or unmap) support. That's the whole mechanism, bestie. That's the tea.


πŸ€– MACHINE READABLE OUTPUT πŸ€–

The --machine-readable option can be used to make the output more machine friendly, which is useful when calling virt-sparsify from other programs, GUIs etc. It's giving "let the robots read it."

There are two ways to use this option, and yes you need to know both.

Way 1: the solo vibe check πŸ‘‘

Firstly, use the option on its own to query the capabilities of the virt-sparsify binary. Typical output looks like this:

$ virt-sparsify --machine-readable
virt-sparsify
ntfs
btrfs

A list of features is printed, one per line, and the program exits with status 0. Clean, simple, no drama.

Way 2: the collab 🀝

Secondly, use the option in conjunction with other options to make the regular program output more machine friendly.

At the moment this means:

  1. Progress bar messages can be parsed from stdout by looking for this regular expression:
^[0-9]+/[0-9]+$
  1. The calling program should treat messages sent to stdout (except for progress bar messages) as status messages. They can be logged and/or displayed to the user.
  2. The calling program should treat messages sent to stderr as error messages. In addition, virt-sparsify exits with a non-zero status code if there was a fatal error.

All versions of virt-sparsify have supported the --machine-readable option. Ancient. Historic. A legend, fr fr.

It is possible to specify a format string for controlling the output; see "ADVANCED MACHINE READABLE OUTPUT" in guestfs(3).


πŸͺŸ WINDOWS 8 πŸͺŸ

Not me having to warn you about Windows 8 in the year of our lord. 😭 Windows 8 "fast startup" can prevent virt-sparsify from working. See "WINDOWS HIBERNATION AND WINDOWS 8 FAST STARTUP" in guestfs(3).


🌸 ENVIRONMENT VARIABLES 🌸

TMPDIR

Location of the temporary directory used for the potentially large temporary overlay file. (Potentially large. Emphasis on potentially. Plan accordingly.)

In virt-sparsify β‰₯ 1.28, you can override this environment variable using the --tmp option. Slay.

You should ensure there is enough free space in the worst case for a full copy of the source disk (virtual size), or else set $TMPDIR to point to another directory that has enough space. Like, actually do this. Don't be the person who fills up /tmp and then messages me at 2am. πŸ™ˆ

This defaults to /tmp.

🚨 Real talk, this one matters: note that if $TMPDIR is a tmpfs (eg. if /tmp is on tmpfs, or if you use "TMPDIR=/dev/shm"), tmpfs defaults to a maximum size of half of physical RAM. If virt-sparsify exceeds this, it will hang. Not error out. Hang. Just vibes and silence forever.

The solution is either to use a real disk, or to increase the maximum size of the tmpfs mountpoint, eg:

mount -o remount,size=10G /tmp

If you are using the --in-place option, then large amounts of temporary space are not required. Bestie said "don't worry about it" and meant it. πŸ’–

For other environment variables, see "ENVIRONMENT VARIABLES" in guestfs(3).


πŸšͺ EXIT STATUS πŸšͺ

This program returns 0 if the operation completed without errors. (This doesn't necessarily mean that space could be freed up.) Read that parenthetical twice, okay? Zero means "it worked," not "your disk got smaller." 🫢

A non-zero exit code indicates an error.

If the exit code is 3 and the --in-place option was used, that indicates that discard support is not available in libguestfs, so copying mode must be used instead. It's giving "wrong tool, bestie, go the long way round."


πŸ‘€ SEE ALSO πŸ‘€

virt-df(1), virt-filesystems(1), virt-resize(1), virt-rescue(1), guestfs(3), guestfish(1), truncate(1), fallocate(1), qemu-img(1), http://libguestfs.org/.

The whole friend group showed up. Periodt.


✍️ AUTHOR ✍️

Richard W.M. Jones http://people.redhat.com/~rjones/

Legend behaviour. We stan. πŸ‘‘


Copyright (C) 2011-2025 Red Hat Inc.


πŸ“œ LICENSE πŸ“œ

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

(Yes I kept all the legal caps-lock. You're welcome. That's the one place yelling is correct.)


πŸ› BUGS πŸ›

To get a list of bugs against libguestfs, use this link:

https://bugzilla.redhat.com/buglist.cgi?component=libguestfs&product=Virtualization+Tools

To report a new bug against libguestfs, use this link:

https://bugzilla.redhat.com/enter_bug.cgi?component=libguestfs&product=Virtualization+Tools

When reporting a bug, please supply β€” and bestie, please actually supply all of these, this is how you get your bug fixed fast:

  • The version of libguestfs.
  • Where you got libguestfs (eg. which Linux distro, compiled from source, etc)
  • Describe the bug accurately and give a way to reproduce it.
  • Run libguestfs-test-tool(1) and paste the complete, unedited output into the bug report.
🚨 "complete, unedited output" β€” do not trim it, do not paraphrase it, do not give me the highlight reel. The unedited part is doing a lot of heavy lifting here.

Okay that's the whole page, you're free πŸ¦‹ This one's from guestfs-tools 1.57.3, dated 2026-09-16, and it's manual page virt-sparsify(1) β€” go forth and free up some space, bestie. βœ¨πŸ’œ

πŸ“¦ where this page comes from