From 351a76fc96dd56ce1baf742ee10722cde5766864 Mon Sep 17 00:00:00 2001 From: David Rochberg Date: Wed, 16 Feb 2011 11:14:00 -0500 Subject: [PATCH] enter_chroot: Only mount SSH auth socket when we need to. Also refactors some cut-and-paste code Fix: makes mountpoint for subversion directory BUG=chromium-os:11944 chromium-os:12058 TEST=enter_chroot.sh with and without another enter_chroot running. Note that there are 0 or 1 mounts of the /tmp/ssh-.... directory. Check that /proc,/sys,/dev,$SSH_AUTH_SOCK,/dev/pts,/home/${USER}/trunk,/home/${USER}/.subversion are all mounted. Test that subversion mountpoint is created Change-Id: I9dada6f7f98d263345af29a5734c1c70709f6a1e Review URL: http://codereview.chromium.org/6498001 Review URL: http://codereview.chromium.org/6525020 --- enter_chroot.sh | 71 +++++++++++++++++++------------------------------ 1 file changed, 28 insertions(+), 43 deletions(-) diff --git a/enter_chroot.sh b/enter_chroot.sh index 8e3d5898ec..ac34ad961f 100755 --- a/enter_chroot.sh +++ b/enter_chroot.sh @@ -73,7 +73,7 @@ Otherwise, provides an interactive shell. # Version of info from common.sh that only echos if --verbose is set. function debug { if [ $FLAGS_verbose -eq $FLAGS_TRUE ]; then - info "$1" + info "$*" fi } @@ -132,6 +132,24 @@ sudo chmod 0777 "$FLAGS_chroot/var/lock" LOCKFILE="$FLAGS_chroot/var/lock/enter_chroot" + +function ensure_mounted { + # If necessary, mount $source in the host FS at $target inside the + # chroot directory with $mount_args. + local source="$1" + local mount_args="$2" + local target="$3" + + local mounted_path="$(readlink -f "${FLAGS_chroot}/$target")" + + if [ -z "$(mount | grep -F "on ${mounted_path} ")" ]; then + # NB: mount_args deliberately left unquoted + debug mount ${mount_args} "${source}" "${mounted_path}" + sudo -- mount ${mount_args} "${source}" "${mounted_path}" || \ + die "Could not mount ${source} on ${mounted_path}" + fi +} + function setup_env { # Validate sudo timestamp before entering the critical section so that we # don't stall for a password while we have the lockfile. @@ -143,25 +161,11 @@ function setup_env { echo $$ >> "$LOCKFILE" debug "Mounting chroot environment." - - # Mount only if not already mounted - MOUNTED_PATH="$(readlink -f "$FLAGS_chroot/proc")" - if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]; then - sudo mount none -t proc "$MOUNTED_PATH" || \ - die "Could not mount $MOUNTED_PATH" - fi - - MOUNTED_PATH="$(readlink -f "$FLAGS_chroot/sys")" - if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]; then - sudo mount none -t sysfs "$MOUNTED_PATH" || \ - die "Could not mount $MOUNTED_PATH" - fi - - MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}/dev")" - if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]; then - sudo mount --bind /dev "$MOUNTED_PATH" || \ - die "Could not mount $MOUNTED_PATH" - fi + ensure_mounted none "-t proc" /proc + ensure_mounted none "-t sysfs" /sys + ensure_mounted /dev "--bind" /dev + ensure_mounted none "-t devpts" /dev/pts + ensure_mounted "${FLAGS_trunk}" "--bind" "${CHROOT_TRUNK_DIR}" if [ $FLAGS_ssh_agent -eq $FLAGS_TRUE ]; then TARGET_DIR="$(readlink -f "${FLAGS_chroot}/home/${USER}/.ssh")" @@ -170,24 +174,10 @@ function setup_env { cp -r "${HOME}/.ssh/known_hosts" "${TARGET_DIR}" cp -r "${HOME}/.ssh/config" "${TARGET_DIR}" ASOCK="$(dirname "${SSH_AUTH_SOCK}")" - mkdir -p "${FLAGS_chroot}/${ASOCK}" - sudo mount --bind "${ASOCK}" "${FLAGS_chroot}/${ASOCK}" || \ - die "Count not mount ${ASOCK}" + ensure_mounted "${ASOCK}" "--bind" "${ASOCK}" fi fi - MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}/dev/pts")" - if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]; then - sudo mount none -t devpts "$MOUNTED_PATH" || \ - die "Could not mount $MOUNTED_PATH" - fi - - MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}$CHROOT_TRUNK_DIR")" - if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]; then - sudo mount --bind "$FLAGS_trunk" "$MOUNTED_PATH" || \ - die "Could not mount $MOUNTED_PATH" - fi - MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}${INNER_CHROME_ROOT}")" if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]; then ! CHROME_ROOT="$(readlink -f "$FLAGS_chrome_root")" @@ -378,14 +368,9 @@ CHROOT_PASSTHRU="${CHROOT_PASSTHRU} \ CHROMEOS_VERSION_TRACK=$CHROMEOS_VERSION_TRACK CHROMEOS_VERSION_AUSERVER=$CHROMEOS_VERSION_AUSERVER CHROMEOS_VERSION_DEVSERVER=$CHROMEOS_VERSION_DEVSERVER" if [ -d "$HOME/.subversion" ]; then - # Bind mounting .subversion into chroot - debug "mounting ~/.subversion into chroot" - MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}/home/${USER}/.subversion")" - if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]; then - mkdir -p "$MOUNTED_PATH" - sudo mount --bind "$HOME/.subversion" "$MOUNTED_PATH" || \ - die "Could not mount $MOUNTED_PATH" - fi + TARGET="/home/${USER}/.subversion" + mkdir -p "${FLAGS_chroot}${TARGET}" + ensure_mounted "${HOME}/.subversion" "--bind" "${TARGET}" fi # Configure committer username and email in chroot .gitconfig