41 lines
1.7 KiB
Bash
41 lines
1.7 KiB
Bash
#!/usr/bin/env bash
|
|
# 005-mount-pgdata - Mount the dedicated 20G data disk (/dev/sdb, ext4)
|
|
# at /var/lib/postgresql so the PostgreSQL cluster lives on its own disk.
|
|
# Mounted by UUID (stable across device reordering), noatime for DB workload.
|
|
# up: fstab entry + mount (idempotent; refuses if mountpoint holds data)
|
|
# down: unmount + remove fstab entry (data on the disk is preserved)
|
|
set -euo pipefail
|
|
|
|
DISK_UUID=d44ad362-947a-46de-acbf-5530f83e4506
|
|
MOUNTPOINT=/var/lib/postgresql
|
|
FSTAB_LINE="UUID=$DISK_UUID $MOUNTPOINT ext4 defaults,noatime 0 2"
|
|
|
|
case "${1:-}" in
|
|
up)
|
|
if findmnt -rn "$MOUNTPOINT" >/dev/null; then
|
|
echo " $MOUNTPOINT already mounted - skipping"
|
|
exit 0
|
|
fi
|
|
dev=$(findfs UUID="$DISK_UUID") || {
|
|
echo "disk with UUID=$DISK_UUID not found - is /dev/sdb attached?" >&2
|
|
exit 1
|
|
}
|
|
# Never mount over existing data - it would be silently hidden
|
|
if [[ -d $MOUNTPOINT ]] && find "$MOUNTPOINT" -mindepth 1 -print -quit | grep -q .; then
|
|
echo "refusing to mount: $MOUNTPOINT exists and is not empty" >&2
|
|
exit 1
|
|
fi
|
|
mkdir -p "$MOUNTPOINT"
|
|
grep -q "^UUID=$DISK_UUID" /etc/fstab || echo "$FSTAB_LINE" >> /etc/fstab
|
|
mount "$MOUNTPOINT" # via fstab entry - also validates it
|
|
echo " $dev mounted at $MOUNTPOINT (noatime, fstab entry added)"
|
|
;;
|
|
down)
|
|
# sysmig rolls back in reverse order, so 006 (postgres) is already down
|
|
if findmnt -rn "$MOUNTPOINT" >/dev/null; then umount "$MOUNTPOINT"; fi
|
|
sed -i "\|^UUID=$DISK_UUID|d" /etc/fstab
|
|
rmdir "$MOUNTPOINT" 2>/dev/null || true # keep dir if data remains
|
|
echo " $MOUNTPOINT unmounted + fstab entry removed (disk data kept)"
|
|
;;
|
|
esac
|