-
Notifications
You must be signed in to change notification settings - Fork 1
/
install.sh
executable file
·440 lines (369 loc) · 11.5 KB
/
install.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
#! /bin/bash
#
# Raspberry Pi network configuration / AP, MESH install script
# Author: Sarah Grant
# Contributors: Mark Hansen, Matthias Strubel, Danja Vasiliev
# took guidance from a script by Paul Miller : https://dl.dropboxusercontent.com/u/1663660/scripts/install-rtl8188cus.sh
# Updated 20 April 2024
#
# TO-DO
# - allow a selection of radio drivers
# - fix addressing to avoid collisions below w/avahi
USERNAME=$1
KUBO="kubo_v0.28.0_linux-arm64.tar.gz"
# # # # # # # # # # # # # # # # # # # # # # # # # # #
# first find out if this is RPi3 or not, based on revision code
# reference: http://www.raspberrypi-spy.co.uk/2012/09/checking-your-raspberry-pi-board-version/
REV="$(cat /proc/cpuinfo | grep 'Revision' | awk '{print $3}')"
# # # # # # # # # # # # # # # # # # # # # # # # # # #
# READ configuration file
. ./subnodes.config
# # # # # # # # # # # # # # # # # # # # # # # # # # #
# CHECK USER PRIVILEGES
(( `id -u` )) && echo "This script must be ran with root privileges, try prefixing with sudo. i.e sudo $0" && exit 1
# # # # # # # # # # # # # # # # # # # # # # # # # # #
# BEGIN INSTALLATION PROCESS
#
clear
echo "Installing Subnodes..."
echo ""
read -p "This script will install ipfs, nginx, set up a wireless access point and captive portal with hostapd and dnsmasq, and provide the option of configuring a BATMAN-ADV mesh point with batctl. Make sure you have one (or two, if installing the additional mesh point) USB wifi radios connected to your Raspberry Pi before proceeding. Press any key to continue..."
echo ""
clear
# # # # # # # # # # # # # # # # # # # # # # # # # # #
# CHECK REQUIRED RADIOS
# check that iw list does not fail with 'nl80211 not found'
echo -en "Checking that USB wifi radio is available for access point..."
readarray IW < <(iw dev | awk '$1~"phy#"{PHY=$1}; $1=="Interface" && $2~"wlan"{WLAN=$2; sub(/#/, "", PHY); print PHY " " WLAN}')
if [[ -z $IW ]] ; then
echo -en "[FAIL]\n"
echo "Warning! Wireless adapter not found! Please plug in a wireless radio after installation completes and before reboot."
echo "Installation process will proceed in 5 seconds..."
sleep 5
else
echo -en "[OK]\n"
fi
# now check that iw list finds a radio other than wlan0 if mesh point option was set to 'y' in config file
case $DO_SET_MESH in
[Yy]* )
echo -en "Checking that USB wifi radio is available for mesh point..."
readarray IW < <(iw dev | awk '$1~"phy#"{PHY=$1}; $1=="Interface" && $2!="wlan0"{WLAN=$2; sub(/#/, "", PHY); print PHY " " WLAN}')
if [[ -z $IW ]] ; then
echo -en "[FAIL]\n"
echo "Warning! Second wireless adapter not found! Please plug in an addition wireless radio after installation completes and before reboot."
echo "Installation process will proceed in 5 seconds..."
sleep 5
else
echo -en "[OK]\n"
fi
;;
esac
# # # # # # # # # # # # # # # # # # # # # # # # # # #
# SOFTWARE INSTALL
#
# update the packages
echo -en "Updating apt and installing iw, dnsutils, nginx, batctl, tar, wget"
apt update && apt install -y iw dnsutils nginx batctl tar wget
# Change the directory owner and group
chown www-data:www-data /var/www
# allow the group to write to the directory
chmod 775 /var/www
# Add the user to the www-data group
usermod -a -G www-data $USERNAME
systemctl start nginx
# install ipfs-rpi repo
cd /home/$USERNAME
wget https://sourceforge.net/projects/ipfs-kubo.mirror/files/v0.28.0/$KUBO
tar -xvzf $KUBO
rm $KUBO
cd kubo && ./install.sh
sudo -u $USERNAME ipfs init
sed -i -e '$i \ipfs daemon &\n' /etc/rc.local
cd /home/$USERNAME/subnodes-ipfs
echo pwd
echo -en "Loading the subnodes configuration file..."
# Check if configuration exists, ask for overwriting
if [ -e /etc/subnodes.config ] ; then
read -p "Older config file found! Overwrite? (y/n) [N]" -e $q
if [ "$q" == "y" ] ; then
echo "...overwriting"
copy_ok="yes"
else
echo "...not overwriting. Re-reading found configuration file..."
. /etc/subnodes.config
fi
else
copy_ok="yes"
fi
# copy config file to /etc
[ "$copy_ok" == "yes" ] && cp subnodes.config /etc
# # # # # # # # # # # # # # # # # # # # # # # # # # #
# Configure the access point and captive portal
#
clear
echo -en "Configuring Access Point..."
# install required packages
echo -en "Installing bridge-utils, hostapd and dnsmasq..."
apt install -y bridge-utils hostapd dnsmasq
echo -en "[OK]\n"
# backup the existing interfaces file
echo -en "Creating backup of network interfaces configuration file..."
cp /etc/network/interfaces /etc/network/interfaces.bak
rc=$?
if [[ $rc != 0 ]] ; then
echo -en "[FAIL]\n"
exit $rc
else
echo -en "[OK]\n"
fi
# create hostapd init file
echo -en "Creating default hostapd file..."
cat <<EOF > /etc/default/hostapd
DAEMON_CONF="/etc/hostapd/hostapd.conf"
EOF
rc=$?
if [[ $rc != 0 ]] ; then
echo -en "[FAIL]\n"
echo ""
exit $rc
else
echo -en "[OK]\n"
fi
# # # # # # # # # # # # # # # # # # # # # # # # # # #
# Check if we are configuring a mesh node
clear
echo -en "Checking whether to configure mesh point or not..."
case $DO_SET_MESH in
[Yy]* )
clear
echo -en "Configuring Raspberry Pi as a BATMAN-ADV mesh point..."
echo -en "Enabling the batman-adv kernel module..."
# add the batman-adv module to be started on boot
sed -i '$a batman-adv' /etc/modules
modprobe batman-adv;
# pass the selected mesh ssid into mesh startup script
sed -i "s/MTU/$MTU/" scripts/subnodes_mesh.sh
sed -i "s/SSID/$MESH_SSID/" scripts/subnodes_mesh.sh
sed -i "s/CELL_ID/$CELL_ID/" scripts/subnodes_mesh.sh
sed -i "s/CHAN/$MESH_CHANNEL/" scripts/subnodes_mesh.sh
sed -i "s/GW_MODE/$GW_MODE/" scripts/subnodes_mesh.sh
#sed -i "s/GW_SEL_CLASS/$GW_SEL_CLASS/" scripts/subnodes_mesh.sh
#sed -i "s/GW_BANDWIDTH/$GW_BANDWIDTH/" scripts/subnodes_mesh.sh
#sed -i "s/GW_IP/$GW_IP/" scripts/subnodes_mesh.sh
# configure dnsmasq
echo -en "Creating dnsmasq configuration file..."
cat <<EOF > /etc/dnsmasq.conf
interface=br0
address=/#/$BRIDGE_IP
dhcp-range=$BR_DHCP_START,$BR_DHCP_END,$DHCP_NETMASK,$DHCP_LEASE
EOF
rc=$?
if [[ $rc != 0 ]] ; then
echo -en "[FAIL]\n"
echo ""
exit $rc
else
echo -en "[OK]\n"
fi
# create new /etc/network/interfaces
echo -en "Creating new network interfaces with your settings..."
cat <<EOF > /etc/network/interfaces
auto lo
iface lo inet loopback
allow-hotplug eth0
auto eth0
iface eth0 inet dhcp
auto wlan0
iface wlan0 inet static
address $AP_IP
netmask $AP_NETMASK
auto br0
iface br0 inet static
address $BRIDGE_IP
netmask $BRIDGE_NETMASK
bridge_ports bat0 wlan0
bridge_stp off
iface default inet dhcp
EOF
rc=$?
if [[ $rc != 0 ]] ; then
echo -en "[FAIL]\n"
echo ""
exit $rc
else
echo -en "[OK]\n"
fi
# create hostapd configuration with user's settings
echo -en "Creating hostapd.conf file..."
cat <<EOF > /etc/hostapd/hostapd.conf
interface=wlan0
bridge=br0
driver=$RADIO_DRIVER
country_code=$AP_COUNTRY
ctrl_interface=/var/run/hostapd
ctrl_interface_group=0
ssid=$AP_SSID
hw_mode=g
channel=$AP_CHAN
beacon_int=100
auth_algs=1
wpa=0
ap_isolate=1
macaddr_acl=0
wmm_enabled=1
ieee80211n=1
EOF
rc=$?
if [[ $rc != 0 ]] ; then
echo -en "[FAIL]\n"
exit $rc
else
echo -en "[OK]\n"
fi
# COPY OVER START UP SCRIPTS
echo ""
echo "Adding startup scripts to init.d..."
cp scripts/subnodes_ap.sh /etc/init.d/subnodes_ap
chmod 755 /etc/init.d/subnodes_ap
update-rc.d subnodes_ap defaults
cp scripts/subnodes_mesh.sh /etc/init.d/subnodes_mesh
chmod 755 /etc/init.d/subnodes_mesh
update-rc.d subnodes_mesh defaults
;;
# # # # # # # # # # # # # # # # # # # # # # # # # # #
# Access Point only
#
[Nn]* )
# if no mesh point is created, set up network interfaces, hostapd and dnsmasq to operate without a bridge
clear
# configure dnsmasq
echo -en "Creating dnsmasq configuration file..."
cat <<EOF > /etc/dnsmasq.conf
# Captive Portal logic (redirects traffic coming in on br0 to our web server)
interface=wlan0
address=/#/$AP_IP
# DHCP server
dhcp-range=$AP_DHCP_START,$AP_DHCP_END,$DHCP_NETMASK,$DHCP_LEASE
EOF
rc=$?
if [[ $rc != 0 ]] ; then
echo -en "[FAIL]\n"
echo ""
exit $rc
else
echo -en "[OK]\n"
fi
# create new /etc/network/interfaces
echo -en "Creating new network interfaces with your settings..."
cat <<EOF > /etc/network/interfaces
auto lo
iface lo inet loopback
allow-hotplug eth0
auto eth0
iface eth0 inet dhcp
auto wlan0
iface wlan0 inet static
address $AP_IP
netmask $AP_NETMASK
iface default inet dhcp
EOF
rc=$?
if [[ $rc != 0 ]] ; then
echo -en "[FAIL]\n"
echo ""
exit $rc
else
echo -en "[OK]\n"
fi
# create hostapd configuration with user's settings
echo -en "Creating hostapd.conf file..."
cat <<EOF > /etc/hostapd/hostapd.conf
interface=wlan0
driver=$RADIO_DRIVER
country_code=$AP_COUNTRY
ctrl_interface=/var/run/hostapd
ctrl_interface_group=0
ssid=$AP_SSID
hw_mode=g
channel=$AP_CHAN
auth_algs=1
wpa=0
ap_isolate=1
macaddr_acl=0
wmm_enabled=1
ieee80211n=1
EOF
rc=$?
if [[ $rc != 0 ]] ; then
echo -en "[FAIL]\n"
exit $rc
else
echo -en "[OK]\n"
fi
# COPY OVER START UP SCRIPTS
echo ""
echo "Adding startup scripts to init.d..."
cp scripts/subnodes_ap.sh /etc/init.d/subnodes_ap
chmod 755 /etc/init.d/subnodes_ap
update-rc.d subnodes_ap defaults
;;
esac
# # # # # # # # # # # # # # # # # # # # # # # # # # #
# IPFS CONFIG
#
case $BOOTSTRAP_NODE in
[Yy]* )
sudo -u $USERNAME echo -e "/key/swarm/psk/1.0.0/\n/base16/\n`tr -dc 'a-f0-9' < /dev/urandom | head -c64`" > sudo -u $USERNAME $HOME/.ipfs/swarm.key
echo -en "! You must copy this key to ~/.ipfs/swarm.key on all client nodes:"
sudo -u $USERNAME cat $HOME/.ipfs/swarm.key
BOOTSTRAP_IP=$(hostname -I)
echo -en "Copy this IP address to share with client nodes: $BOOTSTRAP_IP"
BOOTSTRAP_PEER_ID=$(ipfs config show | grep "PeerID" | cut -d'"' -f 4)
echo -en "Copy this peer ID to share with client nodes: $BOOTSTRAP_PEER_ID"
read -p "Did you copy everything? Hit <enter> to keep going..."
sudo -u $USERNAME ipfs bootstrap rm --all
sudo -u $USERNAME ipfs bootstrap add /ip4/$BOOTSTRAP_IP/tcp/4001/ipfs/$BOOTSTRAP_PEER_ID
;;
[Nn]* )
echo "Adding swarm.key to .ipfs..."
cp scripts/swarm.key /home/$USERNAME/.ipfs/swarm.key
chown $USERNAME:$USERNAME /home/$USERNAME/.ipfs/swarm.key
chmod 644 /home/$USERNAME/.ipfs/swarm.key
# Check if a swarm.key exists, ask for overwriting
#if [ -e sudo -u $USERNAME $HOME/.ipfs/swarm.key ] ; then
# read -p "swarm.key already exists! Overwrite? (y/n) [N]" -e $q
# if [ "$q" == "y" ] ; then
# echo "...overwriting"
# overwrite_sk="yes"
# else
# echo "...not overwriting."
# fi
#else
# overwrite_sk="yes"
#fi
# copy swarm.key from config to .ipfs
# [ "$overwrite_sk" == "yes" ] && sudo -u $USERNAME echo $SWARM_KEY > sudo -u $USERNAME $HOME/.ipfs/swarm.key
sudo -u $USERNAME ipfs bootstrap rm --all
sudo -u $USERNAME ipfs bootstrap add /ip4/$BOOTSTRAP_IP/tcp/4001/ipfs/$BOOTSTRAP_PEER_ID
;;
esac
export LIBP2P_FORCE_PNET=1
sed -i -e "\$i sudo -u $USERNAME ipfs daemon &\n" /etc/rc.local
# TO-DO: Give ppl the choice of which site to host on IPFS
chgrp www-data /var/www/html
chown www-data /var/www/html
chmod 775 /var/www/html
# # # # # # # # # # # # # # # # # # # # # # # # # # #
# enable services
#
clear
update-rc.d dnsmasq enable
update-rc.d hostapd remove
systemctl unmask hostapd
systemctl enable hostapd
read -p "Do you wish to reboot now? [N] " yn
case $yn in
[Yy]* )
reboot;;
[Nn]* ) exit 0;;
esac