Save disk space on Linux by cloning files on Btrfs and OCFS2
By Lenz Grimmer on Aug 31, 2011
Btrfs and OCFS2 are two very advanced file systems for Linux. Btrfs is a next-generation local file system for Linux, and it provides a number of nice features like snapshots and subvolumes, dynamic resizing and built-in RAID functionality. OCFS2 is the ideal candidate for creating cluster file systems that can be shared across multiple machines (but it can also be used for local storage).
There is one neat little feature that both Btrfs and OCFS2 have in common — they are capable of creating "lightweight" copies ("snapshots" or "clones") of a file.
In this case the file system does not create a new link pointing to an existing inode, it rather creates a new inode that shares the same disk blocks as the original file. This means that this operation only works within the boundaries of the same file system or subvolume. The outcome looks very much like a copy of the source file, but the actual data blocks have not been duplicated. Due to the copy-on-write nature, a modification of any one of the files will not be visible in the other file. Note that this should not be confused with hard links – this web page provides a good explanation of the differences.
For Btrfs, you can invoke this feature by using the cp(1) utility with the --reflink option, which was added to the GNU coreutils in version 7.5 (released in Aug. 2009):
cp --reflink <source file> <destination file>
Adding support for the reflink implementation of OCFS2 to cp still seems to be under development. For now, you need to download and install a separate reflink binary from here. It works like the ln(1) utility:
reflink <source file> <destination file>
These kind of file clones save disk space and allow copy operations to perform much quicker than actually copying entire files. This can be quite useful if you need to create copies of very large files that differ very little from each other, e.g. virtual machine disk images. In this case the disk space savings can be quite significant!