| OLD | NEW |
| 1 #!/bin/bash | 1 #!/bin/bash |
| 2 | 2 |
| 3 # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. | 3 # Copyright (c) 2011 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 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 INNER_CHROME_ROOT=$FLAGS_chrome_root_mount # inside chroot | 122 INNER_CHROME_ROOT=$FLAGS_chrome_root_mount # inside chroot |
| 123 CHROME_ROOT_CONFIG="/var/cache/chrome_root" # inside chroot | 123 CHROME_ROOT_CONFIG="/var/cache/chrome_root" # inside chroot |
| 124 INNER_DEPOT_TOOLS_ROOT="/home/$USER/depot_tools" # inside chroot | 124 INNER_DEPOT_TOOLS_ROOT="/home/$USER/depot_tools" # inside chroot |
| 125 FUSE_DEVICE="/dev/fuse" | 125 FUSE_DEVICE="/dev/fuse" |
| 126 AUTOMOUNT_PREF="/apps/nautilus/preferences/media_automount" | 126 AUTOMOUNT_PREF="/apps/nautilus/preferences/media_automount" |
| 127 SAVED_AUTOMOUNT_PREF_FILE="/tmp/.automount_pref" | 127 SAVED_AUTOMOUNT_PREF_FILE="/tmp/.automount_pref" |
| 128 | 128 |
| 129 sudo chmod 0777 "$FLAGS_chroot/var/lock" | 129 sudo chmod 0777 "$FLAGS_chroot/var/lock" |
| 130 | 130 |
| 131 LOCKFILE="$FLAGS_chroot/var/lock/enter_chroot" | 131 LOCKFILE="$FLAGS_chroot/var/lock/enter_chroot" |
| 132 SYNCERPIDFILE="${FLAGS_chroot}/var/tmp/enter_chroot_sync.pid" |
| 132 | 133 |
| 133 | 134 |
| 134 function ensure_mounted { | 135 function ensure_mounted { |
| 135 # If necessary, mount $source in the host FS at $target inside the | 136 # If necessary, mount $source in the host FS at $target inside the |
| 136 # chroot directory with $mount_args. | 137 # chroot directory with $mount_args. |
| 137 local source="$1" | 138 local source="$1" |
| 138 local mount_args="$2" | 139 local mount_args="$2" |
| 139 local target="$3" | 140 local target="$3" |
| 140 | 141 |
| 141 local mounted_path="$(readlink -f "${FLAGS_chroot}/$target")" | 142 local mounted_path="$(readlink -f "${FLAGS_chroot}/$target")" |
| 142 | 143 |
| 143 if [ -z "$(mount | grep -F "on ${mounted_path} ")" ]; then | 144 if [ -z "$(mount | grep -F "on ${mounted_path} ")" ]; then |
| 144 # Attempt to make the mountpoint as the user. This depends on the | 145 # Attempt to make the mountpoint as the user. This depends on the |
| 145 # fact that all mountpoints that should be owned by root are | 146 # fact that all mountpoints that should be owned by root are |
| 146 # already present. | 147 # already present. |
| 147 mkdir -p "${mounted_path}" | 148 mkdir -p "${mounted_path}" |
| 148 | 149 |
| 149 # NB: mount_args deliberately left unquoted | 150 # NB: mount_args deliberately left unquoted |
| 150 debug mount ${mount_args} "${source}" "${mounted_path}" | 151 debug mount ${mount_args} "${source}" "${mounted_path}" |
| 151 sudo -- mount ${mount_args} "${source}" "${mounted_path}" || \ | 152 sudo -- mount ${mount_args} "${source}" "${mounted_path}" || \ |
| 152 die "Could not mount ${source} on ${mounted_path}" | 153 die "Could not mount ${source} on ${mounted_path}" |
| 153 fi | 154 fi |
| 154 } | 155 } |
| 155 | 156 |
| 157 function env_sync_proc { |
| 158 # This function runs and performs periodic updates to the chroot env, if |
| 159 # necessary. |
| 160 |
| 161 local poll_interval=10 |
| 162 local sync_files="etc/resolv.conf etc/hosts" |
| 163 |
| 164 # Make sure the synced files are writable by normal user, so that we |
| 165 # don't have to sudo inside the loop. |
| 166 for file in ${sync_files}; do |
| 167 sudo chown ${USER} ${FLAGS_chroot}/${file} 1>&2 |
| 168 done |
| 169 |
| 170 while true; do |
| 171 # Sync files |
| 172 for file in ${sync_files}; do |
| 173 if ! cmp /${file} ${FLAGS_chroot}/${file} &> /dev/null; then |
| 174 cp -f /${file} ${FLAGS_chroot}/${file} |
| 175 fi |
| 176 done |
| 177 |
| 178 sleep ${poll_interval} |
| 179 done |
| 180 } |
| 181 |
| 156 function setup_env { | 182 function setup_env { |
| 157 # Validate sudo timestamp before entering the critical section so that we | 183 # Validate sudo timestamp before entering the critical section so that we |
| 158 # don't stall for a password while we have the lockfile. | 184 # don't stall for a password while we have the lockfile. |
| 159 # Don't use sudo -v since that has issues on machines w/ no password. | 185 # Don't use sudo -v since that has issues on machines w/ no password. |
| 160 sudo echo "" > /dev/null | 186 sudo echo "" > /dev/null |
| 161 | 187 |
| 188 # Syncer proc, but only the first time |
| 189 if ! [ -f "${SYNCERPIDFILE}" ] || \ |
| 190 ! [ -d /proc/$(cat "${SYNCERPIDFILE}") ]; then |
| 191 debug "Starting sync process" |
| 192 env_sync_proc & |
| 193 echo $! > "${SYNCERPIDFILE}" |
| 194 disown $! |
| 195 fi |
| 196 |
| 162 ( | 197 ( |
| 163 flock 200 | 198 flock 200 |
| 164 echo $$ >> "$LOCKFILE" | 199 echo $$ >> "$LOCKFILE" |
| 165 | 200 |
| 166 debug "Mounting chroot environment." | 201 debug "Mounting chroot environment." |
| 167 ensure_mounted none "-t proc" /proc | 202 ensure_mounted none "-t proc" /proc |
| 168 ensure_mounted none "-t sysfs" /sys | 203 ensure_mounted none "-t sysfs" /sys |
| 169 ensure_mounted /dev "--bind" /dev | 204 ensure_mounted /dev "--bind" /dev |
| 170 ensure_mounted none "-t devpts" /dev/pts | 205 ensure_mounted none "-t devpts" /dev/pts |
| 171 ensure_mounted "${FLAGS_trunk}" "--bind" "${CHROOT_TRUNK_DIR}" | 206 ensure_mounted "${FLAGS_trunk}" "--bind" "${CHROOT_TRUNK_DIR}" |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 if [ $(which gconftool-2 2>/dev/null) ]; then | 311 if [ $(which gconftool-2 2>/dev/null) ]; then |
| 277 SAVED_PREF=$(cat "${FLAGS_chroot}${SAVED_AUTOMOUNT_PREF_FILE}") | 312 SAVED_PREF=$(cat "${FLAGS_chroot}${SAVED_AUTOMOUNT_PREF_FILE}") |
| 278 gconftool-2 -s --type=boolean ${AUTOMOUNT_PREF} ${SAVED_PREF} || \ | 313 gconftool-2 -s --type=boolean ${AUTOMOUNT_PREF} ${SAVED_PREF} || \ |
| 279 warn "could not re-set your automount preference." | 314 warn "could not re-set your automount preference." |
| 280 fi | 315 fi |
| 281 | 316 |
| 282 if [ -s "$LOCKFILE" ]; then | 317 if [ -s "$LOCKFILE" ]; then |
| 283 debug "At least one other pid is running in the chroot, so not" | 318 debug "At least one other pid is running in the chroot, so not" |
| 284 debug "tearing down env." | 319 debug "tearing down env." |
| 285 else | 320 else |
| 321 debug "Stopping syncer process" |
| 322 kill $(cat "${SYNCERPIDFILE}") |
| 323 |
| 286 MOUNTED_PATH=$(readlink -f "$FLAGS_chroot") | 324 MOUNTED_PATH=$(readlink -f "$FLAGS_chroot") |
| 287 debug "Unmounting chroot environment." | 325 debug "Unmounting chroot environment." |
| 288 # sort the list of mounts in reverse order, to ensure umount of | 326 # sort the list of mounts in reverse order, to ensure umount of |
| 289 # cascading mounts in proper order | 327 # cascading mounts in proper order |
| 290 for i in \ | 328 for i in \ |
| 291 $(mount | grep -F "on $MOUNTED_PATH/" | sort -r | awk '{print $3}'); do | 329 $(mount | grep -F "on $MOUNTED_PATH/" | sort -r | awk '{print $3}'); do |
| 292 safe_umount "$i" | 330 safe_umount "$i" |
| 293 done | 331 done |
| 294 fi | 332 fi |
| 295 ) 200>>"$LOCKFILE" || die "teardown_env failed" | 333 ) 200>>"$LOCKFILE" || die "teardown_env failed" |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 380 # Run command or interactive shell. Also include the non-chrooted path to | 418 # Run command or interactive shell. Also include the non-chrooted path to |
| 381 # the source trunk for scripts that may need to print it (e.g. | 419 # the source trunk for scripts that may need to print it (e.g. |
| 382 # build_image.sh). | 420 # build_image.sh). |
| 383 sudo -- chroot "$FLAGS_chroot" sudo -i -u $USER $CHROOT_PASSTHRU \ | 421 sudo -- chroot "$FLAGS_chroot" sudo -i -u $USER $CHROOT_PASSTHRU \ |
| 384 EXTERNAL_TRUNK_PATH="${FLAGS_trunk}" LANG=C SSH_AGENT_PID="${SSH_AGENT_PID}" \ | 422 EXTERNAL_TRUNK_PATH="${FLAGS_trunk}" LANG=C SSH_AGENT_PID="${SSH_AGENT_PID}" \ |
| 385 SSH_AUTH_SOCK="${SSH_AUTH_SOCK}" "$@" | 423 SSH_AUTH_SOCK="${SSH_AUTH_SOCK}" "$@" |
| 386 | 424 |
| 387 # Remove trap and explicitly unmount | 425 # Remove trap and explicitly unmount |
| 388 trap - EXIT | 426 trap - EXIT |
| 389 teardown_env | 427 teardown_env |
| OLD | NEW |