diff --git a/README.md b/README.md index 01d1e9d..c585575 100644 --- a/README.md +++ b/README.md @@ -12,3 +12,4 @@ * [Multicities](./cities.sh): Neocities static site manager with multiple account support * [Rusty Checker](./rusty-checker.sh): quickly check if a public FOSS project is written in Rust (or another language that you pass to the script) * [Image Transfer](./it.sh): a more convenient wrapper around dd for writing and reading removable media images +* [Mount Manager](./mm.sh): a simple script to mount/unmount removable media partitions diff --git a/mm.sh b/mm.sh new file mode 100755 index 0000000..7a142cd --- /dev/null +++ b/mm.sh @@ -0,0 +1,52 @@ +#!/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