scripts: move reboot to the main upgrade script 07/318807/3
authorJacek Kryszyn <j.kryszyn@samsung.com>
Fri, 4 Oct 2024 10:43:07 +0000 (12:43 +0200)
committerJacek Kryszyn <j.kryszyn@samsung.com>
Thu, 10 Oct 2024 14:39:43 +0000 (16:39 +0200)
This solves few issues with upgrade scripts. Before
this change reboot was executed in upgrade-fota.sh which
was executed by upgrade-trigger.sh. There had to be
some checks to decide if upgrade-fota.sh was interrupted
by reboot or for other reasons. In case of interruption
by reboot there were some unwanted error messages anyway.
Moving reboot to upgrade-trigger.sh simplifies scripts.
Now it is certain that if the exit status of upgrade-fota.sh
is different than 0 then it has to be treated as an error.

This change unifies the way how set flags are used and
error_exit function is used more often instead of
combination of setting upgrade progess status and exit.

Change-Id: I7850e498fb26cd52565fd320709b4e38cf4f221a

scripts/clone_partitions/clone_partitions.sh
scripts/upgrade-support/upgrade-fota.sh
scripts/upgrade-support/upgrade-partial.sh
scripts/upgrade-support/upgrade-prepare-partitions.sh
scripts/upgrade-support/upgrade-trigger.sh

index 33d667815ecc5f5a058b984e0d6076426d189c77..d6204dd42be9e11b5e09fc497f6091850632913e 100644 (file)
@@ -100,12 +100,11 @@ clone_background() {
        start_clone
 }
 
-set -o errexit
+set -o errexit -o errtrace -o pipefail
 trap 'echo "Aborting due to errexit on ${0##*/}:$LINENO. Exit code: $?" >&2' ERR
-set -o errtrace -e -o pipefail
 
 if [ -n "${UPGRADE_DEBUG}" ]; then
-       set -x
+       set -o xtrace
 fi
 
 . /usr/libexec/upgrade-support/upgrade-common.inc
index 98b550cc6a3d19f97283663d6b57f4ef84347520..f3eb1b187c2f34c6f3d46617fab08c1e86d92ba1 100755 (executable)
@@ -1,13 +1,13 @@
 #!/bin/bash
-set -o errexit
+set -o errexit -o errtrace -o pipefail
 trap 'echo "Aborting due to errexit on ${0##*/}:$LINENO. Exit code: $?" >&2' ERR
-set -o errtrace -e -o pipefail
-
 
 if [ -n "${UPGRADE_DEBUG}" ]; then
-       set -x
+       set -o xtrace
 fi
 
+progress_to=$1
+
 PATH=/bin:/usr/bin:/sbin:/usr/sbin
 FOTA_DIR="/opt/usr/data/fota"
 
@@ -50,7 +50,7 @@ mount() {
        return 0
 }
 
