Linux btrfs Command
Btrfs (B-tree File System) is an advanced Linux filesystem developed by Oracle and first released in 2007. It was designed to overcome the limitations of traditional filesystems, providing better scalability, reliability, and management features.
Core Features
- Copy-on-Write (CoW): All write operations do not overwrite existing data; instead, they create new copies
- Snapshot capability: You can quickly create instant snapshots of the filesystem, taking up almost no extra space
- Subvolume management: Supports dividing the filesystem into multiple independent subvolumes
- Data checksums: Automatically detects data corruption
- Transparent compression: Supports real-time compression and decompression of file data
- RAID support: Built-in support for multiple RAID levels
btrfs basic command syntax
btrfs [选项] [子命令选项] [参数]
Common options
| Option | Description |
|---|---|
-v |
Verbose output mode |
--help |
Display help information |
--version |
Display version information |
Details of major subcommands
Filesystem creation and management
Create a btrfs filesystem
Example
Create a btrfs filesystem on a device
sudo
mkfs.btrfs
/
dev
/
sdX
Create a labeled filesystem
sudo
mkfs.btrfs
-L
mylabel
/
dev
/
sdX
Create RAID1 using multiple devices
sudo
mkfs.btrfs
-m
raid1
-d
raid1
/
dev
/
sdX
/
dev
/
sdY
Mount a btrfs filesystem
Example
Basic mount
sudo
mount
/
dev
/
sdX
/
mnt
/
btrfs
Enable compression (lzo or zstd recommended)
sudo
mount
-o
compress
=lzo
/
dev
/
sdX
/
mnt
/
btrfs
Display mount options
mount
|
grep
btrfs
Subvolume operations
Create a subvolume
Example
Create a subvolume
sudo
btrfs subvolume create
/
mnt
/
btrfs
/
subvol1
List subvolumes
sudo
btrfs subvolume list
/
mnt
/
btrfs
Delete a subvolume
sudo
btrfs subvolume delete
/
mnt
/
btrfs
/
subvol1
Snapshot management
Example
Create a snapshot
sudo
btrfs subvolume snapshot
/
mnt
/
btrfs
/
mnt
/
btrfs
/
snapshot_$
(
date
%
Y
%
m
%
d
)
Read-only snapshot
sudo
btrfs subvolume snapshot
-r
/
mnt
/
btrfs
/
mnt
/
btrfs
/
readonly_snap
Delete a snapshot
sudo
btrfs subvolume delete
/
mnt
/
btrfs
/
snapshot_20230101
Space management
View filesystem usage
Example
Display detailed space information
sudo
btrfs filesystem usage
/
mnt
/
btrfs
Display disk usage
sudo
btrfs filesystem
df
/
mnt
/
btrfs
Balance data distribution (use with caution, may be time-consuming)
sudo
btrfs balance start
/
mnt
/
btrfs
Resize the filesystem
Example
Expand the filesystem
sudo
btrfs filesystem resize +10G
/
mnt
/
btrfs
Shrink the filesystem
sudo
btrfs filesystem resize
-5G
/
mnt
/
btrfs
Data integrity check
Check the filesystem
Example
Read-only check
sudo
btrfs check
/
dev
/
sdX
Attempt repair (use with caution)
sudo
btrfs check
--repair
/
dev
/
sdX
Cleanup and repair
Example
Clean up free space
sudo
btrfs filesystem defrag
/
mnt
/
btrfs
Check and repair the filesystem
sudo
btrfs scrub start
/
mnt
/
btrfs
View repair status
sudo
btrfs scrub status
/
mnt
/
btrfs
Practical Application Examples
Example 1: Setting up a btrfs home directory with compression
Example
Create a partition
sudo
mkfs.btrfs
-L
home
/
dev
/
sdX
Mount and enable compression
sudo
mount
-o
compress
=zstd
/
dev
/
sdX
/
mnt
/
newhome
Copy existing data
sudo
rsync
-avx
/
home
/
/
mnt
/
newhome
/
Update fstab for automatic mounting
echo
"UUID=$(blkid -s UUID -o value /dev/sdX) /home btrfs defaults,compress=zstd 0 2"
|
sudo
tee
-a
/
etc
/
fstab
Remount
sudo
umount
/
mnt
/
newhome
sudo
mount
-a
Example 2: Periodic snapshots and cleanup
Example
#!/bin/bash
Create daily snapshots
SNAP_DIR
=
"/mnt/btrfs/.snapshots"
mkdir
-p
"$SNAP_DIR"
sudo
btrfs subvolume snapshot
-r
/
mnt
/
btrfs
"$SNAP_DIR/$(date +%Y%m%d)"
Keep snapshots from the last 7 days
find
"$SNAP_DIR"
-mindepth
1
-maxdepth
1
-type
d
-name
"2*"
|
sort
-r
|
tail
-n
8
|
xargs
-r
sudo
btrfs subvolume delete
Common Problem Solutions
Problem 1: Out of space but df shows free space
Example
Check space used by undeleted snapshots
sudo
btrfs subvolume list
-s
/
mnt
/
btrfs
Clean up unneeded snapshots
sudo
btrfs subvolume delete
/
mnt
/
btrfs
/
old_snapshot
Problem 2: Balance operation stuck
Example
View balance status
sudo
btrfs balance status
/
mnt
/
btrfs
Pause the balance operation
sudo
btrfs balance pause
/
mnt
/
btrfs
Resume the balance operation
sudo
btrfs balance resume
/
mnt
/
btrfs
Problem 3: Mount failure
Example
Check the filesystem
sudo
btrfs check
/
dev
/
sdX
Attempt repair
sudo
btrfs rescue zero-log
/
dev
/
sdX
sudo
mount
/
dev
/
sdX
/
mnt
/
btrfs
Best Practice Recommendations
-
Enable compression: Especially for compressible data such as text and logs
The zstd compression algorithm is recommended
mount -o compress=zstd /dev/sdX /mnt/btrfs 2.
Create snapshots regularly: Manually create a snapshot before important operations
3.Monitor space usage: btrfs space reporting differs from traditional filesystems
4.Avoid filling to 100%: Keep at least 5-10% of space available
5.Use balance with caution: Only run it when necessary, as it can be very time-consuming
6.Consider a RAID configuration: Use RAID1 or RAID10 for important data
7.Run scrub regularly: Check data integrity
每月执行一次数据检查
sudo btrfs scrub start /mnt/btrfs
Through this article, you should have mastered the basic operations and management methods of the btrfs filesystem. The powerful features of btrfs make it an ideal choice for modern Linux systems, especially for scenarios that require advanced storage capabilities.
Linux Command Reference