Sparse Files

by Stephen Fluin 2010.01.17

Whether or not Sparse Files have a practical purpose, they are a very interesting piece of computer science. Sparse Files allow you to create a file on the filesystem that appears to be one size, but is actually created empty. You can create one with the following line:

dd if=/dev/zero of=disk.ext3 bs=1 count=0 seek=4G

How it works is that it creates a file that the filesystem will report as being 4 Gigabytes, but until something is written to the file, it takes up virtually no space on the hard disk. As you write to this file, space on the hard disk is allocated and associated with the file, so the contents are always readable.

But what could it be good for?

One use of a sparse file is that you could create a sparse file, then mount it as a filesystem. The filesystem will have the full capability of the max size of the file, but it will only use up real space on the disk as you fill the filesystem. This may not be practical, but it is very interesting from a capabilities perspective.

I'm not sure if this is exactly the same implementation, but an identical concept is used by VirtualBox and other virtualization systems. They call it "Dynamically Expanding Hard Drives", but they follow the same concept of a file on disk that is capable of representing something much larger, but doesn't take up the space until it is used.


permalink