1
- #! /bin/sh
1
+ #! /bin/sh
2
+
3
+ # This software originates from Freifunk Berlin and implements a basic autoupdate mechanism
4
+ # by using OpenWrts built-in sysupgrade.
5
+ # It is licensed under GNU General Public License v3.0 or later
6
+ # Copyright (C) 2022 Martin Hübner and Tobias Schwarz
7
+
8
+ # shellcheck shell=dash
2
9
3
10
# except than noted, this script is not posix-compliant in one way: we use "local"
4
11
# variables definition. As nearly all shells out there implement local, this should
5
12
# work anyway. This is a little reminder to you, if you use some rare shell without
6
13
# a builtin "local" statement.
7
14
15
+ # We don't need the return values and check the correct execution in other ways.
16
+ # shellcheck disable=SC2155
17
+
18
+ # we can't check those dependencies at the CI
19
+ # shellcheck source=/dev/null
8
20
. /lib/functions.sh
9
21
. /lib/config/uci.sh
10
22
. /etc/freifunk_release
@@ -54,14 +66,14 @@ Example call:
54
66
# Load Configuration #
55
67
# #########################
56
68
57
- SELECTOR_URL=$( uci_get autoupdate cfg selector_fqdn)
69
+ export SELECTOR_URL=$( uci_get autoupdate cfg selector_fqdn)
58
70
FW_SERVER_URL=$( uci_get autoupdate cfg fw_server_fqdn)
59
71
MIN_CERTS=$( uci_get autoupdate cfg minimum_certs)
60
72
DISABLED=$( uci_get autoupdate cfg disabled)
61
73
62
74
PATH_DIR=" /tmp/autoupdate"
63
75
PATH_BIN=" $PATH_DIR /freifunk_syupgrade.bin"
64
- KEY_DIR=" /etc/autoupdate/keys/"
76
+ export KEY_DIR=" /etc/autoupdate/keys/"
65
77
66
78
MIN_RAM_FREE=1536 # amount of kiB that must be free in RAM after firmware-download
67
79
@@ -107,19 +119,19 @@ log "starting autoupdate..."
107
119
# Checks and Checks again
108
120
109
121
is_stable_release=$( echo " $FREIFUNK_RELEASE " | grep -E ' ^[0-9]+\.[0-9]+\.[0-9]+$' )
110
- if [ -z $OPT_FORCE ] && [ -z " $is_stable_release " ]; then
122
+ if [ -z " $OPT_FORCE " ] && [ -z " $is_stable_release " ]; then
111
123
log " automatic updates aren't supported for development-firmwares. Please update manually or use the force update option '-f' or new-install option '-n'."
112
124
exit 2
113
125
fi
114
126
115
- if [ -z $OPT_FORCE ] && { [ " $DISABLED " = " 1" ] || [ " $DISABLED " = " yes" ] || [ " $DISABLED " = " true" ]; }; then
127
+ if [ -z " $OPT_FORCE " ] && { [ " $DISABLED " = " 1" ] || [ " $DISABLED " = " yes" ] || [ " $DISABLED " = " true" ]; }; then
116
128
log " autoupdate is disabled. Change the configs at /et/config/autoupdate to enable it."
117
129
exit 2
118
130
fi
119
131
120
132
UPTIME=$( cut -d' .' -f1 < /proc/uptime)
121
133
# only update, if router runs for at least two hours (so the update probably won't get disrupted)
122
- if [ -z $OPT_FORCE ] && [ " $UPTIME " -lt 7200 ]; then
134
+ if [ -z " $OPT_FORCE " ] && [ " $UPTIME " -lt 7200 ]; then
123
135
log " Router didn't run for two hours. It might be just plugged in for testing. Aborting..."
124
136
exit 2
125
137
fi
@@ -129,15 +141,14 @@ rm -rf "$PATH_DIR"
129
141
mkdir -p " $PATH_DIR "
130
142
131
143
log " fetch autoupdate.json from $FW_SERVER_URL ..."
132
- load_overview_and_certs " $FW_SERVER_URL "
133
- if [ $? != 0 ]; then
144
+ if load_overview_and_certs " $FW_SERVER_URL " ; then
134
145
log " fetching autoupdate.json failed. Probably no internet connection."
135
146
exit 2
136
147
fi
137
148
log " done."
138
149
139
150
# prove to be signed by minimum amount of certs
140
- if [ -z $OPT_IGNORE_CERTS ]; then
151
+ if [ -z " $OPT_IGNORE_CERTS " ]; then
141
152
log " Verifying image-signatures..."
142
153
min_valid_certificates " $PATH_DIR /autoupdate.json" " $MIN_CERTS "
143
154
ret_code=$?
151
162
log " ignoring certificates as requested."
152
163
fi
153
164
154
- latest_release=$( read_latest_stable " $PATH_DIR /autoupdate.json" )
155
- if [ $? != 0 ]; then
165
+ if latest_release=$( read_latest_stable " $PATH_DIR /autoupdate.json" ) ; then
156
166
log " wasn't able to read latest stable version from autoupdate.json"
157
167
exit 2
158
168
else
@@ -182,7 +192,7 @@ if semverLT "$FREIFUNK_RELEASE" "$latest_release"; then
182
192
log " download link is: $link ."
183
193
184
194
# delete json and signatures to save space in RAM
185
- if [ -z $OPT_TESTRUN ]; then
195
+ if [ -z " $OPT_TESTRUN " ]; then
186
196
json_sig_files=$( find " $PATH_DIR " -name " autoupdate.json*" )
187
197
for f in $json_sig_files ; do
188
198
rm " $f "
@@ -222,12 +232,12 @@ if semverLT "$FREIFUNK_RELEASE" "$latest_release"; then
222
232
fi
223
233
224
234
# flash image
225
- if [ -z $OPT_TESTRUN ]; then
235
+ if [ -z " $OPT_TESTRUN " ]; then
226
236
log " start flashing the image..."
227
237
if [ -n " $OPT_N " ]; then
228
- sysupgrade -n --ignore-minor-compat-version " $PATH_BIN "
238
+ sysupgrade -n " $PATH_BIN "
229
239
else
230
- sysupgrade --ignore-minor-compat-version " $PATH_BIN "
240
+ sysupgrade " $PATH_BIN "
231
241
fi
232
242
log " done."
233
243
fi
0 commit comments