Hi,

If you wish to check the diskspace used by the btrfs subvolume, you are
suppose to use qgroup thing as mentiond. That is not safe to use.

But if the objective is to avoid filling up the disk. may be abit
simpler approach may help.  Let me explain.

FYI:
If the base of Btrfs is at $BTRFS_BASE, somthing like the following
should be OK as shell code. (__echo is a shell function)

The idea is set limit for minimum absolute disk size and minumum free
desk space percentage.  I am testing idea with free 10% (FMIN=10) disk
space with the following code. 

```
  # disk size sanity check without quota consideration
  BSS_STAT_TOTAL=$(stat -f -c %b "$BTRFS_BASE")
  # Even on 4k block system, disk with 200 blocks is less than 1MB
  if [ "$BSS_STAT_TOTAL" -le 200 ]; then
    __echo 0 "Total disk size: $BSS_STAT_TOTAL blocks (too small,
minimum required 200)"
    __echo 0 "skip snapshot: BASE=$BTRFS_BASE TIME=${NOW_TSTR}
TYPE=${BSS_TYPE}"
    exit 1
  fi
  BSS_STAT_UNIT=$((BSS_STAT_TOTAL/100))
  BSS_STAT_FREE=$(stat -f -c %f "$BTRFS_BASE")
  BSS_FREE_UNIT=$((BSS_STAT_FREE/BSS_STAT_UNIT))
  if [ "$BSS_FREE_UNIT" -le "$BSS_FMIN" ]; then
    __echo 0 "Free disk space: $BSS_FREE_UNIT%  (too small, minimum
required FMIN=$BSS_FMIN%)"
    __echo 0 "skip snapshot: BASE=$BTRFS_BASE TIME=${NOW_TSTR}
TYPE=${BSS_TYPE}"
    exit 1
  else
    __echo 2 "Free disk space: $BSS_FREE_UNIT%  (enough, minimum
required FMIN=$BSS_FMIN%)"
  fi
  # make snapshot
  $NOOP $SUDO mkdir -p "$BSS_DIR_BASE" >/dev/null
  while [ -d "$BSS_DIR_BASE/${NOW_TSTR}.${BSS_TYPE}" ]; do
    sleep "1s"
    NOW_TSTR=$(date -u --iso=second)
  done
  __echo 2 "make snapshot: BASE=$BTRFS_BASE TIME=${NOW_TSTR}
TYPE=${BSS_TYPE}"
  # shellcheck disable=SC2086
  $NOOP $BSV snapshot -r "$BTRFS_BASE"
"$BSS_DIR_BASE/${NOW_TSTR}.${BSS_TYPE}"

```
(This is what I do here for my own needs here and playing with data
aging parameters.)

I think snapper can do the similar.

Reply via email to