base-files: sysupgrade: do not hide tar errors
[openwrt/openwrt.git] / package / base-files / files / sbin / sysupgrade
index 6c518b780eddd973966537b50d8f7c82539bb1e1..eba11ce78ed5d45f700fbf3e8e8fd9b5d53f40c1 100755 (executable)
@@ -19,6 +19,7 @@ export CONF_IMAGE=
 export CONF_BACKUP_LIST=0
 export CONF_BACKUP=
 export CONF_RESTORE=
+export IGNORE_MINOR_COMPAT=0
 export NEED_IMAGE=
 export HELP=0
 export FORCE=0
@@ -44,6 +45,7 @@ while [ -n "$1" ]; do
                -F|--force) export FORCE=1;;
                -T|--test) export TEST=1;;
                -h|--help) export HELP=1; break;;
+               --ignore-minor-compat-version) export IGNORE_MINOR_COMPAT=1;;
                -*)
                        echo "Invalid option: $1" >&2
                        exit 1
@@ -80,6 +82,8 @@ upgrade-option:
                     Verify image and config .tar.gz but do not actually flash.
        -F | --force
                     Flash image even if image checks fail, this is dangerous!
+       --ignore-minor-compat-version
+                    Flash image even if the minor compat version is incompatible.
        -q           less verbose
        -v           more verbose
        -h | --help  display this help
@@ -127,16 +131,23 @@ list_changed_conffiles() {
        list_conffiles | while read file csum; do
                [ -r "$file" ] || continue
 
-               echo "${csum}  ${file}" | sha256sum -sc - || echo "$file"
+               echo "${csum}  ${file}" | busybox sha256sum -sc - || echo "$file"
        done
 }
 
+list_static_conffiles() {
+       local filter=$1
+
+       find $(sed -ne '/^[[:space:]]*$/d; /^#/d; p' \
+               /etc/sysupgrade.conf /lib/upgrade/keep.d/* 2>/dev/null) \
+               \( -type f -o -type l \) $filter 2>/dev/null
+}
+
 add_conffiles() {
        local file="$1"
-       ( find $(sed -ne '/^[[:space:]]*$/d; /^#/d; p' \
-               /etc/sysupgrade.conf /lib/upgrade/keep.d/* 2>/dev/null) \
-               \( -type f -o -type l \) $find_filter 2>/dev/null;
-         list_changed_conffiles ) | sort -u > "$file"
+
+       ( list_static_conffiles "$find_filter"; list_changed_conffiles ) |
+               sort -u > "$file"
        return 0
 }
 
@@ -154,9 +165,7 @@ add_overlayfiles() {
 
                # backup files from /etc/sysupgrade.conf and /lib/upgrade/keep.d, but
                # ignore those aready controlled by opkg conffiles
-               find $(sed -ne '/^[[:space:]]*$/d; /^#/d; p' \
-                       /etc/sysupgrade.conf /lib/upgrade/keep.d/* 2>/dev/null) \
-                       \( -type f -o -type l \) 2>/dev/null | sort -u |
+               list_static_conffiles | sort -u |
                        grep -h -v -x -F -f $conffiles > "$keepfiles"
 
                # backup conffiles, but only those changed if '-u'
@@ -216,7 +225,7 @@ include /lib/upgrade
 do_save_conffiles() {
        local conf_tar="$1"
 
-       [ -z "$(rootfs_type)" ] && {
+       [ "$(rootfs_type)" = "tmpfs" ] && {
                echo "Cannot save config while running from ramdisk." >&2
                ask_bool 0 "Abort" && exit
                rm -f "$conf_tar"
@@ -248,7 +257,8 @@ do_save_conffiles() {
 
        v "Saving config files..."
        [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
-       tar c${TAR_V}zf "$conf_tar" -T "$CONFFILES" 2>/dev/null
+       sed -i -e 's,^/,,' "$CONFFILES"
+       tar c${TAR_V}zf "$conf_tar" -C / -T "$CONFFILES"
        if [ "$?" -ne 0 ]; then
                echo "Failed to create the configuration backup."
                rm -f "$conf_tar"
@@ -282,6 +292,7 @@ if [ -n "$CONF_RESTORE" ]; then
        fi
 
        [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
+       v "Restoring config files..."
        tar -C / -x${TAR_V}zf "$CONF_RESTORE"
        exit $?
 fi
@@ -360,18 +371,15 @@ if [ -n "$FAILSAFE" ]; then
        printf '%s\x00%s\x00%s' "$RAM_ROOT" "$IMAGE" "$COMMAND" >/tmp/sysupgrade
        lock -u /tmp/.failsafe
 else
-       force_attr=""
-       [ $FORCE -eq 1 ] && force_attr="\"force\": true,"
-       backup_attr=""
-       [ $SAVE_CONFIG -eq 1 ] && backup_attr="\"backup\": $(json_string $CONF_TAR),"
-       ubus call system sysupgrade "{
-               \"prefix\": $(json_string "$RAM_ROOT"),
-               \"path\": $(json_string "$IMAGE"),
-               $force_attr
-               $backup_attr
-               \"command\": $(json_string "$COMMAND"),
-               \"options\": {
-                       \"save_partitions\": $SAVE_PARTITIONS
-               }
-       }"
+       json_init
+       json_add_string prefix "$RAM_ROOT"
+       json_add_string path "$IMAGE"
+       [ $FORCE -eq 1 ] && json_add_boolean force 1
+       [ $SAVE_CONFIG -eq 1 ] && json_add_string backup "$CONF_TAR"
+       json_add_string command "$COMMAND"
+       json_add_object options
+       json_add_int save_partitions "$SAVE_PARTITIONS"
+       json_close_object
+
+       ubus call system sysupgrade "$(json_dump)"
 fi