.\" -*- mode: troff; coding: utf-8 -*-
.\" Automatically generated by Podwrapper::Man 1.55.3 (Pod::Simple 3.47)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
.ie n \{\
.    ds C` ""
.    ds C' ""
'br\}
.el\{\
.    ds C`
.    ds C'
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el       .ds Aq '
.\"
.\" If the F register is >0, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD.  Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.\"
.\" Avoid warning from groff about undefined register 'F'.
.de IX
..
.nr rF 0
.if \n(.g .if rF .nr rF 1
.if (\n(rF:(\n(.g==0)) \{\
.    if \nF \{\
.        de IX
.        tm Index:\\$1\t\\n%\t"\\$2"
..
.        if !\nF==2 \{\
.            nr % 0
.            nr F 2
.        \}
.    \}
.\}
.rr rF
.\"
.\" Required to disable full justification in groff 1.23.0.
.if n .ds AD l
.\" ========================================================================
.\"
.IX Title "virt-sparsify 1"
.TH virt-sparsify 1 2025-11-04 guestfs-tools-1.55.3 "Virtualization Support"
.\" For nroff, turn off justification.  Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH NAME
virt\-sparsify \- Make a virtual machine disk sparse
.SH SYNOPSIS
.IX Header "SYNOPSIS"
.Vb 1
\& virt\-sparsify [\-\-options] indisk outdisk
\&
\& virt\-sparsify [\-\-options] \-\-in\-place disk
.Ve
.SH WARNING
.IX Header "WARNING"
Using \f(CW\*(C`virt\-sparsify\*(C'\fR
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.
.SH DESCRIPTION
.IX Header "DESCRIPTION"
Virt\-sparsify is a tool which can make a virtual machine disk (or any
disk image) sparse a.k.a. thin\-provisioned.  This means that free
space within the disk image can be converted back to free space on the
host.
.PP
Virt\-sparsify can locate and sparsify free space in most filesystems
(eg. ext2/3/4, btrfs, NTFS, etc.), and also in LVM physical volumes.
.PP
Virt\-sparsify can also convert between some disk formats, for example
converting a raw disk image to a thin\-provisioned qcow2 image.
.PP
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).
.SS "IMPORTANT NOTE ABOUT SPARSE OUTPUT IMAGES"
.IX Subsection "IMPORTANT NOTE ABOUT SPARSE OUTPUT IMAGES"
If the input is raw, then the default output is raw sparse.  \fBYou
must check the output size using a tool that understands sparseness\fR
such as \f(CW\*(C`du \-sh\*(C'\fR.  It can make a huge difference:
.PP
.Vb 4
\& $ 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
.Ve
.PP
(Compare the apparent size \fB100M\fR vs the actual size \fB3.6M\fR)
.SS "IMPORTANT LIMITATIONS"
.IX Subsection "IMPORTANT LIMITATIONS"
.IP \(bu 4
The virtual machine \fImust be shut down\fR before using this tool.
.IP \(bu 4
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.
.Sp
If you are using the \fI\-\-in\-place\fR option, then large amounts of
temporary space are \fBnot\fR required.
.IP \(bu 4
Virt\-sparsify cannot resize disk images.  To do that, use
\&\fBvirt\-resize\fR\|(1).
.IP \(bu 4
Virt\-sparsify cannot handle encrypted disks.  Libguestfs supports
encrypted disks, but encrypted disks themselves cannot be sparsified.
.IP \(bu 4
Virt\-sparsify cannot yet sparsify the space between partitions.  Note
that this space is often used for critical items like bootloaders so
it\*(Aqs not really unused.
.IP \(bu 4
In copy mode, qcow2 internal snapshots are not copied over to the
destination image.
.PP
You may also want to read the manual pages for the associated tools
\&\fBvirt\-filesystems\fR\|(1) and \fBvirt\-df\fR\|(1) before starting.
.SH EXAMPLES
.IX Header "EXAMPLES"
Typical usage is:
.PP
.Vb 1
\& virt\-sparsify indisk outdisk
.Ve
.PP
which copies \f(CW\*(C`indisk\*(C'\fR to \f(CW\*(C`outdisk\*(C'\fR, making the output sparse.
\&\f(CW\*(C`outdisk\*(C'\fR 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.
.PP
To convert between formats, use the \fI\-\-convert\fR option:
.PP
.Vb 1
\& virt\-sparsify disk.raw \-\-convert qcow2 disk.qcow2
.Ve
.PP
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\*(Aqt zero free space on) certain filesystems by doing:
.PP
.Vb 1
\& virt\-sparsify \-\-ignore /dev/sda1 indisk outdisk
.Ve
.PP
See \fBvirt\-filesystems\fR\|(1) to get a list of filesystems within a disk
image.
.PP
Since virt\-sparsify ≥ 1.26, you can now sparsify a disk image
in place by doing:
.PP
.Vb 1
\& virt\-sparsify \-\-in\-place disk.img
.Ve
.SH OPTIONS
.IX Header "OPTIONS"
.IP \fB\-\-help\fR 4
.IX Item "--help"
Display help.
.IP "\fB\-\-check\-tmpdir\fR \fBignore\fR" 4
.IX Item "--check-tmpdir ignore"
.PD 0
.IP "\fB\-\-check\-tmpdir\fR \fBcontinue\fR" 4
.IX Item "--check-tmpdir continue"
.IP "\fB\-\-check\-tmpdir\fR \fBwarn\fR" 4
.IX Item "--check-tmpdir warn"
.IP "\fB\-\-check\-tmpdir\fR \fBfail\fR" 4
.IX Item "--check-tmpdir fail"
.PD
Check if "TMPDIR" or \fI\-\-tmp\fR directory has enough space to complete
the operation.  This is just an estimate.
.Sp
If the check indicates a problem, then you can either:
.RS 4
.IP \(bu 4
\&\fBignore\fR it,
.IP \(bu 4
print a warning and \fBcontinue\fR,
.IP \(bu 4
\&\fBwarn\fR and wait for the user to press the Return key
(this is the default), or:
.IP \(bu 4
\&\fBfail\fR and exit.
.RE
.RS 4
.Sp
You cannot use this option and \fI\-\-in\-place\fR together.
.RE
.IP \fB\-\-colors\fR 4
.IX Item "--colors"
.PD 0
.IP \fB\-\-colours\fR 4
.IX Item "--colours"
.PD
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.
.IP \fB\-\-compress\fR 4
.IX Item "--compress"
Compress the output file.  This \fIonly\fR works if the output format is
\&\f(CW\*(C`qcow2\*(C'\fR.
.Sp
You cannot use this option and \fI\-\-in\-place\fR together.
.IP "\fB\-\-convert\fR raw" 4
.IX Item "--convert raw"
.PD 0
.IP "\fB\-\-convert\fR qcow2" 4
.IX Item "--convert qcow2"
.IP "\fB\-\-convert\fR [other formats]" 4
.IX Item "--convert [other formats]"
.PD
Use \f(CW\*(C`output\-format\*(C'\fR as the format for the destination image.  If this
is not specified, then the input format is used.
.Sp
Supported and known\-working output formats are: \f(CW\*(C`raw\*(C'\fR, \f(CW\*(C`qcow2\*(C'\fR, \f(CW\*(C`vdi\*(C'\fR.
.Sp
You can also use any format supported by the \fBqemu\-img\fR\|(1) program,
eg. \f(CW\*(C`vmdk\*(C'\fR, but support for other formats is reliant on qemu.
.Sp
Specifying the \fI\-\-convert\fR option is usually a good idea, because
then virt\-sparsify doesn\*(Aqt need to try to guess the input format.
.Sp
For fine\-tuning the output format, see: \fI\-\-compress\fR, \fI\-o\fR.
.Sp
You cannot use this option and \fI\-\-in\-place\fR together.
.IP \fB\-\-echo\-keys\fR 4
.IX Item "--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.
.IP "\fB\-\-format\fR raw" 4
.IX Item "--format raw"
.PD 0
.IP "\fB\-\-format\fR qcow2" 4
.IX Item "--format qcow2"
.PD
Specify the format of the input disk image.  If this flag is not
given then it is auto\-detected from the image itself.
.Sp
If working with untrusted raw\-format guest disk images, you should
ensure the format is always specified.
.IP "\fB\-\-ignore\fR filesystem" 4
.IX Item "--ignore filesystem"
.PD 0
.IP "\fB\-\-ignore\fR volgroup" 4
.IX Item "--ignore volgroup"
.PD
Ignore the named filesystem.
.Sp
When not using \fI\-\-in\-place\fR: Free space on the filesystem will not be
zeroed, but existing blocks of zeroes will still be sparsified.
.Sp
When using \fI\-\-in\-place\fR, the filesystem is ignored completely.
.Sp
In the second form, this ignores the named volume group.  Use the
volume group name without the \fI/dev/\fR prefix, eg. \fI\-\-ignore vg_foo\fR
.Sp
You can give this option multiple times.
.IP \fB\-\-in\-place\fR 4
.IX Item "--in-place"
Do in\-place sparsification instead of copying sparsification.
See "IN\-PLACE SPARSIFICATION" below.
.IP "\fB\-\-key\fR SELECTOR" 4
.IX Item "--key SELECTOR"
Specify a key for LUKS, to automatically open a LUKS device when using
the inspection.
.RS 4
.IP "\fB\-\-key\fR NAME\fB:key:\fRKEY_STRING" 4
.IX Item "--key NAME:key:KEY_STRING"
.PD 0
.IP "\fB\-\-key\fR UUID\fB:key:\fRKEY_STRING" 4
.IX Item "--key UUID:key:KEY_STRING"
.IP "\fB\-\-key\fR \fBall:key:\fRKEY_STRING" 4
.IX Item "--key all:key:KEY_STRING"
.PD
\&\f(CW\*(C`NAME\*(C'\fR is the libguestfs device name (eg. \f(CW\*(C`/dev/sda1\*(C'\fR).  \f(CW\*(C`UUID\*(C'\fR is
the device UUID.  \f(CW\*(C`all\*(C'\fR means try the key against any encrypted
device.
.Sp
Use the specified \f(CW\*(C`KEY_STRING\*(C'\fR as passphrase.
.IP "\fB\-\-key\fR NAME\fB:file:\fRFILENAME" 4
.IX Item "--key NAME:file:FILENAME"
.PD 0
.IP "\fB\-\-key\fR UUID\fB:file:\fRFILENAME" 4
.IX Item "--key UUID:file:FILENAME"
.IP "\fB\-\-key\fR \fBall:file:\fRFILENAME" 4
.IX Item "--key all:file:FILENAME"
.PD
Read the passphrase from \fIFILENAME\fR.
.IP "\fB\-\-key\fR NAME\fB:clevis\fR" 4
.IX Item "--key NAME:clevis"
.PD 0
.IP "\fB\-\-key\fR UUID\fB:clevis\fR" 4
.IX Item "--key UUID:clevis"
.IP "\fB\-\-key\fR \fBall:clevis\fR" 4
.IX Item "--key all:clevis"
.PD
Attempt passphrase\-less unlocking for the device with Clevis, over the
network.  Please refer to "ENCRYPTED DISKS" in \fBguestfs\fR\|(3) for more
information on network\-bound disk encryption (NBDE).
.Sp
Note that if any such option is present on the command line, QEMU user
networking will be automatically enabled for the libguestfs appliance.
.RE
.RS 4
.RE
.IP \fB\-\-keys\-from\-stdin\fR 4
.IX Item "--keys-from-stdin"
Read key or passphrase parameters from stdin.  The default is
to try to read passphrases from the user by opening \fI/dev/tty\fR.
.Sp
If there are multiple encrypted devices then you may need to supply
multiple keys on stdin, one per line.
.IP \fB\-\-machine\-readable\fR 4
.IX Item "--machine-readable"
.PD 0
.IP \fB\-\-machine\-readable\fR=format 4
.IX Item "--machine-readable=format"
.PD
This option is used to make the output more machine friendly
when being parsed by other programs.  See
"MACHINE READABLE OUTPUT" below.
.IP "\fB\-o\fR option[,option,...]" 4
.IX Item "-o option[,option,...]"
Pass \fI\-o\fR option(s) to the \fBqemu\-img\fR\|(1) command to fine\-tune the
output format.  Options available depend on the output format (see
\&\fI\-\-convert\fR) and the installed version of the qemu\-img program.
.Sp
You should use \fI\-o\fR at most once.  To pass multiple options, separate
them with commas, eg:
.Sp
.Vb 2
\& virt\-sparsify \-\-convert qcow2 \e
\&   \-o cluster_size=512,preallocation=metadata ...
.Ve
.Sp
You cannot use this option and \fI\-\-in\-place\fR together.
.IP \fB\-q\fR 4
.IX Item "-q"
.PD 0
.IP \fB\-\-quiet\fR 4
.IX Item "--quiet"
.PD
This disables progress bars and other unnecessary output.
.IP "\fB\-\-tmp\fR block_device" 4
.IX Item "--tmp block_device"
.PD 0
.IP "\fB\-\-tmp\fR dir" 4
.IX Item "--tmp dir"
.PD
In copying mode only, use the named device or directory as the
location of the temporary overlay (see also "TMPDIR" below).
.Sp
If the parameter given is a block device, then the block device is
written to directly.
\&\fBNote this erases the existing contents of the block device\fR.
.Sp
If the parameter is a directory, then this is the same as setting the
"TMPDIR" environment variable.
.Sp
You cannot use this option and \fI\-\-in\-place\fR together.
.IP "\fB\-\-tmp\fR prebuilt:file" 4
.IX Item "--tmp prebuilt:file"
In copying mode only, the specialized option \fI\-\-tmp prebuilt:file\fR
(where \f(CW\*(C`prebuilt:\*(C'\fR is a literal string) causes virt\-sparsify to use
the qcow2 \f(CW\*(C`file\*(C'\fR as temporary space.
.RS 4
.IP \(bu 4
The file \fBmust\fR be freshly formatted as qcow2, with indisk as the
backing file.
.IP \(bu 4
If you rerun virt\-sparsify, you \fBmust\fR recreate the file before
each run.
.IP \(bu 4
Virt\-sparsify does not delete the file.
.RE
.RS 4
.Sp
This option is used by oVirt which requires a specially formatted
temporary file.
.RE
.IP \fB\-v\fR 4
.IX Item "-v"
.PD 0
.IP \fB\-\-verbose\fR 4
.IX Item "--verbose"
.PD
Enable verbose messages for debugging.
.IP \fB\-V\fR 4
.IX Item "-V"
.PD 0
.IP \fB\-\-version\fR 4
.IX Item "--version"
.PD
Display version number and exit.
.IP \fB\-\-wrap\fR 4
.IX Item "--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.
.IP \fB\-x\fR 4
.IX Item "-x"
Enable tracing of libguestfs API calls.
.IP "\fB\-\-zero\fR partition" 4
.IX Item "--zero partition"
.PD 0
.IP "\fB\-\-zero\fR logvol" 4
.IX Item "--zero logvol"
.PD
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.
.SH "IN\-PLACE SPARSIFICATION"
.IX Header "IN-PLACE SPARSIFICATION"
Since virt\-sparsify ≥ 1.26, the tool is able to do in\-place
sparsification (instead of copying from an input disk to an output
disk).  This is more efficient.  It is not able to recover quite as
much space as copying sparsification.
.PP
To use this mode, specify a disk image which will be modified in
place:
.PP
.Vb 1
\& virt\-sparsify \-\-in\-place disk.img
.Ve
.PP
Some options are not compatible with this mode: \fI\-\-convert\fR,
\&\fI\-\-compress\fR and \fI\-o\fR because they require wholesale disk format
changes; \fI\-\-check\-tmpdir\fR because large amounts of temporary space
are not required.
.PP
In\-place sparsification works using discard (a.k.a trim or unmap)
support.
.SH "MACHINE READABLE OUTPUT"
.IX Header "MACHINE READABLE OUTPUT"
The \fI\-\-machine\-readable\fR option can be used to make the output more
machine friendly, which is useful when calling virt\-sparsify from
other programs, GUIs etc.
.PP
There are two ways to use this option.
.PP
Firstly use the option on its own to query the capabilities of the
virt\-sparsify binary.  Typical output looks like this:
.PP
.Vb 4
\& $ virt\-sparsify \-\-machine\-readable
\& virt\-sparsify
\& ntfs
\& btrfs
.Ve
.PP
A list of features is printed, one per line, and the program exits
with status 0.
.PP
Secondly use the option in conjunction with other options to make the
regular program output more machine friendly.
.PP
At the moment this means:
.IP 1. 4
Progress bar messages can be parsed from stdout by looking for this
regular expression:
.Sp
.Vb 1
\& ^[0\-9]+/[0\-9]+$
.Ve
.IP 2. 4
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.
.IP 3. 4
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.
.PP
All versions of virt\-sparsify have supported the \fI\-\-machine\-readable\fR
option.
.PP
It is possible to specify a format string for controlling the output;
see "ADVANCED MACHINE READABLE OUTPUT" in \fBguestfs\fR\|(3).
.SH "WINDOWS 8"
.IX Header "WINDOWS 8"
Windows 8 "fast startup" can prevent virt\-sparsify from working.
See "WINDOWS HIBERNATION AND WINDOWS 8 FAST STARTUP" in \fBguestfs\fR\|(3).
.SH "ENVIRONMENT VARIABLES"
.IX Header "ENVIRONMENT VARIABLES"
.IP TMPDIR 4
.IX Item "TMPDIR"
Location of the temporary directory used for the potentially large
temporary overlay file.
.Sp
In virt\-sparsify ≥ 1.28, you can override this environment
variable using the \fI\-\-tmp\fR option.
.Sp
You should ensure there is enough free space in the worst case for a
full copy of the source disk (\fIvirtual\fR size), or else set \f(CW$TMPDIR\fR
to point to another directory that has enough space.
.Sp
This defaults to \fI/tmp\fR.
.Sp
Note that if \f(CW$TMPDIR\fR is a tmpfs (eg. if \fI/tmp\fR is on tmpfs, or if
you use \f(CW\*(C`TMPDIR=/dev/shm\*(C'\fR), tmpfs defaults to a maximum size of
\&\fIhalf\fR of physical RAM.  If virt\-sparsify exceeds this, it will hang.
The solution is either to use a real disk, or to increase the maximum
size of the tmpfs mountpoint, eg:
.Sp
.Vb 1
\& mount \-o remount,size=10G /tmp
.Ve
.Sp
If you are using the \fI\-\-in\-place\fR option, then large amounts of
temporary space are \fBnot\fR required.
.PP
For other environment variables, see "ENVIRONMENT VARIABLES" in \fBguestfs\fR\|(3).
.SH "EXIT STATUS"
.IX Header "EXIT STATUS"
This program returns 0 if the operation completed without errors.
(This doesn\*(Aqt necessarily mean that space could be freed up.)
.PP
A non\-zero exit code indicates an error.
.PP
If the exit code is \f(CW3\fR and the \fI\-\-in\-place\fR option was used, that
indicates that discard support is not available in libguestfs, so
copying mode must be used instead.
.SH "SEE ALSO"
.IX Header "SEE ALSO"
\&\fBvirt\-df\fR\|(1),
\&\fBvirt\-filesystems\fR\|(1),
\&\fBvirt\-resize\fR\|(1),
\&\fBvirt\-rescue\fR\|(1),
\&\fBguestfs\fR\|(3),
\&\fBguestfish\fR\|(1),
\&\fBtruncate\fR\|(1),
\&\fBfallocate\fR\|(1),
\&\fBqemu\-img\fR\|(1),
http://libguestfs.org/.
.SH AUTHOR
.IX Header "AUTHOR"
Richard W.M. Jones http://people.redhat.com/~rjones/
.SH COPYRIGHT
.IX Header "COPYRIGHT"
Copyright (C) 2011\-2025 Red Hat Inc.
.SH LICENSE
.IX Header "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.
.PP
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.
.PP
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.
.SH BUGS
.IX Header "BUGS"
To get a list of bugs against libguestfs, use this link:
https://bugzilla.redhat.com/buglist.cgi?component=libguestfs&product=Virtualization+Tools
.PP
To report a new bug against libguestfs, use this link:
https://bugzilla.redhat.com/enter_bug.cgi?component=libguestfs&product=Virtualization+Tools
.PP
When reporting a bug, please supply:
.IP \(bu 4
The version of libguestfs.
.IP \(bu 4
Where you got libguestfs (eg. which Linux distro, compiled from source, etc)
.IP \(bu 4
Describe the bug accurately and give a way to reproduce it.
.IP \(bu 4
Run \fBlibguestfs\-test\-tool\fR\|(1) and paste the \fBcomplete, unedited\fR
output into the bug report.
