cros_sdk: speed up unmounting when tearing down env

The teardown_env is a little slow when unmounting things because it runs
`sudo umount` once per mount point.  This is so that when things go wrong,
it can easily recover.  However, this slows down the common case at the
expense of the uncommon.

Refactor the code so that in the common case, we run one `sudo umount`.
When things do fail, we're a bit slower as we reparse the entire mount,
list, but that's fine as it's an error case.

BUG=None
TEST=enable `set -x`; run `cros_sdk true`; see all mount points unmounted in one shot

Change-Id: Iec98a7b9f51a77e90c30e6f6acae26e528b8c50d
Reviewed-on: http://gerrit.chromium.org/gerrit/7822
Tested-by: Mike Frysinger <vapier@chromium.org>
Reviewed-by: David James <davidjames@chromium.org>
This commit is contained in:
Mike Frysinger 2011-09-15 17:46:44 -04:00 committed by chrome-bot
parent 882cae5df4
commit 1aa6124f1d
2 changed files with 28 additions and 13 deletions

View File

@ -373,18 +373,38 @@ function sudo_append() {
sudo tee -a "$1" > /dev/null
}
# Locate all mounts below a specified directory.
#
# $1 - The root tree.
function sub_mounts() {
# Assume that `mount` outputs a list of mount points in the order
# that things were mounted (since it always has and hopefully always
# will). As such, we have to unmount in reverse order to cleanly
# unmount submounts (think /dev/pts and /dev).
mount | \
awk -v path="$1" -v l="${#1}" \
'(substr($3, 0, l) == path) { print $3 }' | \
tac
}
# Unmounts a directory, if the unmount fails, warn, and then lazily unmount.
#
# $1 - The path to unmount.
function safe_umount {
path=${1:?}
shift
function safe_umount_tree {
local mounts=$(sub_mounts "$1")
if ! sudo umount -d "${path}"; then
warn "Failed to unmount ${path}"
warn "Doing a lazy unmount"
# First try to unmount in one shot to speed things up.
if sudo umount -d ${mounts}; then
return 0
fi
sudo umount -d -l "${path}" || die "Failed to lazily unmount ${path}"
# Well that didn't work, so lazy unmount remaining ones.
mounts=$(sub_mounts "$1")
warn "Failed to unmount ${mounts}"
warn "Doing a lazy unmount"
if ! sudo umount -d -l ${mounts}; then
mounts=$(sub_mounts "$1")
die "Failed to lazily unmount ${mounts}"
fi
}

View File

@ -408,12 +408,7 @@ function teardown_env {
MOUNTED_PATH=$(readlink -f "$FLAGS_chroot")
debug "Unmounting chroot environment."
# sort the list of mounts in reverse order, to ensure umount of
# cascading mounts in proper order
for i in \
$(mount | grep -F "on $MOUNTED_PATH/" | sort -r | awk '{print $3}'); do
safe_umount "$i"
done
safe_umount_tree "${MOUNTED_PATH}/"
fi
) 200>>"$LOCKFILE" || die "teardown_env failed"
}