Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: enter_chroot.sh

Issue 6525020: enter_chroot: Only mount SSH auth socket when we need to. (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/crosutils.git@master
Patch Set: Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/bin/bash 1 #!/bin/bash
2 2
3 # Copyright (c) 2009 The Chromium OS Authors. All rights reserved. 3 # Copyright (c) 2009 The Chromium OS Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be 4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file. 5 # found in the LICENSE file.
6 6
7 # Script to enter the chroot environment 7 # Script to enter the chroot environment
8 8
9 # --- BEGIN COMMON.SH BOILERPLATE --- 9 # --- BEGIN COMMON.SH BOILERPLATE ---
10 # Load common CrOS utilities. Inside the chroot this file is installed in 10 # Load common CrOS utilities. Inside the chroot this file is installed in
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 the command nor args should include single quotes. For example: 66 the command nor args should include single quotes. For example:
67 67
68 $0 -- ./build_platform_packages.sh 68 $0 -- ./build_platform_packages.sh
69 69
70 Otherwise, provides an interactive shell. 70 Otherwise, provides an interactive shell.
71 " 71 "
72 72
73 # Version of info from common.sh that only echos if --verbose is set. 73 # Version of info from common.sh that only echos if --verbose is set.
74 function debug { 74 function debug {
75 if [ $FLAGS_verbose -eq $FLAGS_TRUE ]; then 75 if [ $FLAGS_verbose -eq $FLAGS_TRUE ]; then
76 info "$1" 76 info "$*"
77 fi 77 fi
78 } 78 }
79 79
80 # Double up on the first '--' argument. Why? For enter_chroot, we want to 80 # Double up on the first '--' argument. Why? For enter_chroot, we want to
81 # emulate the behavior of sudo for setting environment vars. That is, we want: 81 # emulate the behavior of sudo for setting environment vars. That is, we want:
82 # ./enter_chroot [flags] [VAR=val] [-- command] 82 # ./enter_chroot [flags] [VAR=val] [-- command]
83 # ...but shflags ends up eating the '--' out of the command line and gives 83 # ...but shflags ends up eating the '--' out of the command line and gives
84 # us back "VAR=val" and "command" together in one chunk. By doubling up, we 84 # us back "VAR=val" and "command" together in one chunk. By doubling up, we
85 # end up getting what we want back from shflags. 85 # end up getting what we want back from shflags.
86 # 86 #
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 CHROME_ROOT_CONFIG="/var/cache/chrome_root" # inside chroot 125 CHROME_ROOT_CONFIG="/var/cache/chrome_root" # inside chroot
126 INNER_DEPOT_TOOLS_ROOT="/home/$USER/depot_tools" # inside chroot 126 INNER_DEPOT_TOOLS_ROOT="/home/$USER/depot_tools" # inside chroot
127 FUSE_DEVICE="/dev/fuse" 127 FUSE_DEVICE="/dev/fuse"
128 AUTOMOUNT_PREF="/apps/nautilus/preferences/media_automount" 128 AUTOMOUNT_PREF="/apps/nautilus/preferences/media_automount"
129 SAVED_AUTOMOUNT_PREF_FILE="/tmp/.automount_pref" 129 SAVED_AUTOMOUNT_PREF_FILE="/tmp/.automount_pref"
130 130
131 sudo chmod 0777 "$FLAGS_chroot/var/lock" 131 sudo chmod 0777 "$FLAGS_chroot/var/lock"
132 132
133 LOCKFILE="$FLAGS_chroot/var/lock/enter_chroot" 133 LOCKFILE="$FLAGS_chroot/var/lock/enter_chroot"
134 134
135
136 function ensure_mounted {
137 # If necessary, mount $source in the host FS at $target inside the
138 # chroot directory with $mount_args.
139 local source="$1"
140 local mount_args="$2"
141 local target="$3"
142
143 local mounted_path="$(readlink -f "${FLAGS_chroot}/$target")"
144
145 if [ -z "$(mount | grep -F "on ${mounted_path} ")" ]; then
146 # NB: mount_args deliberately left unquoted
147 debug mount ${mount_args} "${source}" "${mounted_path}"
148 sudo -- mount ${mount_args} "${source}" "${mounted_path}" || \
149 die "Could not mount ${source} on ${mounted_path}"
150 fi
151 }
152
135 function setup_env { 153 function setup_env {
136 # Validate sudo timestamp before entering the critical section so that we 154 # Validate sudo timestamp before entering the critical section so that we
137 # don't stall for a password while we have the lockfile. 155 # don't stall for a password while we have the lockfile.
138 # Don't use sudo -v since that has issues on machines w/ no password. 156 # Don't use sudo -v since that has issues on machines w/ no password.
139 sudo echo "" > /dev/null 157 sudo echo "" > /dev/null
140 158
141 ( 159 (
142 flock 200 160 flock 200
143 echo $$ >> "$LOCKFILE" 161 echo $$ >> "$LOCKFILE"
144 162
145 debug "Mounting chroot environment." 163 debug "Mounting chroot environment."
146 164 ensure_mounted none "-t proc" /proc
147 # Mount only if not already mounted 165 ensure_mounted none "-t sysfs" /sys
148 MOUNTED_PATH="$(readlink -f "$FLAGS_chroot/proc")" 166 ensure_mounted /dev "--bind" /dev
149 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]; then 167 ensure_mounted none "-t devpts" /dev/pts
150 sudo mount none -t proc "$MOUNTED_PATH" || \ 168 ensure_mounted "${FLAGS_trunk}" "--bind" "${CHROOT_TRUNK_DIR}"
151 die "Could not mount $MOUNTED_PATH"
152 fi
153
154 MOUNTED_PATH="$(readlink -f "$FLAGS_chroot/sys")"
155 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]; then
156 sudo mount none -t sysfs "$MOUNTED_PATH" || \
157 die "Could not mount $MOUNTED_PATH"
158 fi
159
160 MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}/dev")"
161 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]; then
162 sudo mount --bind /dev "$MOUNTED_PATH" || \
163 die "Could not mount $MOUNTED_PATH"
164 fi
165 169
166 if [ $FLAGS_ssh_agent -eq $FLAGS_TRUE ]; then 170 if [ $FLAGS_ssh_agent -eq $FLAGS_TRUE ]; then
167 TARGET_DIR="$(readlink -f "${FLAGS_chroot}/home/${USER}/.ssh")" 171 TARGET_DIR="$(readlink -f "${FLAGS_chroot}/home/${USER}/.ssh")"
168 if [ -n "${SSH_AUTH_SOCK}" -a -d "${HOME}/.ssh" ]; then 172 if [ -n "${SSH_AUTH_SOCK}" -a -d "${HOME}/.ssh" ]; then
169 mkdir -p "${TARGET_DIR}" 173 mkdir -p "${TARGET_DIR}"
170 cp -r "${HOME}/.ssh/known_hosts" "${TARGET_DIR}" 174 cp -r "${HOME}/.ssh/known_hosts" "${TARGET_DIR}"
171 cp -r "${HOME}/.ssh/config" "${TARGET_DIR}" 175 cp -r "${HOME}/.ssh/config" "${TARGET_DIR}"
172 ASOCK="$(dirname "${SSH_AUTH_SOCK}")" 176 ASOCK="$(dirname "${SSH_AUTH_SOCK}")"
173 mkdir -p "${FLAGS_chroot}/${ASOCK}" 177 ensure_mounted "${ASOCK}" "--bind" "${ASOCK}"
174 sudo mount --bind "${ASOCK}" "${FLAGS_chroot}/${ASOCK}" || \
175 die "Count not mount ${ASOCK}"
176 fi 178 fi
177 fi 179 fi
178 180
179 MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}/dev/pts")"
180 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]; then
181 sudo mount none -t devpts "$MOUNTED_PATH" || \
182 die "Could not mount $MOUNTED_PATH"
183 fi
184
185 MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}$CHROOT_TRUNK_DIR")"
186 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]; then
187 sudo mount --bind "$FLAGS_trunk" "$MOUNTED_PATH" || \
188 die "Could not mount $MOUNTED_PATH"
189 fi
190
191 MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}${INNER_CHROME_ROOT}")" 181 MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}${INNER_CHROME_ROOT}")"
192 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]; then 182 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]; then
193 ! CHROME_ROOT="$(readlink -f "$FLAGS_chrome_root")" 183 ! CHROME_ROOT="$(readlink -f "$FLAGS_chrome_root")"
194 if [ -z "$CHROME_ROOT" ]; then 184 if [ -z "$CHROME_ROOT" ]; then
195 ! CHROME_ROOT="$(cat "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" \ 185 ! CHROME_ROOT="$(cat "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" \
196 2>/dev/null)" 186 2>/dev/null)"
197 CHROME_ROOT_AUTO=1 187 CHROME_ROOT_AUTO=1
198 fi 188 fi
199 if [[ ( -n "$CHROME_ROOT" ) ]]; then 189 if [[ ( -n "$CHROME_ROOT" ) ]]; then
200 if [[ ( ! -d "${CHROME_ROOT}/src" ) ]]; then 190 if [[ ( ! -d "${CHROME_ROOT}/src" ) ]]; then
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
371 # In that case, check against origin/HEAD and mark** revision. 361 # In that case, check against origin/HEAD and mark** revision.
372 # Use git:8 chars of sha1 362 # Use git:8 chars of sha1
373 REVISION=$(cd ${FLAGS_trunk}/src/scripts ; git rev-parse --short=8 HEAD) 363 REVISION=$(cd ${FLAGS_trunk}/src/scripts ; git rev-parse --short=8 HEAD)
374 CHROOT_PASSTHRU="CHROMEOS_REVISION=$REVISION BUILDBOT_BUILD=$FLAGS_build_number CHROMEOS_OFFICIAL=$CHROMEOS_OFFICIAL" 364 CHROOT_PASSTHRU="CHROMEOS_REVISION=$REVISION BUILDBOT_BUILD=$FLAGS_build_number CHROMEOS_OFFICIAL=$CHROMEOS_OFFICIAL"
375 CHROOT_PASSTHRU="${CHROOT_PASSTHRU} \ 365 CHROOT_PASSTHRU="${CHROOT_PASSTHRU} \
376 CHROMEOS_RELEASE_APPID=${CHROMEOS_RELEASE_APPID:-"{DEV-BUILD}"}" 366 CHROMEOS_RELEASE_APPID=${CHROMEOS_RELEASE_APPID:-"{DEV-BUILD}"}"
377 CHROOT_PASSTHRU="${CHROOT_PASSTHRU} \ 367 CHROOT_PASSTHRU="${CHROOT_PASSTHRU} \
378 CHROMEOS_VERSION_TRACK=$CHROMEOS_VERSION_TRACK CHROMEOS_VERSION_AUSERVER=$CHROME OS_VERSION_AUSERVER CHROMEOS_VERSION_DEVSERVER=$CHROMEOS_VERSION_DEVSERVER" 368 CHROMEOS_VERSION_TRACK=$CHROMEOS_VERSION_TRACK CHROMEOS_VERSION_AUSERVER=$CHROME OS_VERSION_AUSERVER CHROMEOS_VERSION_DEVSERVER=$CHROMEOS_VERSION_DEVSERVER"
379 369
380 if [ -d "$HOME/.subversion" ]; then 370 if [ -d "$HOME/.subversion" ]; then
381 # Bind mounting .subversion into chroot 371 TARGET="/home/${USER}/.subversion"
382 debug "mounting ~/.subversion into chroot" 372 mkdir -p "${FLAGS_chroot}${TARGET}"
383 MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}/home/${USER}/.subversion")" 373 ensure_mounted "${HOME}/.subversion" "--bind" "${TARGET}"
384 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]; then
385 mkdir -p "$MOUNTED_PATH"
386 sudo mount --bind "$HOME/.subversion" "$MOUNTED_PATH" || \
387 die "Could not mount $MOUNTED_PATH"
388 fi
389 fi 374 fi
390 375
391 # Configure committer username and email in chroot .gitconfig 376 # Configure committer username and email in chroot .gitconfig
392 if [ $FLAGS_git_config -eq $FLAGS_TRUE ]; then 377 if [ $FLAGS_git_config -eq $FLAGS_TRUE ]; then
393 git config -f ${FLAGS_chroot}/home/${USER}/.gitconfig --replace-all \ 378 git config -f ${FLAGS_chroot}/home/${USER}/.gitconfig --replace-all \
394 user.name "$(cd /tmp; git var GIT_COMMITTER_IDENT | sed -e 's/ *<.*//')" 379 user.name "$(cd /tmp; git var GIT_COMMITTER_IDENT | sed -e 's/ *<.*//')"
395 git config -f ${FLAGS_chroot}/home/${USER}/.gitconfig --replace-all \ 380 git config -f ${FLAGS_chroot}/home/${USER}/.gitconfig --replace-all \
396 user.email "$(cd /tmp; git var GIT_COMMITTER_IDENT | \ 381 user.email "$(cd /tmp; git var GIT_COMMITTER_IDENT | \
397 sed -e 's/.*<\([^>]*\)>.*/\1/')" 382 sed -e 's/.*<\([^>]*\)>.*/\1/')"
398 fi 383 fi
399 384
400 # Run command or interactive shell. Also include the non-chrooted path to 385 # Run command or interactive shell. Also include the non-chrooted path to
401 # the source trunk for scripts that may need to print it (e.g. 386 # the source trunk for scripts that may need to print it (e.g.
402 # build_image.sh). 387 # build_image.sh).
403 sudo -- chroot "$FLAGS_chroot" sudo -i -u $USER $CHROOT_PASSTHRU \ 388 sudo -- chroot "$FLAGS_chroot" sudo -i -u $USER $CHROOT_PASSTHRU \
404 EXTERNAL_TRUNK_PATH="${FLAGS_trunk}" LANG=C SSH_AGENT_PID="${SSH_AGENT_PID}" \ 389 EXTERNAL_TRUNK_PATH="${FLAGS_trunk}" LANG=C SSH_AGENT_PID="${SSH_AGENT_PID}" \
405 SSH_AUTH_SOCK="${SSH_AUTH_SOCK}" "$@" 390 SSH_AUTH_SOCK="${SSH_AUTH_SOCK}" "$@"
406 391
407 # Remove trap and explicitly unmount 392 # Remove trap and explicitly unmount
408 trap - EXIT 393 trap - EXIT
409 teardown_env 394 teardown_env
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698