How to Check Btrfs FileSystem Usage and Perform Balancing

In Btrfs file system there are chunks that are allocated for regular data, metadata. There are also chunks that are allocated to store file system information and also about where other chunks are located on the physical disk. Those chunks can only store the data. Very occasionally, a chunk type may fill up and start to run out of space. However, there may (and often is) still space available in other chunks. Therefore, some space needs to be freed up on it.

There is a balancing feature available in Btrfs FileSystem. The balance operation effectively frees up space in the other chunks to make it available for allocation.

1. Below command shows how much space has been allocated on your filesystem:

# btrfs fi show

2. Below command shows the file usage:

# btrfs filesystem du /btr

3. Complete filesystem usage by data and metadata( Before Balance):

# btrfs filesystem usage /btr

The below command is used for balance and this command does the full balance without filters requested. This operation is very intense and takes potentially very long. It is recommended to use the balance filters to narrow down the balanced data.

[Without Filter]

# btrfs filesystem balance /btr

[With Filter(use dusage and musage)]

# btrfs filesystem balance start -dusage=5 /btr

Note that there should be no space between the -d and usage. This command will attempt to relocate data in empty or near-empty data chunks (at most 5% used, in this example), allowing the space to be reclaimed and reassigned to metadata.

If the balance command ends with “Done, had to relocate 0 out of XX chunks“, then you need to increase the “dusage” percentage parameter till at least one chunk is relocated. If you don’t use the filters then it balances both data and metadata.

Another way for balancing the btrfs filesystem through script

for i in 0 5 10 15 20 25 30 40 50 60 70 80 90 100
do
 echo "${0}: Running with ${i}%"
 sudo btrfs balance start -dusage=$i -musage=$i /btr/
done

After balance check through below command:

# btrfs filesystem usage /btr

Depending on ‘btrfs filesystem du’ and ‘btrfs filesystem usage’ decide the values for -dusage and -musage.

Related Post