-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmountman
More file actions
executable file
·120 lines (102 loc) · 3.64 KB
/
mountman
File metadata and controls
executable file
·120 lines (102 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env bash
#
# _
# _ __ ___ ___ _ _ _ __ | |_ _ __ ___ __ _ _ __
# | '_ ` _ \\ / _ \\| | | | '_ \\| __| '_ ` _ \\ / _` | '_ \\
# | | | | | | (_) | |_| | | | | |_| | | | | | (_| | | | |
# |_| |_| |_|\\___/ \\__,_|_| |_|\\__|_| |_| |_|\\__,_|_| |_|
#
# Script : mountman
# Location : /usr/local/bin/mountman
# Author : Jens Ochmann
# Date : 2025-04-20
# License : MIT
# Description : Mount / Unmount removable devices.
# mountman → list USB / RM=1 devices (excl. loop)
# mountman -a|--all → list all block devices.
# Requires root : Elevates via sudo if needed
#
set -euo pipefail
# ---------- colours (fallback) ----------
RED=${RED:-"\033[0;31m"}
GREEN=${GREEN:-"\033[0;32m"}
YELLOW=${YELLOW:-"\033[0;33m"}
BLUE=${BLUE:-"\033[0;34m"}
NC=${NC:-"\033[0m"}
info() { printf "%b%s%b\n" "${YELLOW}" "$*" "${NC}"; }
ok() { printf "%b%s%b\n" "${GREEN}" "$*" "${NC}"; }
err() { printf "%b%s%b\n" "${RED}" "$*" "${NC}" >&2; }
# ---------- root elevation -------------
if (( EUID != 0 )); then
exec sudo --preserve-env=RED,GREEN,YELLOW,BLUE,NC "$0" "$@"
fi
# ---------- CLI options ----------------
SHOW_ALL=false
while [[ ${1:-} ]]; do
case $1 in
-a|--all) SHOW_ALL=true; shift ;;
-h|--help) echo "Usage: mountman [-a|--all]"; exit 0 ;;
*) err "Unknown option: $1"; exit 1 ;;
esac
done
# ---------- config file (optional) -----
if [[ -n ${SUDO_USER:-} ]]; then
USER_HOME=$(getent passwd "$SUDO_USER" | cut -d: -f6)
else
USER_HOME="$HOME"
fi
CONFIG_FILE="${USER_HOME}/.config/sc.conf"
[[ -f "$CONFIG_FILE" ]] && source "$CONFIG_FILE" || true
MOUNT_BASE="/media/${SUDO_USER:-$USER}"
# ---------- helpers --------------------
list_block_devices() { lsblk -rno NAME,RM,TRAN,SIZE,MOUNTPOINT; }
fmt() { printf "%-12s %-6s %-6s %s\n" "$1" "$4" "$3" "${5:-}"; }
# fields: 1-name 2-rm 3-tran 4-size 5-mnt (see while read order)
mount_device() {
local dev="$1" label
label=$(lsblk -rno LABEL "/dev/$dev" || true)
[[ -z $label ]] && label="$dev"
label=$(tr -cd '[:alnum:]' <<<"$label")
local mnt="${MOUNT_BASE}/${label}"
mkdir -p "$mnt"
mount "/dev/$dev" "$mnt" && ok "Mounted /dev/$dev → $mnt" || err "Mount failed"
}
unmount_device() {
local dev="$1" mnt
mnt=$(lsblk -rno MOUNTPOINT "/dev/$dev" || true)
[[ -z $mnt ]] && { err "/dev/$dev is not mounted"; return; }
umount -l "$mnt" && ok "Unmounted $mnt" || err "Failed to unmount"
}
is_candidate() {
local name=$1 rm=$2 tran=$3
$SHOW_ALL && return 0
[[ $name == loop* ]] && return 1 # exclude loop devices by default
[[ $rm == 1 ]] && return 0 # removable flag
[[ $tran == usb ]] && return 0 # usb transport
return 1
}
select_device() {
local -a menu=()
while read -r name rm tran size mnt; do
if is_candidate "$name" "$rm" "$tran"; then
menu+=("$(fmt "$name" "$rm" "$tran" "$size" "$mnt")")
fi
done < <(list_block_devices)
(( ${#menu[@]} )) || { err "No suitable block devices found."; exit 1; }
printf "%s\n" "${menu[@]}" | nl -w2 -s": "
read -rp "Select device [1-${#menu[@]}]: " idx
[[ $idx =~ ^[0-9]+$ ]] && (( idx>=1 && idx<=${#menu[@]} )) || { err "Invalid choice"; exit 1; }
printf '%s\n' "${menu[idx-1]}" | awk '{print $1}'
}
# ---------- main -----------------------
clear
printf "\n%sMountman – manage removable devices%s\n\n" "$BLUE" "$NC"
device=$(select_device)
info "Chosen device: /dev/$device"
echo -e "${YELLOW}1) Mount\n2) Unmount${NC}"
read -rp "Select action [1/2]: " action
case "$action" in
1) mount_device "$device" ;;
2) unmount_device "$device" ;;
*) err "Invalid action" ;;
esac