Add more information to loopback mount failures.

Buildbots are periodically failing to build images; the evidence
points to failures in mount_gpt_image.sh, but it's inconclusive.
Add detailed error messages to mount failures so as to be able
to debug more the next time it happens.

BUG=chromium-os:24975
TEST=build_image, to see that successful cases still work
TEST=repeatedly mount new images with mount_gpt_image until it fails

Change-Id: I05a09c8eec46d9296a24ff36904a4aea20d2aae5
Reviewed-on: https://gerrit.chromium.org/gerrit/14433
Reviewed-by: David James <davidjames@chromium.org>
Tested-by: Richard Barnette <jrbarnette@chromium.org>
diff --git a/mount_gpt_image.sh b/mount_gpt_image.sh
index 368af5c..3ab2e99 100755
--- a/mount_gpt_image.sh
+++ b/mount_gpt_image.sh
@@ -129,19 +129,31 @@
     enable_rw_mount "${FLAGS_from}/${filename}" "$(( offset * 512 ))"
   fi
 
-  sudo mount ${safe_flag} -o loop,offset=$(( offset * 512 )) \
-    "${FLAGS_from}/${filename}" "${FLAGS_rootfs_mountpt}"
+  if ! sudo mount ${safe_flag} -o loop,offset=$(( offset * 512 )) \
+      "${FLAGS_from}/${filename}" "${FLAGS_rootfs_mountpt}" ; then
+    error "mount failed: options=${safe_flag} offset=$(( offset * 512 ))" \
+        "target=${FLAGS_rootfs_mountpt}"
+    return 1
+  fi
 
   # Mount the stateful partition using a loopback device.
   offset=$(partoffset "${FLAGS_from}/${filename}" 1)
-  sudo mount ${ro_flag} -o loop,offset=$(( offset * 512 )) \
-    "${FLAGS_from}/${filename}" "${FLAGS_stateful_mountpt}"
+  if ! sudo mount ${ro_flag} -o loop,offset=$(( offset * 512 )) \
+      "${FLAGS_from}/${filename}" "${FLAGS_stateful_mountpt}" ; then
+    error "mount failed: options=${ro_flag} offset=$(( offset * 512 ))" \
+        "target=${FLAGS_stateful_mountpt}"
+    return 1
+  fi
 
   # Mount the stateful partition using a loopback device.
   if [[ -n "${FLAGS_esp_mountpt}" ]]; then
     offset=$(partoffset "${FLAGS_from}/${filename}" 12)
-    sudo mount ${ro_flag} -o loop,offset=$(( offset * 512 )) \
-      "${FLAGS_from}/${filename}" "${FLAGS_esp_mountpt}"
+    if ! sudo mount ${ro_flag} -o loop,offset=$(( offset * 512 )) \
+        "${FLAGS_from}/${filename}" "${FLAGS_esp_mountpt}" ; then
+      error "mount failed: options=${ro_flag} offset=$(( offset * 512 ))" \
+          "target=${FLAGS_esp_mountpt}"
+      return 1
+    fi
   fi
 }
 
@@ -156,8 +168,10 @@
   # Get the partitions for the image / device.
   if [ -b ${FLAGS_from} ] ; then
     get_usb_partitions
-  else
-    get_gpt_partitions
+  elif ! get_gpt_partitions ; then
+    echo "Current loopback device status:"
+    sudo losetup --all | sed 's/^/    /'
+    die "Failed to mount all partitions in ${FLAGS_from}/${FLAGS_image}"
   fi
 
   # Mount directories and setup symlinks.