-
Notifications
You must be signed in to change notification settings - Fork 1
/
live_system_update.sh
350 lines (258 loc) · 9.04 KB
/
live_system_update.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
#!/bin/bash
# LIVE UPDATE
# If the Candle controller uses the new overlay system, then this script might be able to update it "live", without needing a reboot or fully disabling the overlay.
echo
echo "CANDLE LIVE UPDATE"
echo "Candle: starting live update" >> /dev/kmsg
# Check if script is being run as root
if [ "$EUID" -ne 0 ]; then
echo "Please run as root (use sudo)"
echo "Candle: Could not continue (use sudo)" >> /dev/kmsg
exit
else
echo "running as user $(whoami)"
fi
if [ ! -d /ro ]; then
echo
echo "Candle: no overlay detected, aborting." >> /dev/kmsg
exit 1
fi
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: live update detected OLD read-only mode. Aborting." >> /dev/kmsg
echo "Candle: live update detected OLD read-only mode. Aborting." >> /boot/candle_log.txt
# Show error image
if [ -e "/bin/ply-image" ] && [ -e /dev/fb0 ] && [ -f "/boot/error.png" ]; then
/bin/ply-image /boot/error.png
sleep 7200
fi
exit 1
fi
fi
echo
cd /ro/home/pi || exit
echo
echo "Starting live system update"
echo "$(date) - starting live system update" >> /boot/candle_log.txt
echo "$(date) - starting live system update" >> /home/pi/.webthings/candle.log
echo
echo "/etc/resolv.conf: "
cat /etc/resolv.conf
echo
if [ ! -s /etc/resolv.conf ]; then
# no nameserver
echo "no nameserver, aborting"
echo "Candle: no nameserver, aborting" >> /dev/kmsg
exit 1
else
echo "/etc/resolv.conf did not seem to be empty:"
cat /etc/resolv.conf
echo
# enable internet use inside the chroot
#cp /etc/resolv.conf /ro/etc/resolv.conf
#echo "juggling /etc/resolv.conf"
#cp /etc/resolv.conf /ro/etc/resolv.conf.jump
#rm /rw/upper/etc/resolv.conf
#rm /etc/resolv.conf
#cp /ro/etc/resolv.conf.jump /ro/etc/resolv.conf
fi
# make sure there is a current time
if [ -f /boot/candle_hardware_clock.txt ];
then
echo "Candle: Detected hardware clock indicator. Trying to get latest time update from NTP server"
rm /boot/candle_hardware_clock.txt
systemctl restart systemd-timesyncd.service
timedatectl set-ntp true
sleep 2
/usr/sbin/fake-hwclock save
echo "Candle: attempted requested of latest time from internet." >> /dev/kmsg
else
echo "Candle: no hardware clock detected, assuming time is current" >> /dev/kmsg
fi
timedatectl set-ntp true
sleep 2
/usr/sbin/fake-hwclock save
echo "Candle: current date: $(date)" >> /dev/kmsg
echo
echo "Setting /ro to RW"
# This shouldn't even be possible really
mount -o remount,rw /ro
echo "remount done"
timedatectl set-ntp true
if [ -d /ro/home/pi/webthings ]; then
availMem=$(df -P "/dev/mmcblk0p2" | awk 'END{print $4}')
echo "Candle: calculated free system space: $availMem" >> /dev/kmsg
echo "calculating size of webthings folder"
fileSize=$(du -k --max-depth=0 "/ro/home/pi/webthings" | awk '{print $1}')
if [ "$fileSize" -gt "$availMem" ];
then
echo "WARNING: NOT ENOUGH DISK SPACE TO CREATE WEBTHINGS BACKUP. SKIPPING."
echo "Candle: WARNING: LOW DISK SPACE, NOT CREATING BACKUP FIRST" >> /dev/kmsg
else
echo "Creating fresh backup of webthings folder" >> /dev/kmsg
echo "Candle: creating fresh backup copy" >> /dev/kmsg
# cp -r /ro/home/pi/webthings /ro/home/pi/webthings-old
tar -czf /ro/home/pi//controller_backup_fresh.tar /ro/home/pi/webthings
chown pi:pi /ro/home/pi//controller_backup_fresh.tar
fi
else
echo "ERROR: /ro/home/pi/webthings does not exist??"
fi
#mkdir -p /ro/home/pi/tmp/boot
#cp -r --verbose /boot/* /ro/home/pi/tmp/boot
#mkdir -p /ro/home/pi/tmp/root
#cp -r --verbose /root/* /ro/home/pi/tmp/root
#cp -r --verbose /proc/device-tree /ro/home/pi/tmp/proc/device-tree
#cp -r --verbose /proc/mounts /ro/home/pi/tmp/proc/mounts
#mkdir -p /ro/home/pi/tmp/etc
#cp -r --verbose /ro/home/pi/.webthings/etc/hostname /ro/home/pi/tmp/etc/hostname
#cp -r --verbose /ro/home/pi/.webthings/etc/hosts /ro/home/pi/tmp/etc/hosts
echo "Candle: Downloading latest install script" >> /dev/kmsg
# TODO: change this to use the release, and only clone if developer mode active
wget https://raw.githubusercontent.com/createcandle/install-scripts/main/create_latest_candle.sh -O /ro/home/pi/create_latest_candle.sh
if [ -f /ro/home/pi/create_latest_candle.sh ]; then
echo "Candle: Download succesful" >> /dev/kmsg
chmod +x /ro/home/pi/create_latest_candle.sh
else
echo "Candle: ERROR: DOWNLOADING LATEST SCRIPT FAILED" >> /dev/kmsg
exit 1
fi
# sudo chroot /ro sh -c "whoami"
# sudo chroot /ro sh -c "ls /dev"
# sudo chroot /ro sh -c "ls /mnt"
# sudo chroot /ro sh -c "apt update"
# sudo chroot /ro sh -c "cat /proc/mounts"
# sudo chroot /ro sh -c "wget https://raw.githubusercontent.com/createcandle/install-scripts/main/create_latest_candle.sh"
# sudo chroot /ro sh -c "ls /home/pi/alfred"
# sudo chroot /ro sh -c "mount -t proc proc /proc && mount -t sysfs && ls /proc"
# sudo chroot /ro sh -c "mount -t procfs && mount -t sysfs && ls /proc"
#sudo chroot /ro ln -s /proc/mounts /etc/mtab
# mount -o bind /dir/outside/chroot /dir/inside/chroot
# sudo chroot /ro sh -c "ls /home/pi/alfred"
echo "bind-mounting /boot into chroot"
echo "Candle: preparing chroot mounts" >> /dev/kmsg
#if [ ! -f /boot/cmdline.txt ]; then
if ! findmnt | grep -q '/ro/boot'; then
echo "trying to mount /boot"
mount /boot /ro/boot -o bind
fi
if ! findmnt | grep -q '/ro/home/pi/.webthings'; then
echo "trying to mount .webthings"
mkdir -p /ro/home/pi/.webthings
mount /home/pi/.webthings /ro/home/pi/.webthings -o bind
fi
if ! findmnt | grep -q '/ro/dev'; then
echo "trying to mount /dev"
mkdir -p /ro/dev
mount /dev /ro/dev -o bind
#mount --rbind /dev dev/
mount -o remount,rw /ro/dev
fi
if ! findmnt | grep -q '/ro/lib'; then
echo "trying to mount /lib"
mkdir -p /ro/dev
mount /dev /ro/dev -o bind
#mount --rbind /dev dev/
mount -o remount,rw /ro/dev
fi
if ! findmnt | grep -q '/ro/etc/resolv.conf'; then
echo "trying to mount /etc/resolv.conf"
mkdir -p /ro/etc
mount /etc/resolv.conf /ro/etc/resolv.conf -o bind
fi
if ! findmnt | grep -q '/ro/home/pi/.webthings'; then
echo "trying to mount /ro/home/pi/.webthings"
mkdir -p /ro/home/pi/.webthings
mount /home/pi/.webthings /ro/home/pi/.webthings -o bind
fi
echo "starting chroot"
echo "Candle: starting actual update process" >> /dev/kmsg
chroot /ro sh -c "$(cat <<END
echo "Candle: live update in chroot"
whoami
echo "/etc/resolv.conf: $(cat /etc/resolv.conf)"
echo "cat /mnt/etc/resolv.conf: $(cat /mnt/etc/resolv.conf)"
cd /home/pi
export CHROOTED=yes
export STOP_EARLY=yes
export SKIP_PARTITIONS=yes
export SKIP_RO=yes
#if [ -d /home/pi/tmp/boot ]; then
#echo "creating bind mount over /boot"
#mount --bind /home/pi/tmp/boot /boot
#else
#echo "/home/pi/tmp/boot was missing"
#fi
#mount -t procfs
if [ ! -f /proc/partitions ]; then
echo "chroot: mounting proc"
mount -t proc proc /proc
fi
if [ ! -d /sys/kernel ]; then
echo "chroot: mounting sys"
mount -t sysfs sysfs /sys
fi
#if [ ! -d /dev/pts ]; then
#mount --rbind /dev dev/
#mount -o remount,rw /dev
#fi
#if [ ! -d /run/mount ]; then
#echo "chroot: mounting run"
#mount --rbind /run run/
#fi
if [ -f /home/pi/create_latest_candle.sh ]; then
echo "Install script found, starting it"
/home/pi/create_latest_candle.sh #> /home/pi/update_report.txt
else
echo "Error in chroot: create_latest_candle.sh not found"
fi
#touch /home/pi/TEST_FILE_CREATED_IN_CHROOT.txt
umount /run
umount /dev
umount /sys
umount /proc
umount /boot
END
)"
echo "Finalising outside of chroot"
echo "Candle: Finalising update" >> /dev/kmsg
sleep 5
echo "setting fkms driver"
sed -i 's/dtoverlay=vc4-kms-v3d/dtoverlay=vc4-fkms-v3d/' /boot/config.txt
# delete the temporary directory
#rm -rf /ro/home/pi/tmp
if [ -f /ro/home/pi/create_latest_candle.sh ]; then
rm /ro/home/pi/create_latest_candle.sh
else
echo "strange, the install script is gone"
fi
# does not make sense:
#if [ -d /ro/home/pi/configuration-files ]; then
# echo "removing /ro/home/pi/configuration-files"
#else
# echo "/ro/home/pi/configuration-files did not exist, so is not being deleted"
# git clone --depth 1 https://github.com/createcandle/configuration-files /ro/home/pi/configuration-files
#fi
#if [ -d /home/pi/configuration-files/boot ]; then
# cp --verbose -r /ro/home/pi/configuration-files/boot/* /boot/
# rm -rf /ro/home/pi/configuration-files
#else
# echo "ERROR: configuration files not downloaded?"
# echo "Candle: ERROR: could not update files in /boot" >> /dev/kmsg
#fi
echo "Candle: restoring disk to normal" >> /dev/kmsg
umount /ro/run
umount /ro/dev
umount /ro/sys
umount /ro/proc
umount /ro/boot
umount /ro/home/pi/,webthings
umount /ro/etc/resolv.conf
# re-enable read-only mode
echo "Setting /ro back to RO"
mount -l -o remount,ro /ro
echo "Candle: live controller update done" >> /dev/kmsg
reboot
exit 0