Linux btrfs Command
Btrfs (B-tree File System) is an advanced Linux file system developed by Oracle and first released in 2007. Its design goal is to solve the limitations of traditional file systems, providing better scalability, reliability, and management features.
Core Features
- Copy-on-Write (CoW): All write operations do not overwrite existing data, but instead create new copies
- Snapshot Functionality: Can quickly create instant snapshots of the file system, taking up almost no extra space
- Subvolume Management: Supports dividing the file system 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 |
Main Subcommands Explained
File System Creation and Management
Create a btrfs File System
Example
sudo mkfs.btrfs /dev/sdX
# Create a file system with a label
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 File System
Example
sudo mount /dev/sdX /mnt/btrfs
# Enable compression (lzo or zstd recommended)
sudo mount -o compress=lzo /dev/sdX /mnt/btrfs
# Show mount options
mount | grep btrfs
Subvolume Operations
Create a Subvolume
Example
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
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 File System Usage
Example
sudo btrfs filesystem usage /mnt/btrfs
# Show 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 File System
Example
sudo btrfs filesystem resize +10G /mnt/btrfs
# Shrink the file system
sudo btrfs filesystem resize -5G /mnt/btrfs
Data Integrity Check
Check the File System
Example
sudo btrfs check /dev/sdX
# Attempt repair (use with caution)
sudo btrfs check --repair /dev/sdX
Cleanup and Repair
Example
sudo btrfs filesystem defrag /mnt/btrfs
# Check and repair the file system
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
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 to enable 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: Regular Snapshots and Cleanup
Example
# 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 the last 7 days of snapshots
find "$SNAP_DIR" -mindepth 1 -maxdepth 1 -type d -name "2*" | sort -r | tail -n +8 | xargs -r sudo btrfs subvolume delete
Common Problem Solving
Problem 1: Insufficient Space but df Shows Available Space
Example
sudo btrfs subvolume list -s /mnt/btrfs
# Clean up unnecessary snapshots
sudo btrfs subvolume delete /mnt/btrfs/old_snapshot
Problem 2: Balance Operation Stuck
Example
sudo btrfs balance status /mnt/btrfs
# Pause balance operation
sudo btrfs balance pause /mnt/btrfs
# Resume balance operation
sudo btrfs balance resume /mnt/btrfs
Problem 3: Mount Failure
Example
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
# 推荐使用 zstd 压缩算法 mount -o compress=zstd /dev/sdX /mnt/btrfs
-
Create snapshots regularly: Manually create snapshots before important operations
-
Monitor space usage: btrfs space reporting differs from traditional file systems
-
Avoid 100% full: Keep at least 5-10% free space
-
Use balance with caution: Only run when necessary, as it can be very time-consuming
-
Consider RAID configuration: Use RAID1 or RAID10 for important data
-
Perform 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 file system. btrfs's powerful features make it an ideal choice for modern Linux systems, especially suitable for scenarios requiring advanced storage features.
Other Extensions
Linux Command Reference