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
This commit is contained in:
David Rochberg 2011-02-16 11:14:00 -05:00
parent da36382f03
commit 351a76fc96

View File

@ -73,7 +73,7 @@ Otherwise, provides an interactive shell.
# Version of info from common.sh that only echos if --verbose is set. # Version of info from common.sh that only echos if --verbose is set.
function debug { function debug {
if [ $FLAGS_verbose -eq $FLAGS_TRUE ]; then if [ $FLAGS_verbose -eq $FLAGS_TRUE ]; then
info "$1" info "$*"
fi fi
} }
@ -132,6 +132,24 @@ sudo chmod 0777 "$FLAGS_chroot/var/lock"
LOCKFILE="$FLAGS_chroot/var/lock/enter_chroot" 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 { function setup_env {
# Validate sudo timestamp before entering the critical section so that we # Validate sudo timestamp before entering the critical section so that we
# don't stall for a password while we have the lockfile. # don't stall for a password while we have the lockfile.
@ -143,25 +161,11 @@ function setup_env {
echo $$ >> "$LOCKFILE" echo $$ >> "$LOCKFILE"
debug "Mounting chroot environment." debug "Mounting chroot environment."
ensure_mounted none "-t proc" /proc
# Mount only if not already mounted ensure_mounted none "-t sysfs" /sys
MOUNTED_PATH="$(readlink -f "$FLAGS_chroot/proc")" ensure_mounted /dev "--bind" /dev
if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]; then ensure_mounted none "-t devpts" /dev/pts
sudo mount none -t proc "$MOUNTED_PATH" || \ ensure_mounted "${FLAGS_trunk}" "--bind" "${CHROOT_TRUNK_DIR}"
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
if [ $FLAGS_ssh_agent -eq $FLAGS_TRUE ]; then if [ $FLAGS_ssh_agent -eq $FLAGS_TRUE ]; then
TARGET_DIR="$(readlink -f "${FLAGS_chroot}/home/${USER}/.ssh")" 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/known_hosts" "${TARGET_DIR}"
cp -r "${HOME}/.ssh/config" "${TARGET_DIR}" cp -r "${HOME}/.ssh/config" "${TARGET_DIR}"
ASOCK="$(dirname "${SSH_AUTH_SOCK}")" ASOCK="$(dirname "${SSH_AUTH_SOCK}")"
mkdir -p "${FLAGS_chroot}/${ASOCK}" ensure_mounted "${ASOCK}" "--bind" "${ASOCK}"
sudo mount --bind "${ASOCK}" "${FLAGS_chroot}/${ASOCK}" || \
die "Count not mount ${ASOCK}"
fi fi
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}")" MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}${INNER_CHROME_ROOT}")"
if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]; then if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]; then
! CHROME_ROOT="$(readlink -f "$FLAGS_chrome_root")" ! 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" CHROMEOS_VERSION_TRACK=$CHROMEOS_VERSION_TRACK CHROMEOS_VERSION_AUSERVER=$CHROMEOS_VERSION_AUSERVER CHROMEOS_VERSION_DEVSERVER=$CHROMEOS_VERSION_DEVSERVER"
if [ -d "$HOME/.subversion" ]; then if [ -d "$HOME/.subversion" ]; then
# Bind mounting .subversion into chroot TARGET="/home/${USER}/.subversion"
debug "mounting ~/.subversion into chroot" mkdir -p "${FLAGS_chroot}${TARGET}"
MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}/home/${USER}/.subversion")" ensure_mounted "${HOME}/.subversion" "--bind" "${TARGET}"
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
fi fi
# Configure committer username and email in chroot .gitconfig # Configure committer username and email in chroot .gitconfig