-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.sh
549 lines (521 loc) · 15.8 KB
/
build.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
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
#!/bin/bash
INTERACTIVE=no
TIMESTAMP=$(date +%Y-%m-%d_%H-%M)
BUILD=no
##########################################################################################################################
usage() {
echo "-------------------------------------------------------------------------------------------------"
echo "Options:"
echo " -h, --help Show this help message and exit"
echo " -B, --board Define a Rock-board"
echo " --boardlist Show list of supported boards"
echo " -s, --suite SUITE Choose the Debian suite (e.g., testing, experimental, trixie)"
echo " -k, --kernel latest/standard Choose which kernel to install"
echo " -d, --desktop DESKTOP Choose the desktop environment."
echo " (none/xfce4/gnome/cinnamon/lxqt/lxde/unity/budgie/kde)"
echo " This only has an effect in kombination with -d or --desktop"
echo " -u, --username USERNAME Enter the username for the sudo user"
echo " -p, --password PASSWORD Enter the password for the sudo user"
echo " -i, --interactive yes/no Start an interactive shell inside the container"
echo " -b Build the image with the specified configuration without asking"
echo "-------------------------------------------------------------------------------------------------"
echo "For example: $0 -s sid -d none -k latest -B rock3a -u USERNAME123 -p PASSWORD123 -b"
exit 1
}
boardlist() {
echo "Supported boards are:"
echo "====================="
echo "rock3a"
echo "rock4b"
echo "rock4bplus"
echo "rock4c"
echo "rock4cplus"
echo "rock4se"
echo "rock5a"
echo "rock5b"
exit 1
}
# Check if running with sudo
if [ "$UID" -ne 0 ]; then
echo "This program needs sudo rights."
echo "Run it with 'sudo $0'"
exit 1
fi
# Parse arguments
while [[ "$#" -gt 0 ]]; do
case $1 in
-h|--help) usage;;
-B|--board) BOARD="$2"; shift ;;
--boardlist) boardlist;;
-s|--suite) SUITE="$2"; shift ;;
-k|--kernel) KERNEL="$2"; shift ;;
-d|--desktop) DESKTOP="$2"; shift ;;
-u|--username) USERNAME="$2"; shift ;;
-p|--password) PASSWORD="$2"; shift ;;
-i|--interactive) INTERACTIVE="$2"; shift ;;
-b) BUILD="yes" ;;
*) echo "Unknown parameter passed: $1"; usage ;;
esac
shift
done
if [ "$BOARD" == "rock4b" ]; then
BOOTLOADER="boot-rock_pi_4.bin.gz"
elif [ "$BOARD" == "rock4c" ]; then
BOOTLOADER="boot-rock_pi_4c.bin.gz"
elif [ "$BOARD" == "rock4se" ]; then
BOOTLOADER="boot-rock_pi_4se.bin.gz"
elif [ "$BOARD" == "rock5a" ]; then
BOOTLOADER="boot-rock_5a.bin.gz"
elif [ "$BOARD" == "rock5b" ]; then
BOOTLOADER="boot-rock_5b.bin.gz"
elif [ "$BOARD" == "rock3a" ]; then
BOOTLOADER="boot-rock_3a.bin.gz"
elif [ "$BOARD" == "rock4cplus" ]; then
BOOTLOADER="boot-rock_pi_4c_plus.bin.gz"
fi
echo "----------------------"
echo "cleaning build area..."
echo "----------------------"
sleep 2
rm -rf .rootfs/
echo ""
if [ -z "$SUITE" ] || [ -z "$DESKTOP" ] || [ -z "$USERNAME" ] || [ -z "$PASSWORD" ] || [ -z "$KERNEL" ] || [ -z "$BOARD" ]; then
##########################################################################################################################
info_text=" HELP ME TO IMPROVE THIS PROGRAM\n\nSend an E-mail with suggestions to: [email protected]"
whiptail --title "Information" --msgbox "$info_text" 20 65
##########################################################################################################################
whiptail --title "Menu" --menu "Choose a Debian Suite" 20 65 6 \
"1" "testing" \
"2" "experimental" \
"3" "trixie" \
"4" "sid" \
"5" "bookworm" \
"6" "bullseye" 2> choice.txt
ret=$?
case $ret in
0) # OK
choice=$(cat choice.txt)
case $choice in
1)
echo "SUITE=testing" > .config
;;
2)
echo "SUITE=experimental" > .config
;;
3)
echo "SUITE=trixie" > .config
;;
4)
echo "SUITE=sid" > .config
;;
5)
echo "SUITE=bookworm" > .config
;;
6)
echo "SUITE=bullseye" > .config
;;
*)
echo "Invalid option"
;;
esac
;;
1) # Cancel
echo "Cancelled by user"
rm choice.txt
exit 1
;;
*)
echo "An unexpected error occurred"
rm choice.txt
exit 1
;;
esac
rm choice.txt
##########################################################################################################################
whiptail --title "Menu" --menu "Choose a board" 20 65 6 \
"1" "rock3a" \
"2" "rock4b(maybe also works for rock4a)" \
"3" "rock4bplus" \
"4" "rock4c" \
"5" "rock4se" \
"6" "rock5a" \
"7" "rock5b" 2> choice.txt
ret=$?
case $ret in
0) # OK
choice=$(cat choice.txt)
case $choice in
1)
echo "BOARD=rock3a" >> .config
;;
2)
echo "BOARD=rock4b" >> .config
;;
3)
echo "BOARD=rock4bplus" >> .config
;;
4)
echo "BOARD=rock4c" >> .config
;;
5)
echo "BOARD=rock4se" >> .config
;;
6)
echo "BOARD=rock4cplus" >> .config
;;
7)
echo "BOARD=rock5a" >> .config
;;
8)
echo "BOARD=rock5b" >> .config
;;
*)
echo "Invalid option"
;;
esac
;;
1) # Cancel
echo "Cancelled by user"
rm choice.txt
exit 1
;;
*)
echo "An unexpected error occurred"
rm choice.txt
exit 1
;;
esac
rm choice.txt
##########################################################################################################################
whiptail --title "Menu" --menu "Choose Kernel to install" 20 65 6 \
"1" "Standardkernel of the Debian Suite" \
"2" "Download and compile latest availible Kernel" 2> choice.txt
ret=$?
case $ret in
0) # OK
choice=$(cat choice.txt)
case $choice in
1)
echo "KERNEL=standard" >> .config
;;
2)
echo "KERNEL=latest" >> .config
;;
*)
echo "Invalid option"
;;
esac
;;
1) # Cancel
echo "Cancelled by user"
rm choice.txt
exit 1
;;
*)
echo "An unexpected error occurred"
rm choice.txt
exit 1
;;
esac
rm choice.txt
##########################################################################################################################
whiptail --title "Menu" --menu "Choose a Desktop option" 20 65 10 \
"1" "none" \
"2" "xfce" \
"3" "gnome" \
"4" "mate" \
"5" "cinnamon" \
"6" "lxqt" \
"7" "lxde" \
"8" "unity" \
"9" "budgie" \
"10" "kde" 2> choice.txt
ret=$?
case $ret in
0) # OK
choice=$(cat choice.txt)
case $choice in
1)
echo "DESKTOP=CLI" >> .config
;;
2)
echo "DESKTOP=xfce4" >> .config
;;
3)
echo "DESKTOP=gnome" >> .config
;;
4)
echo "DESKTOP=mate" >> .config
;;
5)
echo "DESKTOP=cinnamon" >> .config
;;
6)
echo "DESKTOP=lxqt" >> .config
;;
7)
echo "DESKTOP=lxde" >> .config
;;
8)
echo "DESKTOP=unity" >> .config
;;
9)
echo "DESKTOP=budgie" >> .config
;;
10)
echo "DESKTOP=kde" >> .config
;;
*)
echo "Invalid option"
;;
esac
;;
1) # Cancel
echo "Cancelled by user"
rm choice.txt
exit 1
;;
*)
echo "An unexpected error occurred"
rm choice.txt
exit 1
;;
esac
rm choice.txt
##########################################################################################################################
USERNAME=$(whiptail --title "Create sudo user" --inputbox "Enter username:" 20 65 3>&1 1>&2 2>&3)
# Passwort abfragen
PASSWORD=$(whiptail --title "Create sudo user" --passwordbox "Enter password:" 20 65 3>&1 1>&2 2>&3)
echo "USERNAME=${USERNAME}" >> .config
echo "PASSWORD=${PASSWORD}" >> .config
##########################################################################################################################
whiptail --title "Menu" --menu "Choose an option" 20 65 4 \
"1" "Just build with the given configuration" \
"2" "Start interactive shell in the container" 2> choice.txt
ret=$?
case $ret in
0) # OK
choice=$(cat choice.txt)
case $choice in
1)
echo "INTERACTIVE=no" >> .config
;;
2)
echo "INTERACTIVE=yes" >> .config
;;
*)
echo "Invalid option"
;;
esac
;;
1) # Cancel
echo "Cancelled by user"
rm choice.txt
exit 1
;;
*)
echo "An unexpected error occurred"
rm choice.txt
exit 1
;;
esac
rm choice.txt
##########################################################################################################################
while IFS='=' read -r key value; do
case "$key" in
SUITE)
SUITE="$value"
;;
BOARD)
BOARD="$value"
;;
KERNEL)
KERNEL="$value"
;;
DESKTOP)
DESKTOP="$value"
;;
HEADERS)
HEADERS="$value"
;;
USERNAME)
USERNAME="$value"
;;
PASSWORD)
PASSWORD="$value"
;;
INTERACTIVE)
INTERACTIVE="$value"
;;
*)
;;
esac
done < .config
fi
if [ "$BUILD" == "no" ]; then
##########################################################################################################################
display_variables() {
whiptail --title "Is this configuration correct?" --yesno \
"SUITE=$SUITE\nBoard=$BOARD\nKERNEL=$KERNEL\nDESKTOP=$DESKTOP\nUSERNAME=$USERNAME\nPASSWORD=$PASSWORD\nINTERACTIVE=$INTERACTIVE" \
20 65
}
display_variables
if [ $? -eq 0 ]; then
BUILD=yes
else
BUILD=no
fi
fi
##########################################################################################################################
if [[ "$BUILD" == "yes" ]]; then
while IFS='=' read -r key value; do
case "$key" in
SUITE)
SUITE="$value"
;;
DESKTOP)
DESKTOP="$value"
;;
KERNEL)
KERNEL="$value"
;;
BOARD)
BOARD="$value"
;;
HEADERS)
HEADERS="$value"
;;
USERNAME)
USERNAME="$value"
;;
PASSWORD)
PASSWORD="$value"
;;
INTERACTIVE)
INTERACTIVE="$value"
;;
*)
;;
esac
done < .config
##########################################################################################################################
if [ "$KERNEL" == "latest" ]; then
echo "0" > config/kernel_status
xfce4-terminal --title="Building Kernel" --command="config/makekernel.sh" &
fi
##########################################################################################################################
echo "Building Docker image..."
docker build --build-arg "SUITE="$SUITE --build-arg "BOARD="$BOARD --build-arg "DESKTOP="$DESKTOP --build-arg "USERNAME="$USERNAME --build-arg "PASSWORD="$PASSWORD --build-arg "KERNEL="$KERNEL -t debian:finest -f config/Dockerfile .
##########################################################################################################################
docker run --platform=aarch64 -dit --name debiancontainer debian:finest /bin/bash
if [ "$KERNEL" == "latest" ]; then
echo "Waiting for Kernel compilation..."
while [[ "$(cat config/kernel_status)" != "1" ]]; do
sleep 2
done
rm config/kernel_status
docker cp kernel*.zip debiancontainer:/
docker cp config/installkernel.sh debiancontainer:/
docker exec debiancontainer bash -c '/installkernel.sh kernel-*.zip'
docker exec debiancontainer bash -c 'rm -rf /kernel*.zip'
docker exec debiancontainer bash -c 'rm /installkernel.sh'
docker exec debiancontainer bash -c 'u-boot-update'
rm kernel-*.zip
else
echo "standard" > config/release
fi
docker cp config/resizeroot debiancontainer:/usr/local/bin
docker exec debiancontainer bash -c 'chmod +x /usr/local/bin/resizeroot'
##########################################################################################################################
if [[ "$INTERACTIVE" == "yes" ]]; then
docker attach debiancontainer
docker start debiancontainer
fi
##########################################################################################################################
docker cp debiancontainer:/rootfs_size.txt config/
ROOTFS=.rootfs.img
if [[ "$KERNEL" == "standard" ]]; then
reserved=750
else
reserved=1300
fi
rootfs_size=$(cat config/rootfs_size.txt)
echo "Creating an empty rootfs image..."
dd if=/dev/zero of=$ROOTFS bs=1M count=$((${rootfs_size} + $reserved)) status=progress
rm config/rootfs_size.txt
mkfs.ext4 -L rootfs $ROOTFS -F
mkfs.ext4 ${ROOTFS} -L rootfs -F
mkdir -p .loop/root
mount ${ROOTFS} .loop/root
docker export -o .rootfs.tar debiancontainer
tar -xvf .rootfs.tar -C .loop/root
docker kill debiancontainer
docker rm debiancontainer
docker rmi debiancontainer:latest
mkdir -p output/Debian-${SUITE}-${DESKTOP}-${BOARD}-build-${TIMESTAMP}/.qemu
rm .loop/root/.dockerenv
cp .loop/root/boot/vmlinuz* output/Debian-${SUITE}-${DESKTOP}-${BOARD}-build-${TIMESTAMP}/.qemu/vmlinuz
cp .loop/root/boot/initrd* output/Debian-${SUITE}-${DESKTOP}-${BOARD}-build-${TIMESTAMP}/.qemu/initrd.img
umount .loop/root
e2fsck -fvC 0 ${ROOTFS}
gzip ${ROOTFS}
RELEASE=$(cat config/release)
if [ "$DESKTOP" == "none" ]; then
DESKTOP="CLI"
fi
if [ "$KERNEL" == "standard" ]; then
RELEASE=standard
fi
zcat config/${BOOTLOADER} ${ROOTFS}.gz > "output/Debian-${SUITE}-${DESKTOP}-${BOARD}-build-${TIMESTAMP}/Debian-${SUITE}-${DESKTOP}-Kernel-${RELEASE}.img"
rm -rf .loop/root .loop/ .rootfs.img .rootfs.tar "${ROOTFS}.gz"
SDCARD="output/Debian-${SUITE}-${DESKTOP}-${BOARD}-build-${TIMESTAMP}/Debian-${SUITE}-${DESKTOP}-Kernel-${RELEASE}.img"
available_cpus=$(nproc)
max_cpus=8
if ((available_cpus > max_cpus)); then
CPUS=$max_cpus
else
CPUS=$available_cpus
fi
if [ "$BOARD" == "rock4b" ]; then
CPU="cortex-a72"
elif [ "$BOARD" == "rock4bplus" ]; then
CPU="cortex-a72"
elif [ "$BOARD" == "rock4c" ]; then
CPU="cortex-a72"
elif [ "$BOARD" == "rock4cplus" ]; then
CPU="cortex-a72"
elif [ "$BOARD" == "rock4se" ]; then
CPU="cortex-a72"
elif [ "$BOARD" == "rock5a" ]; then
CPU="cortex-a76"
elif [ "$BOARD" == "rock5b" ]; then
CPU="cortex-a76"
fi
sudo qemu-system-aarch64 \
-M virt \
-cpu $CPU -smp $CPUS \
-m 4096 \
-kernel "$(dirname "$SDCARD")/.qemu/"vmlinuz \
-initrd "$(dirname "$SDCARD")/.qemu/"initrd.img \
-drive if=none,file=${SDCARD},format=raw,id=disk \
-bios /usr/lib/u-boot/qemu_arm64/u-boot.bin \
-append "root=LABEL=rootfs rw" \
-device virtio-blk-device,drive=disk \
-device virtio-keyboard-pci \
-netdev user,id=net0 \
-device virtio-net-pci,netdev=net0 \
-device virtio-mouse-pci -nographic -no-reboot
##########################################################################################################################
filesize=$(stat -c %s "output/Debian-${SUITE}-${DESKTOP}-${BOARD}-build-${TIMESTAMP}/Debian-${SUITE}-${DESKTOP}-Kernel-${RELEASE}.img")
if [ $filesize -gt 1073741824 ]; then
echo "--------------------------------------"
echo "CONGRATULATION, BUILD WAS SUCCESSFULL!"
echo "--------------------------------------"
else
echo "--------------------------------"
echo "SORRY, BUILD WAS NOT SUCCESSFULL"
echo "--------------------------------"
fi
chown -R ${SUDO_USER}:${SUDO_USER} ./
rm config/release
fi