forked from adafruit/Raspberry-Pi-Installer-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtc.sh
executable file
·373 lines (320 loc) · 9.94 KB
/
rtc.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
#!/bin/bash
: <<'DISCLAIMER'
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
This script is licensed under the terms of the MIT license.
Unless otherwise noted, code reproduced herein
was written for this script.
Originally written by the - The Pimoroni Crew -
DISCLAIMER
productname="Real Time Clock module" # the name of the product to install
rtcsubtype="ds1307"
baseurl="http://www.adafru.it"
scriptname="rtc.sh" # the name of this script
spacereq=1 # minimum size required on root partition in MB
debugmode="no" # whether the script should use debug routines
debuguser="none" # optional test git user to use in debug mode
debugpoint="none" # optional git repo branch or tag to checkout
forcesudo="yes" # whether the script requires to be ran with root privileges
promptreboot="no" # whether the script should always prompt user to reboot
customcmd="yes" # whether to execute commands specified before exit
i2creq="yes" # whether the i2c interface is required
i2sreq="no" # whether the i2s interface is required
spireq="no" # whether the spi interface is required
uartreq="no" # whether uart communication is required
armhfonly="yes" # whether the script is allowed to run on other arch
armv6="yes" # whether armv6 processors are supported
armv7="yes" # whether armv7 processors are supported
armv8="yes" # whether armv8 processors are supported
raspbianonly="yes" # whether the script is allowed to run on other OSes
macosxsupport="no" # whether Mac OS X is supported by the script
osreleases=( "Raspbian" ) # list os-releases supported
oswarning=( "Debian" "Ubuntu" "Mate" ) # list experimental os-releases
squeezesupport="no" # whether Squeeze is supported
wheezysupport="no" # whether Wheezy is supported
jessiesupport="yes" # whether Jessie is supported
FORCE=$1
DEVICE_TREE=true
ASK_TO_REBOOT=false
CURRENT_SETTING=false
UPDATE_DB=false
BOOTCMD=/boot/cmdline.txt
CONFIG=/boot/config.txt
APTSRC=/etc/apt/sources.list
INITABCONF=/etc/inittab
BLACKLIST=/etc/modprobe.d/raspi-blacklist.conf
LOADMOD=/etc/modules
DTBODIR=/boot/overlays
cred=`tput setaf 1`
cyellow=`tput setaf 3`
cgreen=`tput setaf 2`
cblue=`tput setaf 4`
creset=`tput sgr0`
confirm() {
if [ "$FORCE" == '-y' ]; then
true
else
read -r -p "$1 [y/N] " response < /dev/tty
if [[ $response =~ ^(yes|y|Y)$ ]]; then
true
else
false
fi
fi
}
prompt() {
read -r -p "$1 [y/N] " response < /dev/tty
if [[ $response =~ ^(yes|y|Y)$ ]]; then
true
else
false
fi
}
success() {
echo "$(tput setaf 2)$1$(tput sgr0)"
}
warning() {
echo "$(tput setaf 1)$1$(tput sgr0)"
}
newline() {
echo ""
}
sudocheck() {
if [ $(id -u) -ne 0 ]; then
echo -e "Install must be run as root. Try 'sudo ./$scriptname'\n"
exit 1
fi
}
sysclean() {
sudo apt-get clean && sudo apt-get autoclean
sudo apt-get -y autoremove &> /dev/null
}
sysupdate() {
if ! $UPDATE_DB; then
sudo apt-get update
UPDATE_DB=true
fi
}
sysupgrade() {
sudo apt-get update && sudo apt-get upgrade
sudo apt-get clean && sudo apt-get autoclean
sudo apt-get -y autoremove &> /dev/null
}
sysreboot() {
warning "Some changes made to your system require"
warning "your computer to reboot to take effect."
newline
if prompt "Would you like to reboot now?"; then
sync && sudo reboot
fi
}
arch_check() {
IS_ARMHF=false
IS_ARMv6=false
if uname -m | grep "armv.l" > /dev/null; then
IS_ARMHF=true
if uname -m | grep "armv6l" > /dev/null; then
IS_ARMv6=true
fi
fi
}
os_check() {
IS_RASPBIAN=false
IS_MACOSX=false
IS_SUPPORTED=false
IS_EXPERIMENTAL=false
if [ -f /etc/os-release ]; then
if cat /etc/os-release | grep "Raspbian" > /dev/null; then
IS_RASPBIAN=true && IS_SUPPORTED=true
fi
if command -v apt-get > /dev/null; then
for os in ${osreleases[@]}; do
if cat /etc/os-release | grep "$os" > /dev/null; then
IS_SUPPORTED=true
fi
done
for os in ${oswarning[@]}; do
if cat /etc/os-release | grep "$os" > /dev/null; then
IS_EXPERIMENTAL=true
fi
done
fi
elif uname -s | grep "Darwin" > /dev/null; then
IS_MACOSX=true
if [ $macosxsupport == "yes" ]; then
IS_SUPPORTED=true
fi
fi
}
raspbian_check() {
IS_SQUEEZE=false
IS_WHEEZY=false
IS_JESSIE=false
if [ -f /etc/os-release ]; then
if cat /etc/os-release | grep "jessie" > /dev/null; then
IS_JESSIE=true
elif cat /etc/os-release | grep "wheezy" > /dev/null; then
IS_WHEEZY=true
elif cat /etc/os-release | grep "squeeze" > /dev/null; then
IS_SQUEEZE=true
else
echo "Unsupported distribution"
exit 1
fi
fi
}
raspbian_old() {
if $IS_SQUEEZE || $IS_WHEEZY ;then
true
else
false
fi
}
dt_check() {
if [ -e $CONFIG ] && grep -q "^device_tree=$" $CONFIG; then
DEVICE_TREE=false
fi
}
i2c_check() {
if [ -e $CONFIG ] && grep -q -E "^(device_tree_param|dtparam)=([^,]*,)*i2c(_arm)?(=(on|true|yes|1))?(,.*)?$" $CONFIG; then
CURRENT_SETTING=true
else
CURRENT_SETTING=false
fi
}
spi_check() {
if [ -e $CONFIG ] && grep -q -E "^(device_tree_param|dtparam)=([^,]*,)*spi(=(on|true|yes|1))?(,.*)?$" $CONFIG; then
CURRENT_SETTING=true
else
CURRENT_SETTING=false
fi
}
get_init_sys() {
if command -v systemctl > /dev/null && systemctl | grep -q '\-\.mount'; then
SYSTEMD=1
elif [ -f /etc/init.d/cron ] && [ ! -h /etc/init.d/cron ]; then
SYSTEMD=0
else
echo "Unrecognised init system" && exit 1
fi
}
get_hw_rev() {
hwrid=$(grep "^Revision" /proc/cpuinfo | rev | cut -c 2-3 | rev)
if [ "$hwrid" == "00" ] || [ "$hwrid" == "01" ];then # Pi 1
hwver="$(grep "^Revision" /proc/cpuinfo | rev | cut -c 1-4 | rev)"
if [ "$hwrid" == "00" ];then
hwgen="126" # P1
elif [ "$hwrid" == "01" ];then
hwgen="140" # J8
fi
else
hwver="$(grep "^Revision" /proc/cpuinfo | rev | cut -c 1-6 | rev)"
if [ "$hwrid" == "04" ];then # Pi 2
hwgen="240"
elif [ "$hwrid" == "08" ];then # Pi 3
hwgen="340"
elif [ "$hwrid" == "09" ];then # Pi 0
hwgen="040"
else # Unknown
hwgen="000"
fi
fi
}
: <<'MAINSTART'
Perform all global variables declarations as well as function definition
above this section for clarity, thanks!
MAINSTART
dt_check
arch_check
os_check
if ! $IS_ARMHF; then
warning "This hardware is not supported, sorry!"
warning "Config files have been left untouched"
newline && exit 1
fi
if $IS_RASPBIAN; then
raspbian_check
if [ $wheezysupport == "no" ] && raspbian_old; then
newline && warning "--- Warning ---" && newline
echo "The $productname installer"
echo "does not work on this version of Raspbian."
echo "Check https://github.com/$gitusername/$gitreponame"
echo "for additional information and support"
newline && exit 1
fi
elif [ $raspbianonly == "yes" ];then
warning "This script is intended for Raspbian on a Raspberry Pi!"
newline && exit 1
else
if ! $IS_SUPPORTED && ! $IS_EXPERIMENTAL; then
warning "Your operating system is not supported, sorry!"
warning "Config files have been left untouched"
newline && exit 1
fi
fi
if [ $forcesudo == "yes" ]; then
sudocheck
fi
newline
echo "This script will install everything needed to use"
echo "$productname"
newline
warning "--- Warning ---"
newline
echo "Always be careful when running scripts and commands"
echo "copied from the internet. Ensure they are from a"
echo "trusted source."
newline
echo "If you want to see what this script does before"
echo "running it, you should run:"
echo " \curl -sS $baseurl/$scriptname"
newline
if confirm "Do you wish to continue?"; then
newline
echo "Checking hardware requirements..."
if [ $i2creq == "yes" ]; then
newline
if confirm "Hardware requires I2C, enable now?"; then
\curl -sS $baseurl/i2c.sh | bash -s - "-y"
fi
fi
if ! $DEVICE_TREE; then
newline
warning "Device Tree support required!"
warning "Config files have been left untouched"
newline && exit 1
fi
read -r -p "What RTC would you like to install? [ds1307 ds3231 or pcf8523] " response < /dev/tty
if [[ $response == "ds1307" || $response == "ds3231" || $response == "pcf8523" ]]; then
rtcsubtype=$response
else
warning "RTC type $response not supported!"
newline && exit 1
fi
echo "Disabling any current RTC"
sudo sed --in-place '/dtoverlay[[:space:]]*=[[:space:]]*i2c-rtc/d' $CONFIG &> /dev/null
echo "Adding DT overlay for $rtcsubtype"
echo "dtoverlay=i2c-rtc,$rtcsubtype" | sudo tee -a $CONFIG &> /dev/null
echo "Removing fake-hwclock"
sudo apt-get -y remove fake-hwclock
sudo update-rc.d -f fake-hwclock remove
if ! [ -e "/lib/udev/hwclock-set" ]; then
newline
warning "Couldn't find /lib/udev/hwclock-set"
newline && exit 1
fi
echo "${cyellow}Configuring HW Clock ${creset}"
sudo sed --in-place '/if \[ -e \/run\/systemd\/system \] ; then/,+2 s/^#*/#/' /lib/udev/hwclock-set
sysreboot
else
newline
warning "Aborting..."
newline
fi
exit 0