12.4. Administering Btrfs

Btrfs stores data and metadata in a copy-on-write B-tree. A filesystem contains one or more subvolumes, each an independent tree with its own inode namespace. Subvolumes share the same pool of devices and the same free space, but a snapshot of one subvolume is independent of the live tree. Btrfs checksums every data and metadata block, can compress writes, and can mirror data across devices with RAID1-style profiles. Its administration is therefore less about tuning a single superblock and more about arranging subvolumes, snapshots, and device profiles.

12.4.1. Subvolumes, Default Subvolumes, and Mount Layouts

The second practice partition is used only for this exercise. The mkfs.btrfs -f command overwrites any existing signature; confirm the device first, and note that -L sets the label. Btrfs prints its feature set and initial block-group profiles:

# mkfs.btrfs -f -L handbook-btrfs /dev/vdb2
btrfs-progs v6.14
See https://btrfs.readthedocs.io for more information.

Performing full device TRIM /dev/vdb2 (2.15GiB) ...
NOTE: several default settings have changed in version 5.15, please make sure
      this does not affect your deployments:
      - DUP for metadata (-m dup)
      - enabled no-holes (-O no-holes)
      - enabled free-space-tree (-R free-space-tree)

Label:              handbook-btrfs
UUID:               ef5cbaf2-e010-4311-a625-7ae2a3b30084
Node size:          16384
Sector size:        4096    (CPU page size: 4096)
Filesystem size:    2.15GiB
Block group profiles:
  Data:             single            8.00MiB
  Metadata:         DUP             110.00MiB
  System:           DUP               8.00MiB
SSD detected:       no
Zoned device:       no
Features:           extref, skinny-metadata, no-holes, free-space-tree
Checksum:           crc32c
Number of devices:  1
Devices:
   ID        SIZE  PATH     
    1     2.15GiB  /dev/vdb2

Metadata: DUP means Btrfs stores two copies of metadata on the same device; this is not RAID1, because a device failure still loses both copies. Data: single stores one copy. The Checksum: crc32c line means data blocks are checksummed; no-holes records sparse extents efficiently; free-space-tree keeps free-space accounting in a B-tree.

mkfs.btrfs option Description
-L LABEL Set the filesystem label.
-f Force creation over an existing signature.
-d PROFILE Set the initial data profile (single, dup, raid1, ...).
-m PROFILE Set the initial metadata profile.
-O FEATURE[,^FEATURE] Enable or disable filesystem features at creation.
-n SIZE Set the node size.
-q Suppress the creation report.

Mount the top-level filesystem first, create two subvolumes, and list them. The top-level subvolume has ID 5 and is normally hidden from users after a default subvolume is set:

# mkdir -p /mnt/btrfs /mnt/btrfs-snapshots
# mount /dev/vdb2 /mnt/btrfs

Both commands are silent. Create the two subvolumes:

# btrfs subvolume create /mnt/btrfs/@data
Create subvolume '/mnt/btrfs/@data'
# btrfs subvolume create /mnt/btrfs/@snapshots
Create subvolume '/mnt/btrfs/@snapshots'
# btrfs subvolume list /mnt/btrfs
ID 256 gen 9 top level 5 path @data
ID 257 gen 9 top level 5 path @snapshots

@data is ID 256 and @snapshots is ID 257; both are children of the top-level subvolume (ID 5). The @ names are a convention, not a kernel requirement. They make the mount layout explicit and distinguish a subvolume used as a mount root from one used to hold snapshots.

Show the details of @data, including its ID and flags:

# btrfs subvolume show /mnt/btrfs/@data
@data
    Name:           @data
    UUID:           701789f6-4275-d54e-a9bc-0304b48d2bb3
    Parent UUID:        -
    Received UUID:      -
    Creation time:      2026-11-20 07:33:27 +0000
    Subvolume ID:       256
    Generation:         9
    Gen at creation:    9
    Parent ID:      5
    Top level ID:       5
    Flags:          -
    Send transid:       0
    Send time:      2026-11-20 07:33:27 +0000
    Receive transid:    0
    Receive time:       -
    Snapshot(s):
    Quota group:        n/a

