Btrfs/Usage
Usage[edit | edit source]
Mount options[edit | edit source]
See Btrfs Mount Options for full list of mount options.
Subvolumes[edit | edit source]
Btrfs subvolumes is one of the unique features of Btrfs. A subvolume is a part of the filesystem but has its own independent file/directory hierarchy. Every mount of a btrfs filesystem also mounts a subvolume. In most ways, a subvolume looks like a normal directory. You can copy it, write in it, rename it, etc. Subvolumes can also be nested.
When you do mkfs.btrfs /dev/sdb1
btrfs automatically creates a default subvolume. This will be the defaul rootvolume when mounting the filesystem with no options.
This is an example how a Btrfs filesystem can look like.
toplevel (default subvolume root directory) +-- dir_1 (normal directory) | +-- file_2 (normal file) | \-- file_3 (normal file) \-- volumes (directory) +-- root (subvolume) | \-- file_4 (normal file) +-- home (subvolume) +-- tmp (subvolume) \-- file_5 (normal file)
Use the -o subvol= mount option to specify what subvolume root to mount.
- mount /dev/sdb1 /home -o subvol=volumes/home (mounts the home subvolume in /home)
- mount /dev/sdb1 /var/tmp -o subvol=volume/tmp (mounts the tmp subvolume in /var/tmp)
Compression[edit | edit source]
Btrfs supports several different compression algorithms. Currently Btrfs supports zlib (2009), lzo (20011) and zstd (2017). Zstd is probably the best all-round choice since is it very fast and have good compression ratios.
Compression can be enabled using the -o compress or -o compress-force mount options. Zlib and zstd has adjustable compression levels. Compression can also be set on individual files and folders using btrfs property set.
Btrfs has built-in checks to see if given data is compressible, so it does not try to spend time compressing data with no gain. This was developed with zlib in mind. The zstd algorithm is faster in determining compressibility. Therefore it is better to use the compress-force mount option.
- mount -o compress-force=zstd:5 (sets the compression level to 5)
- mount -o compress=zlib
- mount -o compress=lzo
- btrfs property set <somefile or folder> compression zstd (it is not yet possible to set compression level this way)
Note that setting compression only applies to new data, not existing data. In order to compress existing data you need to use btrfs filesystem defrag.
Space Cache[edit | edit source]
Due to its CoW nature, Btrfs has to know where free data is available in order to do any writes. Space Cache helps keep track of the available free space to speed up its operations.
There are two versions available, space_cache and an improved version called space_cache=v2 (A.K.A Free Space Tree) which can handle larger volumes much better. Default is still space_cache due to compatibility with Kernels older than kernel-4.5.
Recommendation today is to use space_cache=v2 unless you know you will be using an older kernel. Once mounting with space_cache=v2, the filesystem will only be mountable as read-only on older kernels.
- mount -o space_cache
- mount -o space_cache=v1
- mount -o space_cache=v2
- mount -o nospace_cache (mounts without using the space_cache)
- mount -o clear_cache,nospace_cache (removes the space cache from disk)
Discard / Trim[edit | edit source]
SSD and flash storage internally writes large blocks of data (called erase blocks). Even if you want to write 1kiB, the SSD still may write out a whole block of 4MiB. The difficulty happens when the disk starts to fill up. There may not be 4MiB free erase blocks. In this case the SSD needs to read a block containing some data, add the data you want to write, then re-write that whole block. This process is called Read-Modify-Write (RMW). When this happends, write performance goes down.
DISCARD / TRIM is a way for the filesystem to let the SSD know what areas of the disk that are not used. For example when you delete a file, the SSD does not know normally know this, since the file isn't actually erased, merely removed in the filesystem metadata.
There two main ways to use DISCARD / TRIM; using the fstrim tool or using the -o discard mount option.
- fstrim -v /mnt/btrfsvolume (this frees up all unused space on the volume. Can be slow - several seconds or minutes)
- mount -o discard (this tells Btrfs to send discard commands after every deletion, can affect performance depending on the disk model)
- mount -o discard=async (The same a before, but buffers up the discard commands to avoid the performance impact. Available since kernel-5.6)
Currently, it's recommended to setup fstrim in a cronjob and run it daily or weekly at a time when it doesn't affect the users so much.