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
26 export UMOUNT_ETCBACKUP_DIR=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;;
48 echo "Invalid option: $1" >&2
56 export CONFFILES=/tmp/sysupgrade.conffiles
57 export CONF_TAR=/tmp/sysupgrade.tgz
58 export ETCBACKUP_DIR=/etc/backup
59 export INSTALLED_PACKAGES=${ETCBACKUP_DIR}/installed_packages.txt
63 [ -z "$IMAGE" -a -z "$NEED_IMAGE" -a $CONF_BACKUP_LIST -eq 0 -o $HELP -gt 0 ] && {
65 Usage: $0 [<upgrade-option>...] <image file or URL>
66 $0 [-q] [-i] [-c] [-u] [-o] [-k] <backup-command> <file>
69 -f <config> restore configuration from .tar.gz (file or url)
71 -c attempt to preserve all changed files in /etc/
72 -o attempt to preserve all changed files in /, except those
73 from packages but including changed confs.
74 -u skip from backup files that are equal to those in /rom
75 -n do not save configuration over reflash
76 -p do not attempt to restore the partition table after flash.
77 -k include in backup a list of current installed packages at
80 Verify image and config .tar.gz but do not actually flash.
82 Flash image even if image checks fail, this is dangerous!
85 -h | --help display this help
88 -b | --create-backup <file>
89 create .tar.gz of files specified in sysupgrade.conf
90 then exit. Does not flash an image. If file is '-',
91 i.e. stdout, verbosity is set to 0 (i.e. quiet).
92 -r | --restore-backup <file>
93 restore a .tar.gz created with sysupgrade -b
94 then exit. Does not flash an image. If file is '-',
95 the archive is read from stdin.
97 list the files that would be backed up when calling
98 sysupgrade -b. Does not create a backup file.
104 [ -n "$IMAGE" -a -n "$NEED_IMAGE" ] && {
106 -b|--create-backup and -r|--restore-backup do not perform a firmware upgrade.
107 Do not specify both -b|-r and a firmware image.
112 # prevent messages from clobbering the tarball when using stdout
113 [ "$CONF_BACKUP" = "-" ] && export VERBOSE=0
118 BEGIN { conffiles = 0 }
119 /^Conffiles:/ { conffiles = 1; next }
120 !/^ / { conffiles = 0; next }
121 conffiles == 1 { print }
122 ' /usr/lib/opkg/status
125 list_changed_conffiles() {
126 # Cannot handle spaces in filenames - but opkg cannot either...
127 list_conffiles | while read file csum; do
128 [ -r "$file" ] || continue
130 echo "${csum} ${file}" | busybox sha256sum -sc - || echo "$file"
136 ( find $(sed -ne '/^[[:space:]]*$/d; /^#/d; p' \
137 /etc/sysupgrade.conf /lib/upgrade/keep.d/* 2>/dev/null) \
138 \( -type f -o -type l \) $find_filter 2>/dev/null;
139 list_changed_conffiles ) | sort -u > "$file"
146 local packagesfiles=$1.packagesfiles
147 touch "$packagesfiles"
149 if [ "$SAVE_OVERLAY_PATH" = / ]; then
150 local conffiles=$1.conffiles
151 local keepfiles=$1.keepfiles
153 list_conffiles | cut -f2 -d ' ' | sort -u > "$conffiles"
155 # backup files from /etc/sysupgrade.conf and /lib/upgrade/keep.d, but
156 # ignore those aready controlled by opkg conffiles
157 find $(sed -ne '/^[[:space:]]*$/d; /^#/d; p' \
158 /etc/sysupgrade.conf /lib/upgrade/keep.d/* 2>/dev/null) \
159 \( -type f -o -type l \) 2>/dev/null | sort -u |
160 grep -h -v -x -F -f $conffiles > "$keepfiles"
162 # backup conffiles, but only those changed if '-u'
163 [ $SKIP_UNCHANGED = 1 ] &&
164 list_changed_conffiles | sort -u > "$conffiles"
166 # do not backup files from packages, except those listed
167 # in conffiles and keep.d
169 find /usr/lib/opkg/info -type f -name "*.list" -exec cat {} \;
170 find /usr/lib/opkg/info -type f -name "*.control" -exec sed \
171 -ne '/^Alternatives/{s/^Alternatives: //;s/, /\n/g;p}' {} \; |
173 } | grep -v -x -F -f $conffiles |
174 grep -v -x -F -f $keepfiles | sort -u > "$packagesfiles"
175 rm -f "$keepfiles" "$conffiles"
178 # busybox grep bug when file is empty
179 [ -s "$packagesfiles" ] || echo > $packagesfiles
181 ( cd /overlay/upper/; find .$SAVE_OVERLAY_PATH \( -type f -o -type l \) $find_filter | sed \
183 -e '\,^/etc/board.json$,d' \
184 -e '\,/[^/]*-opkg$,d' \
185 -e '\,^/etc/urandom.seed$,d' \
186 -e "\,^$INSTALLED_PACKAGES$,d" \
187 -e '\,^/usr/lib/opkg/.*,d' \
188 ) | grep -v -x -F -f $packagesfiles > "$file"
190 rm -f "$packagesfiles"
195 if [ $SAVE_OVERLAY = 1 ]; then
196 [ ! -d /overlay/upper/etc ] && {
197 echo "Cannot find '/overlay/upper/etc', required for '-c'" >&2
200 sysupgrade_init_conffiles="add_overlayfiles"
202 sysupgrade_init_conffiles="add_conffiles"
206 if [ $SKIP_UNCHANGED = 1 ]; then
208 echo "'/rom/' is required by '-u'"
211 find_filter='( ( -exec test -e /rom/{} ; -exec cmp -s /{} /rom/{} ; ) -o -print )'
216 do_save_conffiles() {
219 [ -z "$(rootfs_type)" ] && {
220 echo "Cannot save config while running from ramdisk." >&2
221 ask_bool 0 "Abort" && exit
225 run_hooks "$CONFFILES" $sysupgrade_init_conffiles
226 ask_bool 0 "Edit config file list" && vi "$CONFFILES"
228 if [ "$SAVE_INSTALLED_PKGS" -eq 1 ]; then
229 echo "${INSTALLED_PACKAGES}" >> "$CONFFILES"
230 mkdir -p "$ETCBACKUP_DIR"
231 # Avoid touching filesystem on each backup
232 RAMFS="$(mktemp -d -t sysupgrade.XXXXXX)"
233 mkdir -p "$RAMFS/upper" "$RAMFS/work"
234 mount -t overlay overlay -o lowerdir=$ETCBACKUP_DIR,upperdir=$RAMFS/upper,workdir=$RAMFS/work $ETCBACKUP_DIR &&
235 UMOUNT_ETCBACKUP_DIR=1 || {
236 echo "Cannot mount '$ETCBACKUP_DIR' as tmpfs to avoid touching disk while saving the list of installed packages." >&2
237 ask_bool 0 "Abort" && exit
240 # Format: pkg-name<TAB>{rom,overlay,unkown}
241 # rom is used for pkgs in /rom, even if updated later
242 find /usr/lib/opkg/info -name "*.control" \( \
243 \( -exec test -f /rom/{} \; -exec echo {} rom \; \) -o \
244 \( -exec test -f /overlay/upper/{} \; -exec echo {} overlay \; \) -o \
245 \( -exec echo {} unknown \; \) \
246 \) | sed -e 's,.*/,,;s/\.control /\t/' > ${INSTALLED_PACKAGES}
249 v "Saving config files..."
250 [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
251 tar c${TAR_V}zf "$conf_tar" -T "$CONFFILES" 2>/dev/null
252 if [ "$?" -ne 0 ]; then
253 echo "Failed to create the configuration backup."
258 [ "$UMOUNT_ETCBACKUP_DIR" -eq 1 ] && {
259 umount "$ETCBACKUP_DIR"
265 if [ $CONF_BACKUP_LIST -eq 1 ]; then
266 run_hooks "$CONFFILES" $sysupgrade_init_conffiles
267 [ "$SAVE_INSTALLED_PKGS" -eq 1 ] && echo ${INSTALLED_PACKAGES} >> "$CONFFILES"
273 if [ -n "$CONF_BACKUP" ]; then
274 do_save_conffiles "$CONF_BACKUP"
278 if [ -n "$CONF_RESTORE" ]; then
279 if [ "$CONF_RESTORE" != "-" ] && [ ! -f "$CONF_RESTORE" ]; then
280 echo "Backup archive '$CONF_RESTORE' not found." >&2
284 [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
285 tar -C / -x${TAR_V}zf "$CONF_RESTORE"
289 type platform_check_image >/dev/null 2>/dev/null || {
290 echo "Firmware upgrade is not implemented for this platform." >&2
297 wget -O/tmp/sysupgrade.img "$IMAGE" || exit 1
298 IMAGE=/tmp/sysupgrade.img
302 IMAGE="$(readlink -f "$IMAGE")"
306 echo "Image file not found." >&2
311 v "Image not in /tmp, copying..."
312 cp -f "$IMAGE" /tmp/sysupgrade.img
313 IMAGE=/tmp/sysupgrade.img
317 json_load "$(/usr/libexec/validate_firmware_image "$IMAGE")" || {
318 echo "Failed to check image"
321 json_get_var valid "valid"
322 [ "$valid" -eq 0 ] && {
323 if [ $FORCE -eq 1 ]; then
324 echo "Image check failed but --force given - will update anyway!" >&2
326 echo "Image check failed." >&2
331 if [ -n "$CONF_IMAGE" ]; then
332 case "$(get_magic_word $CONF_IMAGE cat)" in
336 echo "Invalid config file. Please use only .tar.gz files" >&2
340 get_image "$CONF_IMAGE" "cat" > "$CONF_TAR"
342 elif ask_bool $SAVE_CONFIG "Keep config files over reflash"; then
343 [ $TEST -eq 1 ] || do_save_conffiles "$CONF_TAR"
346 [ $TEST -eq 1 ] || rm -f "$CONF_TAR"
350 if [ $TEST -eq 1 ]; then
354 install_bin /sbin/upgraded
355 v "Commencing upgrade. Closing all shell sessions."
357 COMMAND='/lib/upgrade/do_stage2'
359 if [ -n "$FAILSAFE" ]; then
360 printf '%s\x00%s\x00%s' "$RAM_ROOT" "$IMAGE" "$COMMAND" >/tmp/sysupgrade
361 lock -u /tmp/.failsafe
364 json_add_string prefix "$RAM_ROOT"
365 json_add_string path "$IMAGE"
366 [ $FORCE -eq 1 ] && json_add_boolean force 1
367 [ $SAVE_CONFIG -eq 1 ] && json_add_string backup "$CONF_TAR"
368 json_add_string command "$COMMAND"
369 json_add_object options
370 json_add_int save_partitions "$SAVE_PARTITIONS"
373 ubus call system sysupgrade "$(json_dump)"