Set @data as the default subvolume by ID. The ID is the one printed by btrfs subvolume list; it is 256 on this fresh filesystem. The change takes effect on the next mount:

# btrfs subvolume set-default 256 /mnt/btrfs
# umount /mnt/btrfs
# mount /dev/vdb2 /mnt/btrfs

All three commands are silent. findmnt shows that the visible root is now @data, even though the device itself is unchanged:

# findmnt /mnt/btrfs
TARGET     SOURCE            FSTYPE OPTIONS
/mnt/btrfs /dev/vdb2[/@data] btrfs  rw,relatime,discard=async,space_cache=v2,subvolid=256,subvol=/@data

The bracket notation in SOURCE is the subvolume path inside the filesystem. A mount with no subvol= or subvolid= option uses the default subvolume. To reach the snapshots subvolume, create a directory in the default tree and mount @snapshots there:

# mkdir -p /mnt/btrfs/snapshots
# mount -o subvol=@snapshots /dev/vdb2 /mnt/btrfs-snapshots

Both commands are silent. Confirm the subvolume mount:

# findmnt /mnt/btrfs-snapshots
TARGET               SOURCE                 FSTYPE OPTIONS
/mnt/btrfs-snapshots /dev/vdb2[/@snapshots] btrfs  rw,relatime,discard=async,space_cache=v2,subvolid=257,subvol=/@snapshots

The same filesystem appears at two mount points, each exposing a different subvolume. The top-level subvolume remains reachable with -o subvolid=5; that is useful for maintenance and for creating or deleting subvolumes, but it should not be the normal user-visible root.

A Btrfs filesystem with a top-level subvolume, a default @data subvolume, an @snapshots subvolume, and a read-only snapshot sharing extents with the live tree.

Figure 12.3. Btrfs subvolumes are separate roots in one filesystem. The default subvolume determines what a plain mount shows; snapshots appear as additional subvolumes under @snapshots.

Mount option Description
subvol=PATH Mount the named subvolume as the root of the mount.
subvolid=ID Mount by numeric subvolume ID; 5 is the top level.
compress=zstd[:LEVEL] Compress new writes (or zlib, lzo; level where supported).
compress-force=zstd Compress even when the heuristic would skip an extent.
noatime Do not update access times on every read.
space_cache=v2 Use the free-space tree (the modern default).
discard=async Queue discard requests asynchronously.
autodefrag Defragment small random writes in the background; workload-dependent.
skip_balance Do not resume an interrupted balance on mount.

The mount layout is a policy decision. A common arrangement is one subvolume for the root filesystem, one for /home, and one for snapshots, each mounted with subvol=. Keeping /home in a separate subvolume lets a root snapshot omit user data, and lets a home snapshot be managed independently. A single @data subvolume is simpler and is what the rest of this chapter uses.

12.4.2. Snapshots, Writable Clones, and Retention

A snapshot is a subvolume whose initial tree points at the same extents as its source. btrfs subvolume snapshot -r creates a read-only snapshot; without -r the snapshot is writable. Create a project file as john and take a read-only snapshot into the snapshots subvolume:

# mkdir -p /mnt/btrfs/projects
# chown john:john /mnt/btrfs/projects

Both commands are silent. As john:

$ printf "%s\n" "version one" > /mnt/btrfs/projects/plan.txt

The write is silent. Take the snapshot as root:

# btrfs subvolume snapshot -r /mnt/btrfs /mnt/btrfs-snapshots/plan-2026-11-20
Create readonly snapshot of '/mnt/btrfs' in '/mnt/btrfs-snapshots/plan-2026-11-20'

The snapshot is itself a subvolume under @snapshots:

# btrfs subvolume list /mnt/btrfs
ID 256 gen 10 top level 5 path @data
ID 257 gen 10 top level 5 path @snapshots
ID 258 gen 10 top level 257 path @snapshots/plan-2026-11-20

btrfs subvolume show confirms the read-only flag and records the parent UUID. Selected lines:

# btrfs subvolume show /mnt/btrfs-snapshots/plan-2026-11-20
@snapshots/plan-2026-11-20
    Name:           plan-2026-11-20
    UUID:           76797c89-4dae-e842-bc42-d752471dc151
    Parent UUID:        701789f6-4275-d54e-a9bc-0304b48d2bb3
    Subvolume ID:       258
    Parent ID:      257
    Flags:          readonly

