-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloopback
99 lines (84 loc) · 2.72 KB
/
loopback
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
#!/bin/bash
# setup loopback device & mount image
# default options
RPI_LOOPBACK_IMAGE="${RPI_LOOPBACK_IMAGE:=}"
rpi_loopback_prerun() {
# check arguments
[[ -n "${RPI_LOOPBACK_IMAGE}" ]] || error "RPI_LOOPBACK_IMAGE check"
}
rpi_loopback_run() {
rpi_loopback_setup "${RPI_LOOPBACK_IMAGE}" || return 1
rpi_loopback_mount || return 1
return 0
}
rpi_loopback_postrun() {
# unmount partitions
rpi_loopback_umount
# tear down loopback device
rpi_loopback_cleanup
}
rpi_loopback_description() {
echo "setup loopback device & mount image"
}
rpi_loopback_help_params() {
help_param "RPI_BOOT" "mountpoint for BOOT partition"
help_param "RPI_ROOT" "mountpoint for root partition"
help_param "RPI_LOOPBACK_IMAGE" "image filename"
help_param "RPI_LOOPBACK_DEVICE" "will be set to used /dev/loopX device"
}
# ---------------------------------------------------------------------
# linux specific stuff
function loopback_linux() {
# already attached?
local device
base_name="$(basename "${image}")"
# shellcheck disable=SC2312
device="$(sudo losetup -l | grep "${base_name}" | cut -d " " -f1)"
if [[ -z "${device}" ]] ; then
# attach image
device="$(sudo losetup --show --find --partscan "${image}")" || error losetup
fi
log "using \"${device}\""
# store device
RPI_LOOPBACK_DEVICE="${device}"
# store img name
RPI_LOOPBACK_IMAGE="${image}"
}
# setup loopback device to mount image
function rpi_loopback_setup() {
local image="$1"
# argument valid?
[[ -f "${image}" ]] || error "${image} not found"
if [[ "${OSTYPE}" == "linux-gnu"* ]]; then
loopback_linux "${image}"
else
error "I have no clue how to mount the image on a ${OSTYPE} system." \
"Please file an issue if you know: https://github.com/heeplr/rpi-cookstrap/issues/new/choose" \
"loopback mount"
fi
}
# tear down loopback device
function rpi_loopback_cleanup() {
sudo losetup -d "${RPI_LOOPBACK_DEVICE}" || warn "${RPI_LOOPBACK_DEVICE} cleanup failed"
sync || warn "sync"
}
# mount raspberry image
function rpi_loopback_mount() {
log "mounting images..."
# already mounted?
# shellcheck disable=SC2312
mount | grep --quiet "${RPI_LOOPBACK_DEVICE}" && return 0
# wait for sync
sync
# mount
log " - mounting ${RPI_BOOT}"
sudo mount "${RPI_LOOPBACK_DEVICE}p1" "${RPI_BOOT}" || return 1
log " - mounting ${RPI_ROOT}"
sudo mount "${RPI_LOOPBACK_DEVICE}p2" "${RPI_ROOT}" || return 1
return 0
}
# unmount raspberry image
function rpi_loopback_umount() {
sudo "umount" "${RPI_BOOT}" || warn "${RPI_BOOT} not mounted"
sudo "umount" "${RPI_ROOT}" || warn "${RPI_ROOT} not mounted"
}