fota-init.sh: improve logging:
[platform/core/system/initrd-fota.git] / scripts / fota-init.sh
index 0b9e2d9..d70b261 100755 (executable)
@@ -42,6 +42,19 @@ FOTA_GUI_ENABLE=
 SYSTEM_DATA_MNT=opt
 USER_MNT=opt/usr
 
+SCRIPT_NAME="fota-init.sh"
+#------------------------------------------------
+#       log msg [file]
+#------------------------------------------------
+log() {
+       # log format: [script_name][tag]actual_log
+       LOG="[${SCRIPT_NAME}]$1"
+       if [ "$2" != "" ]; then
+               echo "$LOG" >> "$2"
+       fi
+       echo "$LOG"
+}
+
 #------------------------------------------------
 #       get partition id
 #------------------------------------------------
@@ -91,7 +104,7 @@ umount_partitions() {
 #      handle_no_delta
 #------------------------------------------------
 handle_no_delta() {
-       echo "delta does not exist ..." >> "${INT_LOG_FILE}"
+       log "[Error] delta does not exist ..." "${INT_LOG_FILE}"
 
        echo "${UPI_NO_DELTA_ERROR}" > "${RESULT_FILE}"
 }
@@ -100,7 +113,7 @@ handle_no_delta() {
 #      handle_no_ua
 #------------------------------------------------
 handle_no_ua() {
-       echo "ua does not exist ..." >> "${INT_LOG_FILE}"
+       log "[Error] ua does not exist ..." "${INT_LOG_FILE}"
 
        echo "${UPI_NO_UA_ERROR}" > "${RESULT_FILE}"
 }
@@ -127,10 +140,10 @@ do_create_part_table() {
 #      do_fota_update
 #------------------------------------------------
 do_fota_update() {
-       echo "fota update start ..." >> "${INT_LOG_FILE}"
+       log "[Info] fota update start ..." "${INT_LOG_FILE}"
 
        DELTA_DIR=$("$CAT" "${DELTA_PATH_FILE}")
-       echo "DELTA_DIR = ${DELTA_DIR}" >> "${INT_LOG_FILE}"
+       log "[Info] DELTA_DIR = ${DELTA_DIR}" "${INT_LOG_FILE}"
 
        RESULT_FILE=${FAKE_ROOT}${FOTA_RESULT_DIR}/result
        if [ -e "${RESULT_FILE}" ]; then
@@ -153,20 +166,20 @@ do_fota_update() {
 
        do_create_part_table
        if [ ! -e "${PART_TBL_PATH}" ]; then
-               echo "No partition table" >> "${INT_LOG_FILE}"
+               log "[Error] No partition table" "${INT_LOG_FILE}"
                return 1
        fi
 
        VERIFY_TOOL=/usr/sbin/img-verifier
        if [ -e "${VERIFY_TOOL}" ]; then
                if ! "${VERIFY_TOOL}" -i "${DELTA}" -l "/opt/var/log/last_iv.log"; then
-                       echo "signature verification failed!" >> "${INT_LOG_FILE}"
+                       log "[Error] signature verification failed!" "${INT_LOG_FILE}"
                        return 1
                else
-                       echo "update package verification success"
+                       log "[Info] update package verification success"
                fi
        else
-               echo "Warning: No signature verifier... Skip it" >> "${INT_LOG_FILE}"
+               log "[Error] Warning: No signature verifier... Skip it" "${INT_LOG_FILE}"
        fi
 
        FOTA_ARG="1"
@@ -174,10 +187,10 @@ do_fota_update() {
                FOTA_ARG="0"
        fi
        if ! "${UA}" "${DELTA_DIR}" "${FOTA_TEMP_DIR}" "$FOTA_ARG"; then
-               echo "update agent fail!!!" >> "${INT_LOG_FILE}"
+               log "[Error] update agent fail!!!" "${INT_LOG_FILE}"
                return 1
        else
-               echo "update agent success"
+               log "[Info] update agent success"
                return 0
        fi
 
@@ -203,9 +216,9 @@ init_fota_dir() {
        for TEST_DIR in "${TEST_DIRS[@]}"; do
                if [ ! -e "${TEST_DIR}" ]; then
                        if "$MKDIR" -p "${TEST_DIR}"; then
-                               echo "Successfully created <${TEST_DIR}>" >> "${INT_LOG_FILE}"
+                               log "[Info] Successfully created <${TEST_DIR}>" "${INT_LOG_FILE}"
                        else
-                               echo "Unable to create <${TEST_DIR}>"
+                               log "[Error] Unable to create <${TEST_DIR}>"
                        fi
                fi
        done
@@ -215,7 +228,7 @@ init_fota_dir() {
 #      do_reboot
 #------------------------------------------------
 do_reboot() {
-       echo "Reboot"
+       log "Reboot"
        "$SYNC"
        "$REBOOT"
        while true
@@ -231,8 +244,8 @@ do_reboot() {
 check_for_rw_power_fail() {
        # Case : Power fail during RW update after RO update
        if [ -r "${STATUS_FILE}" ]; then
-               echo "${STATUS_FILE} exists" >> "${INT_LOG_FILE}"
-               "$CAT" "${STATUS_FILE}" >> "${INT_LOG_FILE}"
+               log "[Info] ${STATUS_FILE} exists" "${INT_LOG_FILE}"
+               log "[Info] Status file: $("$CAT" "${STATUS_FILE}")" "${INT_LOG_FILE}"
                "$SYNC"
                umount_partitions
                exec /sbin/fus_rw-init
@@ -244,7 +257,7 @@ check_for_rw_power_fail() {
 #------------------------------------------------
 check_for_fota_gui() {
        if [ -r "${FOTA_GUI}" ]; then
-               echo "GUI Enabled" >> "${INT_LOG_FILE}"
+               log "[Info] GUI Enabled" "${INT_LOG_FILE}"
                FOTA_GUI_ENABLE=1
        fi
 }
@@ -257,15 +270,15 @@ check_debug_mode() {
        if [ -f "${DEBUG_MODE_FILE}" ]; then
                DEBUG_MODE_FILE_OWNER=$("${LS}" -l "${DEBUG_MODE_FILE}" | "${CUT}" -d " " -f 3)
                if [ "${DEBUG_MODE_FILE_OWNER}" = "0" ]; then
-                       echo "Enter RO debug mode"
-                       echo "If you want to continue FOTA, please remove ${DEBUG_MODE_FILE}"
+                       log "[Info] Enter RO debug mode"
+                       log "[Info] If you want to continue FOTA, please remove ${DEBUG_MODE_FILE}"
                        # Wait until debug mode file is removed
                        while [ -f "${DEBUG_MODE_FILE}" ]; do
                                sleep 3
                        done
-                       echo "Debug mode file is removed. Resume FOTA"
+                       log "[Info] Debug mode file is removed. Resume FOTA"
                else
-                       echo "Warning: somebody made non-root debug mode file... ignore it"
+                       log "[Info] Warning: somebody made non-root debug mode file... ignore it"
                fi
        fi
 }
@@ -279,18 +292,18 @@ remake_hash_table() {
                        "$MOUNT" -o remount,ro "${FAKE_ROOT}"
 
                        if ! "${VERITY_HANDLER}" "${PART_ROOTFS}"; then
-                               echo "verity-handler fail!!!"
+                               log "[Error] verity-handler fail!!!"
                                return 1
                        else
-                               echo "verity-handler success"
+                               log "[Info] verity-handler success"
                                return 0
                        fi
                else
-                       echo "Warning: ${VERITYCTL} exists but ${VERITY_HANDLER} does not exists"
+                       log "[Error] Warning: ${VERITYCTL} exists but ${VERITY_HANDLER} does not exists"
                        return 2
                fi
        else
-               echo "verityctl does not exist. Skip hash remaking"
+               log "[Info] verityctl does not exist. Skip hash remaking"
                return 0
        fi
 }
@@ -339,6 +352,6 @@ umount_partitions
 if [ "$fota_result" = "0" ] && [ "$remake_result" = "0" ]; then
        exec /sbin/fus_rw-init
 else
-       echo "Upgrade FAILED"
+       log "[Error] Upgrade FAILED"
        do_reboot
 fi