The parent UUID matches the @data UUID from Section 12.4.1. Now change the live file and read both versions:

$ printf "%s\n" "version two" > /mnt/btrfs/projects/plan.txt

The write is silent. The snapshot still contains the old content:

$ cat /mnt/btrfs-snapshots/plan-2026-11-20/projects/plan.txt
version one

The live tree contains the new content:

$ cat /mnt/btrfs/projects/plan.txt
version two

A writable snapshot is a clone. Changes to the clone are independent of both the source and the read-only snapshot:

# btrfs subvolume snapshot /mnt/btrfs /mnt/btrfs-snapshots/plan-working
Create snapshot of '/mnt/btrfs' in '/mnt/btrfs-snapshots/plan-working'

As john, edit the clone:

$ printf "%s\n" "clone edit" > /mnt/btrfs-snapshots/plan-working/projects/plan.txt

The write is silent. Read the clone:

$ cat /mnt/btrfs-snapshots/plan-working/projects/plan.txt
clone edit

The live tree is unchanged:

$ cat /mnt/btrfs/projects/plan.txt
version two

The clone and the live tree now have different content. Deleting a snapshot removes its root, but the data blocks are freed in the background. btrfs subvolume sync waits for the deletion to finish:

# btrfs subvolume delete /mnt/btrfs-snapshots/plan-working
Delete subvolume 259 (no-commit): '/mnt/btrfs-snapshots/plan-working'

Wait for the background deletion to finish:

# btrfs subvolume sync /mnt/btrfs-snapshots
Waiting for 1 subvolume
Subvolume id 259 is gone (1/1)

A later btrfs subvolume list no longer shows plan-working:

# btrfs subvolume list /mnt/btrfs
ID 256 gen 11 top level 5 path @data
ID 257 gen 12 top level 5 path @snapshots
ID 258 gen 10 top level 257 path @snapshots/plan-2026-11-20
btrfs subvolume command Description
create PATH Create a new subvolume.
snapshot [-r] SRC DEST Create a writable snapshot, or a read-only one with -r.
list PATH List subvolumes with ID, generation, parent, and path.
show PATH Show UUID, parent, creation time, flags, and related snapshots.
set-default ID PATH Make a subvolume the default for plain mounts.
delete PATH Delete a subvolume; data is reclaimed asynchronously.
sync PATH Wait for pending subvolume deletions.

Snapshots are not backups. They share blocks with the live tree, so a device error or an accidental write to the underlying device can damage both. They are useful for short-term rollback, for taking a consistent view before a risky change, and as the source of btrfs send streams for off-site copies. Chapter 15 covers backup retention and remote copies. A snapshot on the same device does not protect against losing that device.

Retention is an administrative policy, not a filesystem feature. Keep snapshots with meaningful names, remove them with btrfs subvolume delete, and wait for deletion with btrfs subvolume sync when space reclamation matters. btrfs subvolume list shows the current set; a naming scheme such as plan-2026-11-20 makes the snapshot's purpose and date visible without a separate database.

12.4.3. Compression, Checksums, and Scrubbing

Btrfs can compress data on write. The mount option applies to new writes; existing extents keep their current compression until rewritten or defragmented. Remount the default subvolume with zstd compression at level 3:

# mount -o remount,compress=zstd:3 /mnt/btrfs

The remount is silent. Confirm the option:

# findmnt -no OPTIONS /mnt/btrfs
rw,relatime,compress=zstd:3,discard=async,space_cache=v2,subvolid=256,subvol=/@data

As john, write a 32 MiB file of zeros. The status=none option suppresses dd's transfer report:

$ dd if=/dev/zero of=/mnt/btrfs/projects/zeros.bin bs=1M count=32 status=none

The write is silent. sync completes the writeback so the compressed extents exist before they are inspected:

# sync

The command prints nothing. du and du --apparent-size both report 32 MiB, because Btrfs records the uncompressed allocated size in the inode's block count:

