-
Notifications
You must be signed in to change notification settings - Fork 0
/
mount
executable file
·72 lines (60 loc) · 1.69 KB
/
mount
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
#!/bin/bash
set -eu
TARGET="$HOME/di/ext"
is_mounted() {
lsblk | grep $TARGET | grep -q lvm
}
info() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $*"
}
if is_mounted; then
echo "Already mounted"
exit 0
fi
# list devices
info "Listing devices:"
uuids=()
while read -r uuid _ _; do
if ! [[ $uuid =~ ^\{?[A-F0-9a-f]{8}-[A-F0-9a-f]{4}-[A-F0-9a-f]{4}-[A-F0-9a-f]{4}-[A-F0-9a-f]{12}\}?$ ]]; then
continue
fi
if ! sudo file -sL "/dev/disk/by-uuid/$uuid" | grep -q LUKS; then
continue
fi
uuids+=("$uuid")
done< <(sudo lsblk -o UUID,NAME,TYPE | grep disk | grep -v nvme)
# Check if locked disk exists
has_one=0
for uuid in "${uuids[@]}"; do
id=$(echo "$uuid" | awk -F'-' '{print $1}')
if ! lsblk | grep "lv-$id"; then
has_one=1
break
fi
done
info "Found ${#uuids[@]} LUKS devices. Unlocking.."
if [ $has_one -eq 1 ]; then
# unlock disks
read -rsp "Enter passphrase: " passwd; echo
for uuid in "${uuids[@]}"; do
id=$(echo "$uuid" | awk -F'-' '{print $1}')
name=$(lsblk -o NAME,UUID | grep "$uuid" | awk '{print $1}')
is_mounted=$(lsblk | grep "lv-$id" | wc -l)
info "$name ($uuid) -> lv-$id (mounted: $is_mounted)"
# FIXME: hacky way of unlocking disks in parallel.
# LVM is automatically scanned and activated when 3/4 disks are unlocked,
# so we need to unlock all disks at once before udev(?) activating LVM.
# This behavior occurs only on WSL (not on pure Linux).
echo "$passwd" | sudo cryptsetup luksOpen "/dev/disk/by-uuid/$uuid" "lv-$id" &
done
# wait for all disks to unlock
wait
sleep 3
fi
# mount disks
# Check if mounted
if ! is_mounted; then
info "Mounting disks.."
sudo mount /dev/mapper/vg--ext-lv--ext "$TARGET"
fi
info 'done'