-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathimage-chroot
178 lines (164 loc) · 3.64 KB
/
image-chroot
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
#!/bin/bash
trap '{ stty sane; echo ""; errexit "Aborted"; }' SIGINT SIGTERM
MNTPATH="/tmp/image-chroot-mnt"
errexit()
{
echo ""
echo -e "$1"
echo ""
if [ "${MNTED}" = "TRUE" ]; then
umount "${BOOTMNT}/" &> /dev/null
umount "${MNTPATH}/" &> /dev/null
fi
rm -rf "${MNTPATH}/" &> /dev/null
rmloop
exit 1
}
mkloop()
{
LOOP="$(losetup -f --show -P "${IMGFILE}")"
if [ $? -ne 0 ]; then
errexit "Unable to create loop device"
fi
}
rmloop()
{
if [ "${LOOP}" != "" ]; then
losetup -d "${LOOP}"
LOOP=""
fi
}
mntimg()
{
mkloop
if [ ! -d "${MNTPATH}/" ]; then
mkdir "${MNTPATH}/"
if [ $? -ne 0 ]; then
errexit "Unable to make ROOT partition mount point"
fi
fi
mount "${LOOP}p2" "${MNTPATH}/"
if [ $? -ne 0 ]; then
errexit "Unable to mount image ROOT partition"
fi
MNTED=TRUE
BOOTMNT="${MNTPATH}$(sed -n '/^[[:space:]]*#/!s|^\S\+\s\+\(/boot\S*\)\s\+.*$|\1|p' "${MNTPATH}/etc/fstab")"
if [ ! -d "${BOOTMNT}/" ]; then
mkdir -p "${BOOTMNT}/"
if [ $? -ne 0 ]; then
errexit "Unable to make BOOT partition mount point"
fi
fi
mount "${LOOP}p1" "${BOOTMNT}/"
if [ $? -ne 0 ]; then
errexit "Unable to mount image BOOT partition"
fi
}
umntimg()
{
umount "${BOOTMNT}/"
if [ $? -ne 0 ]; then
errexit "Unable to unmount image BOOT partition"
fi
umount "${MNTPATH}/"
if [ $? -ne 0 ]; then
errexit "Unable to unmount image ROOT partition"
fi
MNTED=FALSE
rmloop
rm -r "${MNTPATH}/"
}
usage()
{
errexit "Usage: $0 [options] pathto/imagefile\n\
-h,--help This usage description\n\
-u,--ubuntu Ubuntu (Deprecated)"
}
LOOP=""
MNTED=FALSE
if [ $(id -u) -ne 0 ]; then
errexit "Must be run as root user: sudo $0"
fi
PGMNAME="$(basename $0)"
for PID in $(pidof -x -o %PPID "${PGMNAME}"); do
if [ ${PID} -ne $$ ]; then
errexit "${PGMNAME} is already running"
fi
done
file --version &> /dev/null
if [ $? -eq 127 ]; then
echo ""
echo "Installing file"
echo ""
apt-get update
apt-get install file
fi
IMGFILE=""
while [ $# -gt 0 ]; do
case "$1" in
-h|--help)
usage
;;
-u|--ubuntu)
shift
;;
-*|--*)
usage
;;
*)
IMGFILE="$1"
shift
;;
esac
done
if [ "${IMGFILE}" = "" ]; then
errexit "No image file specified"
fi
if [ ! -f "${IMGFILE}" ]; then
errexit "${IMGFILE} not found"
fi
mntimg
SYS="$(file -b /bin/bash | sed -n 's|^.*, \(ARM\s*\S*\),.*$|\1|p')"
IMG="$(file -b ${MNTPATH}/bin/bash | sed -n 's|^.*, \(ARM\s*\S*\),.*$|\1|p')"
umntimg
if [ "${SYS}" != "${IMG}" ]; then
errexit "${IMGFILE} architecture differs from system architecture"
fi
echo ""
echo -n "chroot to ${IMGFILE} (y/n)? "
while read -r -n 1 -s answer; do
if [[ ${answer} = [yYnN] ]]; then
echo "${answer}"
if [[ ${answer} = [yY] ]]; then
break
else
errexit "Aborted"
fi
fi
done
echo ""
echo "Attempting to chroot to ${IMGFILE}"
echo "Host root filesystem is available at /host-root-fs"
echo "Use exit or ^D to terminate chroot"
echo ""
mntimg
mkdir "${MNTPATH}/host-root-fs"
mount --bind / "${MNTPATH}/host-root-fs"
mount --bind /dev "${MNTPATH}/dev"
mount --bind /sys "${MNTPATH}/sys"
mount --bind /proc "${MNTPATH}/proc"
mount --bind /dev/pts "${MNTPATH}/dev/pts"
chroot "${MNTPATH}"
umount "${MNTPATH}/dev/pts" "${MNTPATH}/proc" "${MNTPATH}/sys" "${MNTPATH}/dev"
while read MNTPT
do
if [[ "${MNTPT}" != "${BOOTMNT}" && "${MNTPT}" != "${MNTPATH}/host-root-fs" ]]; then
umount "${MNTPT}"
fi
done <<< "$(findmnt -lR ${MNTPATH} | sed -n "s|^\(${MNTPATH}/\S\+\)\s\+.*$|\1|p")"
umount "${MNTPATH}/host-root-fs"
rmdir "${MNTPATH}/host-root-fs"
umntimg
echo ""
echo "Exited chroot to ${IMGFILE}"
echo ""