-
Notifications
You must be signed in to change notification settings - Fork 30
/
1_Pi_Config.sh
executable file
·178 lines (160 loc) · 5.01 KB
/
1_Pi_Config.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
#!/bin/bash
if [ $(id -u) -ne 0 ]; then
echo "Installer must be run as root."
echo "Try 'sudo bash $0'"
exit 1
fi
MODEL=`cat /proc/device-tree/model`
echo "This script configure a Raspberry Pi"
echo "Raspian for being a LoRaWAN Gateway"
echo
echo "Device is $MODEL"
echo
echo "Run time ~5 minutes. Reboot required."
echo
echo -n "CONTINUE? [Y/n] "
read
if [[ "$REPLY" =~ ^(no|n|N)$ ]]; then
echo "Canceled."
exit 0
fi
# These functions have been copied from excellent Adafruit Read only tutorial
# https://github.com/adafruit/Raspberry-Pi-Installer-Scripts/blob/master/read-only-fs.sh
# the one inspired by my original article http://hallard.me/raspberry-pi-read-only/
# That's an excellent demonstration of collaboration and open source sharing
#
# Given a filename, a regex pattern to match and a replacement string:
# Replace string if found, else no change.
# (# $1 = filename, $2 = pattern to match, $3 = replacement)
replace() {
grep $2 $1 >/dev/null
if [ $? -eq 0 ]; then
# Pattern found; replace in file
sed -i "s/$2/$3/g" $1 >/dev/null
fi
}
# Given a filename, a regex pattern to match and a replacement string:
# If found, perform replacement, else append file w/replacement on new line.
replaceAppend() {
grep $2 $1 >/dev/null
if [ $? -eq 0 ]; then
# Pattern found; replace in file
sed -i "s/$2/$3/g" $1 >/dev/null
else
# Not found; append on new line (silently)
echo $3 | sudo tee -a $1 >/dev/null
fi
}
# Given a filename, a regex pattern to match and a string:
# If found, no change, else append file with string on new line.
append1() {
grep $2 $1 >/dev/null
if [ $? -ne 0 ]; then
# Not found; append on new line (silently)
echo $3 | sudo tee -a $1 >/dev/null
fi
}
# Given a list of strings representing options, display each option
# preceded by a number (1 to N), display a prompt, check input until
# a valid number within the selection range is entered.
selectN() {
for ((i=1; i<=$#; i++)); do
echo $i. ${!i}
done
echo
REPLY=""
while :
do
echo -n "SELECT 1-$#: "
read
if [[ $REPLY -ge 1 ]] && [[ $REPLY -le $# ]]; then
return $REPLY
fi
done
}
echo "Updating dependencies"
apt-get update && apt-get upgrade -y --force-yes && apt-get update
apt-get install -y --force-yes git-core build-essential ntp scons i2c-tools
echo "Updating python dependencies"
apt-get install -y --force-yes python-dev swig python-psutil python-rpi.gpio python-pip
python -m pip install --upgrade pip setuptools wheel
if [[ ! -d /home/loragw ]]; then
echo "Adding new user loragw, enter it password"
useradd -m loragw -s /bin/bash
passwd loragw
usermod -a -G sudo loragw
cp /etc/sudoers.d/010_pi-nopasswd /etc/sudoers.d/010_loragw-nopasswd
sed -i -- 's/pi/loragw/g' /etc/sudoers.d/010_loragw-nopasswd
cp /home/pi/.profile /home/loragw/
cp /home/pi/.bashrc /home/loragw/
chown loragw:loragw /home/loragw/.*
usermod -a -G i2c,spi,gpio loragw
fi
echo "Enabling Uart, I2C, SPI, Video Memory to 16MB"
replaceAppend /boot/config.txt "^.*enable_uart.*$" "enable_uart=1"
replaceAppend /boot/config.txt "^.*dtparam=i2c_arm=.*$" "dtparam=i2c_arm=on"
replaceAppend /boot/config.txt "^.*dtparam=spi=.*$" "dtparam=spi=on"
replaceAppend /boot/config.txt "^.*gpu_mem=.*$" "gpu_mem=16"
replaceAppend /etc/modules "^.*i2c-dev.*$" "i2c-dev"
echo -n "Do you want to configure timezone [y/N] "
read
if [[ "$REPLY" =~ ^(yes|y|Y)$ ]]; then
echo "Reconfiguring Time Zone."
dpkg-reconfigure tzdata
fi
if [[ ! -f /usr/local/bin/log2ram ]]; then
echo -n "Do you want to enable log2ram [y/N] "
read
if [[ "$REPLY" =~ ^(yes|y|Y)$ ]]; then
echo "Setting up log2ram."
git clone https://github.com/azlux/log2ram.git
cd log2ram
chmod +x install.sh uninstall.sh
./install.sh
ln -s /usr/local/bin/log2ram /etc/cron.hourly/
echo "cleaning up log rotation"
replace /etc/logrotage.d/rsyslog "^.*daily.*$" " hourly"
replace /etc/logrotage.d/rsyslog "^.*monthly.*$" " daily"
replace /etc/logrotage.d/rsyslog "^.*delaycompress.*$" " "
echo "forcing one log rotation"
logrotate /etc/logrotate.conf
echo "Please don't forget to adjust the logrotate"
echo "paratemeters in /etc/logrotage.d/* to avoid"
echo "filling up the ramdisk, see README in"
echo "https://github.com/ch2i/LoraGW-Setup/"
echo ""
fi
fi
# set hostname to loragw-xxyy with xxyy last MAC Address digits
set -- `cat /sys/class/net/wlan0/address`
IFS=":"; declare -a Array=($*)
NEWHOST=loragw-${Array[4]}${Array[5]}
echo ""
echo "Please select new device name (hostname)"
selectN "Leave as $HOSTNAME" "loragw" "$NEWHOST"
SEL=$?
if [[ $SEL -gt 1 ]]; then
if [[ $SEL == 2 ]]; then
NEWHOST=loragw
fi
sudo bash -c "echo $NEWHOST" > /etc/hostname
replace /etc/hosts "^127.0.1.1.*$HOSTNAME.*$" "127.0.1.1\t$NEWHOST"
echo "New hostname set to $NEWHOST"
else
echo "hostname unchanged"
fi
echo "Done."
echo
echo "Settings take effect on next boot."
echo "after reboot, login back here with"
echo "ssh loragw@$NEWHOST.local"
echo
echo -n "REBOOT NOW? [y/N] "
read
if [[ ! "$REPLY" =~ ^(yes|y|Y)$ ]]; then
echo "Exiting without reboot."
exit 0
fi
echo "Reboot started..."
reboot
exit 0