-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmount-external-disks
More file actions
executable file
·192 lines (158 loc) · 4.69 KB
/
mount-external-disks
File metadata and controls
executable file
·192 lines (158 loc) · 4.69 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
#!/bin/bash
#
# Mount external disks
# Automatically discovers USB-connected partitions and handles LUKS encryption
#
set -euo pipefail
MOUNT_BASE="/run/media/dan"
MIN_PARTITION_SIZE=$((1000 * 1024 * 1024))
# Discover USB-connected partitions
discover_usb_partitions() {
local usb_disks partitions disk part size
# Find USB disks
usb_disks=$(lsblk -rno NAME,TYPE,TRAN | awk '$2=="disk" && $3=="usb" {print $1}')
for disk in $usb_disks; do
# Find partitions of this disk
partitions=$(lsblk -rno NAME,TYPE "/dev/$disk" | awk '$2=="part" {print $1}')
for part in $partitions; do
# Skip small partitions (EFI, recovery, etc.)
size=$(lsblk -brno SIZE "/dev/$part" | head -1)
if [[ "$size" -lt "$MIN_PARTITION_SIZE" ]]; then
continue
fi
echo "/dev/$part"
done
done
}
# Check if device is a LUKS container
is_luks() {
local device="$1"
sudo cryptsetup isLuks "$device" 2>/dev/null
}
# Get LUKS UUID for a device
get_luks_uuid() {
local device="$1"
sudo blkid -o value -s UUID "$device" 2>/dev/null || true
}
# Get filesystem label, fall back to UUID prefix
get_mount_label() {
local device="$1"
local label uuid
label=$(sudo blkid -o value -s LABEL "$device" 2>/dev/null) || true
if [[ -n "$label" ]]; then
echo "$label"
return
fi
uuid=$(sudo blkid -o value -s UUID "$device" 2>/dev/null) || true
if [[ -n "$uuid" ]]; then
echo "${uuid:0:8}"
return
fi
# Last resort: use device name
basename "$device"
}
# Check if LUKS container is already open
is_luks_open() {
local name="$1"
[[ -e "/dev/mapper/$name" ]]
}
# Check if filesystem is already mounted
is_mounted() {
local mountpoint="$1"
mountpoint -q "$mountpoint" 2>/dev/null
}
# Check if any LUKS partitions need opening
any_luks_needs_opening() {
local partitions partition uuid mapper_name
partitions=$(discover_usb_partitions)
for partition in $partitions; do
if is_luks "$partition"; then
uuid=$(get_luks_uuid "$partition")
mapper_name="luks-$uuid"
if ! is_luks_open "$mapper_name"; then
return 0
fi
fi
done
return 1
}
# Mount a partition
mount_disk() {
local partition="$1"
local password="${2:-}"
local uuid mapper_name device label mountpoint
echo "=== $partition ==="
if is_luks "$partition"; then
uuid=$(get_luks_uuid "$partition")
mapper_name="luks-$uuid"
device="/dev/mapper/$mapper_name"
echo " LUKS encrypted (UUID: $uuid)"
# Open LUKS container if not already open
if is_luks_open "$mapper_name"; then
echo " LUKS container already open"
else
echo " Opening LUKS container..."
if [[ -n "$password" ]]; then
if ! echo "$password" | sudo cryptsetup open "$partition" "$mapper_name"; then
echo " Failed to open LUKS container"
return 1
fi
else
if ! sudo cryptsetup open "$partition" "$mapper_name"; then
echo " Failed to open LUKS container"
return 1
fi
fi
echo " LUKS container opened"
fi
# Get label from the inner filesystem
label=$(get_mount_label "$device")
else
device="$partition"
label=$(get_mount_label "$device")
echo " Plain filesystem"
fi
mountpoint="$MOUNT_BASE/$label"
# Create mount point if needed
if [[ ! -d "$mountpoint" ]]; then
sudo mkdir -p "$mountpoint"
fi
# Mount if not already mounted
if is_mounted "$mountpoint"; then
echo " Already mounted at $mountpoint"
else
echo " Mounting to $mountpoint..."
if ! sudo mount "$device" "$mountpoint"; then
echo " Failed to mount"
return 1
fi
echo " Mounted successfully"
fi
return 0
}
main() {
local partitions partition
local success=0
local failed=0
local password=""
partitions=$(discover_usb_partitions)
if [[ -z "$partitions" ]]; then
echo "No USB partitions found"
exit 0
fi
# Prompt for password once if any LUKS partitions need opening
if any_luks_needs_opening; then
read -s -p "Enter LUKS password: " password
echo
fi
for partition in $partitions; do
if mount_disk "$partition" "$password"; then
((++success))
else
((++failed))
fi
echo
done
echo "Summary: $success mounted, $failed failed"
}
main "$@"