Will btrfs automatically compress existing files when compression is enabled?
Solution 1:
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 runbtrfs fi defragment
to recompress everything. This may take a while.
Solution 2:
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
Solution 3:
According to Oracle's documentation, you can compress existing files on an existing, online filesystem by defragmenting each file in it with the -c, -clzo, or -czlib options. LZO is recommended for speed.
find / -xdev \( -type f -o -type d \) -exec btrfs filesystem defragment -v -clzo -- {} +
This uses the find command to run the btrfs defragmenter on every file in the root filesystem (given by the slash right after the "find" command at the beginning). If you have other subvolumes, you can use it again with the path of a subvolume (I have one at /home, for example) instead of the single slash.
You'll need root privileges for this, so add sudo to the front if you need to.
See:
- http://docs.oracle.com/cd/E37670_01/E37355/html/ol_use_case1_btrfs.html
- https://btrfs.wiki.kernel.org/index.php/UseCases#How_do_I_defragment_many_files.3F