-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_latest_candle.sh
3142 lines (2324 loc) · 103 KB
/
create_latest_candle.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/bash
set +e # continue on errors
# CANDLE INSTALL AND UPDATE SCRIPT
# This script will turn a Raspberry Pi OS Lite installation into a Candle controller
# It can also update a Candle controller to the latest available version (with some limitations)
# By default it acts as an upgrade script (not doing a factory reset at the end, rebooting the system when done)
# If you want to use this script to directly create disk imagesrecovery, then you can use:
# export CREATE_DISK_IMAGE=yes
# Set the script to download the very latest available version. Risky. Enabling this creates the /boot/candle_cutting_edge.txt file
# export CUTTING_EDGE=yes
# Other parts of the script that can be skipped:
# export SKIP_PARTITIONS=yes
# export TINY_PARTITIONS=yes
# export SKIP_APT_INSTALL=yes
# export SKIP_APT_UPGRADE=yes
# export SKIP_PYTHON=yes
# export SKIP_RESPEAKER=yes
# export SKIP_BLUEALSA=yes
# export SKIP_CONTROLLER=yes
# export SKIP_DEBUG=yes
# export SKIP_REBOOT=yes
#SKIP_PARTITIONS=yes
#TINY_PARTITIONS=yes # used to create smaller partition images, which are used in the new system update process
#SKIP_APT_INSTALL=yes
#SKIP_APT_UPGRADE=yes
#SKIP_PYTHON=yes
#SKIP_RESPEAKER=yes
#SKIP_BLUEALSA=yes
#SKIP_CONTROLLER=yes
#SKIP_DEBUG=yes
#SKIP_REBOOT=yes
# SKIP_PARTITIONS=yes SKIP_APT_INSTALL=yes SKIP_APT_UPGRADE=yes SKIP_PYTHON=yes SKIP_RESPEAKER=yes SKIP_BLUEALSA=yes SKIP_CONTROLLER=yes SKIP_DEBUG=yes SKIP_REBOOT=yes
# The script can add the re-install option to every Apt install command. This feature may be removed in the future.
# export APT_REINSTALL=yes
# To skip the creation of the read-only mode:
# export SKIP_RO=yes
# If you also want this script to download all the installed packages' as .deb files, then set this environment variable:
# export DOWNLOAD_DEB=yes
# An indicator that the script is inside chroot. It doesn't do anything at the moment.
# export CHROOTED=yes
# Note to self
# Upgrade conflicts with:
# - installing ReSpeaker drivers
# - installing the new read only mode
# Check if script is being run as root
if [ "$EUID" -ne 0 ]; then
echo "Please run candle update script as root (use sudo)"
exit
fi
scriptname=$(basename "$0")
# Not used in this script yet, currently /home/pi is still hardcoded
if [ -d /home/pi ]; then
CANDLE_BASE="/home/pi"
else
CANDLE_BASE="$(pwd)"
fi
echo "" >> /boot/candle_log.txt
echo "Candle: starting update - $(date) - $scriptname" >> /dev/kmsg
echo "starting update - $(date) - $scriptname" >> /boot/candle_log.txt
if [ -f /proc/mounts ];
then
# Detect is read-only mode is active
if [ -n "$(grep "[[:space:]]ro[[:space:],]" /proc/mounts | grep ' /ro ')" ]; then
echo
echo "Detected read-only mode. Create /boot/candle_rw_once.txt, reboot, and then try again."
echo "Candle: detected read-only mode. Aborting." >> /dev/kmsg
echo "Candle: detected read-only mode. Aborting." >> /boot/candle_log.txt
# Show error image
if [ "$scriptname" = "bootup_actions.sh" ] || [ "$scriptname" = "bootup_actions_failed.sh" ] || [ "$scriptname" = "post_bootup_actions.sh" ] || [ "$scriptname" = "post_bootup_actions_failed.sh" ];
then
if [ -e "/bin/ply-image" ] && [ -e /dev/fb0 ] && [ -f /boot/error.png ]; then
/bin/ply-image /boot/error.png
#sleep 7200
fi
fi
exit 1
fi
fi
# Detect if old overlay system is active
if [ -f /boot/cmdline.txt ]; then
if grep -q "boot=overlay" /boot/cmdline.txt; then
echo
echo "Detected the OLD raspi-config read-only overlay. Disable it under raspi-config -> performance (and then reboot)"
echo "Candle: detected OLD read-only mode. Aborting." >> /dev/kmsg
echo "Candle: detected OLD read-only mode. Aborting." >> /boot/candle_log.txt
# Show error image
if [ "$scriptname" = "bootup_actions.sh" ] || [ "$scriptname" = "bootup_actions_failed.sh" ] || [ "$scriptname" = "post_bootup_actions.sh" ] || [ "$scriptname" = "post_bootup_actions_failed.sh" ];
then
if [ -e "/bin/ply-image" ] && [ -e /dev/fb0 ] && [ -f /boot/error.png ]; then
/bin/ply-image /boot/error.png
#sleep 7200
fi
fi
exit 1
fi
fi
echo
# Check if /boot is available
if [ ! -f /boot/cmdline.txt ]; then
echo "Candle: ERROR, missing cmdline.txt??" >> /dev/kmsg
echo "Candle: ERROR, missing cmdline.txt??" >> /boot/candle_log.txt
if [ "$scriptname" = "bootup_actions.sh" ] || [ "$scriptname" = "bootup_actions_failed.sh" ] || [ "$scriptname" = "post_bootup_actions.sh" ] || [ "$scriptname" = "post_bootup_actions_failed.sh" ];
then
if [ -e "/bin/ply-image" ] && [ -e /dev/fb0 ] && [ -f /boot/error.png ]; then
/bin/ply-image /boot/error.png
#sleep 7200
fi
fi
exit 1
fi
if [ "$CUTTING_EDGE" = no ] || [[ -z "${CUTTING_EDGE}" ]];
then
if [ -f /boot/candle_cutting_edge.txt ]; then
echo "/boot/candle_cutting_edge.txt exists"
else
echo "no environment indication to go cutting edge"
fi
else
echo "going cutting edge"
touch /boot/candle_cutting_edge.txt
fi
# OUTPUT SOME INFORMATION
BIT_TYPE=$(getconf LONG_BIT)
cd /home/pi
echo
echo "CREATING CANDLE"
echo
echo "DATE : $(date)"
echo "IP ADDRESS : $(hostname -I)"
echo "BITS : $BIT_TYPE"
echo "PATH : $PATH"
echo "USER : $(whoami)"
echo "SCRIPT NAME : $scriptname"
if [ -f /boot/candle_cutting_edge.txt ]; then
echo "CUTTING EDGE : yes"
echo "CUTTING EDGE : yes" >> /dev/kmsg
echo "CUTTING EDGE : yes" >> /boot/candle_log.txt
else
echo "CUTTING EDGE : no"
echo "CUTTING EDGE : no" >> /dev/kmsg
echo "CUTTING EDGE : no" >> /boot/candle_log.txt
fi
if [ "$CHROOTED" = no ] || [[ -z "${CHROOTED}" ]]; then
echo "CHROOT : Not in chroot"
echo "CHROOT : Not in chroot" >> /boot/candle_log.txt
else
echo "CHROOT : Inside chroot"
fi
if [[ -z "${APT_REINSTALL}" ]] || [ "$APT_REINSTALL" = no ] ; then
echo "APT REINSTALL: no"
echo "APT REINSTALL: no" >> /dev/kmsg
else
reinstall="--reinstall"
echo "APT REINSTALL: yes"
echo "APT REINSTALL: yes" >> /dev/kmsg
echo "APT REINSTALL: yes" >> /boot/candle_log.txt
echo
echo "reinstall flag: $reinstall"
fi
echo
echo
echo "Current version of Raspbery Pi OS:"
cat /etc/os-release
echo
echo
echo "/boot/cmdline.txt before:"
cat /boot/cmdline.txt
echo
# Wait for IP address for at most 30 seconds
echo "Waiting for IP address..." >> /dev/kmsg
for i in {1..30}
do
#echo "current IP: $(hostname -I)"
if [ "$(hostname -I)" = "" ]
then
echo "Candle: no network yet $i"
echo "no network yet $i"
sleep 1
else
echo "Candle: IP address detected: $(hostname -I)" >> /dev/kmsg
echo "Candle: IP address detected: $(hostname -I)" >> /boot/candle_log.txt
break
fi
done
# ADDITIONAL SANITY CHECKS
if [ ! -s /etc/resolv.conf ]; then
# no nameserver
echo "no nameserver, aborting"
echo "Candle: no nameserver, aborting" >> /dev/kmsg
echo "Candle: no nameserver, aborting" >> /boot/candle_log.txt
exit 1
fi
# CREATE PARTITIONS
if [ ! -d /home/pi/.webthings/addons ] && [[ -z "${SKIP_PARTITIONS}" ]];
then
#if [ -f /dev/mmcblk0p4 ]; then
if lsblk | grep -q 'mmcblk0p3'; then
echo
echo "partitions already created:"
else
#if ls /dev/mmcblk0p2; then
#if [ -f /dev/mmcblk0p2 ]; then
if lsblk | grep -q 'mmcblk0p2'; then
echo
echo "CREATING PARTITIONS"
echo "Candle: creating partitions" >> /dev/kmsg
echo "Candle: creating partitions" >> /boot/candle_log.txt
echo
if [[ -z "${TINY_PARTITIONS}" ]]; then
printf "resizepart 2 7000MB\nmkpart\np\next4\n7001MB\n7500MB\nmkpart\np\next4\n7502MB\n14000MB\nquit" | parted
resize2fs /dev/mmcblk0p2
else
printf "resizepart 2 5000MB\nmkpart\np\next4\n5001MB\n7500MB\nmkpart\np\next4\n7502MB\n9500MB\nquit" | parted
resize2fs /dev/mmcblk0p2
fi
printf "y" | mkfs.ext4 /dev/mmcblk0p3
printf "y" | mkfs.ext4 /dev/mmcblk0p4
mkdir -p /home/pi/.webthings
chown pi:pi /home/pi/.webthings
touch /boot/candle_has_4th_partition.txt
e2label /dev/mmcblk0p2 system
e2label /dev/mmcblk0p3 recovery
e2label /dev/mmcblk0p4 user
else
echo
echo "Partition 2 was missing. Inside chroot?"
echo
fi
fi
sleep 5
if ls /dev/mmcblk0p4; then
echo "Mounting /dev/mmcblk0p4 to /home/pi/.webthings"
mount /dev/mmcblk0p4 /home/pi/.webthings
chown pi:pi /home/pi/.webthings
else
echo "Error, /dev/mmcblk0p4 was missing. Exiting."
exit
fi
lsblk
else
echo "Partitions seem to already exist (addons dir existed)"
echo "Candle: partitions seem to already exist" >> /dev/kmsg
echo "$(date) - starting create_latest_candle" >> /home/pi/.webthings/candle.log
echo "$(date) - starting create_latest_candle" >> /boot/candle_log.txt
fi
echo
# Clean up any files that may be left over to make sure there is enough space
if [ -f /zero.fill ]; then
rm /zero.fill
echo "Warning, removed /zero.fill"
fi
if [ -f /home/pi/.webthings/zero.fill ]; then
rm /home/pi/.webthings/zero.fill
echo "Warning, removed /home/pi/.webthings/zero.fill"
fi
if [ -d /home/pi/webthings/gateway2 ]; then
rm -rf /home/pi/webthings/gateway2
echo "Warning, removed /home/pi/webthings/gateway2"
fi
if [ -f /home/pi/latest_stable_controller.tar ]; then
rm /home/pi/latest_stable_controller.tar
echo "Warning, removed /home/pi/latest_stable_controller.tar"
fi
if [ -f /home/pi/latest_stable_controller.tar.txt ]; then
rm /home/pi/latest_stable_controller.tar.txt
echo "Warning, removed /home/pi/latest_stable_controller.tar.txt"
fi
# pre-made mountpoints for mounting user or recovery partition
#mkdir -p /mnt/userpart
#mkdir -p /mnt/recoverypart
#mkdir -p /mnt/ramdrive
# not compatible with /ro
sleep 3
cd /home/pi
# Save the bits of the initial kernel the boot partition to a file
if [ "$BIT_TYPE" == 64 ]; then
echo "creating /boot/candle_64bits.txt"
echo "creating /boot/candle_64bits.txt" >> /dev/kmsg
touch /boot/candle_64bits.txt
fi
# Make sure there is a current time
if [ -f /boot/candle_hardware_clock.txt ]; then
rm /boot/candle_hardware_clock.txt
systemctl restart systemd-timesyncd.service
timedatectl set-ntp true
timedatectl
sleep 2
/usr/sbin/fake-hwclock save
echo "Candle: requested latest time. Date is now: $(date)" >> /dev/kmsg
echo "Candle: requested latest time. Date is now: $(date)" >> /boot/candle_log.txt
fi
# Download error image first
if [ -f /boot/cmdline.txt ]; then
wget https://www.candlesmarthome.com/tools/error.png -O /boot/error.png
if [ ! -f /boot/error.png ]; then
echo "Candle: ERROR, download of error.png failed. How ironic." >> /dev/kmsg
echo "Candle: ERROR, download of error.png failed. How ironic." >> /boot/candle_log.txt
exit 1
fi
fi
# Quickly install Git if it hasn't been already
if [ -z "$(which git)" ]; then
echo
echo "installing git"
echo "Candle: installing git" >> /dev/kmsg
echo "Candle: installing git" >> /boot/candle_log.txt
echo
apt -y install git "$reinstall"
fi
# PLYMOUTH LITE
if [ ! -f /bin/ply-image ];
then
echo
echo "creating Plymouth lite (to show splash images)" >> /dev/kmsg
echo "creating Plymouth lite (to show splash images)" >> /boot/candle_log.txt
echo
git clone --depth 1 https://github.com/createcandle/Plymouth-lite.git
cd Plymouth-lite
./configure
make
cp ply-image /usr/bin
cd /home/pi
rm -rf Plymouth-lite
fi
echo
echo "updating /usr/bin/candle_hostname_fix.sh"
echo -e '#!/bin/bash\nhostname -F /home/pi/.webthings/etc/hostname\nhostnamectl set-hostname $(cat /home/pi/.webthings/etc/hostname) --static\nhostnamectl set-hostname $(cat /home/pi/.webthings/etc/hostname) --transient\nhostnamectl set-hostname $(cat /home/pi/.webthings/etc/hostname) --pretty' > /usr/bin/candle_hostname_fix.sh
chmod +x /usr/bin/candle_hostname_fix.sh
echo
# BULLSEYE SOURCES
# Make sure Bullseye sources are used
if cat /etc/apt/sources.list.d/raspi.list | grep -q buster; then
echo "changing /etc/apt/sources.list.d/raspi.list from buster to bullseye" >> /dev/kmsg
echo "changing /etc/apt/sources.list.d/raspi.list from buster to bullseye" >> /boot/candle_log.txt
sed -i 's/buster/bullseye/' /etc/apt/sources.list.d/raspi.list
fi
if cat /etc/apt/sources.list | grep -q buster; then
echo "changing /etc/apt/sources.list from buster to bullseye" >> /dev/kmsg
echo "changing /etc/apt/sources.list from buster to bullseye" >> /boot/candle_log.txt
sed -i 's/buster/bullseye/' /etc/apt/sources.list
fi
# Add option to download source code from RaspberryPi server
echo "modifying /etc/apt/sources.list - allowing apt access to source code"
sed -i 's/#deb-src/deb-src/' /etc/apt/sources.list
# Unhold browser
echo
apt-mark unhold chromium-browser
# 64 BIT
# If this Raspbery Pi OS is 64 bit, then install support for 32 bit as well.
if [ $BIT_TYPE -eq 64 ]; then
echo "Adding support for 32 bit architecture"
dpkg --add-architecture armhf
apt update -y && apt install -y screen:armhf
fi
echo
echo "doing apt-get update"
apt-get update -y
echo
# Upgrade Python to 3.11
if [ "$CUTTING_EDGE" = no ] || [[ -z "${CUTTING_EDGE}" ]];
then
echo ""
echo "Skipping Python upgrade"
else
if [ ! -e /usr/bin/python3.11 ]; then
echo "Upgrading Python to 3.11"
for pkg in build-essential zlib1g-dev libbz2-dev liblzma-dev libncurses5-dev libreadline6-dev libsqlite3-dev libssl-dev libgdbm-dev liblzma-dev tk8.5-dev lzma lzma-dev libgdbm-dev
do
apt -y install $pkg
done
wget https://www.python.org/ftp/python/3.11.1/Python-3.11.1.tar.xz -O python11.tar.xz --retry-connrefused --waitretry=5 --read-timeout=20 --timeout=15 -t 3
if [ ! -f python11.tar.xz ]; then
echo "Error, Python 11 failed to download. Aborting."
exit 1
else
tar -xvf python11.tar.xz
rm python11.tar.xz
echo "ls:"
ls
for directory in Python-*; do
[[ -d $directory ]] || continue
echo "Moving directory: $directory"
mv -f "$directory" ./python311
done
cd python311
./configure --enable-optimizations --prefix=/usr
make altinstall
cd ..
rm -rf python311
# Upgrade symlink for python3
if [ -e /usr/bin/python3.11 ]; then
cd /usr/bin/
echo "creating symlink python3 -> python 3.11"
ln -vfns python3.11 python3
cd -
else
echo "Error, /usr/bin/python3.11 binary is missing"
exit 1
fi
fi
else
echo "Python 11 seems to already be installed"
fi
# Install latest version of Pip
apt update
apt install python3-pip
# Re-install modules that come with Raspberry Pi OS by default
echo
echo "re-installing modules for Python 11"
for i in certifi chardet colorzero distro gpiozero idna numpy picamera2 pidng piexif Pillow python-apt python-prctl \
requests RPi.GPIO setuptools simplejpeg six spidev ssh-import-id toml urllib3 v4l2-python3 wheel; do
echo "$i"
yes | pip3 install "$i" --upgrade
echo
done
fi
echo
echo "Downloading read only script"
echo
if [ -f /boot/candle_cutting_edge.txt ]; then
echo "Candle: Downloading cutting edge read-only script" >> /dev/kmsg
echo "Candle: Downloading cutting edge read-only script" >> /boot/candle_log.txt
wget https://raw.githubusercontent.com/createcandle/ro-overlay/main/bin/ro-root.sh -O ./ro-root.sh --retry-connrefused
else
echo "Candle: Downloading stable read-only script" >> /dev/kmsg
echo "Candle: Downloading stable read-only script" >> /boot/candle_log.txt
curl -s https://api.github.com/repos/createcandle/ro-overlay/releases/latest \
| grep "tarball_url" \
| cut -d : -f 2,3 \
| tr -d \" \
| sed 's/,*$//' \
| wget -qi - -O ro-overlay.tar --retry-connrefused
if [ -f ro-overlay.tar ]; then
if [ -d ./ro-overlay ]; then
echo "WARNING. Somehow detected old ro-overlay folder. Removing it first."
rm -rf ./ro-overlay
fi
echo "unpacking ro-overlay.tar"
tar -xf ro-overlay.tar
rm ./ro-overlay.tar
for directory in createcandle-ro-overlay*; do
[[ -d $directory ]] || continue
echo "Directory: $directory"
mv -- "$directory" ./ro-overlay
done
if [ -d ./ro-overlay ]; then
echo "ro-overlay folder exists, OK"
cp ./ro-overlay/bin/ro-root.sh ./ro-root.sh
rm -rf ./ro-overlay
else
echo "ERROR, ro-overlay folder missing"
echo "Candle: WARNING, ro-overlay folder missing" >> /dev/kmsg
echo "Candle: WARNING, ro-overlay folder missing" >> /boot/candle_log.txt
fi
else
echo "Ro-root tar file missing, download failed"
echo "Candle: ERROR, stable read-only tar download failed" >> /dev/kmsg
echo "Candle: ERROR, stable read-only tar download failed" >> /boot/candle_log.txt
# Show error image
if [ "$scriptname" = "bootup_actions.sh" ] || [ "$scriptname" = "bootup_actions_failed.sh" ] || [ "$scriptname" = "post_bootup_actions.sh" ] || [ "$scriptname" = "post_bootup_actions_failed.sh" ];
then
if [ -e "/bin/ply-image" ] && [ -e /dev/fb0 ] && [ -f /boot/error.png ]; then
/bin/ply-image /boot/error.png
#sleep 7200
fi
fi
exit 1
fi
fi
# If the file exists, make it executable and move it into place
if [ -f ./ro-root.sh ]; then
if [ -n "$(lsblk | grep mmcblk0p3)" ] || [ -n "$(lsblk | grep mmcblk0p4)" ]; then
echo "Candle: ro-root.sh file downloaded OK" >> /dev/kmsg
echo "Candle: ro-root.sh file downloaded OK" >> /boot/candle_log.txt
chmod +x ./ro-root.sh
# Avoid risky move if possible
if ! diff -q ./ro-root.sh /bin/ro-root.sh &>/dev/null; then
echo "ro-root.sh file is different, moving it into place"
echo "Candle: ro-root.sh file is different, moving it into place" >> /dev/kmsg
echo "Candle: ro-root.sh file is different, moving it into place" >> /boot/candle_log.txt
if [ -f /bin/ro-root.sh ]; then
rm /bin/ro-root.sh
fi
mv -f ./ro-root.sh /bin/ro-root.sh
chmod +x /bin/ro-root.sh
else
echo "new ro-root.sh file is same as the old one, not moving it"
echo "Candle: downloaded ro-root.sh file is same as the old one, not moving it" >> /dev/kmsg
echo "Candle: downloaded ro-root.sh file is same as the old one, not moving it" >> /boot/candle_log.txt
fi
fi
else
echo "ERROR: failed to download ro-root.sh"
echo "Candle: ERROR, download of read-only overlay script failed" >> /dev/kmsg
echo "$(date) - download of read-only overlay script failed" >> /home/pi/.webthings/candle.log
echo "$(date) - download of read-only overlay script failed" >> /boot/candle_log.txt
# Show error image
if [ "$scriptname" = "bootup_actions.sh" ] || [ "$scriptname" = "bootup_actions_failed.sh" ] || [ "$scriptname" = "post_bootup_actions.sh" ] || [ "$scriptname" = "post_bootup_actions_failed.sh" ];
then
if [ -e "/bin/ply-image" ] && [ -e /dev/fb0 ] && [ -f /boot/error.png ]; then
/bin/ply-image /boot/error.png
#sleep 7200
fi
fi
exit 1
fi
# ENABLE READ_ONLY MODE
if [ "$SKIP_RO" = no ] || [[ -z "${SKIP_RO}" ]]; then
if [ -n "$(lsblk | grep mmcblk0p3)" ] || [ -n "$(lsblk | grep mmcblk0p4)" ]; then
if [ -f /bin/ro-root.sh ]; then
#isInFile4=$(cat /boot/config.txt | grep -c "ramfsaddr")
if [ $(cat /boot/config.txt | grep -c ramfsaddr) -eq 0 ];
then
echo
echo "ADDING READ-ONLY MODE"
echo
echo "Candle: adding read-only mode" >> /dev/kmsg
echo "Candle: adding read-only mode" >> /boot/candle_log.txt
mkinitramfs -o /boot/initrd
echo "- Adding read only mode to config.txt"
echo >> /boot/config.txt
echo '# Read only mode' >> /boot/config.txt
echo 'initramfs initrd followkernel' >> /boot/config.txt
echo 'ramfsfile=initrd' >> /boot/config.txt
echo 'ramfsaddr=-1' >> /boot/config.txt
else
echo "- Read only file system mode was already in config.txt"
echo "Candle: read-only mode already existed" >> /dev/kmsg
echo "Candle: read-only mode already existed" >> /boot/candle_log.txt
fi
if [ -f /boot/initrd ] && [ ! $(cat /boot/config.txt | grep -c "ramfsaddr") -eq 0 ];
then
#isInFile5=$(cat /boot/cmdline.txt | grep -c "init=/bin/ro-root.sh")
if [ $(cat /boot/cmdline.txt | grep -c "init=/bin/ro-root.sh") -eq 0 ]
then
echo "- Modifying cmdline.txt for read-only file system"
echo "- - before: $(cat /boot/cmdline.txt)"
sed -i ' 1 s|.*|& init=/bin/ro-root.sh|' /boot/cmdline.txt
echo "- - after : $(cat /boot/cmdline.txt)"
echo "Candle: read-only mode is now enabled" >> /dev/kmsg
echo "Candle: read-only mode is now enabled" >> /boot/candle_log.txt
else
echo "- The cmdline.txt file was already modified with the read-only filesystem init command"
echo "Candle: read-only mode was already enabled" >> /dev/kmsg
echo "Candle: read-only mode was already enabled" >> /boot/candle_log.txt
fi
fi
# Add RW and RO alias shortcuts to .profile
if [ -f /home/pi/.profile ]; then
if [ $(cat /home/pi/.profile | grep -c "alias rw=") -eq 0 ];
then
echo "adding ro and rw aliases to /home/pi/.profile"
echo "" >> /home/pi/.profile
echo "alias ro='sudo mount -o remount,ro /ro'" >> /home/pi/.profile
echo "alias rw='sudo mount -o remount,rw /ro'" >> /home/pi/.profile
fi
fi
else
echo "ERROR, /bin/ro-root.sh is missing, not even attempting to further install read-only mode"
fi
fi
fi
if [ "$SKIP_APT_UPGRADE" = no ] || [[ -z "${SKIP_APT_UPGRADE}" ]];
then
echo
echo "doing upgrade first"
apt update -y
apt-get update -y
apt --fix-broken install -y
# Check if kernel or bootloader can be updated
if apt list --upgradable | grep raspberrypi-bootloader; then
echo "WARNING, BOOTLOADER IS UPGRADEABLE"
echo "WARNING, BOOTLOADER IS UPGRADEABLE" >> /dev/kmsg
echo "WARNING, BOOTLOADER IS UPGRADEABLE" >> /boot/candle_log.txt
fi
if apt list --upgradable | grep raspberrypi-kernel; then
echo "WARNING, KERNEL IS UPGRADEABLE"
echo "WARNING, KERNEL IS UPGRADEABLE" >> /dev/kmsg
echo "WARNING, KERNEL IS UPGRADEABLE" >> /boot/candle_log.txt
fi
if [ -n "$(apt list --upgradable | grep raspberrypi-kernel)" ] || [ -n "$(apt list --upgradable | grep raspberrypi-bootloader)" ]; then
#apt install -y raspberrypi-kernel
#apt install -y raspberrypi-bootloader
echo "Candle: WARNING, DOING FULL UPGRADE. THIS WILL UPDATE THE KERNEL TOO. Takes a while!"
echo "Candle: WARNING, DOING FULL UPGRADE. THIS WILL UPDATE THE KERNEL TOO. Takes a while!" >> /dev/kmsg
echo "WARNING, DOING FULL UPGRADE. THIS WILL UPDATE THE KERNEL TOO." >> /boot/candle_log.txt
echo "no /boot/candle_first_run_complete.txt file yet, probably creating disk image"
# A little overkill:
apt-get update -y
DEBIAN_FRONTEND=noninteractive apt full-upgrade -y &
wait
apt-get --fix-missing
apt --fix-broken install -y
sed -i 's|/usr/lib/dhcpcd5/dhcpcd|/usr/sbin/dhcpcd|g' /etc/systemd/system/dhcpcd.service.d/wait.conf # Fix potential issue with dhcpdp on Bullseye
echo ""
if [ -d /opt/vc/lib ]; then
echo "removing left over /opt/vc/lib"
rm -rf /opt/vc/lib
fi
if [ -d /var/lib/dhcpcd5 ]; then
echo "removing left over dhcpcd5"
rm -rf /var/lib/dhcpcd5
fi
apt-get update -y
DEBIAN_FRONTEND=noninteractive apt full-upgrade -y &
wait
apt-get --fix-missing
apt --fix-broken install -y
apt autoremove -y
sed -i 's|/usr/lib/dhcpcd5/dhcpcd|/usr/sbin/dhcpcd|g' /etc/systemd/system/dhcpcd.service.d/wait.conf # Fix potential issue with dhcpdp on Bullseye
echo
echo
echo
echo
apt-get update -y
apt-get update --fix-missing -y
DEBIAN_FRONTEND=noninteractive apt upgrade -y &
wait
apt-get update --fix-missing -y
apt --fix-broken install -y
sed -i 's|/usr/lib/dhcpcd5/dhcpcd|/usr/sbin/dhcpcd|g' /etc/systemd/system/dhcpcd.service.d/wait.conf # Fix potential issue with dhcpdp on Bullseye
echo
echo
echo
echo
apt --fix-broken install -y
apt autoremove -y
#apt clean -y
echo
echo "Candle: Apt upgrade complete."
echo "Candle: Apt upgrade complete." >> /dev/kmsg
echo "Apt upgrade done" >> /boot/candle_log.txt
echo
apt-mark unhold chromium-browser
if chromium-browser --version | grep -q 'Chromium 88'; then
echo "Version 88 of ungoogled chromium detected. Removing..."
echo "Version 88 of ungoogled chromium detected. Removing..." >> /dev/kmsg
echo "Version 88 of ungoogled chromium detected. Removing..." >> /boot/candle_log.txt
apt-get purge chromium-browser -y --allow-change-held-packages
apt purge chromium-browser -y --allow-change-held-packages
apt purge chromium-codecs-ffmpeg-extra -y --allow-change-held-packages
apt autoremove -y --allow-change-held-packages
apt install chromium-browser -y --allow-change-held-packages
fi
if [ -f /boot/candle_original_version.txt ] || [ ! -f /boot/candle_first_run_complete.txt ]; then
echo
echo "rebooting"
echo "$(date) - rebooting" >> /boot/candle_log.txt
echo
# make sure the system is read-write and accessible again after the reboot.
touch /boot/candle_rw_once.txt
#touch /boot/ssh.txt
reboot
sleep 60
#echo "calling apt upgrade"
#echo "Candle: doing apt upgrade" >> /dev/kmsg
#echo "Candle: doing apt upgrade" >> /boot/candle_log.txt
#DEBIAN_FRONTEND=noninteractive apt-get upgrade -y &
#wait
#echo
#echo "Upgrade complete"
fi
fi
fi
# Download splash images
if [ -f /boot/cmdline.txt ]; then
wget https://www.candlesmarthome.com/tools/error.png -O /boot/error.png --retry-connrefused
echo
echo "Candle: downloading splash images and videos" >> /dev/kmsg
echo "Candle: downloading splash images and videos" >> /boot/candle_log.txt
echo
wget https://www.candlesmarthome.com/tools/splash_updating.png -O /boot/splash_updating.png --retry-connrefused
wget https://www.candlesmarthome.com/tools/splash_updating180.png -O /boot/splash_updating180.png --retry-connrefused
if [ "$scriptname" = "bootup_actions.sh" ] || [ "$scriptname" = "bootup_actions_failed.sh" ] || [ "$scriptname" = "post_bootup_actions.sh" ] || [ "$scriptname" = "post_bootup_actions_failed.sh" ];
then
if [ -e "/bin/ply-image" ] && [ -e /dev/fb0 ] && [ -f "/boot/splash_updating.png" ]; then
echo "Candle: showing updating splash image" >> /dev/kmsg
echo "Candle: showing updating splash image" >> /boot/candle_log.txt
if [ -f /boot/rotate180.txt ]; then
/bin/ply-image /boot/splash_updating180.png
if ps aux | grep -q /usr/bin/startx; then
DISPLAY=:0 feh --bg-fill /boot/splash_updating180.png
fi
else
/bin/ply-image /boot/splash_updating.png
if ps aux | grep -q /usr/bin/startx; then
DISPLAY=:0 feh --bg-fill /boot/splash_updating.png
fi
fi
fi
# Also start SSH
if [ -f /boot/developer.txt ] || [ -f /boot/candle_cutting_edge.txt ]; then
echo "Candle: starting ssh" >> /dev/kmsg
echo "Candle: starting ssh" >> /boot/candle_log.txt
systemctl start ssh.service
fi
fi
# Download the rest of the images
wget https://www.candlesmarthome.com/tools/splash.png -O /boot/splash.png --retry-connrefused
wget https://www.candlesmarthome.com/tools/splash180.png -O /boot/splash180.png --retry-connrefused
wget https://www.candlesmarthome.com/tools/splashalt.png -O /boot/splashalt.png --retry-connrefused
wget https://www.candlesmarthome.com/tools/splash180alt.png -O /boot/splash180alt.png --retry-connrefused
wget https://www.candlesmarthome.com/tools/splash.mp4 -O /boot/splash.mp4 --retry-connrefused
wget https://www.candlesmarthome.com/tools/splash180.mp4 -O /boot/splash180.mp4 --retry-connrefused
wget https://www.candlesmarthome.com/tools/splash_preparing.png -O /boot/splash_preparing.png --retry-connrefused
wget https://www.candlesmarthome.com/tools/splash_preparing180.png -O /boot/splash_preparing180.png --retry-connrefused
fi
echo
echo "PRE-DOWNLOADING CONFIGURATION FILES FROM GITHUB"
echo
# Remove old left-over configuration files if they exist
if [ -d /home/pi/configuration-files ]; then
echo "Warning, found a left over configuration-files directory"
rm -rf /home/pi/configuration-files
fi
# Download ready-made settings files from the Candle github
if [ -f /boot/candle_cutting_edge.txt ]; then
echo "Candle: Starting download of cutting edge configuration files"
echo "Candle: Starting download of cutting edge configuration files" >> /dev/kmsg
echo "Candle: Starting download of cutting edge configuration files" >> /boot/candle_log.txt
git clone --depth 1 https://github.com/createcandle/configuration-files /home/pi/configuration-files
if [ -d /home/pi/configuration-files ]; then
rm /home/pi/configuration-files/LICENSE
rm /home/pi/configuration-files/README.md
rm -rf /home/pi/configuration-files/.git
fi
else
echo "Candle: Starting download of stable configuration files"
echo "Candle: Starting download of stable configuration files" >> /dev/kmsg
echo "Candle: Starting download of stable configuration files" >> /boot/candle_log.txt
curl -s https://api.github.com/repos/createcandle/configuration-files/releases/latest \
| grep "tarball_url" \
| cut -d : -f 2,3 \
| tr -d \" \
| sed 's/,*$//' \
| wget -qi - -O configuration-files.tar --retry-connrefused
tar -xf configuration-files.tar
rm configuration-files.tar