11#! /usr/bin/env bash
2-
32set -euo pipefail
43
54DEVICE=${1:- }
@@ -8,55 +7,99 @@ MOUNT_POINT=${2:-}
87if [[ -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
1213fi
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-
1915OWNER=" postgres:postgres"
2016PERMISSIONS=" 750"
2117FSTYPE=" ext4"
2218MOUNT_OPTS=" defaults"
2319FSTAB_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 ---
2553if [ ! -b " $DEVICE " ]; then
26- echo " Error: Block device '$DEVICE ' does not exist."
54+ log " Error: Block device '$DEVICE ' does not exist."
2755 exit 2
2856fi
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 ---
3068if ! 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 "
3371else
34- echo " $DEVICE already has a filesystem — skipping format."
72+ log " $DEVICE already has a filesystem — skipping format."
3573fi
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 ---
4182if ! 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 "
4485else
45- echo " $MOUNT_POINT is already mounted"
86+ log " $MOUNT_POINT is already mounted"
4687fi
4788
48- echo " Setting ownership and permissions on $MOUNT_POINT "
89+ # --- Ownership and permissions ---
90+ log " Setting ownership and permissions on $MOUNT_POINT "
4991chown " $OWNER " " $MOUNT_POINT "
5092chmod " $PERMISSIONS " " $MOUNT_POINT "
5193
94+ # --- Persist in /etc/fstab ---
5295UUID=$( blkid -s UUID -o value " $DEVICE " )
5396FSTAB_LINE=" UUID=$UUID $MOUNT_POINT $FSTYPE $MOUNT_OPTS 0 2"
5497
5598if ! 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 "
58101else
59- echo " UUID $UUID already in $FSTAB_FILE — skipping"
102+ log " UUID $UUID already in $FSTAB_FILE — skipping"
60103fi
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