blob: 012cbb06d3f2f3d2d286c3ce2d7b13464b8f1936 [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,
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.
59set -e
60
61FULLNAME="ChromeOS Developer"
62DEFGROUPS="eng,adm,cdrom,floppy,audio,video,portage"
63PASSWORD=chronos
64CRYPTED_PASSWD=$(perl -e 'print crypt($ARGV[0], "foo")', $PASSWORD)
65
66USEPKG=""
67if [[ $FLAGS_usepkg -eq $FLAGS_TRUE ]]; then
68 # Use binary packages. Include all build-time dependencies,
69 # so as to avoid unnecessary differences between source
70 # and binary builds.
71 USEPKG="--getbinpkg --usepkg --with-bdeps y"
72fi
73
74# Support faster build if necessary.
75EMERGE_CMD="emerge"
76if [ "$FLAGS_fast" -eq "${FLAGS_TRUE}" ]; then
77 CHROOT_CHROMITE_DIR="/home/${USER}/trunk/chromite"
78 EMERGE_CMD="${CHROOT_CHROMITE_DIR}/bin/parallel_emerge"
79fi
80
Brian Harring35767822012-02-01 23:50:45 -080081ENTER_CHROOT_ARGS=(
82 CROS_WORKON_SRCROOT="$CHROOT_TRUNK"
83 PORTAGE_USERNAME="$USER"
84 IGNORE_PREFLIGHT_BINHOST="$IGNORE_PREFLIGHT_BINHOST"
85)
86
87# Invoke enter_chroot. This can only be used after sudo has been installed.
88function enter_chroot {
89 "$ENTER_CHROOT" --chroot "$FLAGS_chroot" -- "${ENTER_CHROOT_ARGS[@]}" "$@"
Brian Harringf539bc32012-02-06 00:18:37 -080090}
91
Brian Harring35767822012-02-01 23:50:45 -080092# Invoke enter_chroot running the command as root, and w/out sudo.
93# This should be used prior to sudo being merged.
94function early_enter_chroot() {
95 "$ENTER_CHROOT" --chroot "$FLAGS_chroot" --early_make_chroot \
96 -- "${ENTER_CHROOT_ARGS[@]}" "$@"
Brian Harringf539bc32012-02-06 00:18:37 -080097}
98
Brian Harring35767822012-02-01 23:50:45 -080099# Run a command within the chroot. The main usage of this is to avoid
100# the overhead of enter_chroot, and do not need access to the source tree,
101# don't need the actual chroot profile env, and can run the command as root.
102sudo_chroot() {
103 sudo chroot "${FLAGS_chroot}" "$@"
Brian Harringf539bc32012-02-06 00:18:37 -0800104}
105
106function cleanup {
107 # Clean up mounts
108 safe_umount_tree "${FLAGS_chroot}"
109}
110
111function delete_existing {
Brian Harring35767822012-02-01 23:50:45 -0800112 # Delete old chroot dir.
113 if [[ ! -e "$FLAGS_chroot" ]]; then
114 return
Brian Harringf539bc32012-02-06 00:18:37 -0800115 fi
Brian Harring35767822012-02-01 23:50:45 -0800116 info "Cleaning up old mount points..."
117 cleanup
118 info "Deleting $FLAGS_chroot..."
119 sudo rm -rf "$FLAGS_chroot"
120 info "Done."
Brian Harringf539bc32012-02-06 00:18:37 -0800121}
122
123function init_users () {
Brian Harring35767822012-02-01 23:50:45 -0800124 info "Set timezone..."
125 # date +%Z has trouble with daylight time, so use host's info.
126 sudo rm -f "${FLAGS_chroot}/etc/localtime"
Brian Harringf539bc32012-02-06 00:18:37 -0800127 if [ -f /etc/localtime ] ; then
128 sudo cp /etc/localtime "${FLAGS_chroot}/etc"
129 else
Brian Harring35767822012-02-01 23:50:45 -0800130 sudo ln -sf /usr/share/zoneinfo/PST8PDT "${FLAGS_chroot}/etc/localtime"
Brian Harringf539bc32012-02-06 00:18:37 -0800131 fi
Brian Harring35767822012-02-01 23:50:45 -0800132 info "Adding user/group..."
Brian Harringf539bc32012-02-06 00:18:37 -0800133 # Add ourselves as a user inside the chroot.
Brian Harring35767822012-02-01 23:50:45 -0800134 sudo_chroot groupadd -g 5000 eng
Brian Harringf539bc32012-02-06 00:18:37 -0800135 # We need the UID to match the host user's. This can conflict with
136 # a particular chroot UID. At the same time, the added user has to
137 # be a primary user for the given UID for sudo to work, which is
138 # determined by the order in /etc/passwd. Let's put ourselves on top
139 # of the file.
Brian Harring35767822012-02-01 23:50:45 -0800140 sudo_chroot useradd -o -G ${DEFGROUPS} -g eng -u `id -u` -s \
Brian Harringf539bc32012-02-06 00:18:37 -0800141 /bin/bash -m -c "${FULLNAME}" -p ${CRYPTED_PASSWD} ${USER}
142 # Because passwd generally isn't sorted and the entry ended up at the
143 # bottom, it is safe to just take it and move it to top instead.
Brian Harring35767822012-02-01 23:50:45 -0800144 sudo sed -e '1{h;d};$!{H;d};$G' -i "${FLAGS_chroot}/etc/passwd"
Brian Harringf539bc32012-02-06 00:18:37 -0800145}
146
147function init_setup () {
Brian Harring35767822012-02-01 23:50:45 -0800148 info "Running init_setup()..."
149 sudo mkdir -p -m 755 "${FLAGS_chroot}/usr" \
150 "${FLAGS_chroot}/usr/local/portage" \
151 "${FLAGS_chroot}"/"${CROSSDEV_OVERLAY}"
Brian Harringf539bc32012-02-06 00:18:37 -0800152 sudo ln -sf "${CHROOT_TRUNK}/src/third_party/portage" \
153 "${FLAGS_chroot}/usr/portage"
Brian Harringf539bc32012-02-06 00:18:37 -0800154 sudo ln -sf "${CHROOT_TRUNK}/src/third_party/chromiumos-overlay" \
155 "${FLAGS_chroot}"/"${CHROOT_OVERLAY}"
156 sudo ln -sf "${CHROOT_TRUNK}/src/third_party/portage-stable" \
157 "${FLAGS_chroot}"/"${PORTAGE_STABLE_OVERLAY}"
Brian Harringf539bc32012-02-06 00:18:37 -0800158
Brian Harring35767822012-02-01 23:50:45 -0800159 # Some operations need an mtab.
160 sudo ln -s /proc/mounts "${FLAGS_chroot}/etc/mtab"
Brian Harringf539bc32012-02-06 00:18:37 -0800161
162 # Set up sudoers. Inside the chroot, the user can sudo without a password.
163 # (Safe enough, since the only way into the chroot is to 'sudo chroot', so
164 # the user's already typed in one sudo password...)
165 # Make sure the sudoers.d subdir exists as older stage3 base images lack it.
166 sudo mkdir -p "${FLAGS_chroot}/etc/sudoers.d"
167 sudo_clobber "${FLAGS_chroot}/etc/sudoers.d/90_cros" <<EOF
168Defaults env_keep += CROS_WORKON_SRCROOT
169Defaults env_keep += CHROMEOS_OFFICIAL
170Defaults env_keep += PORTAGE_USERNAME
171Defaults env_keep += http_proxy
172Defaults env_keep += ftp_proxy
173Defaults env_keep += all_proxy
174%adm ALL=(ALL) ALL
175root ALL=(ALL) ALL
176$USER ALL=NOPASSWD: ALL
177EOF
Brian Harring35767822012-02-01 23:50:45 -0800178 sudo find "${FLAGS_chroot}/etc/"sudoers* -type f -exec chmod 0440 {} +
179 # Fix bad group for some.
180 sudo chown -R root:root "${FLAGS_chroot}/etc/"sudoers*
Brian Harringf539bc32012-02-06 00:18:37 -0800181
Brian Harring35767822012-02-01 23:50:45 -0800182 info "Setting up hosts/resolv..."
183 # Copy config from outside chroot into chroot.
184 sudo cp /etc/{hosts,resolv.conf} "$FLAGS_chroot/etc/"
185 sudo chmod 0644 "$FLAGS_chroot"/etc/{hosts,resolv.conf}
Brian Harringf539bc32012-02-06 00:18:37 -0800186
187 # Setup host make.conf. This includes any overlay that we may be using
188 # and a pointer to pre-built packages.
Brian Harring35767822012-02-01 23:50:45 -0800189 # TODO: This should really be part of a profile in the portage.
190 info "Setting up /etc/make.*..."
Brian Harringf539bc32012-02-06 00:18:37 -0800191 sudo mv "${FLAGS_chroot}"/etc/make.conf{,.orig}
192 sudo ln -sf "${CHROOT_CONFIG}/make.conf.amd64-host" \
193 "${FLAGS_chroot}/etc/make.conf"
194 sudo mv "${FLAGS_chroot}"/etc/make.profile{,.orig}
195 sudo ln -sf "${CHROOT_OVERLAY}/profiles/default/linux/amd64/10.0" \
196 "${FLAGS_chroot}/etc/make.profile"
197
Brian Harring35767822012-02-01 23:50:45 -0800198 # Create make.conf.user .
Brian Harringf539bc32012-02-06 00:18:37 -0800199 sudo touch "${FLAGS_chroot}"/etc/make.conf.user
200 sudo chmod 0644 "${FLAGS_chroot}"/etc/make.conf.user
201
202 # Create directories referred to by our conf files.
Brian Harring36b102b2012-02-06 23:34:25 -0800203 sudo mkdir -p -m 775 "${FLAGS_chroot}/var/lib/portage/pkgs" \
204 "${FLAGS_chroot}/var/cache/distfiles" \
205 "${FLAGS_chroot}/var/cache/chromeos-chrome"
206
207 # Run this from w/in the chroot so we use whatever uid/gid
208 # these are defined as w/in the chroot.
209 sudo_chroot chown "${USER}:portage" /var/cache/chromeos-chrome
Brian Harring7ee892d2012-02-02 09:33:10 -0800210
211 # These are created for compatibility while transitioning
212 # make.conf and friends over to the new location.
213 # TODO(ferringb): remove this 03/12 or so.
214 sudo ln -s ../../cache/distfiles/host \
215 "${FLAGS_chroot}/var/lib/portage/distfiles"
216 sudo ln -s ../../cache/distfiles/target \
217 "${FLAGS_chroot}/var/lib/portage/distfiles-target"
Brian Harringf539bc32012-02-06 00:18:37 -0800218
Brian Harringf539bc32012-02-06 00:18:37 -0800219 # Add chromite/bin and depot_tools into the path globally; note that the
220 # chromite wrapper itself might also be found in depot_tools.
221 # We rely on 'env-update' getting called below.
222 target="${FLAGS_chroot}/etc/env.d/99chromiumos"
223 sudo_clobber "${target}" <<EOF
224PATH=/home/$USER/trunk/chromite/bin:/home/$USER/depot_tools
225CROS_WORKON_SRCROOT="${CHROOT_TRUNK}"
226PORTAGE_USERNAME=$USER
227EOF
228
229 # TODO(zbehan): Configure stuff that is usually configured in postinst's,
230 # but wasn't. Fix the postinst's. crosbug.com/18036
Brian Harring35767822012-02-01 23:50:45 -0800231 info "Running post-inst configuration hacks"
232 early_enter_chroot env-update
Brian Harringf539bc32012-02-06 00:18:37 -0800233 if [ -f ${FLAGS_chroot}/usr/bin/build-docbook-catalog ]; then
234 # For too ancient chroots that didn't have build-docbook-catalog, this
235 # is not relevant, and will get installed during update.
Brian Harring35767822012-02-01 23:50:45 -0800236 early_enter_chroot build-docbook-catalog
237 # Configure basic stuff needed.
238 early_enter_chroot env-update
Brian Harringf539bc32012-02-06 00:18:37 -0800239 fi
240
Brian Harring35767822012-02-01 23:50:45 -0800241 # This is basically a sanity check of our chroot. If any of these
242 # don't exist, then either bind mounts have failed, an invocation
243 # from above is broke, or some assumption about the stage3 is no longer
244 # true.
245 early_enter_chroot ls -l /etc/make.{conf,profile} \
246 /usr/local/portage/chromiumos/profiles/default/linux/amd64/10.0
Brian Harringf539bc32012-02-06 00:18:37 -0800247
248 target="${FLAGS_chroot}/etc/profile.d"
249 sudo mkdir -p "${target}"
250 sudo_clobber "${target}/chromiumos-niceties.sh" << EOF
251# Niceties for interactive logins. (cr) denotes this is a chroot, the
252# __git_branch_ps1 prints current git branch in ./ . The $r behavior is to
253# make sure we don't reset the previous $? value which later formats in
254# $PS1 might rely on.
255PS1='\$(r=\$?; __git_branch_ps1 "(%s) "; exit \$r)'"\${PS1}"
256PS1="(cr) \${PS1}"
257EOF
258
259 # Select a small set of locales for the user if they haven't done so
260 # already. This makes glibc upgrades cheap by only generating a small
261 # set of locales. The ones listed here are basically for the buildbots
262 # which always assume these are available. This works in conjunction
263 # with `cros_sdk --enter`.
264 # http://crosbug.com/20378
265 local localegen="$FLAGS_chroot/etc/locale.gen"
266 if ! grep -q -v -e '^#' -e '^$' "${localegen}" ; then
267 sudo_append "${localegen}" <<EOF
268en_US ISO-8859-1
269en_US.UTF-8 UTF-8
270EOF
271 fi
272
273 # Add chromite as a local site-package.
274 mkdir -p "${FLAGS_chroot}/home/$USER/.local/lib/python2.6/site-packages"
275 ln -s ../../../../trunk/chromite \
276 "${FLAGS_chroot}/home/$USER/.local/lib/python2.6/site-packages/"
277
278 chmod a+x "$FLAGS_chroot/home/$USER/.bashrc"
Brian Harring35767822012-02-01 23:50:45 -0800279 # Automatically change to scripts directory.
Brian Harringf539bc32012-02-06 00:18:37 -0800280 echo 'cd ${CHROOT_CWD:-~/trunk/src/scripts}' \
281 >> "$FLAGS_chroot/home/$USER/.bash_profile"
282
Brian Harring35767822012-02-01 23:50:45 -0800283 # Enable bash completion for build scripts.
Brian Harringf539bc32012-02-06 00:18:37 -0800284 echo ". ~/trunk/src/scripts/bash_completion" \
285 >> "$FLAGS_chroot/home/$USER/.bashrc"
286
Brian Harring35767822012-02-01 23:50:45 -0800287 # Warn if attempting to use source control commands inside the chroot.
Brian Harringf539bc32012-02-06 00:18:37 -0800288 for NOUSE in svn gcl gclient
289 do
290 echo "alias $NOUSE='echo In the chroot, it is a bad idea to run $NOUSE'" \
291 >> "$FLAGS_chroot/home/$USER/.bash_profile"
292 done
293
294 if [[ "$USER" = "chrome-bot" ]]; then
295 # Copy ssh keys, so chroot'd chrome-bot can scp files from chrome-web.
296 cp -r ~/.ssh "$FLAGS_chroot/home/$USER/"
297 fi
298
299 if [[ -f $HOME/.gitconfig ]]; then
Brian Harring35767822012-02-01 23:50:45 -0800300 # Copy .gitconfig into chroot so repo and git can be used from inside.
301 # This is required for repo to work since it validates the email address.
Brian Harringf539bc32012-02-06 00:18:37 -0800302 echo "Copying ~/.gitconfig into chroot"
303 cp $HOME/.gitconfig "$FLAGS_chroot/home/$USER/"
304 fi
305}
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..."
Brian Harringf539bc32012-02-06 00:18:37 -0800358 sudo tar -xp -I $(type -p pbzip2 || echo bzip2) \
Brian Harring35767822012-02-01 23:50:45 -0800359 -C "${FLAGS_chroot}" -f "${STAGE3}"
360 sudo rm -f "$FLAGS_chroot/etc/"make.{globals,conf.user}
Brian Harringf539bc32012-02-06 00:18:37 -0800361fi
362
Brian Harring35767822012-02-01 23:50:45 -0800363# Set up users, if needed, before mkdir/mounts below.
Brian Harringf539bc32012-02-06 00:18:37 -0800364[ -f $CHROOT_STATE ] || init_users
365
366echo
Brian Harring35767822012-02-01 23:50:45 -0800367info "Setting up mounts..."
368# Set up necessary mounts and make sure we clean them up on exit.
369sudo mkdir -p "${FLAGS_chroot}/${CHROOT_TRUNK}" "${FLAGS_chroot}/run"
Brian Harringf539bc32012-02-06 00:18:37 -0800370PREBUILT_SETUP="$FLAGS_chroot/etc/make.conf.prebuilt_setup"
371if [[ -z "$IGNORE_PREFLIGHT_BINHOST" ]]; then
Brian Harring35767822012-02-01 23:50:45 -0800372 echo 'PORTAGE_BINHOST="$FULL_BINHOST"'
373fi | sudo_clobber "$PREBUILT_SETUP"
374
Brian Harringf539bc32012-02-06 00:18:37 -0800375sudo chmod 0644 "$PREBUILT_SETUP"
376
377# For bootstrapping from old wget, disable certificate checking. Once we've
378# upgraded to new curl (below), certificate checking is re-enabled. See
379# http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=409938
Brian Harring35767822012-02-01 23:50:45 -0800380sudo_clobber "${FLAGS_chroot}/etc/make.conf.fetchcommand_setup" <<'EOF'
Brian Harringf539bc32012-02-06 00:18:37 -0800381FETCHCOMMAND="/usr/bin/wget -t 5 -T 60 --no-check-certificate --passive-ftp -O \"\${DISTDIR}/\${FILE}\" \"\${URI}\""
382RESUMECOMMAND="/usr/bin/wget -c -t 5 -T 60 --no-check-certificate --passive-ftp -O \"\${DISTDIR}/\${FILE}\" \"\${URI}\""
383EOF
Brian Harring35767822012-02-01 23:50:45 -0800384
385sudo_clobber "${FLAGS_chroot}/etc/make.conf.host_setup" <<EOF
386# Created by make_chroot.
Brian Harringf539bc32012-02-06 00:18:37 -0800387source make.conf.prebuilt_setup
388source make.conf.fetchcommand_setup
389MAKEOPTS="-j${NUM_JOBS}"
390EOF
Brian Harring35767822012-02-01 23:50:45 -0800391
392sudo chmod 0644 "${FLAGS_chroot}"/etc/make.conf.host_setup \
393 "${FLAGS_chroot}"/etc/make.conf.fetchcommand_setup
Brian Harringf539bc32012-02-06 00:18:37 -0800394
395if ! [ -f "$CHROOT_STATE" ];then
396 INITIALIZE_CHROOT=1
397fi
398
399
400if [ -z "${INITIALIZE_CHROOT}" ];then
Brian Harring35767822012-02-01 23:50:45 -0800401 info "chroot already initialized. Skipping..."
Brian Harringf539bc32012-02-06 00:18:37 -0800402else
Brian Harring35767822012-02-01 23:50:45 -0800403 # Run all the init stuff to setup the env.
Brian Harringf539bc32012-02-06 00:18:37 -0800404 init_setup
405fi
406
Brian Harring35767822012-02-01 23:50:45 -0800407# Add file to indicate that it is a chroot.
408# Add version of $STAGE3 for update checks.
Brian Harringf539bc32012-02-06 00:18:37 -0800409sudo sh -c "echo STAGE3=$STAGE3 > $CHROOT_STATE"
410
Brian Harring35767822012-02-01 23:50:45 -0800411info "Updating portage"
412early_enter_chroot emerge -uNv portage
Brian Harringf539bc32012-02-06 00:18:37 -0800413
Brian Harring35767822012-02-01 23:50:45 -0800414info "Updating toolchain"
415early_enter_chroot emerge -uNv $USEPKG '>=sys-devel/gcc-4.4' sys-libs/glibc \
416 sys-devel/binutils sys-kernel/linux-headers
Brian Harringf539bc32012-02-06 00:18:37 -0800417
418# HACK: Select the latest toolchain. We're assuming that when this is
419# ran, the chroot has no experimental versions of new toolchains, just
420# one that is very old, and one that was just emerged.
Ahmad Sharif211259a2012-02-08 19:45:57 -0800421GCC_ATOM="$(early_enter_chroot portageq best_version / sys-devel/gcc)"
422early_enter_chroot emerge --unmerge "<${GCC_ATOM}"
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}"
Brian Harringf539bc32012-02-06 00:18:37 -0800427
428# dhcpcd is included in 'world' by the stage3 that we pull in for some reason.
429# We have no need to install it in our host environment, so pull it out here.
Brian Harring35767822012-02-01 23:50:45 -0800430info "Deselecting dhcpcd"
431early_enter_chroot $EMERGE_CMD --deselect dhcpcd
Brian Harringf539bc32012-02-06 00:18:37 -0800432
Brian Harring35767822012-02-01 23:50:45 -0800433info "Running emerge ccache curl sudo ..."
434early_enter_chroot $EMERGE_CMD -uNv $USEPKG $EMERGE_JOS \
435 ccache net-misc/curl sudo
Brian Harringf539bc32012-02-06 00:18:37 -0800436
437# Curl is now installed, so we can depend on it now.
Brian Harring35767822012-02-01 23:50:45 -0800438sudo_clobber "${FLAGS_chroot}/etc/make.conf.fetchcommand_setup" <<'EOF'
Brian Harringf539bc32012-02-06 00:18:37 -0800439FETCHCOMMAND='curl -f -y 30 --retry 9 -L --output \${DISTDIR}/\${FILE} \${URI}'
440RESUMECOMMAND='curl -f -y 30 -C - --retry 9 -L --output \${DISTDIR}/\${FILE} \${URI}'
441EOF
442sudo chmod 0644 "${FLAGS_chroot}"/etc/make.conf.fetchcommand_setup
443
444if [ -n "${INITIALIZE_CHROOT}" ]; then
445 # If we're creating a new chroot, we also want to set it to the latest
446 # version.
Brian Harring35767822012-02-01 23:50:45 -0800447 enter_chroot \
448 "${CHROOT_TRUNK}/src/scripts/run_chroot_version_hooks" --force_latest
Brian Harringf539bc32012-02-06 00:18:37 -0800449fi
450
Brian Harring35767822012-02-01 23:50:45 -0800451# Update chroot.
452UPDATE_ARGS=()
Brian Harringf539bc32012-02-06 00:18:37 -0800453if [[ $FLAGS_usepkg -eq $FLAGS_TRUE ]]; then
Brian Harring35767822012-02-01 23:50:45 -0800454 UPDATE_ARGS+=( --usepkg )
Brian Harringf539bc32012-02-06 00:18:37 -0800455else
Brian Harring35767822012-02-01 23:50:45 -0800456 UPDATE_ARGS+=( --nousepkg )
Brian Harringf539bc32012-02-06 00:18:37 -0800457fi
458if [[ ${FLAGS_fast} -eq ${FLAGS_TRUE} ]]; then
Brian Harring35767822012-02-01 23:50:45 -0800459 UPDATE_ARGS+=( --fast )
Brian Harringf539bc32012-02-06 00:18:37 -0800460else
Brian Harring35767822012-02-01 23:50:45 -0800461 UPDATE_ARGS+=( --nofast )
Brian Harringf539bc32012-02-06 00:18:37 -0800462fi
David James184e3902012-02-23 20:19:28 -0800463if [[ "${FLAGS_jobs}" -ne -1 ]]; then
464 UPDATE_ARGS+=( --jobs=${FLAGS_jobs} )
465fi
Brian Harring35767822012-02-01 23:50:45 -0800466enter_chroot "${CHROOT_TRUNK}/src/scripts/update_chroot" "${UPDATE_ARGS[@]}"
Brian Harringf539bc32012-02-06 00:18:37 -0800467
Brian Harring35767822012-02-01 23:50:45 -0800468CHROOT_EXAMPLE_OPT=""
469if [[ "$FLAGS_chroot" != "$DEFAULT_CHROOT_DIR" ]]; then
Brian Harringf539bc32012-02-06 00:18:37 -0800470 CHROOT_EXAMPLE_OPT="--chroot=$FLAGS_chroot"
471fi
472
473print_time_elapsed
474
Brian Harring35767822012-02-01 23:50:45 -0800475cat <<EOF
Mike Frysingerbdc4fb12012-02-10 11:20:03 -0500476
477${CROS_LOG_PREFIX:-cros_sdk}: All set up. To enter the chroot, run:
478$ cros_sdk --enter $CHROOT_EXAMPLE_OPT
Brian Harring35767822012-02-01 23:50:45 -0800479
480CAUTION: Do *NOT* rm -rf the chroot directory; if there are stale bind
481mounts you may end up deleting your source tree too. To unmount and
482delete the chroot cleanly, use:
483$ cros_sdk --delete $CHROOT_EXAMPLE_OPT
Mike Frysingerbdc4fb12012-02-10 11:20:03 -0500484
Brian Harring35767822012-02-01 23:50:45 -0800485EOF