-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbluesignage-setup.sh
513 lines (436 loc) · 12.4 KB
/
bluesignage-setup.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
#!/bin/bash -eu
if [ "$BASH_SOURCE" != "$0" ]
then
echo "$BASH_SOURCE must be executed, not sourced"
return 1 # shouldn't use exit when sourced
fi
if [ "${FLOCKED:-}" != "$0" ]
then
if FLOCKED="$0" flock -en -E 99 "$0" "$0" "$@" || case "$?" in
99) echo already running
exit 99
;;
*) exit $?
;;
esac
then
# success
exit 0
fi
fi
HEADLESS_SETUP=${HEADLESS_SETUP:-false}
if [ "$HEADLESS_SETUP" = "false" -a -t 0 ]
then
# running in terminal in non-headless mode
if [ -f /boot/bluesignage_setup_variables.conf -o -f /root/bluesignage_setup_variables.conf ]
then
# headless setup variables are available
read -p "Read setup info from bluesignage_setup_variables.conf (yes/no/cancel)? " answer
case ${answer:0:1} in
y|Y )
HEADLESS_SETUP=true
;;
n|N )
;;
* )
exit
;;
esac
fi
fi
REPO=${REPO:-PiSignage}
BRANCH=${BRANCH:-master}
USE_LED_FOR_SETUP_PROGRESS=true
TESLAUSB_HOSTNAME=${BLUESIGNAGE_HOSTNAME:-bluesignage}
function setup_progress () {
local setup_logfile=/boot/bluesignage-headless-setup.log
if [ $HEADLESS_SETUP = "true" -a -w $setup_logfile ]
then
echo "$( date ) : $@" >> "$setup_logfile"
fi
echo "$@"
}
if ! [ $(id -u) = 0 ]
then
setup_progress "STOP: Run sudo -i."
exit 1
fi
function headless_setup_populate_variables () {
# Pull in the conf file variables to make avail to this script and subscripts
# If setup-teslausb is run from rc.local, the conf file will have been moved
# to /root by rc.local
if [ $HEADLESS_SETUP = "true" ]
then
if [ -e /boot/bluesignage_setup_variables.conf ]
then
setup_progress "reading config from /boot/bluesignage_setup_variables.conf"
source /boot/bluesignage_setup_variables.conf
elif [ -e /root/bluesignage_setup_variables.conf ]
then
setup_progress "reading config from /root/bluesignage_setup_variables.conf"
source /root/bluesignage_setup_variables.conf
else
setup_progress "couldn't find config file"
fi
fi
}
function headless_setup_mark_setup_success () {
if [ $HEADLESS_SETUP = "true" ]
then
if [ -e /boot/BLUESIGNAGE_SETUP_FAILED ]
then
rm /boot/BLUESIGNAGE_SETUP_FAILED
fi
rm -f /boot/BLUESIGNAGE_SETUP_STARTED
touch /boot/BLUESIGNAGE_SETUP_FINISHED
setup_progress "Main setup completed."
fi
}
function headless_setup_progress_flash () {
if [ $USE_LED_FOR_SETUP_PROGRESS = "true" ] && [ $HEADLESS_SETUP = "true" ]
then
/etc/stage_flash $1
fi
}
function setup_led_off () {
if [ $USE_LED_FOR_SETUP_PROGRESS = "true" ] && [ $HEADLESS_SETUP = "true" ]
then
echo "none" | sudo tee /sys/class/leds/led0/trigger > /dev/null
echo 1 | sudo tee /sys/class/leds/led0/brightness > /dev/null
fi
}
function setup_led_on () {
if [ $USE_LED_FOR_SETUP_PROGRESS = "true" ] && [ $HEADLESS_SETUP = "true" ]
then
echo 0 | sudo tee /sys/class/leds/led0/brightness > /dev/null
fi
}
function verify_configuration () {
get_script /tmp verify-configuration.sh setup/pi
/tmp/verify-configuration.sh
}
function curlwrapper () {
setup_progress "curl $@"
while ! curl --fail "$@"
do
setup_progress "'curl $@' failed, retrying" > /dev/null
sleep 3
done
}
function get_script () {
local local_path="$1"
local name="$2"
local remote_path="${3:-}"
curlwrapper -o "$local_path/$name" https://raw.githubusercontent.com/"$REPO"/bluesignage/"$BRANCH"/"$remote_path"/"$name"
chmod +x "$local_path/$name"
setup_progress "Downloaded $local_path/$name ..."
}
function get_ancillary_setup_scripts () {
get_script /tmp make-root-fs-readonly.sh setup/pi
get_script /tmp configure.sh setup/pi
}
function get_common_scripts () {
get_script /root/bin remountfs_rw run
get_script /root/bin mount_snapshot.sh run
get_script /root/bin release_snapshot.sh run
}
function fix_cmdline_txt_modules_load ()
{
setup_progress "Fixing the modules-load parameter in /boot/cmdline.txt..."
cp /boot/cmdline.txt ~
cat ~/cmdline.txt | sed 's/ modules-load=dwc2,g_ether/ modules-load=dwc2/' > /boot/cmdline.txt
rm ~/cmdline.txt
setup_progress "Fixed cmdline.txt."
}
BACKINGFILES_MOUNTPOINT=/backingfiles
MUTABLE_MOUNTPOINT=/mutable
function create_usb_drive_backing_files () {
if [ ! -e "$BACKINGFILES_MOUNTPOINT" ]
then
mkdir "$BACKINGFILES_MOUNTPOINT"
fi
if [ ! -e "$MUTABLE_MOUNTPOINT" ]
then
mkdir "$MUTABLE_MOUNTPOINT"
fi
/tmp/create-backingfiles-partition.sh "$BACKINGFILES_MOUNTPOINT" "$MUTABLE_MOUNTPOINT"
if ! findmnt --mountpoint $BACKINGFILES_MOUNTPOINT
then
setup_progress "Mounting the partition for the backing files..."
mount $BACKINGFILES_MOUNTPOINT
setup_progress "Mounted the partition for the backing files."
fi
if [ ! -e $BACKINGFILES_MOUNTPOINT/cam_disk.bin ]
then
setup_progress "Creating backing disk files."
/tmp/create-backingfiles.sh "$camsize" "$musicsize" "$BACKINGFILES_MOUNTPOINT"
fi
}
function configure_hostname () {
local old_host_name=$(hostname)
local new_host_name="$TESLAUSB_HOSTNAME"
# Set the specified hostname if it differs from the current name
if [ "$new_host_name" != "$old_host_name" ]
then
setup_progress "Configuring the hostname..."
sed -i -e "s/$old_host_name/$new_host_name/g" /etc/hosts
sed -i -e "s/$old_host_name/$new_host_name/g" /etc/hostname
while ! hostnamectl set-hostname $new_host_name
do
setup_progress "hostnamectl failed, retrying"
sleep 1
done
systemctl restart avahi-daemon
setup_progress "Configured hostname: $(hostname)"
fi
}
function make_root_fs_readonly () {
/tmp/make-root-fs-readonly.sh
}
function update_package_index () {
setup_progress "Updating package index files..."
apt-get update
}
function upgrade_packages () {
if [ "$UPGRADE_PACKAGES" = true ]
then
setup_progress "Upgrading installed packages..."
apt-get --assume-yes upgrade
else
setup_progress "Skipping package upgrade."
fi
}
function set_timezone () {
if [ ! -z ${timezone:+x} ]
then
if [ -f "/usr/share/zoneinfo/$timezone" ]
then
ln -sf "/usr/share/zoneinfo/$timezone" /etc/localtime
elif [ "$timezone" = "auto" ]
then
if curlwrapper -o /root/bin/tzupdate.py https://raw.githubusercontent.com/marcone/tzupdate/develop/tzupdate.py
then
apt-get -y --force-yes install python-requests
chmod +x /root/bin/tzupdate.py
if ! tzout=$(/root/bin/tzupdate.py 2>&1)
then
setup_progress "auto timezone failed: $tzout"
else
setup_progress "$tzout"
fi
fi
else
setup_progress "invalid timezone: $timezone"
fi
fi
}
function cmd_selfupdate {
get_script /tmp setup-teslausb setup/pi &> /dev/null
if cmp -s /tmp/setup-teslausb /root/bin/setup-teslausb
then
setup_progress "already up to date"
else
/root/bin/remountfs_rw > /dev/null
mv /tmp/setup-teslausb /root/bin/setup-teslausb
setup_progress "$0 updated"
fi
exit 0
}
function cmd_diagnose {
{
echo -e "====== summary ======"
if [ -e /root/teslausb_setup_variables.conf ]
then
echo "headless setup config in /root"
source /root/teslausb_setup_variables.conf
elif [ -e /boot/teslausb_setup_variables.conf ]
then
echo "headless setup config in /boot"
source /boot/teslausb_setup_variables.conf
else
echo "headless setup config not found"
fi
if [ "${ARCHIVE_SYSTEM:-none}" = "cifs" ]
then
if grep -q '/mnt/archive' /etc/fstab
then
echo "CIFS archiving selected"
else
echo "CIFS archiving selected, but archive not defined in fstab"
fi
else
echo "archive method: ${ARCHIVE_SYSTEM:-none}"
fi
if ! blkid -L backingfiles > /dev/null
then
echo "backingfiles partition does not exist"
fi
if [ ! -d /backingfiles ]
then
echo "backingfiles directory does not exist"
fi
if ! grep -q '/backingfiles' /etc/fstab
then
echo "backingfiles not in fstab"
fi
if [ ! -f /backingfiles/cam_disk.bin ]
then
echo "cam disk image does not exist"
fi
if ! grep -q '/backingfiles/cam_disk.bin' /etc/fstab
then
echo "cam disk image not in fstab"
fi
LUN0=/sys/devices/platform/soc/20980000.usb/gadget/lun0/file
LUN1=/sys/devices/platform/soc/20980000.usb/gadget/lun1/file
if [ -e $LUN0 ]
then
echo "lun0 connected, from file $(cat $LUN0)"
fi
if [ -e $LUN1 ]
then
echo "lun1 connected, from file $(cat $LUN1)"
fi
if ! blkid -L mutable > /dev/null
then
echo "mutable partition does not exist"
fi
if [ ! -d /mutable ]
then
echo "mutable directory does not exist"
fi
if ! grep -q '/mutable' /etc/fstab
then
echo "mutable not in fstab"
fi
if [ ! -e /boot/TESLAUSB_SETUP_FINISHED ]
then
echo 'setup did not finish'
fi
echo -e "====== disk ======"
parted /dev/mmcblk0 print
echo -e "====== network ======"
ifconfig
echo -e "====== fstab ======"
if [ -e /etc/fstab ]
then
cat /etc/fstab
else
echo "no fstab found"
fi
echo -e "====== initial setup boot log ======"
mkdir /tmp/root$$
mount --bind / /tmp/root$$
cat /tmp/root$$/var/log/boot.log
umount /tmp/root$$
rmdir /tmp/root$$
echo -e "====== setup log ======"
if [ -e /boot/teslausb-headless-setup.log ]
then
cat /boot/teslausb-headless-setup.log
else
echo "no setup log found"
fi
echo -e "====== archiveloop log ======"
if [ -e /mutable/archiveloop.log ]
then
cat /mutable/archiveloop.log
else
echo "no archiveloop log found"
fi
echo -e "====== system log ======"
if [ -x /bin/logread ]
then
/bin/logread
else
echo "logread not installed"
fi
echo -e "====== dmesg ======"
dmesg
echo -e "====== process list and uptime ======"
ps -eaf
echo "$(hostname) has been $(uptime -p). System time is $(date)"
echo -e "====== end of diagnostics ======"
} |
# clean up the output a bit
tr '\r' '\n' |
sed '/^ *$/d' |
grep -a -v '^Reading package lists' |
grep -a -v '^(Reading database' |
grep -a -v "^Adding 'diversion of" |
grep -a -v "^Removing 'diversion of" |
sed -E 's/\o033\[0;32m//' |
sed -E 's/\o033\[0m//'
}
export -f setup_progress
export HEADLESS_SETUP
headless_setup_populate_variables
INSTALL_DIR=${INSTALL_DIR:-/root/bin}
if [ "$INSTALL_DIR" != "/root/bin" ]
then
setup_progress "WARNING: 'INSTALL_DIR' setup variable no longer supported"
fi
if [ ! -z ${1:+x} ]
then
if typeset -f cmd_$1 > /dev/null; then
cmd_$1
exit 0
else
setup_progress "unknown command: $1"
exit 1
fi
fi
configure_hostname
HEAD_VERSION=$(curlwrapper -s "https://api.github.com/repos/$REPO/teslausb/git/refs/heads/$BRANCH" | grep '"sha":' | awk '{print $2}' | sed 's/"\|,//g')
BRANCHNAME="$BRANCH"
BRANCH="$HEAD_VERSION"
get_script /tmp setup-teslausb setup/pi &> /dev/null
if cmp -s /tmp/setup-teslausb /root/bin/setup-teslausb
then
setup_progress "$0 is up to date"
else
setup_progress "WARNING: $BRANCHNAME contains a different version of $0. It is recommended to run '$0 selfupdate' to update to that version"
fi
update_package_index
# If USE_LED_FOR_SETUP_PROGRESS = true.
setup_led_off
# set time zone so we get decent timestamps in the rest of the setup log
set_timezone
# Flash for stage 2 headless (verify requested configuration)
headless_setup_progress_flash 1
setup_progress "Verifying that the requested configuration is valid..."
verify_configuration
# Flash for Stage 3 headless (grab scripts)
headless_setup_progress_flash 2
setup_progress "Downloading additional setup scripts."
mkdir -p /root/bin
get_common_scripts
get_ancillary_setup_scripts
pushd ~
fix_cmdline_txt_modules_load
# Flash for stage 4 headless (Create backing files)
headless_setup_progress_flash 3
create_usb_drive_backing_files
# Flash for stage 5 headless (Mark success, FS readonly)
headless_setup_progress_flash 4
headless_setup_mark_setup_success
if [ "$CONFIGURE_ARCHIVING" = true ]
then
setup_progress "calling configure.sh"
/tmp/configure.sh
else
setup_progress "skipping configure.sh"
fi
if [ "$SAMBA_ENABLED" = "true" ]
then
export SAMBA_GUEST
get_script /root/bin smb-on.sh run
get_script /root/bin smb-off.sh run
get_script /tmp configure-samba.sh setup/pi
/tmp/configure-samba.sh
fi
make_root_fs_readonly
upgrade_packages
# If USE_LED_FOR_SETUP_PROGRESS = true.
setup_led_on
setup_progress "All done."