blob: 5479a0fc3308ab3fb3508fc531ee3da4c0f6669e [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 Harringf539bc32012-02-06 00:18:37 -080017# Check if the host machine architecture is supported.
18ARCHITECTURE="$(uname -m)"
19if [[ "$ARCHITECTURE" != "x86_64" ]]; then
20 echo "$SCRIPT_NAME: $ARCHITECTURE is not supported as a host machine architecture."
21 exit 1
22fi
23
Brian Harring35767822012-02-01 23:50:45 -080024# Script must be run outside the chroot.
Brian Harringf539bc32012-02-06 00:18:37 -080025assert_outside_chroot
26
Brian Harring35767822012-02-01 23:50:45 -080027# Define command line flags.
Brian Harringf539bc32012-02-06 00:18:37 -080028# See http://code.google.com/p/shflags/wiki/Documentation10x
29
30DEFINE_string chroot "$DEFAULT_CHROOT_DIR" \
31 "Destination dir for the chroot environment."
32DEFINE_boolean usepkg $FLAGS_TRUE "Use binary packages to bootstrap."
33DEFINE_boolean delete $FLAGS_FALSE "Delete an existing chroot."
34DEFINE_boolean replace $FLAGS_FALSE "Overwrite existing chroot, if any."
35DEFINE_integer jobs -1 "How many packages to build in parallel at maximum."
36DEFINE_boolean fast ${DEFAULT_FAST} "Call many emerges in parallel"
37DEFINE_string stage3_date "2010.03.09" \
38 "Use the stage3 with the given date."
39DEFINE_string stage3_path "" \
40 "Use the stage3 located on this path."
Brian Harringf539bc32012-02-06 00:18:37 -080041
Brian Harring35767822012-02-01 23:50:45 -080042# Parse command line flags.
Brian Harringf539bc32012-02-06 00:18:37 -080043FLAGS_HELP="usage: $SCRIPT_NAME [flags]"
44FLAGS "$@" || exit 1
45eval set -- "${FLAGS_ARGV}"
46check_flags_only_and_allow_null_arg "$@" && set --
47
Brian Harring35767822012-02-01 23:50:45 -080048CROS_LOG_PREFIX=cros_sdk
Brian Harringf539bc32012-02-06 00:18:37 -080049
50assert_not_root_user
51# Set the right umask for chroot creation.
52umask 022
53
54# Only now can we die on error. shflags functions leak non-zero error codes,
55# so will die prematurely if 'set -e' is specified before now.
56# TODO: replace shflags with something less error-prone, or contribute a fix.
57set -e
58
59FULLNAME="ChromeOS Developer"
60DEFGROUPS="eng,adm,cdrom,floppy,audio,video,portage"
61PASSWORD=chronos
62CRYPTED_PASSWD=$(perl -e 'print crypt($ARGV[0], "foo")', $PASSWORD)
63
64USEPKG=""
65if [[ $FLAGS_usepkg -eq $FLAGS_TRUE ]]; then
66 # Use binary packages. Include all build-time dependencies,
67 # so as to avoid unnecessary differences between source
68 # and binary builds.
69 USEPKG="--getbinpkg --usepkg --with-bdeps y"
70fi
71
72# Support faster build if necessary.
73EMERGE_CMD="emerge"
74if [ "$FLAGS_fast" -eq "${FLAGS_TRUE}" ]; then
75 CHROOT_CHROMITE_DIR="/home/${USER}/trunk/chromite"
76 EMERGE_CMD="${CHROOT_CHROMITE_DIR}/bin/parallel_emerge"
77fi
78
Brian Harring35767822012-02-01 23:50:45 -080079ENTER_CHROOT_ARGS=(
80 CROS_WORKON_SRCROOT="$CHROOT_TRUNK"
81 PORTAGE_USERNAME="$USER"
82 IGNORE_PREFLIGHT_BINHOST="$IGNORE_PREFLIGHT_BINHOST"
83)
84
85# Invoke enter_chroot. This can only be used after sudo has been installed.
86function enter_chroot {
87 "$ENTER_CHROOT" --chroot "$FLAGS_chroot" -- "${ENTER_CHROOT_ARGS[@]}" "$@"
Brian Harringf539bc32012-02-06 00:18:37 -080088}
89
Brian Harring35767822012-02-01 23:50:45 -080090# Invoke enter_chroot running the command as root, and w/out sudo.
91# This should be used prior to sudo being merged.
92function early_enter_chroot() {
93 "$ENTER_CHROOT" --chroot "$FLAGS_chroot" --early_make_chroot \
94 -- "${ENTER_CHROOT_ARGS[@]}" "$@"
Brian Harringf539bc32012-02-06 00:18:37 -080095}
96
Brian Harring35767822012-02-01 23:50:45 -080097# Run a command within the chroot. The main usage of this is to avoid
98# the overhead of enter_chroot, and do not need access to the source tree,
99# don't need the actual chroot profile env, and can run the command as root.
100sudo_chroot() {
101 sudo chroot "${FLAGS_chroot}" "$@"
Brian Harringf539bc32012-02-06 00:18:37 -0800102}
103
104function cleanup {
105 # Clean up mounts
106 safe_umount_tree "${FLAGS_chroot}"
107}
108
109function delete_existing {
Brian Harring35767822012-02-01 23:50:45 -0800110 # Delete old chroot dir.
111 if [[ ! -e "$FLAGS_chroot" ]]; then
112 return
Brian Harringf539bc32012-02-06 00:18:37 -0800113 fi
Brian Harring35767822012-02-01 23:50:45 -0800114 info "Cleaning up old mount points..."
115 cleanup
116 info "Deleting $FLAGS_chroot..."
117 sudo rm -rf "$FLAGS_chroot"
118 info "Done."
Brian Harringf539bc32012-02-06 00:18:37 -0800119}
120
121function init_users () {
Brian Harring35767822012-02-01 23:50:45 -0800122 info "Set timezone..."
123 # date +%Z has trouble with daylight time, so use host's info.
124 sudo rm -f "${FLAGS_chroot}/etc/localtime"
Brian Harringf539bc32012-02-06 00:18:37 -0800125 if [ -f /etc/localtime ] ; then
126 sudo cp /etc/localtime "${FLAGS_chroot}/etc"
127 else
Brian Harring35767822012-02-01 23:50:45 -0800128 sudo ln -sf /usr/share/zoneinfo/PST8PDT "${FLAGS_chroot}/etc/localtime"
Brian Harringf539bc32012-02-06 00:18:37 -0800129 fi
Brian Harring35767822012-02-01 23:50:45 -0800130 info "Adding user/group..."
Brian Harringf539bc32012-02-06 00:18:37 -0800131 # Add ourselves as a user inside the chroot.
Brian Harring35767822012-02-01 23:50:45 -0800132 sudo_chroot groupadd -g 5000 eng
Brian Harringf539bc32012-02-06 00:18:37 -0800133 # We need the UID to match the host user's. This can conflict with
134 # a particular chroot UID. At the same time, the added user has to
135 # be a primary user for the given UID for sudo to work, which is
136 # determined by the order in /etc/passwd. Let's put ourselves on top
137 # of the file.
Brian Harring35767822012-02-01 23:50:45 -0800138 sudo_chroot useradd -o -G ${DEFGROUPS} -g eng -u `id -u` -s \
Brian Harringf539bc32012-02-06 00:18:37 -0800139 /bin/bash -m -c "${FULLNAME}" -p ${CRYPTED_PASSWD} ${USER}
140 # Because passwd generally isn't sorted and the entry ended up at the
141 # bottom, it is safe to just take it and move it to top instead.
Brian Harring35767822012-02-01 23:50:45 -0800142 sudo sed -e '1{h;d};$!{H;d};$G' -i "${FLAGS_chroot}/etc/passwd"
Brian Harringf539bc32012-02-06 00:18:37 -0800143}
144
145function init_setup () {
Brian Harring35767822012-02-01 23:50:45 -0800146 info "Running init_setup()..."
147 sudo mkdir -p -m 755 "${FLAGS_chroot}/usr" \
148 "${FLAGS_chroot}/usr/local/portage" \
149 "${FLAGS_chroot}"/"${CROSSDEV_OVERLAY}"
Brian Harringf539bc32012-02-06 00:18:37 -0800150 sudo ln -sf "${CHROOT_TRUNK}/src/third_party/portage" \
151 "${FLAGS_chroot}/usr/portage"
Brian Harringf539bc32012-02-06 00:18:37 -0800152 sudo ln -sf "${CHROOT_TRUNK}/src/third_party/chromiumos-overlay" \
153 "${FLAGS_chroot}"/"${CHROOT_OVERLAY}"
154 sudo ln -sf "${CHROOT_TRUNK}/src/third_party/portage-stable" \
155 "${FLAGS_chroot}"/"${PORTAGE_STABLE_OVERLAY}"
Brian Harringf539bc32012-02-06 00:18:37 -0800156
Brian Harring35767822012-02-01 23:50:45 -0800157 # Some operations need an mtab.
158 sudo ln -s /proc/mounts "${FLAGS_chroot}/etc/mtab"
Brian Harringf539bc32012-02-06 00:18:37 -0800159
160 # Set up sudoers. Inside the chroot, the user can sudo without a password.
161 # (Safe enough, since the only way into the chroot is to 'sudo chroot', so
162 # the user's already typed in one sudo password...)
163 # Make sure the sudoers.d subdir exists as older stage3 base images lack it.
164 sudo mkdir -p "${FLAGS_chroot}/etc/sudoers.d"
165 sudo_clobber "${FLAGS_chroot}/etc/sudoers.d/90_cros" <<EOF
166Defaults env_keep += CROS_WORKON_SRCROOT
167Defaults env_keep += CHROMEOS_OFFICIAL
168Defaults env_keep += PORTAGE_USERNAME
169Defaults env_keep += http_proxy
170Defaults env_keep += ftp_proxy
171Defaults env_keep += all_proxy
172%adm ALL=(ALL) ALL
173root ALL=(ALL) ALL
174$USER ALL=NOPASSWD: ALL
175EOF
Brian Harring35767822012-02-01 23:50:45 -0800176 sudo find "${FLAGS_chroot}/etc/"sudoers* -type f -exec chmod 0440 {} +
177 # Fix bad group for some.
178 sudo chown -R root:root "${FLAGS_chroot}/etc/"sudoers*
Brian Harringf539bc32012-02-06 00:18:37 -0800179
Brian Harring35767822012-02-01 23:50:45 -0800180 info "Setting up hosts/resolv..."
181 # Copy config from outside chroot into chroot.
182 sudo cp /etc/{hosts,resolv.conf} "$FLAGS_chroot/etc/"
183 sudo chmod 0644 "$FLAGS_chroot"/etc/{hosts,resolv.conf}
Brian Harringf539bc32012-02-06 00:18:37 -0800184
185 # Setup host make.conf. This includes any overlay that we may be using
186 # and a pointer to pre-built packages.
Brian Harring35767822012-02-01 23:50:45 -0800187 # TODO: This should really be part of a profile in the portage.
188 info "Setting up /etc/make.*..."
Brian Harringf539bc32012-02-06 00:18:37 -0800189 sudo mv "${FLAGS_chroot}"/etc/make.conf{,.orig}
190 sudo ln -sf "${CHROOT_CONFIG}/make.conf.amd64-host" \
191 "${FLAGS_chroot}/etc/make.conf"
192 sudo mv "${FLAGS_chroot}"/etc/make.profile{,.orig}
193 sudo ln -sf "${CHROOT_OVERLAY}/profiles/default/linux/amd64/10.0" \
194 "${FLAGS_chroot}/etc/make.profile"
195
Brian Harring35767822012-02-01 23:50:45 -0800196 # Create make.conf.user .
Brian Harringf539bc32012-02-06 00:18:37 -0800197 sudo touch "${FLAGS_chroot}"/etc/make.conf.user
198 sudo chmod 0644 "${FLAGS_chroot}"/etc/make.conf.user
199
200 # Create directories referred to by our conf files.
Brian Harring36b102b2012-02-06 23:34:25 -0800201 sudo mkdir -p -m 775 "${FLAGS_chroot}/var/lib/portage/pkgs" \
202 "${FLAGS_chroot}/var/cache/distfiles" \
203 "${FLAGS_chroot}/var/cache/chromeos-chrome"
204
205 # Run this from w/in the chroot so we use whatever uid/gid
206 # these are defined as w/in the chroot.
207 sudo_chroot chown "${USER}:portage" /var/cache/chromeos-chrome
Brian Harring7ee892d2012-02-02 09:33:10 -0800208
209 # These are created for compatibility while transitioning
210 # make.conf and friends over to the new location.
211 # TODO(ferringb): remove this 03/12 or so.
212 sudo ln -s ../../cache/distfiles/host \
213 "${FLAGS_chroot}/var/lib/portage/distfiles"
214 sudo ln -s ../../cache/distfiles/target \
215 "${FLAGS_chroot}/var/lib/portage/distfiles-target"
Brian Harringf539bc32012-02-06 00:18:37 -0800216
217 if [[ $FLAGS_jobs -ne -1 ]]; then
218 EMERGE_JOBS="--jobs=$FLAGS_jobs"
219 fi
220
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
226PATH=/home/$USER/trunk/chromite/bin:/home/$USER/depot_tools
227CROS_WORKON_SRCROOT="${CHROOT_TRUNK}"
228PORTAGE_USERNAME=$USER
229EOF
230
231 # TODO(zbehan): Configure stuff that is usually configured in postinst's,
232 # but wasn't. Fix the postinst's. crosbug.com/18036
Brian Harring35767822012-02-01 23:50:45 -0800233 info "Running post-inst configuration hacks"
234 early_enter_chroot env-update
Brian Harringf539bc32012-02-06 00:18:37 -0800235 if [ -f ${FLAGS_chroot}/usr/bin/build-docbook-catalog ]; then
236 # For too ancient chroots that didn't have build-docbook-catalog, this
237 # is not relevant, and will get installed during update.
Brian Harring35767822012-02-01 23:50:45 -0800238 early_enter_chroot build-docbook-catalog
239 # Configure basic stuff needed.
240 early_enter_chroot env-update
Brian Harringf539bc32012-02-06 00:18:37 -0800241 fi
242
Brian Harring35767822012-02-01 23:50:45 -0800243 # This is basically a sanity check of our chroot. If any of these
244 # don't exist, then either bind mounts have failed, an invocation
245 # from above is broke, or some assumption about the stage3 is no longer
246 # true.
247 early_enter_chroot ls -l /etc/make.{conf,profile} \
248 /usr/local/portage/chromiumos/profiles/default/linux/amd64/10.0
Brian Harringf539bc32012-02-06 00:18:37 -0800249
250 target="${FLAGS_chroot}/etc/profile.d"
251 sudo mkdir -p "${target}"
252 sudo_clobber "${target}/chromiumos-niceties.sh" << EOF
253# Niceties for interactive logins. (cr) denotes this is a chroot, the
254# __git_branch_ps1 prints current git branch in ./ . The $r behavior is to
255# make sure we don't reset the previous $? value which later formats in
256# $PS1 might rely on.
257PS1='\$(r=\$?; __git_branch_ps1 "(%s) "; exit \$r)'"\${PS1}"
258PS1="(cr) \${PS1}"
259EOF
260
261 # Select a small set of locales for the user if they haven't done so
262 # already. This makes glibc upgrades cheap by only generating a small
263 # set of locales. The ones listed here are basically for the buildbots
264 # which always assume these are available. This works in conjunction
265 # with `cros_sdk --enter`.
266 # http://crosbug.com/20378
267 local localegen="$FLAGS_chroot/etc/locale.gen"
268 if ! grep -q -v -e '^#' -e '^$' "${localegen}" ; then
269 sudo_append "${localegen}" <<EOF
270en_US ISO-8859-1
271en_US.UTF-8 UTF-8
272EOF
273 fi
274
275 # Add chromite as a local site-package.
276 mkdir -p "${FLAGS_chroot}/home/$USER/.local/lib/python2.6/site-packages"
277 ln -s ../../../../trunk/chromite \
278 "${FLAGS_chroot}/home/$USER/.local/lib/python2.6/site-packages/"
279
280 chmod a+x "$FLAGS_chroot/home/$USER/.bashrc"
Brian Harring35767822012-02-01 23:50:45 -0800281 # Automatically change to scripts directory.
Brian Harringf539bc32012-02-06 00:18:37 -0800282 echo 'cd ${CHROOT_CWD:-~/trunk/src/scripts}' \
283 >> "$FLAGS_chroot/home/$USER/.bash_profile"
284
Brian Harring35767822012-02-01 23:50:45 -0800285 # Enable bash completion for build scripts.
Brian Harringf539bc32012-02-06 00:18:37 -0800286 echo ". ~/trunk/src/scripts/bash_completion" \
287 >> "$FLAGS_chroot/home/$USER/.bashrc"
288
Brian Harring35767822012-02-01 23:50:45 -0800289 # Warn if attempting to use source control commands inside the chroot.
Brian Harringf539bc32012-02-06 00:18:37 -0800290 for NOUSE in svn gcl gclient
291 do
292 echo "alias $NOUSE='echo In the chroot, it is a bad idea to run $NOUSE'" \
293 >> "$FLAGS_chroot/home/$USER/.bash_profile"
294 done
295
296 if [[ "$USER" = "chrome-bot" ]]; then
297 # Copy ssh keys, so chroot'd chrome-bot can scp files from chrome-web.
298 cp -r ~/.ssh "$FLAGS_chroot/home/$USER/"
299 fi
300
301 if [[ -f $HOME/.gitconfig ]]; then
Brian Harring35767822012-02-01 23:50:45 -0800302 # Copy .gitconfig into chroot so repo and git can be used from inside.
303 # This is required for repo to work since it validates the email address.
Brian Harringf539bc32012-02-06 00:18:37 -0800304 echo "Copying ~/.gitconfig into chroot"
305 cp $HOME/.gitconfig "$FLAGS_chroot/home/$USER/"
306 fi
307}
308
Brian Harring35767822012-02-01 23:50:45 -0800309# Handle deleting an existing environment.
Brian Harringf539bc32012-02-06 00:18:37 -0800310if [[ $FLAGS_delete -eq $FLAGS_TRUE || \
Brian Harring35767822012-02-01 23:50:45 -0800311 $FLAGS_replace -eq $FLAGS_TRUE ]]; then
Brian Harringf539bc32012-02-06 00:18:37 -0800312 delete_existing
313 [[ $FLAGS_delete -eq $FLAGS_TRUE ]] && exit 0
314fi
315
316CHROOT_TRUNK="${CHROOT_TRUNK_DIR}"
317PORTAGE="${SRC_ROOT}/third_party/portage"
318OVERLAY="${SRC_ROOT}/third_party/chromiumos-overlay"
319CONFIG_DIR="${OVERLAY}/chromeos/config"
320CHROOT_CONFIG="${CHROOT_TRUNK}/src/third_party/chromiumos-overlay/chromeos/config"
321PORTAGE_STABLE_OVERLAY="/usr/local/portage/stable"
322CROSSDEV_OVERLAY="/usr/local/portage/crossdev"
323CHROOT_OVERLAY="/usr/local/portage/chromiumos"
324CHROOT_STATE="${FLAGS_chroot}/etc/debian_chroot"
Brian Harringf539bc32012-02-06 00:18:37 -0800325
326# Pass proxy variables into the environment.
327for type in http ftp all; do
328 value=$(env | grep ${type}_proxy || true)
329 if [ -n "${value}" ]; then
330 CHROOT_PASSTHRU+=("$value")
331 fi
332done
333
Brian Harring35767822012-02-01 23:50:45 -0800334# Create the base Gentoo stage3 based on last version put in chroot.
Brian Harringf539bc32012-02-06 00:18:37 -0800335STAGE3="${OVERLAY}/chromeos/stage3/stage3-amd64-${FLAGS_stage3_date}.tar.bz2"
336if [ -f $CHROOT_STATE ] && \
337 ! sudo egrep -q "^STAGE3=$STAGE3" $CHROOT_STATE >/dev/null 2>&1
338then
Brian Harring35767822012-02-01 23:50:45 -0800339 info "STAGE3 version has changed."
Brian Harringf539bc32012-02-06 00:18:37 -0800340 delete_existing
341fi
342
343if [ -n "${FLAGS_stage3_path}" ]; then
Brian Harring35767822012-02-01 23:50:45 -0800344 if [ ! -f "${FLAGS_stage3_path}" ]; then
Brian Harringf539bc32012-02-06 00:18:37 -0800345 error "Invalid stage3!"
346 exit 1;
347 fi
Brian Harring35767822012-02-01 23:50:45 -0800348 STAGE3="${FLAGS_stage3_path}"
Brian Harringf539bc32012-02-06 00:18:37 -0800349fi
350
Brian Harring35767822012-02-01 23:50:45 -0800351# Create the destination directory.
Brian Harringf539bc32012-02-06 00:18:37 -0800352mkdir -p "$FLAGS_chroot"
353
354echo
355if [ -f $CHROOT_STATE ]
356then
Brian Harring35767822012-02-01 23:50:45 -0800357 info "STAGE3 already set up. Skipping..."
Brian Harringf539bc32012-02-06 00:18:37 -0800358else
Brian Harring35767822012-02-01 23:50:45 -0800359 info "Unpacking STAGE3..."
Brian Harringf539bc32012-02-06 00:18:37 -0800360 sudo tar -xp -I $(type -p pbzip2 || echo bzip2) \
Brian Harring35767822012-02-01 23:50:45 -0800361 -C "${FLAGS_chroot}" -f "${STAGE3}"
362 sudo rm -f "$FLAGS_chroot/etc/"make.{globals,conf.user}
Brian Harringf539bc32012-02-06 00:18:37 -0800363fi
364
Brian Harring35767822012-02-01 23:50:45 -0800365# Set up users, if needed, before mkdir/mounts below.
Brian Harringf539bc32012-02-06 00:18:37 -0800366[ -f $CHROOT_STATE ] || init_users
367
368echo
Brian Harring35767822012-02-01 23:50:45 -0800369info "Setting up mounts..."
370# Set up necessary mounts and make sure we clean them up on exit.
371sudo mkdir -p "${FLAGS_chroot}/${CHROOT_TRUNK}" "${FLAGS_chroot}/run"
Brian Harringf539bc32012-02-06 00:18:37 -0800372PREBUILT_SETUP="$FLAGS_chroot/etc/make.conf.prebuilt_setup"
373if [[ -z "$IGNORE_PREFLIGHT_BINHOST" ]]; then
Brian Harring35767822012-02-01 23:50:45 -0800374 echo 'PORTAGE_BINHOST="$FULL_BINHOST"'
375fi | sudo_clobber "$PREBUILT_SETUP"
376
Brian Harringf539bc32012-02-06 00:18:37 -0800377sudo chmod 0644 "$PREBUILT_SETUP"
378
379# For bootstrapping from old wget, disable certificate checking. Once we've
380# upgraded to new curl (below), certificate checking is re-enabled. See
381# http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=409938
Brian Harring35767822012-02-01 23:50:45 -0800382sudo_clobber "${FLAGS_chroot}/etc/make.conf.fetchcommand_setup" <<'EOF'
Brian Harringf539bc32012-02-06 00:18:37 -0800383FETCHCOMMAND="/usr/bin/wget -t 5 -T 60 --no-check-certificate --passive-ftp -O \"\${DISTDIR}/\${FILE}\" \"\${URI}\""
384RESUMECOMMAND="/usr/bin/wget -c -t 5 -T 60 --no-check-certificate --passive-ftp -O \"\${DISTDIR}/\${FILE}\" \"\${URI}\""
385EOF
Brian Harring35767822012-02-01 23:50:45 -0800386
387sudo_clobber "${FLAGS_chroot}/etc/make.conf.host_setup" <<EOF
388# Created by make_chroot.
Brian Harringf539bc32012-02-06 00:18:37 -0800389source make.conf.prebuilt_setup
390source make.conf.fetchcommand_setup
391MAKEOPTS="-j${NUM_JOBS}"
392EOF
Brian Harring35767822012-02-01 23:50:45 -0800393
394sudo chmod 0644 "${FLAGS_chroot}"/etc/make.conf.host_setup \
395 "${FLAGS_chroot}"/etc/make.conf.fetchcommand_setup
Brian Harringf539bc32012-02-06 00:18:37 -0800396
397if ! [ -f "$CHROOT_STATE" ];then
398 INITIALIZE_CHROOT=1
399fi
400
401
402if [ -z "${INITIALIZE_CHROOT}" ];then
Brian Harring35767822012-02-01 23:50:45 -0800403 info "chroot already initialized. Skipping..."
Brian Harringf539bc32012-02-06 00:18:37 -0800404else
Brian Harring35767822012-02-01 23:50:45 -0800405 # Run all the init stuff to setup the env.
Brian Harringf539bc32012-02-06 00:18:37 -0800406 init_setup
407fi
408
Brian Harring35767822012-02-01 23:50:45 -0800409# Add file to indicate that it is a chroot.
410# Add version of $STAGE3 for update checks.
Brian Harringf539bc32012-02-06 00:18:37 -0800411sudo sh -c "echo STAGE3=$STAGE3 > $CHROOT_STATE"
412
Brian Harring35767822012-02-01 23:50:45 -0800413info "Updating portage"
414early_enter_chroot emerge -uNv portage
Brian Harringf539bc32012-02-06 00:18:37 -0800415
Brian Harring35767822012-02-01 23:50:45 -0800416info "Updating toolchain"
417early_enter_chroot emerge -uNv $USEPKG '>=sys-devel/gcc-4.4' sys-libs/glibc \
418 sys-devel/binutils sys-kernel/linux-headers
Brian Harringf539bc32012-02-06 00:18:37 -0800419
420# HACK: Select the latest toolchain. We're assuming that when this is
421# ran, the chroot has no experimental versions of new toolchains, just
422# one that is very old, and one that was just emerged.
Brian Harring35767822012-02-01 23:50:45 -0800423CHOST="$(early_enter_chroot portageq envvar CHOST)"
424LATEST="$(early_enter_chroot gcc-config -l | grep "${CHOST}" | tail -n1 | \
Brian Harringf539bc32012-02-06 00:18:37 -0800425 cut -f3 -d' ')"
Brian Harring35767822012-02-01 23:50:45 -0800426early_enter_chroot gcc-config "${LATEST}"
427early_enter_chroot emerge --unmerge "<sys-devel/gcc-${LATEST/${CHOST}-/}"
Brian Harringf539bc32012-02-06 00:18:37 -0800428
429# dhcpcd is included in 'world' by the stage3 that we pull in for some reason.
430# We have no need to install it in our host environment, so pull it out here.
Brian Harring35767822012-02-01 23:50:45 -0800431info "Deselecting dhcpcd"
432early_enter_chroot $EMERGE_CMD --deselect dhcpcd
Brian Harringf539bc32012-02-06 00:18:37 -0800433
Brian Harring35767822012-02-01 23:50:45 -0800434info "Running emerge ccache curl sudo ..."
435early_enter_chroot $EMERGE_CMD -uNv $USEPKG $EMERGE_JOS \
436 ccache net-misc/curl sudo
Brian Harringf539bc32012-02-06 00:18:37 -0800437
438# Curl is now installed, so we can depend on it now.
Brian Harring35767822012-02-01 23:50:45 -0800439sudo_clobber "${FLAGS_chroot}/etc/make.conf.fetchcommand_setup" <<'EOF'
Brian Harringf539bc32012-02-06 00:18:37 -0800440FETCHCOMMAND='curl -f -y 30 --retry 9 -L --output \${DISTDIR}/\${FILE} \${URI}'
441RESUMECOMMAND='curl -f -y 30 -C - --retry 9 -L --output \${DISTDIR}/\${FILE} \${URI}'
442EOF
443sudo chmod 0644 "${FLAGS_chroot}"/etc/make.conf.fetchcommand_setup
444
445if [ -n "${INITIALIZE_CHROOT}" ]; then
446 # If we're creating a new chroot, we also want to set it to the latest
447 # version.
Brian Harring35767822012-02-01 23:50:45 -0800448 enter_chroot \
449 "${CHROOT_TRUNK}/src/scripts/run_chroot_version_hooks" --force_latest
Brian Harringf539bc32012-02-06 00:18:37 -0800450fi
451
Brian Harring35767822012-02-01 23:50:45 -0800452# Update chroot.
453UPDATE_ARGS=()
Brian Harringf539bc32012-02-06 00:18:37 -0800454if [[ $FLAGS_usepkg -eq $FLAGS_TRUE ]]; then
Brian Harring35767822012-02-01 23:50:45 -0800455 UPDATE_ARGS+=( --usepkg )
Brian Harringf539bc32012-02-06 00:18:37 -0800456else
Brian Harring35767822012-02-01 23:50:45 -0800457 UPDATE_ARGS+=( --nousepkg )
Brian Harringf539bc32012-02-06 00:18:37 -0800458fi
459if [[ ${FLAGS_fast} -eq ${FLAGS_TRUE} ]]; then
Brian Harring35767822012-02-01 23:50:45 -0800460 UPDATE_ARGS+=( --fast )
Brian Harringf539bc32012-02-06 00:18:37 -0800461else
Brian Harring35767822012-02-01 23:50:45 -0800462 UPDATE_ARGS+=( --nofast )
Brian Harringf539bc32012-02-06 00:18:37 -0800463fi
Brian Harring35767822012-02-01 23:50:45 -0800464enter_chroot "${CHROOT_TRUNK}/src/scripts/update_chroot" "${UPDATE_ARGS[@]}"
Brian Harringf539bc32012-02-06 00:18:37 -0800465
Brian Harring35767822012-02-01 23:50:45 -0800466CHROOT_EXAMPLE_OPT=""
467if [[ "$FLAGS_chroot" != "$DEFAULT_CHROOT_DIR" ]]; then
Brian Harringf539bc32012-02-06 00:18:37 -0800468 CHROOT_EXAMPLE_OPT="--chroot=$FLAGS_chroot"
469fi
470
471print_time_elapsed
472
Brian Harring35767822012-02-01 23:50:45 -0800473cat <<EOF
474${CROS_LOG_PREFIX:-cros_sdk}: All set up. To enter the chroot, run:"
475${CROS_LOG_PREFIX:-cros_sdk}: $ cros_sdk --enter $CHROOT_EXAMPLE_OPT"
476
477CAUTION: Do *NOT* rm -rf the chroot directory; if there are stale bind
478mounts you may end up deleting your source tree too. To unmount and
479delete the chroot cleanly, use:
480$ cros_sdk --delete $CHROOT_EXAMPLE_OPT
481EOF