upgrade-scripts: Get rid of error info during reboot
[platform/core/system/upgrade.git] / scripts / upgrade-support / upgrade-fota.sh
1 #!/bin/bash
2 set -o errexit
3 trap 'echo "Aborting due to errexit on ${0##*/}:$LINENO. Exit code: $?" >&2' ERR
4 set -o errtrace -e -o pipefail
5
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
14 . "$FOTA_DIR"/upgrade-common.inc
15
16 FOTA_UPDATE_PREFIX="/run/upgrade-sysroot"
17 VERSION_FILE="$FOTA_UPDATE_PREFIX/opt/etc/version"
18
19 SCRIPT_NAME="upgrade-fota.sh"
20 LOG_FILE="/tmp/update-fota.log"
21
22 cleanup() {
23         cleanup_partitions
24         cleanup_files
25 }
26
27 cleanup_files() {
28         rm -f -- "$FOTA_DIR"/upgrade-common.inc
29         rm -f -- "$FOTA_DIR/$SCRIPT_NAME"
30         rm -f -- "$FOTA_DIR/$CONFIG_FILE"
31         rm -f -- "$FOTA_DIR/upgrade-trigger.sh"
32         rm -f -- "$FOTA_DIR/checksum.SHA1"
33 }
34
35 mount() {
36         # We need to mount the system-data partition to update /opt/etc/version and
37         # create a .do_rw_update file
38         mkdir -p "$FOTA_UPDATE_PREFIX/opt"
39
40         if ! mount_partition system-data "$FOTA_UPDATE_PREFIX/opt" "rw"; then
41                 critical_flog "[Error] Unable to mount opt"
42                 return 1
43         fi
44         return 0
45 }
46
47 reboot_to_fota() {
48         flog "[Info] Write paths..."
49         touch "$DO_RW_UPDATE_FILE"
50         flog "[Info] calling sync"
51
52         flog "[Info] Switching board partition from $CURRENT_AB, to $NEXT_AB"
53         if ! device_board_switch_partition $NEXT_AB; then
54                 critical_flog "[Error] Failed to switch board slot to $NEXT_AB"
55                 exit 1
56         fi
57         cleanup_files
58         /bin/sync
59         flog "[Info] Rebooting to fota"
60
61         # System will reboot, so the script will be aborted. We don't want this to be
62         # treated as an error.
63         set -u errexit
64         untrap
65         if ! /sbin/reboot fota; then
66                 critical_flog "[Error] Failed to reboot fota"
67                 exit 1
68         fi
69 }
70
71 prepare_fota_update_dir() {
72         mkdir -p ${FOTA_UPDATE_PREFIX}
73 }
74
75 prepare_fota_update_dir
76 check_ab_partition_scheme
77 check_used_block_device
78 if ! mount; then
79         cleanup
80         exit 1
81 fi
82 if ! write_version_info "$VERSION_FILE"; then
83         cleanup
84         exit 1
85 fi
86
87 critical_log "RO update: $SCRIPT_NAME success" "$LOG_FILE"
88
89 if ! reboot_to_fota; then
90         cleanup
91         exit 1
92 fi