Skip to content

Commit d0fbf41

Browse files
valigulaandres.suarez
andauthored
feat: add script to mount/unmount disks (#1727)
* feat: add script to mount/unmount disks * feat: add script to unmount disk * fix: rmove files to correct path * feat: validating the disk in a state --------- Co-authored-by: andres.suarez <andres.suarez@supabase.io>
1 parent ae29c6f commit d0fbf41

File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
# 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+
19+
OWNER="postgres:postgres"
20+
PERMISSIONS="750"
21+
FSTYPE="ext4"
22+
MOUNT_OPTS="defaults"
23+
FSTAB_FILE="/etc/fstab"
24+
25+
if [ ! -b "$DEVICE" ]; then
26+
echo "Error: Block device '$DEVICE' does not exist."
27+
exit 2
28+
fi
29+
30+
if ! blkid "$DEVICE" >/dev/null 2>&1; then
31+
echo "Device $DEVICE appears unformatted. Formatting as $FSTYPE..."
32+
mkfs."$FSTYPE" -F "$DEVICE"
33+
else
34+
echo "$DEVICE already has a filesystem — skipping format."
35+
fi
36+
37+
mkdir -p "$MOUNT_POINT"
38+
39+
e2fsck -pf "$DEVICE"
40+
41+
if ! mountpoint -q "$MOUNT_POINT"; then
42+
echo "Mounting $DEVICE to $MOUNT_POINT"
43+
mount -t "$FSTYPE" -o "$MOUNT_OPTS" "$DEVICE" "$MOUNT_POINT"
44+
else
45+
echo "$MOUNT_POINT is already mounted"
46+
fi
47+
48+
echo "Setting ownership and permissions on $MOUNT_POINT"
49+
chown "$OWNER" "$MOUNT_POINT"
50+
chmod "$PERMISSIONS" "$MOUNT_POINT"
51+
52+
UUID=$(blkid -s UUID -o value "$DEVICE")
53+
FSTAB_LINE="UUID=$UUID $MOUNT_POINT $FSTYPE $MOUNT_OPTS 0 2"
54+
55+
if ! grep -q "$UUID" "$FSTAB_FILE"; then
56+
echo "Adding $FSTAB_LINE to $FSTAB_FILE"
57+
echo "$FSTAB_LINE" >> "$FSTAB_FILE"
58+
else
59+
echo "UUID $UUID already in $FSTAB_FILE — skipping"
60+
fi
61+
62+
echo "Mounted $DEVICE at $MOUNT_POINT with postgres:postgres and mode $PERMISSIONS"
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
MOUNT_POINT=${1:-}
6+
DELETE_FLAG=${2:-}
7+
8+
if [[ -z "$MOUNT_POINT" ]]; then
9+
echo "Usage: $0 <mount_point> [--delete-dir]"
10+
echo "Unmount only: sudo ./unmount-volume.sh /data/150008"
11+
echo "Unmount delete dir: sudo ./unmount-volume.sh /data/150008 --delete-dir"
12+
exit 1
13+
fi
14+
15+
# Unmount a block device from a specified mount point
16+
# Remove the corresponding entry from /etc/fstab for persistence across reboots
17+
18+
FSTAB_FILE="/etc/fstab"
19+
BACKUP_FILE="/etc/fstab.bak"
20+
21+
if mountpoint -q "$MOUNT_POINT"; then
22+
echo "Unmounting $MOUNT_POINT"
23+
umount "$MOUNT_POINT"
24+
else
25+
echo "$MOUNT_POINT is not currently mounted — skipping umount"
26+
fi
27+
28+
UUID=$(findmnt -no UUID "$MOUNT_POINT" 2>/dev/null || true)
29+
30+
if [[ -n "$UUID" ]]; then
31+
echo "Removing UUID=$UUID from $FSTAB_FILE"
32+
cp "$FSTAB_FILE" "$BACKUP_FILE"
33+
sed -i "/UUID=${UUID//\//\\/}/d" "$FSTAB_FILE"
34+
else
35+
echo "Could not find UUID for $MOUNT_POINT — skipping fstab cleanup"
36+
fi
37+
38+
if [[ "$DELETE_FLAG" == "--delete-dir" ]]; then
39+
echo "Deleting mount point directory: $MOUNT_POINT"
40+
rm -rf "$MOUNT_POINT"
41+
fi
42+
43+
echo "Unmount and cleanup complete for $MOUNT_POINT"

ansible/files/adminapi.sudoers.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ 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
9+
%adminapi ALL= NOPASSWD: /root/unmount-volume.sh
810
%adminapi ALL= NOPASSWD: /root/manage_readonly_mode.sh
911
%adminapi ALL= NOPASSWD: /etc/adminapi/pg_upgrade_scripts/prepare.sh
1012
%adminapi ALL= NOPASSWD: /etc/adminapi/pg_upgrade_scripts/initiate.sh

ansible/tasks/internal/admin-api.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
- { file: "grow_fs.sh" }
1515
- { file: "manage_readonly_mode.sh" }
1616
- { file: "pg_egress_collect.pl" }
17+
- { file: "mount-volume.sh" }
18+
- { file: "unmount-volume.sh" }
1719

1820
- name: give adminapi user permissions
1921
copy:

0 commit comments

Comments
 (0)