4 .
/lib
/functions
/system.sh
5 .
/usr
/share
/libubox
/jshn.sh
9 export MTD_CONFIG_ARGS
=""
14 export SAVE_OVERLAY_PATH
=
15 export SAVE_PARTITIONS
=1
16 export SAVE_INSTALLED_PKGS
=0
17 export SKIP_UNCHANGED
=0
19 export CONF_BACKUP_LIST
=0
22 export IGNORE_MINOR_COMPAT
=0
31 -i) export INTERACTIVE
=1;;
32 -v) export VERBOSE
="$(($VERBOSE + 1))";;
33 -q) export VERBOSE
="$(($VERBOSE - 1))";;
34 -n) export SAVE_CONFIG
=0;;
35 -c) export SAVE_OVERLAY
=1 SAVE_OVERLAY_PATH
=/etc
;;
36 -o) export SAVE_OVERLAY
=1 SAVE_OVERLAY_PATH
=/;;
37 -p) export SAVE_PARTITIONS
=0;;
38 -k) export SAVE_INSTALLED_PKGS
=1;;
39 -u) export SKIP_UNCHANGED
=1;;
40 -b|
--create-backup) export CONF_BACKUP
="$2" NEED_IMAGE
=1; shift;;
41 -r|
--restore-backup) export CONF_RESTORE
="$2" NEED_IMAGE
=1; shift;;
42 -l|
--list-backup) export CONF_BACKUP_LIST
=1;;
43 -f) export CONF_IMAGE
="$2"; shift;;
44 -F|
--force) export FORCE
=1;;
45 -T|
--test) export TEST
=1;;
46 -h|
--help) export HELP
=1; break;;
47 --ignore-minor-compat-version) export IGNORE_MINOR_COMPAT
=1;;
49 echo "Invalid option: $1" >&2
57 export CONFFILES
=/tmp
/sysupgrade.conffiles
58 export CONF_TAR
=/tmp
/sysupgrade.tgz
59 export ETCBACKUP_DIR
=/etc
/backup
60 export INSTALLED_PACKAGES
=${ETCBACKUP_DIR}/installed_packages.txt
64 [ -z "$IMAGE" -a -z "$NEED_IMAGE" -a $CONF_BACKUP_LIST -eq 0 -o $HELP -gt 0 ] && {
66 Usage: $0 [<upgrade-option>...] <image file or URL>
67 $0 [-q] [-i] [-c] [-u] [-o] [-k] <backup-command> <file>
70 -f <config> restore configuration from .tar.gz (file or url)
72 -c attempt to preserve all changed files in /etc/
73 -o attempt to preserve all changed files in /, except those
74 from packages but including changed confs.
75 -u skip from backup files that are equal to those in /rom
76 -n do not save configuration over reflash
77 -p do not attempt to restore the partition table after flash.
78 -k include in backup a list of current installed packages at
81 Verify image and config .tar.gz but do not actually flash.
83 Flash image even if image checks fail, this is dangerous!
84 --ignore-minor-compat-version
85 Flash image even if the minor compat version is incompatible.
88 -h | --help display this help
91 -b | --create-backup <file>
92 create .tar.gz of files specified in sysupgrade.conf
93 then exit. Does not flash an image. If file is '-',
94 i.e. stdout, verbosity is set to 0 (i.e. quiet).
95 -r | --restore-backup <file>
96 restore a .tar.gz created with sysupgrade -b
97 then exit. Does not flash an image. If file is '-',
98 the archive is read from stdin.
100 list the files that would be backed up when calling
101 sysupgrade -b. Does not create a backup file.
107 [ -n "$IMAGE" -a -n "$NEED_IMAGE" ] && {
109 -b|--create-backup and -r|--restore-backup do not perform a firmware upgrade.
110 Do not specify both -b|-r and a firmware image.
115 # prevent messages from clobbering the tarball when using stdout
116 [ "$CONF_BACKUP" = "-" ] && export VERBOSE
=0
121 BEGIN { conffiles = 0 }
122 /^Conffiles:/ { conffiles = 1; next }
123 !/^ / { conffiles = 0; next }
124 conffiles == 1 { print }
125 ' /usr
/lib
/opkg
/status
128 list_changed_conffiles
() {
129 # Cannot handle spaces in filenames - but opkg cannot either...
130 list_conffiles |
while read file csum
; do
131 [ -r "$file" ] ||
continue
133 echo "${csum} ${file}" | busybox sha256sum
-sc - ||
echo "$file"
137 list_static_conffiles
() {
140 find $
(sed -ne '/^[[:space:]]*$/d; /^#/d; p' \
141 /etc
/sysupgrade.conf
/lib
/upgrade
/keep.d
/* 2>/dev
/null
) \
142 \
( -type f
-o -type l \
) $filter 2>/dev
/null
148 ( list_static_conffiles
"$find_filter"; list_changed_conffiles
) |
156 local packagesfiles
=$1.packagesfiles
157 touch "$packagesfiles"
159 if [ "$SAVE_OVERLAY_PATH" = / ]; then
160 local conffiles
=$1.conffiles
161 local keepfiles
=$1.keepfiles
163 list_conffiles | cut
-f2 -d ' ' |
sort -u > "$conffiles"
165 # backup files from /etc/sysupgrade.conf and /lib/upgrade/keep.d, but
166 # ignore those aready controlled by opkg conffiles
167 list_static_conffiles |
sort -u |
168 grep -h -v -x -F -f $conffiles > "$keepfiles"
170 # backup conffiles, but only those changed if '-u'
171 [ $SKIP_UNCHANGED = 1 ] &&
172 list_changed_conffiles |
sort -u > "$conffiles"
174 # do not backup files from packages, except those listed
175 # in conffiles and keep.d
177 find /usr
/lib
/opkg
/info
-type f
-name "*.list" -exec cat {} \
;
178 find /usr
/lib
/opkg
/info
-type f
-name "*.control" -exec sed \
179 -ne '/^Alternatives/{s/^Alternatives: //;s/, /\n/g;p}' {} \
; |
181 } |
grep -v -x -F -f $conffiles |
182 grep -v -x -F -f $keepfiles |
sort -u > "$packagesfiles"
183 rm -f "$keepfiles" "$conffiles"
186 # busybox grep bug when file is empty
187 [ -s "$packagesfiles" ] ||
echo > $packagesfiles
189 ( cd /overlay
/upper
/; find .
$SAVE_OVERLAY_PATH \
( -type f
-o -type l \
) $find_filter |
sed \
191 -e '\,^/etc/board.json$,d' \
192 -e '\,/[^/]*-opkg$,d' \
193 -e '\,^/etc/urandom.seed$,d' \
194 -e "\,^$INSTALLED_PACKAGES$,d" \
195 -e '\,^/usr/lib/opkg/.*,d' \
196 ) |
grep -v -x -F -f $packagesfiles > "$file"
198 rm -f "$packagesfiles"
203 if [ $SAVE_OVERLAY = 1 ]; then
204 [ ! -d /overlay
/upper
/etc
] && {
205 echo "Cannot find '/overlay/upper/etc', required for '-c'" >&2
208 sysupgrade_init_conffiles
="add_overlayfiles"
210 sysupgrade_init_conffiles
="add_conffiles"
214 if [ $SKIP_UNCHANGED = 1 ]; then
216 echo "'/rom/' is required by '-u'"
219 find_filter
='( ( -exec test -e /rom/{} ; -exec cmp -s /{} /rom/{} ; ) -o -print )'
224 do_save_conffiles
() {
229 [ "$(rootfs_type)" = "tmpfs" ] && {
230 echo "Cannot save config while running from ramdisk." >&2
231 ask_bool
0 "Abort" && exit
235 run_hooks
"$CONFFILES" $sysupgrade_init_conffiles
236 ask_bool
0 "Edit config file list" && vi
"$CONFFILES"
238 # Mount root directory with temporary overlay on top of it.
239 # This allows including extra (temporary) files in backup archive
240 # without messing actual rootfs.
241 tmp
="$(mktemp -d -t overlay.XXXXXX)"
242 mkdir
-p "$tmp/upper" "$tmp/work" "$tmp/backup"
244 if ! mount
-t overlay overlay
-o lowerdir
=/,upperdir
="$tmp/upper",workdir
="$tmp/work" "$dir"; then
245 echo "Cannot mount backup overlay to $dir." >&2
246 ask_bool
0 "Abort" && exit
250 if [ "$SAVE_INSTALLED_PKGS" -eq 1 ]; then
251 echo "${INSTALLED_PACKAGES}" >> "$CONFFILES"
252 mkdir
-p "$dir/$ETCBACKUP_DIR"
254 # Format: pkg-name<TAB>{rom,overlay,unkown}
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/' > "$dir/${INSTALLED_PACKAGES}"
263 mkdir
-p $dir/etc
/uci-defaults
/
264 touch $dir/etc
/uci-defaults
/10_disable_services
265 for service
in /etc
/init.d
/*; do
266 if ! $service enabled
; then
267 echo "$service disable" >> $dir/etc
/uci-defaults
/10_disable_services
270 echo "exit 0" >> $dir/etc
/uci-defaults
/10_disable_services
271 echo "/etc/uci-defaults/10_disable_services" >> "$CONFFILES"
273 v
"Saving config files..."
274 [ "$VERBOSE" -gt 1 ] && TAR_V
="v" || TAR_V
=""
275 sed -i -e 's,^/,,' "$CONFFILES"
276 tar c
${TAR_V}zf
"$conf_tar" -C "${dir:-/}" -T "$CONFFILES"
278 if [ "$err" -ne 0 ]; then
279 echo "Failed to create the configuration backup."
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 COMMAND
='/lib/upgrade/do_stage2'
386 if [ -n "$FAILSAFE" ]; then
387 printf '%s\x00%s\x00%s' "$RAM_ROOT" "$IMAGE" "$COMMAND" >/tmp
/sysupgrade
388 lock
-u /tmp
/.failsafe
391 json_add_string prefix
"$RAM_ROOT"
392 json_add_string path
"$IMAGE"
393 [ $FORCE -eq 1 ] && json_add_boolean force
1
394 [ $SAVE_CONFIG -eq 1 ] && json_add_string backup
"$CONF_TAR"
395 json_add_string
command "$COMMAND"
396 json_add_object options
397 json_add_int save_partitions
"$SAVE_PARTITIONS"
400 ubus call system sysupgrade
"$(json_dump)"