How to show Btrfs usage → btrfs filesystem df /mnt

How to Show Btrfs Usage → btrfs filesystem df /mnt Table of Contents 1. [Introduction](#introduction) 2. [Prerequisites](#prerequisites) 3. [Understanding Btrfs Filesystem Structure](#understanding-btrfs-filesystem-structure) 4. [Basic Usage of btrfs filesystem df](#basic-usage-of-btrfs-filesystem-df) 5. [Detailed Command Syntax and Options](#detailed-command-syntax-and-options) 6. [Practical Examples and Use Cases](#practical-examples-and-use-cases) 7. [Interpreting Output Results](#interpreting-output-results) 8. [Advanced Usage Scenarios](#advanced-usage-scenarios) 9. [Troubleshooting Common Issues](#troubleshooting-common-issues) 10. [Best Practices and Professional Tips](#best-practices-and-professional-tips) 11. [Related Commands and Tools](#related-commands-and-tools) 12. [Conclusion](#conclusion) Introduction Btrfs (B-tree File System) is a modern copy-on-write filesystem for Linux that offers advanced features like snapshots, subvolumes, and built-in RAID capabilities. One of the essential aspects of managing any filesystem is monitoring its usage and available space. The `btrfs filesystem df` command is a powerful tool that provides detailed information about space allocation and usage within Btrfs filesystems. Unlike traditional filesystems where space monitoring is straightforward, Btrfs uses a more complex allocation system with different types of block groups for data, metadata, and system information. This complexity makes understanding space usage more challenging but also more informative when properly interpreted. In this comprehensive guide, you'll learn how to effectively use the `btrfs filesystem df` command to monitor your Btrfs filesystem usage, understand the output, troubleshoot common issues, and implement best practices for filesystem management. Whether you're a system administrator, developer, or Linux enthusiast, this guide will provide you with the knowledge needed to effectively monitor and manage Btrfs filesystem space. Prerequisites Before diving into the `btrfs filesystem df` command, ensure you have the following prerequisites: System Requirements - Linux system with Btrfs support (kernel 2.6.29 or later) - Btrfs-tools (btrfs-progs) package installed - At least one mounted Btrfs filesystem - Appropriate permissions to access the filesystem Knowledge Requirements - Basic understanding of Linux filesystem concepts - Familiarity with command-line interface - Basic knowledge of file permissions and mounting Installation Verification First, verify that Btrfs tools are installed on your system: ```bash Check if btrfs command is available which btrfs Verify btrfs-progs version btrfs --version ``` If btrfs tools are not installed, install them using your distribution's package manager: ```bash Ubuntu/Debian sudo apt-get install btrfs-progs CentOS/RHEL/Fedora sudo yum install btrfs-progs or for newer versions sudo dnf install btrfs-progs Arch Linux sudo pacman -S btrfs-progs ``` Understanding Btrfs Filesystem Structure Before using the `btrfs filesystem df` command effectively, it's crucial to understand how Btrfs manages space allocation. Unlike traditional filesystems, Btrfs uses a two-stage allocation system: Block Group Types Btrfs organizes storage into three main types of block groups: 1. Data Block Groups: Store actual file content 2. Metadata Block Groups: Store filesystem metadata, including directory structures, file attributes, and extent information 3. System Block Groups: Store critical filesystem structures like chunk tree and device tree information Allocation Profiles Btrfs supports different allocation profiles that determine how data is distributed across devices: - Single: Data stored on one device without redundancy - DUP: Data duplicated on the same device (metadata only) - RAID0: Data striped across multiple devices - RAID1: Data mirrored across two devices - RAID5: Data with parity distributed across multiple devices - RAID6: Data with double parity distributed across multiple devices - RAID10: Combination of RAID1 and RAID0 Basic Usage of btrfs filesystem df The most basic usage of the `btrfs filesystem df` command is straightforward: ```bash btrfs filesystem df /path/to/mountpoint ``` Simple Example ```bash Show filesystem usage for a Btrfs filesystem mounted at /mnt btrfs filesystem df /mnt ``` This command will display output similar to: ``` Data, single: total=8.00GiB, used=4.50GiB System, DUP: total=8.00MiB, used=16.00KiB Metadata, DUP: total=1.00GiB, used=176.00MiB GlobalReserve, single: total=16.00MiB, used=0.00B ``` Understanding the Basic Output Each line in the output represents a different type of block group: - Data, single: Shows total allocated space and used space for data blocks - System, DUP: Shows system block group information with DUP profile - Metadata, DUP: Shows metadata block group information - GlobalReserve: Shows reserved space for filesystem operations Detailed Command Syntax and Options The `btrfs filesystem df` command supports several options that provide different levels of detail: Command Syntax ```bash btrfs filesystem df [options] ``` Available Options -b, --raw Display raw numbers in bytes instead of human-readable format: ```bash btrfs filesystem df -b /mnt ``` Output example: ``` Data, single: total=8589934592, used=4831838208 System, DUP: total=8388608, used=16384 Metadata, DUP: total=1073741824, used=184549376 GlobalReserve, single: total=16777216, used=0 ``` -h, --human-readable Display sizes in human-readable format (default behavior): ```bash btrfs filesystem df -h /mnt ``` -T, --show-space Show space in different units: ```bash btrfs filesystem df -T /mnt ``` Combining Options You can combine multiple options for customized output: ```bash Show raw bytes with detailed information btrfs filesystem df -b --show-space /mnt ``` Practical Examples and Use Cases Example 1: Monitoring a Single-Device Btrfs Filesystem For a typical single-device Btrfs filesystem: ```bash Check usage on root filesystem sudo btrfs filesystem df / Expected output Data, single: total=20.00GiB, used=15.32GiB System, DUP: total=8.00MiB, used=16.00KiB Metadata, DUP: total=2.00GiB, used=1.25GiB GlobalReserve, single: total=128.00MiB, used=0.00B ``` Example 2: Multi-Device RAID1 Configuration For a RAID1 Btrfs filesystem across two devices: ```bash btrfs filesystem df /home Expected output Data, RAID1: total=50.00GiB, used=35.67GiB System, RAID1: total=8.00MiB, used=16.00KiB Metadata, RAID1: total=3.00GiB, used=1.89GiB GlobalReserve, single: total=256.00MiB, used=0.00B ``` Example 3: Monitoring Space Before and After Operations Monitor filesystem usage before and after large file operations: ```bash Check usage before copying large files echo "Before operation:" btrfs filesystem df /data Perform large file copy cp -r /source/large_directory /data/ Check usage after operation echo "After operation:" btrfs filesystem df /data ``` Example 4: Scripted Monitoring Create a script for regular monitoring: ```bash #!/bin/bash btrfs_monitor.sh MOUNT_POINT="/mnt/btrfs" LOG_FILE="/var/log/btrfs_usage.log" echo "$(date): Btrfs filesystem usage for $MOUNT_POINT" >> $LOG_FILE btrfs filesystem df $MOUNT_POINT >> $LOG_FILE echo "----------------------------------------" >> $LOG_FILE ``` Interpreting Output Results Understanding the output of `btrfs filesystem df` is crucial for effective filesystem management. Data Block Groups ``` Data, single: total=8.00GiB, used=4.50GiB ``` - total: Total space allocated for data block groups - used: Actual space used by file data - Available space for data: total - used = 3.50GiB Metadata Block Groups ``` Metadata, DUP: total=1.00GiB, used=176.00MiB ``` - total: Total space allocated for metadata - used: Space used by filesystem metadata - DUP profile: Metadata is duplicated for redundancy System Block Groups ``` System, DUP: total=8.00MiB, used=16.00KiB ``` - total: Space allocated for system structures - used: Space used by critical filesystem data - Usually small: System blocks typically use minimal space Global Reserve ``` GlobalReserve, single: total=16.00MiB, used=0.00B ``` - total: Space reserved for filesystem operations - used: Currently used reserve space - Emergency space: Reserved for critical operations when filesystem is full Calculating Available Space To calculate total available space: 1. Data space available: Data total - Data used 2. Unallocated space: Check with `btrfs filesystem show` 3. Consider allocation overhead: Btrfs allocates in chunks Advanced Usage Scenarios Scenario 1: Monitoring Subvolume Usage While `btrfs filesystem df` shows overall filesystem usage, you might want to monitor specific subvolumes: ```bash Show filesystem usage btrfs filesystem df /mnt Show subvolume list btrfs subvolume list /mnt Check specific subvolume usage (requires additional tools) btrfs filesystem usage /mnt ``` Scenario 2: Automated Alerting Create an automated monitoring system: ```bash #!/bin/bash btrfs_alert.sh MOUNT_POINT="/data" THRESHOLD=80 EMAIL="admin@example.com" Get usage percentage for data USAGE_LINE=$(btrfs filesystem df $MOUNT_POINT | grep "Data") TOTAL=$(echo $USAGE_LINE | grep -o 'total=[0-9.]*GiB' | cut -d'=' -f2 | cut -d'G' -f1) USED=$(echo $USAGE_LINE | grep -o 'used=[0-9.]*GiB' | cut -d'=' -f2 | cut -d'G' -f1) USAGE_PERCENT=$(echo "scale=2; $USED / $TOTAL * 100" | bc) if (( $(echo "$USAGE_PERCENT > $THRESHOLD" | bc -l) )); then echo "Warning: Btrfs filesystem usage is ${USAGE_PERCENT}%" | mail -s "Filesystem Alert" $EMAIL fi ``` Scenario 3: Performance Monitoring Monitor allocation patterns over time: ```bash #!/bin/bash btrfs_performance_monitor.sh MOUNT_POINT="/mnt/btrfs" OUTPUT_FILE="/var/log/btrfs_performance.csv" Create CSV header if file doesn't exist if [ ! -f $OUTPUT_FILE ]; then echo "timestamp,data_total,data_used,metadata_total,metadata_used,system_total,system_used" > $OUTPUT_FILE fi Parse btrfs filesystem df output OUTPUT=$(btrfs filesystem df $MOUNT_POINT) DATA_LINE=$(echo "$OUTPUT" | grep "Data") METADATA_LINE=$(echo "$OUTPUT" | grep "Metadata") SYSTEM_LINE=$(echo "$OUTPUT" | grep "System") Extract values (simplified parsing) TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S') echo "$TIMESTAMP,$DATA_TOTAL,$DATA_USED,$METADATA_TOTAL,$METADATA_USED,$SYSTEM_TOTAL,$SYSTEM_USED" >> $OUTPUT_FILE ``` Troubleshooting Common Issues Issue 1: Permission Denied Problem: Getting permission denied when running the command. Solution: ```bash Use sudo for system filesystems sudo btrfs filesystem df / Check mount permissions ls -la /mnt/btrfs Ensure user has read access to mount point ``` Issue 2: Command Not Found Problem: `btrfs` command not found. Solution: ```bash Install btrfs-progs sudo apt-get install btrfs-progs # Ubuntu/Debian sudo yum install btrfs-progs # CentOS/RHEL Verify installation which btrfs btrfs --version ``` Issue 3: Inconsistent or Confusing Output Problem: Output doesn't match expected filesystem usage. Solution: ```bash Check filesystem consistency sudo btrfs check /dev/sdX Compare with other tools df -h /mnt/btrfs btrfs filesystem usage /mnt/btrfs Check for snapshots consuming space btrfs subvolume list /mnt/btrfs ``` Issue 4: No Space Left Despite Showing Available Space Problem: System reports no space but `btrfs filesystem df` shows available space. Solution: ```bash Check unallocated space btrfs filesystem usage /mnt/btrfs Balance filesystem to reallocate space sudo btrfs balance start /mnt/btrfs Check metadata space specifically btrfs filesystem df /mnt/btrfs | grep Metadata ``` Issue 5: Metadata Space Full Problem: Metadata space is exhausted. Solution: ```bash Check metadata usage btrfs filesystem df /mnt/btrfs | grep Metadata Balance metadata sudo btrfs balance start -m /mnt/btrfs Add more devices if necessary sudo btrfs device add /dev/sdY /mnt/btrfs ``` Best Practices and Professional Tips Regular Monitoring 1. Set up automated monitoring: Create scripts to regularly check filesystem usage 2. Monitor trends: Track usage patterns over time to predict space needs 3. Set up alerts: Configure alerts before reaching critical space levels Understanding Allocation Patterns 1. Monitor all block group types: Don't focus only on data usage 2. Watch metadata growth: Metadata can fill up independently of data 3. Consider allocation profiles: Different RAID levels affect space calculations Maintenance Recommendations 1. Regular balancing: Perform periodic balance operations to optimize space usage 2. Cleanup snapshots: Remove old snapshots that are no longer needed 3. Monitor subvolumes: Keep track of subvolume space usage Performance Optimization ```bash Example monitoring script for performance #!/bin/bash MOUNT="/data" Check allocation efficiency echo "=== Allocation Efficiency ===" btrfs filesystem usage $MOUNT echo "=== Space Distribution ===" btrfs filesystem df $MOUNT echo "=== Device Usage ===" btrfs filesystem show ``` Capacity Planning 1. Plan for growth: Monitor growth rates to predict future needs 2. Consider redundancy overhead: RAID configurations reduce available space 3. Account for metadata: Metadata requirements grow with file count Security Considerations 1. Limit access: Restrict who can run filesystem commands 2. Log monitoring: Keep logs of filesystem usage and changes 3. Regular backups: Maintain backups regardless of RAID configuration Related Commands and Tools Complementary Btrfs Commands ```bash Show detailed filesystem usage btrfs filesystem usage /mnt Show device information btrfs filesystem show List subvolumes btrfs subvolume list /mnt Show filesystem statistics btrfs device stats /mnt ``` Traditional Tools for Comparison ```bash Standard disk usage df -h /mnt Directory usage du -sh /mnt/* Inode usage df -i /mnt ``` Monitoring Integration ```bash Integration with system monitoring Add to crontab for regular checks 0 /6 /usr/local/bin/btrfs_monitor.sh Integration with Nagios/Zabbix Create custom check scripts using btrfs filesystem df ``` Conclusion The `btrfs filesystem df` command is an essential tool for monitoring and managing Btrfs filesystem space usage. Understanding its output and proper usage is crucial for maintaining healthy Btrfs filesystems. Key takeaways from this guide include: 1. Comprehensive monitoring: Always monitor data, metadata, and system block groups 2. Regular maintenance: Implement automated monitoring and alerting systems 3. Proper interpretation: Understand the difference between allocated and used space 4. Proactive management: Address space issues before they become critical 5. Integration approach: Use `btrfs filesystem df` alongside other tools for complete visibility By following the practices and techniques outlined in this guide, you'll be well-equipped to effectively monitor and manage your Btrfs filesystems. Remember that Btrfs space management is more complex than traditional filesystems, but this complexity provides greater flexibility and advanced features when properly understood and utilized. Regular monitoring with `btrfs filesystem df` should be part of your routine system administration tasks, helping ensure optimal performance and preventing space-related issues before they impact your systems and users.