Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | # This script sets up a Gentoo chroot environment. The script is passed the |
| 8 | # path to an empty folder, which will be populated with a Gentoo stage3 and |
| 9 | # setup for development. Once created, the password is set to PASSWORD (below). |
| 10 | # One can enter the chrooted environment for work by running enter_chroot.sh. |
| 11 | |
| 12 | SCRIPT_ROOT=$(readlink -f $(dirname "$0")/..) |
| 13 | . "${SCRIPT_ROOT}/common.sh" || exit 1 |
| 14 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 15 | ENTER_CHROOT=$(readlink -f $(dirname "$0")/enter_chroot.sh) |
| 16 | |
Brian Harring | feb04f7 | 2012-02-03 21:22:50 -0800 | [diff] [blame] | 17 | enable_strict_sudo |
| 18 | |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 19 | # Check if the host machine architecture is supported. |
| 20 | ARCHITECTURE="$(uname -m)" |
| 21 | if [[ "$ARCHITECTURE" != "x86_64" ]]; then |
| 22 | echo "$SCRIPT_NAME: $ARCHITECTURE is not supported as a host machine architecture." |
| 23 | exit 1 |
| 24 | fi |
| 25 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 26 | # Script must be run outside the chroot. |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 27 | assert_outside_chroot |
| 28 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 29 | # Define command line flags. |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 30 | # See http://code.google.com/p/shflags/wiki/Documentation10x |
| 31 | |
| 32 | DEFINE_string chroot "$DEFAULT_CHROOT_DIR" \ |
| 33 | "Destination dir for the chroot environment." |
| 34 | DEFINE_boolean usepkg $FLAGS_TRUE "Use binary packages to bootstrap." |
| 35 | DEFINE_boolean delete $FLAGS_FALSE "Delete an existing chroot." |
| 36 | DEFINE_boolean replace $FLAGS_FALSE "Overwrite existing chroot, if any." |
| 37 | DEFINE_integer jobs -1 "How many packages to build in parallel at maximum." |
| 38 | DEFINE_boolean fast ${DEFAULT_FAST} "Call many emerges in parallel" |
| 39 | DEFINE_string stage3_date "2010.03.09" \ |
| 40 | "Use the stage3 with the given date." |
| 41 | DEFINE_string stage3_path "" \ |
| 42 | "Use the stage3 located on this path." |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 43 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 44 | # Parse command line flags. |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 45 | FLAGS_HELP="usage: $SCRIPT_NAME [flags]" |
| 46 | FLAGS "$@" || exit 1 |
| 47 | eval set -- "${FLAGS_ARGV}" |
| 48 | check_flags_only_and_allow_null_arg "$@" && set -- |
| 49 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 50 | CROS_LOG_PREFIX=cros_sdk |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 51 | |
| 52 | assert_not_root_user |
| 53 | # Set the right umask for chroot creation. |
| 54 | umask 022 |
| 55 | |
| 56 | # Only now can we die on error. shflags functions leak non-zero error codes, |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 57 | # so will die prematurely if 'switch_to_strict_mode' is specified before now. |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 58 | # TODO: replace shflags with something less error-prone, or contribute a fix. |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 59 | switch_to_strict_mode |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 60 | |
J. Richard Barnette | e80f6de | 2012-02-24 14:08:34 -0800 | [diff] [blame] | 61 | . "${SCRIPT_ROOT}"/sdk_lib/make_conf_util.sh |
| 62 | |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 63 | FULLNAME="ChromeOS Developer" |
| 64 | DEFGROUPS="eng,adm,cdrom,floppy,audio,video,portage" |
| 65 | PASSWORD=chronos |
| 66 | CRYPTED_PASSWD=$(perl -e 'print crypt($ARGV[0], "foo")', $PASSWORD) |
| 67 | |
| 68 | USEPKG="" |
| 69 | if [[ $FLAGS_usepkg -eq $FLAGS_TRUE ]]; then |
| 70 | # Use binary packages. Include all build-time dependencies, |
| 71 | # so as to avoid unnecessary differences between source |
| 72 | # and binary builds. |
| 73 | USEPKG="--getbinpkg --usepkg --with-bdeps y" |
| 74 | fi |
| 75 | |
| 76 | # Support faster build if necessary. |
| 77 | EMERGE_CMD="emerge" |
| 78 | if [ "$FLAGS_fast" -eq "${FLAGS_TRUE}" ]; then |
| 79 | CHROOT_CHROMITE_DIR="/home/${USER}/trunk/chromite" |
| 80 | EMERGE_CMD="${CHROOT_CHROMITE_DIR}/bin/parallel_emerge" |
| 81 | fi |
| 82 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 83 | ENTER_CHROOT_ARGS=( |
| 84 | CROS_WORKON_SRCROOT="$CHROOT_TRUNK" |
| 85 | PORTAGE_USERNAME="$USER" |
| 86 | IGNORE_PREFLIGHT_BINHOST="$IGNORE_PREFLIGHT_BINHOST" |
| 87 | ) |
| 88 | |
| 89 | # Invoke enter_chroot. This can only be used after sudo has been installed. |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 90 | enter_chroot() { |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 91 | "$ENTER_CHROOT" --chroot "$FLAGS_chroot" -- "${ENTER_CHROOT_ARGS[@]}" "$@" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 94 | # Invoke enter_chroot running the command as root, and w/out sudo. |
| 95 | # This should be used prior to sudo being merged. |
Mike Frysinger | ba75845 | 2012-04-02 13:28:31 -0400 | [diff] [blame] | 96 | early_env=() |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 97 | early_enter_chroot() { |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 98 | "$ENTER_CHROOT" --chroot "$FLAGS_chroot" --early_make_chroot \ |
Mike Frysinger | ba75845 | 2012-04-02 13:28:31 -0400 | [diff] [blame] | 99 | -- "${ENTER_CHROOT_ARGS[@]}" "${early_env[@]}" "$@" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 102 | # Run a command within the chroot. The main usage of this is to avoid |
| 103 | # the overhead of enter_chroot, and do not need access to the source tree, |
| 104 | # don't need the actual chroot profile env, and can run the command as root. |
| 105 | sudo_chroot() { |
| 106 | sudo chroot "${FLAGS_chroot}" "$@" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 109 | cleanup() { |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 110 | # Clean up mounts |
| 111 | safe_umount_tree "${FLAGS_chroot}" |
| 112 | } |
| 113 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 114 | delete_existing() { |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 115 | # Delete old chroot dir. |
| 116 | if [[ ! -e "$FLAGS_chroot" ]]; then |
| 117 | return |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 118 | fi |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 119 | info "Cleaning up old mount points..." |
| 120 | cleanup |
| 121 | info "Deleting $FLAGS_chroot..." |
| 122 | sudo rm -rf "$FLAGS_chroot" |
| 123 | info "Done." |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 126 | init_users () { |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 127 | info "Set timezone..." |
| 128 | # date +%Z has trouble with daylight time, so use host's info. |
| 129 | sudo rm -f "${FLAGS_chroot}/etc/localtime" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 130 | if [ -f /etc/localtime ] ; then |
| 131 | sudo cp /etc/localtime "${FLAGS_chroot}/etc" |
| 132 | else |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 133 | sudo ln -sf /usr/share/zoneinfo/PST8PDT "${FLAGS_chroot}/etc/localtime" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 134 | fi |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 135 | info "Adding user/group..." |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 136 | # Add ourselves as a user inside the chroot. |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 137 | sudo_chroot groupadd -g 5000 eng |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 138 | # We need the UID to match the host user's. This can conflict with |
| 139 | # a particular chroot UID. At the same time, the added user has to |
| 140 | # be a primary user for the given UID for sudo to work, which is |
| 141 | # determined by the order in /etc/passwd. Let's put ourselves on top |
| 142 | # of the file. |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 143 | sudo_chroot useradd -o -G ${DEFGROUPS} -g eng -u `id -u` -s \ |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 144 | /bin/bash -m -c "${FULLNAME}" -p ${CRYPTED_PASSWD} ${USER} |
| 145 | # Because passwd generally isn't sorted and the entry ended up at the |
| 146 | # bottom, it is safe to just take it and move it to top instead. |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 147 | sudo sed -e '1{h;d};$!{H;d};$G' -i "${FLAGS_chroot}/etc/passwd" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 150 | init_setup () { |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 151 | info "Running init_setup()..." |
| 152 | sudo mkdir -p -m 755 "${FLAGS_chroot}/usr" \ |
| 153 | "${FLAGS_chroot}/usr/local/portage" \ |
| 154 | "${FLAGS_chroot}"/"${CROSSDEV_OVERLAY}" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 155 | sudo ln -sf "${CHROOT_TRUNK}/src/third_party/portage" \ |
| 156 | "${FLAGS_chroot}/usr/portage" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 157 | sudo ln -sf "${CHROOT_TRUNK}/src/third_party/chromiumos-overlay" \ |
| 158 | "${FLAGS_chroot}"/"${CHROOT_OVERLAY}" |
| 159 | sudo ln -sf "${CHROOT_TRUNK}/src/third_party/portage-stable" \ |
| 160 | "${FLAGS_chroot}"/"${PORTAGE_STABLE_OVERLAY}" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 161 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 162 | # Some operations need an mtab. |
| 163 | sudo ln -s /proc/mounts "${FLAGS_chroot}/etc/mtab" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 164 | |
| 165 | # Set up sudoers. Inside the chroot, the user can sudo without a password. |
| 166 | # (Safe enough, since the only way into the chroot is to 'sudo chroot', so |
| 167 | # the user's already typed in one sudo password...) |
| 168 | # Make sure the sudoers.d subdir exists as older stage3 base images lack it. |
| 169 | sudo mkdir -p "${FLAGS_chroot}/etc/sudoers.d" |
| 170 | sudo_clobber "${FLAGS_chroot}/etc/sudoers.d/90_cros" <<EOF |
| 171 | Defaults env_keep += CROS_WORKON_SRCROOT |
| 172 | Defaults env_keep += CHROMEOS_OFFICIAL |
| 173 | Defaults env_keep += PORTAGE_USERNAME |
| 174 | Defaults env_keep += http_proxy |
| 175 | Defaults env_keep += ftp_proxy |
| 176 | Defaults env_keep += all_proxy |
| 177 | %adm ALL=(ALL) ALL |
| 178 | root ALL=(ALL) ALL |
| 179 | $USER ALL=NOPASSWD: ALL |
| 180 | EOF |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 181 | sudo find "${FLAGS_chroot}/etc/"sudoers* -type f -exec chmod 0440 {} + |
| 182 | # Fix bad group for some. |
| 183 | sudo chown -R root:root "${FLAGS_chroot}/etc/"sudoers* |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 184 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 185 | info "Setting up hosts/resolv..." |
| 186 | # Copy config from outside chroot into chroot. |
| 187 | sudo cp /etc/{hosts,resolv.conf} "$FLAGS_chroot/etc/" |
| 188 | sudo chmod 0644 "$FLAGS_chroot"/etc/{hosts,resolv.conf} |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 189 | |
| 190 | # Setup host make.conf. This includes any overlay that we may be using |
| 191 | # and a pointer to pre-built packages. |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 192 | # TODO: This should really be part of a profile in the portage. |
| 193 | info "Setting up /etc/make.*..." |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 194 | sudo mv "${FLAGS_chroot}"/etc/make.conf{,.orig} |
| 195 | sudo ln -sf "${CHROOT_CONFIG}/make.conf.amd64-host" \ |
| 196 | "${FLAGS_chroot}/etc/make.conf" |
| 197 | sudo mv "${FLAGS_chroot}"/etc/make.profile{,.orig} |
| 198 | sudo ln -sf "${CHROOT_OVERLAY}/profiles/default/linux/amd64/10.0" \ |
| 199 | "${FLAGS_chroot}/etc/make.profile" |
| 200 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 201 | # Create make.conf.user . |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 202 | sudo touch "${FLAGS_chroot}"/etc/make.conf.user |
| 203 | sudo chmod 0644 "${FLAGS_chroot}"/etc/make.conf.user |
| 204 | |
| 205 | # Create directories referred to by our conf files. |
Brian Harring | 36b102b | 2012-02-06 23:34:25 -0800 | [diff] [blame] | 206 | sudo mkdir -p -m 775 "${FLAGS_chroot}/var/lib/portage/pkgs" \ |
| 207 | "${FLAGS_chroot}/var/cache/distfiles" \ |
| 208 | "${FLAGS_chroot}/var/cache/chromeos-chrome" |
| 209 | |
| 210 | # Run this from w/in the chroot so we use whatever uid/gid |
| 211 | # these are defined as w/in the chroot. |
| 212 | sudo_chroot chown "${USER}:portage" /var/cache/chromeos-chrome |
Brian Harring | 7ee892d | 2012-02-02 09:33:10 -0800 | [diff] [blame] | 213 | |
| 214 | # These are created for compatibility while transitioning |
| 215 | # make.conf and friends over to the new location. |
| 216 | # TODO(ferringb): remove this 03/12 or so. |
| 217 | sudo ln -s ../../cache/distfiles/host \ |
| 218 | "${FLAGS_chroot}/var/lib/portage/distfiles" |
| 219 | sudo ln -s ../../cache/distfiles/target \ |
| 220 | "${FLAGS_chroot}/var/lib/portage/distfiles-target" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 221 | |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 222 | # Add chromite/bin and depot_tools into the path globally; note that the |
| 223 | # chromite wrapper itself might also be found in depot_tools. |
| 224 | # We rely on 'env-update' getting called below. |
| 225 | target="${FLAGS_chroot}/etc/env.d/99chromiumos" |
| 226 | sudo_clobber "${target}" <<EOF |
| 227 | PATH=/home/$USER/trunk/chromite/bin:/home/$USER/depot_tools |
| 228 | CROS_WORKON_SRCROOT="${CHROOT_TRUNK}" |
| 229 | PORTAGE_USERNAME=$USER |
| 230 | EOF |
| 231 | |
| 232 | # TODO(zbehan): Configure stuff that is usually configured in postinst's, |
Mike Frysinger | eb1a9b4 | 2012-03-28 16:21:04 -0400 | [diff] [blame] | 233 | # but wasn't. Fix the postinst's. |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 234 | info "Running post-inst configuration hacks" |
| 235 | early_enter_chroot env-update |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 236 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 237 | # This is basically a sanity check of our chroot. If any of these |
| 238 | # don't exist, then either bind mounts have failed, an invocation |
| 239 | # from above is broke, or some assumption about the stage3 is no longer |
| 240 | # true. |
| 241 | early_enter_chroot ls -l /etc/make.{conf,profile} \ |
| 242 | /usr/local/portage/chromiumos/profiles/default/linux/amd64/10.0 |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 243 | |
| 244 | target="${FLAGS_chroot}/etc/profile.d" |
| 245 | sudo mkdir -p "${target}" |
| 246 | sudo_clobber "${target}/chromiumos-niceties.sh" << EOF |
| 247 | # Niceties for interactive logins. (cr) denotes this is a chroot, the |
| 248 | # __git_branch_ps1 prints current git branch in ./ . The $r behavior is to |
| 249 | # make sure we don't reset the previous $? value which later formats in |
| 250 | # $PS1 might rely on. |
| 251 | PS1='\$(r=\$?; __git_branch_ps1 "(%s) "; exit \$r)'"\${PS1}" |
| 252 | PS1="(cr) \${PS1}" |
| 253 | EOF |
| 254 | |
| 255 | # Select a small set of locales for the user if they haven't done so |
| 256 | # already. This makes glibc upgrades cheap by only generating a small |
| 257 | # set of locales. The ones listed here are basically for the buildbots |
| 258 | # which always assume these are available. This works in conjunction |
| 259 | # with `cros_sdk --enter`. |
| 260 | # http://crosbug.com/20378 |
| 261 | local localegen="$FLAGS_chroot/etc/locale.gen" |
| 262 | if ! grep -q -v -e '^#' -e '^$' "${localegen}" ; then |
| 263 | sudo_append "${localegen}" <<EOF |
| 264 | en_US ISO-8859-1 |
| 265 | en_US.UTF-8 UTF-8 |
| 266 | EOF |
| 267 | fi |
| 268 | |
| 269 | # Add chromite as a local site-package. |
| 270 | mkdir -p "${FLAGS_chroot}/home/$USER/.local/lib/python2.6/site-packages" |
| 271 | ln -s ../../../../trunk/chromite \ |
| 272 | "${FLAGS_chroot}/home/$USER/.local/lib/python2.6/site-packages/" |
| 273 | |
| 274 | chmod a+x "$FLAGS_chroot/home/$USER/.bashrc" |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 275 | # Automatically change to scripts directory. |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 276 | echo 'cd ${CHROOT_CWD:-~/trunk/src/scripts}' \ |
| 277 | >> "$FLAGS_chroot/home/$USER/.bash_profile" |
| 278 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 279 | # Enable bash completion for build scripts. |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 280 | echo ". ~/trunk/src/scripts/bash_completion" \ |
| 281 | >> "$FLAGS_chroot/home/$USER/.bashrc" |
| 282 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 283 | # Warn if attempting to use source control commands inside the chroot. |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 284 | for NOUSE in svn gcl gclient |
| 285 | do |
| 286 | echo "alias $NOUSE='echo In the chroot, it is a bad idea to run $NOUSE'" \ |
| 287 | >> "$FLAGS_chroot/home/$USER/.bash_profile" |
| 288 | done |
| 289 | |
| 290 | if [[ "$USER" = "chrome-bot" ]]; then |
| 291 | # Copy ssh keys, so chroot'd chrome-bot can scp files from chrome-web. |
| 292 | cp -r ~/.ssh "$FLAGS_chroot/home/$USER/" |
| 293 | fi |
| 294 | |
| 295 | if [[ -f $HOME/.gitconfig ]]; then |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 296 | # Copy .gitconfig into chroot so repo and git can be used from inside. |
| 297 | # This is required for repo to work since it validates the email address. |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 298 | echo "Copying ~/.gitconfig into chroot" |
| 299 | cp $HOME/.gitconfig "$FLAGS_chroot/home/$USER/" |
| 300 | fi |
| 301 | } |
| 302 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 303 | # Handle deleting an existing environment. |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 304 | if [[ $FLAGS_delete -eq $FLAGS_TRUE || \ |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 305 | $FLAGS_replace -eq $FLAGS_TRUE ]]; then |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 306 | delete_existing |
| 307 | [[ $FLAGS_delete -eq $FLAGS_TRUE ]] && exit 0 |
| 308 | fi |
| 309 | |
| 310 | CHROOT_TRUNK="${CHROOT_TRUNK_DIR}" |
| 311 | PORTAGE="${SRC_ROOT}/third_party/portage" |
| 312 | OVERLAY="${SRC_ROOT}/third_party/chromiumos-overlay" |
| 313 | CONFIG_DIR="${OVERLAY}/chromeos/config" |
| 314 | CHROOT_CONFIG="${CHROOT_TRUNK}/src/third_party/chromiumos-overlay/chromeos/config" |
| 315 | PORTAGE_STABLE_OVERLAY="/usr/local/portage/stable" |
| 316 | CROSSDEV_OVERLAY="/usr/local/portage/crossdev" |
| 317 | CHROOT_OVERLAY="/usr/local/portage/chromiumos" |
| 318 | CHROOT_STATE="${FLAGS_chroot}/etc/debian_chroot" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 319 | |
| 320 | # Pass proxy variables into the environment. |
| 321 | for type in http ftp all; do |
| 322 | value=$(env | grep ${type}_proxy || true) |
| 323 | if [ -n "${value}" ]; then |
| 324 | CHROOT_PASSTHRU+=("$value") |
| 325 | fi |
| 326 | done |
| 327 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 328 | # Create the base Gentoo stage3 based on last version put in chroot. |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 329 | STAGE3="${OVERLAY}/chromeos/stage3/stage3-amd64-${FLAGS_stage3_date}.tar.bz2" |
| 330 | if [ -f $CHROOT_STATE ] && \ |
| 331 | ! sudo egrep -q "^STAGE3=$STAGE3" $CHROOT_STATE >/dev/null 2>&1 |
| 332 | then |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 333 | info "STAGE3 version has changed." |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 334 | delete_existing |
| 335 | fi |
| 336 | |
| 337 | if [ -n "${FLAGS_stage3_path}" ]; then |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 338 | if [ ! -f "${FLAGS_stage3_path}" ]; then |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 339 | error "Invalid stage3!" |
| 340 | exit 1; |
| 341 | fi |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 342 | STAGE3="${FLAGS_stage3_path}" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 343 | fi |
| 344 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 345 | # Create the destination directory. |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 346 | mkdir -p "$FLAGS_chroot" |
| 347 | |
| 348 | echo |
| 349 | if [ -f $CHROOT_STATE ] |
| 350 | then |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 351 | info "STAGE3 already set up. Skipping..." |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 352 | else |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 353 | info "Unpacking STAGE3..." |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 354 | sudo tar -xp -I $(type -p pbzip2 || echo bzip2) \ |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 355 | -C "${FLAGS_chroot}" -f "${STAGE3}" |
| 356 | sudo rm -f "$FLAGS_chroot/etc/"make.{globals,conf.user} |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 357 | fi |
| 358 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 359 | # Set up users, if needed, before mkdir/mounts below. |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 360 | [ -f $CHROOT_STATE ] || init_users |
| 361 | |
| 362 | echo |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 363 | info "Setting up mounts..." |
| 364 | # Set up necessary mounts and make sure we clean them up on exit. |
| 365 | sudo mkdir -p "${FLAGS_chroot}/${CHROOT_TRUNK}" "${FLAGS_chroot}/run" |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 366 | |
J. Richard Barnette | e80f6de | 2012-02-24 14:08:34 -0800 | [diff] [blame] | 367 | # Create a special /etc/make.conf.host_setup that we use to bootstrap |
| 368 | # the chroot. The regular content for the file will be generated the |
| 369 | # first time we invoke update_chroot (further down in this script). |
| 370 | create_bootstrap_host_setup "${FLAGS_chroot}" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 371 | |
| 372 | if ! [ -f "$CHROOT_STATE" ];then |
| 373 | INITIALIZE_CHROOT=1 |
| 374 | fi |
| 375 | |
| 376 | |
Mike Frysinger | ba75845 | 2012-04-02 13:28:31 -0400 | [diff] [blame] | 377 | if ! early_enter_chroot bash -c 'type -P pbzip2' >/dev/null ; then |
| 378 | # This chroot lacks pbzip2 early on, so we need to disable it. |
| 379 | early_env+=( |
| 380 | PORTAGE_BZIP2_COMMAND="bzip2" |
| 381 | PORTAGE_BUNZIP2_COMMAND="bunzip2" |
| 382 | ) |
| 383 | fi |
| 384 | |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 385 | if [ -z "${INITIALIZE_CHROOT}" ];then |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 386 | info "chroot already initialized. Skipping..." |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 387 | else |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 388 | # Run all the init stuff to setup the env. |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 389 | init_setup |
| 390 | fi |
| 391 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 392 | # Add file to indicate that it is a chroot. |
| 393 | # Add version of $STAGE3 for update checks. |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 394 | sudo sh -c "echo STAGE3=$STAGE3 > $CHROOT_STATE" |
| 395 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 396 | info "Updating portage" |
Mike Frysinger | 66fd81f | 2012-02-28 13:13:20 -0500 | [diff] [blame] | 397 | early_enter_chroot emerge -uNv --quiet portage |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 398 | |
Zdenek Behan | 26fe570 | 2012-05-03 15:05:33 -0700 | [diff] [blame] | 399 | info "Updating toolchain" |
| 400 | early_enter_chroot emerge -uNv --quiet $USEPKG '>=sys-devel/gcc-4.4' \ |
| 401 | sys-libs/glibc sys-devel/binutils sys-kernel/linux-headers |
| 402 | |
| 403 | # HACK: Select the latest toolchain. We're assuming that when this is |
| 404 | # ran, the chroot has no experimental versions of new toolchains, just |
| 405 | # one that is very old, and one that was just emerged. |
| 406 | GCC_ATOM="$(early_enter_chroot portageq best_version / sys-devel/gcc)" |
| 407 | early_enter_chroot emerge --unmerge "<${GCC_ATOM}" |
| 408 | CHOST="$(early_enter_chroot portageq envvar CHOST)" |
| 409 | LATEST="$(early_enter_chroot gcc-config -l | grep "${CHOST}" | tail -n1 | \ |
| 410 | cut -f3 -d' ')" |
| 411 | early_enter_chroot gcc-config "${LATEST}" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 412 | |
| 413 | # dhcpcd is included in 'world' by the stage3 that we pull in for some reason. |
| 414 | # We have no need to install it in our host environment, so pull it out here. |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 415 | info "Deselecting dhcpcd" |
| 416 | early_enter_chroot $EMERGE_CMD --deselect dhcpcd |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 417 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 418 | info "Running emerge ccache curl sudo ..." |
Mike Frysinger | 650bf87 | 2012-02-27 11:05:26 -0500 | [diff] [blame] | 419 | early_enter_chroot $EMERGE_CMD -uNv $USEPKG --select $EMERGE_JOBS \ |
Mike Frysinger | ba75845 | 2012-04-02 13:28:31 -0400 | [diff] [blame] | 420 | pbzip2 ccache net-misc/curl sudo |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 421 | |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 422 | if [ -n "${INITIALIZE_CHROOT}" ]; then |
| 423 | # If we're creating a new chroot, we also want to set it to the latest |
| 424 | # version. |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 425 | enter_chroot \ |
| 426 | "${CHROOT_TRUNK}/src/scripts/run_chroot_version_hooks" --force_latest |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 427 | fi |
| 428 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 429 | # Update chroot. |
Zdenek Behan | 26fe570 | 2012-05-03 15:05:33 -0700 | [diff] [blame] | 430 | UPDATE_ARGS=() |
| 431 | if [[ $FLAGS_usepkg -eq $FLAGS_TRUE ]]; then |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 432 | UPDATE_ARGS+=( --usepkg ) |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 433 | else |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 434 | UPDATE_ARGS+=( --nousepkg ) |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 435 | fi |
| 436 | if [[ ${FLAGS_fast} -eq ${FLAGS_TRUE} ]]; then |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 437 | UPDATE_ARGS+=( --fast ) |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 438 | else |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 439 | UPDATE_ARGS+=( --nofast ) |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 440 | fi |
David James | 184e390 | 2012-02-23 20:19:28 -0800 | [diff] [blame] | 441 | if [[ "${FLAGS_jobs}" -ne -1 ]]; then |
| 442 | UPDATE_ARGS+=( --jobs=${FLAGS_jobs} ) |
| 443 | fi |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 444 | enter_chroot "${CHROOT_TRUNK}/src/scripts/update_chroot" "${UPDATE_ARGS[@]}" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 445 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 446 | CHROOT_EXAMPLE_OPT="" |
| 447 | if [[ "$FLAGS_chroot" != "$DEFAULT_CHROOT_DIR" ]]; then |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 448 | CHROOT_EXAMPLE_OPT="--chroot=$FLAGS_chroot" |
| 449 | fi |
| 450 | |
| 451 | print_time_elapsed |
| 452 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 453 | cat <<EOF |
Mike Frysinger | bdc4fb1 | 2012-02-10 11:20:03 -0500 | [diff] [blame] | 454 | |
| 455 | ${CROS_LOG_PREFIX:-cros_sdk}: All set up. To enter the chroot, run: |
| 456 | $ cros_sdk --enter $CHROOT_EXAMPLE_OPT |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 457 | |
| 458 | CAUTION: Do *NOT* rm -rf the chroot directory; if there are stale bind |
| 459 | mounts you may end up deleting your source tree too. To unmount and |
| 460 | delete the chroot cleanly, use: |
| 461 | $ cros_sdk --delete $CHROOT_EXAMPLE_OPT |
Mike Frysinger | bdc4fb1 | 2012-02-10 11:20:03 -0500 | [diff] [blame] | 462 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 463 | EOF |