In this lesson, you will learn how to enable transparent compression on your Btrfs filesystems to trade a small amount of CPU overhead for significant gains in storage capacity.
The Logic of Transparent Compression
When we talk about "transparent" compression, we mean that the process is entirely invisible to your applications. When you write a file to the disk, the filesystem automatically shrinks it before saving it; when you read that file back, the filesystem decompresses it on-the-fly. Your programs behave exactly as they would on an uncompressed drive, but your disk space lasts longer.
Btrfs supports several algorithms, but zstd and lzo are the industry standards:
zstd(Zstandard): The current "gold standard." It offers an excellent balance of high compression ratios and speed. It is highly tunable, allowing you to choose how hard the CPU works to save space.lzo: An older, lightweight algorithm. It is incredibly fast and requires very little CPU, but it does not compress files as tightly aszstd. It is a great choice for systems with very limited processing power.
Implementation
You can enable these algorithms at mount time. This setting applies to all new data written to the filesystem. To enable zstd compression on a mounted Btrfs partition, you can remount it with the following command:
sudo mount -o remount,compress=zstd /mnt/your_drive
If you prefer to use the lighter lzo algorithm, simply swap the parameter:
sudo mount -o remount,compress=lzo /mnt/your_drive
Important Considerations
Because Btrfs is a Copy-on-Write (CoW) filesystem, compression only happens when new data is written. If you have an existing directory full of files, they will not be compressed until you modify them or rewrite them.
Furthermore, while compression can save space, it is not "free." On systems with extremely fast storage (like high-end NVMe drives) and very slow CPUs, heavy compression might become a bottleneck. However, for most general-purpose servers and workstations, the CPU overhead is negligible, and the space savings—especially for text-heavy logs or source code—are substantial.
Key Takeaway: Transparent compression works in the background to shrink your data automatically, using zstd for a modern, efficient balance or lzo for maximum speed.