$ du -h /mnt/btrfs/projects/zeros.bin
32M /mnt/btrfs/projects/zeros.bin
$ du -h --apparent-size /mnt/btrfs/projects/zeros.bin
32M /mnt/btrfs/projects/zeros.bin

compsize reads the extent metadata and reports the physical compressed size:

# compsize /mnt/btrfs/projects/zeros.bin
Processed 1 file, 256 regular extents (256 refs), 0 inline.
Type       Perc     Disk Usage   Uncompressed Referenced  
TOTAL        3%      1.0M          32M          32M       
zstd         3%      1.0M          32M          32M       

The file occupies about 1 MiB on disk for 32 MiB of logical content. filefrag -v marks each extent encoded, which indicates compression. Selected output; extents 1 through 254 are omitted:

# filefrag -v /mnt/btrfs/projects/zeros.bin
Filesystem type is: 9123683e
File size of /mnt/btrfs/projects/zeros.bin is 33554432 (8192 blocks of 4096 bytes)
 ext:     logical_offset:        physical_offset: length:   expected: flags:
   0:        0..      31:       3328..      3359:     32:             encoded
...
 255:     8160..    8191:       3583..      3614:     32:       3614: last,encoded,eof
/mnt/btrfs/projects/zeros.bin: 256 extents found

Compression split the file into 256 extents of 32 blocks each - 128 KiB of logical data per extent, stored in one compressed physical extent. The expected column repeats the previous extent's end because the compressed extents are not logically contiguous on disk.

btrfs filesystem usage gives the whole-filesystem view. The Data line reports physical usage, which is far below the 32 MiB logical file. Selected lines; the per-device chunk lines are omitted:

# btrfs filesystem usage /mnt/btrfs
Overall:
    Device size:           2.15GiB
    Device allocated:        468.00MiB
    Device unallocated:        1.69GiB
    Device missing:          0.00B
    Device slack:            0.00B
    Used:              1.50MiB
    Free (estimated):          1.92GiB  (min: 1.07GiB)
    Free (statfs, df):         1.92GiB
    Data ratio:               1.00
    Metadata ratio:           2.00
    Global reserve:        5.50MiB  (used: 0.00B)
    Multiple profiles:              no

Data,single: Size:232.00MiB, Used:1.00MiB (0.43%)
Metadata,DUP: Size:110.00MiB, Used:240.00KiB (0.21%)
System,DUP: Size:8.00MiB, Used:16.00KiB (0.20%)

The difference between du and btrfs filesystem usage is not a bug: du reports logical allocation, while Btrfs's own accounting reports physical chunk usage. Use compsize for per-file compression and btrfs filesystem usage for capacity planning.

Checksums make scrub possible. Scrub reads every data and metadata block, verifies its checksum, and on a redundant profile repairs a bad copy from a good one. Start a blocking scrub of the mounted filesystem:

# btrfs scrub start -B /mnt/btrfs
Starting scrub on devid 1
scrub done for ef5cbaf2-e010-4311-a625-7ae2a3b30084
Scrub started:    Fri Nov 20 07:35:06 2026
Status:           finished
Duration:         0:00:00
Total to scrub:   1.50MiB
Rate:             1.50MiB/s
Error summary:    no errors found

btrfs scrub status reads the recorded status later:

# btrfs scrub status /mnt/btrfs
UUID:             ef5cbaf2-e010-4311-a625-7ae2a3b30084
Scrub started:    Fri Nov 20 07:35:06 2026
Status:           finished
Duration:         0:00:00
Total to scrub:   1.52MiB
Rate:             1.50MiB/s
Error summary:    no errors found

Scrub is not a filesystem checker. It validates checksums and can repair from another replica; it cannot rebuild a broken tree. Files marked NOCOW are not protected by data checksums, so scrub cannot verify or repair their contents from a checksum mismatch. Use btrfs check offline for structural checking (Section 12.5.4). A monthly scrub is a reasonable default; on a busy server, schedule it outside peak I/O. The scrub status is recorded in /var/lib/btrfs/, so an interrupted scrub can resume from its last position.

12.4.4. Device Profiles, Balance, and Space Accounting

