-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaccess-sdcard.sh
executable file
·106 lines (94 loc) · 2.29 KB
/
access-sdcard.sh
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
#!/usr/bin/env bash
# Michael Weber xmw at gentoo dot org 2013
# vim: tabstop=4
RUN_QEMU=0
usage() {
[ -n "${@:2}" ] && echo "${@:2}"
echo "Usage: $(basename ${0}) [-v] [-h|--help] <image file|device> [mountpoint]"
[ -n "${1}" ] && exit ${1}
}
while true ; do
case "${1}" in
-h|--help)
usage 0
;;
-v|--verbose)
set +x
shift
;;
# -r|--run)
# RUN_QEMU=1
# shift
# ;;
*)
break
;;
esac
done
if [ "${RUN_QEMU}" -eq 1 ] ; then
if ! qemu-system-arm -version || \
! qemu-system-arm -cpu ? | grep arm1176 >/dev/null ; then
quit 1 "Please install qemu-system-arm with arm1176 support."
fi
fi
IMAGE=${1}
MOUNTPOINT=${2}
[ -z "${IMAGE}" ] && usage 1 "specify image file or device"
[ -n "${3}" ] && usage 1 "too many arguments"
if [ -n "${MOUNTPOINT}" ] ; then
[ -d "${MOUNTPOINT}" ] || usage 1 "mountpoint is not a directory"
if mountpoint -q "${MOUNTPOINT}" ; then
usage 1 "${MOUNTPOINT} already mounted"
fi
else
TMP=$(mktemp -d /tmp/rpi-card.XXXXX)
MOUNTPOINT=${TMP}
REMOVE_MOUNTPOINT=1
fi
set -e
if [ -f "${IMAGE}" ] ; then
LOOP=$(losetup -f)
losetup "${LOOP}" "${IMAGE}"
partx -d "${LOOP}" || true
partx -a "${LOOP}"
DEV=${LOOP}
else
partx -d "${IMAGE}" || true
partx -a "${IMAGE}"
DEV=${IMAGE}
fi
#try some magic
BOOT=$(fdisk -l "${DEV}" | grep "^${DEV}" | awk '$5 == "c" { print $1 }')
ROOT=$(fdisk -l "${DEV}" | grep "^${DEV}" | awk '$5 == "83" { print $1 }')
if [ "${RUN_QEMU}" -eq 1 ] ; then
mount "${ROOT}" "${MOUNTPOINT}"
pushd "${MOUNTPOINT}"
echo "fix me"
popd >/dev/null
else
mount "${ROOT}" "${MOUNTPOINT}"
mount "${BOOT}" "${MOUNTPOINT}"/boot
[ -e "${MOUNTPOINT}"/var/cache/portage/latest.squashfs ] && \
mount "${MOUNTPOINT}"/var/cache/portage/latest.squashfs \
"${MOUNTPOINT}"/usr/portage
pushd "${MOUNTPOINT}"
$SHELL
popd >/dev/null
while mountpoint "${MOUNTPOINT}"/usr/portage >/dev/null ; do
umount "${MOUNTPOINT}"/usr/portage || sleep 1
done
while mountpoint "${MOUNTPOINT}"/boot >/dev/null ; do
umount "${MOUNTPOINT}"/boot || sleep 1
done
fi
while mountpoint "${MOUNTPOINT}" >/dev/null ; do
umount "${MOUNTPOINT}" || sleep 1
done
if [ -n "${REMOVE_MOUNTPOINT}" ] ; then
rmdir "${MOUNTPOINT}"
fi
if [ -n "${LOOP}" ] ; then
while losetup -a | grep "${IMAGE}" | grep "${LOOP}" >/dev/null ; do
losetup -d "${LOOP}" || sleep 1
done
fi