rss logo

My Personal Notes on the Btrfs File System

Tux logo

Btrfs is a modern file system that provides advanced features such as transparent compression, snapshots, and online volume resizing. It is increasingly adopted in GNU/Linux distributions. This page summarizes the key commands you need to work effectively with Btrfs.

Extend Partition Size

⚠️ Warning: To avoid data loss, make sure to back up your data before performing any resizing operation. Also, ensure you fully understand each command before executing it. ⚠️

  • Let's assume we are starting with the following disk configuration:
Schematic view of a GNU/Linux disk partition layout before resizing using Btrfs
  • First, we need to adjust the partition boundaries:
root@host:~# gdisk /dev/sda

Command (? for help): d
Partition number (1-3): 3

Command (? for help): n
Partition number (3-128, default 3): 3
First sector (34-125829086, default = 3147776) or {+-}size{KMGTP}: 3147776
Last sector (3147776-125829086, default = 167772126) or {+-}size{KMGTP}: 
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300): 
Changed type of partition to 'Linux filesystem'

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): Y
OK; writing new GUID partition table (GPT) to /dev/sda.
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot or after you
run partprobe(8) or kpartx(8)
The operation has completed successfully.
root@host:~# partprobe
  • Extend the file system to use the maximum available space on the partition:
root@host:~# btrfs filesystem resize max /
  • You can also shrink or extend the file system to a specific size:
root@host:~# btrfs filesystem resize -1G /data/

Commands

Scrub

  • Start an online filesystem check (scrub):
root@host:~# btrfs scrub start /
  • Check the status of an ongoing or completed scrub:
root@host:~# btrfs scrub status /

Subvolumes

  • Subvolumes offer several benefits:
    • Isolation: logically separate different areas of the filesystem
    • Snapshots: each subvolume can be snapshotted independently
    • Independent mount points: subvolumes can be mounted at custom locations
    • Quotas: disk usage limits can be set per subvolume
  • Graphical representation of Btrfs subvolumes:
Diagram showing Btrfs subvolumes and their hierarchical structure on a GNU/Linux system
  • Create a subvolume:
root@host:~# btrfs subvolume create /home/SUBV
  • List all subvolumes:
root@host:~# btrfs subvolume list /
  • Mount a subvolume:
root@host:~# mount /dev/sda3 -o subvol=SUBV /home/SUBV/

Misc

  • Format a partition with the Btrfs file system:
root@host:~# mkfs.btrfs /dev/sdb1
  • Start an online defragmentation:
root@host:~# btrfs filesystem defragment -r -v /
  • Display detailed internal file system usage:
root@host:~# btrfs filesystem usage /
  • Summarize disk usage of specific file:
root@host:~# btrfs filesystem du /home/user/file
  • Show space usage information for the mount point:
root@host:~# btrfs filesystem df /
  • Display device IO error statistics:
root@host:~# btrfs dev stats /
  • Show file system and device information (UUID, space used, etc…):
root@host:~# btrfs filesystem show /

Snapshots

  • List subvolumes and snapshots in the root file system:
root@host:~# btrfs subvolume list /
  • Create a snapshot of the root directory:
root@host:~# mkdir /.snapshots
root@host:~# btrfs subvolume snapshot -r / /.snapshots/\@$(date +%Y.%m.%d)
  • Delete a snapshot:
root@host:~# btrfs subvolume delete /.snapshots/@2024.05.04/

Compression

  • Enable automatic compression via /etc/fstab:
UUID=4e39e49c-6934-41f5-97fd-cb7c699a78a5 /               btrfs   defaults,compress=zstd,subvol=@rootfs 0       0
  • Manually compress files in a specific folder:
root@host:~# btrfs filesystem defragment -r -v -czstd /home/user/

References