How can I use the Btrfs compression option when installing Linux?

6,350

Solution 1

I believe you have to run

btrfs fi defragment

after mounting the FS with compression.

Check this out: https://askubuntu.com/questions/129063/will-btrfs-automatically-compress-existing-files-when-compression-is-enabled

Solution 2

When installing you can issue a:

mount -o remount,compress=lzo

Often you will have to do it just as the installer starts installing stuff (maybe you will miss a file or 2. but no biggie). You could check to see if the installer has already mounted the filesystem just before the install stage.

Also make sure you update the /etc/fstab on the installed system. I use the following options that give some other improvements:

compress=lzo,ssd,relatime,space_cache,inode_cache,autodefrag

You can also try using compress-force=lzo for the install rather than compress=lzo, it makes sure that even files that don't compress that well are still compressed. That would make writes slower but since it's a one time install that might be worth it. But I'm not sure if it improves reads or not.

I have seen another way that involves renaming the mount.btrfs binary and replacing it with a script but when I did it under Ubuntu it messed up as it normally installs to subvolumes @ and @home but everything just got dumped to the root of the filesystem.

Alternatively you can install and then use the defragment, but you must defragment every file individually as the command isn't recursive. This could come in handy to upgrade to the new compression methods that are likely to appear in the newer versions of btrfs, snappy and lz4. Run the following from the / directory.

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

Solution 3

Like many questions with software - the answers change depending on WHEN they get asked/answered. I'm freshening the answer to help anyone Googling their way into topic. :)

The correct answer for btrfs 3.12 and higher to compress directories/mountpoints POST installation (the required recursive option for defrag was added with btrfs 3.12 released in Nov 2013) :
btrfs filesystem defragment -c -r MOUNTPOINT

Here is the current options from the cli of btrfs version 4.4:
btrfs filesystem defragment
usage: btrfs filesystem defragment [options] | [|...]

Defragment a file or a directory

-v             be verbose
-r             defragment files recursively
-c[zlib,lzo]   compress the file while defragmenting
-f             flush data to disk immediately after defragmenting
-s start       defragment only from byte onward
-l len         defragment only up to len bytes
-t size        target extent size hint

BTRFS Use Case from their wiki for using the recursive param to defrag directories/mount points

BTRFS Changelog entry for v3.12

Share:
6,350

Related videos on Youtube

Phil Hannent
Author by

Phil Hannent

I am a director of a software company. We code for interactive touch screens, React based activity sites for business and education. We also have software as a service offerings. Our main technology stacks are: Qt/C++, ReactJS, NodeJS, Azure.

Updated on September 18, 2022

Comments

  • Phil Hannent
    Phil Hannent over 1 year

    I've been trying out the Btrfs with an SSD drive and like the compress option which is set in the fstab.

    However this only applies to the files created after I have installed the operating system and modified the fstab file.

    What I would like is to apply the compression to all the files on the system, to see if it improves boot time and application start up times.

    Does anybody know of a way that I could apply the change to all files either pre or post installation?

  • DanielSmedegaardBuus
    DanielSmedegaardBuus over 9 years
    This is the more correct answer. The only drawback to doing post-installation compression is that a lot of files won't be compressed on account of being busy. Tricking the installer by preparing the filesystem beforehand is more effective, but also a lot more difficult for the less experienced user.
  • bain
    bain over 9 years
    "you must defragment every file individually as the command isn't recursive" - use -r for recursive defrag: btrfs filesystem defragment -r -v -clzo /
  • CharlieRB
    CharlieRB about 8 years
    Welcome to Super User. Please be aware you have posted an answer to a question that is years old and already has an accepted answer. Although there is nothing wrong with doing so, just be aware you may not get a response.
  • Jeff Burns
    Jeff Burns about 8 years
    I'm OK with not getting a response, but the accepted answer is no longer correct. So a Google search results in users getting bad data.
  • CharlieRB
    CharlieRB about 8 years
    And that is a perfectly good reason to post it. Good job.