bin: ease partition management

This commit is contained in:
xeruf 2021-12-02 18:15:26 +01:00
parent 73dee5e5d2
commit 6bdcf07198
3 changed files with 26 additions and 14 deletions

View File

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

9
.local/bin/scripts/part Executable file
View File

@ -0,0 +1,9 @@
#!/bin/sh
# Run parted with rlwrap
case "$1" in
([a-z]) part=/dev/sd$1;;
("") part= ;;
(*) part=$1;;
esac
shift
sudo rlwrap parted $part "$@"

View File

@ -5,7 +5,7 @@ if test "$1" = "f"
else
IFS="\n"
cols=$(tput cols)
lsblk --output name,size,fsavail,fsuse%,mountpoints,label$(test $cols -gt 110 && echo ',uuid,fstype') --width $cols |
lsblk --output name,size,fsavail,fsuse%,mountpoints,label,fstype$(test $cols -gt 110 && echo ',uuid') --width $cols |
while read line
do p=$(echo "$line" | sed 's|.* \([[:digit:]]\+\)%.*|\1|;t;c0')
printf "$(expr "$line" : " " >/dev/null || echo "\033[$(test -z "$first" && echo "4" || expr "5;31" \& "$p" \> 98 \| "31" \& "$p" \> 97 \| "33" \& "$p" \> 94 \| "35" \& "$p" \> 90)m")%s\n" "$line"