2021-05-12 18:00:43 -07:00
#!/bin/bash
set -eu -o pipefail
if [ [ ${ EUID } -ne 0 ] ] ; then
echo "This script should be run as root." > /dev/stderr
exit 1
fi
if [ [ $# -eq 0 ] ] ; then
echo "No arguments supplied"
exit 1
fi
if [ [ " $1 " = = "--check" ] ] ; then
echo "OK"
exit 0
fi
mount_file_contents = " $1 "
2021-06-21 12:11:05 -07:00
timeout = " $2 " # seconds
2021-05-12 18:00:43 -07:00
# mount units must be named after the mount point directories they control
where = $( echo " ${ mount_file_contents } " | grep "^Where=" | cut -d'=' -f 2)
mount_filename = $( systemd-escape -p --suffix= mount " $where " )
2021-05-21 17:31:54 -07:00
mount_file = " /etc/systemd/system/ ${ mount_filename } "
2021-05-12 18:00:43 -07:00
2021-06-21 12:11:05 -07:00
# cleanup any previous mount of same name (after midway box crash?)
2021-06-22 09:53:31 -07:00
if systemctl -q is-active " ${ mount_filename } " ; then
2023-09-29 06:49:55 +05:30
echo " Previous mount ${ mount_filename } active, unmounting "
2021-06-22 09:53:31 -07:00
# unmounting can fail if a user is cd'ed into the directory. if we go ahead and mount anyway, systemd says "active" because it's referring to the previous mount config
if ! systemctl stop " ${ mount_filename } " ; then
echo "Failed to unmount"
exit 2
fi
2021-06-21 12:11:05 -07:00
fi
2021-05-21 17:31:54 -07:00
echo " $mount_file_contents " > " ${ mount_file } "
2021-05-12 18:00:43 -07:00
systemctl daemon-reload
2021-05-26 23:01:05 -07:00
2021-06-21 12:11:05 -07:00
if ! timeout " ${ timeout } " systemctl enable --now " ${ mount_filename } " ; then
echo "Failed to mount"
2021-06-22 09:53:31 -07:00
exit 3
2021-06-21 12:11:05 -07:00
fi
2023-09-29 06:49:55 +05:30
echo " Mount ${ mount_filename } succeeded "
2021-05-26 23:01:05 -07:00
2021-06-21 12:11:05 -07:00
# this has to be done post-mount because permissions come from the underlying mount file system and not the mount point
chmod 777 " ${ where } "