37

I chose btrfs as the format of my / filesystem in the Ubuntu 12.04 LTS installer. After the installation completed, I added compress=lzo to the mount options in /etc/fstab and rebooted.

Will the existing files be automatically compressed now, or must I explicitly do something to cause that to happen?

ændrük
  • 78,726
  • 1
    You don't have to reboot, you could just sudo mount -o remount / and the new fstab options will take effect. – sep332 Jan 16 '14 at 19:13
  • NB that you may need to use the compress-force option or some files will still remain uncompressed no matter what you do... – rogerdpack Mar 12 '14 at 19:40
  • 2
    A note regarding compress-force. Normal compress has a feature in that if it detects that compression has little to no benefit, it doesn't bother continuing the compression for that particular file (thereby saving wasted CPU). By using compress-force, it attempts useless compression anyway.

    Good examples where this applies are for files that are already compressed (multimedia, zip files, etc).

    In other words, compress-force is generally a bad idea. ;)

    – zaTricky Oct 15 '16 at 08:10

5 Answers5

34

You will have to run btrfs fi defragment to force recompression of existing data. Otherwise, only new data will be compressed.

From the FAQ:

...consider remounting with -o compress, and either rewrite particular files in-place, or run btrfs fi defragment to recompress everything. This may take a while.

ændrük
  • 78,726
10

I've made what Norbert Fabritius said, but I didn't notice any compression in the existing files - df -h / before btrfs fi defragment = 658MB | df -h / after btrfs fi defragment = 658MB. New files are ok. Searching a little bring me this quote:

Running this:

# btrfs filesystem defragment ~/stuff

does not defragment the contents of the directory.

This is by design. btrfs fi defrag operates on the single filesystem object passed to it. This means that the command defragments just the metadata held by the directory object, and not the contents of the directory. If you want to defragment the contents of the directory, something like this would be more useful:

# find -xdev -type f -exec btrfs fi defrag '{}' \;

After this, my / it's occupping 656MB - nothing huge, but certainly there is compression.

Source: https://btrfs.wiki.kernel.org/index.php/Problem_FAQ#Defragmenting_a_directory_doesn.27t_work

Hi-Angel
  • 4,915
J. Neto
  • 109
  • 4
    Shouldn't your command line include defrag -clzo or defrag -czlib instead of just defrag, if you want to actually compress things? – jbo5112 Nov 17 '13 at 05:14
  • 12
    There's a "-r" recursive flag so you can just do this now: btrfs fi defrag -r -czlib ~/stuff – Salami Oct 14 '16 at 17:28
  • 1
    Just for reference, I got a 9.17 -> 5.89 GiB result with zstd:3 (default) on a fresh Ubuntu 22.04 install. Lovely. – martixy May 03 '23 at 07:29