dotfiles/.local/bin/scripts/moul

56 lines
1.6 KiB
Plaintext
Raw Normal View History

2022-09-09 19:49:37 +00:00
#!/bin/sh
2021-10-27 08:00:54 +00:00
# Mount a partition by label or device identifier automatically
2022-09-09 19:49:37 +00:00
set -eo pipefail
_help="$0 <device> [mountpoint] [options...]"
case $1 in
2022-11-23 01:28:30 +00:00
(-v) shift
bash -x "$0" "$@";;
2022-09-16 12:48:43 +00:00
("") # TODO include size
arg=$(lsblk --list --noheadings --output name,label,fstype,mountpoint |
2022-09-09 19:49:37 +00:00
grep -v '/' | grep ".\+ [^ ]\+" | fzf --select-1 --exit-0 |
sed "s/^\([^ ]\+ \+\)\?\([^ ]\+\) \+[^ ]\+ *$/\2/");;
2022-11-23 01:28:30 +00:00
(--help) echo "$_help" && exit 0;;
(-u) shift
for last; do true; done
sudo umount --verbose "$@"
code=$?
sudo rm -df "$last"
exit $code;;
2022-09-09 19:49:37 +00:00
(*) arg=$1;;
esac
# FSTAB: BY LABEL
if grep --word-regexp "LABEL=$arg" /etc/fstab
2022-01-31 09:36:25 +00:00
then # have to mount twice as the first one might be creating the directory
2022-10-15 00:08:53 +00:00
mount -L "$arg" "$@" 2>/dev/null || mount -L "$arg" "$@"
2022-01-31 09:36:25 +00:00
exit $?
2021-11-09 04:17:51 +00:00
fi
# FSTAB: BY MOUNTPOINT/NAME
if grep --word-regexp "$arg" /etc/fstab
2021-11-09 04:17:51 +00:00
then mount "$@"; exit $?
fi
# MANUALLY
mountpoint="${2:-${MNT:-${XDG_RUNTIME_DIR}/mnt}/$(basename "$arg")}"
2021-11-09 04:17:51 +00:00
if grep -e "[^\w=/]$mountpoint[^\w/]" /etc/fstab
2022-09-09 19:49:37 +00:00
then test $# -gt 0 && shift
mount "$mountpoint" "$@"
code=$?
cd $mountpoint
exit $code
2021-11-09 04:17:51 +00:00
fi
2021-12-02 17:15:26 +00:00
case "$arg" in
(sd*|loop*|nvme*|mm*|md*|dm*|vg*) partition="/dev/$arg";;
(/dev/*) partition="$arg";;
(*) partition="-L $arg";;
2021-11-04 11:19:43 +00:00
esac
2022-09-09 19:49:37 +00:00
shift $(expr 2 \& $# \> 2 \| $#)
#uid=$(id --user),gid=$(id --group), \
2021-12-16 22:04:50 +00:00
if ! mountpoint "$mountpoint" 2>/dev/null
then mp="/run/media/$USER/$arg" && test -e "$mp" && mountpoint="$mp"
2022-11-23 01:28:30 +00:00
sudo mount -vo users,X-mount.mkdir,noatime,umask=003,gid=users,uid=$USER $partition $mountpoint "$@" ||
sudo mount -vo users,X-mount.mkdir,noatime $partition $mountpoint "$@"
2021-12-16 22:04:50 +00:00
fi
2021-10-27 08:00:54 +00:00
cd $mountpoint
exec $SHELL