rss logo

My personal notes on the Btrfs file system

Tux logo

Btrfs is a relatively new file system which offers a number of features such as transparent compression, snapshots, online volume resizing, etc… It is increasingly used in GNU/Linux distributions. Here, I'll outline the main commands you need to know when using this file system.

Extend Partition Size

⚠️To avoid data loss, ensure to back up your data before starting any resizing task. And make sure you understand the commands before entering them.⚠️

  • Let's assume we have this type of disk configuration:
GNU/Linux | Disk partition scheme
  • First, we need to reconfigure the disk 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 size to the maximum size available on the partition:
root@host:~# btrfs filesystem resize max /
  • We can also reduce or extend the file system to a specific size:
root@host:~# btrfs filesystem resize -1G /data/

Commands

Scrub

  • Performs an online file system check:
root@host:~# btrfs scrub start /
  • Check status of running scrub:
root@host:~# btrfs scrub status /

Subvolumes

  • Subvolumes provide:
    • Isolation: Subvolumes can be used to isolate different parts of your file system
    • Snapshots: Subvolumes can be independently snapshotted
    • Separate mount point: Subvolumes can be mounted independently at different points in the file system hierarchy.
    • Quotas: Btrfs lets you set quotas on subvolumes
  • Graphical view of Btrfs subvolumes:
Disk partition scheme showing Btrfs subvolumes
  • Create a subvolume:
root@host:~# btrfs subvolume create /home/SUBV
  • List subvolumes:
root@host:~# btrfs subvolume list /
  • Mount subvolume:
root@host:~# mount /dev/sda3 -o subvol=SUBV /home/SUBV/

Misc

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

Snaphots

  • List subvolumes and snapshots in the root node:
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)
  • Deleting a snapshot:
root@host:~# btrfs subvolume delete /.snapshots/@2024.05.04/

Compression

  • Modify the /etc/fstab file so that each new file is automatically taken into account for compression:
UUID=4e39e49c-6934-41f5-97fd-cb7c699a78a5 / btrfs defaults,compress=zstd,subvol=@rootfs 0 0
  • Compressing files in a specific folder:
root@host:~# btrfs filesystem defragment -r -v -czstd /home/user/

References

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Contact :

contact mail address