Skip to content

Commit 4ec721d

Browse files
committed
feat: SUCCESS-220 check vol is available dding more kogs
1 parent a720562 commit 4ec721d

File tree

1 file changed

+61
-18
lines changed

1 file changed

+61
-18
lines changed
Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/env bash
2-
32
set -euo pipefail
43

54
DEVICE=${1:-}
@@ -8,55 +7,99 @@ MOUNT_POINT=${2:-}
87
if [[ -z "$DEVICE" || -z "$MOUNT_POINT" ]]; then
98
echo "Usage: $0 <device> <mount_point>"
109
echo "Example: sudo ./mount-volume.sh /dev/nvme1n1 /data/150008"
10+
logger "Usage: $0 <device> <mount_point>"
11+
logger "Example: sudo ./mount-volume.sh /dev/nvme1n1 /data/150008"
1112
exit 1
1213
fi
1314

14-
# Mount a block device to a specified mount point
15-
# If the device is not formatted, format it as ext4
16-
# Set ownership to postgres:postgres and permissions to 750
17-
# Add the mount entry to /etc/fstab for persistence across reboots
18-
1915
OWNER="postgres:postgres"
2016
PERMISSIONS="750"
2117
FSTYPE="ext4"
2218
MOUNT_OPTS="defaults"
2319
FSTAB_FILE="/etc/fstab"
20+
TIMEOUT=60
21+
INTERVAL=2
22+
ELAPSED=0
23+
LOGGER_TAG="mount-volume"
24+
25+
# --- Helper function for echo + logger ---
26+
log() {
27+
echo "$1"
28+
logger -t "$LOGGER_TAG" "$1"
29+
}
30+
31+
log "Starting mount procedure for device $DEVICE$MOUNT_POINT"
32+
33+
# --- Wait for block device ---
34+
log "Waiting for block device $DEVICE to become available..."
35+
while true; do
36+
if [ -b "$DEVICE" ]; then
37+
if blkid "$DEVICE" >/dev/null 2>&1 || true; then
38+
log "$DEVICE is ready"
39+
break
40+
fi
41+
fi
42+
43+
ELAPSED=$((ELAPSED + INTERVAL))
44+
if [ $ELAPSED -ge $TIMEOUT ]; then
45+
log "Error: $DEVICE did not become ready after $TIMEOUT seconds"
46+
exit 3
47+
fi
48+
49+
sleep $INTERVAL
50+
done
2451

52+
# --- Validate device ---
2553
if [ ! -b "$DEVICE" ]; then
26-
echo "Error: Block device '$DEVICE' does not exist."
54+
log "Error: Block device '$DEVICE' does not exist."
2755
exit 2
2856
fi
2957

58+
# --- Safety: refuse to mount over non-empty directory ---
59+
mkdir -p "$MOUNT_POINT"
60+
if [ "$(ls -A "$MOUNT_POINT" 2>/dev/null)" ]; then
61+
if ! mountpoint -q "$MOUNT_POINT"; then
62+
log "Error: Mount point $MOUNT_POINT is not empty. Aborting to protect existing data."
63+
exit 4
64+
fi
65+
fi
66+
67+
# --- Format if needed ---
3068
if ! blkid "$DEVICE" >/dev/null 2>&1; then
31-
echo "Device $DEVICE appears unformatted. Formatting as $FSTYPE..."
69+
log "Device $DEVICE appears unformatted. Formatting as $FSTYPE..."
3270
mkfs."$FSTYPE" -F "$DEVICE"
3371
else
34-
echo "$DEVICE already has a filesystem — skipping format."
72+
log "$DEVICE already has a filesystem — skipping format."
3573
fi
3674

37-
mkdir -p "$MOUNT_POINT"
38-
39-
e2fsck -pf "$DEVICE"
75+
# --- Filesystem check ---
76+
if ! mountpoint -q "$MOUNT_POINT"; then
77+
log "Running e2fsck check on $DEVICE"
78+
e2fsck -pf "$DEVICE" || log "Warning: e2fsck returned non-zero exit code"
79+
fi
4080

81+
# --- Mount ---
4182
if ! mountpoint -q "$MOUNT_POINT"; then
42-
echo "Mounting $DEVICE to $MOUNT_POINT"
83+
log "Mounting $DEVICE to $MOUNT_POINT"
4384
mount -t "$FSTYPE" -o "$MOUNT_OPTS" "$DEVICE" "$MOUNT_POINT"
4485
else
45-
echo "$MOUNT_POINT is already mounted"
86+
log "$MOUNT_POINT is already mounted"
4687
fi
4788

48-
echo "Setting ownership and permissions on $MOUNT_POINT"
89+
# --- Ownership and permissions ---
90+
log "Setting ownership and permissions on $MOUNT_POINT"
4991
chown "$OWNER" "$MOUNT_POINT"
5092
chmod "$PERMISSIONS" "$MOUNT_POINT"
5193

94+
# --- Persist in /etc/fstab ---
5295
UUID=$(blkid -s UUID -o value "$DEVICE")
5396
FSTAB_LINE="UUID=$UUID $MOUNT_POINT $FSTYPE $MOUNT_OPTS 0 2"
5497

5598
if ! grep -q "$UUID" "$FSTAB_FILE"; then
56-
echo "Adding $FSTAB_LINE to $FSTAB_FILE"
99+
log "Adding $FSTAB_LINE to $FSTAB_FILE"
57100
echo "$FSTAB_LINE" >> "$FSTAB_FILE"
58101
else
59-
echo "UUID $UUID already in $FSTAB_FILE — skipping"
102+
log "UUID $UUID already in $FSTAB_FILE — skipping"
60103
fi
61104

62-
echo "Mounted $DEVICE at $MOUNT_POINT with postgres:postgres and mode $PERMISSIONS"
105+
log "Mounted $DEVICE at $MOUNT_POINT with owner=$OWNER and mode=$PERMISSIONS"

0 commit comments

Comments
 (0)