blob: e0ddd0b964a4ff961115e4aa237b53664c43ee1b [file] [log] [blame]
Brian Harringf539bc32012-02-06 00:18:37 -08001#!/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
12SCRIPT_ROOT=$(readlink -f $(dirname "$0")/..)
13. "${SCRIPT_ROOT}/common.sh" || exit 1
14
Brian Harring35767822012-02-01 23:50:45 -080015ENTER_CHROOT=$(readlink -f $(dirname "$0")/enter_chroot.sh)
16
Brian Harringfeb04f72012-02-03 21:22:50 -080017enable_strict_sudo
18
Brian Harringf539bc32012-02-06 00:18:37 -080019# Check if the host machine architecture is supported.
20ARCHITECTURE="$(uname -m)"
21if [[ "$ARCHITECTURE" != "x86_64" ]]; then
22 echo "$SCRIPT_NAME: $ARCHITECTURE is not supported as a host machine architecture."
23 exit 1
24fi
25
Brian Harring35767822012-02-01 23:50:45 -080026# Script must be run outside the chroot.
Brian Harringf539bc32012-02-06 00:18:37 -080027assert_outside_chroot
28
Brian Harring35767822012-02-01 23:50:45 -080029# Define command line flags.
Brian Harringf539bc32012-02-06 00:18:37 -080030# See http://code.google.com/p/shflags/wiki/Documentation10x
31
32DEFINE_string chroot "$DEFAULT_CHROOT_DIR" \
33 "Destination dir for the chroot environment."
34DEFINE_boolean usepkg $FLAGS_TRUE "Use binary packages to bootstrap."
35DEFINE_boolean delete $FLAGS_FALSE "Delete an existing chroot."
36DEFINE_boolean replace $FLAGS_FALSE "Overwrite existing chroot, if any."
37DEFINE_integer jobs -1 "How many packages to build in parallel at maximum."
38DEFINE_boolean fast ${DEFAULT_FAST} "Call many emerges in parallel"
39DEFINE_string stage3_date "2010.03.09" \
40 "Use the stage3 with the given date."
41DEFINE_string stage3_path "" \
42 "Use the stage3 located on this path."
Brian Harringf539bc32012-02-06 00:18:37 -080043
Brian Harring35767822012-02-01 23:50:45 -080044# Parse command line flags.
Brian Harringf539bc32012-02-06 00:18:37 -080045FLAGS_HELP="usage: $SCRIPT_NAME [flags]"
46FLAGS "$@" || exit 1
47eval set -- "${FLAGS_ARGV}"
48check_flags_only_and_allow_null_arg "$@" && set --
49
Brian Harring35767822012-02-01 23:50:45 -080050CROS_LOG_PREFIX=cros_sdk
Brian Harringf539bc32012-02-06 00:18:37 -080051
52assert_not_root_user
53# Set the right umask for chroot creation.
54umask 022
55
56# Only now can we die on error. shflags functions leak non-zero error codes,
Brian Harring7f175a52012-03-02 05:37:00 -080057# so will die prematurely if 'switch_to_strict_mode' is specified before now.
Brian Harringf539bc32012-02-06 00:18:37 -080058# TODO: replace shflags with something less error-prone, or contribute a fix.
Brian Harring7f175a52012-03-02 05:37:00 -080059switch_to_strict_mode
Brian Harringf539bc32012-02-06 00:18:37 -080060
J. Richard Barnettee80f6de2012-02-24 14:08:34 -080061. "${SCRIPT_ROOT}"/sdk_lib/make_conf_util.sh
62
Brian Harringf539bc32012-02-06 00:18:37 -080063FULLNAME="ChromeOS Developer"
64DEFGROUPS="eng,adm,cdrom,floppy,audio,video,portage"
65PASSWORD=chronos
66CRYPTED_PASSWD=$(perl -e 'print crypt($ARGV[0], "foo")', $PASSWORD)
67
68USEPKG=""
69if [[ $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"
74fi
75
76# Support faster build if necessary.
77EMERGE_CMD="emerge"
78if [ "$FLAGS_fast" -eq "${FLAGS_TRUE}" ]; then
79 CHROOT_CHROMITE_DIR="/home/${USER}/trunk/chromite"
80 EMERGE_CMD="${CHROOT_CHROMITE_DIR}/bin/parallel_emerge"
81fi
82
Brian Harring35767822012-02-01 23:50:45 -080083ENTER_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 Frysinger6b1abb22012-05-11 13:44:06 -040090enter_chroot() {
Brian Harring35767822012-02-01 23:50:45 -080091 "$ENTER_CHROOT" --chroot "$FLAGS_chroot" -- "${ENTER_CHROOT_ARGS[@]}" "$@"
Brian Harringf539bc32012-02-06 00:18:37 -080092}
93
Brian Harring35767822012-02-01 23:50:45 -080094# Invoke enter_chroot running the command as root, and w/out sudo.
95# This should be used prior to sudo being merged.
Mike Frysingerba758452012-04-02 13:28:31 -040096early_env=()
Mike Frysinger6b1abb22012-05-11 13:44:06 -040097early_enter_chroot() {
Brian Harring35767822012-02-01 23:50:45 -080098 "$ENTER_CHROOT" --chroot "$FLAGS_chroot" --early_make_chroot \
Mike Frysingerba758452012-04-02 13:28:31 -040099 -- "${ENTER_CHROOT_ARGS[@]}" "${early_env[@]}" "$@"
Brian Harringf539bc32012-02-06 00:18:37 -0800100}
101
Brian Harring35767822012-02-01 23:50:45 -0800102# 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.
105sudo_chroot() {
106 sudo chroot "${FLAGS_chroot}" "$@"
Brian Harringf539bc32012-02-06 00:18:37 -0800107}
108
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400109cleanup() {
Brian Harringf539bc32012-02-06 00:18:37 -0800110 # Clean up mounts
111 safe_umount_tree "${FLAGS_chroot}"
112}
113
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400114delete_existing() {
Brian Harring35767822012-02-01 23:50:45 -0800115 # Delete old chroot dir.
116 if [[ ! -e "$FLAGS_chroot" ]]; then
117 return
Brian Harringf539bc32012-02-06 00:18:37 -0800118 fi
Brian Harring35767822012-02-01 23:50:45 -0800119 info "Cleaning up old mount points..."
120 cleanup
121 info "Deleting $FLAGS_chroot..."
122 sudo rm -rf "$FLAGS_chroot"
123 info "Done."
Brian Harringf539bc32012-02-06 00:18:37 -0800124}
125
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400126init_users () {
Brian Harring35767822012-02-01 23:50:45 -0800127 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 Harringf539bc32012-02-06 00:18:37 -0800130 if [ -f /etc/localtime ] ; then
131 sudo cp /etc/localtime "${FLAGS_chroot}/etc"
132 else
Brian Harring35767822012-02-01 23:50:45 -0800133 sudo ln -sf /usr/share/zoneinfo/PST8PDT "${FLAGS_chroot}/etc/localtime"
Brian Harringf539bc32012-02-06 00:18:37 -0800134 fi
Brian Harring35767822012-02-01 23:50:45 -0800135 info "Adding user/group..."
Brian Harringf539bc32012-02-06 00:18:37 -0800136 # Add ourselves as a user inside the chroot.
Brian Harring35767822012-02-01 23:50:45 -0800137 sudo_chroot groupadd -g 5000 eng
Brian Harringf539bc32012-02-06 00:18:37 -0800138 # 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 Harring35767822012-02-01 23:50:45 -0800143 sudo_chroot useradd -o -G ${DEFGROUPS} -g eng -u `id -u` -s \
Brian Harringf539bc32012-02-06 00:18:37 -0800144 /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 Harring35767822012-02-01 23:50:45 -0800147 sudo sed -e '1{h;d};$!{H;d};$G' -i "${FLAGS_chroot}/etc/passwd"
Brian Harringf539bc32012-02-06 00:18:37 -0800148}
149
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400150init_setup () {
Brian Harring35767822012-02-01 23:50:45 -0800151 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 Harringf539bc32012-02-06 00:18:37 -0800155 sudo ln -sf "${CHROOT_TRUNK}/src/third_party/portage" \
156 "${FLAGS_chroot}/usr/portage"
Brian Harringf539bc32012-02-06 00:18:37 -0800157 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 Harringf539bc32012-02-06 00:18:37 -0800161
Brian Harring35767822012-02-01 23:50:45 -0800162 # Some operations need an mtab.
163 sudo ln -s /proc/mounts "${FLAGS_chroot}/etc/mtab"
Brian Harringf539bc32012-02-06 00:18:37 -0800164
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
171Defaults env_keep += CROS_WORKON_SRCROOT
172Defaults env_keep += CHROMEOS_OFFICIAL
173Defaults env_keep += PORTAGE_USERNAME
174Defaults env_keep += http_proxy
175Defaults env_keep += ftp_proxy
176Defaults env_keep += all_proxy
177%adm ALL=(ALL) ALL
178root ALL=(ALL) ALL
179$USER ALL=NOPASSWD: ALL
180EOF
Brian Harring35767822012-02-01 23:50:45 -0800181 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 Harringf539bc32012-02-06 00:18:37 -0800184
Brian Harring35767822012-02-01 23:50:45 -0800185 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 Harringf539bc32012-02-06 00:18:37 -0800189
190 # Setup host make.conf. This includes any overlay that we may be using
191 # and a pointer to pre-built packages.
Brian Harring35767822012-02-01 23:50:45 -0800192 # TODO: This should really be part of a profile in the portage.
193 info "Setting up /etc/make.*..."
Brian Harringf539bc32012-02-06 00:18:37 -0800194 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 Harring35767822012-02-01 23:50:45 -0800201 # Create make.conf.user .
Brian Harringf539bc32012-02-06 00:18:37 -0800202 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 Harring36b102b2012-02-06 23:34:25 -0800206 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 Harring7ee892d2012-02-02 09:33:10 -0800213
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 Harringf539bc32012-02-06 00:18:37 -0800221
Brian Harringf539bc32012-02-06 00:18:37 -0800222 # 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
227PATH=/home/$USER/trunk/chromite/bin:/home/$USER/depot_tools
228CROS_WORKON_SRCROOT="${CHROOT_TRUNK}"
229PORTAGE_USERNAME=$USER
230EOF
231
232 # TODO(zbehan): Configure stuff that is usually configured in postinst's,
Mike Frysingereb1a9b42012-03-28 16:21:04 -0400233 # but wasn't. Fix the postinst's.
Brian Harring35767822012-02-01 23:50:45 -0800234 info "Running post-inst configuration hacks"
235 early_enter_chroot env-update
Brian Harringf539bc32012-02-06 00:18:37 -0800236
Brian Harring35767822012-02-01 23:50:45 -0800237 # 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 Harringf539bc32012-02-06 00:18:37 -0800243
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.
251PS1='\$(r=\$?; __git_branch_ps1 "(%s) "; exit \$r)'"\${PS1}"
252PS1="(cr) \${PS1}"
253EOF
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
264en_US ISO-8859-1
265en_US.UTF-8 UTF-8
266EOF
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 Harring35767822012-02-01 23:50:45 -0800275 # Automatically change to scripts directory.
Brian Harringf539bc32012-02-06 00:18:37 -0800276 echo 'cd ${CHROOT_CWD:-~/trunk/src/scripts}' \
277 >> "$FLAGS_chroot/home/$USER/.bash_profile"
278
Brian Harring35767822012-02-01 23:50:45 -0800279 # Enable bash completion for build scripts.
Brian Harringf539bc32012-02-06 00:18:37 -0800280 echo ". ~/trunk/src/scripts/bash_completion" \
281 >> "$FLAGS_chroot/home/$USER/.bashrc"
282
Brian Harring35767822012-02-01 23:50:45 -0800283 # Warn if attempting to use source control commands inside the chroot.
Brian Harringf539bc32012-02-06 00:18:37 -0800284 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 Harring35767822012-02-01 23:50:45 -0800296 # 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 Harringf539bc32012-02-06 00:18:37 -0800298 echo "Copying ~/.gitconfig into chroot"
299 cp $HOME/.gitconfig "$FLAGS_chroot/home/$USER/"
300 fi
Luigi Semenzato2443fdd2012-05-29 10:34:04 -0700301
302 if [[ -f $HOME/.cros_chroot_init ]]; then
303 /bin/bash $HOME/.cros_chroot_init "${FLAGS_chroot}"
304 fi
Brian Harringf539bc32012-02-06 00:18:37 -0800305}
306
Brian Harring35767822012-02-01 23:50:45 -0800307# Handle deleting an existing environment.
Brian Harringf539bc32012-02-06 00:18:37 -0800308if [[ $FLAGS_delete -eq $FLAGS_TRUE || \
Brian Harring35767822012-02-01 23:50:45 -0800309 $FLAGS_replace -eq $FLAGS_TRUE ]]; then
Brian Harringf539bc32012-02-06 00:18:37 -0800310 delete_existing
311 [[ $FLAGS_delete -eq $FLAGS_TRUE ]] && exit 0
312fi
313
314CHROOT_TRUNK="${CHROOT_TRUNK_DIR}"
315PORTAGE="${SRC_ROOT}/third_party/portage"
316OVERLAY="${SRC_ROOT}/third_party/chromiumos-overlay"
317CONFIG_DIR="${OVERLAY}/chromeos/config"
318CHROOT_CONFIG="${CHROOT_TRUNK}/src/third_party/chromiumos-overlay/chromeos/config"
319PORTAGE_STABLE_OVERLAY="/usr/local/portage/stable"
320CROSSDEV_OVERLAY="/usr/local/portage/crossdev"
321CHROOT_OVERLAY="/usr/local/portage/chromiumos"
322CHROOT_STATE="${FLAGS_chroot}/etc/debian_chroot"
Brian Harringf539bc32012-02-06 00:18:37 -0800323
324# Pass proxy variables into the environment.
325for type in http ftp all; do
326 value=$(env | grep ${type}_proxy || true)
327 if [ -n "${value}" ]; then
328 CHROOT_PASSTHRU+=("$value")
329 fi
330done
331
Brian Harring35767822012-02-01 23:50:45 -0800332# Create the base Gentoo stage3 based on last version put in chroot.
Brian Harringf539bc32012-02-06 00:18:37 -0800333STAGE3="${OVERLAY}/chromeos/stage3/stage3-amd64-${FLAGS_stage3_date}.tar.bz2"
334if [ -f $CHROOT_STATE ] && \
335 ! sudo egrep -q "^STAGE3=$STAGE3" $CHROOT_STATE >/dev/null 2>&1
336then
Brian Harring35767822012-02-01 23:50:45 -0800337 info "STAGE3 version has changed."
Brian Harringf539bc32012-02-06 00:18:37 -0800338 delete_existing
339fi
340
341if [ -n "${FLAGS_stage3_path}" ]; then
Brian Harring35767822012-02-01 23:50:45 -0800342 if [ ! -f "${FLAGS_stage3_path}" ]; then
Brian Harringf539bc32012-02-06 00:18:37 -0800343 error "Invalid stage3!"
344 exit 1;
345 fi
Brian Harring35767822012-02-01 23:50:45 -0800346 STAGE3="${FLAGS_stage3_path}"
Brian Harringf539bc32012-02-06 00:18:37 -0800347fi
348
Brian Harring35767822012-02-01 23:50:45 -0800349# Create the destination directory.
Brian Harringf539bc32012-02-06 00:18:37 -0800350mkdir -p "$FLAGS_chroot"
351
352echo
353if [ -f $CHROOT_STATE ]
354then
Brian Harring35767822012-02-01 23:50:45 -0800355 info "STAGE3 already set up. Skipping..."
Brian Harringf539bc32012-02-06 00:18:37 -0800356else
Brian Harring35767822012-02-01 23:50:45 -0800357 info "Unpacking STAGE3..."
Zdenek Behan074f9ef2012-05-30 01:23:59 +0200358 case ${STAGE3} in
359 *.tbz2|*.tar.bz2) DECOMPRESS=$(type -p pbzip2 || echo bzip2) ;;
360 *.tar.xz) DECOMPRESS="xz" ;;
361 *) die "Unknown tarball compression: ${STAGE3}";;
362 esac
363 ${DECOMPRESS} -dc "${STAGE3}" | \
Brian Harring021858a2012-05-26 19:33:01 -0700364 sudo tar -xp -C "${FLAGS_chroot}"
Brian Harring35767822012-02-01 23:50:45 -0800365 sudo rm -f "$FLAGS_chroot/etc/"make.{globals,conf.user}
Brian Harringf539bc32012-02-06 00:18:37 -0800366fi
367
Brian Harring35767822012-02-01 23:50:45 -0800368# Set up users, if needed, before mkdir/mounts below.
Brian Harringf539bc32012-02-06 00:18:37 -0800369[ -f $CHROOT_STATE ] || init_users
370
371echo
Brian Harring35767822012-02-01 23:50:45 -0800372info "Setting up mounts..."
373# Set up necessary mounts and make sure we clean them up on exit.
374sudo mkdir -p "${FLAGS_chroot}/${CHROOT_TRUNK}" "${FLAGS_chroot}/run"
Brian Harring35767822012-02-01 23:50:45 -0800375
J. Richard Barnettee80f6de2012-02-24 14:08:34 -0800376# Create a special /etc/make.conf.host_setup that we use to bootstrap
377# the chroot. The regular content for the file will be generated the
378# first time we invoke update_chroot (further down in this script).
379create_bootstrap_host_setup "${FLAGS_chroot}"
Brian Harringf539bc32012-02-06 00:18:37 -0800380
381if ! [ -f "$CHROOT_STATE" ];then
382 INITIALIZE_CHROOT=1
383fi
384
385
Mike Frysingerba758452012-04-02 13:28:31 -0400386if ! early_enter_chroot bash -c 'type -P pbzip2' >/dev/null ; then
387 # This chroot lacks pbzip2 early on, so we need to disable it.
388 early_env+=(
389 PORTAGE_BZIP2_COMMAND="bzip2"
390 PORTAGE_BUNZIP2_COMMAND="bunzip2"
391 )
392fi
393
Brian Harringf539bc32012-02-06 00:18:37 -0800394if [ -z "${INITIALIZE_CHROOT}" ];then
Brian Harring35767822012-02-01 23:50:45 -0800395 info "chroot already initialized. Skipping..."
Brian Harringf539bc32012-02-06 00:18:37 -0800396else
Brian Harring35767822012-02-01 23:50:45 -0800397 # Run all the init stuff to setup the env.
Brian Harringf539bc32012-02-06 00:18:37 -0800398 init_setup
399fi
400
Brian Harring35767822012-02-01 23:50:45 -0800401# Add file to indicate that it is a chroot.
402# Add version of $STAGE3 for update checks.
Brian Harringf539bc32012-02-06 00:18:37 -0800403sudo sh -c "echo STAGE3=$STAGE3 > $CHROOT_STATE"
404
Brian Harring35767822012-02-01 23:50:45 -0800405info "Updating portage"
Mike Frysinger66fd81f2012-02-28 13:13:20 -0500406early_enter_chroot emerge -uNv --quiet portage
Brian Harringf539bc32012-02-06 00:18:37 -0800407
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100408info "Updating host toolchain"
409early_enter_chroot emerge -uNv --quiet crossdev
410TOOLCHAIN_ARGS=( --deleteold )
411if [[ ${FLAGS_usepkg} -eq ${FLAGS_FALSE} ]]; then
412 TOOLCHAIN_ARGS+=( --nousepkg )
413fi
414# Note: early_enter_chroot executes as root.
415early_enter_chroot "${CHROOT_TRUNK}/chromite/bin/cros_setup_toolchains" \
416 --hostonly "${TOOLCHAIN_ARGS[@]}"
Brian Harringf539bc32012-02-06 00:18:37 -0800417
418# dhcpcd is included in 'world' by the stage3 that we pull in for some reason.
419# We have no need to install it in our host environment, so pull it out here.
Brian Harring35767822012-02-01 23:50:45 -0800420info "Deselecting dhcpcd"
421early_enter_chroot $EMERGE_CMD --deselect dhcpcd
Brian Harringf539bc32012-02-06 00:18:37 -0800422
Mike Frysinger279f1032012-05-17 15:54:31 -0400423info "Running emerge curl sudo ..."
Mike Frysinger650bf872012-02-27 11:05:26 -0500424early_enter_chroot $EMERGE_CMD -uNv $USEPKG --select $EMERGE_JOBS \
Mike Frysinger279f1032012-05-17 15:54:31 -0400425 pbzip2 net-misc/curl sudo
Brian Harringf539bc32012-02-06 00:18:37 -0800426
Brian Harringf539bc32012-02-06 00:18:37 -0800427if [ -n "${INITIALIZE_CHROOT}" ]; then
428 # If we're creating a new chroot, we also want to set it to the latest
429 # version.
Brian Harring35767822012-02-01 23:50:45 -0800430 enter_chroot \
431 "${CHROOT_TRUNK}/src/scripts/run_chroot_version_hooks" --force_latest
Brian Harringf539bc32012-02-06 00:18:37 -0800432fi
433
Brian Harring35767822012-02-01 23:50:45 -0800434# Update chroot.
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100435# Skip toolchain update because it already happened above, and the chroot is
436# not ready to emerge all cross toolchains.
437UPDATE_ARGS=( --skip_toolchain_update )
438if [[ ${FLAGS_usepkg} -eq ${FLAGS_TRUE} ]]; then
Brian Harring35767822012-02-01 23:50:45 -0800439 UPDATE_ARGS+=( --usepkg )
Brian Harringf539bc32012-02-06 00:18:37 -0800440else
Brian Harring35767822012-02-01 23:50:45 -0800441 UPDATE_ARGS+=( --nousepkg )
Brian Harringf539bc32012-02-06 00:18:37 -0800442fi
443if [[ ${FLAGS_fast} -eq ${FLAGS_TRUE} ]]; then
Brian Harring35767822012-02-01 23:50:45 -0800444 UPDATE_ARGS+=( --fast )
Brian Harringf539bc32012-02-06 00:18:37 -0800445else
Brian Harring35767822012-02-01 23:50:45 -0800446 UPDATE_ARGS+=( --nofast )
Brian Harringf539bc32012-02-06 00:18:37 -0800447fi
David James184e3902012-02-23 20:19:28 -0800448if [[ "${FLAGS_jobs}" -ne -1 ]]; then
449 UPDATE_ARGS+=( --jobs=${FLAGS_jobs} )
450fi
Brian Harring35767822012-02-01 23:50:45 -0800451enter_chroot "${CHROOT_TRUNK}/src/scripts/update_chroot" "${UPDATE_ARGS[@]}"
Brian Harringf539bc32012-02-06 00:18:37 -0800452
Brian Harring35767822012-02-01 23:50:45 -0800453CHROOT_EXAMPLE_OPT=""
454if [[ "$FLAGS_chroot" != "$DEFAULT_CHROOT_DIR" ]]; then
Brian Harringf539bc32012-02-06 00:18:37 -0800455 CHROOT_EXAMPLE_OPT="--chroot=$FLAGS_chroot"
456fi
457
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100458# As a final pass, build all desired cross-toolchains.
459info "Updating toolchains"
460enter_chroot sudo "${CHROOT_TRUNK}/chromite/bin/cros_setup_toolchains" \
461 "${TOOLCHAIN_ARGS[@]}"
462
Brian Harringf539bc32012-02-06 00:18:37 -0800463print_time_elapsed
464
Brian Harring35767822012-02-01 23:50:45 -0800465cat <<EOF
Mike Frysingerbdc4fb12012-02-10 11:20:03 -0500466
467${CROS_LOG_PREFIX:-cros_sdk}: All set up. To enter the chroot, run:
468$ cros_sdk --enter $CHROOT_EXAMPLE_OPT
Brian Harring35767822012-02-01 23:50:45 -0800469
470CAUTION: Do *NOT* rm -rf the chroot directory; if there are stale bind
471mounts you may end up deleting your source tree too. To unmount and
472delete the chroot cleanly, use:
473$ cros_sdk --delete $CHROOT_EXAMPLE_OPT
Mike Frysingerbdc4fb12012-02-10 11:20:03 -0500474
Brian Harring35767822012-02-01 23:50:45 -0800475EOF