Install Ubuntu or Linux Mint with Btrfs disk compression enabled

This is a guide for installing Ubuntu or Linux Mint with full file system compression enabled, on a BTRFS partition.

This guide is intended to be simple and not for advanced users.
For beginners, this guide will take less time than other advanced guides.

This is useful in case you have a storage device with limited space, like an SSD, or you just want few extra GBs.

The installer does not allow to enable compression during installation, we will work around that by installing Ubuntu on a partition with filesystem that allows compression, and then apply compression right after installation is finished.

This was tested on the ubuntu 13.04 Raring Ringtail.
This guide is expected to work on all OSes based on this ubuntu version like:
Linux Mint 15 Olivia Cinnamon/Mate.
This guide should also be working fine on earlier or later versions of ubuntu.
There is not guarantee that this guide will work in your scenario, always backup your data first.

(tested on xubuntu-13.04-desktop-i386.iso)

Part.1: Install on a BTRFS partition.

1- Decide on suitable partition sizes.
In this example, since the total size 21474 MB, we divided it to two partitions: main partition = 18474 MB, and swap partition = 3000 MB.
The swap partition should be larger then or equal to the RAM size if you want hibernation to work.
You can read more about swap partitions in here:
https://help.ubuntu.com/community/SwapFaq#How_much_swap_do_I_need.3F

2- During installation, the installer will ask you what you want to do about the partitions (screenshot 1) select as shown, “something else” to edit the partition table manually.

ubuntu-mint-partition-ss-1

3- Click “New partition table…” (screenshot 2) , confirm when required.

ubuntu-mint-partition-ss-2

4- Select the “Free space” from the device list, and then click on the button with the plus “+” sign (screenshot 3)

ubuntu-mint-partition-ss-3

5- Create the first parition, Options:
Size: 18475 MB.
Type for new partition: Primary
Location for the new partition: Beginning of this space
Use as: BTRFS journaling file system
Mount point: “/”

ubuntu-mint-partition-ss-4

6- Once again, Select the “Free space” from the device list, and then click on the button with the plus “+” sign, and then make those changes:
Size: (leave unchanged to occupy all freespace)
Type for new partition: Primary
Location for the new partition: Beginning of this space
Use as: swap area

ubuntu-mint-partition-ss-5

7- After you have done the above, the partitions should be looking similar to the screen shot.

ubuntu-mint-partition-ss-6

8- Click Install Now

Part.2: Apply compression to main partition.

After installation finishes and the system starts up for the first time:

9- Check the current free space, by using this command:

$ df -TBM
Filesystem  Type   1M-blocks  Used   Available  Use%  Mounted on
/dev/sda1   btrfs  17618M     2254M  13661M     15%   /

The output will have a line that contains btrfs, and showing size, used and available space.

10- Now, you need to choose a compression algorithm:
“compress-force=zlib” – Better compression ratio, uses more CPU.
“compress-force=lzo” – Faster compression, Does not save space as much as zlib.
“compress-force” – the default, equals to “compress-force=zlib”

In this guide we will use “compress-force”

11- You will need to edit the file of list of partitions, to configure the new settings:

$ sudo nano /etc/fstab

Find the lines that look similar to this example, that has “btrfs” in them, there should be 1 or 2 lines:

UUID=00000000-0000-0000-0000-000000000000 / btrfs defaults,[email protected] 0 1
UUID=00000000-0000-0000-0000-000000000000 /home btrfs defaults,[email protected] 0 2

in the part right after “btrfs”, where it says “defaults,[email protected] , add “,compress-force”
it should look like this:

UUID=00000000-0000-0000-0000-000000000000 / btrfs defaults,[email protected],compress-force 0 1
UUID=00000000-0000-0000-0000-000000000000 /home btrfs defaults,[email protected],compress-force 0 2

If you have an SSD you should add the SSD options, “ssd” and “discard”, like this:

UUID=00000000-0000-0000-0000-000000000000 / btrfs defaults,[email protected],compress-force,ssd,discard 0 1
UUID=00000000-0000-0000-0000-000000000000 /home btrfs defaults,[email protected],compress-force,ssd,discard 0 2

If you want to use “compress-force=lzo” instead of “compress-force” , the lines will be:

UUID=00000000-0000-0000-0000-000000000000 / btrfs defaults,[email protected],compress-force=lzo 0 1
UUID=00000000-0000-0000-0000-000000000000 /home btrfs defaults,[email protected],compress-force=lzo 0 2

12- Now reboot the computer to make sure everything is working fine.

13- After the system reboots, open up a terminal and do the following:

$ sudo -s
$ cd /
$ find -xdev -type f -exec btrfs fi defrag '{}' \;
$ exit

14- Wait a little bit for all files to compress, and you are all done, you might see some few errors, they are not important.

15- Check the current free space, by using this command:

$ df -TBM
Filesystem  Type   1M-blocks  Used   Available  Use%  Mounted on
/dev/sda1   btrfs  17618M     2104M  13812M     14%   /

Compare that with the previous stats, there was 2254M used, now there is 2104M

Compression ratio is 93%. (Compression ratio is the size of compressed files compared to the non-compressed files)
Compression ratio will be better when the system have more text/php/html files and documents.

Please leave a comment if you have any suggestion to make this article better.

2 thoughts on “Install Ubuntu or Linux Mint with Btrfs disk compression enabled”

  1. Your compression ratio is kind of ambiguous. The “standard” (from wikipedia, gzip, etc) compression ratio is (uncompressed size)/(compressed size), so here it’s about 1.07. Meaning space savings of about 7% [=1-(compressed/uncompressed)].

    Saying the compression ratio is 93% makes it sound like the files are 93% *smaller* than what they used to be. Their new size is 93% of what they used to be, though. It would probably be better if there were a clearer standard way of describing compressed file sizes, even gzip calls the “space savings” calculation the “ratio”, and programs like file roller or archive manager use the 1.05 type first ratio.

Leave a Reply

Your email address will not be published. Required fields are marked *

(Your message will only be visible after moderation)