-
Notifications
You must be signed in to change notification settings - Fork 53
/
setup
executable file
·255 lines (227 loc) · 9.64 KB
/
setup
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
#!/bin/bash
set -uo pipefail
shopt -s extglob
trap 's=$?; echo "$0: Error on line "$LINENO": $BASH_COMMAND"; exit $s' ERR
IFS=$'\n\t'
# Original source for this script:
# https://github.com/kr15h/travis-raspbian-image
# https://disconnected.systems/blog/custom-rpi-image-with-github-travis/
pynab_repository=${GITHUB_REPOSITORY:-nabaztag2018/pynab}
pynab_branch=${GITHUB_BRANCH:-release}
case ${pynab_branch} in
v+([0-9]).+([0-9]).+([0-9])*)
# vX.Y.Z tag-driven build: assume release branch
pynab_tag=" (tag ${pynab_branch})"
pynab_branch=release
driver_branch=release
st25_branch=release
;;
*)
# assume branch-driven build on given branch
pynab_tag=""
driver_branch=master
st25_branch=main
;;
esac
LC_ALL=C
if [ -d "/boot/dietpi" ]
then
# DietPi distro
distro="dietpi"
elif [ -f "/etc/rpi-issue" ]
then
# Raspberry Pi OS distro
distro="raspios"
else
echo "$(basename ${0}): unsupported Linux distribution!"
exit 3
fi
owner=$(getent passwd 1000 | cut -d: -f1)
group=$(getent passwd 1000 | cut -d: -f4)
home_dir=$(getent passwd 1000 | cut -d: -f6)
inst_dir="/opt"
if [ "${1:-}" == "ci-chroot" ]
then
# CI chroot setup: building image in chroot environment.
install_env="ci-chroot"
elif [ "${1:-}" == "pi-zero" ]
then
# Local setup: allows doing initial or replacing existing setup on Pi Zero.
install_env=""
if [ "$(id -u)" != "0" ]
then
echo "$(basename ${0}) ${1} must be run as root."
echo "WARNING: Removes any existing Pynab setup."
exit 1
fi
rm -rf ${home_dir}/pynab ${home_dir}/wm8960 ${home_dir}/tagtagtag-ears ${home_dir}/cr14 ${home_dir}/st25r391x
rm -rf ${inst_dir}/pynab ${inst_dir}/wm8960 ${inst_dir}/tagtagtag-ears ${inst_dir}/cr14 ${inst_dir}/st25r391x
else
echo "Usage: $(basename ${0}) pi-zero|ci-chroot"
exit 2
fi
echo "Doing setup for ${pynab_repository} ${pynab_branch} branch${pynab_tag}."
# Edit /boot/cmdline.txt since it contains root device mapping to a PARTUUID that
# changed during parted resize when image was built.
echo "Editing /boot/cmdline.txt."
sed -i "s|root=PARTUUID=........-02|root=/dev/mmcblk0p2|" /boot/cmdline.txt
cat "/boot/cmdline.txt"
# Replace /etc/fstab since the non existing PARTUUID has to be changed here as well.
echo "Editing /etc/fstab."
sed -i -e "s|PARTUUID=........-01|/dev/mmcblk0p1|" -e "s|PARTUUID=........-02|/dev/mmcblk0p2|" /etc/fstab
cat "/etc/fstab" | grep "^/dev/"
echo "Setting gpu_mem."
GPU_MEM=16
if grep -q "^gpu_mem.*=" /boot/config.txt
then
sed -i "s/\(gpu_mem.*\)=.*/\1=${GPU_MEM}/" /boot/config.txt
else
echo "gpu_mem=${GPU_MEM}" >> /boot/config.txt
fi
cat /boot/config.txt | grep "gpu_mem.*="
echo "Disabling onboard sound."
if grep -q "^dtparam=audio" /boot/config.txt
then
sed -i "s/dtparam=audio.*/dtparam=audio=off/" /boot/config.txt
else
echo "dtparam=audio=off" >> /boot/config.txt
fi
cat /boot/config.txt | grep "dtparam=audio"
echo "Enabling DAC for Maker Faire 2018 cards."
if ! grep -q "^dtoverlay=hifiberry-dac" /boot/config.txt
then
echo "dtoverlay=hifiberry-dac" >> /boot/config.txt
fi
cat /boot/config.txt | grep "dtoverlay=hifiberry-dac"
if [ "${install_env}" == "ci-chroot" ]
then
# Install comitup package for headless WiFi setup
wget https://davesteele.github.io/comitup/deb/davesteele-comitup-apt-source_1.2_all.deb
dpkg -i --force-all davesteele-comitup-apt-source_1.2_all.deb
rm davesteele-comitup-apt-source_1.2_all.deb
apt-get update -y
apt-get install --no-install-recommends -y comitup comitup-watch
rm /etc/network/interfaces
systemctl mask dnsmasq.service
systemctl mask systemd-resolved.service
systemctl mask dhcpd.service
systemctl mask dhcpcd.service
systemctl mask wpa-supplicant.service
systemctl enable NetworkManager.service
echo "web_service: nginx.service" >> /etc/comitup.conf
echo "ap_name: Nabaztag-<nnn>" >> /etc/comitup.conf
echo "service_name: Nabaztag" >> /etc/comitup.conf
echo "external_callback: /opt/pynab/comitup-callback.sh" >> /etc/comitup.conf
if [ "${distro}" == "raspios" ]
then
# assume user will do any needed SD card configuration.
echo "Disabling SSH (can be enabled by putting an ssh file in /boot/)"
sudo update-rc.d ssh disable
elif [ "${distro}" == "dietpi" ]
then
echo "Setting DietPi 'first boot' parameters."
sed -i "s/\(AUTO_SETUP_KEYBOARD_LAYOUT\)=.*/\1=fr/" /boot/dietpi.txt
sed -i "s/\(AUTO_SETUP_TIMEZONE\)=.*/\1=Europe\/Paris/" /boot/dietpi.txt
sed -i "s/\(AUTO_SETUP_NET_WIFI_ENABLED\)=.*/\1=1/" /boot/dietpi.txt
sed -i "s/\(AUTO_SETUP_NET_WIFI_COUNTRY_CODE\)=.*/\1=FR/" /boot/dietpi.txt
sed -i "s/\(AUTO_SETUP_NET_HOSTNAME\)=.*/\1=Nabaztag/" /boot/dietpi.txt
sed -i "s/\(AUTO_SETUP_HEADLESS\)=.*/\1=1/" /boot/dietpi.txt
# -1=Nginx rather than -2=Lighttpd (DietPi default):
sed -i "s/\(AUTO_SETUP_WEB_SERVER_INDEX\)=.*/\1=-1/" /boot/dietpi.txt
# -4=Daemon + Drift rather than 2=boot + daily (DietPi default):
sed -i "s/\(CONFIG_NTP_MODE\)=.*/\1=4/" /boot/dietpi.txt
sed -i "s/\(CONFIG_SERIAL_CONSOLE_ENABLE\)=.*/\1=0/" /boot/dietpi.txt
fi
echo "Setting hostname."
hostname=$(cat /etc/hostname | tr -d " \t\n\r")
sed -i "s/${hostname}/Nabaztag/" /etc/hostname
sed -i "s/${hostname}/Nabaztag/" /etc/hosts
echo "Setting timezone."
echo "Europe/Paris" > /etc/timezone
ln -fs /usr/share/zoneinfo/Europe/Paris /etc/localtime
fi
echo "Ensuring user ${owner} has appropriate sudo privileges."
usermod -aG sudo ${owner}
if [ "${distro}" == "raspios" ]
then
echo "${owner} ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/010_${owner}-nopasswd
elif [ "${distro}" == "dietpi" ]
then
echo "${owner} ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/${owner}
fi
echo "Installing required packages."
sudo apt-get update -y
sudo apt-get dist-upgrade -y
sudo apt-get install --no-install-recommends -y postgresql libpq-dev git python3 python3-venv python3-dev gettext nginx openssl libssl-dev libffi-dev libmpg123-dev libasound2-dev libatlas-base-dev libgfortran5 libopenblas-dev liblapack-dev zram-tools
sudo apt-get install --no-install-recommends -y gcc make raspberrypi-kernel-headers
build_and_install_driver() {
for dir in /lib/modules/*/build
do
kernel=$(basename $(dirname ${dir}))
make KERNELRELEASE=${kernel}
sudo make install KERNELRELEASE=${kernel}
make clean KERNELRELEASE=${kernel}
done
}
echo "Installing sound driver for Ulule 2019 cards."
sudo mkdir -p ${inst_dir}/wm8960
sudo chown ${owner}:${group} ${inst_dir}/wm8960
sudo -u ${owner} git clone --depth 1 -b tagtagtag-sound https://github.com/pguyot/wm8960 ${inst_dir}/wm8960
sudo git config --system --add safe.directory ${inst_dir}/wm8960
cd ${inst_dir}/wm8960
build_and_install_driver
echo "Installing ears driver."
sudo mkdir -p ${inst_dir}/tagtagtag-ears
sudo chown ${owner}:${group} ${inst_dir}/tagtagtag-ears
sudo -u ${owner} git clone --depth 1 -b ${driver_branch} https://github.com/pguyot/tagtagtag-ears ${inst_dir}/tagtagtag-ears
sudo git config --system --add safe.directory ${inst_dir}/tagtagtag-ears
cd ${inst_dir}/tagtagtag-ears
build_and_install_driver
echo "Installing RFID reader driver for TAGTAG."
sudo mkdir -p ${inst_dir}/cr14
sudo chown ${owner}:${group} ${inst_dir}/cr14
sudo -u ${owner} git clone --depth 1 -b ${driver_branch} https://github.com/pguyot/cr14 ${inst_dir}/cr14
sudo git config --system --add safe.directory ${inst_dir}/cr14
cd ${inst_dir}/cr14
build_and_install_driver
echo "Installing RFID reader driver for 2022 NFC card."
sudo mkdir -p ${inst_dir}/st25r391x
sudo chown ${owner}:${group} ${inst_dir}/st25r391x
sudo -u ${owner} git clone --depth 1 -b ${st25_branch} https://github.com/pguyot/st25r391x ${inst_dir}/st25r391x
sudo git config --system --add safe.directory ${inst_dir}/st25r391x
cd ${inst_dir}/st25r391x
build_and_install_driver
echo "Disabling 2022 NFC card driver and making sure i2c-dev is enabled"
# Disable this driver as it conflicts with cr14 (nabboot will do the switch)
sed /boot/config.txt -i -e "s/^dtoverlay=st25r391x/#dtoverlay=st25r391x/"
# Enable i2c-dev
grep -q -E "^i2c-dev" /etc/modules || printf "i2c-dev\n" >> /etc/modules
if [ -d "${inst_dir}/pynab" ]
then
echo "Using existing local Pynab repository."
else
echo "Cloning Pynab ${pynab_branch} branch from ${pynab_repository}."
sudo mkdir -p ${inst_dir}/pynab
sudo chown ${owner}:${group} ${inst_dir}/pynab
sudo -u ${owner} git clone --depth 1 -b ${pynab_branch} https://github.com/${pynab_repository}.git ${inst_dir}/pynab
fi
sudo git config --system --add safe.directory ${inst_dir}/pynab
echo "Installing NabBlockly."
sudo apt-get install --no-install-recommends -y erlang-base erlang-dev erlang-inets erlang-tools erlang-xmerl
cd ${inst_dir}/pynab
sudo -u ${owner} git clone --depth 1 -b ${driver_branch} https://github.com/pguyot/nabblockly
sudo git config --system --add safe.directory ${inst_dir}/pynab/nabblockly
cd ${inst_dir}/pynab/nabblockly
# Until we can get OTP 24 from Raspian or Erlang Solutions, get an older rebar binary
sudo -u ${owner} wget https://github.com/erlang/rebar3/releases/download/3.15.1/rebar3 && chmod +x rebar3
sudo apt-get install --no-install-recommends -y g++
sudo -u ${owner} ./rebar3 release
echo "Running Pynab install script."
sudo apt-get install --no-install-recommends -y alsa-utils xz-utils avahi-daemon
cd ${inst_dir}/pynab
sudo -u ${owner} /bin/bash install.sh ${install_env}
echo "Cleaning up."
# Cleanup caches.
sudo rm -rf /root/.cache /root/.local /root/.wget-hsts
sudo rm -rf ${home_dir}/.cache ${home_dir}/.local ${home_dir}/.wget-hsts
echo "Pynab setup done."