build_image: cache DISK_LAYOUT_PATH early

Since get_disk_layout_path gets run in subshells, the computed value of
DISK_LAYOUT_PATH never makes it to the larger env.  Even if it did, we
currently always recompute it.  This can be a little expensive since it
involves walking all our overlays to find the json file.

Instead, let's compute this value once before we start building the image
and have the code use that value if it's been set already.  This should
be backwards compatible.

TEST=`./build_image` produces the same image

Change-Id: I7360995982f35289eda0c029493d435909b587b3
Reviewed-by: Liam McLoughlin <>
Commit-Queue: Mike Frysinger <>
Tested-by: Mike Frysinger <>
diff --git a/build_library/ b/build_library/
index eb8900d..170f5f3 100755
--- a/build_library/
+++ b/build_library/
@@ -62,6 +62,8 @@
   check_valid_layout ${image_type}
   info "Using image type ${image_type}"
+  get_disk_layout_path
+  info "Using disk layout ${DISK_LAYOUT_PATH}"
diff --git a/build_library/ b/build_library/
index e8ed70f..a872934 100644
--- a/build_library/
+++ b/build_library/
@@ -4,6 +4,7 @@
 cgpt_py() {
   if [[ -n "${FLAGS_adjust_part-}" ]]; then
@@ -19,6 +20,9 @@
 get_disk_layout_path() {
+  if [[ -n ${DISK_LAYOUT_PATH} ]]; then
+    return 0
+  fi
   local partition_script_path=$(tempfile)
   for overlay in $(cros_list_overlays --board "$BOARD"); do