-
Notifications
You must be signed in to change notification settings - Fork 1
/
coinbootmaker
executable file
·481 lines (445 loc) · 16.6 KB
/
coinbootmaker
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#!/bin/bash
set -Eeo pipefail
set -x
# Copyright (C) 2018 - 2021 Gunter Miegel coinboot.io
#
# This file is part of Coinboot.
# This software may be modified and distributed under the terms
# of the MIT license. See the LICENSE file for details.
# Helper script to convert initramfs archive into a Docker container.
display_help() {
echo
echo 'Coinbootmaker creates an environment for building Coinboot plugins from a'
echo 'given Coinboot Initramfs.'
echo
echo 'Packaged Coinboot pluings are written to the ./builds directory'
echo
echo 'Usage: coinbootmaker [-i] [-h] [-l] [-p <plugin name> <path to initramfs>]'
echo
echo '-i Interactive mode - opens a shell in the build environment'
echo '-p <file name> Plugin to build'
echo '-l List plugins available to build'
echo '-h Display this help'
echo
}
list_plugins() {
pushd . > /dev/null
cd src
echo
find * -type f -print
echo
popd > /dev/null
}
while getopts "ip:lh" opt; do
case $opt in
i)
interactive=true
;;
p)
plugin=$OPTARG
;;
l)
list_plugins
exit 1
;;
h)
display_help
exit 1
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done
shift $((OPTIND -1))
WGET='wget --retry-connrefused --waitretry=5 --read-timeout=20 --timeout=15 -t 0'
CURL='curl --max-time 5 --retry-max-time 20 --retry 999'
CACHE_DIR=$(readlink -f ./cache)
GITHUB_REPO=frzb/coinboot
RELEASE=latest
## initramfs and kernel vmlinuz ##
# RELEASE is set via an environment variable under ./conf/environment
# If the value is 'latest' we determine the latest release, else we use the set value.
if [ $RELEASE = latest ]; then
RESPONSE=$($CURL --silent "https://api.github.com/repos/${GITHUB_REPO}/tags")
sleep 5
while ! TAG=$(echo $RESPONSE | jq -r '.[0].name'); do
echo "Calling the Github API has failed, repeat ..."
RESPONSE=$($CURL --silent "https://api.github.com/repos/${GITHUB_REPO}/tags")
sleep 5
done
echo "Using latest coinboot-debirf release: $TAG"
fi
DOWNLOAD_URL=https://github.com/${GITHUB_REPO}/releases/download/${TAG}
if [ -z $KERNEL ]; then
KERNEL=5.4.0-58-generic
fi
INITRAMFS=coinboot-initramfs-$KERNEL
if ! [ -f $CACHE_DIR/$INITRAMFS ]; then
$WGET $DOWNLOAD_URL/$INITRAMFS -P $CACHE_DIR
fi
BASEDIR=$PWD
#INITRAMFS=$(readlink -f $1)
LOWER=/tmp/$(basename $INITRAMFS)_extracted_by_coinbootmaker/lower
UPPER=/tmp/$(basename $INITRAMFS)_extracted_by_coinbootmaker/upper
WORKING_DIRECTORY=/tmp/$(basename $INITRAMFS)_extracted_by_coinbootmaker/working_dir
MERGED=/tmp/$(basename $INITRAMFS)_extracted_by_coinbootmaker/merged
while sudo runc list | grep coinbootmaker | grep running; do
echo 'Waiting for Coinbootmaker container to be stopped ...'
sudo runc kill coinbootmaker KILL
sleep 1
done
while sudo runc list | grep coinbootmaker | grep stopped; do
echo 'Waiting for Coinbootmaker container to be cleaned up ...'
sudo runc delete coinbootmaker
sleep 1
done
sudo runc delete coinbootmaker || true
sudo ip link delete cbm-host || true
sudo ip netns delete coinbootmaker || true
if mountpoint $MERGED; then
sudo umount $MERGED
fi
sudo rm -rf $UPPER $LOWER $WORKING_DIRECTORY $MERGED
sleep 1
sudo mkdir -p $UPPER $LOWER $WORKING_DIRECTORY $MERGED
# We create our own TMPFS.
# Beforehand we tried to use '/dev/shm' for our purposes but it is mounted with
# a 'nosuid' flag so that all commands inside the container that leverage SUID
# like e.g. 'sudo' are failing.
# TODO: Check/Define size of TMPFS
sudo mount -t overlay overlay -o lowerdir=$LOWER,upperdir=$UPPER,workdir=$WORKING_DIRECTORY $MERGED
sudo mkdir -p $LOWER/rootfs
# Do some file work in the base/lower directory
cd $LOWER/rootfs
# We have to use 'sudo' for 'cpio' else the ownership of the files in the
# archive is messed up.
# We just extract the nested initramfs archive
zcat $CACHE_DIR/$INITRAMFS | sudo cpio -idvm "rootfs.cgz"
zcat rootfs.cgz | sudo cpio -idm
# The nested initramfs archive can be removed now
sudo rm -v rootfs.cgz
# Adapt nameserver settings.
# resolv.conf is a symling to the systemd stub resolver which we have to delete beforehand.
sudo rm etc/resolv.conf
sudo tee etc/resolv.conf << EOF
nameserver 1.1.1.1
EOF
sudo tee etc/hosts << EOF
127.0.1.1 coinbootmaker
EOF
cd $LOWER
# Generate container spec file config.json
# We have to manipulate the config.json created by 'runc spec'.
# At first I tried to handle this with jq - this was a nice exercise.
# But we have a further dependency to jq in this case so we
# now go with just putting the JSON in this file as here-document.
# So we omit the jq limbo and the dependency to jq.
# We use the same set of capabilities as Docker by default does.
#https://github.com/moby/moby/blob/master/oci/defaults.go#L14-L30
sudo tee ./config.json << EOF
{
"ociVersion": "1.0.0",
"process": {
"terminal": false,
"user": {
"uid": 0,
"gid": 0
},
"args": ["/usr/bin/sleep", "infinity"
],
"env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"TERM=xterm",
"KERNEL_RELEASE=$(echo $INITRAMFS | grep -oP '(?<=coinboot-initramfs-)[^\s]+')",
"LC_ALL=en_US.UTF-8"
],
"cwd": "/",
"capabilities": {
"bounding": [
"CAP_CHOWN",
"CAP_DAC_OVERRIDE",
"CAP_FSETID",
"CAP_FOWNER",
"CAP_MKNOD",
"CAP_NET_RAW",
"CAP_SETGID",
"CAP_SETUID",
"CAP_SETFCAP",
"CAP_SETPCAP",
"CAP_NET_BIND_SERVICE",
"CAP_SYS_CHROOT",
"CAP_KILL",
"CAP_AUDIT_WRITE"
],
"effective": [
"CAP_CHOWN",
"CAP_DAC_OVERRIDE",
"CAP_FSETID",
"CAP_FOWNER",
"CAP_MKNOD",
"CAP_NET_RAW",
"CAP_SETGID",
"CAP_SETUID",
"CAP_SETFCAP",
"CAP_SETPCAP",
"CAP_NET_BIND_SERVICE",
"CAP_SYS_CHROOT",
"CAP_KILL",
"CAP_AUDIT_WRITE"
],
"inheritable": [
"CAP_CHOWN",
"CAP_DAC_OVERRIDE",
"CAP_FSETID",
"CAP_FOWNER",
"CAP_MKNOD",
"CAP_NET_RAW",
"CAP_SETGID",
"CAP_SETUID",
"CAP_SETFCAP",
"CAP_SETPCAP",
"CAP_NET_BIND_SERVICE",
"CAP_SYS_CHROOT",
"CAP_KILL",
"CAP_AUDIT_WRITE"
],
"permitted": [
"CAP_CHOWN",
"CAP_DAC_OVERRIDE",
"CAP_FSETID",
"CAP_FOWNER",
"CAP_MKNOD",
"CAP_NET_RAW",
"CAP_SETGID",
"CAP_SETUID",
"CAP_SETFCAP",
"CAP_SETPCAP",
"CAP_NET_BIND_SERVICE",
"CAP_SYS_CHROOT",
"CAP_KILL",
"CAP_AUDIT_WRITE"
],
"ambient": [
"CAP_CHOWN",
"CAP_DAC_OVERRIDE",
"CAP_FSETID",
"CAP_FOWNER",
"CAP_MKNOD",
"CAP_NET_RAW",
"CAP_SETGID",
"CAP_SETUID",
"CAP_SETFCAP",
"CAP_SETPCAP",
"CAP_NET_BIND_SERVICE",
"CAP_SYS_CHROOT",
"CAP_KILL",
"CAP_AUDIT_WRITE"
]
},
"rlimits": [
{
"type": "RLIMIT_NOFILE",
"hard": 1024,
"soft": 1024
}
],
"noNewPrivileges": true
},
"root": {
"path": "rootfs",
"readonly": false
},
"hostname": "coinbootmaker",
"mounts": [
{
"destination": "/proc",
"type": "proc",
"source": "proc"
},
{
"destination": "/dev",
"type": "tmpfs",
"source": "tmpfs",
"options": [
"nosuid",
"strictatime",
"mode=755",
"size=65536k"
]
},
{
"destination": "/dev/pts",
"type": "devpts",
"source": "devpts",
"options": [
"nosuid",
"noexec",
"newinstance",
"ptmxmode=0666",
"mode=0620",
"gid=5"
]
},
{
"destination": "/dev/shm",
"type": "tmpfs",
"source": "shm",
"options": [
"nosuid",
"noexec",
"nodev",
"mode=1777",
"size=65536k"
]
},
{
"destination": "/dev/mqueue",
"type": "mqueue",
"source": "mqueue",
"options": [
"nosuid",
"noexec",
"nodev"
]
},
{
"destination": "/sys",
"type": "sysfs",
"source": "sysfs",
"options": [
"nosuid",
"noexec",
"nodev",
"ro"
]
},
{
"destination": "/sys/fs/cgroup",
"type": "cgroup",
"source": "cgroup",
"options": [
"nosuid",
"noexec",
"nodev",
"relatime",
"ro"
]
},
{
"destination": "/mnt",
"type": "none",
"source": "$BASEDIR",
"options": [
"bind",
"rw"
]
},
{
"destination": "/mnt/plugin",
"type": "none",
"source": "$UPPER",
"options": [
"bind",
"ro"
]
}
],
"linux": {
"resources": {
"devices": [
{
"allow": false,
"access": "rwm"
}
]
},
"namespaces": [
{
"type": "pid"
},
{
"type": "network",
"path": "/var/run/netns/coinbootmaker"
},
{
"type": "ipc"
},
{
"type": "uts"
},
{
"type": "mount"
}
],
"maskedPaths": [
"/proc/kcore",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/sys/firmware",
"/proc/scsi"
],
"readonlyPaths": [
"/proc/asound",
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
}
}
EOF
# We hijack the default Docker bridge docker0 for network access of our container.
# Least effort approch to determine a free und usable adress:
# Pick the highest available address of the 172.17.0.0. network which is 172.17.255.254/16.
# There are good chances that this address is unused.
# TODO: We assume that the default Docker network 172.17.0.0/16 exists.
# This is pretty naive and we need to handle this better - at least to add
# exeption handling.
sudo ip link add name cbm-host type veth peer name cbm-guest
sudo ip link set cbm-host up
sudo brctl addif docker0 cbm-host
sudo ip netns add coinbootmaker
sudo ip link set cbm-guest netns coinbootmaker
cd $MERGED
# TODO: We should go rootless: https://github.com/opencontainers/runc/pull/774
sudo runc run -d coinbootmaker
# This commands can only be executed if the container is already running.
# So let's wait until it is ready.
while ! sudo runc list | grep coinbootmaker; do
echo 'Waiting for Coinbootmaker container...'
sleep 1
done
sudo ip netns exec coinbootmaker ip link set cbm-guest name eth0
sudo ip netns exec coinbootmaker ip addr add 172.17.255.254/16 dev eth0
sudo ip netns exec coinbootmaker ip link set eth0 up
sudo ip netns exec coinbootmaker ip route add default via 172.17.0.1
if [ ! -z $interactive ] && [ $interactive = 'true' ]; then
sudo runc exec -t coinbootmaker /usr/bin/bash
elif [[ ./src/$plugin == *.yaml ]]; then
sudo runc exec --cwd /mnt/build coinbootmaker create_plugin.py start
sudo runc exec --cwd /mnt/build coinbootmaker /usr/bin/bash -e < <(yq eval '.run' $BASEDIR/src/$plugin)
NAME=$(yq eval '.archive_name' $BASEDIR/src/$plugin)
VERSION=$(yq eval '.version' $BASEDIR/src/$plugin)
BUILD_DATE=$(date +%Y%m%d.%H%M)
sudo runc exec --cwd /mnt/build coinbootmaker create_plugin.py finish coinboot_${NAME}_${VERSION}_$BUILD_DATE
else
sudo runc exec coinbootmaker /usr/bin/bash -c "cd /mnt/build/ && /mnt/src/$plugin"
fi
# Cleanup
sudo runc kill coinbootmaker KILL
while ! sudo runc list | grep coinbootmaker | grep stopped; do
echo 'Waiting for Coinbootmaker container to be stopped ...'
sleep 1
done
sudo runc delete coinbootmaker
# Dismantle the networking limbo we have done before.
sudo ip link delete cbm-host
sudo ip netns delete coinbootmaker
echo "Cleaning up directories"
cd $BASEDIR
sudo umount -v $MERGED
sudo rm -rf $UPPER $LOWER $WORKING_DIRECTORY $MERGED