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