blob: 611d8830bfd1ffea4ee84d931cda1f3984885b96 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001#!/bin/sh
2
3. /lib/functions.sh
4. /lib/functions/system.sh
5. /usr/share/libubox/jshn.sh
6
7# File-local constants
8CONF_TAR=/tmp/sysupgrade.tgz
9ETCBACKUP_DIR=/etc/backup
10INSTALLED_PACKAGES=${ETCBACKUP_DIR}/installed_packages.txt
11COMMAND=/lib/upgrade/do_stage2
12
13# File-local globals
14SAVE_OVERLAY=0
15SAVE_OVERLAY_PATH=
16SAVE_PARTITIONS=1
17SAVE_INSTALLED_PKGS=0
18SKIP_UNCHANGED=0
19CONF_IMAGE=
20CONF_BACKUP_LIST=0
21CONF_BACKUP=
22CONF_RESTORE=
23NEED_IMAGE=
24HELP=0
25TEST=0
26
27# Globals accessed in other files
28export MTD_ARGS=""
29export MTD_CONFIG_ARGS=""
30export INTERACTIVE=0
31export VERBOSE=1
32export SAVE_CONFIG=1
33export IGNORE_MINOR_COMPAT=0
34export FORCE=0
35export CONFFILES=/tmp/sysupgrade.conffiles
36
37# parse options
38while [ -n "$1" ]; do
39 case "$1" in
40 -i) export INTERACTIVE=1;;
41 -v) export VERBOSE="$(($VERBOSE + 1))";;
42 -q) export VERBOSE="$(($VERBOSE - 1))";;
43 -n) export SAVE_CONFIG=0;;
44 -c) SAVE_OVERLAY=1 SAVE_OVERLAY_PATH=/etc;;
45 -o) SAVE_OVERLAY=1 SAVE_OVERLAY_PATH=/;;
46 -p) SAVE_PARTITIONS=0;;
47 -k) SAVE_INSTALLED_PKGS=1;;
48 -u) SKIP_UNCHANGED=1;;
49 -b|--create-backup) CONF_BACKUP="$2" NEED_IMAGE=1; shift;;
50 -r|--restore-backup) CONF_RESTORE="$2" NEED_IMAGE=1; shift;;
51 -l|--list-backup) CONF_BACKUP_LIST=1;;
52 -f) CONF_IMAGE="$2"; shift;;
53 -F|--force) export FORCE=1;;
54 -T|--test) TEST=1;;
55 -h|--help) HELP=1; break;;
56 --ignore-minor-compat-version) export IGNORE_MINOR_COMPAT=1;;
57 -*)
58 echo "Invalid option: $1" >&2
59 exit 1
60 ;;
61 *) break;;
62 esac
63 shift;
64done
65
66print_help() {
67 cat <<EOF
68Usage: $0 [<upgrade-option>...] <image file or URL>
69 $0 [-q] [-i] [-c] [-u] [-o] [-k] <backup-command> <file>
70
71upgrade-option:
72 -f <config> restore configuration from .tar.gz (file or url)
73 -i interactive mode
74 -c attempt to preserve all changed files in /etc/
75 -o attempt to preserve all changed files in /, except those
76 from packages but including changed confs.
77 -u skip from backup files that are equal to those in /rom
78 -n do not save configuration over reflash
79 -p do not attempt to restore the partition table after flash.
80 -k include in backup a list of current installed packages at
81 $INSTALLED_PACKAGES
82 -T | --test
83 Verify image and config .tar.gz but do not actually flash.
84 -F | --force
85 Flash image even if image checks fail, this is dangerous!
86 --ignore-minor-compat-version
87 Flash image even if the minor compat version is incompatible.
88 -q less verbose
89 -v more verbose
90 -h | --help display this help
91
92backup-command:
93 -b | --create-backup <file>
94 create .tar.gz of files specified in sysupgrade.conf
95 then exit. Does not flash an image. If file is '-',
96 i.e. stdout, verbosity is set to 0 (i.e. quiet).
97 -r | --restore-backup <file>
98 restore a .tar.gz created with sysupgrade -b
99 then exit. Does not flash an image. If file is '-',
100 the archive is read from stdin.
101 -l | --list-backup
102 list the files that would be backed up when calling
103 sysupgrade -b. Does not create a backup file.
104
105EOF
106}
107
108IMAGE="$1"
109
110if [ $HELP -gt 0 ]; then
111 print_help
112 exit 0
113fi
114
115if [ -z "$IMAGE" -a -z "$NEED_IMAGE" -a $CONF_BACKUP_LIST -eq 0 ]; then
116 print_help
117 exit 1
118fi
119
120[ -n "$IMAGE" -a -n "$NEED_IMAGE" ] && {
121 cat <<-EOF
122 -b|--create-backup and -r|--restore-backup do not perform a firmware upgrade.
123 Do not specify both -b|-r and a firmware image.
124 EOF
125 exit 1
126}
127
128# prevent messages from clobbering the tarball when using stdout
129[ "$CONF_BACKUP" = "-" ] && export VERBOSE=0
130
131
132list_conffiles() {
133 if [ -f /usr/lib/opkg/status ]; then
134 awk '
135 BEGIN { conffiles = 0 }
136 /^Conffiles:/ { conffiles = 1; next }
137 !/^ / { conffiles = 0; next }
138 conffiles == 1 { print }
139 ' /usr/lib/opkg/status
140 elif [ -d /lib/apk/packages ]; then
141 conffiles=""
142 for file in /lib/apk/packages/*.conffiles_static; do
143 conffiles="$(echo -e "$(cat $file)\n$conffiles")"
144 done
145 echo "$conffiles"
146 fi
147}
148
149list_changed_conffiles() {
150 # Cannot handle spaces in filenames - but opkg cannot either...
151 list_conffiles | while read file csum; do
152 [ -r "$file" ] || continue
153
154 echo "${csum} ${file}" | busybox sha256sum -sc - || echo "$file"
155 done
156}
157
158list_static_conffiles() {
159 local filter=$1
160
161 find $(sed -ne '/^[[:space:]]*$/d; /^#/d; p' \
162 /etc/sysupgrade.conf /lib/upgrade/keep.d/* 2>/dev/null) \
163 \( -type f -o -type l \) $filter 2>/dev/null
164}
165
166build_list_of_backup_config_files() {
167 local file="$1"
168
169 ( list_static_conffiles "$find_filter"; list_changed_conffiles ) |
170 sort -u > "$file"
171 return 0
172}
173
174build_list_of_backup_overlay_files() {
175 local file="$1"
176
177 local packagesfiles=$1.packagesfiles
178 touch "$packagesfiles"
179
180 if [ "$SAVE_OVERLAY_PATH" = / ]; then
181 local conffiles=$1.conffiles
182 local keepfiles=$1.keepfiles
183
184 list_conffiles | cut -f2 -d ' ' | sort -u > "$conffiles"
185
186 # backup files from /etc/sysupgrade.conf and /lib/upgrade/keep.d, but
187 # ignore those aready controlled by opkg conffiles
188 list_static_conffiles | sort -u |
189 grep -h -v -x -F -f $conffiles > "$keepfiles"
190
191 # backup conffiles, but only those changed if '-u'
192 [ $SKIP_UNCHANGED = 1 ] &&
193 list_changed_conffiles | sort -u > "$conffiles"
194
195 # do not backup files from packages, except those listed
196 # in conffiles and keep.d
197 {
198 find /usr/lib/opkg/info -type f -name "*.list" -exec cat {} \;
199 find /usr/lib/opkg/info -type f -name "*.control" -exec sed \
200 -ne '/^Alternatives/{s/^Alternatives: //;s/, /\n/g;p}' {} \; |
201 cut -f2 -d:
202 } | grep -v -x -F -f $conffiles |
203 grep -v -x -F -f $keepfiles | sort -u > "$packagesfiles"
204 rm -f "$keepfiles" "$conffiles"
205 fi
206
207 # busybox grep bug when file is empty
208 [ -s "$packagesfiles" ] || echo > $packagesfiles
209
210 ( cd /overlay/upper/; find .$SAVE_OVERLAY_PATH \( -type f -o -type l \) $find_filter | sed \
211 -e 's,^\.,,' \
212 -e '\,^/etc/board.json$,d' \
213 -e '\,/[^/]*-opkg$,d' \
214 -e '\,^/etc/urandom.seed$,d' \
215 -e "\,^$INSTALLED_PACKAGES$,d" \
216 -e '\,^/usr/lib/opkg/.*,d' \
217 ) | grep -v -x -F -f $packagesfiles > "$file"
218
219 rm -f "$packagesfiles"
220
221 return 0
222}
223
224if [ $SAVE_OVERLAY = 1 ]; then
225 [ ! -d /overlay/upper/etc ] && {
226 echo "Cannot find '/overlay/upper/etc', required for '-c' or '-o'" >&2
227 exit 1
228 }
229 sysupgrade_init_conffiles="build_list_of_backup_overlay_files"
230else
231 sysupgrade_init_conffiles="build_list_of_backup_config_files"
232fi
233
234find_filter=""
235if [ $SKIP_UNCHANGED = 1 ]; then
236 [ ! -d /rom/ ] && {
237 echo "'/rom/' is required by '-u'"
238 exit 1
239 }
240 find_filter='( ( -exec test -e /rom/{} ; -exec cmp -s /{} /rom/{} ; ) -o -print )'
241fi
242
243include /lib/upgrade
244
245create_backup_archive() {
246 local conf_tar="$1"
247 local disabled
248 local err
249
250 [ "$(rootfs_type)" = "tmpfs" ] && {
251 echo "Cannot save config while running from ramdisk." >&2
252 ask_bool 0 "Abort" && exit
253 return 0
254 }
255 run_hooks "$CONFFILES" $sysupgrade_init_conffiles
256 ask_bool 0 "Edit config file list" && vi "$CONFFILES"
257
258 [ "$conf_tar" != "-" ] || conf_tar=""
259
260 v "Saving config files..."
261 [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
262 sed -i -e 's,^/,,' "$CONFFILES"
263 set -o pipefail
264 {
265 local ret=0
266
267 if [ $ret -eq 0 ]; then
268 for service in /etc/init.d/*; do
269 if ! $service enabled; then
270 disabled="$disabled$service disable\n"
271 fi
272 done
273 disabled="$disabled\nexit 0"
274 tar_print_member "/etc/uci-defaults/10_disable_services" "$(echo -e $disabled)" || ret=1
275 fi
276
277 # Part of archive with installed packages info
278 if [ $ret -eq 0 ]; then
279 if [ "$SAVE_INSTALLED_PKGS" -eq 1 ]; then
280 # Format: pkg-name<TAB>{rom,overlay,unknown}
281 # rom is used for pkgs in /rom, even if updated later
282 tar_print_member "$INSTALLED_PACKAGES" "$(find /usr/lib/opkg/info -name "*.control" \( \
283 \( -exec test -f /rom/{} \; -exec echo {} rom \; \) -o \
284 \( -exec test -f /overlay/upper/{} \; -exec echo {} overlay \; \) -o \
285 \( -exec echo {} unknown \; \) \
286 \) | sed -e 's,.*/,,;s/\.control /\t/')" || ret=1
287 fi
288 fi
289
290 # Rest of archive with config files and ending padding
291 if [ $ret -eq 0 ]; then
292 tar c${TAR_V} -C / -T "$CONFFILES" || ret=1
293 fi
294
295 [ $ret -eq 0 ]
296 } | gzip > "${conf_tar:-/proc/self/fd/1}"
297 err=$?
298 set +o pipefail
299
300 if [ "$err" -ne 0 ]; then
301 echo "Failed to create the configuration backup."
302 [ -f "$conf_tar" ] && rm -f "$conf_tar"
303 fi
304
305 rm -f "$CONFFILES"
306
307 return "$err"
308}
309
310if [ $CONF_BACKUP_LIST -eq 1 ]; then
311 run_hooks "$CONFFILES" $sysupgrade_init_conffiles
312 [ "$SAVE_INSTALLED_PKGS" -eq 1 ] && echo ${INSTALLED_PACKAGES} >> "$CONFFILES"
313 cat "$CONFFILES"
314 rm -f "$CONFFILES"
315 exit 0
316fi
317
318if [ -n "$CONF_BACKUP" ]; then
319 create_backup_archive "$CONF_BACKUP"
320 exit
321fi
322
323if [ -n "$CONF_RESTORE" ]; then
324 if [ "$CONF_RESTORE" != "-" ] && [ ! -f "$CONF_RESTORE" ]; then
325 echo "Backup archive '$CONF_RESTORE' not found." >&2
326 exit 1
327 fi
328
329 [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
330 v "Restoring config files..."
331 if [ "$(type -t platform_restore_backup)" == 'platform_restore_backup' ]; then
332 platform_restore_backup "$TAR_V"
333 else
334 tar -C / -x${TAR_V}zf "$CONF_RESTORE"
335 fi
336 exit $?
337fi
338
339type platform_check_image >/dev/null 2>/dev/null || {
340 echo "Firmware upgrade is not implemented for this platform." >&2
341 exit 1
342}
343
344case "$IMAGE" in
345 http://*|\
346 https://*)
347 wget -O/tmp/sysupgrade.img "$IMAGE" || exit 1
348 IMAGE=/tmp/sysupgrade.img
349 ;;
350esac
351
352IMAGE="$(readlink -f "$IMAGE")"
353
354case "$IMAGE" in
355 '')
356 echo "Image file not found." >&2
357 exit 1
358 ;;
359 /tmp/*) ;;
360 *)
361 v "Image not in /tmp, copying..."
362 cp -f "$IMAGE" /tmp/sysupgrade.img
363 IMAGE=/tmp/sysupgrade.img
364 ;;
365esac
366
367json_load "$(/usr/libexec/validate_firmware_image "$IMAGE")" || {
368 echo "Failed to check image"
369 exit 1
370}
371json_get_var valid "valid"
372[ "$valid" -eq 0 ] && {
373 if [ $FORCE -eq 1 ]; then
374 echo "Image check failed but --force given - will update anyway!" >&2
375 else
376 echo "Image check failed." >&2
377 exit 1
378 fi
379}
380
381if [ -n "$CONF_IMAGE" ]; then
382 case "$(get_magic_word $CONF_IMAGE cat)" in
383 # .gz files
384 1f8b) ;;
385 *)
386 echo "Invalid config file. Please use only .tar.gz files" >&2
387 exit 1
388 ;;
389 esac
390 get_image "$CONF_IMAGE" "cat" > "$CONF_TAR"
391 export SAVE_CONFIG=1
392elif ask_bool $SAVE_CONFIG "Keep config files over reflash"; then
393 [ $TEST -eq 1 ] || create_backup_archive "$CONF_TAR" || exit
394 export SAVE_CONFIG=1
395else
396 [ $TEST -eq 1 ] || rm -f "$CONF_TAR"
397 export SAVE_CONFIG=0
398fi
399
400if [ $TEST -eq 1 ]; then
401 exit 0
402fi
403
404install_bin /sbin/upgraded
405v "Commencing upgrade. Closing all shell sessions."
406
407if [ -n "$FAILSAFE" ]; then
408 printf '%s\x00%s\x00%s' "$RAM_ROOT" "$IMAGE" "$COMMAND" >/tmp/sysupgrade
409 lock -u /tmp/.failsafe
410else
411 json_init
412 json_add_string prefix "$RAM_ROOT"
413 json_add_string path "$IMAGE"
414 [ $FORCE -eq 1 ] && json_add_boolean force 1
415 [ $SAVE_CONFIG -eq 1 ] && json_add_string backup "$CONF_TAR"
416 json_add_string command "$COMMAND"
417 json_add_object options
418 json_add_int save_partitions "$SAVE_PARTITIONS"
419 json_close_object
420
421 ubus call system sysupgrade "$(json_dump)"
422fi