Skip to content

Instantly share code, notes, and snippets.

@enkore
Last active September 13, 2023 16:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enkore/0c93ba5c122c18628035ffc89df98c03 to your computer and use it in GitHub Desktop.
Save enkore/0c93ba5c122c18628035ffc89df98c03 to your computer and use it in GitHub Desktop.
Report pool usage on Samba shares using multiple ZFS datasets
#!/bin/bash
# Suppose you export a dataset via Samba, then Samba will do df(1), which will only
# show the space used by the dataset of the shared directory. If you have datasets nestled
# within, then the space used by them will not show up in the overall share space usage:
#
# /mnt/tank/ -- using X TB from the pool
# /mnt/tank/dataset1 -- using Y TB from the pool
# /mnt/tank/dataset2 -- using Z TB from the pool
#
# By default you would only see X TB used on the /mnt/tank share.
# This small script simply sums the space used by all datasets in a share recursively,
# so you end up with X+Y+Z TB reported for the /mnt/tank share when you set this script
# as the "dfree command" (https://www.samba.org/samba/docs/current/man-html/smb.conf.5.html#idm2868).
# Linuxistic Bashisms.
POOL=$(findmnt -no source -T "$PWD")
USED=$(zfs list -rHp -o used "$POOL" | awk '{s+=$1} END {printf "%.0f\n", s}')
AVAIL=$(zfs list -Hp -o avail "$POOL")
TOTAL=$(($USED + $AVAIL))
echo "$TOTAL $AVAIL 1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment