blob: 44933092ca1786604accdfb79fb7fdfef1cc3487 [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 Harring7b6f3772012-09-23 14:01:13 -070048DEFINE_string cache_dir "" "Directory to store caches within."
Brian Harringf539bc32012-02-06 00:18:37 -080049
Brian Harring35767822012-02-01 23:50:45 -080050# Parse command line flags.
Brian Harringf539bc32012-02-06 00:18:37 -080051FLAGS_HELP="usage: $SCRIPT_NAME [flags]"
52FLAGS "$@" || exit 1
53eval set -- "${FLAGS_ARGV}"
54check_flags_only_and_allow_null_arg "$@" && set --
55
Peter Mayo4411efe2012-09-21 04:41:27 -040056CROS_LOG_PREFIX=cros_sdk:make_chroot
Brian Harringf539bc32012-02-06 00:18:37 -080057
58assert_not_root_user
59# Set the right umask for chroot creation.
60umask 022
61
62# Only now can we die on error. shflags functions leak non-zero error codes,
Brian Harring7f175a52012-03-02 05:37:00 -080063# so will die prematurely if 'switch_to_strict_mode' is specified before now.
Brian Harringf539bc32012-02-06 00:18:37 -080064# TODO: replace shflags with something less error-prone, or contribute a fix.
Brian Harring7f175a52012-03-02 05:37:00 -080065switch_to_strict_mode
Brian Harringf539bc32012-02-06 00:18:37 -080066
Brian Harring7b6f3772012-09-23 14:01:13 -070067[[ "${FLAGS_delete}" == "${FLAGS_FALSE}" ]] && \
68 [[ -z "${FLAGS_cache_dir}" ]] && \
69 die "--cache_dir is required"
70
J. Richard Barnettee80f6de2012-02-24 14:08:34 -080071. "${SCRIPT_ROOT}"/sdk_lib/make_conf_util.sh
72
Brian Harringf539bc32012-02-06 00:18:37 -080073FULLNAME="ChromeOS Developer"
74DEFGROUPS="eng,adm,cdrom,floppy,audio,video,portage"
75PASSWORD=chronos
76CRYPTED_PASSWD=$(perl -e 'print crypt($ARGV[0], "foo")', $PASSWORD)
77
78USEPKG=""
79if [[ $FLAGS_usepkg -eq $FLAGS_TRUE ]]; then
80 # Use binary packages. Include all build-time dependencies,
81 # so as to avoid unnecessary differences between source
82 # and binary builds.
83 USEPKG="--getbinpkg --usepkg --with-bdeps y"
84fi
85
86# Support faster build if necessary.
87EMERGE_CMD="emerge"
88if [ "$FLAGS_fast" -eq "${FLAGS_TRUE}" ]; then
89 CHROOT_CHROMITE_DIR="/home/${USER}/trunk/chromite"
90 EMERGE_CMD="${CHROOT_CHROMITE_DIR}/bin/parallel_emerge"
91fi
92
Brian Harring35767822012-02-01 23:50:45 -080093ENTER_CHROOT_ARGS=(
94 CROS_WORKON_SRCROOT="$CHROOT_TRUNK"
95 PORTAGE_USERNAME="$USER"
96 IGNORE_PREFLIGHT_BINHOST="$IGNORE_PREFLIGHT_BINHOST"
97)
98
99# Invoke enter_chroot. This can only be used after sudo has been installed.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400100enter_chroot() {
Brian Harring7b6f3772012-09-23 14:01:13 -0700101 "$ENTER_CHROOT" --cache_dir "${FLAGS_cache_dir}" --chroot "$FLAGS_chroot" \
102 -- "${ENTER_CHROOT_ARGS[@]}" "$@"
Brian Harringf539bc32012-02-06 00:18:37 -0800103}
104
Brian Harring35767822012-02-01 23:50:45 -0800105# Invoke enter_chroot running the command as root, and w/out sudo.
106# This should be used prior to sudo being merged.
Mike Frysingerba758452012-04-02 13:28:31 -0400107early_env=()
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400108early_enter_chroot() {
Brian Harring35767822012-02-01 23:50:45 -0800109 "$ENTER_CHROOT" --chroot "$FLAGS_chroot" --early_make_chroot \
Brian Harring7b6f3772012-09-23 14:01:13 -0700110 --cache_dir "${FLAGS_cache_dir}" \
Mike Frysingerba758452012-04-02 13:28:31 -0400111 -- "${ENTER_CHROOT_ARGS[@]}" "${early_env[@]}" "$@"
Brian Harringf539bc32012-02-06 00:18:37 -0800112}
113
Brian Harring35767822012-02-01 23:50:45 -0800114# Run a command within the chroot. The main usage of this is to avoid
115# the overhead of enter_chroot, and do not need access to the source tree,
116# don't need the actual chroot profile env, and can run the command as root.
117sudo_chroot() {
118 sudo chroot "${FLAGS_chroot}" "$@"
Brian Harringf539bc32012-02-06 00:18:37 -0800119}
120
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400121cleanup() {
Brian Harringf539bc32012-02-06 00:18:37 -0800122 # Clean up mounts
123 safe_umount_tree "${FLAGS_chroot}"
124}
125
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400126delete_existing() {
Brian Harring35767822012-02-01 23:50:45 -0800127 # Delete old chroot dir.
128 if [[ ! -e "$FLAGS_chroot" ]]; then
129 return
Brian Harringf539bc32012-02-06 00:18:37 -0800130 fi
Brian Harring35767822012-02-01 23:50:45 -0800131 info "Cleaning up old mount points..."
132 cleanup
133 info "Deleting $FLAGS_chroot..."
134 sudo rm -rf "$FLAGS_chroot"
135 info "Done."
Brian Harringf539bc32012-02-06 00:18:37 -0800136}
137
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400138init_users () {
Brian Harring35767822012-02-01 23:50:45 -0800139 info "Set timezone..."
140 # date +%Z has trouble with daylight time, so use host's info.
141 sudo rm -f "${FLAGS_chroot}/etc/localtime"
Brian Harringf539bc32012-02-06 00:18:37 -0800142 if [ -f /etc/localtime ] ; then
143 sudo cp /etc/localtime "${FLAGS_chroot}/etc"
144 else
Brian Harring35767822012-02-01 23:50:45 -0800145 sudo ln -sf /usr/share/zoneinfo/PST8PDT "${FLAGS_chroot}/etc/localtime"
Brian Harringf539bc32012-02-06 00:18:37 -0800146 fi
Brian Harring35767822012-02-01 23:50:45 -0800147 info "Adding user/group..."
Brian Harringf539bc32012-02-06 00:18:37 -0800148 # Add ourselves as a user inside the chroot.
Brian Harring35767822012-02-01 23:50:45 -0800149 sudo_chroot groupadd -g 5000 eng
Brian Harringf539bc32012-02-06 00:18:37 -0800150 # We need the UID to match the host user's. This can conflict with
151 # a particular chroot UID. At the same time, the added user has to
152 # be a primary user for the given UID for sudo to work, which is
153 # determined by the order in /etc/passwd. Let's put ourselves on top
154 # of the file.
Brian Harring35767822012-02-01 23:50:45 -0800155 sudo_chroot useradd -o -G ${DEFGROUPS} -g eng -u `id -u` -s \
Brian Harringf539bc32012-02-06 00:18:37 -0800156 /bin/bash -m -c "${FULLNAME}" -p ${CRYPTED_PASSWD} ${USER}
157 # Because passwd generally isn't sorted and the entry ended up at the
158 # bottom, it is safe to just take it and move it to top instead.
Brian Harring35767822012-02-01 23:50:45 -0800159 sudo sed -e '1{h;d};$!{H;d};$G' -i "${FLAGS_chroot}/etc/passwd"
Brian Harringf539bc32012-02-06 00:18:37 -0800160}
161
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400162init_setup () {
Brian Harring35767822012-02-01 23:50:45 -0800163 info "Running init_setup()..."
164 sudo mkdir -p -m 755 "${FLAGS_chroot}/usr" \
165 "${FLAGS_chroot}/usr/local/portage" \
166 "${FLAGS_chroot}"/"${CROSSDEV_OVERLAY}"
Brian Harringf539bc32012-02-06 00:18:37 -0800167 sudo ln -sf "${CHROOT_TRUNK}/src/third_party/portage" \
168 "${FLAGS_chroot}/usr/portage"
Brian Harringf539bc32012-02-06 00:18:37 -0800169 sudo ln -sf "${CHROOT_TRUNK}/src/third_party/chromiumos-overlay" \
170 "${FLAGS_chroot}"/"${CHROOT_OVERLAY}"
171 sudo ln -sf "${CHROOT_TRUNK}/src/third_party/portage-stable" \
172 "${FLAGS_chroot}"/"${PORTAGE_STABLE_OVERLAY}"
Brian Harringf539bc32012-02-06 00:18:37 -0800173
Brian Harring35767822012-02-01 23:50:45 -0800174 # Some operations need an mtab.
175 sudo ln -s /proc/mounts "${FLAGS_chroot}/etc/mtab"
Brian Harringf539bc32012-02-06 00:18:37 -0800176
177 # Set up sudoers. Inside the chroot, the user can sudo without a password.
178 # (Safe enough, since the only way into the chroot is to 'sudo chroot', so
179 # the user's already typed in one sudo password...)
180 # Make sure the sudoers.d subdir exists as older stage3 base images lack it.
181 sudo mkdir -p "${FLAGS_chroot}/etc/sudoers.d"
Brian Harring06d3c2e2012-08-23 07:35:43 -0700182
183 # Use the standardized upgrade script to setup proxied vars.
184 sudo bash -e "${SCRIPT_ROOT}/chroot_version_hooks.d/45_rewrite_sudoers.d" \
185 "${FLAGS_chroot}" "${USER}" "${ENVIRONMENT_WHITELIST[@]}"
186
Brian Harringf264b822012-09-01 01:39:03 -0700187 # Turn on the path overrides; subshelled to protect our env from whatever
188 # vars the scriptlet may bleed.
189 ( CROS_CHROOT="${FLAGS_chroot}"
190 . "${SCRIPT_ROOT}/chroot_version_hooks.d/47_path_overrides" )
191
Brian Harring35767822012-02-01 23:50:45 -0800192 sudo find "${FLAGS_chroot}/etc/"sudoers* -type f -exec chmod 0440 {} +
193 # Fix bad group for some.
194 sudo chown -R root:root "${FLAGS_chroot}/etc/"sudoers*
Brian Harringf539bc32012-02-06 00:18:37 -0800195
Brian Harring35767822012-02-01 23:50:45 -0800196 info "Setting up hosts/resolv..."
197 # Copy config from outside chroot into chroot.
198 sudo cp /etc/{hosts,resolv.conf} "$FLAGS_chroot/etc/"
199 sudo chmod 0644 "$FLAGS_chroot"/etc/{hosts,resolv.conf}
Brian Harringf539bc32012-02-06 00:18:37 -0800200
201 # Setup host make.conf. This includes any overlay that we may be using
202 # and a pointer to pre-built packages.
Brian Harring35767822012-02-01 23:50:45 -0800203 # TODO: This should really be part of a profile in the portage.
204 info "Setting up /etc/make.*..."
Brian Harringf539bc32012-02-06 00:18:37 -0800205 sudo mv "${FLAGS_chroot}"/etc/make.conf{,.orig}
206 sudo ln -sf "${CHROOT_CONFIG}/make.conf.amd64-host" \
207 "${FLAGS_chroot}/etc/make.conf"
208 sudo mv "${FLAGS_chroot}"/etc/make.profile{,.orig}
209 sudo ln -sf "${CHROOT_OVERLAY}/profiles/default/linux/amd64/10.0" \
210 "${FLAGS_chroot}/etc/make.profile"
211
Brian Harring35767822012-02-01 23:50:45 -0800212 # Create make.conf.user .
Brian Harringf539bc32012-02-06 00:18:37 -0800213 sudo touch "${FLAGS_chroot}"/etc/make.conf.user
214 sudo chmod 0644 "${FLAGS_chroot}"/etc/make.conf.user
215
216 # Create directories referred to by our conf files.
Brian Harring36b102b2012-02-06 23:34:25 -0800217 sudo mkdir -p -m 775 "${FLAGS_chroot}/var/lib/portage/pkgs" \
Brian Harring7b6f3772012-09-23 14:01:13 -0700218 "${FLAGS_chroot}/var/cache/"chromeos-{cache,chrome} \
219 "${FLAGS_chroot}/etc/profile.d"
220
221 echo "export CHROMEOS_CACHEDIR=/var/cache/chromeos-cache" | \
222 sudo_clobber "${FLAGS_chroot}/etc/profile.d/chromeos-cachedir.sh"
223 sudo_multi \
224 "chmod 0644 '${FLAGS_chroot}/etc/profile.d/chromeos-cachedir.sh'" \
225 "rm -rf '${FLAGS_chroot}/var/cache/distfiles'" \
226 "ln -s chromeos-cache/distfiles '${FLAGS_chroot}/var/cache/distfiles'"
Brian Harring36b102b2012-02-06 23:34:25 -0800227
228 # Run this from w/in the chroot so we use whatever uid/gid
229 # these are defined as w/in the chroot.
230 sudo_chroot chown "${USER}:portage" /var/cache/chromeos-chrome
Brian Harring7ee892d2012-02-02 09:33:10 -0800231
232 # These are created for compatibility while transitioning
233 # make.conf and friends over to the new location.
Brian Harring7b6f3772012-09-23 14:01:13 -0700234 # TODO(ferringb): remove this 01/13 or so.
235 sudo ln -s ../../cache/chromeos-cache/distfiles/host \
Brian Harring7ee892d2012-02-02 09:33:10 -0800236 "${FLAGS_chroot}/var/lib/portage/distfiles"
Brian Harring7b6f3772012-09-23 14:01:13 -0700237 sudo ln -s ../../cache/chromeos-cache/distfiles/target \
Brian Harring7ee892d2012-02-02 09:33:10 -0800238 "${FLAGS_chroot}/var/lib/portage/distfiles-target"
Brian Harringf539bc32012-02-06 00:18:37 -0800239
Brian Harringf539bc32012-02-06 00:18:37 -0800240 # Add chromite/bin and depot_tools into the path globally; note that the
241 # chromite wrapper itself might also be found in depot_tools.
242 # We rely on 'env-update' getting called below.
243 target="${FLAGS_chroot}/etc/env.d/99chromiumos"
244 sudo_clobber "${target}" <<EOF
245PATH=/home/$USER/trunk/chromite/bin:/home/$USER/depot_tools
246CROS_WORKON_SRCROOT="${CHROOT_TRUNK}"
247PORTAGE_USERNAME=$USER
248EOF
249
250 # TODO(zbehan): Configure stuff that is usually configured in postinst's,
Mike Frysingereb1a9b42012-03-28 16:21:04 -0400251 # but wasn't. Fix the postinst's.
Brian Harring35767822012-02-01 23:50:45 -0800252 info "Running post-inst configuration hacks"
253 early_enter_chroot env-update
Brian Harringf539bc32012-02-06 00:18:37 -0800254
Brian Harring35767822012-02-01 23:50:45 -0800255 # This is basically a sanity check of our chroot. If any of these
256 # don't exist, then either bind mounts have failed, an invocation
257 # from above is broke, or some assumption about the stage3 is no longer
258 # true.
259 early_enter_chroot ls -l /etc/make.{conf,profile} \
260 /usr/local/portage/chromiumos/profiles/default/linux/amd64/10.0
Brian Harringf539bc32012-02-06 00:18:37 -0800261
262 target="${FLAGS_chroot}/etc/profile.d"
263 sudo mkdir -p "${target}"
264 sudo_clobber "${target}/chromiumos-niceties.sh" << EOF
265# Niceties for interactive logins. (cr) denotes this is a chroot, the
266# __git_branch_ps1 prints current git branch in ./ . The $r behavior is to
267# make sure we don't reset the previous $? value which later formats in
268# $PS1 might rely on.
269PS1='\$(r=\$?; __git_branch_ps1 "(%s) "; exit \$r)'"\${PS1}"
270PS1="(cr) \${PS1}"
271EOF
272
273 # Select a small set of locales for the user if they haven't done so
274 # already. This makes glibc upgrades cheap by only generating a small
275 # set of locales. The ones listed here are basically for the buildbots
276 # which always assume these are available. This works in conjunction
277 # with `cros_sdk --enter`.
278 # http://crosbug.com/20378
279 local localegen="$FLAGS_chroot/etc/locale.gen"
280 if ! grep -q -v -e '^#' -e '^$' "${localegen}" ; then
281 sudo_append "${localegen}" <<EOF
282en_US ISO-8859-1
283en_US.UTF-8 UTF-8
284EOF
285 fi
286
287 # Add chromite as a local site-package.
288 mkdir -p "${FLAGS_chroot}/home/$USER/.local/lib/python2.6/site-packages"
289 ln -s ../../../../trunk/chromite \
290 "${FLAGS_chroot}/home/$USER/.local/lib/python2.6/site-packages/"
291
292 chmod a+x "$FLAGS_chroot/home/$USER/.bashrc"
Brian Harring35767822012-02-01 23:50:45 -0800293 # Automatically change to scripts directory.
Brian Harringf539bc32012-02-06 00:18:37 -0800294 echo 'cd ${CHROOT_CWD:-~/trunk/src/scripts}' \
295 >> "$FLAGS_chroot/home/$USER/.bash_profile"
296
Brian Harring35767822012-02-01 23:50:45 -0800297 # Enable bash completion for build scripts.
Brian Harringf539bc32012-02-06 00:18:37 -0800298 echo ". ~/trunk/src/scripts/bash_completion" \
299 >> "$FLAGS_chroot/home/$USER/.bashrc"
300
Brian Harring35767822012-02-01 23:50:45 -0800301 # Warn if attempting to use source control commands inside the chroot.
Brian Harringf539bc32012-02-06 00:18:37 -0800302 for NOUSE in svn gcl gclient
303 do
304 echo "alias $NOUSE='echo In the chroot, it is a bad idea to run $NOUSE'" \
305 >> "$FLAGS_chroot/home/$USER/.bash_profile"
306 done
307
308 if [[ "$USER" = "chrome-bot" ]]; then
309 # Copy ssh keys, so chroot'd chrome-bot can scp files from chrome-web.
310 cp -r ~/.ssh "$FLAGS_chroot/home/$USER/"
311 fi
312
313 if [[ -f $HOME/.gitconfig ]]; then
Brian Harring35767822012-02-01 23:50:45 -0800314 # Copy .gitconfig into chroot so repo and git can be used from inside.
315 # This is required for repo to work since it validates the email address.
Brian Harringf539bc32012-02-06 00:18:37 -0800316 echo "Copying ~/.gitconfig into chroot"
317 cp $HOME/.gitconfig "$FLAGS_chroot/home/$USER/"
318 fi
Luigi Semenzato2443fdd2012-05-29 10:34:04 -0700319
320 if [[ -f $HOME/.cros_chroot_init ]]; then
321 /bin/bash $HOME/.cros_chroot_init "${FLAGS_chroot}"
322 fi
Brian Harringf539bc32012-02-06 00:18:37 -0800323}
324
Brian Harring35767822012-02-01 23:50:45 -0800325# Handle deleting an existing environment.
Brian Harringf539bc32012-02-06 00:18:37 -0800326if [[ $FLAGS_delete -eq $FLAGS_TRUE || \
Brian Harring35767822012-02-01 23:50:45 -0800327 $FLAGS_replace -eq $FLAGS_TRUE ]]; then
Brian Harringf539bc32012-02-06 00:18:37 -0800328 delete_existing
329 [[ $FLAGS_delete -eq $FLAGS_TRUE ]] && exit 0
330fi
331
332CHROOT_TRUNK="${CHROOT_TRUNK_DIR}"
333PORTAGE="${SRC_ROOT}/third_party/portage"
334OVERLAY="${SRC_ROOT}/third_party/chromiumos-overlay"
335CONFIG_DIR="${OVERLAY}/chromeos/config"
336CHROOT_CONFIG="${CHROOT_TRUNK}/src/third_party/chromiumos-overlay/chromeos/config"
337PORTAGE_STABLE_OVERLAY="/usr/local/portage/stable"
338CROSSDEV_OVERLAY="/usr/local/portage/crossdev"
339CHROOT_OVERLAY="/usr/local/portage/chromiumos"
340CHROOT_STATE="${FLAGS_chroot}/etc/debian_chroot"
Brian Harringf539bc32012-02-06 00:18:37 -0800341
342# Pass proxy variables into the environment.
343for type in http ftp all; do
344 value=$(env | grep ${type}_proxy || true)
345 if [ -n "${value}" ]; then
346 CHROOT_PASSTHRU+=("$value")
347 fi
348done
349
Brian Harring35767822012-02-01 23:50:45 -0800350# Create the base Gentoo stage3 based on last version put in chroot.
Brian Harringf539bc32012-02-06 00:18:37 -0800351STAGE3="${OVERLAY}/chromeos/stage3/stage3-amd64-${FLAGS_stage3_date}.tar.bz2"
352if [ -f $CHROOT_STATE ] && \
353 ! sudo egrep -q "^STAGE3=$STAGE3" $CHROOT_STATE >/dev/null 2>&1
354then
Brian Harring35767822012-02-01 23:50:45 -0800355 info "STAGE3 version has changed."
Brian Harringf539bc32012-02-06 00:18:37 -0800356 delete_existing
357fi
358
359if [ -n "${FLAGS_stage3_path}" ]; then
Brian Harring35767822012-02-01 23:50:45 -0800360 if [ ! -f "${FLAGS_stage3_path}" ]; then
Brian Harringf539bc32012-02-06 00:18:37 -0800361 error "Invalid stage3!"
362 exit 1;
363 fi
Brian Harring35767822012-02-01 23:50:45 -0800364 STAGE3="${FLAGS_stage3_path}"
Brian Harringf539bc32012-02-06 00:18:37 -0800365fi
366
Brian Harring35767822012-02-01 23:50:45 -0800367# Create the destination directory.
Brian Harringf539bc32012-02-06 00:18:37 -0800368mkdir -p "$FLAGS_chroot"
369
370echo
371if [ -f $CHROOT_STATE ]
372then
Brian Harring35767822012-02-01 23:50:45 -0800373 info "STAGE3 already set up. Skipping..."
Brian Harringf539bc32012-02-06 00:18:37 -0800374else
Brian Harring35767822012-02-01 23:50:45 -0800375 info "Unpacking STAGE3..."
Zdenek Behan074f9ef2012-05-30 01:23:59 +0200376 case ${STAGE3} in
377 *.tbz2|*.tar.bz2) DECOMPRESS=$(type -p pbzip2 || echo bzip2) ;;
378 *.tar.xz) DECOMPRESS="xz" ;;
379 *) die "Unknown tarball compression: ${STAGE3}";;
380 esac
381 ${DECOMPRESS} -dc "${STAGE3}" | \
Brian Harring021858a2012-05-26 19:33:01 -0700382 sudo tar -xp -C "${FLAGS_chroot}"
Brian Harring35767822012-02-01 23:50:45 -0800383 sudo rm -f "$FLAGS_chroot/etc/"make.{globals,conf.user}
Brian Harringf539bc32012-02-06 00:18:37 -0800384fi
385
Brian Harring35767822012-02-01 23:50:45 -0800386# Set up users, if needed, before mkdir/mounts below.
Brian Harringf539bc32012-02-06 00:18:37 -0800387[ -f $CHROOT_STATE ] || init_users
388
389echo
Brian Harring35767822012-02-01 23:50:45 -0800390info "Setting up mounts..."
391# Set up necessary mounts and make sure we clean them up on exit.
392sudo mkdir -p "${FLAGS_chroot}/${CHROOT_TRUNK}" "${FLAGS_chroot}/run"
Brian Harring35767822012-02-01 23:50:45 -0800393
J. Richard Barnettee80f6de2012-02-24 14:08:34 -0800394# Create a special /etc/make.conf.host_setup that we use to bootstrap
395# the chroot. The regular content for the file will be generated the
396# first time we invoke update_chroot (further down in this script).
397create_bootstrap_host_setup "${FLAGS_chroot}"
Brian Harringf539bc32012-02-06 00:18:37 -0800398
399if ! [ -f "$CHROOT_STATE" ];then
400 INITIALIZE_CHROOT=1
401fi
402
403
Mike Frysingerba758452012-04-02 13:28:31 -0400404if ! early_enter_chroot bash -c 'type -P pbzip2' >/dev/null ; then
405 # This chroot lacks pbzip2 early on, so we need to disable it.
406 early_env+=(
407 PORTAGE_BZIP2_COMMAND="bzip2"
408 PORTAGE_BUNZIP2_COMMAND="bunzip2"
409 )
410fi
411
Brian Harringf539bc32012-02-06 00:18:37 -0800412if [ -z "${INITIALIZE_CHROOT}" ];then
Brian Harring35767822012-02-01 23:50:45 -0800413 info "chroot already initialized. Skipping..."
Brian Harringf539bc32012-02-06 00:18:37 -0800414else
Brian Harring35767822012-02-01 23:50:45 -0800415 # Run all the init stuff to setup the env.
Brian Harringf539bc32012-02-06 00:18:37 -0800416 init_setup
417fi
418
Brian Harring35767822012-02-01 23:50:45 -0800419# Add file to indicate that it is a chroot.
420# Add version of $STAGE3 for update checks.
Brian Harringf539bc32012-02-06 00:18:37 -0800421sudo sh -c "echo STAGE3=$STAGE3 > $CHROOT_STATE"
422
Brian Harring35767822012-02-01 23:50:45 -0800423info "Updating portage"
Mike Frysinger66fd81f2012-02-28 13:13:20 -0500424early_enter_chroot emerge -uNv --quiet portage
Brian Harringf539bc32012-02-06 00:18:37 -0800425
Paul Drews8bae3b52012-10-10 11:18:13 -0700426# Packages that inherit cros-workon commonly get a circular dependency
427# curl->openssl->git->curl that is broken by emerging an early version of git
428# without curl (and webdav that depends on it).
429need_git_rebuild=${FLAGS_FALSE}
430if [[ ! -e "${FLAGS_chroot}/usr/bin/git" ]]; then
431 need_git_rebuild=${FLAGS_TRUE}
432 info "Updating early git"
433 USE="-curl -webdav" early_enter_chroot emerge -uNv $USEPKG dev-vcs/git
434fi
435
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100436info "Updating host toolchain"
437early_enter_chroot emerge -uNv --quiet crossdev
438TOOLCHAIN_ARGS=( --deleteold )
439if [[ ${FLAGS_usepkg} -eq ${FLAGS_FALSE} ]]; then
440 TOOLCHAIN_ARGS+=( --nousepkg )
441fi
442# Note: early_enter_chroot executes as root.
443early_enter_chroot "${CHROOT_TRUNK}/chromite/bin/cros_setup_toolchains" \
444 --hostonly "${TOOLCHAIN_ARGS[@]}"
Brian Harringf539bc32012-02-06 00:18:37 -0800445
446# dhcpcd is included in 'world' by the stage3 that we pull in for some reason.
447# We have no need to install it in our host environment, so pull it out here.
Brian Harring35767822012-02-01 23:50:45 -0800448info "Deselecting dhcpcd"
449early_enter_chroot $EMERGE_CMD --deselect dhcpcd
Brian Harringf539bc32012-02-06 00:18:37 -0800450
Mike Frysinger279f1032012-05-17 15:54:31 -0400451info "Running emerge curl sudo ..."
Mike Frysinger650bf872012-02-27 11:05:26 -0500452early_enter_chroot $EMERGE_CMD -uNv $USEPKG --select $EMERGE_JOBS \
Paul Drews8bae3b52012-10-10 11:18:13 -0700453 pbzip2 dev-libs/openssl net-misc/curl sudo
454
455if [[ ${need_git_rebuild} -eq ${FLAGS_TRUE} ]]; then
456 # (Re-)emerge the full version of git, without preventing curl.
457 info "Updating full verison of git"
458 early_enter_chroot emerge -uNv $USEPKG dev-vcs/git
459fi
Brian Harringf539bc32012-02-06 00:18:37 -0800460
Brian Harringf539bc32012-02-06 00:18:37 -0800461if [ -n "${INITIALIZE_CHROOT}" ]; then
462 # If we're creating a new chroot, we also want to set it to the latest
463 # version.
Brian Harring35767822012-02-01 23:50:45 -0800464 enter_chroot \
465 "${CHROOT_TRUNK}/src/scripts/run_chroot_version_hooks" --force_latest
Brian Harringf539bc32012-02-06 00:18:37 -0800466fi
467
Brian Harring35767822012-02-01 23:50:45 -0800468# Update chroot.
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100469# Skip toolchain update because it already happened above, and the chroot is
470# not ready to emerge all cross toolchains.
471UPDATE_ARGS=( --skip_toolchain_update )
472if [[ ${FLAGS_usepkg} -eq ${FLAGS_TRUE} ]]; then
Brian Harring35767822012-02-01 23:50:45 -0800473 UPDATE_ARGS+=( --usepkg )
Brian Harringf539bc32012-02-06 00:18:37 -0800474else
Brian Harring35767822012-02-01 23:50:45 -0800475 UPDATE_ARGS+=( --nousepkg )
Brian Harringf539bc32012-02-06 00:18:37 -0800476fi
477if [[ ${FLAGS_fast} -eq ${FLAGS_TRUE} ]]; then
Brian Harring35767822012-02-01 23:50:45 -0800478 UPDATE_ARGS+=( --fast )
Brian Harringf539bc32012-02-06 00:18:37 -0800479else
Brian Harring35767822012-02-01 23:50:45 -0800480 UPDATE_ARGS+=( --nofast )
Brian Harringf539bc32012-02-06 00:18:37 -0800481fi
David James184e3902012-02-23 20:19:28 -0800482if [[ "${FLAGS_jobs}" -ne -1 ]]; then
483 UPDATE_ARGS+=( --jobs=${FLAGS_jobs} )
484fi
Brian Harring35767822012-02-01 23:50:45 -0800485enter_chroot "${CHROOT_TRUNK}/src/scripts/update_chroot" "${UPDATE_ARGS[@]}"
Brian Harringf539bc32012-02-06 00:18:37 -0800486
Brian Harring35767822012-02-01 23:50:45 -0800487CHROOT_EXAMPLE_OPT=""
488if [[ "$FLAGS_chroot" != "$DEFAULT_CHROOT_DIR" ]]; then
Brian Harringf539bc32012-02-06 00:18:37 -0800489 CHROOT_EXAMPLE_OPT="--chroot=$FLAGS_chroot"
490fi
491
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100492# As a final pass, build all desired cross-toolchains.
493info "Updating toolchains"
Zdenek Behan4d21a292012-08-17 04:02:29 +0200494enter_chroot sudo -E "${CHROOT_TRUNK}/chromite/bin/cros_setup_toolchains" \
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100495 "${TOOLCHAIN_ARGS[@]}"
496
Matt Tennant0a9d32d2012-07-30 16:51:37 -0700497command_completed
Brian Harringf539bc32012-02-06 00:18:37 -0800498
Brian Harring35767822012-02-01 23:50:45 -0800499cat <<EOF
Mike Frysingerbdc4fb12012-02-10 11:20:03 -0500500
501${CROS_LOG_PREFIX:-cros_sdk}: All set up. To enter the chroot, run:
502$ cros_sdk --enter $CHROOT_EXAMPLE_OPT
Brian Harring35767822012-02-01 23:50:45 -0800503
504CAUTION: Do *NOT* rm -rf the chroot directory; if there are stale bind
505mounts you may end up deleting your source tree too. To unmount and
506delete the chroot cleanly, use:
507$ cros_sdk --delete $CHROOT_EXAMPLE_OPT
Mike Frysingerbdc4fb12012-02-10 11:20:03 -0500508
Brian Harring35767822012-02-01 23:50:45 -0800509EOF