-reboot_to_fota() {
+prepare_reboot_to_fota() {
        log "[Info] Write paths..."
        touch "$DO_RW_UPDATE_FILE"
        log "[Info] calling sync"
@@ -58,20 +58,10 @@ reboot_to_fota() {
        log "[Info] Switching board partition from $CURRENT_AB, to $NEXT_AB"
        if ! device_board_switch_partition "$NEXT_AB"; then
                critical_log "[Error] Failed to switch board slot to $NEXT_AB"
-               exit 1
+               return 1
        fi
        cleanup_files
        /bin/sync
-       log "[Info] Rebooting to fota"
-
-       # System will reboot, so the script will be aborted. We don't want this to be
-       # treated as an error.
-       set -u errexit
-       untrap
-       if ! /sbin/reboot fota; then
-               critical_log "[Error] Failed to reboot fota"
-               exit 1
-       fi
 }
 
 prepare_fota_update_dir() {
@@ -81,18 +71,20 @@ prepare_fota_update_dir() {
 prepare_fota_update_dir
 check_ab_partition_scheme
 check_used_block_device
+
 if ! mount; then
        cleanup
-       exit 1
+       exit_error
 fi
+
 if ! write_version_info "$VERSION_FILE"; then
        cleanup
-       exit 1
+       exit_error
 fi
 
 critical_log "[Info] RO update: $SCRIPT_NAME success"
 
-if ! reboot_to_fota; then
+if ! prepare_reboot_to_fota; then
        cleanup
-       exit 1
+       exit_error
 fi
index 0001baedfad6026b647bf52216c6484282aa64a4..226e9b8d5298feb4364f102830b52acc791bb27d 100755 (executable)
@@ -1,10 +1,9 @@
 #!/bin/bash
-set -o errexit
+set -o errexit -o errtrace -o pipefail
 trap 'echo "Aborting due to errexit on ${0##*/}:$LINENO. Exit code: $?" >&2' ERR
-set -o errtrace -e -o pipefail
 
 if [ -n "${UPGRADE_DEBUG}" ]; then
-       set -x
+       set -o xtrace
 fi
 
 PATH=/bin:/usr/bin:/sbin:/usr/sbin
@@ -29,7 +28,7 @@ check_args() {
        if [[ (( $SINGLE_DELTA = true ) && ( ! -f "$DELTA_TAR_A" )) || \
                (( $SINGLE_DELTA = false ) && (( ! -f "$DELTA_TAR_A" ) || ( ! -f "$DELTA_TAR_B"))) ]]; then
                log "[Error] Usage: $0 path_to_delta.tar[.gz] [path_to_second_delta.tar[.gz]]"
-               exit 1
+               exit_error
        fi
 
        if [ $SINGLE_DELTA = true ]; then
index 75576ffc1573ef3ca20449aa7d2b23aed84962ae..908af46136d5e6a516da695c219b197aaf8fbef4 100755 (executable)
@@ -1,10 +1,9 @@
 #!/bin/bash
-set -o errexit
+set -o errexit -o errtrace -o pipefail
 trap 'echo "Aborting due to errexit on ${0##*/}:$LINENO. Exit code: $?" >&2' ERR
-set -o errtrace -e -o pipefail
 
 if [ -n "${UPGRADE_DEBUG}" ]; then
-       set -x
+       set -o xtrace
 fi
 
 progress_to=$1
index 7c24580d87ac62f6cf9298545b366fbf8cde9c3b..5bc9649ca3e68370f3757b53cae9fdf8868afd23 100644 (file)
@@ -1,15 +1,15 @@
 #!/bin/bash
 # Master script for upgrade. Called by update-manager/manually to start upgrade procedure.
-set -o errexit
+set -o errexit -o errtrace -o pipefail
 trap 'echo "Aborting due to errexit on ${0##*/}:$LINENO. Exit code: $?" >&2' ERR
-set -o errtrace -e -o pipefail
 
 if [ -n "${UPGRADE_DEBUG}" ]; then
-       set -x
+       set -o xtrace
 fi
 
-PROGRESS_AFTER_RO=80
 PROGRESS_AFTER_CLONING=20
+PROGRESS_AFTER_UPDATE=79
+PROGRESS_AFTER_FINISH=80
 
 ERR_RO_UPDATE_IN_PROGRESS=2
 ERR_FINISH_BEFORE_RO_UPDATE_COMPLETE=3
@@ -121,6 +121,7 @@ do_update() {
                        unpack_file "${DELTA_A}" "${SCRIPT_UPGRADE_PREPARE_PARTITIONS}" "${DELTA_A_CHECKSUM_FILE}" ""
 
                        run_script "${FOTA_DIR}/${SCRIPT_UPGRADE_PREPARE_PARTITIONS} $PROGRESS_AFTER_CLONING"
+
                        if [ $RET -ne 0 ]; then
                                exit_error
                        fi
@@ -133,7 +134,6 @@ do_update() {
                # be modified, so they will not be a clone of the current partitions
                device_board_clear_partition_ab_cloned
 
-
                run_script "${FOTA_DIR}/${SCRIPT_UPGRADE_PARTIAL} ${DELTA_A} ${DELTA_B}"
 
                if [ $RET -ne 0 ] ; then
@@ -141,40 +141,38 @@ do_update() {
                        exit_error
                fi
 
-               set_upgrade_progress_status $PROGRESS_AFTER_RO
-
-               # During the execution of ${SCRIPT_UPGRADE_FOTA} system will reboot, so the
-               # script will be aborted. We don't want this to be treated as an error.
-               untrap
-               set -u errexit
+               set_upgrade_progress_status $PROGRESS_AFTER_UPDATE
        else
                log "[Error] Non-A/B upgrade is unsupported"
-               device_board_set_upgrade_progress_status -1
-               exit 1
+               exit_error
        fi
 }
 
 do_finish() {
-       current_upgrade_progress_status=$(device_board_get_upgrade_progress_status)
-       if [ "$current_upgrade_progress_status" != "$PROGRESS_AFTER_RO" ]; then
+       current_upgrade_progress_status=$($GET_UPGRADE_PROGRESS_STATUS)
+
+       if [ "$current_upgrade_progress_status" != "$PROGRESS_AFTER_UPDATE" ]; then
                critical_log "[Error] Cannot finish upgrade - RO Upgrade has not been completed"
                exit $ERR_FINISH_BEFORE_RO_UPDATE_COMPLETE
        fi
 
        run_script "${FOTA_DIR}/${SCRIPT_UPGRADE_FOTA}"
-       if ! [ $RET -ne 0 ]; then
-               # We want to check if we are here becasue the reboot interrupted the
-               # execution of ${SCRIPT_UPGRADE_FOTA} or for some other reason.
-               # If a reboot is currently in progress then /proc/1/exe will point to
-               # something other than "systemd" (e.g. deviced-shudown).
-               if [ "$(basename "$(readlink /proc/1/exe)")" = "systemd" ]; then
-                       # No, it's not a reboot. Rather it is a regular error.
-                       exit_error
-               else
-                       set_upgrade_progress_status $PROGRESS_AFTER_RO
-                       log "[Info] RO update: $SCRIPT_NAME success"
-               fi
+
+       if [ $RET -ne 0 ]; then
+               critical_log "Error: failed tu run ${FOTA_DIR}/${SCRIPT_UPGRADE_FOTA}" "$LOG_FILE"
+               exit_error
+       fi
+
+       set_upgrade_progress_status $PROGRESS_AFTER_FINISH
+
+       log "[Info] Rebooting to fota"
+
+       if ! /sbin/reboot fota; then
+               critical_log "[Error] Failed to reboot fota"
+               exit_error
        fi
+
+       log "[Info] RO update: $SCRIPT_NAME success"
 }
 
 verify_file() {
@@ -185,14 +183,12 @@ verify_file() {
        VALID_CHECKSUM=$(awk "\$2 ~ /^$FILE_NAME\$/ {print \$1}" "$CHECKSUM_FILE")
        if [ "$VALID_CHECKSUM" == "" ]; then
                log "[Error] No $FILE_NAME in $CHECKSUM_FILE"
-               device_board_set_upgrade_progress_status -1
-               exit 1
+               exit_error
        fi
 
        if ! echo "$VALID_CHECKSUM  $FILE_PATH" | sha1sum --check --status; then
                log "[Error] Checksum for file $FILE_NAME is invalid [$CHECKSUM_FILE]"
-               device_board_set_upgrade_progress_status -1
-               exit 1
+               exit_error
        fi
 
        log "[Info] Checksum of $FILE_NAME is OK [$CHECKSUM_FILE]"
@@ -320,8 +316,7 @@ if [ $MODE_RO_UPDATE = true ]; then
 
        if [ ${RET} -ne 0 ]; then
                log "[Error] Delta verification unsuccessful"
-               device_board_set_upgrade_progress_status -1
-               exit 1
+               exit_error
        fi
        log "[Info] Delta verification success"