Btrfs (B-tree File System) is a modern GPL-licensed Copy-on-Write (CoW) file system for Linux aimed at implementing advanced features while focusing on fault tolerance, repair, and easy administration. It is designed to address the lack of pooling, snapshots, checksums, and integral multi-device spanning in older Linux file systems.
Developed by Oracle, Red Hat, Intel, and others, it supports large storage capacities, subvolumes, snapshots, compression, and built-in RAID.
Core Features#
- Copy-on-Write (CoW): When data is modified, it is written to a new location rather than overwriting the old data. This prevents data corruption during power losses.
- Subvolumes: Essentially independent file trees within the main file system. They look like normal directories but can be mounted separately and snapshot independently.
- Snapshots: Instantaneous, read-only (or read-write) point-in-time copies of a subvolume, made possible by CoW.
- Checksumming: Both data and metadata are checksummed (typically using CRC32C) to detect silent data corruption.
- Self-Healing: If a checksum fails and the system is in a RAID configuration, Btrfs will automatically fetch a good copy of the data from another drive.
Important Commands Reference#
Btrfs administration is centralized under the btrfs command-line utility.
1. File System Creation & Info#
Commands used for formatting drives and checking general file system health.
- Format a drive:
bashmkfs.btrfs /dev/sdX - Format multiple drives (e.g., RAID 1 for data and metadata):
bashmkfs.btrfs -m raid1 -d raid1 /dev/sdX /dev/sdY - Show file system usage:
bashbtrfs filesystem df /mount/point - Show overall Btrfs disk usage and device info:
bashbtrfs filesystem show
2. Subvolumes#
Subvolumes are the building blocks of a Btrfs deployment.
-
Create a subvolume:
bashbtrfs subvolume create /mount/point/subvol_name -
Delete a subvolume:
bashbtrfs subvolume delete /mount/point/subvol_name -
List all subvolumes:
bashbtrfs subvolume list /mount/pointMounting Subvolumes To mount a specific subvolume via/etc/fstab, use its ID or name:mount -o subvol=subvol_name /dev/sdX /mnt/target.
3. Snapshots#
Because of CoW, snapshots take up almost zero space initially. They only grow as the original data changes.
- Create a snapshot:
bashbtrfs subvolume snapshot /mount/point/source_subvol /mount/point/snapshot_name - Create a read-only snapshot (ideal for backups):
bashbtrfs subvolume snapshot -r /mount/point/source_subvol /mount/point/snapshot_name
4. Device Management#
Btrfs allows you to add or remove drives on the fly without unmounting the file system.
- Add a device:
bashbtrfs device add /dev/sdZ /mount/point - Remove a device (Btrfs will safely migrate data off it first):
bashbtrfs device remove /dev/sdX /mount/point
5. Maintenance & Repair#
Routine maintenance is crucial for keeping Btrfs healthy and performant.
- Scrub (Check for and repair data corruption):
bashbtrfs scrub start /mount/point - Check scrub status:
bashbtrfs scrub status /mount/point - Balance (Reallocate data chunks across disks): Useful after adding/removing devices, or to reclaim space from partially empty chunks.
bashbtrfs balance start /mount/point - Defragmentation (Fixes fragmentation caused by CoW):
bashbtrfs filesystem defragment -r /mount/point
DOCS#
https://en.wikipedia.org/wiki/Btrfs ↗
https://docs.kernel.org/filesystems/btrfs.html ↗
https://btrfs.readthedocs.io/en/latest/ ↗