Skip to content

Hands On With The Btrfs File System

Introduction

  • Use Btrfs (b-tree file system)
  • 16 GB USB flash drive.
  • Maximize storage use
  • Enable compression
  • Leverage copy-on-write (COW)
  • Enable online Deduplication
  • Encrypt the backup device

Should the file system be mounted without compression enabled you can still manually apply compression to a file or directory (that is a lower case letter c below):

chattr +c /dir

Finally we want to take advantage of the Btrfs checksumming feature to ensure that our backup isn't suffering from any data corruption due to underlying storage bad blocks of file system data structure corruption. Checksumming is turned on by default.

Unmount The Backup Device

For this exercise we're using a 16 GB USB flash drive. When we insert it into a USB port Linux will automount it. So we'll unmount all of its partitions first. Then we'll create a new partition table and create a single partition consuming the rest of the space on the drive. Our weapon of choice is fdisk.

We know the USB flash drive presents as /dev/sdd so we insert it, see which parititions are mounted and unmount them:

mount |  grep -i sdd
/dev/sdd2 on /media/johnb/BB5E-BAED type vfat (rw,nosuid,nodev,relatime,uid=1000,gid=1000,fmask=0022,dmask=0022,codepage=437,iocharset=iso8859-1,shortname=mixed,showexec,utf8,flush,errors=remount-ro,uhelper=udisks2)
sudo umount /dev/sdd2

Examine Existing Partitions

Next we launch fdisk and list the available partitions:

sudo fdisk /dev/sdd
p
Disk /dev/sdd: 14.63 GiB, 15693664256 bytes, 30651688 sectors
Disk model: Extreme         
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xb032e56b

Device     Boot   Start     End Sectors  Size Id Type
/dev/sdd1  *         64 3764639 3764576  1.8G 17 Hidden HPFS/NTFS
/dev/sdd2       3764640 3766111    1472  736K  1 FAT12

Delete All Partitions

We will delete both partitions next. Still in fdisk:

d
d

The first d delete partition 2, the second deletes partition 1.

Create One Partition

Next we create a new partition spanning the entire device. Still in fdisk:

n
p
1
Return
Return

The new partition 1 is of type Linux and spans the rest of the storage device. Now update the partition table, still in fdisk:

w

Having repartitioned it, we unplug the USB flash drive. The next time we plug in the USB flash drive it appears as /dev/sde. The partition appears as /dev/sde1. It is now ready for the next steps.

Install BTRFS Software

We need to install the Btrfs software:

sudo apt install btrfs-progs

We also need to practice on a safe medium. We choose a USB flash drive at /dev/sdd. When we install it the system automounts it so first unmount it. You don't want a device mounted when you're changing its file system:

sudo umount /dev/sdd1

Format as Btrfs (1)

The USB flash drive has an existing partition so we add the -f command line option to force the operation, labeling the partition as stick:

sudo mkfs.btrfs -f -L stick /dev/sdd1
btrfs-progs v5.4.1 
See http://btrfs.wiki.kernel.org for more information.

Format as Btrfs (2)

Label:              stick
UUID:               b3786b8c-dda7-4e27-8d75-53232ab0d554
Node size:          16384
Sector size:        4096
Filesystem size:    14.58GiB
Block group profiles:
  Data:             single            8.00MiB
  Metadata:         DUP             256.00MiB
  System:           DUP               8.00MiB
SSD detected:       no
Incompat features:  extref, skinny-metadata
Checksum:           crc32c
Number of devices:  1
Devices:
   ID        SIZE  PATH
    1    14.58GiB  /dev/sdd1

Enable Compression (1)

Compression is automatic and transparent for Btrfs file systems. For slower storage devices on multiprocessor systems this will improve file system performance. Four compression options are available: no, zlib, lzo and zstd. The last two offer the best compression.

