dotfiles/.local/bin/scripts/moul

40 lines
1.2 KiB
Text
Raw Normal View History

2021-10-27 10:00:54 +02:00
#!/bin/sh -e
# Mount a partition by label or device identifier automatically
2022-01-15 22:01:18 +01:00
# TODO Autocomplete: lsblk --output label --noheadings
2021-12-02 18:15:26 +01:00
arg=$1
test "$arg" = "--help" && echo "$0 <device> [mountpoint] [options...]" && exit 0
if test "$arg" = "-u"
2021-11-09 05:17:51 +01:00
then
mountpoint="$2"
sudo umount --verbose "$mountpoint"
code=$?
sudo rm -df "$mountpoint"
exit $code
fi
2022-01-08 21:47:30 +01:00
if grep -e "^LABEL=$arg[^\w/]" /etc/fstab
2022-01-31 10:36:25 +01:00
then # have to mount twice as the first one might be creating the directory
mount -L "$@" 2>/dev/null || mount -L "$@"
exit $?
2021-11-09 05:17:51 +01:00
fi
2021-12-02 18:15:26 +01:00
if grep -e "[^\w=/]$arg[^\w/]" /etc/fstab
2021-11-09 05:17:51 +01:00
then mount "$@"; exit $?
fi
mountpoint="${2:-${MNT:-${XDG_RUNTIME_DIR}/mnt}/$(basename "$arg")}"
2021-11-09 05:17:51 +01:00
if grep -e "[^\w=/]$mountpoint[^\w/]" /etc/fstab
then shift; mount "$mountpoint" "$@"; code=$?; cd $mountpoint; exit $code
fi
2021-12-02 18:15:26 +01:00
case "$arg" in
(sd*|loop*|nvme*|mm*|md*|dm*|vg*) partition="/dev/$arg";;
(/dev/*) partition="$arg";;
(*) partition="-L $arg";;
2021-11-04 12:19:43 +01:00
esac
shift $(expr 2 \& $# \> 1 \| 1)
#uid=$(id --user),gid=$(id --group), \
2021-12-16 23:04:50 +01:00
if ! mountpoint "$mountpoint" 2>/dev/null
then mp="/run/media/$USER/$arg" && test -e "$mp" && mountpoint="$mp"
2021-12-02 18:15:26 +01:00
sudo mount -vo users,X-mount.mkdir,noatime $partition $mountpoint "$@"
2021-12-16 23:04:50 +01:00
fi
2021-10-27 10:00:54 +02:00
cd $mountpoint
exec $SHELL