Optimizing SSD on Fedora with Btrfs Transparent Compression: “Expand” Your Storage for Free

Fedora tutorial - IT technology blog
Fedora tutorial - IT technology blog

Why Should You Care About Btrfs Transparent Compression?

After using Fedora as my primary coding machine for over two years, I really appreciate its package update speed. However, an eternal problem is that SSDs (often only 256GB or 512GB) fill up very quickly. Just a few projects containing node_modules, bloated log files, or Docker image builds, and the storage warning turns red immediately.

This is where Btrfs Transparent Compression comes into play. Think of it as a smart filter: every time you write data, the system automatically compresses it before pushing it to the memory chips. When you need to use it, it decompresses instantly. The entire process happens at the filesystem level. Your software has no idea and doesn’t need a single line of code changed.

The biggest benefit is saving space, typically between 30% and 50% for programming data. Interestingly, it also helps improve I/O performance. This might sound counterintuitive since the CPU has to do extra work, but in reality, SSD write speeds are often much slower than modern CPU processing speeds. By compressing files, the CPU pushes less data to the drive. This results in faster file writes and reduces the TBW (Total Bytes Written) index, helping to extend your SSD’s lifespan.

Zstd or LZO: Which Algorithm Is Best?

Btrfs supports several compression algorithms, but in practice, you only need to consider two:

  • LZO: Extremely fast, uses very few resources, but has a low compression ratio. This is the choice for older machines or legacy CPUs.
  • ZSTD (Zstandard): A modern standard developed by Facebook. It perfectly balances speed and compression capability. This is also the default choice trusted by the Fedora community.

For laptops with Intel 10th Gen or AMD Ryzen chips and above, I recommend using zstd. Compression level 1 or 3 is ideal for daily needs, ensuring the machine stays smooth even when compiling large projects.

Detailed Configuration Steps on Fedora

Fedora has been using Btrfs since version 33, but the compression feature is often not enabled at its most optimal level. We will tweak the system configuration in three simple steps.

Step 1: Check Mount Status

First, let’s see what options the current partitions are running with:

mount | grep btrfs

If the output doesn’t contain a line with compress=zstd:1 or similar, it means compression is not yet enabled for new data.

Step 2: Edit the /etc/fstab File

This is the file that manages disk mounting at boot. You need root privileges to edit it:

sudo nano /etc/fstab

Find the lines mounting the root (/) and /home partitions. Add the compress-force=zstd:3 parameter to the options column. The structure will look like this:

# Before editing:
UUID=xxx-yyy / btrfs subvol=root,defaults 0 0

# After editing:
UUID=xxx-yyy / btrfs subvol=root,defaults,compress-force=zstd:3 0 0

A small explanation: I use compress-force instead of the standard compress. By default, Btrfs will stop compressing if it finds a file “difficult” to compress. The force option compels the system to compress everything, maximizing optimization for text files, source code, and logs.

Step 3: Apply Changes

You don’t need to restart your computer. Just run the remount command for the system to recognize the new configuration:

sudo mount -o remount,compress-force=zstd:3 / 
sudo mount -o remount,compress-force=zstd:3 /home

Compressing Existing Data

Note that editing fstab only affects files created from now on. To compress the existing data taking up space, you need to run the defragment command:

# Recompress the entire root partition
sudo btrfs filesystem defragment -r -v -czstd / 

# Recompress the home directory
sudo btrfs filesystem defragment -r -v -czstd /home

This process will re-read all files and write them back to the disk in compressed form. Depending on how much code and log files you have, this can take anywhere from a few minutes to half an hour.

Check the Results: How Many GB Did You Save?

The df -h command won’t show you the actual space saved after compression. You need to install a specialized tool called compsize:

sudo dnf install compsize

Try checking the /var/log directory or your project folder:

sudo compsize -x /var/log

Actual results on my machine usually look like this:

Processed 1528 files, 212530 regular nodes
Data to compress:       1.2GB
Data actually stored:   180MB
Compression ratio:      15%

The line Compression ratio: 15% means the data now only takes up 15% of its original size. Impressive, right?

Real-World Experience

After using it for a long time, I have a few tips for you:

  1. Regarding Databases: If you run heavy PostgreSQL or MySQL workloads, consider disabling compression for their data directories to avoid lag during continuous writes.
  2. Media Files: Formats like .jpg, .mp4, or .zip are already compressed. Btrfs won’t help much with folders containing movies or photos.
  3. The Sweet Spot: Zstd level 3 is the best choice. Increasing it to level 10-15 only makes the CPU run hotter without saving significantly more space.

Enabling Transparent Compression is the first thing I do after installing Fedora. It helps my 256GB laptop operate as comfortably as a 400GB drive. Good luck with your setup!

Share: