Sparse Files

What is a sparse file?

According to Wikipedia,in computer science, a sparse file is a type of computer file that attempts to use file system space more efficiently when blocks allocated to the file are mostly empty. This is achieved by writing brief information (metadata) representing the empty blocks to disk instead of the actual "empty" space which makes up the block, using less disk space (i.e. sparse files contain blocks of zeros whose existance is recorded, but have no space allocated on disk). The full block size is written to disk as the actual size only when the block contains "real" (non-empty) data.

When reading sparse files, the file system transparently converts metadata representing empty blocks into "real" blocks filled with zero bytes at runtime. The application is unaware of this conversion.

Most modern file systems support sparse files, including most Unix variants and NTFS, but notably not Apple's HFS+. Sparse files are commonly used for disk images, database snapshots, log files and in scientific applications.

Advantages

The advantage of sparse files is that storage is only allocated when actually needed: disk space is saved, and large files can be created even if there is insufficient free space on the file system.

Disadvantages

Disadvantages are that sparse files may become fragmented; file system free space reports may be misleading; filling up file systems containing sparse files can have unexpected effects; and copying a sparse file with a program that does not explicitly support them may copy the entire, uncompressed size of the file, including the sparse, mostly zero sections which are not on disk -- losing the benefits of the sparse property in the file.

Creating sparse files in Linux/Unix

Sparse files have different apparent file sizes (the maximum size to which they may expand) and actual file sizes (how large the file "actually" is on the disk). To check the file's apparent size, just run:

# du -h --apparent-size file.img
512M file.img

and, to check the actual size of the file on your disk:

# du -h file.img
0 file.img

As you can see, although the apparent size of the file is 512 Mb, its "actual" size is really 0 B -- that's because due to the nature and beauty of sparse files, it will "expand" arbitrarily to minimize the space required to store its contents.

Formatting the file with a filesystem

Now that we've created the sparse file, it's time to format it with a filesystem; for this example, I will use ReiserFS:

As you may have expected, formatting it with the filesystem has increased its actual size, but left its apparent size the same. Now we can create a directory which we will use to mount our file:

# mkdir folder
# mount -o loop file.img folder

Tada! We now have both a file and a folder into which we may store almost 512 Mb worth of information!

Mounting the file at boot

Having created a sparse file, you may wish to mount it automatically at boot; the best way I can suggest to do this is to add a simple entry to your `/etc/fstab', as follows:

/path/to/file.img /path/to/folder reiserfs loop,defaults 0 0

Warning: Be sure to include the `loop' option -- otherwise, it will not mount!!!

Copying the sparse file

Copying with `cp'

Normally, `cp' is good at detecting whether a file is sparse, so it suffices to run:

cp file.img new_file.img

...and new_file.img will be sparse. However, cp does have a --sparse=WHEN option. This is especially useful if a sparse-file has somehow become non-sparse (i.e. the empty blocks have been written out to disk in full). Disk space can be recovered by doing:

cp --sparse=always new_file.img recovered_file.img

Archiving with `tar'

One day, you may decide to back up your well-loved sparse file, and choose the `tar' utility for that very purpose; however, you soon realize you have a problem:

Apparently, even though the current size of the sparse file is only 33 MB, compressing it with tar created an archive of the ENTIRE SIZE OF THE FILE! Luckily for you, though, tar has a `--sparse' (`-S') flag, that when used in conjunction with the `--create' (`-c') operation, tests all files for sparseness while archiving. If tar finds a file to be sparse, it uses a sparse representation of the file in the archive. This is useful when archiving files, such as dbm files, likely to contain many nulls, and dramatically decreases the amount of space needed to store such an archive.

# tar -Scf file.tar.gz file.img
# du -h file.tar.gz
12K file.tar.gz

Problem solved.

Resizing the sparse file

Before we resize the file, let's populate it with a couple small files for testing purposes: