-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.sh
executable file
·352 lines (283 loc) · 10.3 KB
/
bootstrap.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
#!/bin/bash
# TWIN node - A Flexible Testbed for Wireless Sensor Networks
# Copyright (C) 2016, Communication Networks, University of Bremen, Germany
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 3 of the License.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, see <http://www.gnu.org/licenses/>
#
# This file is part of TWIN
# Run this Bootstrap script as root to make the Pi
# setup according to Wiki
TARGET_SITE=www.google.com # to check internet connectivity
PYTHON_CHECK=/usr/bin/python3 # to check if python3 exists on Jessie
PIP_CHECK=/usr/bin/pip3 # to check if pip3 exists on Jessie
TWIN_HOME=/home/pi/ # Designated Folder
# specific directories for TWIN
DIRECTORIES=(bin bsl incoming logFiles outgoing)
CURRENT_USER=$(who | awk '{print $1}') # used to change ownership
# log files to be in logFiles
LOG_FILES=(bsl incoming paramiko scheduler session TWIN)
# Determine which Raspberry Pi model using Hardware Revision Value
WHICH_PI=$(cat /proc/cpuinfo | grep "Revision" | awk '{print $3}')
# Check for Pi's Wireless Interface: For Pi - 2 --> External USB Dongle
EXT_WLAN_CHIPSET_1=$(lsmod | grep "cfg80211" | awk '{print $4}' | grep "rt2x00lib") # Only Ralink Chipsets for TWIN
# Check for RTL8188EU chipset
EXT_WLAN_CHIPSET_2=$(lsmod | grep "cfg80211" | awk '{print $4}' | grep "r8188eu") # Only Ralink Chipsets for TWIN
# Check for Pi's Wireless Interface: for Pi - 3 --> Internal Driver
INT_WLAN_CHIPSET=$(lsmod | grep "cfg80211" | awk '{print $4}'|grep "brcmfmac") # standard BRCM Based Chipsets
#------------------------------------------------------------------------#
# Step 0
#------------------------------------------------------------------------#
if [[ $EUID -ne 0 ]]; then
echo
echo "TWIN: Step 0: You need root privilege to run this script"
exit 1
fi
#------------------------------------------------------------------------#
# Step 1
#------------------------------------------------------------------------#
echo
echo "TWIN: Step 1: Checking Internet connectivity"
echo
ping -I eth0 -c 1 -q -W 1 $TARGET_SITE > /dev/null
if [[ "$1" -ne 0 ]]; then
echo
echo "Cannot reach target website"
echo
echo "Check internet connectivity on Pi"
exit 1
fi
# Provide User an option to skip the "apt" updates and upgrades
# if the script fails the first time
if [ "$#" = "1" ]; then
argument_cmd="$1" # take the argument
else
argument_cmd="none"
fi
# If the argument along with the script is "skip-apt" then let go
# of these steps
if [ "$argument_cmd" != "skip-apt" ]; then
#------------------------------------------------------------------------#
#Step 2
#------------------------------------------------------------------------#
echo
echo "TWIN: Step 2: update and upgrade"
echo
# always good to be updates
apt update && apt upgrade
#------------------------------------------------------------------------#
# Step 3
#------------------------------------------------------------------------#
echo
echo "TWIN: Step 3: Installing dependencies and modules for TWIN"
echo
# CHECK: For Jessie Lite
# since Jessie Lite does not have python3 pre-installed
if [[ -x $PYTHON_CHECK && -x $PIP_CHECK ]]; then
echo
echo "TWIN: Step 3.a: python3 and pip3 already installed"
echo
else
#------------------------------------------------------------------------#
# Step 3.a
#------------------------------------------------------------------------#
echo "TWIN: Step 3.a: installing python3 and pip3 on Pi"
echo
apt install python3 python3-pip
fi
#------------------------------------------------------------------------#
# Step 3.b
#------------------------------------------------------------------------#
echo
echo "TWIN: Step 3.b: Installing apt dependencies"
echo
# python-pip is for installing Flask
apt install -y git python-pip figlet libssl-dev \
libffi-dev inotify-tools tmux
#------------------------------------------------------------------------#
# Step 3.c
#------------------------------------------------------------------------#
echo
echo "TWIN: Step 3.c: Installing python pip modules"
echo
pip install flask
pip3 install paramiko scp cryptography schedule lt-code
fi
# If "skip-apt" as argument is mentioned
#------------------------------------------------------------------------#
# Step 4
#------------------------------------------------------------------------#
echo
echo "TWIN: Step 4: Creating directories for TWIN/Sprinkler protocol"
echo
if [[ "$TWIN_HOME" != "$pwd" ]]; then
cd $TWIN_HOME # change to /home/pi/ if triggered from cloned Repo
fi
# create the directories in /home/pi/ directory
for directory in ${DIRECTORIES[@]}; do
mkdir $directory
# change ownership of directory since this script is run by root
# change it to pi:pi (generally)
chown -R $CURRENT_USER:$CURRENT_USER $directory
done
touch routeTable.json # Route cache JSON file
chown -R $CURRENT_USER:$CURRENT_USER routeTable.json # change owner
# create empty log files in logFiles directory
for log_file_name in ${LOG_FILES[@]}; do
touch logFiles/${log_file_name}.log
chown -R $CURRENT_USER:$CURRENT_USER logFiles/${log_file_name}.log
done
#------------------------------------------------------------------------#
# Step 5
#------------------------------------------------------------------------#
echo
echo "TWIN: Step 5: Moving Scripts in Designated directories"
echo
# necessary scripts into bin/
mv TWIN/Data-Retrieval/*.py $TWIN_HOME/bin
mv TWIN/Scripts/createSession.sh $TWIN_HOME/bin
# necessary scripts into bsl/
mv TWIN/Scripts/bslChecker.sh $TWIN_HOME/bsl
mv TWIN/Scripts/z1-bsl-nopic $TWIN_HOME/bsl
# necessary scripts into incoming/
mv TWIN/Scripts/incomingChecker.sh $TWIN_HOME/incoming
# create a dummy tar file for TWIN framework check
touch $TWIN_HOME/incoming/incomingData0.tar
#------------------------------------------------------------------------#
# Step 6
#------------------------------------------------------------------------#
echo
echo "TWIN: Step 6: Setting cron job for triggering framework on reboot"
echo
# list all the crontabs for pi (not for root)
crontab -l -u pi > twinCron
echo "@reboot /home/pi/bin/createSession.sh" >> twinCron
crontab -u pi twinCron
rm twinCron
#------------------------------------------------------------------------#
# Step 7
#------------------------------------------------------------------------#
echo
echo "TWIN: Step 7: Changing content of rc.local for Ad Hoc Network setup"
echo
echo "Creating a backup for rc.local"
cp /etc/rc.local /etc/rc.local.backup
# Check Which Model of Pi is configured
case "$WHICH_PI" in
# For Raspberry Pi 2 Model B
"a01041" | "a21041" | "a22042")
echo "Raspberry Pi 2 Model B detected.."
# check for External Chipset
if [[ "$EXT_WLAN_CHIP_1" == "rt2x00lib" ]] || [[ "$EXT_WLAN_CHIP_1" == "mac80211, rt2x00lib" ]]; then
echo "Ralink Chipset Found.."
echo
echo "ipv6" >> /etc/modules; echo "rt2x00lib" >> /etc/modules
echo "Writing rc.local for Pi-2.."
# using here-docs to write to rc.local file
cat <<- 'EOF' > /etc/rc.local
#!/bin/sh -e
# contents of rc.local for Pi - 2 Model B
echo "Setting Ad-Hoc Network Parameters"
ifconfig wlan0 down
iwconfig wlan0 mode ad-hoc essid pi-adhoc channel 6 txpower 0
ifconfig wlan0 up
exit 0
EOF
# here-docs for rc.local ends above
elif [[ "$EXT_WLAN_CHIP_2" == "r8188eu" ]]; then
echo "ipv6" >>/etc/modules; echo "r8188eu" >> /etc/modules
echo
echo "Writing rc.local for Pi-2.. with r8188eu driver"
cat <<- 'EOF' > /etc/rc.local
#!/bin/sh -e
ifconfig wlan0 down
iwconfig wlan0 mode ad-hoc essid pi-adhoc channel 6
# cannot change txpower for r8188eu drivers
ifconfig wlan0 up
exit 0
EOF
else
echo "No chipset found.."
exit 1
fi
;;
# For Raspberry Pi 3
"a02082" | "a22082")
echo "Raspberry Pi 3 detected.."
# check for Internal Chipset
if [[ "$INT_WLAN_CHIP" == "brcmfmac" ]]; then # if Chipset exists
echo "Internal WLAN Chipset exists.."
echo "ipv6" >> /etc/modules; echo "$INT_WLAN_CHIP" >> /etc/modules
echo "Writing rc.local for Pi-3.."
# using here-docs to write to rc.local file
cat <<- 'EOF' > /etc/rc.local
#!/bin/sh -e
# contents of rc.local for Pi - 3
echo "Setting Ad-Hoc Network Parameters"
iwconfig wlan0 mode ad-hoc essid pi-adhoc channel 6 txpower 0
exit 0
EOF
# here-docs ends for rc.local above
else
echo "No Internal Chipset found.."
exit 1
fi
;;
esac # End switch case
#------------------------------------------------------------------------#
# STEP 8
#------------------------------------------------------------------------#
echo
echo "TWIN: Step 8: Creating a backup & updating the network-interfaces file"
echo
cp /etc/network/interfaces /etc/network/interfaces.backup
cat << EOF > /etc/network/interfaces
# loopback interface
auto lo
iface lo inet loopback
# ethernet eth0
iface eth0 inet manual
# WLAN wlan0 for Ad Hoc network
# TWIN Node
allow-hotplug wlan0
iface wlan0 inet6 auto
EOF
#------------------------------------------------------------------------#
# STEP 9
#------------------------------------------------------------------------#
echo
echo "TWIN: Step 9: Changing content of dhcpcd configuration file"
echo
cp /etc/dhcpcd.conf /etc/dhcpcd.conf.backup
cat << EOF >> /etc/dhcpcd.conf
# Use Stateless Auto Configuration for LL IPv6 Addresses
slaac slaac
# Avoid using Bootstrap Protocol for DHCP discover on wlan0
denyinterfaces wlan0
EOF
# Jessie Lite takes a long time to boot if dhcpcd service is on
# might as well disable it since it is not needed
systemctl disable dhcpcd.service
#------------------------------------------------------------------------#
#STEP 10
#------------------------------------------------------------------------#
echo
echo "TWIN: Step 10: Disabling IPv6 on eth0"
echo
cat << EOF >> /etc/sysctl.conf
# disable IPv6
net.ipv6.conf.eth0.disable_ipv6=1
EOF
echo "Bootstrap complete...."
echo "Reboot your Pi using: "
echo "sudo reboot"
exit 0