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 <https://www.cyberciti.biz/>
# (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
DEV="${1:-/dev/xvdf}"
 
# BTRFS mount point 
MNT="/mnt/btrfs"
 
# 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}" 
then
	mount "$DEV" "$MNT" 
else
	MNT="$(mount | grep ^"${DEV}" | awk '{print $3}')"
fi
 
# My quick df for lxd containers 
echo -e "\n\t* Disk usage for LXD containers with BTRFS fs\n"
for d in "${MNT}"/containers/*
do 
	$_BTRFS filesystem du -s "$d"
done
 
# 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:
./lxd-btrfs-df
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

Conclusion

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:
Category List of Unix and Linux commands
File Management cat
Firewall Alpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities 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
0 comments… add one

Leave a Reply

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

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