A Btrfs filesystem can span multiple devices. Adding a device does not redistribute existing blocks; a balance converts block groups to a new profile. Add the 2 GiB second disk, then convert data and metadata to RAID1:

# btrfs device add -f /dev/vdc /mnt/btrfs
Performing full device TRIM /dev/vdc (2.00GiB) ...

Show the device set:

# btrfs filesystem show /mnt/btrfs
Label: 'handbook-btrfs'  uuid: ef5cbaf2-e010-4311-a625-7ae2a3b30084
    Total devices 2 FS bytes used 1.25MiB
    devid    1 size 2.15GiB used 532.00MiB path /dev/vdb2
    devid    2 size 2.00GiB used 0.00B path /dev/vdc

The new device is registered but has no allocated chunks. Balance with the convert filter:

# btrfs balance start -dconvert=raid1 -mconvert=raid1 /mnt/btrfs
Done, had to relocate 5 out of 5 chunks

btrfs filesystem usage now reports two devices, RAID1 profiles, and a data ratio of 2.00. Selected output; the Unallocated per-device summary at the end is omitted:

# btrfs filesystem usage /mnt/btrfs
Overall:
    Device size:           4.15GiB
    Device allocated:          2.75GiB
    Device unallocated:        1.40GiB
    Device missing:          0.00B
    Device slack:            0.00B
    Used:              2.50MiB
    Free (estimated):          1.54GiB  (min: 1.54GiB)
    Free (statfs, df):         1.47GiB
    Data ratio:               2.00
    Metadata ratio:           2.00
    Global reserve:        5.50MiB  (used: 0.00B)
    Multiple profiles:              no

Data,RAID1: Size:864.00MiB, Used:1.00MiB (0.12%)
   /dev/vdb2     864.00MiB
   /dev/vdc  864.00MiB

Metadata,RAID1: Size:512.00MiB, Used:240.00KiB (0.05%)
   /dev/vdb2     512.00MiB
   /dev/vdc  512.00MiB

System,RAID1: Size:32.00MiB, Used:16.00KiB (0.05%)
   /dev/vdb2      32.00MiB
   /dev/vdc   32.00MiB

The size of each RAID1 block group is doubled in the accounting: 864 MiB of data chunks means 864 MiB on each of the two devices, and the 2.00 data ratio records that. btrfs device usage shows the same allocation from the device side. Each RAID1 chunk consumes one copy on each device:

# btrfs device usage /mnt/btrfs
/dev/vdb2, ID: 1
   Device size:             2.15GiB
   Device slack:              0.00B
   Data,RAID1:            864.00MiB
   Metadata,RAID1:        512.00MiB
   System,RAID1:           32.00MiB
   Unallocated:           792.00MiB

/dev/vdc, ID: 2
   Device size:             2.00GiB
   Device slack:              0.00B
   Data,RAID1:            864.00MiB
   Metadata,RAID1:        512.00MiB
   System,RAID1:           32.00MiB
   Unallocated:           640.00MiB

A RAID1 profile needs at least two devices with enough space. Removing one device while the profile still requires two is refused:

# btrfs device remove /dev/vdc /mnt/btrfs
ERROR: error removing device '/dev/vdc': unable to go below two devices on raid1

With RAID1 active, scrub can repair a damaged copy. This controlled exercise corrupts one 4 KiB block on /dev/vdc and lets scrub recover it from /dev/vdb2. Create a test file as john, record its hash, and ask filefrag for the logical extent:

$ dd if=/dev/urandom of=/mnt/btrfs/projects/scrub-test.bin bs=1M count=8 status=none

The write is silent. The inspections below read the extent tree, so let the writeback finish first:

# sync

The command prints nothing. Record the hash:

$ sha256sum /mnt/btrfs/projects/scrub-test.bin
f4ed310f7b36ad67641576b0759208a18b5e4f072ca4b14eb1dd9456c6d3ce5c  /mnt/btrfs/projects/scrub-test.bin

As root, filefrag reports the logical extent's starting block (350976 here). btrfs-map-logical translates that logical address to the physical offset of each mirror:

