|
1 | 1 | #!/bin/bash
|
2 | 2 | set -euo pipefail
|
3 | 3 |
|
4 |
| -DISK_MIN_AVAILABLE=${DISK_MIN_AVAILABLE:-5242880} # 5GB |
5 |
| -DISK_MIN_INODES=${DISK_MIN_INODES:-250000} # docker needs lots |
6 |
| - |
7 |
| -DOCKER_DIR="/var/lib/docker/" |
8 |
| - |
9 |
| -disk_avail=$(df -k --output=avail "$DOCKER_DIR" | tail -n1) |
10 |
| - |
11 |
| -echo "Disk space free: $(df -k -h --output=avail "$DOCKER_DIR" | tail -n1 | sed -e 's/^[[:space:]]//')" |
12 |
| - |
13 |
| -if [[ $disk_avail -lt $DISK_MIN_AVAILABLE ]]; then |
14 |
| - echo "Not enough disk space free, cutoff is ${DISK_MIN_AVAILABLE} 🚨" >&2 |
15 |
| - exit 1 |
| 4 | +# Usage: |
| 5 | +# bk-check-disk-space.sh (min disk required) (min inodes required) |
| 6 | +# min disk required can be either an amount of bytes, a pattern like 10G |
| 7 | +# or 500M, or a percentage like 5% |
| 8 | +# min inodes must be a number, default to 250,000 |
| 9 | + |
| 10 | +# Converts human-readable units like 1.43K and 120.3M to bytes |
| 11 | +dehumanize() { |
| 12 | + awk '/[0-9][bB]?$/ {printf "%u\n", $1*1024} |
| 13 | + /[tT][bB]?$/ {printf "%u\n", $1*(1024*1024*1024)} |
| 14 | + /[gG][bB]?$/ {printf "%u\n", $1*(1024*1024)} |
| 15 | + /[mM][bB]?$/ {printf "%u\n", $1*(1024)} |
| 16 | + /[kK][bB]?$/ {printf "%u\n", $1*1}' <<< "$1" |
| 17 | +} |
| 18 | + |
| 19 | +min_available=${1:-5G} |
| 20 | +docker_dir="/var/lib/docker/" |
| 21 | + |
| 22 | +# First check the disk available |
| 23 | + |
| 24 | +disk_avail=$(df -k --output=avail "$docker_dir" | tail -n1) |
| 25 | +disk_avail_human=$(df -k -h --output=avail "$docker_dir" | tail -n1 | tr -d '[:space:]') |
| 26 | +disk_used_pct=$(df -k --output=pcent "$docker_dir" | tail -n1 | tr -d '[:space:]' | tr -d '%') |
| 27 | +disk_free_pct=$((100-disk_used_pct)) |
| 28 | + |
| 29 | +printf "Disk space free: %s (%s%%)\\n" "$disk_avail_human" "$disk_free_pct" |
| 30 | + |
| 31 | +# Check if the min_available is a percentage |
| 32 | +if [[ $min_available =~ \%$ ]] ; then |
| 33 | + if [[ $(echo "${disk_free_pct}<${min_available}" | sed 's/%//g' | bc) -gt 0 ]] ; then |
| 34 | + echo "Not enough disk space free, cutoff is ${min_available} 🚨" >&2 |
| 35 | + exit 1 |
| 36 | + fi |
| 37 | +else |
| 38 | + if [[ $disk_avail -lt $(dehumanize "$min_available") ]]; then |
| 39 | + echo "Not enough disk space free, cutoff is ${min_available} 🚨" >&2 |
| 40 | + exit 1 |
| 41 | + fi |
16 | 42 | fi
|
17 | 43 |
|
18 |
| -inodes_avail=$(df -k --output=iavail "$DOCKER_DIR" | tail -n1) |
| 44 | +# Next check inodes, these can be exhausted by docker build operations |
| 45 | + |
| 46 | +inodes_min_available=${2:-250000} |
| 47 | +inodes_avail=$(df -k --output=iavail "$docker_dir" | tail -n1 | tr -d '[:space:]') |
| 48 | +inodes_avail_human=$(df -k -h --output=iavail "$docker_dir" | tail -n1 | tr -d '[:space:]') |
| 49 | +inodes_used_pct=$(df -k --output=ipcent "$docker_dir" | tail -n1 | tr -d '[:space:]' | tr -d '%') |
| 50 | +inodes_free_pct=$((100-inodes_used_pct)) |
19 | 51 |
|
20 |
| -echo "Inodes free: $(df -k -h --output=iavail "$DOCKER_DIR" | tail -n1 | sed -e 's/^[[:space:]]//')" |
| 52 | +printf "Inodes free: %s (%s%%)\\n" "$inodes_avail_human" "$inodes_free_pct" |
21 | 53 |
|
22 |
| -if [[ $inodes_avail -lt $DISK_MIN_INODES ]]; then |
23 |
| - echo "Not enough inodes free, cutoff is ${DISK_MIN_INODES} 🚨" >&2 |
| 54 | +if [[ $inodes_avail -lt $inodes_min_available ]]; then |
| 55 | + echo "Not enough inodes free, cutoff is ${inodes_min_available} 🚨" >&2 |
24 | 56 | exit 1
|
25 | 57 | fi
|
0 commit comments