Clear unused space with zeros (ext3,ext4)
Such an utility is zerofree
.
From its description:
Zerofree finds the unallocated, non-zeroed blocks in an ext2 or ext3 file-system and fills them with zeroes. This is useful if the device on which this file-system resides is a disk image. In this case, depending on the type of disk image, a secondary utility may be able to reduce the size of the disk image after zerofree has been run. Zerofree requires the file-system to be unmounted or mounted read-only.
The usual way to achieve the same result (zeroing the unused blocks) is to run "dd" do create a file full of zeroes that takes up the entire free space on the drive, and then delete this file. This has many disadvantages, which zerofree alleviates:
- it is slow
- it makes the disk image (temporarily) grow to its maximal extent
- it (temporarily) uses all free space on the disk, so other concurrent write actions may fail.
Zerofree has been written to be run from GNU/Linux systems installed as guest OSes inside a virtual machine. If this is not your case, you almost certainly don't need this package.
UPDATE #1
The description of the .deb package contains the following paragraph now which would imply this will work fine with ext4 too.
Description: zero free blocks from ext2, ext3 and ext4 file-systems Zerofree finds the unallocated blocks with non-zero value content in an ext2, ext3 or ext4 file-system and fills them with zeroes...
Other uses
Another application this utility is to compress disk images that are a backup of a real disk. A typical example of this is the dump of the SD card in a BeagleBone or a Raspberry Pi. Once empty spaces have been zeroed, backup images can be compressed more efficiently.
Summary of the methods (as mentioned in this question and elsewhere) to clear unused space on ext2/ext3/ext4:
Zeroing unused space
File system is not mounted
- If the "disk" your filesystem is on is thin provisioned (e.g. a modern SSD supporting TRIM, a VM file whose format supports sparseness etc.) and your kernel says the block device understands it, you can use
e2fsck -E discard src_fs
to discard unused space (requires e2fsprogs 1.42.2 or higher). - Using zerofree (e.g.
zerofree src_fs
) to explicitly write zeros over unused blocks. - Using
e2image -rap src_fs dest_fs
to only copy blocks in use (new filesystem should be on an otherwise zeroed "disk", requires e2fsprogs 1.42.9 or higher).
File system is mounted
- If the "disk" your filesystem is on is thin provisioned (e.g. a modern SSD supporting TRIM, a VM file whose format supports sparseness etc.), your kernel says the block device understands it and finally the ext filesystem driver supports it you can use
fstrim /mnt/fs/
to ask the filesystem to discard unused space. - Using
cat /dev/zero > /mnt/fs/zeros; sync; rm /mnt/fs/zeros
(sfill
from secure-delete uses this technique). This method is inefficient, not recommended by Ted Ts'o (author of ext4), may not zero certain things and can slow down futurefsck
s.
Having the filesystem unmounted will give better results than having it mounted. Discarding tends to be the fastest method when a lot of previously used space needs to be zeroed but using zerofree
after the discard process can sometimes zero a little bit extra (depending on how discard is implemented on the "disk").
Making the image file smaller
Image is in a dedicated VM format
You will need to use an appropriate disk image tool (such as qemu-img convert src_image dst_image
) to enable the zeroed space to be reclaimed and to allow the file representing the image to become smaller.
Image is a raw file
One of the following techniques can be used to make the file sparse (so runs of zero stop taking up space):
cp --sparse=always src_image dst_image
.fallocate -d src_image
(requires util-linux v2.25 or higher).
These days it might easier to use a tool like virt-sparsify to do these steps and more in one go.
Sources
- Clear unused space with zeros (ext3,ext4) (this Unix & Linux question!)
- https://ext4.wiki.kernel.org/index.php/Ext4_VM_Images
- Post by Ted Ts'o discussing image reduction techniques in the "kernel bug at fs/ext4/resize.c:409" linux-ext4 thread.
- Keeping filesystem images sparse by the author of
zerofree
- free up not used space on a qcow2-image-file on kvm/qemu answer on Server Fault
- http://libguestfs.org/virt-sparsify.1.html
sfill
from secure-delete can do this and several other related jobs.
e.g.
sfill -l -l -z /mnt/X
UPDATE #1
There is a source tree that appears to be used by the ArchLinux project on github that contains the source for sfill
which is a tool included in the package Secure-Delete.
- https://github.com/BlackArch/secure-delete
Also a copy of sfill
's man page is here:
- http://manpages.ubuntu.com/manpages/xenial/man1/sfill.1.html