Sparse Files in Linux


Thanks for all your LOVE and TRUST that we are able to win the “BEST REDHAT CERTIFIED TRAINING PARTNER AWARD-2012“.

That makes winning 5 in a row !!! WOW !!!

So lets learn something about sparse files.

What is a sparse file?

As per 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. 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.

A very common example is when you create Virtual Machines in your Linux box using either KVM (Must have learned about Virtualization with KVM in your RHCE Training) or VirtualBox or VMWare Client. You create files for those VM’s. Those are actually “sparse files”.

As I had mentioned, sparse files can be useful in situations where the full disk may never be completely used. One such situation would be virtual machines. If a virtual machine never fills the disk entirely, then a certain amount of the disk will never have anything but zeros in it – permitting the saving of disk space by using a sparse file.

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. This also reduces the time of the first write as the system doesn’t have to allocate blocks for the “skipped” space.

Here is the illustration showing the sparse files created by the VirtualBox installed on my machine to handle the VM’s. Use -s switch to view the sparse status with “ls” command.

Sparse Files in Linux

Sparse Files in Linux