cros_sdk: add a mount cache to avoid multiple execs
Since we grab a lock before mounting anything, we know that the relevant
list of mounts won't change (since we haven't changed it yet). So we can
cache the output of `mount` and re-use that in every subsequent check.
BUG=None
TEST=`cros_sdk --enter` still works with simultaneous runs
TEST=`cros_sdk --enter -- ls` still works
Change-Id: I27c98c923761fb777686632fe6e6604ca543cfb6
Reviewed-on: http://gerrit.chromium.org/gerrit/8030
Commit-Ready: Mike Frysinger <vapier@chromium.org>
Reviewed-by: Mike Frysinger <vapier@chromium.org>
Tested-by: Mike Frysinger <vapier@chromium.org>
diff --git a/sdk_lib/enter_chroot.sh b/sdk_lib/enter_chroot.sh
index fff5e4e..69f62e5 100755
--- a/sdk_lib/enter_chroot.sh
+++ b/sdk_lib/enter_chroot.sh
@@ -95,7 +95,11 @@
local mounted_path="$(readlink -f "${FLAGS_chroot}/$target")"
- if [ -z "$(mount | grep -F "on ${mounted_path} ")" ]; then
+ case ${MOUNT_CACHE} in
+ *" on ${mounted_path} "*)
+ # Already mounted!
+ ;;
+ *)
# Attempt to make the mountpoint as the user. This depends on the
# fact that all mountpoints that should be owned by root are
# already present.
@@ -105,7 +109,8 @@
debug mount ${mount_args} "${source}" "${mounted_path}"
sudo -- mount ${mount_args} "${source}" "${mounted_path}" || \
die "Could not mount ${source} on ${mounted_path}"
- fi
+ ;;
+ esac
}
function env_sync_proc {
@@ -219,6 +224,7 @@
fi
debug "Mounting chroot environment."
+ MOUNT_CACHE=$(mount)
ensure_mounted none "-t proc" /proc
ensure_mounted none "-t sysfs" /sys
ensure_mounted /dev "--bind" /dev