BTRFS Compression - Notes

It is possible to mount a Btrfs filesystem with different compression settings and change those settings as needed. Here’s how you can do it:

Mounting with Compression: You can initially mount the Btrfs filesystem with a specified compression method. For example, to mount with maximum compression using zlib, you can use:

sudo mount -o compress=zlib /dev/sdX1 /mnt/mybtrfs

Unloading Data: After moving some data, you can unmount the drive:

sudo umount /mnt/mybtrfs

Remounting with Different Compression: You can then remount the Btrfs filesystem with a different compression option. For example, to use zstd with maximum compression, you would do:

sudo mount -o compress=zstd:9 /dev/sdX1 /mnt/mybtrfs

Important Notes

Compression Options: Btrfs supports several compression algorithms, such as zlib, zstd, and lzo. Each has different performance and compression characteristics.

Per-File Compression: Btrfs allows you to set compression options on a per-file basis, but when mounting, the specified option applies to new files created or copied to the mounted filesystem.

Existing Files: Existing files will not be recompressed when you change the mount options. To recompress existing files with a new compression method, you would need to copy them again or use a command to explicitly change their compression.

Example of Changing Compression on Existing Files

To change the compression of existing files, you can use the following command:

sudo btrfs property set /mnt/mybtrfs/yourfile compress zstd:9

Remember that existing files will not automatically change compression until you take explicit action to re-compress them.



Supported Compression Types in Btrfs

No Compression

sudo mount -o compress=none /dev/sdX1 /mnt/mybtrfs

/etc/fstab line for mounting at boot:

/dev/sdX1 /mnt/mybtrfs btrfs defaults,compress=none 0 0

No compression is applied.


Zlib Compression

sudo mount -o compress=zlib /dev/sdX1 /mnt/mybtrfs

fstab:

/dev/sdX1 /mnt/mybtrfs btrfs defaults,compress=zlib 0 0

Uses the Zlib algorithm, providing moderate compression ratios.


LZO Compression

sudo mount -o compress=lzo /dev/sdX1 /mnt/mybtrfs

fstab:

/dev/sdX1 /mnt/mybtrfs btrfs defaults,compress=lzo 0 0

LZO is a fast, lightweight compression algorithm.


Zstd Compression

sudo mount -o compress=zstd /dev/sdX1 /mnt/mybtrfs

fstab:

/dev/sdX1 /mnt/mybtrfs btrfs defaults,compress=zstd 0 0

Zstd (Zstandard) offers high compression ratios and good speed.

Zstd with Level Options

sudo mount -o compress=zstd:9 /dev/sdX1 /mnt/mybtrfs

fstab:

/dev/sdX1 /mnt/mybtrfs btrfs defaults,compress=zstd:9 0 0

You can specify compression levels (1-15) with Zstd for fine-tuning.


Summary

Compression Type Pros Cons
No Compression Fast performance, no CPU overhead More disk space required
Zlib Good balance of speed and compression Higher CPU usage than no compression
LZO Very fast compression/decompression Lower compression ratios
Zstd High compression with decent speed Slightly higher CPU usage than LZO
Zstd with Levels Flexible for tuning performance High levels increase CPU usage

snippet.cpp
by:
▖     ▘▖▖
▌ ▌▌▛▘▌▙▌
▙▖▙▌▙▖▌ ▌
 
written: a while ago. Edited: June 8 2025