Skip to content

Commit b66218f

Browse files
author
andres.suarez
committed
feat: add script to unmount disk
1 parent b513e82 commit b66218f

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

ansible/files/adminapi.sudoers.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Cmnd_Alias PGBOUNCER = /bin/systemctl start pgbouncer.service, /bin/systemctl st
66

77
%adminapi ALL= NOPASSWD: /root/grow_fs.sh
88
%adminapi ALL= NOPASSWD: /root/mount-volume.sh
9+
%adminapi ALL= NOPASSWD: /root/unmount-volume.sh
910
%adminapi ALL= NOPASSWD: /root/manage_readonly_mode.sh
1011
%adminapi ALL= NOPASSWD: /etc/adminapi/pg_upgrade_scripts/prepare.sh
1112
%adminapi ALL= NOPASSWD: /etc/adminapi/pg_upgrade_scripts/initiate.sh

ansible/files/mount-volume.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ if [[ -z "$DEVICE" || -z "$MOUNT_POINT" ]]; then
1111
exit 1
1212
fi
1313

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+
1419
OWNER="postgres:postgres"
1520
PERMISSIONS="750"
1621
FSTYPE="ext4"

ansible/files/unmount-volume.sh

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/tasks/internal/admin-api.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- { file: "manage_readonly_mode.sh" }
1616
- { file: "pg_egress_collect.pl" }
1717
- { file: "mount-volume.sh" }
18+
- { file: "unmount-volume.sh" }
1819

1920
- name: give adminapi user permissions
2021
copy:

0 commit comments

Comments
 (0)