btrfs, linux, OYB software, shell

Check disk space of your BTRFS snapshots with btrfs-du

Today I want to share a simple way to check the state of our BTRFS snapshots.

Usage

If path is omitted, it will default to / .

We can list easily our snapshots, plus have a clear view of how much data they have in common and how much total overhead are we holding.

Four months worth of snapshots for only 7.48GB!!

Installation

Get the script and make it executable. You can do this in two lines, but better inspect it first. Don’t trust anyone blindly.

Details

Normally you can only get disk usage information through the quota info in qgroups

 

This is not ideal for a couple reasons:

  • First, we need to have quota enabled, which can have a performance impact with many subvolumes.
  • Second, we still have to list our subvolumes to correlate subvolume IDs to subvolume names. Two steps for a simple operation.

I was inspired by btrfs-size by Kyle Agronick, but not only the output wasn’t very clean to my taste, but also it was terribly slow on my root subvolume where docker uses BTRFS as storage driver and I have very many subvolumes. I am talking close to a minute.

Looking into the code to speed it up I decided that it would be cleaner to rewrite it as it was more complex that it needed to be. Also, quotas don’t need to be enabled for btrfs-du to work.

Keep in mind that if quotas are not enabled for the subvolume, the command can take several seconds to calculate the sizes. We can speed the execution by having quotas pre-enabled, but as mentioned before note that this can have an impact in a big subvolume with many snapshots.

Code

Author: nachoparker

Humbly sharing things that I find useful [ github dockerhub ]

6 Comments on “Check disk space of your BTRFS snapshots with btrfs-du

  1. I get an error /home/miguel/bin/btrfs-du: line 88: EXCL_TOTAL + : syntax error: operand expected (error token is “+ “)

    1. yes. Quotas need to be enabled for the calculation. If they were enabled, then it will be fast but if not, they will be enabled and then btrfs-du will wait until the scan is finished, report disk usage and finally disable them again. This can take a couple minutes.

      you can make this faster by leaving quota enabled, as the article explains

Leave a Reply to nachoparker Cancel reply

Your email address will not be published. Required fields are marked *