dotfiles-ansible/roles/scripts/files/bin/nmount

50 lines
1.2 KiB
Bash
Executable File

#!/bin/sh
# Need to run as normal user so that $USER works
# Use when udisks is not available in the system
#
# Usage: nmount [-u] /dev/sda1
if [ "$#" -lt 1 ]; then
echo "Not enough arguments"
exit 1
fi
mode='mount'
if [ "$1" = "-u" ]; then
device="$2"
mode='unmount'
else
device="$1"
fi
if [ -z "$device" ]; then
echo "Need the device name as argument."
exit 1
fi
# Ensure the argument is a valid device
if [ -z "$(mountpoint -x "${device}")" ]; then
echo "The argument is not a valid device"
exit 1
fi
# Make the mount dir compatible with udisks (for Waybar's media.sh script)
parent="/run/media/$USER"
if [ "${mode}" = "mount" ]; then
umask 0022
device_name="${device##*/}"
[ ! -d "${parent}" ] && doas mkdir -p -m 0755 "${parent}"
mount_point=$(doas mktemp -d "${parent}/${device_name}-XXXXXX")
doas mount -o rw,nosuid,nodev,noexec "${device}" "${mount_point}"
else
mount_point=$(findmnt -l -o TARGET,SOURCE | grep -F "${device}" | awk '{print $1}')
doas umount "${device}"
# Clean the mount point if it is in /run/media/$USER
if echo "${mount_point}" | grep -qF "${parent}"; then
doas rm -rf "${mount_point}"
fi
fi