-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathget-mountpoint.sh
More file actions
executable file
·343 lines (304 loc) · 9.99 KB
/
Copy pathget-mountpoint.sh
File metadata and controls
executable file
·343 lines (304 loc) · 9.99 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/bin/sh
########################################################################
# get-mountpoint.sh: Resolve mountpoint from a block device
#
# Description:
# This utility takes a block device path (e.g. /dev/sdc, /dev/sdc1,
# /dev/mapper/truecrypt1, /dev/dm-0) and determines the mountpoint
# where it is currently mounted. It prints a single mountpoint path
# to stdout, making it suitable for both interactive checks and
# automation scripts.
#
# Features:
# - Accepts base disks, partitions, and device-mapper paths.
# - Searches both the device itself and its ancestors/descendants
# and returns the best matching mountpoint deterministically.
# - Uses findmnt(8) primarily; falls back to lsblk(8) scanning.
# - Handles MOUNTPOINTS column used by newer util-linux.
# - Deterministic exit codes for robust scripting.
#
# Requirements:
# - Linux, findmnt(8), lsblk(8), awk(1), mktemp(1)
#
# Notes:
# - Designed for Linux systems with lsblk(8) and findmnt(8) available.
# - Selection policy for multiple candidates:
# A) Prefer the candidate with the greatest dependency depth
# from the base device (deepest descendant).
# B) Tie breaker 1: prefer root mountpoint "/".
# C) Tie breaker 2: prefer non-removable-like FS over vfat/msdos/exfat/iso9660/squashfs.
#
# Author: id774 (More info: http://id774.net)
# Source Code: https://github.com/id774/scripts
# License: The GPL version 3, or LGPL version 3 (Dual License).
# Contact: idnanashi@gmail.com
#
# Usage:
# get-mountpoint.sh <device>
# Examples:
# get-mountpoint.sh /dev/sdc1
# => /mnt/disk1
# get-mountpoint.sh /dev/sde
# => / # when the deepest descendant mounts "/"
#
# Error Conditions:
# 0. Success.
# 1. General failure.
# 2. Device not found or not mounted.
# 3. Path is not a block device.
# 126. Required command is not executable.
# 127. Required command is not installed.
#
# Version History:
# v1.2 2025-12-13
# Resolve symlinked device path before block-device check to ensure correct handling of /dev/disk/by-*.
# Add explicit Requirements section for findmnt, lsblk, awk, mktemp.
# v1.1 2025-11-09
# Remove shared fail function and inline all error handling to comply with implementation policy.
# v1.0 2025-08-31
# Initial release.
#
########################################################################
# Display full script header information extracted from the top comment block
usage() {
awk '
BEGIN { in_header = 0 }
/^#{10,}$/ { if (!in_header) { in_header = 1; next } else exit }
in_header && /^# ?/ { print substr($0, 3) }
' "$0"
exit 0
}
# Check if the system is Linux
check_system() {
if [ "$(uname -s 2>/dev/null)" != "Linux" ]; then
echo "[ERROR] This script is intended for Linux systems only." >&2
exit 1
fi
}
# Check if required commands are available and executable
check_commands() {
for cmd in "$@"; do
cmd_path=$(command -v "$cmd" 2>/dev/null)
if [ -z "$cmd_path" ]; then
echo "[ERROR] Command '$cmd' is not installed. Please install $cmd and try again." >&2
exit 127
elif [ ! -x "$cmd_path" ]; then
echo "[ERROR] Command '$cmd' is not executable. Please check the permissions." >&2
exit 126
fi
done
}
# Validate CLI arguments and device existence
validate_args() {
if [ "$#" -ne 1 ]; then
echo "[ERROR] Exactly one device argument is required." >&2
exit 2
fi
DEV="$1"
# Allow symlinks like /dev/disk/by-id/...; ensure it exists
if [ ! -e "$DEV" ]; then
echo "[ERROR] Device path does not exist: $DEV" >&2
exit 2
fi
# Resolve to the real block device before checking type
REAL_DEV=$(readlink -f -- "$DEV" 2>/dev/null || printf '%s' "$DEV")
# Require a block device (regular file paths are not accepted)
if [ ! -b "$REAL_DEV" ]; then
echo "[ERROR] Not a block device: $DEV" >&2
exit 3
fi
DEVICE="$REAL_DEV"
return 0
}
# Try to obtain mountpoint directly from the device
find_mountpoint_direct() {
SRC="$1"
MP=$(findmnt -nr -S "$SRC" -o TARGET 2>/dev/null || true)
if [ -n "$MP" ]; then
printf '%s\n' "$MP"
return 0
fi
return 1
}
# List dependency chain ancestors for a device (upwards to base)
# and descendants (children) for a base disk. Output: one NAME per line.
list_related_devices() {
DEVPATH="$1"
lsblk -rnp -o NAME -s -- "$DEVPATH" 2>/dev/null
lsblk -rnp -o NAME -- "$DEVPATH" 2>/dev/null
}
# Parse NAME,FSTYPE,MOUNTPOINTS and return NAME<TAB>FSTYPE<TAB>FIRST_MOUNTPOINT
get_name_fstype_first_mp() {
lsblk -rpn -o NAME,FSTYPE,MOUNTPOINTS -P -- "$1" 2>/dev/null | awk '
{
nm=""; mp=""; fs="";
for (i = 1; i <= NF; i++) {
if ($i ~ /^NAME="/) nm = $i;
else if ($i ~ /^FSTYPE="/) fs = $i;
else if ($i ~ /^MOUNTPOINTS="/) mp = $i;
}
if (nm != "" && mp != "") {
gsub(/^[^=]*="/, "", nm); gsub(/"$/, "", nm);
gsub(/^[^=]*="/, "", mp); gsub(/"$/, "", mp);
n = split(mp, arr, /[[:space:]]+/);
if (n >= 1) {
gsub(/^[^=]*="/, "", fs); gsub(/"$/, "", fs);
print nm "\t" fs "\t" arr[1];
}
}
}'
}
# Compute depth from BASE to CAND in the block dependency chain
# Returns integer >=0 when CAND descends from BASE, otherwise -1
depth_from_base() {
base="$1"
cand="$2"
d=$(lsblk -rpn -o NAME -s -- "$cand" 2>/dev/null | awk -v b="$base" '{
if ($0 == b) {
print NR - 1
exit
}
}')
if [ -n "$d" ]; then
printf '%s\n' "$d"
else
printf '%s\n' "-1"
fi
}
# Fallback: scan lsblk NAME and MOUNTPOINTS columns and return first non-empty mountpoint
scan_mountpoint_via_lsblk() {
DEVPATH="$1"
lsblk -rpn -o NAME,TYPE,MOUNTPOINTS,MOUNTPOINT -P -- "$DEVPATH" 2>/dev/null | awk '
{
mp = "";
for (i = 1; i <= NF; i++) {
if ($i ~ /^MOUNTPOINTS="/) {
mp = $i;
} else if ($i ~ /^MOUNTPOINT="/ && mp == "") {
mp = $i;
}
}
if (mp != "") {
gsub(/^[^=]*="/, "", mp); gsub(/"$/, "", mp);
n = split(mp, arr, /[[:space:]]+/);
if (n >= 1) {
print arr[1];
exit
}
}
}'
}
# Resolve and print mountpoint with error handling
resolve_and_print() {
DEV="$1"
# 1) Try direct match for the given device
MP=$(find_mountpoint_direct "$DEV")
if [ "$?" -eq 0 ] && [ -n "$MP" ]; then
printf '%s\n' "$MP"
return 0
fi
# 2) Build candidate list from ancestors and descendants (no subshell leaks)
tmpfile=""
if tmpfile=$(mktemp 2>/dev/null); then
:
else
echo "[ERROR] Failed to create temporary file." >&2
exit 1
fi
list_related_devices "$DEV" | awk 'NF>0' > "$tmpfile"
candfile=""
if candfile=$(mktemp 2>/dev/null); then
:
else
rm -f "$tmpfile"
echo "[ERROR] Failed to create temporary file." >&2
exit 1
fi
seen=""
while IFS= read -r CAND; do
case " $seen " in
*" $CAND "*) continue ;;
*) seen="$seen $CAND" ;;
esac
MP=$(find_mountpoint_direct "$CAND") || :
if [ -n "$MP" ]; then
FS=$(findmnt -nr -S "$CAND" -o FSTYPE 2>/dev/null || true)
[ -n "$FS" ] || FS=""
printf '%s\t%s\t%s\n' "$CAND" "$FS" "$MP" >> "$candfile"
continue
fi
get_name_fstype_first_mp "$CAND" >> "$candfile"
done < "$tmpfile"
rm -f "$tmpfile"
# 3) Select best candidate by policy (depth > root > non-vfat)
best_mp=""
best_depth="-1"
best_fs=""
while IFS= read -r line; do
# Extract fields robustly regardless of IFS/tab handling
CNAME=$(printf '%s\n' "$line" | awk -F '\t' '{print $1}')
CFS=$(printf '%s\n' "$line" | awk -F '\t' '{print $2}')
CMP=$(printf '%s\n' "$line" | awk -F '\t' '{print $3}')
[ -n "$CMP" ] || continue
d=$(depth_from_base "$DEV" "$CNAME")
[ "$d" -ge 0 ] || continue
if [ "$best_depth" = "-1" ]; then
best_depth="$d"
best_mp="$CMP"
best_fs="$CFS"
continue
fi
if [ "$d" -gt "$best_depth" ]; then
best_depth="$d"
best_mp="$CMP"
best_fs="$CFS"
continue
fi
if [ "$d" -eq "$best_depth" ]; then
if [ "$CMP" = "/" ] && [ "$best_mp" != "/" ]; then
best_mp="$CMP"
best_fs="$CFS"
continue
fi
case "$best_fs" in
vfat|msdos|exfat|iso9660|squashfs) best_is_vfat=1 ;;
*) best_is_vfat=0 ;;
esac
case "$CFS" in
vfat|msdos|exfat|iso9660|squashfs) cur_is_vfat=1 ;;
*) cur_is_vfat=0 ;;
esac
if [ "$best_is_vfat" -eq 1 ] && [ "$cur_is_vfat" -eq 0 ]; then
best_mp="$CMP"
best_fs="$CFS"
continue
fi
fi
done < "$candfile"
rm -f "$candfile"
if [ -n "$best_mp" ]; then
printf '%s\n' "$best_mp"
return 0
fi
# 4) Last fallback via lsblk scan
MP=$(scan_mountpoint_via_lsblk "$DEV")
if [ -n "$MP" ]; then
printf '%s\n' "$MP"
return 0
fi
echo "[ERROR] Device not mounted or no mounted children found: $DEV" >&2
exit 2
}
# Main entry point of the script
main() {
case "$1" in
-h|--help|-v|--version) usage ;;
esac
check_system
check_commands lsblk findmnt readlink awk
validate_args "$@"
resolve_and_print "$DEVICE"
return $?
}
# Execute main function
main "$@"