Skip to content

Commit b513e82

Browse files
author
andres.suarez
committed
feat: add script to mount/unmount disks
1 parent ae29c6f commit b513e82

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

ansible/files/adminapi.sudoers.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Cmnd_Alias GOTRUE = /bin/systemctl start gotrue.service, /bin/systemctl stop got
55
Cmnd_Alias PGBOUNCER = /bin/systemctl start pgbouncer.service, /bin/systemctl stop pgbouncer.service, /bin/systemctl restart pgbouncer.service, /bin/systemctl disable pgbouncer.service, /bin/systemctl enable pgbouncer.service, /bin/systemctl reload pgbouncer.service, /bin/systemctl try-restart pgbouncer.service
66

77
%adminapi ALL= NOPASSWD: /root/grow_fs.sh
8+
%adminapi ALL= NOPASSWD: /root/mount-volume.sh
89
%adminapi ALL= NOPASSWD: /root/manage_readonly_mode.sh
910
%adminapi ALL= NOPASSWD: /etc/adminapi/pg_upgrade_scripts/prepare.sh
1011
%adminapi ALL= NOPASSWD: /etc/adminapi/pg_upgrade_scripts/initiate.sh

ansible/files/mount-volume.sh

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
DEVICE=${1:-}
6+
MOUNT_POINT=${2:-}
7+
8+
if [[ -z "$DEVICE" || -z "$MOUNT_POINT" ]]; then
9+
echo "Usage: $0 <device> <mount_point>"
10+
echo "Example: sudo ./mount-volume.sh /dev/nvme1n1 /data/150008"
11+
exit 1
12+
fi
13+
14+
OWNER="postgres:postgres"
15+
PERMISSIONS="750"
16+
FSTYPE="ext4"
17+
MOUNT_OPTS="defaults"
18+
FSTAB_FILE="/etc/fstab"
19+
20+
if [ ! -b "$DEVICE" ]; then
21+
echo "Error: Block device '$DEVICE' does not exist."
22+
exit 2
23+
fi
24+
25+
if ! blkid "$DEVICE" >/dev/null 2>&1; then
26+
echo "Device $DEVICE appears unformatted. Formatting as $FSTYPE..."
27+
mkfs."$FSTYPE" -F "$DEVICE"
28+
else
29+
echo "$DEVICE already has a filesystem — skipping format."
30+
fi
31+
32+
mkdir -p "$MOUNT_POINT"
33+
34+
if ! mountpoint -q "$MOUNT_POINT"; then
35+
echo "Mounting $DEVICE to $MOUNT_POINT"
36+
mount -t "$FSTYPE" -o "$MOUNT_OPTS" "$DEVICE" "$MOUNT_POINT"
37+
else
38+
echo "$MOUNT_POINT is already mounted"
39+
fi
40+
41+
echo "Setting ownership and permissions on $MOUNT_POINT"
42+
chown "$OWNER" "$MOUNT_POINT"
43+
chmod "$PERMISSIONS" "$MOUNT_POINT"
44+
45+
UUID=$(blkid -s UUID -o value "$DEVICE")
46+
FSTAB_LINE="UUID=$UUID $MOUNT_POINT $FSTYPE $MOUNT_OPTS 0 2"
47+
48+
if ! grep -q "$UUID" "$FSTAB_FILE"; then
49+
echo "Adding $FSTAB_LINE to $FSTAB_FILE"
50+
echo "$FSTAB_LINE" >> "$FSTAB_FILE"
51+
else
52+
echo "UUID $UUID already in $FSTAB_FILE — skipping"
53+
fi
54+
55+
echo "Mounted $DEVICE at $MOUNT_POINT with postgres:postgres and mode $PERMISSIONS"

ansible/tasks/internal/admin-api.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- { file: "grow_fs.sh" }
1515
- { file: "manage_readonly_mode.sh" }
1616
- { file: "pg_egress_collect.pl" }
17+
- { file: "mount-volume.sh" }
1718

1819
- name: give adminapi user permissions
1920
copy:

0 commit comments

Comments
 (0)