-
Notifications
You must be signed in to change notification settings - Fork 2
/
pilc-bootstrap.sh
executable file
·1216 lines (1105 loc) · 34.2 KB
/
pilc-bootstrap.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
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
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
#
# PiLC bootstrap
#
# Copyright 2016-2024 Michael Büsch <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
basedir="$(realpath "$0" | xargs dirname)"
[ -n "$basedir" ] || { echo "Failed to canonicalize base directory." >&2; exit 1; }
AWLSIM_MIRROR="https://git.bues.ch/git/awlsim.git"
SUITE=bookworm
MAIN_MIRROR_32="http://raspbian.raspberrypi.com/raspbian/"
MAIN_MIRROR_ARCHIVE="http://archive.raspberrypi.org/debian/"
MAIN_MIRROR_64="http://deb.debian.org/debian/"
MAIN_MIRROR_64_SECURITY="http://deb.debian.org/debian-security/"
KEYRING_VERSION="20120528.2"
KEYRING_BASEURL="$MAIN_MIRROR_32/pool/main/r/raspbian-archive-keyring"
KEYRING_TGZ_FILE="raspbian-archive-keyring_${KEYRING_VERSION}.tar.gz"
KEYRING_TGZ_SHA256="fdf50f775b60901a2783f21a6362e2bf5ee6203983e884940b163faa1293c002"
PPL_VERSION="0.1.1"
PPL_FILE="ppl_v$PPL_VERSION.zip"
PPL_PATH="libs/pixtend/v1/ppl/$PPL_FILE"
PPL_SHA256="103edcdbc377f8b478fcbc89117cbad143500c611cb714568f55513cece220d4"
PPL2_VERSION="0.1.4"
PPL2_FILE="pplv2_v$PPL2_VERSION.zip"
PPL2_PATH="libs/pixtend/v2/pplv2/$PPL2_FILE"
PPL2_SHA256="4c379e15c6536b67b3eb36b79946a52e57aa79681265e6d46104e07987147bf1"
info()
{
echo "--- $*"
}
error()
{
echo "=== ERROR: $*" >&2
}
warning()
{
echo "=== WARNING: $*" >&2
}
die()
{
error "$*"
exit 1
}
# print the first of its arguments.
first()
{
echo "$1"
}
# print the last of its arguments.
last()
{
while [ $# -gt 1 ]; do shift; done
echo "$1"
}
# $1=program_name
have_program()
{
which "$1" >/dev/null 2>&1
}
# $1=program_name, ($2=description)
assert_program()
{
local bin="$1"
local desc="$2"
[ -n "$desc" ] || desc="$bin"
have_program "$bin" || die "$bin not found. Please install $desc."
}
term_signal()
{
die "Terminating signal received"
}
cleanup()
{
info "Cleaning up..."
for mp in "$mp_shm" "$mp_proc_binfmt_misc" "$mp_proc" "$mp_sys" "$mp_bootimgfile" "$mp_rootimgfile"; do
[ -n "$mp" -a -d "$mp" ] &&\
umount -l "$mp" >/dev/null 2>&1
done
for mp in "$mp_bootimgfile" "$mp_rootimgfile"; do
[ -n "$mp" -a -d "$mp" ] &&\
rmdir "$mp" >/dev/null 2>&1
done
}
do_install()
{
install "$@" || die "Failed install $*"
}
do_systemctl()
{
info "systemctl $*"
systemctl "$@" || die "Failed to systemctl $*"
}
write_image()
{
local image="$1"
local dev="$2"
[ -b "$dev" ] || die "$dev is not a block device"
mount | grep -q "$dev" && die "$dev is mounted. Refusing to write to it!"
if have_program blkdiscard; then
info "Discarding $dev ..."
blkdiscard -f "$dev" ||\
error "blkdiscard failed."
else
warning "Skipping discard. blkdiscard not installed."
fi
info "Writing $image to $dev ..."
dd if="$image" of="$dev" bs=32M status=progress ||\
die "Failed to write image."
}
download()
{
local target="$1"
local mirror="$2"
local sha256="$3"
info "Downloading $mirror..."
rm -f "$target"
if printf '%s' "$mirror" | grep -qEe '^\./|^\../|^/'; then
# "mirror" starts with ./ ../ or /
# This is a local file.
cp "$mirror" "$target" || die "Failed to fetch $mirror"
else
# Download the file
wget -O "$target" "$mirror" || die "Failed to fetch $mirror"
fi
[ "$(sha256sum -b "$target" | cut -f1 -d' ')" = "$sha256" ] ||\
die "SHA256 verification of $target failed"
}
extract_archive()
{
local archive="$1"
local extract_dir="$2"
local make_extract_dir="$3"
if [ $make_extract_dir -ne 0 ]; then
mkdir "$extract_dir" ||\
die "Failed to create directory $extract_dir"
fi
if printf '%s' "$archive" | grep -qEe '\.zip$'; then
if [ $make_extract_dir -ne 0 ]; then
unzip -d "$extract_dir" "$archive" ||\
die "Failed to unpack $archive"
else
unzip "$archive" ||\
die "Failed to unpack $archive"
fi
else
if [ $make_extract_dir -ne 0 ]; then
tar --one-top-level="$extract_dir" -xf "$archive" ||\
die "Failed to unpack $archive"
else
tar -xf "$archive" ||\
die "Failed to unpack $archive"
fi
fi
}
build_pythonpack()
{
local python="$1"
local name="$2"
local archive="$3"
local extract_dir="$4"
local make_extract_dir="$5"
info "Building $name for $python..."
rm -rf "/tmp/$extract_dir"
(
cd /tmp || die "Failed to cd /tmp"
extract_archive "$archive" "$extract_dir" "$make_extract_dir"
cd "$extract_dir" ||\
die "Failed to cd $extract_dir"
"$python" ./setup.py install ||\
die "Failed to install $name"
) || die
rm -r "/tmp/$extract_dir" ||\
die "Failed to remove $name build files."
}
build_ppl()
{
local archive="$PPL_FILE"
for python in python3; do
build_pythonpack "$python" "ppl-$PPL_VERSION" \
"$archive" "ppl-$PPL_VERSION" 1
done
rm "/tmp/$archive" ||\
die "Failed to remove /tmp/$archive."
}
build_ppl2()
{
local archive="$PPL2_FILE"
for python in python3; do
build_pythonpack "$python" "ppl2-$PPL2_VERSION" \
"$archive" "ppl2-$PPL2_VERSION" 1
done
rm "/tmp/$archive" ||\
die "Failed to remove /tmp/$archive."
}
pilc_bootstrap_first_stage()
{
echo "Running first stage..."
[ "$(id -u)" = "0" ] || die "Permission denied. Must be root."
# Check host tools (first/third stage).
if have_program 7zz; then
SEVENZIP=7zz
elif have_program 7z; then
SEVENZIP=7z
else
die "7z not found. Please install 7z."
fi
assert_program chroot
assert_program dd
assert_program debootstrap
assert_program git
assert_program gpg
assert_program install
assert_program mkfs.ext4
assert_program mkfs.vfat
assert_program parted
assert_program rsync
assert_program setarch
assert_program tar
assert_program unzip
assert_program wget
[ -x "$opt_qemu" ] ||\
die "The qemu binary '$opt_qemu' is not executable."
info "Cleaning tmp..."
rm -rf "$opt_target_dir"/tmp/*
do_install -d -o root -g root -m 1777 "$opt_target_dir/tmp"
info "Downloading and extracting keys..."
do_install -o root -g root -m 644 \
"$basedir/keys/CF8A1AF502A2AA2D763BAE7E82B129927FA3303E.gpg" \
"$opt_target_dir/tmp/"
if [ $opt_bit -eq 32 ]; then
download "$opt_target_dir/tmp/$KEYRING_TGZ_FILE" \
"$KEYRING_BASEURL/$KEYRING_TGZ_FILE" \
"$KEYRING_TGZ_SHA256"
tar -C "$opt_target_dir/tmp" -x -f "$opt_target_dir/tmp/$KEYRING_TGZ_FILE" ||\
die "Failed to extract keys."
local raspbian_asc="$opt_target_dir/tmp/raspbian-archive-keyring-$KEYRING_VERSION/raspbian.public.key"
local raspbian_gpg="$raspbian_asc.gpg"
gpg --dearmor < "$raspbian_asc" > "$raspbian_gpg" ||\
die "Failed to convert key."
fi
# debootstrap first stage.
if [ $opt_skip_debootstrap1 -eq 0 ]; then
info "Running debootstrap first stage..."
if [ $opt_bit -eq 32 ]; then
local arch="armhf"
local keyopt="--keyring=$raspbian_gpg"
local mirror="$MAIN_MIRROR_32"
else
local arch="arm64"
local keyopt=
local mirror="$MAIN_MIRROR_64"
fi
setarch "linux$opt_bit" \
debootstrap --arch="$arch" --foreign \
--components="main,contrib,non-free" \
$keyopt \
"$SUITE" "$opt_target_dir" "$mirror" \
|| die "debootstrap failed"
fi
[ -d "$opt_target_dir" ] ||\
die "Target directory '$opt_target_dir' does not exist."
# Avoid the start of daemons during second stage.
do_install -o root -g root -m 755 \
"$basedir/templates/policy-rc.d" \
"$opt_target_dir/usr/sbin/"
# Copy qemu.
info "Copying qemu binary from '$opt_qemu' into '$opt_target_dir/usr/bin/'..."
do_install -o root -g root -m 755 "$opt_qemu" "$opt_target_dir/usr/bin"
info "Copying PiLC bootstrap script and templates..."
do_install -o root -g root -m 755 \
"$basedir/pilc-bootstrap.sh" \
"$opt_target_dir/"
cp -r "$basedir/templates" "$opt_target_dir/tmp/" ||\
die "Failed to copy PiLC templates"
cp -r "$basedir/deb" "$opt_target_dir/tmp/" ||\
die "Failed to copy PiLC deb packages"
info "Checking out awlsim..."
local awlsim_dir="$opt_target_dir/tmp/awlsim"
local awlsim_checkout_dir="$awlsim_dir/src"
rm -rf "$awlsim_dir"
do_install -d -o root -g root -m 755 "$awlsim_dir"
git clone --no-checkout "$AWLSIM_MIRROR" "$awlsim_checkout_dir" ||\
die "Failed to clone"
(
cd "$awlsim_checkout_dir" ||\
die "Failed to cd"
git checkout "$opt_branch" ||\
die "Failed to check out branch."
git submodule update --init --recursive submodules/pyprofibus ||\
die "Failed to pull pyprofibus submodule"
rm -r \
.git \
submodules/pyprofibus/.git \
submodules/pyprofibus/phy_fpga/crcgen/.git ||\
die "Failed to remove .git directory."
mv submodules/pyprofibus .. ||\
die "Failed to move pyprofibus submodule."
) || die
# Fetch packages
download "$opt_target_dir/tmp/$PPL_FILE" \
"$awlsim_checkout_dir/$PPL_PATH" \
"$PPL_SHA256"
download "$opt_target_dir/tmp/$PPL2_FILE" \
"$awlsim_checkout_dir/$PPL2_PATH" \
"$PPL2_SHA256"
# Second stage will mount a few filesystems.
# Keep track to umount them in cleanup.
mp_proc="$opt_target_dir/proc"
mp_proc_binfmt_misc="$opt_target_dir/proc/sys/fs/binfmt_misc"
mp_sys="$opt_target_dir/sys"
mp_shm="$opt_target_dir/dev/shm"
}
pilc_bootstrap_second_stage()
{
info "Running second stage..."
[ -x /pilc-bootstrap.sh ] ||\
die "Second stage does not contain the bootstrap script."
# Set up environment.
export LC_ALL=C
export LANGUAGE=C
export LANG=C
if [ "$opt_rpiver" = "1" -o "$opt_rpiver" = "0" ]; then
info "Optimizing for RPi 1, zero(w)"
local march="-march=armv6kz"
local mtune="-mtune=arm1176jzf-s"
elif [ "$opt_rpiver" = "2" ]; then
info "Optimizing for RPi 2"
local march="-march=armv7-a"
local mtune="-mtune=cortex-a7"
elif [ "$opt_rpiver" = "3" ]; then
info "Optimizing for RPi 3"
local march="-march=armv8-a"
local mtune="-mtune=cortex-a53"
elif [ "$opt_rpiver" = "4" ]; then
info "Optimizing for RPi 4"
local march="-march=armv8-a"
local mtune="-mtune=cortex-a72"
elif [ "$opt_rpiver" = "5" ]; then
info "Optimizing for RPi 5"
local march="-march=armv8-a"
local mtune="-mtune=cortex-a76"
else
info "Optimizing for generic RPi"
if [ $opt_bit -eq 32 ]; then
local march="-march=armv6kz"
local mtune=
else
local march="-march=armv8-a"
local mtune=
fi
fi
export CFLAGS="-O3 $march $mtune -pipe"
[ $opt_bit -eq 32 ] && export CFLAGS="$CFLAGS -mfpu=vfp -mfloat-abi=hard"
export CXXFLAGS="$CFLAGS"
# debootstrap second stage.
if [ $opt_skip_debootstrap2 -eq 0 ]; then
info "Running debootstrap second stage..."
/debootstrap/debootstrap --verbose --second-stage ||\
die "Debootstrap second stage failed."
fi
if [ $opt_bit -eq 32 ]; then
info "Disabling raspi-copies-and-fills..."
rm -f /etc/ld.so.preload || die "Failed to disable raspi-copies-and-fills"
fi
info "Mounting /proc..."
do_install -d -o root -g root -m 755 /proc
mount -t proc proc /proc || die "Mounting /proc failed."
info "Mounting /sys..."
do_install -d -o root -g root -m 755 /sys
mount -t sysfs sysfs /sys || die "Mounting /sys failed."
info "Mounting /dev/shm..."
do_install -d -o root -g root -m 755 /dev/shm
mount -t tmpfs tmpfs /dev/shm || die "Mounting /dev/shm failed."
info "Creating /etc/fstab"
do_install -d -o root -g root -m 755 /config
do_install -T -o root -g root -m 644 \
/tmp/templates/fstab \
/etc/fstab
info "Writing misc /etc stuff..."
echo "pilc" > /etc/hostname || die "Failed to set hostname"
printf 'PiLC GNU/Linux (based on Raspberry Pi OS) \\n \\l\n\n' > /etc/issue ||\
die "Failed to create /etc/issue"
printf 'PiLC GNU/Linux (based on Raspberry Pi OS)\n' > /etc/issue.net ||\
die "Failed to create /etc/issue.net"
sed -i -e 's|PRETTY_NAME=.*|PRETTY_NAME="PiLC"|' \
/etc/os-release ||\
die "Failed to set os-release PRETTY_NAME."
sed -i -e 's|NAME=.*|NAME="PiLC"|' \
/etc/os-release ||\
die "Failed to set os-release NAME."
sed -i -e 's|ID=.*|ID=pilc|' \
/etc/os-release ||\
die "Failed to set os-release ID."
sed -i -e 's|ID_LIKE=.*|ID_LIKE=debian|' \
/etc/os-release ||\
die "Failed to set os-release ID_LIKE."
sed -i -e 's|HOME_URL=.*|HOME_URL="https://bues.ch/a/pilc"|' \
/etc/os-release ||\
die "Failed to set os-release HOME_URL."
sed -i -e 's|SUPPORT_URL=.*|SUPPORT_URL="https://bues.ch/a/pilc"|' \
/etc/os-release ||\
die "Failed to set os-release SUPPORT_URL."
sed -i -e 's|BUG_REPORT_URL=.*|BUG_REPORT_URL="https://bues.ch/a/pilc"|' \
/etc/os-release ||\
die "Failed to set os-release BUG_REPORT_URL."
info "Writing apt configuration..."
local apt_opts="-y -o Acquire::Retries=3"
if [ $opt_bit -eq 32 ]; then
cat > /etc/apt/sources.list <<EOF
deb [ arch=armhf ] $MAIN_MIRROR_32 $SUITE main contrib non-free rpi
EOF
[ $? -eq 0 ] || die "Failed to set sources.list"
dpkg --add-architecture arm64 ||\
die "dpkg --add-architecture failed"
else
cat > /etc/apt/sources.list <<EOF
deb $MAIN_MIRROR_64 $SUITE main contrib non-free
deb $MAIN_MIRROR_64_SECURITY $SUITE-security main contrib non-free
deb $MAIN_MIRROR_64 $SUITE-updates main contrib non-free
EOF
[ $? -eq 0 ] || die "Failed to set sources.list"
dpkg --add-architecture armhf ||\
die "dpkg --add-architecture failed"
fi
echo 'Acquire { Languages "none"; };' > /etc/apt/apt.conf.d/99no-translations ||\
die "Failed to set apt.conf.d"
cat /tmp/templates/debconf-set-selections-preinstall.conf | debconf-set-selections ||\
die "Failed to configure debconf settings"
apt-get $apt_opts update ||\
die "apt-get update failed"
apt-get $apt_opts install apt-transport-https ||\
die "apt-get install apt-transport-https failed"
apt-get $apt_opts install \
gnupg2 \
debian-keyring \
|| die "apt-get install keyrings failed"
cat > /etc/apt/sources.list.d/raspi.list <<EOF
deb $MAIN_MIRROR_ARCHIVE $SUITE main
EOF
[ $? -eq 0 ] || die "Failed to update sources.list"
do_install -o root -g root -m 644 \
/tmp/CF8A1AF502A2AA2D763BAE7E82B129927FA3303E.gpg \
/etc/apt/trusted.gpg.d/
apt-get $apt_opts update ||\
die "apt-get update failed"
if [ $opt_bit -eq 32 ]; then
apt-get $apt_opts install \
raspbian-archive-keyring \
|| die "apt-get install archive-keyrings failed"
fi
info "Upgrading system..."
apt-get $apt_opts dist-upgrade ||\
die "apt-get dist-upgrade failed"
info "Installing packages..."
apt-get $apt_opts install \
aptitude \
bash \
build-essential \
console-setup \
cython3 \
dbus \
debconf-utils \
debsums \
devscripts \
dh-python \
ethtool \
fdisk \
firmware-atheros \
firmware-brcm80211 \
firmware-libertas \
firmware-linux \
firmware-linux-free \
firmware-linux-nonfree \
firmware-misc-nonfree \
firmware-realtek \
git \
htop \
i2c-tools \
irqbalance \
iw \
locales \
nano \
ntp \
openssh-server \
parted \
python3 \
python3-cffi \
python3-dev \
python3-serial \
python3-setuptools \
python3-spidev \
rng-tools \
schedtool \
sudo \
systemd \
tmux \
vim \
wireless-tools \
wpasupplicant \
|| die "apt-get install failed"
info "Configuring locales..."
dpkg-reconfigure -u locales ||\
die "Failed to reconfigure locales"
info "Configuring console..."
sed -i -e 's|CHARMAP=.*|CHARMAP="UTF-8"|' \
-e 's|FONTFACE=.*|FONTFACE=""|' \
-e 's|FONTSIZE=.*|FONTSIZE=""|' \
/etc/default/console-setup ||\
die "Failed to edit /etc/default/console-setup"
info "Creating /etc/rc.local..."
do_install -o root -g root -m 755 \
/tmp/templates/rc.local \
/etc/
info "Creating users/groups..."
userdel -f pi
groupdel pi
rm -rf /home/pi
groupadd -g 1000 pi ||\
die "Failed to create group pi."
useradd -u 1000 -d /home/pi -m -g pi\
-G pi,lp,dialout,cdrom,floppy,audio,dip,src,video,plugdev,netdev,i2c\
-s /bin/bash\
pi ||\
die "Failed to create user pi."
printf 'raspberry\nraspberry\n' | passwd pi ||\
die "Failed to set 'pi' password."
info "Initializing home directory..."
do_install -d -o pi -g pi -m 755 /home/pi/.vim
do_install -o pi -g pi -m 644 \
/tmp/templates/vimrc \
/home/pi/.vim/
do_install -T -o pi -g pi -m 644 \
/tmp/templates/tmux.conf \
/home/pi/.tmux.conf
info "Installing Raspberry Pi OS packages..."
if [ $opt_bit -eq 32 ]; then
local kernel_pkgs="linux-image-rpi-v6 linux-image-rpi-v7 linux-image-rpi-v7l linux-image-rpi-v8:arm64"
else
local kernel_pkgs="linux-image-rpi-v8"
fi
apt-get $apt_opts install \
$kernel_pkgs \
libraspberrypi-dev \
libraspberrypi-doc \
python3-rpi.gpio \
raspberrypi-net-mods \
raspberrypi-sys-mods \
raspi-config \
raspi-firmware \
raspi-gpio \
raspi-utils \
rpi-eeprom \
rpi-update \
|| die "apt-get install failed"
info "Removing unnecessary keys and repos..."
for file in /etc/apt/trusted.gpg.d/microsoft.gpg \
/etc/apt/sources.list.d/vscode.list; do
info "Replacing $file with dummy..."
if [ -e "$file" ]; then
rm "$file" || die "Failed to rm $file"
fi
touch "$file" || die "Failed to touch $file"
chmod 444 "$file" || die "Failed to chmod 444 $file"
done
for file in /etc/apt/*.gpg~; do
if [ -e "$file" ]; then
info "Removing $file..."
rm "$file" || die "Failed to rm $file"
fi
done
apt-get $apt_opts update ||\
die "apt-get update failed"
info "Running debconf-set-selections..."
cat /tmp/templates/debconf-set-selections-postinstall.conf | debconf-set-selections ||\
die "Failed to configure debconf settings"
info "Cleaning apt..."
apt-get $apt_opts autoremove --purge ||\
die "apt-get autoremove failed"
apt-get $apt_opts clean ||\
die "apt-get clean failed"
info "Disabling some services..."
do_systemctl mask apt-daily.service
do_systemctl mask apt-daily.timer
do_systemctl mask apt-daily-upgrade.timer
do_systemctl mask rsync.service
do_systemctl mask exim4.service
do_systemctl mask triggerhappy.service
do_systemctl mask triggerhappy.socket
do_systemctl mask alsa-state.service
do_systemctl mask alsa-restore.service
do_systemctl mask alsa-utils.service
info "Building and installing PiLC system package..."
(
cd /tmp/deb/pilc-system || die "Failed to cd to pilc-system"
debuild -uc -us -b -d || die "debuild failed"
dpkg -i ../pilc-system_*.deb || die "Failed to install pilc-system"
# Copy debs
rm -rf /home/pi/deb/pilc-system
do_install -d -o pi -g pi -m 755 /home/pi/deb/pilc-system
do_install -o pi -g pi -m 644 \
../*pilc-system*.deb \
/home/pi/deb/pilc-system/
) || die
info "Updating /etc/hosts..."
if ! grep -qe pilc /etc/hosts; then
printf '\n127.0.0.1\tpilc\n' >> /etc/hosts ||\
die "Failed to update /etc/hosts"
fi
info "Building Python modules..."
build_ppl
build_ppl2
info "Building awlsim..."
(
cd /tmp/awlsim/src || die "Failed to cd"
if [ $opt_cython -eq 0 ]; then
# Disable cython
sed -i -e '/Package: cython/,/^$/ d' \
debian/control ||\
die "Failed to patch control file (cython)"
sed -i -e 's/export AWLSIM_CYTHON_BUILD=1/export AWLSIM_CYTHON_BUILD=0/' \
debian/rules ||\
die "Failed to patch rules file (cython)"
fi
# Update the systemd service file.
sed -i -e 's|AWLSIM_SCHED=|AWLSIM_SCHED=realtime-if-multicore|g' \
-e 's|AWLSIM_PRIO=|AWLSIM_PRIO=50|g' \
-e 's|AWLSIM_AFFINITY=|AWLSIM_AFFINITY=-1,-2,-3|g' \
-e 's|AWLSIM_MLOCK=|AWLSIM_MLOCK=1|g' \
-e 's|Nice=.*$|Nice=-5|g' \
awlsim-server.service ||\
die "Failed to patch awlsim-server.service"
# Build the packages.
debuild -uc -us -b -d || die "debuild failed"
info "Installing awlsim..."
# Core
dpkg -i ../python3-awlsim_*.deb ||\
die "Failed to install python3-awlsim"
if [ $opt_cython -ne 0 ]; then
dpkg -i ../cython3-awlsim_*.deb ||\
die "Failed to install cython3-awlsim"
fi
# hardware: dummy
dpkg -i ../python3-awlsimhw-dummy_*.deb ||\
die "Failed to install python3-awlsimhw-dummy"
if [ $opt_cython -ne 0 ]; then
dpkg -i ../cython3-awlsimhw-dummy_*.deb ||\
die "Failed to install cython3-awlsimhw-dummy"
fi
# hardware: linuxcnc
dpkg -i ../python3-awlsimhw-linuxcnc_*.deb ||\
die "Failed to install python3-awlsimhw-linuxcnc"
if [ $opt_cython -ne 0 ]; then
dpkg -i ../cython3-awlsimhw-linuxcnc_*.deb ||\
die "Failed to install cython3-awlsimhw-linuxcnc"
fi
# hardware: profibus
dpkg -i ../python3-awlsimhw-profibus_*.deb ||\
die "Failed to install python3-awlsimhw-profibus"
if [ $opt_cython -ne 0 ]; then
dpkg -i ../cython3-awlsimhw-profibus_*.deb ||\
die "Failed to install cython3-awlsimhw-profibus"
fi
# hardware: RPi GPIO
dpkg -i ../python3-awlsimhw-rpigpio_*.deb ||\
die "Failed to install python3-awlsimhw-rpigpio"
if [ $opt_cython -ne 0 ]; then
dpkg -i ../cython3-awlsimhw-rpigpio_*.deb ||\
die "Failed to install cython3-awlsimhw-rpigpio"
fi
# hardware: PiXtend
dpkg -i ../python3-awlsimhw-pixtend_*.deb ||\
die "Failed to install python3-awlsimhw-pixtend"
if [ $opt_cython -ne 0 ]; then
dpkg -i ../cython3-awlsimhw-pixtend_*.deb ||\
die "Failed to install cython3-awlsimhw-pixtend"
fi
# Executables
dpkg -i ../awlsim-server_*.deb ||\
die "Failed to install awlsim-server"
dpkg -i ../awlsim-client_*.deb ||\
die "Failed to install awlsim-client"
dpkg -i ../awlsim-symtab_*.deb ||\
die "Failed to install awlsim-symtab"
dpkg -i ../awlsim-test_*.deb ||\
die "Failed to install awlsim-test"
dpkg -i ../awlsim-proupgrade_*.deb ||\
die "Failed to install awlsim-proupgrade"
# Copy debs
rm -rf /home/pi/deb/awlsim
do_install -d -o pi -g pi -m 755 /home/pi/deb/awlsim
do_install -o pi -g pi -m 644 \
../*awlsim*.deb \
/home/pi/deb/awlsim/
# Copy examples
do_install -T -o pi -g pi -m 644 \
examples/EXAMPLE.awlpro \
/home/pi/generic-example.awlpro
do_install -T -o pi -g pi -m 644 \
examples/raspberrypi-gpio.awlpro \
/home/pi/raspberrypi-gpio-example.awlpro
do_install -T -o pi -g pi -m 644 \
examples/raspberrypi-profibus.awlpro \
/home/pi/raspberrypi-profibus-example.awlpro
do_install -T -o pi -g pi -m 644 \
examples/raspberrypi-pixtend.awlpro \
/home/pi/raspberrypi-pixtend-example.awlpro
) || die
info "Building pyprofibus..."
(
cd /tmp/awlsim/pyprofibus ||\
die "Failed to cd"
if [ $opt_cython -eq 0 ]; then
# Disable cython
sed -i -e '/Package: cython/,/^$/ d' \
debian/control ||\
die "Failed to patch control file (cython)"
sed -i -e 's/export PYPROFIBUS_CYTHON_BUILD=1/export PYPROFIBUS_CYTHON_BUILD=0/' \
debian/rules ||\
die "Failed to patch rules file (cython)"
fi
# Build the packages.
debuild -uc -us -b -d || die "debuild failed"
info "Installing pyprofibus..."
dpkg -i ../python3-pyprofibus_*.deb ||\
die "Failed to install python3-pyprofibus"
dpkg -i ../profisniff_*.deb ||\
die "Failed to install profisniff"
dpkg -i ../gsdparser_*.deb ||\
die "Failed to install gsdparser"
# Copy debs
rm -rf /home/pi/deb/pyprofibus
do_install -d -o pi -g pi -m 755 /home/pi/deb/pyprofibus
do_install -o pi -g pi -m 644 \
../*pyprofibus*.deb \
../profisniff_*.deb \
../gsdparser_*.deb \
/home/pi/deb/pyprofibus/
) || die
rm -r /tmp/awlsim ||\
die "Failed to remove awlsim checkout."
info "Updating home directory permissions..."
chown -R pi:pi /home/pi || die "Failed to change /home/pi permissions."
# Remove rc.d policy file
if [ -e /usr/sbin/policy-rc.d ]; then
rm /usr/sbin/policy-rc.d ||\
die "Failed to remove policy-rc.d"
fi
if [ $opt_bit -eq 32 ]; then
# Install this last. It won't work correctly in the qemu environment.
info "Installing raspi-copies-and-fills..."
apt-get $apt_opts install --reinstall raspi-copies-and-fills ||\
die "apt-get install failed"
fi
info "Stopping processes..."
for i in dbus ssh irqbalance; do
/etc/init.d/$i stop
done
}
pilc_bootstrap_third_stage()
{
info "Running third stage..."
info "Umounting /dev/shm..."
umount -l "$mp_shm" || die "Failed to umount /dev/shm"
info "Umounting /sys..."
umount -l "$mp_sys" || die "Failed to umount /sys"
info "Umounting /proc/sys/fs/binfmt_misc..."
umount -l "$mp_proc_binfmt_misc"
info "Umounting /proc..."
umount -l "$mp_proc" || die "Failed to umount /proc"
info "Removing PiLC bootstrap script..."
rm "$opt_target_dir/pilc-bootstrap.sh" ||\
die "Failed to remove bootstrap script."
info "Cleaning tmp..."
rm -rf "$opt_target_dir"/tmp/*
info "Configuring /boot/firmware..."
do_install -T -o root -g root -m 644 \
"$basedir/templates/boot_cmdline.txt" \
"$opt_target_dir/boot/firmware/cmdline.txt"
do_install -T -o root -g root -m 644 \
"$basedir/templates/boot_config.txt" \
"$opt_target_dir/boot/firmware/config.txt"
ln -sf firmware/cmdline.txt "$opt_target_dir/boot/cmdline.txt" ||\
die "Failed to create cmdline.txt link."
ln -sf firmware/config.txt "$opt_target_dir/boot/config.txt" ||\
die "Failed to create config.txt link."
if [ $opt_bit -eq 64 ]; then
sed -i -e 's/arm_64bit=0/arm_64bit=1/g' \
"$opt_target_dir/boot/firmware/config.txt" ||\
die "Failed to set arm_64bit=1"
fi
# Prepare image paths.
local imgfile="${opt_target_dir}${opt_imgsuffix}.img"
local imgfile_zip="${imgfile}.7z"
local firmwareimgfile="${imgfile}.firmware"
mp_firmwareimgfile="${firmwareimgfile}.mp"
local rootimgfile="${imgfile}.root"
mp_rootimgfile="${rootimgfile}.mp"
rm -f "$imgfile" "$imgfile_zip" "$rootimgfile" "$firmwareimgfile"
rmdir "$mp_firmwareimgfile" "$mp_rootimgfile" 2>/dev/null
# Create images.
if [ "$opt_img" -ne 0 ]; then
# Calculate image size.
local imgsize_b="$(expr "$opt_imgsize" \* 1000 \* 1000 \* 1000)"
local imgsize_mib="$(expr "$imgsize_b" \/ 1024 \/ 1024)"
# Reduce the size to make sure it fits every SD card.
local imgsize_mib_red="$(expr \( "$imgsize_mib" \* 98 \) \/ 100)"
[ -n "$imgsize_mib_red" ] || die "Failed to calculate image size"
info "SD image size = $imgsize_mib_red MiB"
info "Creating /boot/firmware image..."
mkfs.vfat -F 32 -i 7771B0BB -n boot -C "$firmwareimgfile" \
$(expr \( 256 \* 1024 \) ) ||\
die "Failed to create /boot/firmware partition file system."
mkdir "$mp_firmwareimgfile" ||\
die "Failed to make /boot/firmware partition mount point."
mount -o loop "$firmwareimgfile" "$mp_firmwareimgfile" ||\
die "Failed to mount /boot/firmware partition."
rsync -rt --inplace \
"$opt_target_dir/boot/firmware/" "$mp_firmwareimgfile/" ||\
die "Failed to copy /boot/firmware files."
umount "$mp_firmwareimgfile" ||\
die "Failed to umount /boot/firmware partition."
rmdir "$mp_firmwareimgfile" ||\
die "Failed to remove /boot/firmware partition mount point."
info "Creating root image..."
mkfs.ext4 -O ^metadata_csum_seed \
"$rootimgfile" \
$(expr \( "$imgsize_mib_red" - \( 256 + 4 + 4 \) \) \* 1024 ) ||\
die "Failed to create root filesystem."
mkdir "$mp_rootimgfile" ||\
die "Failed to make root partition mount point."
mount -o loop "$rootimgfile" "$mp_rootimgfile" ||\
die "Failed to mount root partition."
rsync -aHAX --inplace \
--exclude='boot/firmware/*' \
--exclude='dev/shm/*' \
--exclude='proc/*' \
--exclude='run/*' \
--exclude='sys/*' \
--exclude='tmp/*' \
--exclude="$(basename "$opt_qemu")" \
"$opt_target_dir/" "$mp_rootimgfile/" ||\
die "Failed to copy root files."
umount "$mp_rootimgfile" ||\
die "Failed to umount root partition."
rmdir "$mp_rootimgfile" ||\
die "Failed to remove root partition mount point."
info "Creating image '$imgfile'..."
dd if=/dev/zero of="$imgfile" bs=1M count="$imgsize_mib_red" conv=sparse ||\
die "Failed to create image file."
parted "$imgfile" <<EOF
unit b
mklabel msdos
mkpart primary fat32 $(expr 4 \* 1024 \* 1024) $(expr \( 256 + 4 \) \* 1024 \* 1024)
mkpart primary ext4 $(expr \( 256 + 4 + 4 \) \* 1024 \* 1024) 100%
EOF
[ $? -eq 0 ] || die "Failed to create partitions."
info "Integrating /boot/firmware image..."
dd if="$firmwareimgfile" of="$imgfile"\
seek=4 bs=1M conv=notrunc,sparse ||\
die "Failed to integrate /boot/firmware partition."
rm "$firmwareimgfile" ||\
die "Failed to delete /boot/firmware partition image."
info "Integrating root image..."
dd if="$rootimgfile" of="$imgfile"\
seek="$(expr 256 + 4 + 4)" bs=1M conv=notrunc,sparse ||\
die "Failed to integrate root partition."
rm "$rootimgfile" ||\
die "Failed to delete root partition image."
# Create zipped image.
if [ "$opt_zimg" -ne 0 ]; then
info "Compressing image..."
"$SEVENZIP" -mx=9 a "$imgfile_zip" "$imgfile" ||\
die "Failed to compress partition image."
fi
# Write the image to the SD card.
if [ -n "$opt_writedev" ]; then
write_image "$imgfile" "$opt_writedev"
fi
fi
}
usage()
{
echo "pilc-bootstrap.sh [OPTIONS] TARGET_DIR"
echo
echo "Options:"
echo
echo " --branch|-b BRANCH Select the awlsim branch or tag."
echo " Default: $default_branch"
echo
echo " --no-cython|-C Do not build Cython modules."