blob: 4d459d211acc4ad6a1f5e6531490e0a230eb96b4 [file] [log] [blame]
Brian Harringf539bc32012-02-06 00:18:37 -08001#!/bin/bash
2
Matt Tennant0a9d32d2012-07-30 16:51:37 -07003# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Brian Harringf539bc32012-02-06 00:18:37 -08004# 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
Zdenek Behan4d21a292012-08-17 04:02:29 +020019if [ -n "${USE}" ]; then
20 echo "$SCRIPT_NAME: Building with a non-empty USE: ${USE}"
21 echo "This modifies the expected behaviour and can fail."
22fi
23
Brian Harringf539bc32012-02-06 00:18:37 -080024# Check if the host machine architecture is supported.
25ARCHITECTURE="$(uname -m)"
26if [[ "$ARCHITECTURE" != "x86_64" ]]; then
27 echo "$SCRIPT_NAME: $ARCHITECTURE is not supported as a host machine architecture."
28 exit 1
29fi
30
Brian Harring35767822012-02-01 23:50:45 -080031# Script must be run outside the chroot.
Brian Harringf539bc32012-02-06 00:18:37 -080032assert_outside_chroot
33
Brian Harring35767822012-02-01 23:50:45 -080034# Define command line flags.
Brian Harringf539bc32012-02-06 00:18:37 -080035# See http://code.google.com/p/shflags/wiki/Documentation10x
36
37DEFINE_string chroot "$DEFAULT_CHROOT_DIR" \
38 "Destination dir for the chroot environment."
39DEFINE_boolean usepkg $FLAGS_TRUE "Use binary packages to bootstrap."
40DEFINE_boolean delete $FLAGS_FALSE "Delete an existing chroot."
41DEFINE_boolean replace $FLAGS_FALSE "Overwrite existing chroot, if any."
42DEFINE_integer jobs -1 "How many packages to build in parallel at maximum."
43DEFINE_boolean fast ${DEFAULT_FAST} "Call many emerges in parallel"
44DEFINE_string stage3_date "2010.03.09" \
45 "Use the stage3 with the given date."
46DEFINE_string stage3_path "" \
47 "Use the stage3 located on this path."
Brian Harringf539bc32012-02-06 00:18:37 -080048
Brian Harring35767822012-02-01 23:50:45 -080049# Parse command line flags.
Brian Harringf539bc32012-02-06 00:18:37 -080050FLAGS_HELP="usage: $SCRIPT_NAME [flags]"
51FLAGS "$@" || exit 1
52eval set -- "${FLAGS_ARGV}"
53check_flags_only_and_allow_null_arg "$@" && set --
54
Brian Harring35767822012-02-01 23:50:45 -080055CROS_LOG_PREFIX=cros_sdk
Brian Harringf539bc32012-02-06 00:18:37 -080056
57assert_not_root_user
58# Set the right umask for chroot creation.
59umask 022
60
61# Only now can we die on error. shflags functions leak non-zero error codes,
Brian Harring7f175a52012-03-02 05:37:00 -080062# so will die prematurely if 'switch_to_strict_mode' is specified before now.
Brian Harringf539bc32012-02-06 00:18:37 -080063# TODO: replace shflags with something less error-prone, or contribute a fix.
Brian Harring7f175a52012-03-02 05:37:00 -080064switch_to_strict_mode
Brian Harringf539bc32012-02-06 00:18:37 -080065
J. Richard Barnettee80f6de2012-02-24 14:08:34 -080066. "${SCRIPT_ROOT}"/sdk_lib/make_conf_util.sh
67
Brian Harringf539bc32012-02-06 00:18:37 -080068FULLNAME="ChromeOS Developer"
69DEFGROUPS="eng,adm,cdrom,floppy,audio,video,portage"
70PASSWORD=chronos
71CRYPTED_PASSWD=$(perl -e 'print crypt($ARGV[0], "foo")', $PASSWORD)
72
73USEPKG=""
74if [[ $FLAGS_usepkg -eq $FLAGS_TRUE ]]; then
75 # Use binary packages. Include all build-time dependencies,
76 # so as to avoid unnecessary differences between source
77 # and binary builds.
78 USEPKG="--getbinpkg --usepkg --with-bdeps y"
79fi
80
81# Support faster build if necessary.
82EMERGE_CMD="emerge"
83if [ "$FLAGS_fast" -eq "${FLAGS_TRUE}" ]; then
84 CHROOT_CHROMITE_DIR="/home/${USER}/trunk/chromite"
85 EMERGE_CMD="${CHROOT_CHROMITE_DIR}/bin/parallel_emerge"
86fi
87
Brian Harring35767822012-02-01 23:50:45 -080088ENTER_CHROOT_ARGS=(
89 CROS_WORKON_SRCROOT="$CHROOT_TRUNK"
90 PORTAGE_USERNAME="$USER"
91 IGNORE_PREFLIGHT_BINHOST="$IGNORE_PREFLIGHT_BINHOST"
92)
93
94# Invoke enter_chroot. This can only be used after sudo has been installed.
Mike Frysinger6b1abb22012-05-11 13:44:06 -040095enter_chroot() {
Brian Harring35767822012-02-01 23:50:45 -080096 "$ENTER_CHROOT" --chroot "$FLAGS_chroot" -- "${ENTER_CHROOT_ARGS[@]}" "$@"
Brian Harringf539bc32012-02-06 00:18:37 -080097}
98
Brian Harring35767822012-02-01 23:50:45 -080099# Invoke enter_chroot running the command as root, and w/out sudo.
100# This should be used prior to sudo being merged.
Mike Frysingerba758452012-04-02 13:28:31 -0400101early_env=()
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400102early_enter_chroot() {
Brian Harring35767822012-02-01 23:50:45 -0800103 "$ENTER_CHROOT" --chroot "$FLAGS_chroot" --early_make_chroot \
Mike Frysingerba758452012-04-02 13:28:31 -0400104 -- "${ENTER_CHROOT_ARGS[@]}" "${early_env[@]}" "$@"
Brian Harringf539bc32012-02-06 00:18:37 -0800105}
106
Brian Harring35767822012-02-01 23:50:45 -0800107# Run a command within the chroot. The main usage of this is to avoid
108# the overhead of enter_chroot, and do not need access to the source tree,
109# don't need the actual chroot profile env, and can run the command as root.
110sudo_chroot() {
111 sudo chroot "${FLAGS_chroot}" "$@"
Brian Harringf539bc32012-02-06 00:18:37 -0800112}
113
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400114cleanup() {
Brian Harringf539bc32012-02-06 00:18:37 -0800115 # Clean up mounts
116 safe_umount_tree "${FLAGS_chroot}"
117}
118
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400119delete_existing() {
Brian Harring35767822012-02-01 23:50:45 -0800120 # Delete old chroot dir.
121 if [[ ! -e "$FLAGS_chroot" ]]; then
122 return
Brian Harringf539bc32012-02-06 00:18:37 -0800123 fi
Brian Harring35767822012-02-01 23:50:45 -0800124 info "Cleaning up old mount points..."
125 cleanup
126 info "Deleting $FLAGS_chroot..."
127 sudo rm -rf "$FLAGS_chroot"
128 info "Done."
Brian Harringf539bc32012-02-06 00:18:37 -0800129}
130
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400131init_users () {
Brian Harring35767822012-02-01 23:50:45 -0800132 info "Set timezone..."
133 # date +%Z has trouble with daylight time, so use host's info.
134 sudo rm -f "${FLAGS_chroot}/etc/localtime"
Brian Harringf539bc32012-02-06 00:18:37 -0800135 if [ -f /etc/localtime ] ; then
136 sudo cp /etc/localtime "${FLAGS_chroot}/etc"
137 else
Brian Harring35767822012-02-01 23:50:45 -0800138 sudo ln -sf /usr/share/zoneinfo/PST8PDT "${FLAGS_chroot}/etc/localtime"
Brian Harringf539bc32012-02-06 00:18:37 -0800139 fi
Brian Harring35767822012-02-01 23:50:45 -0800140 info "Adding user/group..."
Brian Harringf539bc32012-02-06 00:18:37 -0800141 # Add ourselves as a user inside the chroot.
Brian Harring35767822012-02-01 23:50:45 -0800142 sudo_chroot groupadd -g 5000 eng
Brian Harringf539bc32012-02-06 00:18:37 -0800143 # We need the UID to match the host user's. This can conflict with
144 # a particular chroot UID. At the same time, the added user has to
145 # be a primary user for the given UID for sudo to work, which is
146 # determined by the order in /etc/passwd. Let's put ourselves on top
147 # of the file.
Brian Harring35767822012-02-01 23:50:45 -0800148 sudo_chroot useradd -o -G ${DEFGROUPS} -g eng -u `id -u` -s \
Brian Harringf539bc32012-02-06 00:18:37 -0800149 /bin/bash -m -c "${FULLNAME}" -p ${CRYPTED_PASSWD} ${USER}
150 # Because passwd generally isn't sorted and the entry ended up at the
151 # bottom, it is safe to just take it and move it to top instead.
Brian Harring35767822012-02-01 23:50:45 -0800152 sudo sed -e '1{h;d};$!{H;d};$G' -i "${FLAGS_chroot}/etc/passwd"
Brian Harringf539bc32012-02-06 00:18:37 -0800153}
154
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400155init_setup () {
Brian Harring35767822012-02-01 23:50:45 -0800156 info "Running init_setup()..."
157 sudo mkdir -p -m 755 "${FLAGS_chroot}/usr" \
158 "${FLAGS_chroot}/usr/local/portage" \
159 "${FLAGS_chroot}"/"${CROSSDEV_OVERLAY}"
Brian Harringf539bc32012-02-06 00:18:37 -0800160 sudo ln -sf "${CHROOT_TRUNK}/src/third_party/portage" \
161 "${FLAGS_chroot}/usr/portage"
Brian Harringf539bc32012-02-06 00:18:37 -0800162 sudo ln -sf "${CHROOT_TRUNK}/src/third_party/chromiumos-overlay" \
163 "${FLAGS_chroot}"/"${CHROOT_OVERLAY}"
164 sudo ln -sf "${CHROOT_TRUNK}/src/third_party/portage-stable" \
165 "${FLAGS_chroot}"/"${PORTAGE_STABLE_OVERLAY}"
Brian Harringf539bc32012-02-06 00:18:37 -0800166
Brian Harring35767822012-02-01 23:50:45 -0800167 # Some operations need an mtab.
168 sudo ln -s /proc/mounts "${FLAGS_chroot}/etc/mtab"
Brian Harringf539bc32012-02-06 00:18:37 -0800169
170 # Set up sudoers. Inside the chroot, the user can sudo without a password.
171 # (Safe enough, since the only way into the chroot is to 'sudo chroot', so
172 # the user's already typed in one sudo password...)
173 # Make sure the sudoers.d subdir exists as older stage3 base images lack it.
174 sudo mkdir -p "${FLAGS_chroot}/etc/sudoers.d"
175 sudo_clobber "${FLAGS_chroot}/etc/sudoers.d/90_cros" <<EOF
176Defaults env_keep += CROS_WORKON_SRCROOT
177Defaults env_keep += CHROMEOS_OFFICIAL
178Defaults env_keep += PORTAGE_USERNAME
179Defaults env_keep += http_proxy
180Defaults env_keep += ftp_proxy
181Defaults env_keep += all_proxy
182%adm ALL=(ALL) ALL
183root ALL=(ALL) ALL
184$USER ALL=NOPASSWD: ALL
185EOF
Brian Harring35767822012-02-01 23:50:45 -0800186 sudo find "${FLAGS_chroot}/etc/"sudoers* -type f -exec chmod 0440 {} +
187 # Fix bad group for some.
188 sudo chown -R root:root "${FLAGS_chroot}/etc/"sudoers*
Brian Harringf539bc32012-02-06 00:18:37 -0800189
Brian Harring35767822012-02-01 23:50:45 -0800190 info "Setting up hosts/resolv..."
191 # Copy config from outside chroot into chroot.
192 sudo cp /etc/{hosts,resolv.conf} "$FLAGS_chroot/etc/"
193 sudo chmod 0644 "$FLAGS_chroot"/etc/{hosts,resolv.conf}
Brian Harringf539bc32012-02-06 00:18:37 -0800194
195 # Setup host make.conf. This includes any overlay that we may be using
196 # and a pointer to pre-built packages.
Brian Harring35767822012-02-01 23:50:45 -0800197 # TODO: This should really be part of a profile in the portage.
198 info "Setting up /etc/make.*..."
Brian Harringf539bc32012-02-06 00:18:37 -0800199 sudo mv "${FLAGS_chroot}"/etc/make.conf{,.orig}
200 sudo ln -sf "${CHROOT_CONFIG}/make.conf.amd64-host" \
201 "${FLAGS_chroot}/etc/make.conf"
202 sudo mv "${FLAGS_chroot}"/etc/make.profile{,.orig}
203 sudo ln -sf "${CHROOT_OVERLAY}/profiles/default/linux/amd64/10.0" \
204 "${FLAGS_chroot}/etc/make.profile"
205
Brian Harring35767822012-02-01 23:50:45 -0800206 # Create make.conf.user .
Brian Harringf539bc32012-02-06 00:18:37 -0800207 sudo touch "${FLAGS_chroot}"/etc/make.conf.user
208 sudo chmod 0644 "${FLAGS_chroot}"/etc/make.conf.user
209
210 # Create directories referred to by our conf files.
Brian Harring36b102b2012-02-06 23:34:25 -0800211 sudo mkdir -p -m 775 "${FLAGS_chroot}/var/lib/portage/pkgs" \
212 "${FLAGS_chroot}/var/cache/distfiles" \
213 "${FLAGS_chroot}/var/cache/chromeos-chrome"
214
215 # Run this from w/in the chroot so we use whatever uid/gid
216 # these are defined as w/in the chroot.
217 sudo_chroot chown "${USER}:portage" /var/cache/chromeos-chrome
Brian Harring7ee892d2012-02-02 09:33:10 -0800218
219 # These are created for compatibility while transitioning
220 # make.conf and friends over to the new location.
221 # TODO(ferringb): remove this 03/12 or so.
222 sudo ln -s ../../cache/distfiles/host \
223 "${FLAGS_chroot}/var/lib/portage/distfiles"
224 sudo ln -s ../../cache/distfiles/target \
225 "${FLAGS_chroot}/var/lib/portage/distfiles-target"
Brian Harringf539bc32012-02-06 00:18:37 -0800226
Brian Harringf539bc32012-02-06 00:18:37 -0800227 # Add chromite/bin and depot_tools into the path globally; note that the
228 # chromite wrapper itself might also be found in depot_tools.
229 # We rely on 'env-update' getting called below.
230 target="${FLAGS_chroot}/etc/env.d/99chromiumos"
231 sudo_clobber "${target}" <<EOF
232PATH=/home/$USER/trunk/chromite/bin:/home/$USER/depot_tools
233CROS_WORKON_SRCROOT="${CHROOT_TRUNK}"
234PORTAGE_USERNAME=$USER
235EOF
236
237 # TODO(zbehan): Configure stuff that is usually configured in postinst's,
Mike Frysingereb1a9b42012-03-28 16:21:04 -0400238 # but wasn't. Fix the postinst's.
Brian Harring35767822012-02-01 23:50:45 -0800239 info "Running post-inst configuration hacks"
240 early_enter_chroot env-update
Brian Harringf539bc32012-02-06 00:18:37 -0800241
Brian Harring35767822012-02-01 23:50:45 -0800242 # This is basically a sanity check of our chroot. If any of these
243 # don't exist, then either bind mounts have failed, an invocation
244 # from above is broke, or some assumption about the stage3 is no longer
245 # true.
246 early_enter_chroot ls -l /etc/make.{conf,profile} \
247 /usr/local/portage/chromiumos/profiles/default/linux/amd64/10.0
Brian Harringf539bc32012-02-06 00:18:37 -0800248
249 target="${FLAGS_chroot}/etc/profile.d"
250 sudo mkdir -p "${target}"
251 sudo_clobber "${target}/chromiumos-niceties.sh" << EOF
252# Niceties for interactive logins. (cr) denotes this is a chroot, the
253# __git_branch_ps1 prints current git branch in ./ . The $r behavior is to
254# make sure we don't reset the previous $? value which later formats in
255# $PS1 might rely on.
256PS1='\$(r=\$?; __git_branch_ps1 "(%s) "; exit \$r)'"\${PS1}"
257PS1="(cr) \${PS1}"
258EOF
259
260 # Select a small set of locales for the user if they haven't done so
261 # already. This makes glibc upgrades cheap by only generating a small
262 # set of locales. The ones listed here are basically for the buildbots
263 # which always assume these are available. This works in conjunction
264 # with `cros_sdk --enter`.
265 # http://crosbug.com/20378
266 local localegen="$FLAGS_chroot/etc/locale.gen"
267 if ! grep -q -v -e '^#' -e '^$' "${localegen}" ; then
268 sudo_append "${localegen}" <<EOF
269en_US ISO-8859-1
270en_US.UTF-8 UTF-8
271EOF
272 fi
273
274 # Add chromite as a local site-package.
275 mkdir -p "${FLAGS_chroot}/home/$USER/.local/lib/python2.6/site-packages"
276 ln -s ../../../../trunk/chromite \
277 "${FLAGS_chroot}/home/$USER/.local/lib/python2.6/site-packages/"
278
279 chmod a+x "$FLAGS_chroot/home/$USER/.bashrc"
Brian Harring35767822012-02-01 23:50:45 -0800280 # Automatically change to scripts directory.
Brian Harringf539bc32012-02-06 00:18:37 -0800281 echo 'cd ${CHROOT_CWD:-~/trunk/src/scripts}' \
282 >> "$FLAGS_chroot/home/$USER/.bash_profile"
283
Brian Harring35767822012-02-01 23:50:45 -0800284 # Enable bash completion for build scripts.
Brian Harringf539bc32012-02-06 00:18:37 -0800285 echo ". ~/trunk/src/scripts/bash_completion" \
286 >> "$FLAGS_chroot/home/$USER/.bashrc"
287
Brian Harring35767822012-02-01 23:50:45 -0800288 # Warn if attempting to use source control commands inside the chroot.
Brian Harringf539bc32012-02-06 00:18:37 -0800289 for NOUSE in svn gcl gclient
290 do
291 echo "alias $NOUSE='echo In the chroot, it is a bad idea to run $NOUSE'" \
292 >> "$FLAGS_chroot/home/$USER/.bash_profile"
293 done
294
295 if [[ "$USER" = "chrome-bot" ]]; then
296 # Copy ssh keys, so chroot'd chrome-bot can scp files from chrome-web.
297 cp -r ~/.ssh "$FLAGS_chroot/home/$USER/"
298 fi
299
300 if [[ -f $HOME/.gitconfig ]]; then
Brian Harring35767822012-02-01 23:50:45 -0800301 # Copy .gitconfig into chroot so repo and git can be used from inside.
302 # This is required for repo to work since it validates the email address.
Brian Harringf539bc32012-02-06 00:18:37 -0800303 echo "Copying ~/.gitconfig into chroot"
304 cp $HOME/.gitconfig "$FLAGS_chroot/home/$USER/"
305 fi
Luigi Semenzato2443fdd2012-05-29 10:34:04 -0700306
307 if [[ -f $HOME/.cros_chroot_init ]]; then
308 /bin/bash $HOME/.cros_chroot_init "${FLAGS_chroot}"
309 fi
Brian Harringf539bc32012-02-06 00:18:37 -0800310}
311
Brian Harring35767822012-02-01 23:50:45 -0800312# Handle deleting an existing environment.
Brian Harringf539bc32012-02-06 00:18:37 -0800313if [[ $FLAGS_delete -eq $FLAGS_TRUE || \
Brian Harring35767822012-02-01 23:50:45 -0800314 $FLAGS_replace -eq $FLAGS_TRUE ]]; then
Brian Harringf539bc32012-02-06 00:18:37 -0800315 delete_existing
316 [[ $FLAGS_delete -eq $FLAGS_TRUE ]] && exit 0
317fi
318
319CHROOT_TRUNK="${CHROOT_TRUNK_DIR}"
320PORTAGE="${SRC_ROOT}/third_party/portage"
321OVERLAY="${SRC_ROOT}/third_party/chromiumos-overlay"
322CONFIG_DIR="${OVERLAY}/chromeos/config"
323CHROOT_CONFIG="${CHROOT_TRUNK}/src/third_party/chromiumos-overlay/chromeos/config"
324PORTAGE_STABLE_OVERLAY="/usr/local/portage/stable"
325CROSSDEV_OVERLAY="/usr/local/portage/crossdev"
326CHROOT_OVERLAY="/usr/local/portage/chromiumos"
327CHROOT_STATE="${FLAGS_chroot}/etc/debian_chroot"
Brian Harringf539bc32012-02-06 00:18:37 -0800328
329# Pass proxy variables into the environment.
330for type in http ftp all; do
331 value=$(env | grep ${type}_proxy || true)
332 if [ -n "${value}" ]; then
333 CHROOT_PASSTHRU+=("$value")
334 fi
335done
336
Brian Harring35767822012-02-01 23:50:45 -0800337# Create the base Gentoo stage3 based on last version put in chroot.
Brian Harringf539bc32012-02-06 00:18:37 -0800338STAGE3="${OVERLAY}/chromeos/stage3/stage3-amd64-${FLAGS_stage3_date}.tar.bz2"
339if [ -f $CHROOT_STATE ] && \
340 ! sudo egrep -q "^STAGE3=$STAGE3" $CHROOT_STATE >/dev/null 2>&1
341then
Brian Harring35767822012-02-01 23:50:45 -0800342 info "STAGE3 version has changed."
Brian Harringf539bc32012-02-06 00:18:37 -0800343 delete_existing
344fi
345
346if [ -n "${FLAGS_stage3_path}" ]; then
Brian Harring35767822012-02-01 23:50:45 -0800347 if [ ! -f "${FLAGS_stage3_path}" ]; then
Brian Harringf539bc32012-02-06 00:18:37 -0800348 error "Invalid stage3!"
349 exit 1;
350 fi
Brian Harring35767822012-02-01 23:50:45 -0800351 STAGE3="${FLAGS_stage3_path}"
Brian Harringf539bc32012-02-06 00:18:37 -0800352fi
353
Brian Harring35767822012-02-01 23:50:45 -0800354# Create the destination directory.
Brian Harringf539bc32012-02-06 00:18:37 -0800355mkdir -p "$FLAGS_chroot"
356
357echo
358if [ -f $CHROOT_STATE ]
359then
Brian Harring35767822012-02-01 23:50:45 -0800360 info "STAGE3 already set up. Skipping..."
Brian Harringf539bc32012-02-06 00:18:37 -0800361else
Brian Harring35767822012-02-01 23:50:45 -0800362 info "Unpacking STAGE3..."
Zdenek Behan074f9ef2012-05-30 01:23:59 +0200363 case ${STAGE3} in
364 *.tbz2|*.tar.bz2) DECOMPRESS=$(type -p pbzip2 || echo bzip2) ;;
365 *.tar.xz) DECOMPRESS="xz" ;;
366 *) die "Unknown tarball compression: ${STAGE3}";;
367 esac
368 ${DECOMPRESS} -dc "${STAGE3}" | \
Brian Harring021858a2012-05-26 19:33:01 -0700369 sudo tar -xp -C "${FLAGS_chroot}"
Brian Harring35767822012-02-01 23:50:45 -0800370 sudo rm -f "$FLAGS_chroot/etc/"make.{globals,conf.user}
Brian Harringf539bc32012-02-06 00:18:37 -0800371fi
372
Brian Harring35767822012-02-01 23:50:45 -0800373# Set up users, if needed, before mkdir/mounts below.
Brian Harringf539bc32012-02-06 00:18:37 -0800374[ -f $CHROOT_STATE ] || init_users
375
376echo
Brian Harring35767822012-02-01 23:50:45 -0800377info "Setting up mounts..."
378# Set up necessary mounts and make sure we clean them up on exit.
379sudo mkdir -p "${FLAGS_chroot}/${CHROOT_TRUNK}" "${FLAGS_chroot}/run"
Brian Harring35767822012-02-01 23:50:45 -0800380
J. Richard Barnettee80f6de2012-02-24 14:08:34 -0800381# Create a special /etc/make.conf.host_setup that we use to bootstrap
382# the chroot. The regular content for the file will be generated the
383# first time we invoke update_chroot (further down in this script).
384create_bootstrap_host_setup "${FLAGS_chroot}"
Brian Harringf539bc32012-02-06 00:18:37 -0800385
386if ! [ -f "$CHROOT_STATE" ];then
387 INITIALIZE_CHROOT=1
388fi
389
390
Mike Frysingerba758452012-04-02 13:28:31 -0400391if ! early_enter_chroot bash -c 'type -P pbzip2' >/dev/null ; then
392 # This chroot lacks pbzip2 early on, so we need to disable it.
393 early_env+=(
394 PORTAGE_BZIP2_COMMAND="bzip2"
395 PORTAGE_BUNZIP2_COMMAND="bunzip2"
396 )
397fi
398
Brian Harringf539bc32012-02-06 00:18:37 -0800399if [ -z "${INITIALIZE_CHROOT}" ];then
Brian Harring35767822012-02-01 23:50:45 -0800400 info "chroot already initialized. Skipping..."
Brian Harringf539bc32012-02-06 00:18:37 -0800401else
Brian Harring35767822012-02-01 23:50:45 -0800402 # Run all the init stuff to setup the env.
Brian Harringf539bc32012-02-06 00:18:37 -0800403 init_setup
404fi
405
Brian Harring35767822012-02-01 23:50:45 -0800406# Add file to indicate that it is a chroot.
407# Add version of $STAGE3 for update checks.
Brian Harringf539bc32012-02-06 00:18:37 -0800408sudo sh -c "echo STAGE3=$STAGE3 > $CHROOT_STATE"
409
Brian Harring35767822012-02-01 23:50:45 -0800410info "Updating portage"
Mike Frysinger66fd81f2012-02-28 13:13:20 -0500411early_enter_chroot emerge -uNv --quiet portage
Brian Harringf539bc32012-02-06 00:18:37 -0800412
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100413info "Updating host toolchain"
414early_enter_chroot emerge -uNv --quiet crossdev
415TOOLCHAIN_ARGS=( --deleteold )
416if [[ ${FLAGS_usepkg} -eq ${FLAGS_FALSE} ]]; then
417 TOOLCHAIN_ARGS+=( --nousepkg )
418fi
419# Note: early_enter_chroot executes as root.
420early_enter_chroot "${CHROOT_TRUNK}/chromite/bin/cros_setup_toolchains" \
421 --hostonly "${TOOLCHAIN_ARGS[@]}"
Brian Harringf539bc32012-02-06 00:18:37 -0800422
423# dhcpcd is included in 'world' by the stage3 that we pull in for some reason.
424# We have no need to install it in our host environment, so pull it out here.
Brian Harring35767822012-02-01 23:50:45 -0800425info "Deselecting dhcpcd"
426early_enter_chroot $EMERGE_CMD --deselect dhcpcd
Brian Harringf539bc32012-02-06 00:18:37 -0800427
Mike Frysinger279f1032012-05-17 15:54:31 -0400428info "Running emerge curl sudo ..."
Mike Frysinger650bf872012-02-27 11:05:26 -0500429early_enter_chroot $EMERGE_CMD -uNv $USEPKG --select $EMERGE_JOBS \
Mike Frysinger279f1032012-05-17 15:54:31 -0400430 pbzip2 net-misc/curl sudo
Brian Harringf539bc32012-02-06 00:18:37 -0800431
Brian Harringf539bc32012-02-06 00:18:37 -0800432if [ -n "${INITIALIZE_CHROOT}" ]; then
433 # If we're creating a new chroot, we also want to set it to the latest
434 # version.
Brian Harring35767822012-02-01 23:50:45 -0800435 enter_chroot \
436 "${CHROOT_TRUNK}/src/scripts/run_chroot_version_hooks" --force_latest
Brian Harringf539bc32012-02-06 00:18:37 -0800437fi
438
Brian Harring35767822012-02-01 23:50:45 -0800439# Update chroot.
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100440# Skip toolchain update because it already happened above, and the chroot is
441# not ready to emerge all cross toolchains.
442UPDATE_ARGS=( --skip_toolchain_update )
443if [[ ${FLAGS_usepkg} -eq ${FLAGS_TRUE} ]]; then
Brian Harring35767822012-02-01 23:50:45 -0800444 UPDATE_ARGS+=( --usepkg )
Brian Harringf539bc32012-02-06 00:18:37 -0800445else
Brian Harring35767822012-02-01 23:50:45 -0800446 UPDATE_ARGS+=( --nousepkg )
Brian Harringf539bc32012-02-06 00:18:37 -0800447fi
448if [[ ${FLAGS_fast} -eq ${FLAGS_TRUE} ]]; then
Brian Harring35767822012-02-01 23:50:45 -0800449 UPDATE_ARGS+=( --fast )
Brian Harringf539bc32012-02-06 00:18:37 -0800450else
Brian Harring35767822012-02-01 23:50:45 -0800451 UPDATE_ARGS+=( --nofast )
Brian Harringf539bc32012-02-06 00:18:37 -0800452fi
David James184e3902012-02-23 20:19:28 -0800453if [[ "${FLAGS_jobs}" -ne -1 ]]; then
454 UPDATE_ARGS+=( --jobs=${FLAGS_jobs} )
455fi
Brian Harring35767822012-02-01 23:50:45 -0800456enter_chroot "${CHROOT_TRUNK}/src/scripts/update_chroot" "${UPDATE_ARGS[@]}"
Brian Harringf539bc32012-02-06 00:18:37 -0800457
Brian Harring35767822012-02-01 23:50:45 -0800458CHROOT_EXAMPLE_OPT=""
459if [[ "$FLAGS_chroot" != "$DEFAULT_CHROOT_DIR" ]]; then
Brian Harringf539bc32012-02-06 00:18:37 -0800460 CHROOT_EXAMPLE_OPT="--chroot=$FLAGS_chroot"
461fi
462
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100463# As a final pass, build all desired cross-toolchains.
464info "Updating toolchains"
Zdenek Behan4d21a292012-08-17 04:02:29 +0200465enter_chroot sudo -E "${CHROOT_TRUNK}/chromite/bin/cros_setup_toolchains" \
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100466 "${TOOLCHAIN_ARGS[@]}"
467
Matt Tennant0a9d32d2012-07-30 16:51:37 -0700468command_completed
Brian Harringf539bc32012-02-06 00:18:37 -0800469
Brian Harring35767822012-02-01 23:50:45 -0800470cat <<EOF
Mike Frysingerbdc4fb12012-02-10 11:20:03 -0500471
472${CROS_LOG_PREFIX:-cros_sdk}: All set up. To enter the chroot, run:
473$ cros_sdk --enter $CHROOT_EXAMPLE_OPT
Brian Harring35767822012-02-01 23:50:45 -0800474
475CAUTION: Do *NOT* rm -rf the chroot directory; if there are stale bind
476mounts you may end up deleting your source tree too. To unmount and
477delete the chroot cleanly, use:
478$ cros_sdk --delete $CHROOT_EXAMPLE_OPT
Mike Frysingerbdc4fb12012-02-10 11:20:03 -0500479
Brian Harring35767822012-02-01 23:50:45 -0800480EOF