Files

41 lines
1.4 KiB
Bash
Raw Permalink Normal View History

#!/bin/bash
# 生产数据盘健康检查。
# 生产 server/data 当前是指向外接盘的符号链接;若磁盘未挂载,
# 启动服务会把数据写到错误位置或直接失败,因此在启动前明确阻止。
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
DATA_LINK="$ROOT/server/data"
MIN_FREE_GIB="${YAWAN_MIN_FREE_GIB:-100}"
if [ ! -L "$DATA_LINK" ]; then
echo "storage check failed: $DATA_LINK is not a symlink to the external data disk" >&2
exit 2
fi
TARGET="$(readlink "$DATA_LINK")"
if [ "${TARGET#/}" = "$TARGET" ]; then
TARGET="$(cd "$(dirname "$DATA_LINK")" && cd "$(dirname "$TARGET")" && pwd)/$(basename "$TARGET")"
fi
if [ ! -d "$TARGET" ]; then
echo "storage check failed: external data path is unavailable: $TARGET" >&2
exit 2
fi
# df -Pk reports available KiB in column 4. Keep arithmetic in KiB to avoid
# locale-dependent human-readable output and require at least 100 GiB free.
AVAILABLE_KIB="$(df -Pk "$TARGET" | awk 'NR==2 {print $4}')"
case "$AVAILABLE_KIB" in
''|*[!0-9]*)
echo "storage check failed: cannot read free space for $TARGET" >&2
exit 2
;;
esac
MIN_FREE_KIB=$((MIN_FREE_GIB * 1024 * 1024))
if [ "$AVAILABLE_KIB" -lt "$MIN_FREE_KIB" ]; then
echo "storage check failed: $TARGET has ${AVAILABLE_KIB} KiB free; require ${MIN_FREE_GIB} GiB" >&2
exit 3
fi
echo "storage ok: $TARGET has ${AVAILABLE_KIB} KiB free (minimum ${MIN_FREE_GIB} GiB)"