OLD | NEW |
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 # Load common constants. This should be the first executable line. | 9 # Load common constants. This should be the first executable line. |
10 # The path to common.sh should be relative to your script's location. | 10 # The path to common.sh should be relative to your script's location. |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 FUSE_DEVICE="/dev/fuse" | 71 FUSE_DEVICE="/dev/fuse" |
72 AUTOMOUNT_PREF="/apps/nautilus/preferences/media_automount" | 72 AUTOMOUNT_PREF="/apps/nautilus/preferences/media_automount" |
73 SAVED_AUTOMOUNT_PREF_FILE="/tmp/.automount_pref" | 73 SAVED_AUTOMOUNT_PREF_FILE="/tmp/.automount_pref" |
74 | 74 |
75 sudo chmod 0777 "$FLAGS_chroot/var/lock" | 75 sudo chmod 0777 "$FLAGS_chroot/var/lock" |
76 | 76 |
77 LOCKFILE="$FLAGS_chroot/var/lock/enter_chroot" | 77 LOCKFILE="$FLAGS_chroot/var/lock/enter_chroot" |
78 | 78 |
79 function setup_env { | 79 function setup_env { |
80 ( | 80 ( |
| 81 local mounted_path |
81 flock 200 | 82 flock 200 |
82 echo $$ >> "$LOCKFILE" | 83 echo $$ >> "$LOCKFILE" |
83 | 84 |
84 info "Mounting chroot environment." | 85 info "Mounting chroot environment." |
85 | 86 |
86 # Mount only if not already mounted | 87 # Mount only if not already mounted |
87 MOUNTED_PATH="$(readlink -f "$FLAGS_chroot/proc")" | 88 mounted_path="$(readlink -f "$FLAGS_chroot/proc")" |
88 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ] | 89 if [ -z "$(mount | grep -F "on $mounted_path ")" ] |
89 then | 90 then |
90 sudo mount none -t proc "$MOUNTED_PATH" || \ | 91 sudo mount none -t proc "$mounted_path" || \ |
91 die "Could not mount $MOUNTED_PATH" | 92 die "Could not mount $mounted_path" |
92 fi | 93 fi |
93 | 94 |
94 MOUNTED_PATH="$(readlink -f "$FLAGS_chroot/sys")" | 95 mounted_path="$(readlink -f "$FLAGS_chroot/sys")" |
95 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ] | 96 if [ -z "$(mount | grep -F "on $mounted_path ")" ] |
96 then | 97 then |
97 sudo mount none -t sysfs "$MOUNTED_PATH" || \ | 98 sudo mount none -t sysfs "$mounted_path" || \ |
98 die "Could not mount $MOUNTED_PATH" | 99 die "Could not mount $mounted_path" |
99 fi | 100 fi |
100 | 101 |
101 MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}/dev")" | 102 mounted_path="$(readlink -f "${FLAGS_chroot}/dev")" |
102 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ] | 103 if [ -z "$(mount | grep -F "on $mounted_path ")" ] |
103 then | 104 then |
104 sudo mount --bind /dev "$MOUNTED_PATH" || \ | 105 sudo mount --bind /dev "$mounted_path" || \ |
105 die "Could not mount $MOUNTED_PATH" | 106 die "Could not mount $mounted_path" |
106 fi | 107 fi |
107 | 108 |
108 if [ $FLAGS_ssh_agent -eq $FLAGS_TRUE ]; then | 109 if [ $FLAGS_ssh_agent -eq $FLAGS_TRUE ]; then |
109 TARGET_DIR="$(readlink -f "${FLAGS_chroot}/home/${USER}/.ssh")" | 110 TARGET_DIR="$(readlink -f "${FLAGS_chroot}/home/${USER}/.ssh")" |
110 if [ -n "${SSH_AUTH_SOCK}" \ | 111 if [ -n "${SSH_AUTH_SOCK}" \ |
111 -a -d "${HOME}/.ssh" ] | 112 -a -d "${HOME}/.ssh" ] |
112 then | 113 then |
113 mkdir -p "${TARGET_DIR}" | 114 mkdir -p "${TARGET_DIR}" |
114 cp -r "${HOME}/.ssh/known_hosts" "${TARGET_DIR}" | 115 cp -r "${HOME}/.ssh/known_hosts" "${TARGET_DIR}" |
115 ASOCK="$(dirname "${SSH_AUTH_SOCK}")" | 116 ASOCK="$(dirname "${SSH_AUTH_SOCK}")" |
116 mkdir -p "${FLAGS_chroot}/${ASOCK}" | 117 mkdir -p "${FLAGS_chroot}/${ASOCK}" |
117 sudo mount --bind "${ASOCK}" "${FLAGS_chroot}/${ASOCK}" || \ | 118 sudo mount --bind "${ASOCK}" "${FLAGS_chroot}/${ASOCK}" || \ |
118 die "Count not mount ${ASOCK}" | 119 die "Count not mount ${ASOCK}" |
119 fi | 120 fi |
120 fi | 121 fi |
121 | 122 |
122 MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}/dev/pts")" | 123 mounted_path="$(readlink -f "${FLAGS_chroot}/dev/pts")" |
123 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ] | 124 if [ -z "$(mount | grep -F "on $mounted_path ")" ] |
124 then | 125 then |
125 sudo mount none -t devpts "$MOUNTED_PATH" || \ | 126 sudo mount none -t devpts "$mounted_path" || \ |
126 die "Could not mount $MOUNTED_PATH" | 127 die "Could not mount $mounted_path" |
127 fi | 128 fi |
128 | 129 |
129 MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}$CHROOT_TRUNK_DIR")" | 130 mounted_path="$(readlink -f "${FLAGS_chroot}$CHROOT_TRUNK_DIR")" |
130 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ] | 131 if [ -z "$(mount | grep -F "on $mounted_path ")" ] |
131 then | 132 then |
132 sudo mount --bind "$FLAGS_trunk" "$MOUNTED_PATH" || \ | 133 sudo mount --bind "$FLAGS_trunk" "$mounted_path" || \ |
133 die "Could not mount $MOUNTED_PATH" | 134 die "Could not mount $mounted_path" |
134 fi | 135 fi |
135 | 136 |
136 MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}${INNER_CHROME_ROOT}")" | 137 mounted_path="$(readlink -f "${FLAGS_chroot}${INNER_CHROME_ROOT}")" |
137 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ] | 138 if [ -z "$(mount | grep -F "on $mounted_path ")" ] |
138 then | 139 then |
139 ! CHROME_ROOT="$(readlink -f "$FLAGS_chrome_root")" | 140 ! CHROME_ROOT="$(readlink -f "$FLAGS_chrome_root")" |
140 if [ -z "$CHROME_ROOT" ]; then | 141 if [ -z "$CHROME_ROOT" ]; then |
141 ! CHROME_ROOT="$(cat "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" \ | 142 ! CHROME_ROOT="$(cat "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" \ |
142 2>/dev/null)" | 143 2>/dev/null)" |
143 fi | 144 fi |
144 if [[ ( -z "$CHROME_ROOT" ) || ( ! -d "${CHROME_ROOT}/src" ) ]]; then | 145 if [[ ( -z "$CHROME_ROOT" ) || ( ! -d "${CHROME_ROOT}/src" ) ]]; then |
145 info "Not mounting chrome source" | 146 info "Not mounting chrome source" |
146 sudo rm -f "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" | 147 sudo rm -f "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" |
147 else | 148 else |
148 info "Mounting chrome source at: $INNER_CHROME_ROOT" | 149 info "Mounting chrome source at: $INNER_CHROME_ROOT" |
149 echo "$CHROME_ROOT" | \ | 150 echo "$CHROME_ROOT" | \ |
150 sudo dd of="${FLAGS_chroot}${CHROME_ROOT_CONFIG}" | 151 sudo dd of="${FLAGS_chroot}${CHROME_ROOT_CONFIG}" |
151 mkdir -p "$MOUNTED_PATH" | 152 mkdir -p "$mounted_path" |
152 sudo mount --bind "$CHROME_ROOT" "$MOUNTED_PATH" || \ | 153 sudo mount --bind "$CHROME_ROOT" "$mounted_path" || \ |
153 die "Could not mount $MOUNTED_PATH" | 154 die "Could not mount $mounted_path" |
154 fi | 155 fi |
155 fi | 156 fi |
156 | 157 |
157 MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}${INNER_DEPOT_TOOLS_ROOT}")" | 158 mounted_path="$(readlink -f "${FLAGS_chroot}${INNER_DEPOT_TOOLS_ROOT}")" |
158 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ] | 159 if [ -z "$(mount | grep -F "on $mounted_path ")" ] |
159 then | 160 then |
160 if [ $(which gclient 2>/dev/null) ]; then | 161 if [ $(which gclient 2>/dev/null) ]; then |
161 info "Mounting depot_tools" | 162 info "Mounting depot_tools" |
162 DEPOT_TOOLS=$(dirname $(which gclient) ) | 163 DEPOT_TOOLS=$(dirname $(which gclient) ) |
163 mkdir -p "$MOUNTED_PATH" | 164 mkdir -p "$mounted_path" |
164 if ! sudo mount --bind "$DEPOT_TOOLS" "$MOUNTED_PATH"; then | 165 if ! sudo mount --bind "$DEPOT_TOOLS" "$mounted_path"; then |
165 warn "depot_tools failed to mount; perhaps it's on NFS?" | 166 warn "depot_tools failed to mount; perhaps it's on NFS?" |
166 warn "This may impact chromium build." | 167 warn "This may impact chromium build." |
167 fi | 168 fi |
168 fi | 169 fi |
169 fi | 170 fi |
170 | 171 |
171 # Install fuse module. | 172 # Install fuse module. |
172 if [ -c "${FUSE_DEVICE}" ] ; then | 173 if [ -c "${FUSE_DEVICE}" ] ; then |
173 sudo modprobe fuse 2> /dev/null ||\ | 174 sudo modprobe fuse 2> /dev/null ||\ |
174 warn "-- Note: modprobe fuse failed. gmergefs will not work" | 175 warn "-- Note: modprobe fuse failed. gmergefs will not work" |
175 fi | 176 fi |
176 | 177 |
177 # Turn off automounting of external media when we enter the | 178 # Turn off automounting of external media when we enter the |
178 # chroot; thus we don't have to worry about being able to unmount | 179 # chroot; thus we don't have to worry about being able to unmount |
179 # from inside. | 180 # from inside. |
180 if [ $(which gconftool-2 2>/dev/null) ]; then | 181 if [ $(which gconftool-2 2>/dev/null) ]; then |
181 gconftool-2 -g ${AUTOMOUNT_PREF} > \ | 182 gconftool-2 -g ${AUTOMOUNT_PREF} > \ |
182 "${FLAGS_chroot}${SAVED_AUTOMOUNT_PREF_FILE}" | 183 "${FLAGS_chroot}${SAVED_AUTOMOUNT_PREF_FILE}" |
183 if [ $(gconftool-2 -s --type=boolean ${AUTOMOUNT_PREF} false) ]; then | 184 if [ $(gconftool-2 -s --type=boolean ${AUTOMOUNT_PREF} false) ]; then |
184 warn "-- Note: USB sticks may be automounted by your host OS." | 185 warn "-- Note: USB sticks may be automounted by your host OS." |
185 warn "-- Note: If you plan to burn bootable media, you may need to" | 186 warn "-- Note: If you plan to burn bootable media, you may need to" |
186 warn "-- Note: unmount these devices manually, or run image_to_usb.sh" | 187 warn "-- Note: unmount these devices manually, or run image_to_usb.sh" |
187 warn "-- Note: outside the chroot." | 188 warn "-- Note: outside the chroot." |
188 fi | 189 fi |
189 fi | 190 fi |
190 | 191 |
| 192 # Mount additional directories |
| 193 local local_mounts="${FLAGS_trunk}/src/scripts/.local_mounts" |
| 194 if [ -f "${local_mounts}" ]; then |
| 195 info "Mounting local folders (read-only for safety concern)" |
| 196 # format: mount_source |
| 197 # or mount_source mount_point |
| 198 # or # comments |
| 199 local mount_source mount_point |
| 200 cat "${local_mounts}" | while read mount_source mount_point; do |
| 201 if [ "$(echo "${mount_source}" | cut -b 1)" = "#" ] || |
| 202 [ -z "${mount_source}" ]; then |
| 203 continue |
| 204 fi |
| 205 # if only source is assigned, use source as mount point. |
| 206 mount_point="${mount_point:-$mount_source}" |
| 207 mounted_path="$(readlink -f "${FLAGS_chroot}${mount_point}")" |
| 208 if [ -z "$(mount | grep -F "on ${mounted_path} ")" ] |
| 209 then |
| 210 # --bind can't be initialized as RO, so we have to use "ro,remount". |
| 211 (sudo mount --bind "${mount_source}" "${mounted_path}" && |
| 212 sudo mount -o ro,remount "${mounted_path}") || |
| 213 die "Failed to mount ${mounted_path} from ${mount_source}." |
| 214 fi |
| 215 done |
| 216 fi |
| 217 |
191 ) 200>>"$LOCKFILE" || die "setup_env failed" | 218 ) 200>>"$LOCKFILE" || die "setup_env failed" |
192 } | 219 } |
193 | 220 |
194 function teardown_env { | 221 function teardown_env { |
195 # Only teardown if we're the last enter_chroot to die | 222 # Only teardown if we're the last enter_chroot to die |
196 ( | 223 ( |
| 224 local mounted_path |
197 flock 200 | 225 flock 200 |
198 | 226 |
199 # check each pid in $LOCKFILE to see if it's died unexpectedly | 227 # check each pid in $LOCKFILE to see if it's died unexpectedly |
200 TMP_LOCKFILE="$LOCKFILE.tmp" | 228 TMP_LOCKFILE="$LOCKFILE.tmp" |
201 | 229 |
202 echo -n > "$TMP_LOCKFILE" # Erase/reset temp file | 230 echo -n > "$TMP_LOCKFILE" # Erase/reset temp file |
203 cat "$LOCKFILE" | while read PID; do | 231 cat "$LOCKFILE" | while read PID; do |
204 if [ "$PID" = "$$" ]; then | 232 if [ "$PID" = "$$" ]; then |
205 # ourself, leave PROC_NAME empty | 233 # ourself, leave PROC_NAME empty |
206 PROC_NAME="" | 234 PROC_NAME="" |
(...skipping 12 matching lines...) Expand all Loading... |
219 if [ $(which gconftool-2 2>/dev/null) ]; then | 247 if [ $(which gconftool-2 2>/dev/null) ]; then |
220 SAVED_PREF=$(cat "${FLAGS_chroot}${SAVED_AUTOMOUNT_PREF_FILE}") | 248 SAVED_PREF=$(cat "${FLAGS_chroot}${SAVED_AUTOMOUNT_PREF_FILE}") |
221 gconftool-2 -s --type=boolean ${AUTOMOUNT_PREF} ${SAVED_PREF} || \ | 249 gconftool-2 -s --type=boolean ${AUTOMOUNT_PREF} ${SAVED_PREF} || \ |
222 warn "could not re-set your automount preference." | 250 warn "could not re-set your automount preference." |
223 fi | 251 fi |
224 | 252 |
225 if [ -s "$LOCKFILE" ]; then | 253 if [ -s "$LOCKFILE" ]; then |
226 info "At least one other pid is running in the chroot, so not" | 254 info "At least one other pid is running in the chroot, so not" |
227 info "tearing down env." | 255 info "tearing down env." |
228 else | 256 else |
229 MOUNTED_PATH=$(readlink -f "$FLAGS_chroot") | 257 mounted_path=$(readlink -f "$FLAGS_chroot") |
230 info "Unmounting chroot environment." | 258 info "Unmounting chroot environment." |
231 # sort the list of mounts in reverse order, to ensure umount of | 259 # sort the list of mounts in reverse order, to ensure umount of |
232 # cascading mounts in proper order | 260 # cascading mounts in proper order |
233 for i in \ | 261 for i in \ |
234 $(mount | grep -F "on $MOUNTED_PATH/" | sort -r | awk '{print $3}'); do | 262 $(mount | grep -F "on $mounted_path/" | sort -r | awk '{print $3}'); do |
235 safe_umount "$i" | 263 safe_umount "$i" |
236 done | 264 done |
237 fi | 265 fi |
238 ) 200>>"$LOCKFILE" || die "teardown_env failed" | 266 ) 200>>"$LOCKFILE" || die "teardown_env failed" |
239 } | 267 } |
240 | 268 |
241 if [ $FLAGS_mount -eq $FLAGS_TRUE ] | 269 if [ $FLAGS_mount -eq $FLAGS_TRUE ] |
242 then | 270 then |
243 setup_env | 271 setup_env |
244 info "Make sure you run" | 272 info "Make sure you run" |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 # Run command or interactive shell. Also include the non-chrooted path to | 329 # Run command or interactive shell. Also include the non-chrooted path to |
302 # the source trunk for scripts that may need to print it (e.g. | 330 # the source trunk for scripts that may need to print it (e.g. |
303 # build_image.sh). | 331 # build_image.sh). |
304 sudo chroot "$FLAGS_chroot" sudo -i -u $USER $CHROOT_PASSTHRU \ | 332 sudo chroot "$FLAGS_chroot" sudo -i -u $USER $CHROOT_PASSTHRU \ |
305 EXTERNAL_TRUNK_PATH="${FLAGS_trunk}" LANG=C SSH_AGENT_PID="${SSH_AGENT_PID}" \ | 333 EXTERNAL_TRUNK_PATH="${FLAGS_trunk}" LANG=C SSH_AGENT_PID="${SSH_AGENT_PID}" \ |
306 SSH_AUTH_SOCK="${SSH_AUTH_SOCK}" -- "$@" | 334 SSH_AUTH_SOCK="${SSH_AUTH_SOCK}" -- "$@" |
307 | 335 |
308 # Remove trap and explicitly unmount | 336 # Remove trap and explicitly unmount |
309 trap - EXIT | 337 trap - EXIT |
310 teardown_env | 338 teardown_env |
OLD | NEW |