4 .
/lib
/functions
/system.sh
5 .
/usr
/share
/libubox
/jshn.sh
8 CONF_TAR
=/tmp
/sysupgrade.tgz
9 ETCBACKUP_DIR
=/etc
/backup
10 INSTALLED_PACKAGES
=${ETCBACKUP_DIR}/installed_packages.txt
11 COMMAND
=/lib
/upgrade
/do_stage2
27 # Globals accessed in other files
29 export MTD_CONFIG_ARGS
=""
33 export IGNORE_MINOR_COMPAT
=0
35 export CONFFILES
=/tmp
/sysupgrade.conffiles
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;;
55 -h|
--help) HELP
=1; break;;
56 --ignore-minor-compat-version) export IGNORE_MINOR_COMPAT
=1;;
58 echo "Invalid option: $1" >&2
68 Usage: $0 [<upgrade-option>...] <image file or URL>
69 $0 [-q] [-i] [-c] [-u] [-o] [-k] <backup-command> <file>
72 -f <config> restore configuration from .tar.gz (file or url)
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
83 Verify image and config .tar.gz but do not actually flash.
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.
90 -h | --help display this help
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.
102 list the files that would be backed up when calling
103 sysupgrade -b. Does not create a backup file.
110 if [ $HELP -gt 0 ]; then
115 if [ -z "$IMAGE" -a -z "$NEED_IMAGE" -a $CONF_BACKUP_LIST -eq 0 ]; then
120 [ -n "$IMAGE" -a -n "$NEED_IMAGE" ] && {
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.
128 # prevent messages from clobbering the tarball when using stdout
129 [ "$CONF_BACKUP" = "-" ] && export VERBOSE
=0
134 BEGIN { conffiles = 0 }
135 /^Conffiles:/ { conffiles = 1; next }
136 !/^ / { conffiles = 0; next }
137 conffiles == 1 { print }
138 ' /usr
/lib
/opkg
/status
141 list_changed_conffiles
() {
142 # Cannot handle spaces in filenames - but opkg cannot either...
143 list_conffiles |
while read file csum
; do
144 [ -r "$file" ] ||
continue
146 echo "${csum} ${file}" | busybox sha256sum
-sc - ||
echo "$file"
150 list_static_conffiles
() {
153 find $
(sed -ne '/^[[:space:]]*$/d; /^#/d; p' \
154 /etc
/sysupgrade.conf
/lib
/upgrade
/keep.d
/* 2>/dev
/null
) \
155 \
( -type f
-o -type l \
) $filter 2>/dev
/null
161 ( list_static_conffiles
"$find_filter"; list_changed_conffiles
) |
169 local packagesfiles
=$1.packagesfiles
170 touch "$packagesfiles"
172 if [ "$SAVE_OVERLAY_PATH" = / ]; then
173 local conffiles
=$1.conffiles
174 local keepfiles
=$1.keepfiles
176 list_conffiles | cut
-f2 -d ' ' |
sort -u > "$conffiles"
178 # backup files from /etc/sysupgrade.conf and /lib/upgrade/keep.d, but
179 # ignore those aready controlled by opkg conffiles
180 list_static_conffiles |
sort -u |
181 grep -h -v -x -F -f $conffiles > "$keepfiles"
183 # backup conffiles, but only those changed if '-u'
184 [ $SKIP_UNCHANGED = 1 ] &&
185 list_changed_conffiles |
sort -u > "$conffiles"
187 # do not backup files from packages, except those listed
188 # in conffiles and keep.d
190 find /usr
/lib
/opkg
/info
-type f
-name "*.list" -exec cat {} \
;
191 find /usr
/lib
/opkg
/info
-type f
-name "*.control" -exec sed \
192 -ne '/^Alternatives/{s/^Alternatives: //;s/, /\n/g;p}' {} \
; |
194 } |
grep -v -x -F -f $conffiles |
195 grep -v -x -F -f $keepfiles |
sort -u > "$packagesfiles"
196 rm -f "$keepfiles" "$conffiles"
199 # busybox grep bug when file is empty
200 [ -s "$packagesfiles" ] ||
echo > $packagesfiles
202 ( cd /overlay
/upper
/; find .
$SAVE_OVERLAY_PATH \
( -type f
-o -type l \
) $find_filter |
sed \
204 -e '\,^/etc/board.json$,d' \
205 -e '\,/[^/]*-opkg$,d' \
206 -e '\,^/etc/urandom.seed$,d' \
207 -e "\,^$INSTALLED_PACKAGES$,d" \
208 -e '\,^/usr/lib/opkg/.*,d' \
209 ) |
grep -v -x -F -f $packagesfiles > "$file"
211 rm -f "$packagesfiles"
216 if [ $SAVE_OVERLAY = 1 ]; then
217 [ ! -d /overlay
/upper
/etc
] && {
218 echo "Cannot find '/overlay/upper/etc', required for '-c' or '-o'" >&2
221 sysupgrade_init_conffiles
="add_overlayfiles"
223 sysupgrade_init_conffiles
="add_conffiles"
227 if [ $SKIP_UNCHANGED = 1 ]; then
229 echo "'/rom/' is required by '-u'"
232 find_filter
='( ( -exec test -e /rom/{} ; -exec cmp -s /{} /rom/{} ; ) -o -print )'
237 do_save_conffiles
() {
240 local umount_etcbackup_dir
=0
242 [ "$(rootfs_type)" = "tmpfs" ] && {
243 echo "Cannot save config while running from ramdisk." >&2
244 ask_bool
0 "Abort" && exit
248 run_hooks
"$CONFFILES" $sysupgrade_init_conffiles
249 ask_bool
0 "Edit config file list" && vi
"$CONFFILES"
251 if [ "$SAVE_INSTALLED_PKGS" -eq 1 ]; then
252 echo "${INSTALLED_PACKAGES}" >> "$CONFFILES"
253 mkdir
-p "$ETCBACKUP_DIR"
254 # Avoid touching filesystem on each backup
255 RAMFS
="$(mktemp -d -t sysupgrade.XXXXXX)"
256 mkdir
-p "$RAMFS/upper" "$RAMFS/work"
257 mount
-t overlay overlay
-o lowerdir
=$ETCBACKUP_DIR,upperdir
=$RAMFS/upper
,workdir
=$RAMFS/work
$ETCBACKUP_DIR &&
258 umount_etcbackup_dir
=1 ||
{
259 echo "Cannot mount '$ETCBACKUP_DIR' as tmpfs to avoid touching disk while saving the list of installed packages." >&2
260 ask_bool
0 "Abort" && exit
263 # Format: pkg-name<TAB>{rom,overlay,unknown}
264 # rom is used for pkgs in /rom, even if updated later
265 find /usr
/lib
/opkg
/info
-name "*.control" \
( \
266 \
( -exec test -f /rom
/{} \
; -exec echo {} rom \
; \
) -o \
267 \
( -exec test -f /overlay
/upper
/{} \
; -exec echo {} overlay \
; \
) -o \
268 \
( -exec echo {} unknown \
; \
) \
269 \
) |
sed -e 's,.*/,,;s/\.control /\t/' > ${INSTALLED_PACKAGES}
272 v
"Saving config files..."
273 [ "$VERBOSE" -gt 1 ] && TAR_V
="v" || TAR_V
=""
274 sed -i -e 's,^/,,' "$CONFFILES"
275 tar c
${TAR_V}zf
"$conf_tar" -C / -T "$CONFFILES"
277 if [ "$err" -ne 0 ]; then
278 echo "Failed to create the configuration backup."
282 [ "$umount_etcbackup_dir" -eq 1 ] && {
283 umount
"$ETCBACKUP_DIR"
291 if [ $CONF_BACKUP_LIST -eq 1 ]; then
292 run_hooks
"$CONFFILES" $sysupgrade_init_conffiles
293 [ "$SAVE_INSTALLED_PKGS" -eq 1 ] && echo ${INSTALLED_PACKAGES} >> "$CONFFILES"
299 if [ -n "$CONF_BACKUP" ]; then
300 do_save_conffiles
"$CONF_BACKUP"
304 if [ -n "$CONF_RESTORE" ]; then
305 if [ "$CONF_RESTORE" != "-" ] && [ ! -f "$CONF_RESTORE" ]; then
306 echo "Backup archive '$CONF_RESTORE' not found." >&2
310 [ "$VERBOSE" -gt 1 ] && TAR_V
="v" || TAR_V
=""
311 v
"Restoring config files..."
312 tar -C / -x${TAR_V}zf
"$CONF_RESTORE"
316 type platform_check_image
>/dev
/null
2>/dev
/null ||
{
317 echo "Firmware upgrade is not implemented for this platform." >&2
324 wget
-O/tmp
/sysupgrade.img
"$IMAGE" ||
exit 1
325 IMAGE
=/tmp
/sysupgrade.img
329 IMAGE
="$(readlink -f "$IMAGE")"
333 echo "Image file not found." >&2
338 v
"Image not in /tmp, copying..."
339 cp -f "$IMAGE" /tmp
/sysupgrade.img
340 IMAGE
=/tmp
/sysupgrade.img
344 json_load
"$(/usr/libexec/validate_firmware_image "$IMAGE")" ||
{
345 echo "Failed to check image"
348 json_get_var valid
"valid"
349 [ "$valid" -eq 0 ] && {
350 if [ $FORCE -eq 1 ]; then
351 echo "Image check failed but --force given - will update anyway!" >&2
353 echo "Image check failed." >&2
358 if [ -n "$CONF_IMAGE" ]; then
359 case "$(get_magic_word $CONF_IMAGE cat)" in
363 echo "Invalid config file. Please use only .tar.gz files" >&2
367 get_image
"$CONF_IMAGE" "cat" > "$CONF_TAR"
369 elif ask_bool
$SAVE_CONFIG "Keep config files over reflash"; then
370 [ $TEST -eq 1 ] || do_save_conffiles
"$CONF_TAR" ||
exit
373 [ $TEST -eq 1 ] ||
rm -f "$CONF_TAR"
377 if [ $TEST -eq 1 ]; then
381 install_bin
/sbin
/upgraded
382 v
"Commencing upgrade. Closing all shell sessions."
384 if [ -n "$FAILSAFE" ]; then
385 printf '%s\x00%s\x00%s' "$RAM_ROOT" "$IMAGE" "$COMMAND" >/tmp
/sysupgrade
386 lock
-u /tmp
/.failsafe
389 json_add_string prefix
"$RAM_ROOT"
390 json_add_string path
"$IMAGE"
391 [ $FORCE -eq 1 ] && json_add_boolean force
1
392 [ $SAVE_CONFIG -eq 1 ] && json_add_string backup
"$CONF_TAR"
393 json_add_string
command "$COMMAND"
394 json_add_object options
395 json_add_int save_partitions
"$SAVE_PARTITIONS"
398 ubus call system sysupgrade
"$(json_dump)"