File System and Btrfs

File System

Control the entries in fstab:

nano /etc/fstab

If you read in that file relatime then change this to noatime. This means that the access time of files will not be archived/written to disk. Usually the access time is not required and this is especially important for copy on write file systems like btrfs. If your SSD supports discard, add this as well. Example line:

UUID=????? / btrfs defaults,noatime,compress=zstd,space_cache,autodefrag,inode_cache 0 0

Enable Trim (SSD)

Enabling the fstrim timer from util-linux will run the trim command for SSDs once per week (read https://wiki.archlinux.org/index.php/Solid_State_Drives#Periodic_TRIM).

systemctl enable fstrim.timer

Boot Kernel Load Time

The kernel load time of initramfs can be reduced by switching from gzip to lz4. Actually zstd is not yet supported in Arch Linux kernels. Ubuntu will make this default in 18.10 and according to their measurements they save in average 1 second of boot time. Edit mkinitcpio and uncomment the lz4 line:

$ nano /etc/mkinitcpio.conf
COMPRESSION="lz4"

The run:

$ mkinitcpio -p linux

Btrfs

Before using special Btrfs features read the Status Wiki https://btrfs.wiki.kernel.org/index.php/Status and be aware about the difficulties if you use unstable functionalities. And as with every file system, always make backups.

Btrfs compression

Btrfs offers to compress files automatically during all new write transaction. This is a great functionality, you save disk space especially for small or uncompressed data and can improve read speeds. Compression during write transaction is not notable. For more information, read https://wiki.archlinux.org/index.php/Btrfs#Compression. Compression benchmark https://git.kernel.org/pub/scm/linux/kernel/git/mason/linux-btrfs.git/commit/?h=next&id=5c1aab1dd5445ed8bdcdbb575abc1b0d7ee5b2e7.

Modify in the fstab file the mount options by adding the compression algorithm (choose zstd for better compression but requires Linux 4.14+, lzo for stability and older kernels). If you want to use compression zstd on a root partition, then you need to use systemd-boot or Grub version 2.0.4!

nano /etc/fstab

UUID=?????? / btrfs defaults,noatime,discard,compress=zstd,space_cache,autodefrag,inode_cache 0 0

This will compress all new written data after you have mounted the drive the next time, reboot your system.

To compress already written data you can use the following command for the root / drive (replace czstd if you want to use lzo with clzo):

btrfs filesystem defragment -r -v -czstd /

How much disk space you will save depends from the stored files. If you store highly compressed data like mp3 files you won’t save a lot. Videos files are as well excluded. But especially small files are compressed very well, you root partition will show a big impact on free space.

The root drive of a full desktop environment occupied after installation 7,2 GiB disk space before compression. After compressing all files with the command above it got down to 5,1 GiB disk space only. Additional 2,1 GiB disk space for a feature without any real downsides and probably positive read speed is quite impressive.

If you need to mount a drive with compression enabled for write transactions, you can do it with the following command:

mount -o compress=zstd /dev/sdxY /mnt/

Compression is one of the great features of Btrfs and I do enable it on all my drives.

Btrfs Encountered Issues

There are some issues and bugs that could lead to a bad experience with btrfs:

  • Writing hundreds of GB’s to a Btrfs partition in one go can report the file system as full even if there is plenty of space. Bug encountered in Linux 5.4 when installing all Steam games to a 5TB disk, several times Btrfs reported all disk space as being occupied. But it was not full and the free disk space was reported as being available after running scrub for some minutes (it was not necessary to finish the scrub run and it could be cancelled).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.