2023-04-21 09:26:34 +02:00
|
|
|
#!/bin/sh
|
2021-10-27 10:00:54 +02:00
|
|
|
# Mount a partition by label or device identifier automatically
|
2023-04-07 19:33:05 +02:00
|
|
|
# Includes interactive selection if no argument is given
|
2023-11-07 10:56:27 +01:00
|
|
|
# TODO losetup for raw and qcow2, including lvm handling
|
2022-09-09 21:49:37 +02:00
|
|
|
set -eo pipefail
|
2022-02-12 09:36:09 +01:00
|
|
|
_help="$0 <device> [mountpoint] [options...]"
|
|
|
|
case $1 in
|
2023-04-21 09:26:34 +02:00
|
|
|
(-v) shift
|
|
|
|
bash -x "$0" "$@";;
|
|
|
|
("") # TODO include size
|
|
|
|
arg=$(
|
|
|
|
{
|
|
|
|
#for ip in $(sudo nmblookup -S \* | grep '^[0-9]' | cut -d' ' -f1)
|
|
|
|
#do timeout --kill-after=.1s .1s showmount --no-headers --exports "$ip" 2>/dev/null | sed "s|\([^ ]*\) .*|$ip:\1|"
|
|
|
|
#done &
|
|
|
|
# TODO handle labels with spaces
|
|
|
|
lsblk --list --noheadings --output name,label,fstype,mountpoint | grep -v LUKS | grep -v '/' | grep ".\+ [^ ]\+";
|
|
|
|
} | fzf --exit-0 | sed "s/^\([^ ]\+ \+\)\?\([^ ]\+\) \+[^ ]\+ *$/\2/"
|
|
|
|
);;
|
|
|
|
(--help) echo "$_help" && exit 0;;
|
|
|
|
(-u) shift
|
|
|
|
for last; do true; done
|
|
|
|
sudo umount --verbose "$@"
|
|
|
|
code=$?
|
|
|
|
sudo rm -df "$last"
|
|
|
|
exit $code;;
|
|
|
|
(*) arg=$1
|
|
|
|
shift;;
|
2022-02-12 09:36:09 +01:00
|
|
|
esac
|
2021-10-31 20:30:52 +01:00
|
|
|
|
2022-02-12 09:36:09 +01:00
|
|
|
# FSTAB: BY LABEL
|
|
|
|
if grep --word-regexp "LABEL=$arg" /etc/fstab
|
2022-01-31 10:36:25 +01:00
|
|
|
then # have to mount twice as the first one might be creating the directory
|
2023-04-21 09:26:34 +02:00
|
|
|
mount -L "$arg" "$@" 2>/dev/null || mount -L "$arg" "$@"
|
|
|
|
exit $?
|
2021-11-09 05:17:51 +01:00
|
|
|
fi
|
2022-02-12 09:36:09 +01:00
|
|
|
# FSTAB: BY MOUNTPOINT/NAME
|
|
|
|
if grep --word-regexp "$arg" /etc/fstab
|
2022-12-22 15:40:56 +01:00
|
|
|
then mount "$arg" "$@"; exit $?
|
2021-11-09 05:17:51 +01:00
|
|
|
fi
|
2022-02-12 09:36:09 +01:00
|
|
|
|
|
|
|
# MANUALLY
|
2021-12-05 20:49:32 +01:00
|
|
|
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
|
2022-09-09 21:49:37 +02:00
|
|
|
then test $# -gt 0 && shift
|
2023-04-21 09:26:34 +02:00
|
|
|
mount "$mountpoint" "$@"
|
|
|
|
code=$?
|
|
|
|
cd $mountpoint
|
|
|
|
exit $code
|
2021-11-09 05:17:51 +01:00
|
|
|
fi
|
2021-12-02 18:15:26 +01:00
|
|
|
case "$arg" in
|
2023-04-21 09:26:34 +02:00
|
|
|
(sd*|loop*|nvme*|mm*|md*|dm*|vg*) partition="/dev/$arg";;
|
|
|
|
(/dev/*) partition="$arg";;
|
|
|
|
(*) params="-L"
|
|
|
|
partition="$arg";;
|
2021-11-04 12:19:43 +01:00
|
|
|
esac
|
2022-09-09 21:49:37 +02:00
|
|
|
shift $(expr 2 \& $# \> 2 \| $#)
|
2021-10-31 20:30:52 +01:00
|
|
|
#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"
|
2023-04-21 09:26:34 +02:00
|
|
|
sudo mount -vo users,X-mount.mkdir,noatime,umask=003,gid=users,uid=$USER $params "$partition" "$mountpoint" "$@" ||
|
|
|
|
sudo mount -vo users,X-mount.mkdir,noatime $params "$partition" "$mountpoint" "$@"
|
2021-12-16 23:04:50 +01:00
|
|
|
fi
|
2021-10-27 10:00:54 +02:00
|
|
|
cd $mountpoint
|
|
|
|
exec $SHELL
|