image_signing: simplify & fix mount cleanups

We don't need all this infrastructure for arbitrary cleanups when we
only ever run 2 clean up steps.  This also fixes a subtle bug in the
old logic: we registered cleanups in the logical order of (1) mounts
and then (2) loopbacks, but the cleanup loop walks the registered
calls in reverse order.  This means the loopback cleanup would fail
and timeout because we hadn't unmounted the partitions yet.  The
overall script doesn't fail as cleanup uses `set +e`, but it makes
every script waste ~10 seconds at exit.

BUG=None
TEST=running set_lsb_release.sh on images works quickly now
BRANCH=None

Change-Id: Ibd25ad6ba149c64e08ac3ab860342fe7b2cc7851
Signed-off-by: Mike Frysinger <vapier@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/vboot_reference/+/2923825
Reviewed-by: George Engelbrecht <engeg@google.com>
diff --git a/scripts/image_signing/common.sh b/scripts/image_signing/common.sh
index 9541ca0..efa00a2 100644
--- a/scripts/image_signing/common.sh
+++ b/scripts/image_signing/common.sh
@@ -10,25 +10,6 @@
 . "$(dirname "$0")/common_minimal.sh"
 CROS_LOG_PREFIX="${PROG}: "
 
-# Array of actions that are executed during the clean up process.
-declare -a cleanup_actions
-
-# Adds an action to be executed during the clean up process.
-# Actions are executed in the reverse order of when they were added.
-# ARGS: ACTION
-add_cleanup_action() {
-  cleanup_actions[${#cleanup_actions[*]}]=$1
-}
-
-# Performs the latest clean up action and removes it from the list.
-perform_latest_cleanup_action() {
-  local num_actions=${#cleanup_actions[*]}
-  if [ "${num_actions}" -gt 0 ]; then
-    eval "${cleanup_actions[$num_actions-1]} || true" > /dev/null 2>&1
-    unset "cleanup_actions[$num_actions-1]"
-  fi
-}
-
 # Performs clean up by executing actions in the cleanup_actions array in
 # reversed order.
 cleanup() {
@@ -36,9 +17,8 @@
   rv=$?
   set +e
 
-  while [ ${#cleanup_actions[*]} -gt 0 ]; do
-    perform_latest_cleanup_action
-  done
+  cleanup_temps_and_mounts
+  cleanup_loopbacks
 
   set -e
   return $rv
@@ -158,6 +138,3 @@
 
 # This will override the trap set in common_minmal.sh
 trap "cleanup" INT TERM EXIT
-
-add_cleanup_action "cleanup_temps_and_mounts"
-add_cleanup_action "cleanup_loopbacks"