-
Notifications
You must be signed in to change notification settings - Fork 99
/
dev-mount
executable file
·104 lines (88 loc) · 2.12 KB
/
dev-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
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
#!/bin/bash
set -eo pipefail
retry() {
for i in 1 2 3 4 5; do
if "$@"; then
return 0
fi
sleep 1s
done
return 1
}
if [[ $# -ne 0 ]] && [[ $# -ne 1 ]]; then
echo "usage: $0 [img-or-block]"
exit 1
fi
if mountpoint -q /mnt; then
echo "Something is mounted to '/mnt'. Unmount first."
mount /mnt || true
exit 1
fi
cleanup() {
sync
cd /
umount -f /mnt/dev || true
umount -f /mnt/proc || true
umount -f /mnt/sys || true
umount -f /mnt/boot || true
umount -f /mnt || true
kpartx -d "$DISK" || true
losetup -d "$DISK" || true
[[ -z "$SD_MUX" ]] || sd-mux-ctrl -v 0 --dut
}
trap cleanup EXIT
if [[ $# -eq 0 ]]; then
if SD_MUX=$(sd-mux-ctrl -v 0 --status); then
echo "Using SD_MUX..."
if [[ "$SD_MUX" == "SD connected to: DUT" ]]; then
echo "Switching SD_MUX to TS..."
sd-mux-ctrl -v 0 --ts
sleep 1s
fi
fi
DEVICES=( $(find /dev/disk/by-id/ -name "usb-*" -not -name "*-part*") )
if [[ "${#DEVICES[@]}" -eq 0 ]]; then
echo "No USB devices."
exit 1
elif [[ "${#DEVICES[@]}" -ne 1 ]]; then
echo "Many devices: ${DEVICES[@]}"
exit 1
fi
DISK="${DEVICES[0]}"
echo "Using '$DISK'..."
else
DISK=$(losetup -f --show "${1}")
kpartx -a "$DISK"
echo "Attached '$1' to '$DISK'..."
fi
mount_part() {
local parts="${DISK/\/dev\/loop/\/dev\/mapper\/loop}"
local matches="$1"
shift
for match in $matches; do
if [[ -b "$DISK-$match" ]]; then
local disk="$DISK-$match"
echo "Found '$DISK' for '$match'."
elif MATCH=$(blkid -o device -l -t "PARTLABEL=$match" "$parts"*); then
local disk="$MATCH"
echo "Found '$DISK' for 'PARTLABEL=$match'."
elif MATCH=$(blkid -o device -l -t "LABEL=$match" "$parts"*); then
local disk="$MATCH"
echo "Found '$DISK' for 'LABEL=$match'."
else
continue
fi
echo "Mounting '$disk' to '$@'..."
mount "$disk" "$@"
return 0
done
echo "The '$matches' does not exist."
return 1
}
retry mount_part "part4 rootfs" /mnt
retry mount_part "part3 bootfs" /mnt/boot
mount --bind /dev /mnt/dev
mount --bind /proc /mnt/proc
mount --bind /sys /mnt/sys
cd /mnt
bash