dotfiles/.local/bin/scripts/moul

40 lines
1.2 KiB
Bash
Executable File

#!/bin/sh -e
# Mount a partition by label or device identifier automatically
# TODO Autocomplete: lsblk --output label --noheadings
arg=$1
test "$arg" = "--help" && echo "$0 <device> [mountpoint] [options...]" && exit 0
if test "$arg" = "-u"
then
mountpoint="$2"
sudo umount --verbose "$mountpoint"
code=$?
sudo rm -df "$mountpoint"
exit $code
fi
if grep -e "^LABEL=$arg[^\w/]" /etc/fstab
then # have to mount twice as the first one might be creating the directory
mount -L "$@" 2>/dev/null || mount -L "$@"
exit $?
fi
if grep -e "[^\w=/]$arg[^\w/]" /etc/fstab
then mount "$@"; exit $?
fi
mountpoint="${2:-${MNT:-${XDG_RUNTIME_DIR}/mnt}/$(basename "$arg")}"
if grep -e "[^\w=/]$mountpoint[^\w/]" /etc/fstab
then shift; mount "$mountpoint" "$@"; code=$?; cd $mountpoint; exit $code
fi
case "$arg" in
(sd*|loop*|nvme*|mm*|md*|dm*|vg*) partition="/dev/$arg";;
(/dev/*) partition="$arg";;
(*) partition="-L $arg";;
esac
shift $(expr 2 \& $# \> 1 \| 1)
#uid=$(id --user),gid=$(id --group), \
if ! mountpoint "$mountpoint" 2>/dev/null
then mp="/run/media/$USER/$arg" && test -e "$mp" && mountpoint="$mp"
sudo mount -vo users,X-mount.mkdir,noatime $partition $mountpoint "$@"
fi
cd $mountpoint
exec $SHELL