How To check LXD container BTRFS disk usage on Linux

Here is a quick and dirty shell script I put to check LXD container size and how much space they are taking on the BTRFS subvolume. Naturally, you must run the script as a root user, and LXD must be configured with BTRFS storage backend on Linux operating systems. See how to set up and install LXD on Ubuntu 20.04 LTS using the apt command

Checking LXD container BTRFS disk usage on Linux

Sample shell script:

#!/usr/bin/env bash
# Usage: 
# Find LXD container disk size and how much space they are using when storage back end set to BTRFS.
# Tested on Ubuntu Linux 20.04 LTS only
# Syntax:
# /path/to/lxd-btrfs-df 
# /path/to/lxd-btrfs-df | more
# /path/to/lxd-btrfs-df | grep "container-name"
# /path/to/lxd-btrfs-df  /dev/sda2
# ----------------------------------------------------------------------------
# Written by Vivek Gite <>
# (c) 2020 Vivek Gite under GNU GPL v2.0+
# ----------------------------------------------------------------------------
# Last updated: 23/Oct/2020
# ----------------------------------------------------------------------------
set -eu -o pipefail
# Default set to aws /dev/xvdf but override using the cli arg
# BTRFS mount point 
# Look for btrfs binary
_BTRFS="$(command -v btrfs)"
# Am i root user? if not die.
[ "$(id -u)" -ne "0" ] && { echo "This script must be run as root."; exit 1; }
# Failsafe stuff
[ "$_BTRFS" == "" ] && { echo "btrfs command not found."; exit 2; }
[ ! -d "$MNT" ] && mkdir -p "$MNT"
[ ! -b "$DEV" ] && { echo "$DEV not found. Try '$0 /dev/BTRFS_DEVICE' command."; exit 3; }
# Is $DEV mounted?
if ! mount | grep -q "^${DEV}" 
	mount "$DEV" "$MNT" 
	MNT="$(mount | grep ^"${DEV}" | awk '{print $3}')"
# My quick df for lxd containers 
echo -e "\n\t* Disk usage for LXD containers with BTRFS fs\n"
for d in "${MNT}"/containers/*
	$_BTRFS filesystem du -s "$d"
# Run df on mount fs too to get total disk usage 
echo -e "\n\t* Total (df) for $MNT BTRFS fs\n"
$_BTRFS filesystem df "$MNT"
umount "$MNT"

Sample session

Run it as follows:
Find www1 container size:
./lxd-btrfs-df | grep www1
Work on /dev/sdc3
./lxd-btrfs-df /dev/sdc3
./lxd-btrfs-df /dev/sdc3 | grep db-container


BTRFS for Linux containers powered by LXD offers many benefits such as :

  • Uses a subvolume per instance, image, and snapshot, creating btrfs snapshots when creating a new object.
  • btrfs support storage quotas via qgroups. While btrfs qgroups are hierarchical, new subvolumes will not automatically be added to the qgroups of their parent subvolumes. This means that users can trivially escape any quotas that are set. If adherence to strict quotas is necessary, developers should be mindful of this and consider using a zfs storage pool with refquotas.

I hope you find this tiny shell script useful. See LXD docs for more information.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics via:
Category List of Unix and Linux commands
Disk space analyzers df ncdu pydf
File Management cat tree
Firewall Alpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities NetHogs dig host ip nmap
OpenVPN CentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Manager apk apt
Processes Management bg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searching grep whereis which
User Information groups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPN Alpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
3 comments… add one
  • Tony Feb 27, 2021 @ 10:47

    Will this work if ZFS used as back-end on Ubuntu 20.04 LTS?

    • 🐧 Vivek Gite Feb 27, 2021 @ 10:47

      No this is for BTRFS. For ZFS you can use standard zfs commands to get information.

      • Lonnie Wheatly May 4, 2021 @ 21:27

        Can you please share zfs command to see such information on Ubuntu 20.04 LTS server?

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.