-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbake
executable file
·760 lines (681 loc) · 20.6 KB
/
bake
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
ROOT_DIR="$(dirname $(readlink -f "${BASH_SOURCE[0]}"))"
STATUS=$ROOT_DIR/.status
RPIBAKE=rpibake
PI_UID=1000 # The user id of the regular Raspberry Pi user, pi.
source $ROOT_DIR/vars
Q_IMG_BASENAME=$(basename -- ${Q_IMG_URL})
Q_IMG_EXTENSION="${Q_IMG_BASENAME#*.}"
Q_IMG_BASE=$(basename -- ${Q_IMG_URL} .${Q_IMG_EXTENSION})
Q_IMG=${ROOT_DIR}/binaries/${Q_IMG_BASE}.img
CLIENT_HOSTNAME=localhost
function debug() {
echo $@ 1>&2
}
function help() {
debug "bake"
debug " prerequisites Install host system prerequisites to run QEMU."
debug " image"
debug " reset Download the image specified in the configuration and reset local changes."
debug " expand BYTES Expand the image to a given size in bytes."
debug " mount Mount the image."
debug " init Store the SSH installer into image."
debug " umount Unmount the image."
debug " userpass USER PASS Configure autoconfiguration to create user with password."
debug " qemu Run Raspberry Pi OS in QEMU."
debug " versatile Run with a more generic arm1176 configuration."
debug " raspi3b Run QEMU with a Raspberru Pi 3 Model B native emulation configuration."
debug " wait Wait until SSH succeeds."
debug " ssh SSH into the guest system."
debug " rsync"
debug " ssh Invoke rsync over SSH."
debug " halt SSH into the guest system and turn it off."
debug " get"
debug " user Get the client user name."
debug " ssh"
debug " origin Get the SSH destination user@hostname."
debug " port Get the SSH port."
debug " public Get the location of the public key used for SSH."
debug " private Get the location of the private key used for SSH."
debug " mount"
debug " loop Get the loop device path if it exist."
debug " boot Get the mount location of the boot partition."
debug " user Get the mount location of the user partition."
debug " native"
debug " kernel Get the kernel file to use with native emulation."
debug " dtb Get the device tree binary file to use with native emulation."
debug " set"
debug " user Set the client user name."
debug " ssh"
debug " public FILE Set the location of the public key used for SSH."
debug " private FILE Set the location of the private key used for SSH."
debug " native"
debug " kernel FILE Set the kernel file to use with native emulation."
debug " dtb FILE Set the device tree binary file to use with native emulation."
exit -1
}
function pipe_ssh() {
variable_get_ssh_private
variable_get_client_user
set +e
ssh \
${CLIENT_USER}@${CLIENT_HOSTNAME} \
-p ${Q_HOST_SSH_PORT} \
-i ${SSH_PRIVATE} \
-o StrictHostKeyChecking=no \
-o 'ConnectionAttempts 1000' \
$@ \
;
RETCODE=$?
set -e
return $RETCODE
}
function pipe_ssh_exit() {
pipe_ssh -T <<EOF
exit
EOF
}
function pipe_wait() {
until pipe_ssh_exit
do
sleep 1
done
}
function pipe_rsync_ssh() {
variable_get_ssh_private
rsync -e "ssh -p ${Q_HOST_SSH_PORT} -i ${SSH_PRIVATE}" $@
}
function trim() {
echo "$1" | sed -re 's/^[\t\r\n ]*//' | sed -re 's/[\t\r\n ]*$//'
}
function partition_count() {
IMG=$1
partx $IMG -g | wc -l
}
function partition_query() {
IMG=$1
PARTITION=$2
FIELD=$3
trim "$(partx $IMG --bytes -g -n $PARTITION -o $FIELD)"
}
function variable_set_ssh_public() {
crudini --set $STATUS $RPIBAKE SSH_PUBLIC $1
}
function variable_set_ssh_private() {
crudini --set $STATUS $RPIBAKE SSH_PRIVATE $1
}
function variable_set_user() {
crudini --set $STATUS $RPIBAKE CLIENT_USER $1
}
function variable_set_native_kernel() {
crudini --set $STATUS $RPIBAKE NATIVE_KERNEL $1
}
function variable_set_native_dtb() {
crudini --set $STATUS $RPIBAKE NATIVE_DTB $1
}
function variable_get_ssh_public() {
(crudini --get --existing $STATUS $RPIBAKE SSH_PUBLIC > /dev/null) || (debug "Did you forget to set the ssh public key 'set ssh public'?"; exit 1)
export SSH_PUBLIC=$(crudini --get --existing $STATUS $RPIBAKE SSH_PUBLIC)
}
function variable_get_ssh_private() {
(crudini --get --existing $STATUS $RPIBAKE SSH_PRIVATE > /dev/null) || (debug "Did you forget to set the ssh private key 'set ssh private'?"; exit 1)
export SSH_PRIVATE=$(crudini --get --existing $STATUS $RPIBAKE SSH_PRIVATE)
}
function variable_get_client_user() {
if crudini --get --existing $STATUS $RPIBAKE CLIENT_USER >& /dev/null
then
export CLIENT_USER=$(crudini --get --existing $STATUS $RPIBAKE CLIENT_USER)
else
export CLIENT_USER=pi
fi
}
function variable_get_native_kernel() {
(crudini --get --existing $STATUS $RPIBAKE NATIVE_KERNEL > /dev/null) || (debug "Did you forget to set the native kernel 'set native kernel'?"; exit 1)
export NATIVE_KERNEL=$(crudini --get --existing $STATUS $RPIBAKE NATIVE_KERNEL)
}
function variable_get_native_dtb() {
(crudini --get --existing $STATUS $RPIBAKE NATIVE_DTB > /dev/null) || (debug "Did you forget to set the native dtb 'set native dtb'?"; exit 1)
export NATIVE_DTB=$(crudini --get --existing $STATUS $RPIBAKE NATIVE_DTB)
}
function image_variable_set() {
crudini --set $STATUS $RPIBAKE LOOPBACK $LOOPBACK
crudini --set $STATUS $RPIBAKE PART_BOOT $PART_BOOT
crudini --set $STATUS $RPIBAKE PART_USER $PART_USER
}
function image_variable_del() {
crudini --del $STATUS $RPIBAKE LOOPBACK
crudini --del $STATUS $RPIBAKE PART_BOOT
crudini --del $STATUS $RPIBAKE PART_USER
}
function image_variable_get() {
export LOOPBACK=$(crudini --get $STATUS $RPIBAKE LOOPBACK)
export PART_BOOT=$(crudini --get $STATUS $RPIBAKE PART_BOOT)
export PART_USER=$(crudini --get $STATUS $RPIBAKE PART_USER)
}
function stage_prerequisites() {
sudo apt-get -y install \
crudini \
xz-utils \
;
case $(lsb_release --short --id) in
"Ubuntu")
local release=$(lsb_release --short --release)
local major=$(echo $release | cut -f 1 -d '.')
local minor=$(echo $release | cut -f 2 -d '.')
if [[ ( "$major" -ge "22" && "$minor" -ge "4" ) || "$major" -gt "22" ]]
then
sudo apt-get -y install \
qemu-utils \
qemu-system-arm \
qemu-system-aarch64 \
;
else
debug "This version of Ubuntu may not have QEMU 6.2.0 or later available via a repository."
debug "QEMU will not be installed automatically."
fi
;;
esac
}
function stage_image() {
if [ "$#" -lt 1 ]; then
debug "Missing image argument."
help
fi
COMMAND=$1
shift
case ${COMMAND} in
reset)
stage_image_reset $@
;;
expand)
stage_image_expand $@
;;
mount)
stage_image_mount $@
;;
init)
stage_image_init $@
;;
umount)
stage_image_umount $@
;;
userpass)
stage_image_userpass $@
;;
*)
debug "Unknown image command \"${COMMAND}\"."
help
;;
esac
}
function stage_image_reset() {
if [ ! -f "${HOME}/RaspberryPiOS/${Q_IMG_BASE}.img" ]
then
mkdir -p "${HOME}/RaspberryPiOS"
pushd "${HOME}/RaspberryPiOS" >& /dev/null
debug "Download ${Q_IMG_URL}"
wget --continue "${Q_IMG_URL}"
case "${Q_IMG_EXTENSION}" in
zip)
debug "Unzip ${Q_IMG_BASE}.img"
time unzip "${Q_IMG_BASE}.zip"
;;
img.xz)
debug "Decompress ${Q_IMG_BASE}.img"
time unxz -k "${Q_IMG_BASE}.img.xz"
;;
*)
debug "Unandled image file extension ${Q_IMG_EXTENSION}"
exit -1
esac
popd >& /dev/null
fi
debug "Copy ${Q_IMG_BASE}.img"
mkdir -p "${ROOT_DIR}/binaries"
pushd "${ROOT_DIR}/binaries" >& /dev/null
rm -f "${Q_IMG_BASE}.img"
time cp "${HOME}/RaspberryPiOS/${Q_IMG_BASE}.img" .
popd >& /dev/null
}
function stage_image_expand() {
DISK_SIZE=$1
shift
SECTOR_SIZE=512
# Verify that the new disk size is in a multiple of the sector size.
if [ "0" -ne $(( ${DISK_SIZE} % ${SECTOR_SIZE})) ]
then
debug "The specified disk size ${DISK_SIZE} is not divisible by the sector size ${SECTOR_SIZE}."
exit -1
fi
# Verify that the image contains exactly partitions 1 and 2.
COUNT_PARTITION=$(partition_count ${Q_IMG})
if [ 2 -ne $COUNT_PARTITION ]
then
debug "Expected to find 2 partitions."
exit -1
fi
for i in $(seq $COUNT_PARTITION)
do
case "$i" in
1) # This should map to the boot partition.
;&
2) # This should map to the main partition.
debug "Partition $i"
debug "start $(partition_query ${Q_IMG} $i START)s"
debug "end $(partition_query ${Q_IMG} $i END)s"
debug "sectors $(partition_query ${Q_IMG} $i SECTORS)s"
debug "size $(partition_query ${Q_IMG} $i SIZE)"
debug "name $(partition_query ${Q_IMG} $i NAME)"
debug "uuid $(partition_query ${Q_IMG} $i UUID)"
debug
;;
*)
debug "Unexpected partition number $i."
exit -1
;;
esac
done
# Verify that the partition is actually being expanded and not shrunk.
DISK_SIZE_CURRENT=$(stat -c "%s" ${Q_IMG})
if [ ${DISK_SIZE} -lt ${DISK_SIZE_CURRENT} ]
then
debug "The requested disk size ${DISK_SIZE} is less than the current disk size ${DISK_SIZE_CURRENT}."
help
fi
# Expand disk image file.
qemu-img resize -f raw ${Q_IMG} ${DISK_SIZE} 1>&2
# Expand partition.
DISK_SIZE_SECTORS=$(( ${DISK_SIZE} / ${SECTOR_SIZE} ))
DISK_END_SECTORS=$(( ${DISK_SIZE_SECTORS} - 1 )) # The minus 1 is because this is the last actual sector's number.
parted ${Q_IMG} resizepart 2 ${DISK_END_SECTORS}s
debug "Loop back image."
LOOPBACK=$(sudo losetup -f)
sudo losetup -P $LOOPBACK $Q_IMG
PART_BOOT=$(mktemp -d)
PART_USER=$(mktemp -d)
function unloop_disks() {
debug "Unloop image."
sudo losetup -d $LOOPBACK
}
trap unloop_disks EXIT
# Filesystem check before.
sudo e2fsck -f ${LOOPBACK}p2
# Expand filesystem.
sudo resize2fs ${LOOPBACK}p2
# Filesystem check and after.
sudo e2fsck -f ${LOOPBACK}p2
trap - EXIT
unloop_disks
}
function stage_image_mount() {
debug "Loop back image."
LOOPBACK=$(sudo losetup -f)
sudo losetup -P $LOOPBACK $Q_IMG
PART_BOOT=$(mktemp -d)
PART_USER=$(mktemp -d)
function unloop_image() {
debug "Unloop image."
sudo losetup -d $LOOPBACK
}
trap unloop_image EXIT
debug "Set mount information."
image_variable_set
debug "Mount image."
sudo mount ${LOOPBACK}p1 $PART_BOOT
sudo mount ${LOOPBACK}p2 $PART_USER
trap - EXIT
# Don't run unloop_image.
}
function stage_image_init() {
variable_get_ssh_public
image_variable_get
debug "Install SSH keys."
sudo -u \#$PI_UID mkdir -p $PART_USER/home/pi/.ssh
sudo -u \#$PI_UID cp ${SSH_PUBLIC} $PART_USER/home/pi/.ssh/authorized_keys
debug "Install SSH server installer."
sudo mv $PART_USER/etc/rc.local $PART_USER/etc/rc.local.original
sudo cp $ROOT_DIR/mount/rc.local $PART_USER/etc/
}
function stage_image_umount() {
debug "Get mount information."
image_variable_get
debug "Unmount image."
sudo umount $PART_BOOT
sudo umount $PART_USER
debug "Unloop image."
sudo losetup -d $LOOPBACK
debug "Delete mount information."
image_variable_del
}
function stage_image_userpass() {
if [ "$#" -lt 2 ]; then
debug "Missing user or password arguments."
help
fi
local NEW_USER=$1
local NEW_PASSWD=$2
shift 2
debug "Configuring new user."
image_variable_get
echo | sudo tee -a $PART_BOOT/userconf <<EOF
$NEW_USER:$NEW_PASSWD
EOF
}
function stage_qemu_versatile() {
# The maximum number of CPUs is 1.
# The maximum amount of memory is 256MB.
# iperf reports a network throughput of ~25 Mbits/sec.
sudo qemu-system-arm \
-M versatilepb \
-cpu arm1176 \
-m 256 \
-append "root=/dev/sda2 rootfstype=ext4 rw" \
-net nic \
-net user,hostfwd=tcp::$Q_HOST_SSH_PORT-:22 \
-net tap,ifname=vnet0,script=no,downscript=no \
-drive file=$Q_IMG,format=raw \
-dtb ${ROOT_DIR}/qemu-rpi-kernel/${Q_DTB}.dtb \
-kernel ${ROOT_DIR}/qemu-rpi-kernel/${Q_KERNEL} \
-no-reboot \
$@ \
;
}
function stage_qemu_raspi3b() {
variable_get_native_kernel
variable_get_native_dtb
# iperf reports a network throughput of ~600-850 Kbits/sec.
sudo qemu-system-aarch64 \
-M raspi3b \
-append "rw earlyprintk loglevel=8 console=ttyAMA0,115200 dwc_otg.lpm_enable=0 root=/dev/mmcblk0p2 rootdelay=1" \
-kernel "${NATIVE_KERNEL}" \
-dtb "${NATIVE_DTB}" \
-sd "${Q_IMG}" \
-m 1G \
-smp 4 \
-serial stdio \
-usb \
-device usb-mouse \
-device usb-kbd \
-device usb-net,netdev=net0 \
-device usb-net,netdev=net1 \
-netdev user,id=net0,hostfwd=tcp::$Q_HOST_SSH_PORT-:22 \
-netdev tap,id=net1,script=no,downscript=no \
-no-reboot \
$@ \
;
}
function stage_qemu() {
if [ "$#" -lt 1 ]; then
debug "Missing qemu argument."
help
fi
COMMAND=$1
shift
case ${COMMAND} in
versatile)
stage_qemu_versatile $@
;;
raspi3b)
stage_qemu_raspi3b $@
;;
*)
debug "Unknown qemu command \"${COMMAND}\"."
help
;;
esac
}
function stage_wait() {
pipe_wait $@
}
function stage_ssh() {
pipe_ssh $@
}
function stage_rsync() {
if [ "$#" -lt 1 ]; then
debug "Missing rsync argument."
help
fi
COMMAND=$1
shift
case ${COMMAND} in
ssh)
stage_rsync_ssh $@
;;
*)
debug "Unknown rsync command \"${COMMAND}\"."
help
;;
esac
}
function stage_rsync_ssh() {
pipe_rsync_ssh $@
}
function stage_halt() {
stage_ssh $@ <<EOF
sudo reboot
EOF
}
function stage_get() {
if [ "$#" -lt 1 ]; then
debug "Missing get argument."
help
fi
COMMAND=$1
shift
case ${COMMAND} in
user)
variable_get_client_user
echo ${CLIENT_USER}
;;
ssh)
if [ "$#" -lt 1 ]; then
debug "Missing get ssh argument."
help
fi
COMMAND=$1
shift
case ${COMMAND} in
origin)
variable_get_client_user
echo ${CLIENT_USER}@${CLIENT_HOSTNAME}
;;
port)
echo ${Q_HOST_SSH_PORT}
;;
public)
variable_get_ssh_public
echo ${SSH_PUBLIC}
;;
private)
variable_get_ssh_private
echo ${SSH_PRIVATE}
;;
*)
debug "Unknown get ssh command \"${COMMAND}\"."
help
;;
esac
;;
mount)
if [ "$#" -lt 1 ]; then
debug "Missing get mount argument."
help
fi
COMMAND=$1
shift
image_variable_get
case ${COMMAND} in
loop)
echo ${LOOPBACK}
;;
boot)
echo ${PART_BOOT}
;;
user)
echo ${PART_USER}
;;
*)
debug "Unknown get mount command \"${COMMAND}\"."
help
;;
esac
;;
native)
if [ "$#" -lt 1 ]; then
debug "Missing get native argument."
help
fi
COMMAND=$1
shift
case ${COMMAND} in
kernel)
variable_get_native_kernel
echo ${NATIVE_KERNEL}
;;
dtb)
variable_get_native_dtb
echo ${NATIVE_DTB}
;;
*)
debug "Unknown get native command \"${COMMAND}\"."
help
;;
esac
;;
*)
debug "Unknown get command \"${COMMAND}\"."
help
;;
esac
}
function stage_set() {
if [ "$#" -lt 1 ]; then
debug "Missing set argument."
help
fi
COMMAND=$1
shift
case ${COMMAND} in
user)
if [ "$#" -lt 1 ]; then
debug "Missing set user argument."
help
fi
ARG=$1
shift
variable_set_user $ARG
;;
ssh)
if [ "$#" -lt 1 ]; then
debug "Missing set ssh argument."
help
fi
COMMAND=$1
shift
case ${COMMAND} in
public)
if [ "$#" -lt 1 ]; then
debug "Missing set ssh public argument."
help
fi
ARG=$1
shift
variable_set_ssh_public $ARG
;;
private)
if [ "$#" -lt 1 ]; then
debug "Missing set ssh private argument."
help
fi
ARG=$1
shift
variable_set_ssh_private $ARG
;;
*)
debug "Unknown set ssh command \"${COMMAND}\"."
help
;;
esac
;;
native)
if [ "$#" -lt 1 ]; then
debug "Missing set native argument."
help
fi
COMMAND=$1
shift
case ${COMMAND} in
kernel)
if [ "$#" -lt 1 ]; then
debug "Missing set native kernel argument."
help
fi
COMMAND=$1
shift
variable_set_native_kernel $COMMAND
;;
dtb)
if [ "$#" -lt 1 ]; then
debug "Missing set native kernel dtb."
help
fi
COMMAND=$1
shift
variable_set_native_dtb $COMMAND
;;
*)
debug "Unknown get native command \"${COMMAND}\"."
help
;;
esac
;;
*)
debug "Unknown set command \"${COMMAND}\"."
help
;;
esac
}
function stage_main() {
if [ "$#" -lt 1 ]; then
debug "Missing argument."
help
fi
COMMAND=$1
shift
case ${COMMAND} in
prerequisites)
stage_prerequisites $@
;;
image)
stage_image $@
;;
qemu)
stage_qemu $@
;;
wait)
stage_wait $@
;;
ssh)
stage_ssh $@
;;
rsync)
stage_rsync $@
;;
halt)
stage_halt $@
;;
get)
stage_get $@
;;
set)
stage_set $@
;;
*)
debug "Unknown command \"${COMMAND}\"."
help
;;
esac
}
stage_main $@