Keeping filesystem images sparse (obsolete)

This page contains obsolete and deprecated information. It is only made available for historical reasons.

Rather than zeroing freed blocks manually it might be better to have the guest kernel zero blocks when they're freed. My original inspiration was the ext2fs privacy (i.e. secure deletion) patch described in a Linux kernel mailing list thread. I've also made use of a later patch for ext3 entitled Secure Deletion Functionality in ext3 from the linux-fsdevel mailing list. (See also the authors' paper on Secure Deletion File Systems.) I've modified the patches to make them more suitable for the present purpose.

When a filesystem is mounted with the zerofree option (added by these patches) all the blocks freed when a file is deleted are filled with zeroes. Remember, this extra work will hurt disk performance. Note that the ext3 patch doesn't support data journalling mode, so deleted metadata isn't zeroed. It also hasn't been tested as thoroughly as the patch for ext2. And neither has been maintained for a very long time.

As an alternative method of making files sparse I wrote a utility which can make any specified files on an ext2 filesystem sparse, sparsify.c. This doesn't require any additional disk space to work its magic, but it does require that the filesystem containing the filesystem image is unmounted, which is just a different sort of inconvenience.

As an example, suppose we have an unmounted filesystem image, fs.image, in the directory /data, which is the root of the /dev/hda2 filesystem. We can reclaim deleted blocks and make it sparse like so:

   zerofree /data/fs.image
   umount /data
   sparsify /dev/hda2 /fs.image
   mount /data

Better alternatives to sparsify exist and it should not be used.


Ron Yorston
4th January 2017