# filefrag -v /mnt/btrfs/projects/scrub-test.bin
Filesystem type is: 9123683e
File size of /mnt/btrfs/projects/scrub-test.bin is 8388608 (2048 blocks of 4096 bytes)
 ext:     logical_offset:        physical_offset: length:   expected: flags:
   0:        0..    2047:     350976..    353023:   2048:             last,eof
/mnt/btrfs/projects/scrub-test.bin: 1 extent found

Map the logical address:

# btrfs-map-logical -l 1437597696 -b 4096 /dev/vdc
mirror 1 logical 1437597696 physical 1024458752 device /dev/vdc
mirror 2 logical 1437597696 physical 1594884096 device /dev/vdb2

The logical byte address is 350976 × 4096 = 1437597696. Physical offset 1024458752 on /dev/vdc is the first copy. dd positions its write with seek, which counts blocks of bs=, so the value below is 1024458752 ÷ 4096 = 250112; substitute the offset your own run printed. Overwrite one block there with zeros and flush the device cache:

# dd if=/dev/zero of=/dev/vdc bs=4096 seek=250112 count=1 conv=notrunc oflag=direct status=none
# blockdev --flushbufs /dev/vdc

Both commands are silent. oflag=direct performs the write without the page cache, and blockdev --flushbufs drops anything still cached for /dev/vdc, so the next read of the device sees the zeros. A scrub now reads both mirrors, detects the checksum mismatch on /dev/vdc, and rewrites it from /dev/vdb2:

# btrfs scrub start -B /mnt/btrfs
Starting scrub on devid 1
Starting scrub on devid 2
scrub done for ef5cbaf2-e010-4311-a625-7ae2a3b30084
Scrub started:    Fri Nov 20 07:35:40 2026
Status:           finished
Duration:         0:00:00
Total to scrub:   18.50MiB
Rate:             18.50MiB/s
Error summary:    csum=1
  Corrected:      1
  Uncorrectable:  0
  Unverified:     0
WARNING: errors detected during scrubbing, 1 corrected

The last line is a warning on standard error; the counts above it are on standard output. The hash after the repair matches the recorded one:

$ sha256sum /mnt/btrfs/projects/scrub-test.bin
f4ed310f7b36ad67641576b0759208a18b5e4f072ca4b14eb1dd9456c6d3ce5c  /mnt/btrfs/projects/scrub-test.bin

The data never changed on the surviving mirror, so the repaired copy restored the original content. A single-device filesystem would report Uncorrectable: 1 instead, because there is no good mirror. That is why checksums and redundancy are separate features: checksums detect corruption, and a second copy survives a device-level loss.

Returning to the device removal: the error is a profile constraint, not an I/O failure. To remove the second device, convert the block groups to single first; the conversion reduces metadata redundancy, so it requires -f. Then remove the device:

# btrfs balance start -f -dconvert=single -mconvert=single /mnt/btrfs
Done, had to relocate 6 out of 6 chunks

Remove the now-unused device. It is silent when successful:

# btrfs device remove /dev/vdc /mnt/btrfs

Show the remaining device:

# btrfs filesystem show /mnt/btrfs
Label: 'handbook-btrfs'  uuid: ef5cbaf2-e010-4311-a625-7ae2a3b30084
    Total devices 1 FS bytes used 9.27MiB
    devid    1 size 2.15GiB used 944.00MiB path /dev/vdb2

Balance moves data; it is I/O intensive and needs free workspace. A balance that runs out of space can leave the filesystem in a partially converted state; the operation is restartable, but the free-space calculation is easier if the filesystem is not nearly full. Balance preserves reflinks and snapshots; it does not defragment file extents. btrfs balance start without filters moves every chunk and can take a long time; the warning and delay exist for that reason.

Command Purpose
btrfs device add DEVICE PATH Add a device; existing data is not rebalanced.
btrfs device remove DEVICE PATH Remove a device after its data is relocated; profile constraints apply.
btrfs device usage PATH Show per-device chunk allocation.
btrfs balance start -dconvert=PROFILE -mconvert=PROFILE PATH Convert data and metadata block-group profiles.
btrfs balance start -f ... Allow a conversion that reduces metadata redundancy.
btrfs balance pause, resume, cancel Control a running balance.
btrfs filesystem usage PATH Show device, allocated, used, free, and profile ratios.