-
Notifications
You must be signed in to change notification settings - Fork 7
/
sbts_install_aru.sh
executable file
·622 lines (481 loc) · 14.8 KB
/
sbts_install_aru.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
#!/bin/bash
# Copyright (c) 2023 Kim Hendrikse
export LC_ALL=C
UPDATED=
abort() {
echo $* >&2
echo "Aborting..."
exit 1
}
HERE=$(dirname $0)
cd $HERE || abort "Can't change to script directory"
HERE=`/bin/pwd`
get_non_blank() {
local prompt=$1
local thevariable=$2
local identifier=$3
local thestring=
echo "$prompt" >&2
while [ 1 ] ; do
echo -n "${thevariable}: " >&2
read thestring
if [[ $thestring =~ ^.*[[:blank:]].* || -z "$thestring" ]] ; then
echo ""
echo "You entered blanks" >&2
continue
fi
if [ "$identifier" == "username" ] ; then
if ! [[ $thestring =~ ^[[:alpha:]]([[:alnum:]]|_)*$ ]] ; then
echo "" >&2
echo "The $thevariable should being with an alpha and continue with aphanumerics or underscore" >&2
continue
fi
fi
if [ "$identifier" == "password" ] ; then
if [[ $thestring =~ \& ]] ; then
echo "" >&2
echo "The $thevariable should not contain an ampersand character" >&2
continue
fi
if [[ $thestring =~ \$ ]] ; then
echo "" >&2
echo "The $thevariable should not contain an dollar character" >&2
continue
fi
if [[ $thestring =~ [{}] ]] ; then
echo "" >&2
echo "The $thevariable should not contain an braces" >&2
continue
fi
if [[ $thestring =~ % ]] ; then
echo "" >&2
echo "The $thevariable should not contain a percent character" >&2
continue
fi
if [[ $thestring =~ \" ]] ; then
echo "" >&2
echo "The $thevariable should not contain a quote character" >&2
continue
fi
fi
break
done
echo "$thestring"
}
ask_about_gps() {
echo ""
while [ 1 ] ; do
is_adafruit=$(get_non_blank "Is your GPS an adafruit gps or ublox/other? (Y/N)" "Is Adafruit" "is_adafruit")
case "$is_adafruit" in
y|Y|n|N)
;;
*) echo "Please enter (Y/N)"
echo ""
continue
;;
esac
is_adafruit=$(echo $is_adafruit | tr 'YN' 'yn')
break
done
echo ""
}
set_hostname() {
echo ""
echo "Set the hostname"
echo ""
my_hostname=$(get_non_blank "What hostname would you like? The hostname is used in the name of the audio files, e.g. audio-sbts1" "Hostname" "my_hostname")
echo "$my_hostname" > /etc/hostname
perl -pi -e "s%raspberrypi%$my_hostname%" /etc/hosts
hostname "$my_hostname"
echo ""
}
update_pkg_registry() {
if [ ! "$UPDATED" ] ; then
echo Updating the package registry
echo ""
apt update
apt upgrade -y
UPDATED=1
fi
}
install_package() {
package=$1
echo "Installing package \"$package\""
echo ""
if ! apt install -y "$package" ; then
abort "Can't install package $package"
fi
}
prep_pip_installation() {
echo ""
echo "Prepare pip installation"
echo ""
apt install -y python3-pip
if ! dpkg -l "python3-pip" > /dev/null 2>&1 ; then
echo "Installing \"python3-pip\""
install_package "python3-pip"
fi
}
install_packages() {
echo ""
echo "Installing packages"
echo ""
for package in jackd2 libjack-jackd2-dev libsndfile1-dev pps-tools gpsd jq git i2c-tools git python3-numpy bc ffmpeg sysvbanner ; do
if ! dpkg -l "$package" > /dev/null 2>&1 ; then
echo "Installing package \"$package\""
install_package "$package"
fi
done
apt install -y python3-gps || apt install -y python-gps
install_package gpsd-clients
apt install -y chrony
}
enable_rt() {
echo ""
echo "Enabling real-time scheduling for user $SUDO_USER"
echo ""
sudo usermod -a -G audio "$SUDO_USER" || abort "Can't enable real-time schedule for user $SUDO_USER"
}
install_module() {
module=$1
if ! python3 -c "import $module" ; then
echo "Installing python module \"$module\""
if ! python3 -m pip install "$module" ; then
abort "Can't install pip3 module \"$module\""
fi
fi
}
install_python_modules() {
echo ""
echo "Installing python modules"
echo ""
echo "Installing python3-venv"
apt install -y python3-venv
cd "$SUDO_USER_HOME" || abort "Can't change to HOME directory"
sudo -H -u "$SUDO_USER" mkdir virtualenvs || abort "Can't create virtualenvs directory in HOME dir"
sudo -H -u "$SUDO_USER" python3 -m venv virtualenvs/sbts || abort "Can't create virtual env virtualenvs/sbts"
sudo -H -u "$SUDO_USER" /bin/bash -c "$(cat <<'EOF'
cd "$SUDO_USER_HOME" || exit 1
. ./virtualenvs/sbts/bin/activate
echo "Upgrading pip"
python3 -m pip install --upgrade pip
echo Installing python modules
for m in smbus2 soundfile pydub; do
python3 -m pip install "$m"
done
# Doesn't work on Raspberry Pi zero, I'm guessing because of not enough memory. Assume 1GB is needed
if [ "$(awk '$1 == "MemTotal:" {print int($2 / 1024)}' /proc/meminfo)" -gt 1024 ] ; then
echo "Installing opensoundscape"
python3 -m pip install opensoundscape
fi
EOF
)" || abort "Can't install virtual envs"
}
# Need this export for some versions of numpy to work properly (Not core dump) on arm processors
update_bashrc() {
echo ""
echo "Updating user .bashrc"
echo ""
copy_to "$HERE/bashrc" "$SUDO_USER_HOME/.bashrc"
}
copy_to() {
sudo -H -u "$SUDO_USER" cp "$1" "$2" || abort "Can't copy $1 to $2"
}
make_executable() {
chmod +x "$1" || abort "Change set $1 executable"
}
update_etc_rc() {
echo ""
echo "Update /etc/rc.local"
echo ""
cd "$HERE" || abort "Can't change back to $HERE"
cp etc_rc_local /etc/rc.local
make_executable /etc/rc.local
}
make_readonly_and_reboot() {
if ! "${SUDO_USER_HOME}/sbts-bin/make_readonly.sh" ; then
abort "Can't set the system to boot into read-only mode"
fi
echo ""
echo "Successfully installed stalkedbythestate"
echo ""
echo "A reboot is now required to finish installation. After the reboot, the system will be running in read-only mode"
echo ""
echo "Rebooting in 10 seconds..."
sleep 10
reboot
}
initialize_sbts_bin() {
echo ""
echo "Initializing sbts-bin"
echo ""
if [ ! -d "$SUDO_USER_HOME/sbts-bin" ] ; then
echo mkdir "$SUDO_USER_HOME/sbts-bin"
sudo -H -u "$SUDO_USER" mkdir "$SUDO_USER_HOME/sbts-bin" || abort "Can't create $SUDO_USER_HOME/sbts-bin"
fi
if [ ! -d "$SUDO_USER_HOME/python" ] ; then
echo mkdir "$SUDO_USER_HOME/python"
sudo -H -u "$SUDO_USER" mkdir "$SUDO_USER_HOME/python" || abort "Can't create $SUDO_USER_HOME/python"
fi
cd "$HERE" || abort "Can't change back to $HERE"
./build.sh || abort "Can't build sbts-aru"
local i
# Copy code that doesn't need the user substituted into it
for i in sbts-aru create_partitions.sh get_min_files.sh clean.sh cleaner.sh get_location.sh ; do
echo "cp $i $SUDO_USER_HOME/sbts-bin"
copy_to "$i" "$SUDO_USER_HOME/sbts-bin"
echo "chmod +x $SUDO_USER_HOME/sbts-bin/$i"
make_executable "$SUDO_USER_HOME/sbts-bin/$i"
done
# Copy code that needs USER replaced with the real user
for i in diff_time.sh get_samples.sh get_temp.sh gps_event_time.sh localize_event.sh time_diffs.sh eventtime.sh; do
echo "cp $i $SUDO_USER_HOME/sbts-bin"
copy_to "$i" "$SUDO_USER_HOME/sbts-bin"
echo "chmod +x $SUDO_USER_HOME/sbts-bin/$i"
perl -pi -e "s%USER%$SUDO_USER%" "$SUDO_USER_HOME/sbts-bin/$i"
make_executable "$SUDO_USER_HOME/sbts-bin/$i"
done
# Copy python programs to $HOME/python so that they can be run in virtualenvs
for i in diff_time.py example_localization.py get_samples.py get_temp.py gps_event_time.py localize_event.py time_diffs.py eventtime.py; do
echo "cp $i $SUDO_USER_HOME/python"
copy_to "$i" "$SUDO_USER_HOME/python"
done
echo "cp localize_event.py $SUDO_USER_HOME/sbts-bin"
copy_to localize_event.py "$SUDO_USER_HOME/sbts-bin"
}
turn_off_unused_services() {
echo ""
echo "Disable unused services"
echo ""
local i
for i in avahi-daemon hciuart bluetooth ; do
echo systemctl disable "$i"
systemctl disable "$i"
echo systemctl stop "$i"
systemctl stop "$i"
done
perl -pi -e 's%AutoEnable=true%AutoEnable=false%' /etc/bluetooth/main.conf
}
fix_swap() {
echo ""
echo "Stop file system based swap"
echo ""
echo dphys-swapfile swapoff
dphys-swapfile swapoff
echo dphys-swapfile uninstall
dphys-swapfile uninstall
echo update-rc.d dphys-swapfile remove
update-rc.d dphys-swapfile remove
echo systemctl stop dphys-swapfile
systemctl stop dphys-swapfile
echo systemctl disable dphys-swapfile
systemctl disable dphys-swapfile
}
configure_gpsd() {
echo ""
echo "Configure gpsd"
echo ""
cat > /etc/default/gpsd <<EOF
DEVICES="/dev/ttyAMA0 /dev/pps0"
GPSD_OPTIONS="-n"
EOF
systemctl enable gpsd
}
install_overlayfs() {
echo ""
echo "Installing overlayFS SD card protection"
echo ""
cp "$HERE/resources/overlayRoot.sh" /sbin
make_executable "/sbin/overlayRoot.sh"
perl -pi -e 's%console=serial\d,115200 %%' /boot/cmdline.txt
cp /boot/cmdline.txt /boot/rw_cmdline.txt
cp /boot/cmdline.txt /boot/ro_cmdline.txt
if [ -f /boot/firmware/cmdline.txt ] ; then
perl -pi -e 's%console=serial\d,115200 %%' /boot/firmware/cmdline.txt
cp /boot/firmware/cmdline.txt /boot/firmware/rw_cmdline.txt
cp /boot/firmware/cmdline.txt /boot/firmware/ro_cmdline.txt
perl -pi -e 's%$% init=/sbin/overlayRoot.sh%' /boot/firmware/ro_cmdline.txt
fi
perl -pi -e 's%$% init=/sbin/overlayRoot.sh%' /boot/ro_cmdline.txt
}
tweak_startup_order() {
echo ""
echo "Fix the startup order of gpsd and chrony so that chrony can make use of shared memory"
echo ""
perl -i -e 'undef $/;my $l=<>; $l =~ s/\nAfter=chronyd.service\n/\012#After=chronyd.service\012Before=chronyd.service\012/s;print $l' /lib/systemd/system/gpsd.service
perl -i -e 'undef $/;my $l=<>; $l =~ s/\nAfter=network.target\n/\012After=gpsd.service\012/s;print $l' /lib/systemd/system/chrony.service
}
tweak_chrony_conf() {
echo ""
echo "Tweak /etc/chrony/chrony.conf"
echo ""
perl -pi -e 's%^pool %# pool %' /etc/chrony/chrony.conf
perl -pi -e 's%^rtcsync%#rtcsync%' /etc/chrony/chrony.conf
perl -i -e 'undef $/;my $l=<>;$l =~ s%\n(makestep.*?)\n%\012#$1\012makestep 0.001 100\012%s;print $l' /etc/chrony/chrony.conf
local OFFSET
if [ "$is_adafruit" == "y" ] ; then
if [ "$IS_PI5" ] ; then
OFFSET="0.600"
else
OFFSET="0.200"
fi
else
if [ "$IS_PI5" ] ; then
OFFSET="0.400"
else
OFFSET="0.100"
fi
fi
cat >> /etc/chrony/chrony.conf <<EOF
# Start with an offset of 0.100 for ublox devices and 0.200 for adafruit ultimate breakout
refclock SHM 0 delay 0.325 poll 2 refid NMEA offset $OFFSET noselect
refclock PPS /dev/pps0 lock NMEA poll 2 refid PPS prefer
log measurements statistics refclocks tracking
local stratum 1
EOF
}
tweak_config() {
echo ""
echo "Tweak /boot/config.txt"
echo ""
perl -pi -e 's%^dtparam=audio=on%#dtparam=audio=on%' /boot/config.txt
if [ "$IS_PI5" ] ; then
cat >> /boot/config.txt <<EOF
# sbts-aru extra's
dtoverlay=pps-gpio,gpiopin=18
dtoverlay=uart0
dtoverlay=disable-bt
#dtoverlay=disable-wifi
EOF
else
cat >> /boot/config.txt <<EOF
# sbts-aru extra's
dtoverlay=pps-gpio,gpiopin=18
enable_uart=1
dtoverlay=disable-bt
#dtoverlay=disable-wifi
EOF
fi
if [ -f /boot/firmware/config.txt ] ; then
if [ "$IS_PI5" ] ; then
cat >> /boot/firmware/config.txt <<EOF
# sbts-aru extra's
dtoverlay=pps-gpio,gpiopin=18
dtoverlay=uart0
dtoverlay=disable-bt
#dtoverlay=disable-wifi
EOF
else
cat >> /boot/firmware/config.txt <<EOF
# sbts-aru extra's
dtoverlay=pps-gpio,gpiopin=18
enable_uart=1
dtoverlay=disable-bt
#dtoverlay=disable-wifi
EOF
fi
cat > /etc/modprobe.d/blacklist-pps.conf <<EOF
blacklist pps_ldisc
EOF
fi
}
enable_ssh() {
echo ""
echo "Enable ssh"
echo ""
systemctl enable ssh.service
}
fix_etc_fstab() {
echo ""
echo "Fix /etc/fstab"
echo ""
local PARTUID
PARTUID="$(grep '^PARTUUID=.*-02' /etc/fstab|awk '{print $1}')"
echo "${PARTUID%2}3 swap swap defaults 0 1" >> /etc/fstab
}
make_readonly() {
echo ""
echo "Make readonly mounted root file system"
echo ""
cd "$HERE" || abort "Can't change back to $HERE"
copy_to "$HERE/resources/make_readonly.sh" "$SUDO_USER_HOME/sbts-bin"
make_executable "$SUDO_USER_HOME/sbts-bin/make_readonly.sh"
copy_to "$HERE/resources/make_readwrite.sh" "$SUDO_USER_HOME/sbts-bin"
make_executable "$SUDO_USER_HOME/sbts-bin/make_readwrite.sh"
#"$SUDO_USER_HOME/sbts-bin/make_readonly.sh"
}
enable_partitioning() {
echo ""
echo "Enable re-partitioning"
echo ""
"$SUDO_USER_HOME/sbts-bin/make_readwrite.sh"
#if [ ! "$BULLSEYE" ] ; then
if [ "" ] ; then
readlink /sbin/init > "$SUDO_USER_HOME/sbts-bin/init_location"
rm -f /sbin/init
ln -s "$SUDO_USER_HOME/sbts-bin/create_partitions.sh" /sbin/init
else
if [ -f /boot/firmware/cmdline.txt ] ; then
perl -pi -e 's%$% init='"$SUDO_USER_HOME/sbts-bin/create_partitions.sh"'%' /boot/firmware/cmdline.txt
fi
perl -pi -e 's%$% init='"$SUDO_USER_HOME/sbts-bin/create_partitions.sh"'%' /boot/cmdline.txt
fi
}
setup_partitioning() {
cd "$HERE" || abort "Can't change back to $HERE"
# 2GB space extra over and above what is installed and 4GB for swap
"$HERE"/create_fdisk_cmds.py 2 4 > "$HERE/partitions"
chown "$SUDO_USER:$SUDO_USER" "$HERE/partitions"
copy_to "$HERE/partitions" "$SUDO_USER_HOME/sbts-bin"
}
install_crontab() {
cd "$HERE" || abort "Can't change back to $HERE"
if ! crontab -u "$SUDO_USER" ./resources/crontab ; then
abort "Can't install the user crontab"
fi
}
#
# Main
#
if [ "$(id -n -u)" != "root" ] ; then
abort "You need to execute this script as root"
fi
if [ ! "$SUDO_USER" -o "$SUDO_USER" == "root" ] ; then
abort "Please execute this script simply as sudo $(basename $0)"
fi
SUDO_USER_HOME=$(getent passwd "$SUDO_USER" | cut -d: -f6)
if ! grep bullseye /etc/os-release > /dev/null ; then
export BULLSEYE=
else
export BULLSEYE=1
fi
MODEL=$(cat /proc/device-tree/model | tr '\0' '\n')
if [[ "$MODEL" =~ "Raspberry Pi 5" ]] ; then
IS_PI5=1
else
IS_PI5=
fi
ask_about_gps
set_hostname
update_pkg_registry
prep_pip_installation
install_packages
enable_rt
install_python_modules
update_bashrc
update_etc_rc
initialize_sbts_bin
turn_off_unused_services
fix_swap
configure_gpsd
tweak_startup_order
tweak_chrony_conf
tweak_config
enable_ssh
fix_etc_fstab
make_readonly
install_overlayfs
install_crontab
setup_partitioning
enable_partitioning
reboot