Linux Comm Btrfs
[ Linux Command Manual](#)
* * *
Btrfs (B-tree File System) is an advanced Linux file system developed by Oracle and first released in 2007. Its design goal is to address 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 create new copies
* **Snapshot Functionality**: Can quickly create instant snapshots of the file system with almost no additional space usage
* **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
#### Creating btrfs File System
## Examples
# Create btrfs file system on device
sudo mkfs.btrfs /dev/sdX
# Create file system with label
sudo mkfs.btrfs -L mylabel /dev/sdX
# Create RAID1 using multiple devices
sudo mkfs.btrfs -m raid1 -d raid1 /dev/sdX /dev/sdY
#### Mounting btrfs File System
## Examples
# Basic mount
sudo mount/dev/sdX /mnt/btrfs
# Enable compression (recommend lzo or zstd)
sudo mount-o compress=lzo /dev/sdX /mnt/btrfs
# Display mount options
mount|grep btrfs
### Subvolume Operations
#### Creating Subvolumes
## Examples
# Create subvolume
sudo btrfs subvolume create /mnt/btrfs/subvol1
# List subvolumes
sudo btrfs subvolume list /mnt/btrfs
# Delete subvolume
sudo btrfs subvolume delete /mnt/btrfs/subvol1
#### Snapshot Management
## Examples
# Create 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 snapshot
sudo btrfs subvolume delete /mnt/btrfs/snapshot_20230101
### Space Management
#### Viewing File System Usage
## Examples
# 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
#### Resizing File System
## Examples
# Expand file system
sudo btrfs filesystem resize +10G /mnt/btrfs
# Shrink file system
sudo btrfs filesystem resize -5G/mnt/btrfs
### Data Integrity Checking
#### Checking File System
## Examples
# Read-only check
sudo btrfs check /dev/sdX
# Attempt repair (use with caution)
sudo btrfs check --repair/dev/sdX
#### Cleanup and Repair
## Examples
# Clean up free space
sudo btrfs filesystem defrag /mnt/btrfs
# Check and repair file system
sudo btrfs scrub start /mnt/btrfs
# View repair status
sudo btrfs scrub status /mnt/btrfs
* * *
## Practical Application Examples
### Example 1: Setting Up btrfs Home Directory with Compression
## Examples
# Create 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: Regular Snapshots and Cleanup
## Examples
#!/bin/bash
# Create daily snapshot
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 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 Available Space
## Examples
# Check space used by undeleted snapshots
sudo btrfs subvolume list -s/mnt/btrfs
# Clean up unnecessary snapshots
sudo btrfs subvolume delete /mnt/btrfs/old_snapshot
### Problem 2: Balance Operation Stuck
## Examples
# View balance status
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
## Examples
# Check file system
sudo btrfs check /dev/sdX
# Attempt repair
sudo btrfs rescue zero-log /dev/sdX
sudo mount/dev/sdX /mnt/btrfs
* * *
## Best Practice Recommendations
1. **Enable compression**: Especially for compressible data like text and logs
# Recommend using zstd compression algorithm mount -o compress=zstd /dev/sdX /mnt/btrfs
2. **Create regular snapshots**: Manually create snapshots before important operations
3. **Monitor space usage**: btrfs space reporting differs from traditional file systems
4. **Avoid 100% full**: Maintain at least 5-10% available space
5. **Use balance with caution**: Only execute when necessary, can be very time-consuming
6. **Consider RAID configuration**: Use RAID1 or RAID10 for important data
7. **Execute scrub regularly**: Check data integrity
# Execute data check monthly sudo btrfs scrub start /mnt/btrfs
* * *
Through this article, you should have mastered the basic operations and management methods of the btrfs file system. The powerful features of btrfs make it an ideal choice for modern Linux systems, especially suitable for scenarios requiring advanced storage functionality.
* * Linux Command Manual](#)
YouTip