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 [ -z "$IMAGE" -a -z "$NEED_IMAGE" -a $CONF_BACKUP_LIST -eq 0 -o $HELP -gt 0 ] && {
70 Usage: $0 [<upgrade-option>...] <image file or URL>
71 $0 [-q] [-i] [-c] [-u] [-o] [-k] <backup-command> <file>
74 -f <config> restore configuration from .tar.gz (file or url)
76 -c attempt to preserve all changed files in /etc/
77 -o attempt to preserve all changed files in /, except those
78 from packages but including changed confs.
79 -u skip from backup files that are equal to those in /rom
80 -n do not save configuration over reflash
81 -p do not attempt to restore the partition table after flash.
82 -k include in backup a list of current installed packages at
85 Verify image and config .tar.gz but do not actually flash.
87 Flash image even if image checks fail, this is dangerous!
88 --ignore-minor-compat-version
89 Flash image even if the minor compat version is incompatible.
92 -h | --help display this help
95 -b | --create-backup <file>
96 create .tar.gz of files specified in sysupgrade.conf
97 then exit. Does not flash an image. If file is '-',
98 i.e. stdout, verbosity is set to 0 (i.e. quiet).
99 -r | --restore-backup <file>
100 restore a .tar.gz created with sysupgrade -b
101 then exit. Does not flash an image. If file is '-',
102 the archive is read from stdin.
104 list the files that would be backed up when calling
105 sysupgrade -b. Does not create a backup file.
111 [ -n "$IMAGE" -a -n "$NEED_IMAGE" ] && {
113 -b|--create-backup and -r|--restore-backup do not perform a firmware upgrade.
114 Do not specify both -b|-r and a firmware image.
119 # prevent messages from clobbering the tarball when using stdout
120 [ "$CONF_BACKUP" = "-" ] && export VERBOSE
=0
125 BEGIN { conffiles = 0 }
126 /^Conffiles:/ { conffiles = 1; next }
127 !/^ / { conffiles = 0; next }
128 conffiles == 1 { print }
129 ' /usr
/lib
/opkg
/status
132 list_changed_conffiles
() {
133 # Cannot handle spaces in filenames - but opkg cannot either...
134 list_conffiles |
while read file csum
; do
135 [ -r "$file" ] ||
continue
137 echo "${csum} ${file}" | busybox sha256sum
-sc - ||
echo "$file"
141 list_static_conffiles
() {
144 find $
(sed -ne '/^[[:space:]]*$/d; /^#/d; p' \
145 /etc
/sysupgrade.conf
/lib
/upgrade
/keep.d
/* 2>/dev
/null
) \
146 \
( -type f
-o -type l \
) $filter 2>/dev
/null
152 ( list_static_conffiles
"$find_filter"; list_changed_conffiles
) |
160 local packagesfiles
=$1.packagesfiles
161 touch "$packagesfiles"
163 if [ "$SAVE_OVERLAY_PATH" = / ]; then
164 local conffiles
=$1.conffiles
165 local keepfiles
=$1.keepfiles
167 list_conffiles | cut
-f2 -d ' ' |
sort -u > "$conffiles"
169 # backup files from /etc/sysupgrade.conf and /lib/upgrade/keep.d, but
170 # ignore those aready controlled by opkg conffiles
171 list_static_conffiles |
sort -u |
172 grep -h -v -x -F -f $conffiles > "$keepfiles"
174 # backup conffiles, but only those changed if '-u'
175 [ $SKIP_UNCHANGED = 1 ] &&
176 list_changed_conffiles |
sort -u > "$conffiles"
178 # do not backup files from packages, except those listed
179 # in conffiles and keep.d
181 find /usr
/lib
/opkg
/info
-type f
-name "*.list" -exec cat {} \
;
182 find /usr
/lib
/opkg
/info
-type f
-name "*.control" -exec sed \
183 -ne '/^Alternatives/{s/^Alternatives: //;s/, /\n/g;p}' {} \
; |
185 } |
grep -v -x -F -f $conffiles |
186 grep -v -x -F -f $keepfiles |
sort -u > "$packagesfiles"
187 rm -f "$keepfiles" "$conffiles"
190 # busybox grep bug when file is empty
191 [ -s "$packagesfiles" ] ||
echo > $packagesfiles
193 ( cd /overlay
/upper
/; find .
$SAVE_OVERLAY_PATH \
( -type f
-o -type l \
) $find_filter |
sed \
195 -e '\,^/etc/board.json$,d' \
196 -e '\,/[^/]*-opkg$,d' \
197 -e '\,^/etc/urandom.seed$,d' \
198 -e "\,^$INSTALLED_PACKAGES$,d" \
199 -e '\,^/usr/lib/opkg/.*,d' \
200 ) |
grep -v -x -F -f $packagesfiles > "$file"
202 rm -f "$packagesfiles"
207 if [ $SAVE_OVERLAY = 1 ]; then
208 [ ! -d /overlay
/upper
/etc
] && {
209 echo "Cannot find '/overlay/upper/etc', required for '-c' or '-o'" >&2
212 sysupgrade_init_conffiles
="add_overlayfiles"
214 sysupgrade_init_conffiles
="add_conffiles"
218 if [ $SKIP_UNCHANGED = 1 ]; then
220 echo "'/rom/' is required by '-u'"
223 find_filter
='( ( -exec test -e /rom/{} ; -exec cmp -s /{} /rom/{} ; ) -o -print )'
228 do_save_conffiles
() {
231 local umount_etcbackup_dir
=0
233 [ "$(rootfs_type)" = "tmpfs" ] && {
234 echo "Cannot save config while running from ramdisk." >&2
235 ask_bool
0 "Abort" && exit
239 run_hooks
"$CONFFILES" $sysupgrade_init_conffiles
240 ask_bool
0 "Edit config file list" && vi
"$CONFFILES"
242 if [ "$SAVE_INSTALLED_PKGS" -eq 1 ]; then
243 echo "${INSTALLED_PACKAGES}" >> "$CONFFILES"
244 mkdir
-p "$ETCBACKUP_DIR"
245 # Avoid touching filesystem on each backup
246 RAMFS
="$(mktemp -d -t sysupgrade.XXXXXX)"
247 mkdir
-p "$RAMFS/upper" "$RAMFS/work"
248 mount
-t overlay overlay
-o lowerdir
=$ETCBACKUP_DIR,upperdir
=$RAMFS/upper
,workdir
=$RAMFS/work
$ETCBACKUP_DIR &&
249 umount_etcbackup_dir
=1 ||
{
250 echo "Cannot mount '$ETCBACKUP_DIR' as tmpfs to avoid touching disk while saving the list of installed packages." >&2
251 ask_bool
0 "Abort" && exit
254 # Format: pkg-name<TAB>{rom,overlay,unknown}
255 # rom is used for pkgs in /rom, even if updated later
256 find /usr
/lib
/opkg
/info
-name "*.control" \
( \
257 \
( -exec test -f /rom
/{} \
; -exec echo {} rom \
; \
) -o \
258 \
( -exec test -f /overlay
/upper
/{} \
; -exec echo {} overlay \
; \
) -o \
259 \
( -exec echo {} unknown \
; \
) \
260 \
) |
sed -e 's,.*/,,;s/\.control /\t/' > ${INSTALLED_PACKAGES}
263 v
"Saving config files..."
264 [ "$VERBOSE" -gt 1 ] && TAR_V
="v" || TAR_V
=""
265 sed -i -e 's,^/,,' "$CONFFILES"
266 tar c
${TAR_V}zf
"$conf_tar" -C / -T "$CONFFILES"
268 if [ "$err" -ne 0 ]; then
269 echo "Failed to create the configuration backup."
273 [ "$umount_etcbackup_dir" -eq 1 ] && {
274 umount
"$ETCBACKUP_DIR"
282 if [ $CONF_BACKUP_LIST -eq 1 ]; then
283 run_hooks
"$CONFFILES" $sysupgrade_init_conffiles
284 [ "$SAVE_INSTALLED_PKGS" -eq 1 ] && echo ${INSTALLED_PACKAGES} >> "$CONFFILES"
290 if [ -n "$CONF_BACKUP" ]; then
291 do_save_conffiles
"$CONF_BACKUP"
295 if [ -n "$CONF_RESTORE" ]; then
296 if [ "$CONF_RESTORE" != "-" ] && [ ! -f "$CONF_RESTORE" ]; then
297 echo "Backup archive '$CONF_RESTORE' not found." >&2
301 [ "$VERBOSE" -gt 1 ] && TAR_V
="v" || TAR_V
=""
302 v
"Restoring config files..."
303 tar -C / -x${TAR_V}zf
"$CONF_RESTORE"
307 type platform_check_image
>/dev
/null
2>/dev
/null ||
{
308 echo "Firmware upgrade is not implemented for this platform." >&2
315 wget
-O/tmp
/sysupgrade.img
"$IMAGE" ||
exit 1
316 IMAGE
=/tmp
/sysupgrade.img
320 IMAGE
="$(readlink -f "$IMAGE")"
324 echo "Image file not found." >&2
329 v
"Image not in /tmp, copying..."
330 cp -f "$IMAGE" /tmp
/sysupgrade.img
331 IMAGE
=/tmp
/sysupgrade.img
335 json_load
"$(/usr/libexec/validate_firmware_image "$IMAGE")" ||
{
336 echo "Failed to check image"
339 json_get_var valid
"valid"
340 [ "$valid" -eq 0 ] && {
341 if [ $FORCE -eq 1 ]; then
342 echo "Image check failed but --force given - will update anyway!" >&2
344 echo "Image check failed." >&2
349 if [ -n "$CONF_IMAGE" ]; then
350 case "$(get_magic_word $CONF_IMAGE cat)" in
354 echo "Invalid config file. Please use only .tar.gz files" >&2
358 get_image
"$CONF_IMAGE" "cat" > "$CONF_TAR"
360 elif ask_bool
$SAVE_CONFIG "Keep config files over reflash"; then
361 [ $TEST -eq 1 ] || do_save_conffiles
"$CONF_TAR" ||
exit
364 [ $TEST -eq 1 ] ||
rm -f "$CONF_TAR"
368 if [ $TEST -eq 1 ]; then
372 install_bin
/sbin
/upgraded
373 v
"Commencing upgrade. Closing all shell sessions."
375 if [ -n "$FAILSAFE" ]; then
376 printf '%s\x00%s\x00%s' "$RAM_ROOT" "$IMAGE" "$COMMAND" >/tmp
/sysupgrade
377 lock
-u /tmp
/.failsafe
380 json_add_string prefix
"$RAM_ROOT"
381 json_add_string path
"$IMAGE"
382 [ $FORCE -eq 1 ] && json_add_boolean force
1
383 [ $SAVE_CONFIG -eq 1 ] && json_add_string backup
"$CONF_TAR"
384 json_add_string
command "$COMMAND"
385 json_add_object options
386 json_add_int save_partitions
"$SAVE_PARTITIONS"
389 ubus call system sysupgrade
"$(json_dump)"