Files
sh-goodies/mm.sh
T
2026-02-02 14:09:03 +02:00

53 lines
1.8 KiB
Bash
Executable File

#!/bin/sh
# Mount Manager: a simple script to mount/unmount removable media partitions
# in a more convenient manner
# Depends on basename, id, grep, mount, fzf
# Usage: mm.sh (dir-to-mount-override)
# Created by Luxferre in 2026, released into public domain
get-partitions() {
for ddir in /sys/block/*; do
if grep -q 1 ${ddir}/removable; then
dbase="$(basename $ddir)"
dvendor="$(cat ${ddir}/device/vendor)"
dmodel="$(cat ${ddir}/device/model)"
for p in ${ddir}/${dbase}*; do
pname="$(basename $p)"
ismounted="unmounted"
grep -q "/dev/$pname" /proc/mounts && ismounted="-mounted-"
printf "%s\t%s\t%s %s (%s)\n" "$pname" "$ismounted" "$dvendor" "$dmodel" "$dbase"
done
fi
done
}
MOUNT_DIR="$1"
[ -z "$MOUNT_ROOT" ] && MOUNT_ROOT="/mnt"
[ -z "$SUDO" ] && SUDO="sudo"
MOUNT_OPTS="user,rw,noatime,uid=$(id -u),gid=$(id -g)"
PART_LINE="$(get-partitions | fzf --prompt "Select the partition: ")"
[ -z "$PART_LINE" ] && echo "No partition selected, quitting!" && exit 1
TARGET_PART="$(echo "$PART_LINE" | cut -f 1)"
IS_MOUNTED="$(echo "$PART_LINE" | cut -f 2)"
[ -z "$MOUNT_DIR" ] && MOUNT_DIR="${MOUNT_ROOT}/${TARGET_PART}"
TARGET_DEV="/dev/${TARGET_PART}"
RUN_CMD_1="$SUDO mkdir -p $MOUNT_DIR"
RUN_CMD_2="$SUDO mount -o $MOUNT_OPTS $TARGET_DEV $MOUNT_DIR"
if [ "$IS_MOUNTED" = "-mounted-" ]; then # unmount
RUN_CMD_1="$SUDO umount $TARGET_DEV"
MOUNT_DIR="$(grep $TARGET_DEV /proc/mounts | cut -f 2 -d ' ')"
RUN_CMD_2="$SUDO rmdir $MOUNT_DIR"
fi
echo "Starting the operation..."
if $RUN_CMD_1 && $RUN_CMD_2; then
echo "Operation successful!"
if [ "$IS_MOUNTED" = "-mounted-" ]; then # unmount
echo "Partition $TARGET_PART was unmounted"
else
echo "Partition $TARGET_PART mounted into $MOUNT_DIR"
fi
else
echo "Operation completed with errors!"
fi