Files created or modified after the file system is mounted will be compressed. To compress existing files (with zstd compression on a mounted file system:

btrfs filesystem defragment -r -v -czstd /dev/sdd1

To mount a file system with a specific compression option:

sudo mount -o compress=zstd /dev/sdd1 /mnt/

Enable Compression (2)

Note that the zstd compression option is relatively new older kernels and older builds of btrfs-progs won't support it. Likewise it requires GRUB 2.04 or current. rEFInd post version 0.11.4 has support.

USB flash drives will automount when inserted, or when "touched" by the file manager. To check on the condition of a mounted BTRFS file system:

mount | grep sdd
/dev/sdd1 on /mnt type btrfs (rw,relatime,compress=zstd:3,space_cache,subvolid=5,subvol=/)

Generate Test Files

cd /dev/sdd1
sudo mkdir stuff
sudo chown johnb stuff
sudo chgrp johnb stuff
cd stuff
dd bs=1024 count=10 if=/dev/zero    of=zero
dd bs=1024 count=10 if=/dev/urandom of=random
ps ax > text

Verify Compression Ratio

sudo apt install btrfs-compsize
for f in *; do echo; echo "$f:" ; sudo compsize $f; done

random:
Type       Perc     Disk Usage   Uncompressed Referenced  
TOTAL      100%       12K          12K          12K       
none       100%       12K          12K          12K       

text:
Type       Perc     Disk Usage   Uncompressed Referenced  
TOTAL       17%       12K          68K          68K       
zstd        17%       12K          68K          68K       

zero:
Type       Perc     Disk Usage   Uncompressed Referenced  
TOTAL       33%      4.0K          12K          12K       
zstd        33%      4.0K          12K          12K

Btrfs Big Picture (1)

For a big picture view of used and free storage:

sudo btrfs filesystem usage  /mnt

Overall:
    Device size:          14.58GiB
    Device allocated:        536.00MiB
    Device unallocated:       14.06GiB
    Device missing:          0.00B
    Used:            348.00KiB
    Free (estimated):         14.07GiB  (min: 7.04GiB)
    Data ratio:               1.00
    Metadata ratio:           2.00
    Global reserve:        3.25MiB  (used: 0.00B)

Data,single: Size:8.00MiB, Used:92.00KiB (1.12%)
   /dev/sdd1       8.00MiB

Btrfs Big Picture (2)

Metadata,DUP: Size:256.00MiB, Used:112.00KiB (0.04%)
   /dev/sdd1     512.00MiB

System,DUP: Size:8.00MiB, Used:16.00KiB (0.20%)
   /dev/sdd1      16.00MiB

Unallocated:
   /dev/sdd1      14.06GiB

Encryption

Btrfs does not include built-in encryption support. Nobody is working to add it. Therefore encrypt the partition before formatting it. Use dm-crypt.

We will use Dm-crypt to LUKS-encrypt an existing partition on /dev/sdd using a plaintext keyfile, unlock it, create a Btrfs file system on it, mount it, write some data, and then unmount it and unmap it.

sudo umount /dev/sdd1 /dev/sdd2
echo -n "MyPassword" > ~/Keyfile.txt
sudo chown root:root ~/Keyfile.txt
sudo chmod 400 ~/Keyfile.txt
sudo cryptsetup luksFormat -s 512 /dev/sdd1 ~/Keyfile.txt
sudo cryptsetup open /dev/sdd1 Mybackup --key-file=/home/johnb/Keyfile.txt
sudo mkfs.btrfs -f -L stick /dev/mapper/Mybackup
sudo mount -o compress=zstd /dev/mapper/Mybackup /mnt/

Generate Test Files

cd /mnt
sudo mkdir stuff
sudo chown johnb:johnb stuff
cd stuff
dd bs=1024 count=10 if=/dev/zero    of=zero
dd bs=1024 count=10 if=/dev/urandom of=random
ps ax > text

Check Compression Ratio

for f in *; do echo; echo "$f:" ; sudo compsize $f; done

random:
Type       Perc     Disk Usage   Uncompressed Referenced  
TOTAL      100%       12K          12K          12K       
none       100%       12K          12K          12K       

text:
Type       Perc     Disk Usage   Uncompressed Referenced  
TOTAL       18%       12K          64K          64K       
zstd        18%       12K          64K          64K       

zero:
Type       Perc     Disk Usage   Uncompressed Referenced  
TOTAL       33%      4.0K          12K          12K       
zstd        33%      4.0K          12K          12K       

Strike The Set

sudo umount /mnt/
sudo cryptsetup close Mybackup

At this point the backup device can be unplugged. If it's plugged back in, /dev/sdd2 automounts but not our encrypted partition. That's manual:

sudo cryptsetup open /dev/sdd1 Mybackup --key-file=/home/johnb/Keyfile.txt
sudo mount -o compress=zstd /dev/mapper/Mybackup /mnt/ 
cd stuff
ls -l
total 88
-rw-rw-r-- 1 johnb johnb 10240 Sep 13 17:02 random
-rw-rw-r-- 1 johnb johnb 64360 Sep 13 17:02 text
-rw-rw-r-- 1 johnb johnb 10240 Sep 13 17:02 zero

Password Prompt

When you insert an encrypted flash drive:

Password Prompt

Deduplication

We want to maximize the amount of data we can place on our Btrfs-formatted backup disk. But we realize that some files contain identical content. They may have different names or be located in different directories. The duplicate files may not even come from a Btrfs file system. The deduplication feature addresses this matter.

Btrfs itself does not do in-band deduplication. That is left to tools that do out-of-band deduplication. There are two kinds. The first is the duplicate file finder. The second is batch deduplication.

dduper is a free, open source fast block-level out-of-band BTRFS deduplication tool. It will do both file-based and block-based deduplication. It compares the existing checksums for each block. To deduplicate a directory recusively:

dduper --device /dev/sda1 --dir /mnt/dir --recurse

File-based deduplication tools

Tool Description
dduper Btrfs only
rmlint Includes Btrfs support. It's in the Ubuntu repository. Install rmlint-gui a.k.a. Shredder.
jdupes Includes Btrfs support. In the Ubuntu repository.
fslint CLI and GUI finds file system lint including duplicate files. However it is long in the tooth and won't build on current systems because it depends on Python2 and Gtk2.
duperemove
btrfs-dedup
bedup
dduper-git

Table: List of File-Based Deduplication Tools

Home Directory Deduplication

In retrospect, it would be sensible to run a file deduplication tool over one's home directory from time to time, just to maintain free working storage. For example, we maintain a folder of wallpaper pictures on an EXT4 filel system. Some of the images are identical but named differently. We will check every subdirectory of Pictures and create symbolic links for all duplicates.

jdupes --recurse --linksoft  ~/Pictures

We had a great many duplicate images in the Pictures directory.

  1. Sometimes we find a wallpaper we like, forget we've previously downloaded it from another source, and download it again under a different name.
  2. Sometimes we don't pay attention when we download a wallpaper and wind up creating a copy with (1) appended to it.
  3. We run Variety an automatic wallpaper changer. It makes a copy of images in its own Wallpapers folder.

Regarding Crypto (1)

Package Description
cryptmount Creating encrypted filesystems & swap partitions and which allows an ordinary user to mount/unmount filesystems without requiring superuser privileges
cryptsetup Interface for configuring encryption on block devices (such as /home or swap partitions), using the Linux kernel device mapper target dm-crypt. It features integrated Linux Unified Key Setup (LUKS) support.

Regarding Crypto (2)

Package Description
tomb A tomb is like a locked folder that can be transported and hidden in filesystems.
pmount pmount is a wrapper around the standard mount program which permits normal users to mount removable devices without a matching /etc/fstab entry. If a LUKS capable cryptsetup package is installed, pmount is able to transparently mount encrypted volumes.

Backup Software

Timeshift takes periodic system snapshots using either Btrfs or Rsync. This lets you roll back a system update to a previous snapshot.

BackIntime takes periodic backups of your home partition.

Note that you want both of these tools because if you break your system via an update, you don't want your home directory rolled back also. And if you've erased work from your home directory you don't want your system rolled back as you recover your working files.

Btrfs Maintenance (1)

TRIM Support

Kernel 5.6 supports Btrfs TRIM support on SSDs. Mount option discard=async.

Defragmentation

To enable Btrfs online defragmentation use the autodefrag mount option. To manually defrag a Btrfs file system:

btrfs filesystem defragment -r /filesystem

File System Check

Start a background scrub of Btrfs. Check its progress:

btrfs scrub  start /filesystem
btrfs scrub status /filesystem

Note that the CRC32 CPU instruction accelerates the Btrfs checksumming feature. To verify it's present:

dmesg | grep crc32c
[    4.093157] Btrfs loaded, crc32c=crc32c-intel

Btrfs Maintenance (2)

Balancing RAID

Balancing makes a pass over the RAID disks to rebalance the data across all of them. It also regenerates missing copies. Recall that RAID 0 and 1 are fully supported.

Convert File System to Btrfs

As of btrfs-tools version 5.7-1 is able to safely convert EXT3/4 file systems to Btrfs. It's recommended to boot from a live CD with said tool installed.

Btrfs Adoption

The following Linux distros support installation into a Btrfs file system:

Btrfs Snapshots

This means that once Linux is installed, taking a Btrfs snapshot preserves the "factory settings." The system can be reverted to that snapshot should it be necessary. Indeed the system administrator can take a snapshot at any later time, perform an update and break the system, revert to the snapshot to recover and still be home in time for supper.

Swap File on Btrfs

Why place a swap file on a Btrfs file system? To take advantage of compression. We can get by with a smaller swap file and I/O goes faster because there's less data to write. Recall too that swap space is used for suspend/hibertnate functionality. However the swap file cannot be placed on a Btrfs file system that spans multiple devices.

Why Not Btrfs

  1. EXT4 is far more mature.
  2. ZFS can do everything. Read the OpenZFS documentation.
  3. ZFS does deduplication.
  4. Btrfs can't reliably do RAID 5 and RAID 6.
  5. It's still not mature enough.

Further Btrfs Studies

The following topics may be studied at the oh-most-excellent Btrfs ArchWiki:

  1. SSD and TRIM
  2. Swap files support as of kernel 5.0. Read How to create a Linux swap file for details setting up a swap file.
  3. Defragmentation via the autodefrag mount option
  4. Deduplication may prove beneficial if the backup utility saves duplicates of identical files.
  5. Btrfs FAQ
  6. Btrfs at the WikipediA
  7. dm-crypt at the Arch Wiki
  8. Btrfs can be used to work with JBOD.
  9. How is error checking done?
  10. Read about btrfs-restore to recover files from a corrupt Btrfs file system.

About This Document

This is a Jupyter Notebook named Regarding_Btrfs_Slides.ipynb. Convert it to a PDF slide set:

pandoc Regarding_Btrfs_Slides.ipynb -t beamer \
       -o Regarding_Btrfs_slides.pdf

Everything in this Juyter notebook is written in Markdown so the entirety of its text can be transfered perfectly to any writing tool that also understands plain text. For example Menu → File → Download as → Markdown (.md) → Regarding_Btrfs_Slid.md. Or on the command line:

jupyter nbconvert --to markdown  Regarding_Btrfs_Slides.ipynb