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
29
310# except than noted, this script is not posix-compliant in one way: we use "local"
411# variables definition. As nearly all shells out there implement local, this should
512# work anyway. This is a little reminder to you, if you use some rare shell without
613# a builtin "local" statement.
714
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
820. /lib/functions.sh
921. /lib/config/uci.sh
1022. /etc/freifunk_release
@@ -54,14 +66,14 @@ Example call:
5466# Load Configuration #
5567# #########################
5668
57- SELECTOR_URL=$( uci_get autoupdate cfg selector_fqdn)
69+ export SELECTOR_URL=$( uci_get autoupdate cfg selector_fqdn)
5870FW_SERVER_URL=$( uci_get autoupdate cfg fw_server_fqdn)
5971MIN_CERTS=$( uci_get autoupdate cfg minimum_certs)
6072DISABLED=$( uci_get autoupdate cfg disabled)
6173
6274PATH_DIR=" /tmp/autoupdate"
6375PATH_BIN=" $PATH_DIR /freifunk_syupgrade.bin"
64- KEY_DIR=" /etc/autoupdate/keys/"
76+ export KEY_DIR=" /etc/autoupdate/keys/"
6577
6678MIN_RAM_FREE=1536 # amount of kiB that must be free in RAM after firmware-download
6779
@@ -107,19 +119,19 @@ log "starting autoupdate..."
107119# Checks and Checks again
108120
109121is_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
111123 log " automatic updates aren't supported for development-firmwares. Please update manually or use the force update option '-f' or new-install option '-n'."
112124 exit 2
113125fi
114126
115- if [ -z $OPT_FORCE ] && { [ " $DISABLED " = " 1" ] || [ " $DISABLED " = " yes" ] || [ " $DISABLED " = " true" ]; }; then
127+ if [ -z " $OPT_FORCE " ] && { [ " $DISABLED " = " 1" ] || [ " $DISABLED " = " yes" ] || [ " $DISABLED " = " true" ]; }; then
116128 log " autoupdate is disabled. Change the configs at /et/config/autoupdate to enable it."
117129 exit 2
118130fi
119131
120132UPTIME=$( cut -d' .' -f1 < /proc/uptime)
121133# 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
123135 log " Router didn't run for two hours. It might be just plugged in for testing. Aborting..."
124136 exit 2
125137fi
@@ -129,15 +141,14 @@ rm -rf "$PATH_DIR"
129141mkdir -p " $PATH_DIR "
130142
131143log " 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
134145 log " fetching autoupdate.json failed. Probably no internet connection."
135146 exit 2
136147fi
137148log " done."
138149
139150# prove to be signed by minimum amount of certs
140- if [ -z $OPT_IGNORE_CERTS ]; then
151+ if [ -z " $OPT_IGNORE_CERTS " ]; then
141152 log " Verifying image-signatures..."
142153 min_valid_certificates " $PATH_DIR /autoupdate.json" " $MIN_CERTS "
143154 ret_code=$?
151162 log " ignoring certificates as requested."
152163fi
153164
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
156166 log " wasn't able to read latest stable version from autoupdate.json"
157167 exit 2
158168else
@@ -182,7 +192,7 @@ if semverLT "$FREIFUNK_RELEASE" "$latest_release"; then
182192 log " download link is: $link ."
183193
184194 # delete json and signatures to save space in RAM
185- if [ -z $OPT_TESTRUN ]; then
195+ if [ -z " $OPT_TESTRUN " ]; then
186196 json_sig_files=$( find " $PATH_DIR " -name " autoupdate.json*" )
187197 for f in $json_sig_files ; do
188198 rm " $f "
@@ -222,12 +232,18 @@ if semverLT "$FREIFUNK_RELEASE" "$latest_release"; then
222232 fi
223233
224234 # flash image
225- if [ -z $OPT_TESTRUN ]; then
235+ if [ -z " $OPT_TESTRUN " ]; then
226236 log " start flashing the image..."
227237 if [ -n " $OPT_N " ]; then
228238 sysupgrade -n " $PATH_BIN "
229239 else
230- sysupgrade " $PATH_BIN "
240+ check_ignore_minor_compat
241+ ret_code=$?
242+ if [ $ret_code = 0 ]; then
243+ sysupgrade --ignore-minor-compat-version " $PATH_BIN "
244+ else
245+ sysupgrade " $PATH_BIN "
246+ fi
231247 fi
232248 log " done."
233249 fi
0 commit comments