-
Notifications
You must be signed in to change notification settings - Fork 5
/
dibs.sh
executable file
·361 lines (310 loc) · 8.9 KB
/
dibs.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#!/bin/bash
DIBS=$(dirname $(readlink -e "$0"))
ARGS=("$@")
BUILT=1
CLEANUP=0
CLEANUP_BINFMT=0
run_as_user() {
if [[ -n $SUDO_USER ]]; then
sudo -u $SUDO_USER "$@"
else
"$@"
fi
}
require_conf() {
if [[ ! -f "${CONFIG}" ]]; then
echo "You need a configuration file first! [Hint: use defconfig]"
exit 1
fi
source "${CONFIG}"
if [ "$ARCH" = "arm64" ]; then
QEMU_STATIC="qemu-aarch64-static"
else
QEMU_STATIC="qemu-arm-static"
fi
CACHE="${DIBS}/.cache/$SYSTEM-$SUITE-$ARCH"
}
require_image() {
if [ ! -f "${IMAGE}" ]; then
echo "${IMAGE} does not exist"
exit 1
fi
}
require_elevation() {
if [[ $(id -u) -eq 0 ]]; then
return;
fi
if [[ -z $(which sudo) ]]; then
echo "Superuser privlidges are required for this operation."
exit 1
fi
exec sudo "$0" "${ARGS[@]}"
}
require_rootfs() {
if [[ $CLEANUP -ge 1 ]]; then
return 0
fi
require_elevation
CLEANUP=1
if [[ ! -f "${IMAGE}" ]]; then
run_as_user dd if=/dev/zero "of=${IMAGE}" bs=1M seek=$IMAGE_MB count=0 > /dev/null 2>&1
mkfs -q -t ext4 "${IMAGE}"
fi
LOOP_DEVICE=$(losetup -f)
losetup -P $LOOP_DEVICE "${IMAGE}"
mkdir -p "$ROOTFS"
mount -t ext4 $LOOP_DEVICE "$ROOTFS"
mkdir -p "${ROOTFS}/proc"
mount -t proc proc "${ROOTFS}/proc"
}
require_qemu() {
if [[ $CLEANUP -ge 2 ]]; then
return 0
fi
require_rootfs
CLEANUP=2
if [[ -z $(which $QEMU_STATIC) ]]; then
echo "$QEMU_STATIC is missing"
echo "Please install the qemu-user-static package"
exit 1
fi
local qemu_arm_static=$(which $QEMU_STATIC)
if [[ ! -f /proc/sys/fs/binfmt_misc/arm ]]; then
CLEANUP_BINFMT=1
if [[ ! -f /proc/sys/fs/binfmt_misc/register ]]; then
modprobe binfmt_misc
mount -t binfmt_misc binfmt_misc /proc/sys/fs/binfmt_misc
CLEANUP_BINFMT=2
fi
echo -n ":arm:M::\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:${qemu_arm_static}:" > /proc/sys/fs/binfmt_misc/register
fi
mkdir -p "$ROOTFS"$(dirname $qemu_arm_static)
cp "$qemu_arm_static" "${ROOTFS}${qemu_arm_static}"
}
cleanup() {
if [[ $CLEANUP -ge 2 ]]; then
local qemu_arm_static=$(which $QEMU_STATIC)
rm "${ROOTFS}${qemu_arm_static}"
if [[ $CLEANUP_BINFMT -ge 1 ]]; then
echo -n -1 > /proc/sys/fs/binfmt_misc/arm
fi
if [[ $CLEANUP_BINFMT -ge 2 ]]; then
umount /proc/sys/fs/binfmt_misc
modprobe -r binfmt_misc
fi
fi
if [[ $CLEANUP -ge 1 ]]; then
sync
if [[ -d "${ROOTFS}/sys/kernel" ]]; then
umount "${ROOTFS}/sys"
fi
if [[ -d "${ROOTFS}/proc/1" ]]; then
umount "${ROOTFS}/proc"
fi
umount "$ROOTFS"
losetup --detach $LOOP_DEVICE
rmdir "$ROOTFS"
fi
if [[ $BUILT -ne 1 ]]; then
rm -f "${IMAGE}"
fi
}
try() {
local out=0
local err=0
while [[ ${1:0:1} = - ]]; do
case ${1:1} in
-out)
out=1;;
-err)
err=1;;
*)
echo "unknown option $1 for try"
exit 1
esac
shift
done
local output
if [[ $out -ne 0 ]] && [[ $err -ne 0 ]]; then
"$@"
elif [[ $out -ne 0 ]]; then
output=$("$@" 3>&1 1>&2 2>&3)
elif [[ $err -ne 0 ]]; then
output=$("$@")
else
output=$("$@" 2>&1)
fi
if [[ $? -ne 0 ]]; then
echo "error running \"$@\""
if [[ -n $output ]]; then
echo "$output"
fi
exit 1
fi
}
run_target() {
require_qemu
chroot "$ROOTFS" "$@"
}
do_debootstrap() {
if [ -f "${IMAGE}" ]; then
echo "${IMAGE} already exists!"
exit 1
fi
BUILT=0
require_qemu
if [[ -z $(which debootstrap) ]]; then
echo "debootstrap is missing"
echo "Please install the debootstrap package"
exit 1
fi
if [[ -d "$CACHE" ]]; then
mkdir -p "${ROOTFS}/var/cache/apt/archives"
cp -n "${CACHE}/"*.deb "${ROOTFS}/var/cache/apt/archives"
fi
local extra_args
local removed_keys=/usr/share/keyrings/debian-archive-removed-keys.gpg
if [[ -e $removed_keys ]]; then
extra_args+=" --keyring=$removed_keys"
fi
local packages=$(echo "$EXTRA_PACKAGES" | awk -v ORS=, '{ print $1 }' | sed 's/,$/\n/')
if [[ "$packages" ]]; then
extra_args+=" --include=$packages"
fi
try --out debootstrap --variant=minbase --arch=$ARCH $extra_args $SUITE "$ROOTFS" $MIRROR
run_as_user mkdir -p "$CACHE"
run_as_user cp -n "${ROOTFS}/var/cache/apt/archives/"*.deb "$CACHE"
rm "${ROOTFS}/var/cache/apt/archives/"*.deb
BUILT=1
}
do_configure() {
case $SYSTEM in
debian)
cat > "${ROOTFS}/etc/apt/sources.list" << EOF
deb http://mirrors.kernel.org/debian $SUITE main contrib non-free
#deb-src http://mirrors.kernel.org/debain $SUITE main contrib non-free
EOF
if [[ $SUITE != "experimental" ]]; then
cat >> "${ROOTFS}/etc/apt/sources.list" << EOF
deb http://security.debian.org $SUITE/updates main contrib non-free
#deb-src http://security.debian.org $SUITE/updates main contrib non-free
EOF
fi
;;
*)
;;
esac
rm -rf "${ROOTFS}/var/lib/apt/lists/"*
rm -rf "${ROOTFS}/var/log/"*
# set empty root password
sed -i 's/\(root:\)[^:]*\(:\)/\1\2/' "${ROOTFS}/etc/shadow"
# set hostname and hosts file
echo $HOSTNAME > "${ROOTFS}/etc/hostname"
cat > "${ROOTFS}/etc/hosts" << EOF
127.0.0.1 localhost
127.0.1.1 $HOSTNAME
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
EOF
if [[ -n $LOCALE ]]; then
local lang=$(echo ${LOCALE} | sed 's/^\([^_]\+\)\(_.\+\)\?$/\1/')
if [[ -d "${ROOTFS}/usr/share/locale" ]]; then
(cd "${ROOTFS}/usr/share/locale";
find . -maxdepth 1 ! -name $lang -a ! -name "$lang*" -a ! -name "." -type d -exec rm -r "{}" \;)
fi
if [[ -x "${ROOTFS}/usr/sbin/locale-gen" ]]; then
sed -i 's/^\([^#].*\)$/# \1/' "${ROOTFS}/etc/locale.gen"
sed -i 's/^# \('$LOCALE' .\+\)$/\1/' "${ROOTFS}/etc/locale.gen"
run_target /usr/sbin/locale-gen
fi
if [[ -x "${ROOTFS}/usr/sbin/update-locale" ]]; then
run_target /usr/sbin/update-locale LANG=$LOCALE
fi
else
rm -r "${ROOTFS}/usr/share/locale/"*
fi
declare -Ff post_install > /dev/null && post_install
}
show_usage() {
echo "Usage: $0 NAME COMMAND"
echo
echo "COMMAND is one of:"
echo " defconfig [target]"
echo " build"
echo " reconfigure"
echo " shell"
echo " tarball"
echo " tarxz"
echo " qemu"
exit 1
}
trap cleanup EXIT
if [[ -z "$1" ]]; then
show_usage
fi
NAME="$1"
CONFIG="${NAME}.conf"
IMAGE="${NAME}.img"
ROOTFS="${NAME}_rootfs"
case $2 in
defconfig)
mkdir -p $(dirname "${NAME}")
target=${3:-$(basename "${NAME}")}
default="${DIBS}/targets/default.conf"
if [[ -r "${DIBS}/targets/${target}.conf" ]]; then
default="${DIBS}/targets/${target}.conf"
fi
cp "${default}" "${CONFIG}"
echo "\${DIBS}${default#${DIBS}} copied to ${CONFIG}"
;;
build)
require_conf
do_debootstrap
do_configure
;;
reconfigure)
require_image
require_conf
require_rootfs
do_configure
;;
shell)
require_image
debian_chroot=$(basename "$IMAGE") run_target /bin/bash
;;
tarball)
require_image
require_rootfs
tar -cpzf "${NAME}.tar.gz" --one-file-system -C "$ROOTFS" --exclude "lost+found" .
if [[ -n $SUDO_USER ]]; then
chown $SUDO_USER "${NAME}.tar.gz"
fi
;;
tarxz)
require_image
require_rootfs
tar -cpJf "${NAME}.tar.xz" --one-file-system -C "$ROOTFS" --exclude "lost+found" .
if [[ -n $SUDO_USER ]]; then
chown $SUDO_USER "${NAME}.tar.xz"
fi
;;
qemu)
require_image
if [[ -z $(which qemu-system-arm) ]]; then
echo "qemu-system-arm is missing"
echo "Please install the qemu-system package"
exit 1
fi
try qemu-system-arm -machine vexpress-a9 -cpu cortex-a9 \
-kernel "${DIBS}/qemu/vmlinuz-3.10.79.0-1-linaro-lsk-vexpress" \
-append "root=/dev/mmcblk0 rw rootwait" \
-drive "file=${IMAGE},if=sd,format=raw,cache=writeback"
;;
*)
show_usage
;;
esac