upgrade-scripts: Get rid of error info during reboot
[platform/core/system/upgrade.git] / scripts / upgrade-support / upgrade-trigger.sh
1 #!/bin/bash
2 # Master script for upgrade. Called by update-manager/manually to start upgrade procedure.
3 set -o errexit
4 trap 'echo "Aborting due to errexit on ${0##*/}:$LINENO. Exit code: $?" >&2' ERR
5 set -o errtrace -e -o pipefail
6
7 if [ ! -z "${UPGRADE_DEBUG}" ]; then
8         set -x
9 fi
10
11 PATH=/bin:/usr/bin:/sbin:/usr/sbin
12 FOTA_DIR="/opt/usr/data/fota"
13 STATUS_DIR="/opt/data/update"
14 DOWNLOAD_DELTA="$1"
15 SCRIPT_NAME="upgrade-trigger.sh"
16 LOG_FILE="/tmp/upgrade-trigger.log"
17 SCRIPT_UPGRADE_PREPARE_PARTITIONS="upgrade-prepare-partitions.sh"
18 SCRIPT_UPGRADE_PARTIAL="upgrade-partial.sh"
19 SCRIPT_UPGRADE_FOTA="upgrade-fota.sh"
20 FLOCK_PATH="/var/lock/clone_partitions.lock"
21 DELTA_VERIFIER="/usr/bin/delta-verifier"
22
23 prepare_fota_dir() {
24         if [ -d "$FOTA_DIR" ]; then
25                 # Cleanup FOTA_DIR
26                 if [ "$(dirname $DOWNLOAD_DELTA)" = "$FOTA_DIR" ]; then
27                         # If provided delta is from inside the FOTA_DIR, delete everything else
28                         DELTA_FILE_NAME=$(basename $DOWNLOAD_DELTA)
29                         rm -rf -- "$FOTA_DIR/!($DELTA_FILE_NAME)"
30                 else
31                         # If provided delta is from outside the FOTA_DIR delete everything
32                         rm -rf -- "$FOTA_DIR/*"
33                 fi
34         else
35                 log "[Info] Create fota dir..." "$LOG_FILE"
36                 mkdir -p "$FOTA_DIR"
37         fi
38
39         if [ ! -d "$STATUS_DIR" ]; then
40                 log "[Info] Create status dir..." "$LOG_FILE"
41                 mkdir -p "$STATUS_DIR"
42         fi
43 }
44
45 log() {
46         # log format: [script_name][tag]actual_log
47         LOG="[${SCRIPT_NAME}]$1"
48         if [ "$2" != "" ]; then
49                 echo "[$(date +"%d/%m/%Y %H:%M:%S")]" "$LOG" >> "$2"
50         fi
51         echo "$LOG"
52
53         return 0
54 }
55
56 is_ab_upgrade() {
57         if grep -q partition_ab= /proc/cmdline; then
58                 return $TRUE
59         fi
60         return $FALSE
61 }
62
63 do_update() {
64         if is_ab_upgrade; then
65                 set_upgrade_status 5
66
67                 if [ "$(device_board_get_partition_ab_cloned)" -eq 0 ]; then
68                         log "Starting to prepare the partitions for upgrade.." "$LOG_FILE"
69
70                         unpack_file "${DOWNLOAD_DELTA}" "${SCRIPT_UPGRADE_PREPARE_PARTITIONS}"
71                         if ! "${FOTA_DIR}/${SCRIPT_UPGRADE_PREPARE_PARTITIONS}"; then
72                                 exit_error
73                         fi
74                 else
75                         echo "[Info] Partitions already cloned"
76                 fi
77
78                 set_upgrade_status 20
79
80                 # In the next steps the content of the partitions of the second slot will
81                 # be modified, so they will not be a clone of the current partitions
82                 device_board_clear_partition_ab_cloned
83
84                 unpack_file "${DOWNLOAD_DELTA}" "${SCRIPT_UPGRADE_PARTIAL}"
85                 unpack_file "${DOWNLOAD_DELTA}" "${SCRIPT_UPGRADE_FOTA}"
86
87                 if ! "${FOTA_DIR}/${SCRIPT_UPGRADE_PARTIAL}" "${DOWNLOAD_DELTA}"; then
88                         log "[Error] Failed to run $SCRIPT_UPGRADE_PARTIAL ${DOWNLOAD_DELTA}"
89                         exit_error
90                 fi
91
92                 set_upgrade_status 40
93
94                 # During the execution of ${SCRIPT_UPGRADE_FOTA} system will reboot, so the
95                 # script will be aborted. We don't want this to be treated as an error.
96                 untrap
97                 set -u errexit
98                 if ! "${FOTA_DIR}/${SCRIPT_UPGRADE_FOTA}"; then
99                         # We want to check if we are here becasue the reboot interrupted the
100                         # execution of ${SCRIPT_UPGRADE_FOTA} or for some other reason.
101                         # If a reboot is currently in progress then /proc/1/exe will point to
102                         # something other than "systemd" (e.g. deviced-shudown).
103                         if [ "$(basename "$(readlink /proc/1/exe)")" = "systemd" ]; then
104                                 # No, it's not a reboot. Rather it is a regular error.
105                                 exit_error
106                         else
107                                 set_upgrade_status 80
108                                 echo "[Info] RO update: $SCRIPT_NAME success"
109                         fi
110                 fi
111
112         else
113                 echo "[Error] Non-A/B upgrade is unsupported"
114                 device_board_set_upgrade_status -1
115                 exit 1
116         fi
117 }
118
119 verify_file() {
120         FILE_PATH="$1"
121         FILE_NAME=$(basename "$FILE_PATH")
122
123         VALID_CHECKSUM=$(awk "\$2 ~ /^$FILE_NAME\$/ {print \$1}" "$FOTA_DIR/checksum.SHA1")
124         if [ "$VALID_CHECKSUM" == "" ]; then
125                 echo "[Error] No $FILE_NAME in checksum.SHA1"
126                 device_board_set_upgrade_status -1
127                 exit 1
128         fi
129
130         if ! echo "$VALID_CHECKSUM  $FILE_PATH" | sha1sum --check --status; then
131                 echo "[Error] Checksum for file $FILE_NAME is invalid"
132                 device_board_set_upgrade_status -1
133                 exit 1
134         fi
135
136         echo "[Info] Checksum of $FILE_NAME is OK"
137 }
138
139 (
140 flock 9
141
142 rm -f "$LOG_FILE" && touch "$LOG_FILE"
143
144 if [ "$#" != "1" ] || [ ! -f "$1" ]; then
145         log "[Error] Usage: $0 path_to_delta.tar[.gz]" "$LOG_FILE"
146         exit 1
147 fi
148
149 log "[Info] Triggered upgrade.." "$LOG_FILE"
150 log "[Info] Using <$DOWNLOAD_DELTA> delta file." "$LOG_FILE"
151
152 prepare_fota_dir
153 tar xfp "$DOWNLOAD_DELTA" -C "$FOTA_DIR" checksum.SHA1
154 verify_file "$0"
155 tar xfp "$DOWNLOAD_DELTA" -C "$FOTA_DIR" upgrade-common.inc
156 verify_file "$FOTA_DIR/upgrade-common.inc"
157 tar xfp "$DOWNLOAD_DELTA" -C "$FOTA_DIR" update-info.ini
158 verify_file "$FOTA_DIR/update-info.ini"
159 tar xfp "$DOWNLOAD_DELTA" -C "$FOTA_DIR" delta-verifier
160 verify_file "$FOTA_DIR/delta-verifier"
161
162 log "[Info] Begin delta verification"
163 RET=0
164 # '||'' used to stop script form exiting (errexit is set)
165 "${FOTA_DIR}/delta-verifier" --update_info_path "$FOTA_DIR/update-info.ini" || RET=$?
166
167 if [ ${RET} -ne 0 ]; then
168         log "[Error] Delta verification unsuccessful"
169         device_board_set_upgrade_status -1
170         exit 1
171 fi
172 log "[Info] Delta verification success"
173
174 . "$FOTA_DIR"/upgrade-common.inc
175
176 set_upgrade_status 1
177 verify_img "${DOWNLOAD_DELTA}"
178 do_update
179 ) 9> "$FLOCK_PATH"