Btrfs – subvolumes, snapshots, quotas, and more 2 – Creating and Managing Single-Instance Filesystems

Share this post on:

Note

Automatic snapshots can be enabled so a dnf transaction will create a snapshot. This is done by the dnf-plugin-snapper tool. More information can be found here: https://docs.oracle.com/en/operating-systems/oracle-linux/8/fsadmin/fsadmin-ManagingtheBtrfsFileSystem.html#snapper-btrfs.

This section will cover how to enable compression using the Btrfs filesystem. Btrfs compression is a powerful feature that allows you to compress data in real time while writing it to the filesystem. This feature can significantly reduce storage space, making it ideal for filesystems that store vast amounts of data, such as media archives and backup systems. Additionally, it’s beneficial for systems with limited storage space, such as mobile devices and embedded systems.

Btrfs compression uses various compression algorithms to compress data while writing it to the filesystem. The compressed data is then stored on the disk and automatically decompressed on the fly when accessed. This process is seamless to the applications accessing the data, so there’s no need for them to be aware of the compression process as Btrfs handles it. Btrfs supports three compression algorithms, zlib, lzo, and zstd, each with its own strengths and weaknesses:

  • zlib: This is a widely used general-purpose compression algorithm that provides good compression ratios but can be relatively slow, especially at higher compression levels. It is suitable for compressing general-purpose data and text files.
  • lzo: This is a lightweight compression algorithm that provides good compression ratios and is relatively fast. It is suitable for compressing data that is already compressed, such as media files and archives.
  • zstd: This is a newer compression algorithm that provides a good balance between compression ratio and speed. It is suitable for compressing a wide range of data types, including text, media files, and archives.

The choice of compression algorithm depends on the specific use case and performance requirements.

To enable compression on a filesystem, you can use the btrfs property command to set the compression for the filesystem:


btrfs property set /data/vol1 compression zstd

When compression is enabled, only new data being written to the filesystem is compressed. However, you can also use the defragment command to compress the data that was on the filesystem before compression was enabled. To do this on a subvolume, you will also need to recursively run the command using the -r option along with the -c option to compress data:


btrfs filesystem defragment -rc /data/vol1

There’s more…

Compression efficiency will vary, depending on the data and algorithm used. Let’s first check how much space we have, using the following command:


[root@btrfs vol1]# btrfs filesystem usage /data/vol1 | grep Data | grep Used
Data,RAID1C3: Size:1.00GiB, Used:0.00B (0.00%)

Now, let’s create a 2 GB file with random data:


head -c 2G </dev/urandom > /data/vol1/test1

Next, we can use the df and du commands to compare the space consumed and the space used:


df -h /data/vol1
du -hd0 /data/vol

The sample outputs for these commands are as follows:


[root@btrfs vol1]# df -h /data/vol1
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb         30G  2G   28G   1% /data/vol1
[root@brtfs vol1]# du -hd0 /data/vol1
2.0G    /data/vol1
[root@brtfs vol1]#

Here, we can see 2 GB is used (via the du command), and also 2 GB (via the df command) is actually consumed. So, for this random data file, we can see no benefit from the compression.

Let’s delete the test1 file now:


rm /data/vol1/test1

To speed things up, let’s defrag and rebalance the volume. This is needed to actually free up the space from the deleted file now, instead of waiting:


[root@btrfs vol1]# btrfs filesystem defragment -rc /data/vol1
[root@brtfs vol1]# btrfs filesystem balance /data
Done, had to relocate 4 out of 4 chunks
[root@btrfs vol1]#

Now, with a file with lots of repeating data, we will see more compression. To create a 2 GB file with repeating data, run the following command:


yes “repeating text” | head -c 2G >test2

Now, we repeat the same du and df commands as before, but we will see very different results:


[root@brtfs vol1]# df -h /data/vol1
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb         30G   62M   29G   1% /data/vol1
[root@brtfs vol1]# du -hd0 /data/vol1
2.0G    /data/vol1
[root@brtfs vol1]#

Here, we see 4 GB is allocated, but only 62 MB is actually used! That’s a huge benefit from the compression.

Share this post on:

Leave a Reply

Your email address will not be published. Required fields are marked *