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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 if [ $FLAGS_official_build -eq $FLAGS_TRUE ] | 52 if [ $FLAGS_official_build -eq $FLAGS_TRUE ] |
53 then | 53 then |
54 CHROMEOS_OFFICIAL=1 | 54 CHROMEOS_OFFICIAL=1 |
55 fi | 55 fi |
56 | 56 |
57 # Only now can we die on error. shflags functions leak non-zero error codes, | 57 # Only now can we die on error. shflags functions leak non-zero error codes, |
58 # so will die prematurely if 'set -e' is specified before now. | 58 # so will die prematurely if 'set -e' is specified before now. |
59 # TODO: replace shflags with something less error-prone, or contribute a fix. | 59 # TODO: replace shflags with something less error-prone, or contribute a fix. |
60 set -e | 60 set -e |
61 | 61 |
| 62 sudo chmod 0777 "$FLAGS_chroot/var/lock" |
| 63 |
| 64 LOCKFILE="$FLAGS_chroot/var/lock/enter_chroot" |
| 65 |
62 function setup_env { | 66 function setup_env { |
63 echo "Mounting chroot environment." | 67 ( |
| 68 flock 200 |
| 69 echo $$ >> "$LOCKFILE" |
64 | 70 |
65 # Mount only if not already mounted | 71 echo "Mounting chroot environment." |
66 MOUNTED_PATH="$(readlink -f "$FLAGS_chroot/proc")" | |
67 if [ -z "$(mount | grep -F "on $MOUNTED_PATH")" ] | |
68 then | |
69 sudo mount none -t proc "$MOUNTED_PATH" | |
70 fi | |
71 | 72 |
72 MOUNTED_PATH="$(readlink -f "$FLAGS_chroot/dev/pts")" | 73 # Mount only if not already mounted |
73 if [ -z "$(mount | grep -F "on $MOUNTED_PATH")" ] | 74 MOUNTED_PATH="$(readlink -f "$FLAGS_chroot/proc")" |
74 then | 75 if [ -z "$(mount | grep -F "on $MOUNTED_PATH")" ] |
75 sudo mount none -t devpts "$MOUNTED_PATH" | 76 then |
76 fi | 77 sudo mount none -t proc "$MOUNTED_PATH" |
| 78 fi |
77 | 79 |
78 MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}$CHROOT_TRUNK_DIR")" | 80 MOUNTED_PATH="$(readlink -f "$FLAGS_chroot/dev/pts")" |
79 if [ -z "$(mount | grep -F "on $MOUNTED_PATH")" ] | 81 if [ -z "$(mount | grep -F "on $MOUNTED_PATH")" ] |
80 then | 82 then |
81 sudo mount --bind "$FLAGS_trunk" "$MOUNTED_PATH" | 83 sudo mount none -t devpts "$MOUNTED_PATH" |
82 fi | 84 fi |
| 85 |
| 86 MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}$CHROOT_TRUNK_DIR")" |
| 87 if [ -z "$(mount | grep -F "on $MOUNTED_PATH")" ] |
| 88 then |
| 89 sudo mount --bind "$FLAGS_trunk" "$MOUNTED_PATH" |
| 90 fi |
| 91 ) 200>>"$LOCKFILE" |
83 } | 92 } |
84 | 93 |
85 function teardown_env { | 94 function teardown_env { |
86 echo "Unmounting chroot environment." | 95 # Only teardown if we're the last enter_chroot to die |
87 mount | grep "on $(readlink -f "$FLAGS_chroot")" | awk '{print $3}' \ | 96 |
88 | xargs -r -L1 sudo umount | 97 ( |
| 98 flock 200 |
| 99 |
| 100 # check each pid in $LOCKFILE to see if it's died unexpectedly |
| 101 TMP_LOCKFILE="$LOCKFILE.tmp" |
| 102 |
| 103 echo -n > "$TMP_LOCKFILE" # Erase/reset temp file |
| 104 cat "$LOCKFILE" | while read PID; do |
| 105 if [ "$PID" = "$$" ]; then |
| 106 # ourself, leave PROC_NAME empty |
| 107 PROC_NAME="" |
| 108 else |
| 109 PROC_NAME=$(ps --pid $PID -o comm=) |
| 110 fi |
| 111 |
| 112 if [ ! -z "$PROC_NAME" ]; then |
| 113 # All good, keep going |
| 114 echo "$PID" >> "$TMP_LOCKFILE" |
| 115 fi |
| 116 done |
| 117 # Remove any dups from lock file while installing new one |
| 118 sort -n "$TMP_LOCKFILE" | uniq > "$LOCKFILE" |
| 119 |
| 120 if [ -s "$LOCKFILE" ]; then |
| 121 echo "At least one other pid is running in the chroot, so not" |
| 122 echo "tearing down env." |
| 123 else |
| 124 echo "Unmounting chroot environment." |
| 125 mount | grep "on $(readlink -f "$FLAGS_chroot")" | awk '{print $3}' \ |
| 126 | xargs -r -L1 sudo umount |
| 127 fi |
| 128 ) 200>>"$LOCKFILE" |
89 } | 129 } |
90 | 130 |
91 if [ $FLAGS_mount -eq $FLAGS_TRUE ] | 131 if [ $FLAGS_mount -eq $FLAGS_TRUE ] |
92 then | 132 then |
93 setup_env | 133 setup_env |
94 echo "Make sure you run" | 134 echo "Make sure you run" |
95 echo " $0 --unmount" | 135 echo " $0 --unmount" |
96 echo "before deleting $FLAGS_chroot" | 136 echo "before deleting $FLAGS_chroot" |
97 echo "or you'll end up deleting $FLAGS_trunk too!" | 137 echo "or you'll end up deleting $FLAGS_trunk too!" |
98 exit 0 | 138 exit 0 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 | 178 |
139 # Run command or interactive shell. Also include the non-chrooted path to | 179 # Run command or interactive shell. Also include the non-chrooted path to |
140 # the source trunk for scripts that may need to print it (e.g. | 180 # the source trunk for scripts that may need to print it (e.g. |
141 # build_image.sh). | 181 # build_image.sh). |
142 sudo chroot "$FLAGS_chroot" sudo -i -u $USER $CHROOT_PASSTHRU \ | 182 sudo chroot "$FLAGS_chroot" sudo -i -u $USER $CHROOT_PASSTHRU \ |
143 EXTERNAL_TRUNK_PATH="${FLAGS_trunk}" LANG=C "$@" | 183 EXTERNAL_TRUNK_PATH="${FLAGS_trunk}" LANG=C "$@" |
144 | 184 |
145 # Remove trap and explicitly unmount | 185 # Remove trap and explicitly unmount |
146 trap - EXIT | 186 trap - EXIT |
147 teardown_env | 187 teardown_env |
OLD | NEW |