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 | 71 |
72 function setup_env { | 72 function setup_env { |
73 ( | 73 ( |
74 flock 200 | 74 flock 200 |
75 echo $$ >> "$LOCKFILE" | 75 echo $$ >> "$LOCKFILE" |
76 | 76 |
77 echo "Mounting chroot environment." | 77 echo "Mounting chroot environment." |
78 | 78 |
79 # Mount only if not already mounted | 79 # Mount only if not already mounted |
80 MOUNTED_PATH="$(readlink -f "$FLAGS_chroot/proc")" | 80 MOUNTED_PATH="$(readlink -f "$FLAGS_chroot/proc")" |
81 if [ -z "$(mount | grep -F "on $MOUNTED_PATH")" ] | 81 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ] |
82 then | 82 then |
83 sudo mount none -t proc "$MOUNTED_PATH" | 83 sudo mount none -t proc "$MOUNTED_PATH" || \ |
| 84 die "Could not mount $MOUNTED_PATH" |
84 fi | 85 fi |
85 | 86 |
86 MOUNTED_PATH="$(readlink -f "$FLAGS_chroot/dev/pts")" | 87 MOUNTED_PATH="$(readlink -f "$FLAGS_chroot/dev/pts")" |
87 if [ -z "$(mount | grep -F "on $MOUNTED_PATH")" ] | 88 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ] |
88 then | 89 then |
89 sudo mount none -t devpts "$MOUNTED_PATH" | 90 sudo mount none -t devpts "$MOUNTED_PATH" || \ |
| 91 die "Could not mount $MOUNTED_PATH" |
90 fi | 92 fi |
91 | 93 |
92 MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}$CHROOT_TRUNK_DIR")" | 94 MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}$CHROOT_TRUNK_DIR")" |
93 if [ -z "$(mount | grep -F "on $MOUNTED_PATH")" ] | 95 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ] |
94 then | 96 then |
95 sudo mount --bind "$FLAGS_trunk" "$MOUNTED_PATH" | 97 sudo mount --bind "$FLAGS_trunk" "$MOUNTED_PATH" || \ |
| 98 die "Could not mount $MOUNTED_PATH" |
96 fi | 99 fi |
97 | 100 |
98 MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}${INNER_CHROME_ROOT}")" | 101 MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}${INNER_CHROME_ROOT}")" |
99 if [ -z "$(mount | grep -F "on $MOUNTED_PATH")" ] | 102 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ] |
100 then | 103 then |
101 ! CHROME_ROOT="$(readlink -f "$FLAGS_chrome_root")" | 104 ! CHROME_ROOT="$(readlink -f "$FLAGS_chrome_root")" |
102 if [ -z "$CHROME_ROOT" ]; then | 105 if [ -z "$CHROME_ROOT" ]; then |
103 ! CHROME_ROOT="$(cat "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" \ | 106 ! CHROME_ROOT="$(cat "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" \ |
104 2>/dev/null)" | 107 2>/dev/null)" |
105 fi | 108 fi |
106 if [[ ( -z "$CHROME_ROOT" ) || ( ! -d "${CHROME_ROOT}/src" ) ]]; then | 109 if [[ ( -z "$CHROME_ROOT" ) || ( ! -d "${CHROME_ROOT}/src" ) ]]; then |
107 echo "Not mounting chrome source" | 110 echo "Not mounting chrome source" |
108 sudo rm -f "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" | 111 sudo rm -f "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" |
109 else | 112 else |
110 echo "Mounting chrome source at: $INNER_CHROME_ROOT" | 113 echo "Mounting chrome source at: $INNER_CHROME_ROOT" |
111 echo "$CHROME_ROOT" | \ | 114 echo "$CHROME_ROOT" | \ |
112 sudo dd of="${FLAGS_chroot}${CHROME_ROOT_CONFIG}" | 115 sudo dd of="${FLAGS_chroot}${CHROME_ROOT_CONFIG}" |
113 mkdir -p "$MOUNTED_PATH" | 116 mkdir -p "$MOUNTED_PATH" |
114 sudo mount --bind "$CHROME_ROOT" "$MOUNTED_PATH" | 117 sudo mount --bind "$CHROME_ROOT" "$MOUNTED_PATH" || \ |
| 118 die "Could not mount $MOUNTED_PATH" |
115 fi | 119 fi |
116 fi | 120 fi |
117 | 121 |
118 MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}${INNER_DEPOT_TOOLS_ROOT}")" | 122 MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}${INNER_DEPOT_TOOLS_ROOT}")" |
119 if [ -z "$(mount | grep -F "on $MOUNTED_PATH")" ] | 123 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ] |
120 then | 124 then |
121 if [ $(which gclient 2>/dev/null) ]; then | 125 if [ $(which gclient 2>/dev/null) ]; then |
122 echo "Mounting depot_tools" | 126 echo "Mounting depot_tools" |
123 DEPOT_TOOLS=$(dirname $(which gclient) ) | 127 DEPOT_TOOLS=$(dirname $(which gclient) ) |
124 mkdir -p "$MOUNTED_PATH" | 128 mkdir -p "$MOUNTED_PATH" |
125 if ! sudo mount --bind "$DEPOT_TOOLS" "$MOUNTED_PATH"; then | 129 if ! sudo mount --bind "$DEPOT_TOOLS" "$MOUNTED_PATH"; then |
126 echo "depot_tools failed to mount; perhaps it's on NFS?" | 130 echo "depot_tools failed to mount; perhaps it's on NFS?" |
127 echo "This may impact chromium build." | 131 echo "This may impact chromium build." |
128 fi | 132 fi |
129 fi | 133 fi |
130 fi | 134 fi |
131 ) 200>>"$LOCKFILE" | 135 ) 200>>"$LOCKFILE" || die "setup_env failed" |
132 } | 136 } |
133 | 137 |
134 function teardown_env { | 138 function teardown_env { |
135 # Only teardown if we're the last enter_chroot to die | 139 # Only teardown if we're the last enter_chroot to die |
136 | |
137 # We should not return with an error if cleanup has an error. Cleanup is | |
138 # best effort only. | |
139 set +e | |
140 | |
141 ( | 140 ( |
142 flock 200 | 141 flock 200 |
143 | 142 |
144 # check each pid in $LOCKFILE to see if it's died unexpectedly | 143 # check each pid in $LOCKFILE to see if it's died unexpectedly |
145 TMP_LOCKFILE="$LOCKFILE.tmp" | 144 TMP_LOCKFILE="$LOCKFILE.tmp" |
146 | 145 |
147 echo -n > "$TMP_LOCKFILE" # Erase/reset temp file | 146 echo -n > "$TMP_LOCKFILE" # Erase/reset temp file |
148 cat "$LOCKFILE" | while read PID; do | 147 cat "$LOCKFILE" | while read PID; do |
149 if [ "$PID" = "$$" ]; then | 148 if [ "$PID" = "$$" ]; then |
150 # ourself, leave PROC_NAME empty | 149 # ourself, leave PROC_NAME empty |
151 PROC_NAME="" | 150 PROC_NAME="" |
152 else | 151 else |
153 PROC_NAME=$(ps --pid $PID -o comm=) | 152 PROC_NAME=$(ps --pid $PID -o comm=) |
154 fi | 153 fi |
155 | 154 |
156 if [ ! -z "$PROC_NAME" ]; then | 155 if [ ! -z "$PROC_NAME" ]; then |
157 # All good, keep going | 156 # All good, keep going |
158 echo "$PID" >> "$TMP_LOCKFILE" | 157 echo "$PID" >> "$TMP_LOCKFILE" |
159 fi | 158 fi |
160 done | 159 done |
161 # Remove any dups from lock file while installing new one | 160 # Remove any dups from lock file while installing new one |
162 sort -n "$TMP_LOCKFILE" | uniq > "$LOCKFILE" | 161 sort -n "$TMP_LOCKFILE" | uniq > "$LOCKFILE" |
163 | 162 |
164 if [ -s "$LOCKFILE" ]; then | 163 if [ -s "$LOCKFILE" ]; then |
165 echo "At least one other pid is running in the chroot, so not" | 164 echo "At least one other pid is running in the chroot, so not" |
166 echo "tearing down env." | 165 echo "tearing down env." |
167 else | 166 else |
| 167 MOUNTED_PATH=$(readlink -f "$FLAGS_chroot") |
168 echo "Unmounting chroot environment." | 168 echo "Unmounting chroot environment." |
169 mount | grep "on $(readlink -f "$FLAGS_chroot")" | awk '{print $3}' \ | 169 for i in $(mount | grep -F "on $MOUNTED_PATH/" | awk '{print $3}'); do |
170 | xargs -r -L1 sudo umount | 170 sudo umount "$i" || die "Failed to umount $i" |
| 171 done |
171 fi | 172 fi |
172 ) 200>>"$LOCKFILE" | 173 ) 200>>"$LOCKFILE" || die "teardown_env failed" |
173 } | 174 } |
174 | 175 |
175 if [ $FLAGS_mount -eq $FLAGS_TRUE ] | 176 if [ $FLAGS_mount -eq $FLAGS_TRUE ] |
176 then | 177 then |
177 setup_env | 178 setup_env |
178 echo "Make sure you run" | 179 echo "Make sure you run" |
179 echo " $0 --unmount" | 180 echo " $0 --unmount" |
180 echo "before deleting $FLAGS_chroot" | 181 echo "before deleting $FLAGS_chroot" |
181 echo "or you'll end up deleting $FLAGS_trunk too!" | 182 echo "or you'll end up deleting $FLAGS_trunk too!" |
182 exit 0 | 183 exit 0 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 | 223 |
223 # Run command or interactive shell. Also include the non-chrooted path to | 224 # Run command or interactive shell. Also include the non-chrooted path to |
224 # the source trunk for scripts that may need to print it (e.g. | 225 # the source trunk for scripts that may need to print it (e.g. |
225 # build_image.sh). | 226 # build_image.sh). |
226 sudo chroot "$FLAGS_chroot" sudo -i -u $USER $CHROOT_PASSTHRU \ | 227 sudo chroot "$FLAGS_chroot" sudo -i -u $USER $CHROOT_PASSTHRU \ |
227 EXTERNAL_TRUNK_PATH="${FLAGS_trunk}" LANG=C "$@" | 228 EXTERNAL_TRUNK_PATH="${FLAGS_trunk}" LANG=C "$@" |
228 | 229 |
229 # Remove trap and explicitly unmount | 230 # Remove trap and explicitly unmount |
230 trap - EXIT | 231 trap - EXIT |
231 teardown_env | 232 teardown_env |
OLD | NEW |