Btrfs Commands Guide
Btrfs Filesystem Management
Btrfs (B-tree file system) is a modern copy-on-write (CoW) filesystem for Linux. It offers advanced features such as snapshots, checksums, built-in RAID support, and subvolume management. This guide provides essential Btrfs commands to help you manage your Btrfs filesystems effectively.
Creating Btrfs Filesystems
The mkfs.btrfs command is used to create a new Btrfs
filesystem. You can specify different RAID levels for data and
metadata to ensure redundancy.
# Create a btrfs file system on /dev/sdb, /dev/sdc, and /dev/sdd
mkfs.btrfs /dev/sdb /dev/sdc /dev/sdd
# btrfs with just one hard drive, metadata not redundant
# (this is dangerous: if your metadata is lost, your data is lost as well)
mkfs.btrfs -m single /dev/sdb
# data to be redundant and metadata to be non-redundant:
mkfs.btrfs -m raid0 -d raid1 /dev/sdb /dev/sdc /dev/sdd
# both data and metadata to be redundant
mkfs.btrfs -d raid1 /dev/sdb /dev/sdc /dev/sdd
Filesystem Information and Resizing
View filesystem details and resize your Btrfs volumes online.
# To get a list of all btrfs file systems
btrfs filesystem show
# detailed df for a filesystem (mounted in /mnt)
btrfs filesystem df /mnt
# resize btrfs online (-2g decreases, +2g increases)
btrfs filesystem resize -2g /mnt
# use maximum space
btrfs filesystem resize max /mnt
Device Management
Add or remove devices from an existing Btrfs filesystem.
# add new device to a filesystem
btrfs device add /dev/sdf /mnt
# remove devices from a filesystem
btrfs device delete missing /mnt
Subvolume Management
Subvolumes are like independent filesystem trees within a Btrfs filesystem. They are crucial for features like snapshots.
# create the subvolume /mnt/sv1 in the /mnt volume
btrfs subvolume create /mnt/sv1
# list subvolumes
btrfs subvolume list /mnt
# mount subvolume without mounting the main filesystem
mount -o subvol=sv1 /dev/sdb /mnt
# delete subvolume
btrfs subvolume delete /mnt/sv1
Snapshots and Copy-on-Write
Btrfs snapshots allow you to create read-only or writable copies of subvolumes at a specific point in time. Copy-on-write (CoW) is a fundamental feature that enables efficient snapshots and data integrity.
# taking snapshot of a subvolume
btrfs subvolume snapshot /mnt/sv1 /mnt/sv1_snapshot
# taking snapshot of a file (copy file by reference)
cp --reflink /mnt/sv1/test1 /mnt/sv1/test3
Filesystem Conversion
Convert existing filesystems to Btrfs or convert Btrfs back to traditional Linux filesystems.
# convert ext3/ext4 to btrfs
btrfs-convert /dev/sdb1
# convert btrfs to ext3/ext4
btrfs-convert -r /dev/sdb1