Ny way in BTRFS to set compression permanently

btrfscompressionmount

I have a USB HDD.

I want to mount it with compression enabled. I can do in the fstab of my system or even using udev rules. The problem is that I won't mount my USB HDD on my computer only. Up to now, I used to trigger a terminal each time I mounted it.

Then, I discovered chattr +c. This is working very well but I want to use LZO instead of ZLIB. Is there any way to be more specific and define the compression algorithm once for all?

Best Answer

btrfs will compress every file changed since it was mounted if you use:

mount -o compress-force=lzo /dev/btrfsdev /mnt/btrfsmnt

If you want to see to it that ALL files get compressed this way, I've got a little script I wrote to do it...

du -ht +$((1024*1024)) "$HOME" |\
    sed -rn 's/^[^/]*(.*)/btrfs fi defrag -fvclzo "\1"/p' |\
        sudo sh -n

The above only works on my $HOME directory - but you can use it on anything or everything as you like. It's also got the -n operand fed to sh as is so you can see for yourself what sh is currently not doing before removing it to tell it to do it.

Anyway, first it queries du for files in human-readable format (probably redundant here since we strip that in the next step anyway) that are larger than 1MB or $((1024*1024)).

It |pipes its info to sed which strips off everything before the leading /, "quotes" the filename, builds the btrfs filesystem defragment -verbose -flush-to-disk -compress-lzo \filename command and hands it over a |pipe to sudo sh to execute.

Again, it won't do anything so long as sh --no-execute is in effect, though.

I think some very recent btrfs userspace tool builds do defragment entire directories recursively, but if so it's a pretty new thing, so I've always had to do stuff like this.