scripts: debug output for partitions.

Add more output to help debug partx errors.  Also sync the image file
where that may help.

BUG=chromium:1011891
TEST=unit tests pass

Change-Id: Ibbed570fbc23109ab701cb0db9d750f391294c53
Reviewed-on: https://chromium-review.googlesource.com/1857082
Tested-by: LaMont Jones <lamontjones@chromium.org>
Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com>
Legacy-Commit-Queue: Commit Bot <commit-bot@chromium.org>
Reviewed-by: George Engelbrecht <engeg@google.com>
Reviewed-by: Sean Abraham <seanabraham@chromium.org>
Reviewed-by: Alex Klein <saklein@chromium.org>
diff --git a/build_library/base_image_util.sh b/build_library/base_image_util.sh
index bfadd8a..95b1407 100755
--- a/build_library/base_image_util.sh
+++ b/build_library/base_image_util.sh
@@ -173,6 +173,9 @@
   local bootcache_enabled=$3
   local image_type="usb"
 
+  info "Entering create_base_image $*"
+  set -x
+
   if [[ "${FLAGS_disk_layout}" != "default" ]]; then
     image_type="${FLAGS_disk_layout}"
   else
@@ -195,6 +198,11 @@
   mkdir "${root_fs_dir}" "${stateful_fs_dir}" "${esp_fs_dir}"
   build_gpt_image "${BUILD_DIR}/${image_name}" "${image_type}"
 
+  # Try to get all of the dirty pages written to disk, so that mount doesn't
+  # return EAGAIN.
+  info "Syncing ${BUILD_DIR}/${image_name}."
+  sync -f "${BUILD_DIR}/${image_name}"
+
   trap "check_full_disk ; unmount_image ; delete_prompt" EXIT
   mount_image "${BUILD_DIR}/${image_name}" "${root_fs_dir}" \
     "${stateful_fs_dir}" "${esp_fs_dir}"
diff --git a/common.sh b/common.sh
index ffdc741..3d7ee52 100644
--- a/common.sh
+++ b/common.sh
@@ -615,6 +615,11 @@
 loopback_partscan() {
   local lb_dev image="$1"
   shift
+
+  # Flush any dirty pages in the image before we send losetup that way.
+  info "Running sync -f ${image}"
+  sync -f "${image}"
+
   lb_dev=$(sudo losetup --show -f "$@" "${image}")
 
   # Ignore problems deleting existing partitions. There shouldn't be any
diff --git a/mount_gpt_image.sh b/mount_gpt_image.sh
index c835240..4ada217 100755
--- a/mount_gpt_image.sh
+++ b/mount_gpt_image.sh
@@ -1,5 +1,8 @@
 #!/bin/bash
 
+echo "Entering $0 $*" >&2
+set -x
+
 # Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.