blob: 2817d6c13385fbd87b78e0563629b2a9c07646af [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
Zdenek Behan4d21a292012-08-17 04:02:29 +020017if [ -n "${USE}" ]; then
18 echo "$SCRIPT_NAME: Building with a non-empty USE: ${USE}"
19 echo "This modifies the expected behaviour and can fail."
20fi
21
Brian Harringf539bc32012-02-06 00:18:37 -080022# Check if the host machine architecture is supported.
23ARCHITECTURE="$(uname -m)"
24if [[ "$ARCHITECTURE" != "x86_64" ]]; then
25 echo "$SCRIPT_NAME: $ARCHITECTURE is not supported as a host machine architecture."
26 exit 1
27fi
28
David James76764882012-10-24 19:46:29 -070029# Script must be run outside the chroot and as root.
Brian Harringf539bc32012-02-06 00:18:37 -080030assert_outside_chroot
David James76764882012-10-24 19:46:29 -070031assert_root_user
Brian Harringf539bc32012-02-06 00:18:37 -080032
Brian Harring35767822012-02-01 23:50:45 -080033# Define command line flags.
Brian Harringf539bc32012-02-06 00:18:37 -080034# See http://code.google.com/p/shflags/wiki/Documentation10x
35
36DEFINE_string chroot "$DEFAULT_CHROOT_DIR" \
37 "Destination dir for the chroot environment."
38DEFINE_boolean usepkg $FLAGS_TRUE "Use binary packages to bootstrap."
39DEFINE_boolean delete $FLAGS_FALSE "Delete an existing chroot."
40DEFINE_boolean replace $FLAGS_FALSE "Overwrite existing chroot, if any."
41DEFINE_integer jobs -1 "How many packages to build in parallel at maximum."
Brian Harringf539bc32012-02-06 00:18:37 -080042DEFINE_string stage3_date "2010.03.09" \
43 "Use the stage3 with the given date."
44DEFINE_string stage3_path "" \
45 "Use the stage3 located on this path."
Brian Harring7b6f3772012-09-23 14:01:13 -070046DEFINE_string cache_dir "" "Directory to store caches within."
Brian Harringf539bc32012-02-06 00:18:37 -080047
Brian Harring35767822012-02-01 23:50:45 -080048# Parse command line flags.
Brian Harringf539bc32012-02-06 00:18:37 -080049FLAGS_HELP="usage: $SCRIPT_NAME [flags]"
50FLAGS "$@" || exit 1
51eval set -- "${FLAGS_ARGV}"
52check_flags_only_and_allow_null_arg "$@" && set --
53
Peter Mayo4411efe2012-09-21 04:41:27 -040054CROS_LOG_PREFIX=cros_sdk:make_chroot
David James76764882012-10-24 19:46:29 -070055SUDO_HOME=$(eval echo ~${SUDO_USER})
Brian Harringf539bc32012-02-06 00:18:37 -080056
Brian Harringf539bc32012-02-06 00:18:37 -080057# Set the right umask for chroot creation.
58umask 022
59
60# Only now can we die on error. shflags functions leak non-zero error codes,
Brian Harring7f175a52012-03-02 05:37:00 -080061# so will die prematurely if 'switch_to_strict_mode' is specified before now.
Brian Harringf539bc32012-02-06 00:18:37 -080062# TODO: replace shflags with something less error-prone, or contribute a fix.
Brian Harring7f175a52012-03-02 05:37:00 -080063switch_to_strict_mode
Brian Harringf539bc32012-02-06 00:18:37 -080064
Brian Harring7b6f3772012-09-23 14:01:13 -070065[[ "${FLAGS_delete}" == "${FLAGS_FALSE}" ]] && \
66 [[ -z "${FLAGS_cache_dir}" ]] && \
67 die "--cache_dir is required"
68
J. Richard Barnettee80f6de2012-02-24 14:08:34 -080069. "${SCRIPT_ROOT}"/sdk_lib/make_conf_util.sh
70
Brian Harringf539bc32012-02-06 00:18:37 -080071FULLNAME="ChromeOS Developer"
72DEFGROUPS="eng,adm,cdrom,floppy,audio,video,portage"
Brian Harringf539bc32012-02-06 00:18:37 -080073
74USEPKG=""
75if [[ $FLAGS_usepkg -eq $FLAGS_TRUE ]]; then
76 # Use binary packages. Include all build-time dependencies,
77 # so as to avoid unnecessary differences between source
78 # and binary builds.
79 USEPKG="--getbinpkg --usepkg --with-bdeps y"
80fi
81
Bertrand SIMONNET4dda4f52015-03-19 13:40:58 -070082EMERGE_CMD="${CHROOT_TRUNK_DIR}/chromite/bin/parallel_emerge"
Brian Harringf539bc32012-02-06 00:18:37 -080083
Brian Harring35767822012-02-01 23:50:45 -080084ENTER_CHROOT_ARGS=(
85 CROS_WORKON_SRCROOT="$CHROOT_TRUNK"
David James76764882012-10-24 19:46:29 -070086 PORTAGE_USERNAME="${SUDO_USER}"
Brian Harring35767822012-02-01 23:50:45 -080087 IGNORE_PREFLIGHT_BINHOST="$IGNORE_PREFLIGHT_BINHOST"
88)
89
90# Invoke enter_chroot. This can only be used after sudo has been installed.
Mike Frysinger6b1abb22012-05-11 13:44:06 -040091enter_chroot() {
Brian Harring7b6f3772012-09-23 14:01:13 -070092 "$ENTER_CHROOT" --cache_dir "${FLAGS_cache_dir}" --chroot "$FLAGS_chroot" \
93 -- "${ENTER_CHROOT_ARGS[@]}" "$@"
Brian Harringf539bc32012-02-06 00:18:37 -080094}
95
Brian Harring35767822012-02-01 23:50:45 -080096# Invoke enter_chroot running the command as root, and w/out sudo.
97# This should be used prior to sudo being merged.
Mike Frysingerba758452012-04-02 13:28:31 -040098early_env=()
Mike Frysinger6b1abb22012-05-11 13:44:06 -040099early_enter_chroot() {
Brian Harring35767822012-02-01 23:50:45 -0800100 "$ENTER_CHROOT" --chroot "$FLAGS_chroot" --early_make_chroot \
Brian Harring7b6f3772012-09-23 14:01:13 -0700101 --cache_dir "${FLAGS_cache_dir}" \
Mike Frysingerba758452012-04-02 13:28:31 -0400102 -- "${ENTER_CHROOT_ARGS[@]}" "${early_env[@]}" "$@"
Brian Harringf539bc32012-02-06 00:18:37 -0800103}
104
Mike Frysinger4bb86452013-08-02 13:44:18 -0400105# Run a command within the chroot. The main usage of this is to avoid the
106# overhead of enter_chroot. It's when we do not need access to the source
107# tree, don't need the actual chroot profile env, and can run the command as
108# root. We do have to make sure PATH includes all the right programs as
109# found inside of the chroot since the environment outside of the chroot
110# might be insufficient (like distros with merged /bin /sbin and /usr).
David James76764882012-10-24 19:46:29 -0700111bare_chroot() {
Mike Frysinger4bb86452013-08-02 13:44:18 -0400112 PATH=/bin:/sbin:/usr/bin:/usr/sbin:${PATH} \
113 chroot "${FLAGS_chroot}" "$@"
Brian Harringf539bc32012-02-06 00:18:37 -0800114}
115
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400116cleanup() {
Brian Harringf539bc32012-02-06 00:18:37 -0800117 # Clean up mounts
118 safe_umount_tree "${FLAGS_chroot}"
119}
120
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400121delete_existing() {
Brian Harring35767822012-02-01 23:50:45 -0800122 # Delete old chroot dir.
123 if [[ ! -e "$FLAGS_chroot" ]]; then
124 return
Brian Harringf539bc32012-02-06 00:18:37 -0800125 fi
Brian Harring35767822012-02-01 23:50:45 -0800126 info "Cleaning up old mount points..."
127 cleanup
128 info "Deleting $FLAGS_chroot..."
David James76764882012-10-24 19:46:29 -0700129 rm -rf "$FLAGS_chroot"
Brian Harring35767822012-02-01 23:50:45 -0800130 info "Done."
Brian Harringf539bc32012-02-06 00:18:37 -0800131}
132
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400133init_users () {
Brian Harring35767822012-02-01 23:50:45 -0800134 info "Set timezone..."
135 # date +%Z has trouble with daylight time, so use host's info.
David James76764882012-10-24 19:46:29 -0700136 rm -f "${FLAGS_chroot}/etc/localtime"
Brian Harringf539bc32012-02-06 00:18:37 -0800137 if [ -f /etc/localtime ] ; then
David James76764882012-10-24 19:46:29 -0700138 cp /etc/localtime "${FLAGS_chroot}/etc"
Brian Harringf539bc32012-02-06 00:18:37 -0800139 else
David James76764882012-10-24 19:46:29 -0700140 ln -sf /usr/share/zoneinfo/PST8PDT "${FLAGS_chroot}/etc/localtime"
Brian Harringf539bc32012-02-06 00:18:37 -0800141 fi
Brian Harring35767822012-02-01 23:50:45 -0800142 info "Adding user/group..."
Brian Harringf539bc32012-02-06 00:18:37 -0800143 # Add ourselves as a user inside the chroot.
David James76764882012-10-24 19:46:29 -0700144 bare_chroot groupadd -g 5000 eng
Brian Harringf539bc32012-02-06 00:18:37 -0800145 # We need the UID to match the host user's. This can conflict with
146 # a particular chroot UID. At the same time, the added user has to
147 # be a primary user for the given UID for sudo to work, which is
148 # determined by the order in /etc/passwd. Let's put ourselves on top
149 # of the file.
David James76764882012-10-24 19:46:29 -0700150 bare_chroot useradd -o -G ${DEFGROUPS} -g eng -u ${SUDO_UID} -s \
Simran Basiccfc7ee2014-10-14 13:43:10 -0700151 /bin/bash -m -c "${FULLNAME}" ${SUDO_USER}
Brian Harringf539bc32012-02-06 00:18:37 -0800152 # Because passwd generally isn't sorted and the entry ended up at the
153 # bottom, it is safe to just take it and move it to top instead.
David James76764882012-10-24 19:46:29 -0700154 sed -e '1{h;d};$!{H;d};$G' -i "${FLAGS_chroot}/etc/passwd"
Brian Harringf539bc32012-02-06 00:18:37 -0800155}
156
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400157init_setup () {
Brian Harring35767822012-02-01 23:50:45 -0800158 info "Running init_setup()..."
David James76764882012-10-24 19:46:29 -0700159 mkdir -p -m 755 "${FLAGS_chroot}/usr" \
Mike Frysingerb45dbb52013-12-13 21:14:09 -0500160 "${FLAGS_chroot}${OVERLAYS_ROOT}" \
Mike Frysinger36beaaa2014-09-25 15:52:27 -0400161 "${FLAGS_chroot}"/"${CROSSDEV_OVERLAY}/metadata"
162 # Newer portage complains about bare overlays. Create the file that crossdev
163 # will also create later on.
164 cat <<EOF > "${FLAGS_chroot}/${CROSSDEV_OVERLAY}/metadata/layout.conf"
165# Autogenerated and managed by crossdev
166# Delete the above line if you want to manage this file yourself
167masters = portage-stable chromiumos
168repo-name = crossdev
169use-manifests = true
170thin-manifests = true
171EOF
Mike Frysingerb45dbb52013-12-13 21:14:09 -0500172 ln -sf "${CHROOT_TRUNK_DIR}/src/third_party/eclass-overlay" \
173 "${FLAGS_chroot}"/"${ECLASS_OVERLAY}"
Brian Harring2499bfb2012-12-18 16:23:31 -0800174 ln -sf "${CHROOT_TRUNK_DIR}/src/third_party/chromiumos-overlay" \
Brian Harringf539bc32012-02-06 00:18:37 -0800175 "${FLAGS_chroot}"/"${CHROOT_OVERLAY}"
Brian Harring2499bfb2012-12-18 16:23:31 -0800176 ln -sf "${CHROOT_TRUNK_DIR}/src/third_party/portage-stable" \
Brian Harringf539bc32012-02-06 00:18:37 -0800177 "${FLAGS_chroot}"/"${PORTAGE_STABLE_OVERLAY}"
Brian Harringf539bc32012-02-06 00:18:37 -0800178
Brian Harring35767822012-02-01 23:50:45 -0800179 # Some operations need an mtab.
Mike Frysingerb47a4ff2013-02-20 17:29:05 -0500180 ln -sfT /proc/mounts "${FLAGS_chroot}/etc/mtab"
Brian Harringf539bc32012-02-06 00:18:37 -0800181
182 # Set up sudoers. Inside the chroot, the user can sudo without a password.
183 # (Safe enough, since the only way into the chroot is to 'sudo chroot', so
184 # the user's already typed in one sudo password...)
185 # Make sure the sudoers.d subdir exists as older stage3 base images lack it.
David James76764882012-10-24 19:46:29 -0700186 mkdir -p "${FLAGS_chroot}/etc/sudoers.d"
Brian Harring06d3c2e2012-08-23 07:35:43 -0700187
188 # Use the standardized upgrade script to setup proxied vars.
David James76764882012-10-24 19:46:29 -0700189 load_environment_whitelist
190 bash -e "${SCRIPT_ROOT}/chroot_version_hooks.d/45_rewrite_sudoers.d" \
191 "${FLAGS_chroot}" "${SUDO_USER}" "${ENVIRONMENT_WHITELIST[@]}"
Brian Harring06d3c2e2012-08-23 07:35:43 -0700192
David James76764882012-10-24 19:46:29 -0700193 find "${FLAGS_chroot}/etc/"sudoers* -type f -exec chmod 0440 {} +
Brian Harring35767822012-02-01 23:50:45 -0800194 # Fix bad group for some.
David James76764882012-10-24 19:46:29 -0700195 chown -R root:root "${FLAGS_chroot}/etc/"sudoers*
Brian Harringf539bc32012-02-06 00:18:37 -0800196
Brian Harring35767822012-02-01 23:50:45 -0800197 info "Setting up hosts/resolv..."
198 # Copy config from outside chroot into chroot.
David James76764882012-10-24 19:46:29 -0700199 cp /etc/{hosts,resolv.conf} "$FLAGS_chroot/etc/"
200 chmod 0644 "$FLAGS_chroot"/etc/{hosts,resolv.conf}
Brian Harringf539bc32012-02-06 00:18:37 -0800201
202 # Setup host make.conf. This includes any overlay that we may be using
203 # and a pointer to pre-built packages.
Brian Harring35767822012-02-01 23:50:45 -0800204 # TODO: This should really be part of a profile in the portage.
205 info "Setting up /etc/make.*..."
Mike Frysinger53f79bb2014-04-05 22:27:52 -0400206 rm -f "${FLAGS_chroot}"/etc/{,portage/}make.{conf,profile}{,.catalyst}
Mike Frysingerc6e1ace2013-03-22 00:49:30 -0400207 mkdir -p "${FLAGS_chroot}/etc/portage"
David James76764882012-10-24 19:46:29 -0700208 ln -sf "${CHROOT_CONFIG}/make.conf.amd64-host" \
Brian Harringf539bc32012-02-06 00:18:37 -0800209 "${FLAGS_chroot}/etc/make.conf"
Mike Frysinger411b2422014-06-04 11:49:17 -0400210 ln -sf "${CHROOT_OVERLAY}/profiles/default/linux/amd64/10.0/sdk" \
Brian Harringf539bc32012-02-06 00:18:37 -0800211 "${FLAGS_chroot}/etc/make.profile"
212
Brian Harring35767822012-02-01 23:50:45 -0800213 # Create make.conf.user .
David James76764882012-10-24 19:46:29 -0700214 touch "${FLAGS_chroot}"/etc/make.conf.user
215 chmod 0644 "${FLAGS_chroot}"/etc/make.conf.user
Brian Harringf539bc32012-02-06 00:18:37 -0800216
217 # Create directories referred to by our conf files.
David James76764882012-10-24 19:46:29 -0700218 mkdir -p -m 775 "${FLAGS_chroot}/var/lib/portage/pkgs" \
Brian Harring7b6f3772012-09-23 14:01:13 -0700219 "${FLAGS_chroot}/var/cache/"chromeos-{cache,chrome} \
220 "${FLAGS_chroot}/etc/profile.d"
221
David James76764882012-10-24 19:46:29 -0700222 echo "export CHROMEOS_CACHEDIR=/var/cache/chromeos-cache" > \
223 "${FLAGS_chroot}/etc/profile.d/chromeos-cachedir.sh"
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.
David James76764882012-10-24 19:46:29 -0700230 bare_chroot chown "${SUDO_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.
David James76764882012-10-24 19:46:29 -0700235 ln -s ../../cache/chromeos-cache/distfiles/host \
Brian Harring7ee892d2012-02-02 09:33:10 -0800236 "${FLAGS_chroot}/var/lib/portage/distfiles"
David James76764882012-10-24 19:46:29 -0700237 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"
David James76764882012-10-24 19:46:29 -0700244 cat <<EOF > "${target}"
Brian Harring2499bfb2012-12-18 16:23:31 -0800245PATH=${CHROOT_TRUNK_DIR}/chromite/bin:${DEPOT_TOOLS_DIR}
246CROS_WORKON_SRCROOT="${CHROOT_TRUNK_DIR}"
David James76764882012-10-24 19:46:29 -0700247PORTAGE_USERNAME=${SUDO_USER}
Brian Harringf539bc32012-02-06 00:18:37 -0800248EOF
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"
David James76764882012-10-24 19:46:29 -0700263 mkdir -p "${target}"
264 cat << EOF > "${target}/chromiumos-niceties.sh"
Brian Harringf539bc32012-02-06 00:18:37 -0800265# 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
David James76764882012-10-24 19:46:29 -0700281 cat <<EOF >> "${localegen}"
Brian Harringf539bc32012-02-06 00:18:37 -0800282en_US ISO-8859-1
283en_US.UTF-8 UTF-8
284EOF
285 fi
286
Brian Harring35767822012-02-01 23:50:45 -0800287 # Automatically change to scripts directory.
Brian Harringf539bc32012-02-06 00:18:37 -0800288 echo 'cd ${CHROOT_CWD:-~/trunk/src/scripts}' \
David James76764882012-10-24 19:46:29 -0700289 | user_append "$FLAGS_chroot/home/${SUDO_USER}/.bash_profile"
Brian Harringf539bc32012-02-06 00:18:37 -0800290
Brian Harring35767822012-02-01 23:50:45 -0800291 # Enable bash completion for build scripts.
Brian Harringf539bc32012-02-06 00:18:37 -0800292 echo ". ~/trunk/src/scripts/bash_completion" \
David James76764882012-10-24 19:46:29 -0700293 | user_append "$FLAGS_chroot/home/${SUDO_USER}/.bashrc"
Brian Harringf539bc32012-02-06 00:18:37 -0800294
Peter Mayoe18c7d42013-06-06 13:41:03 -0400295 if [[ "${SUDO_USER}" = "chrome-bot" && -d "${SUDO_HOME}/.ssh" ]]; then
Brian Harringf539bc32012-02-06 00:18:37 -0800296 # Copy ssh keys, so chroot'd chrome-bot can scp files from chrome-web.
Peter Mayo31056952013-06-06 16:22:55 -0400297 user_cp -rp "${SUDO_HOME}/.ssh" "$FLAGS_chroot/home/${SUDO_USER}/"
Brian Harringf539bc32012-02-06 00:18:37 -0800298 fi
299
David James76764882012-10-24 19:46:29 -0700300 if [[ -f ${SUDO_HOME}/.cros_chroot_init ]]; then
301 sudo -u ${SUDO_USER} -- /bin/bash "${SUDO_HOME}/.cros_chroot_init" \
302 "${FLAGS_chroot}"
Luigi Semenzato2443fdd2012-05-29 10:34:04 -0700303 fi
Brian Harringf539bc32012-02-06 00:18:37 -0800304}
305
Brian Harring35767822012-02-01 23:50:45 -0800306# Handle deleting an existing environment.
Brian Harringf539bc32012-02-06 00:18:37 -0800307if [[ $FLAGS_delete -eq $FLAGS_TRUE || \
Brian Harring35767822012-02-01 23:50:45 -0800308 $FLAGS_replace -eq $FLAGS_TRUE ]]; then
Brian Harringf539bc32012-02-06 00:18:37 -0800309 delete_existing
310 [[ $FLAGS_delete -eq $FLAGS_TRUE ]] && exit 0
311fi
312
313CHROOT_TRUNK="${CHROOT_TRUNK_DIR}"
314PORTAGE="${SRC_ROOT}/third_party/portage"
315OVERLAY="${SRC_ROOT}/third_party/chromiumos-overlay"
316CONFIG_DIR="${OVERLAY}/chromeos/config"
Brian Harring2499bfb2012-12-18 16:23:31 -0800317CHROOT_CONFIG="${CHROOT_TRUNK_DIR}/src/third_party/chromiumos-overlay/chromeos/config"
Mike Frysingerb45dbb52013-12-13 21:14:09 -0500318OVERLAYS_ROOT="/usr/local/portage"
319ECLASS_OVERLAY="${OVERLAYS_ROOT}/eclass-overlay"
320PORTAGE_STABLE_OVERLAY="${OVERLAYS_ROOT}/stable"
321CROSSDEV_OVERLAY="${OVERLAYS_ROOT}/crossdev"
322CHROOT_OVERLAY="${OVERLAYS_ROOT}/chromiumos"
Brian Harringf539bc32012-02-06 00:18:37 -0800323CHROOT_STATE="${FLAGS_chroot}/etc/debian_chroot"
Vincent Palatin5ce24062014-02-26 12:08:23 -0800324CHROOT_VERSION="${FLAGS_chroot}/etc/cros_chroot_version"
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 destination directory.
Brian Harringf539bc32012-02-06 00:18:37 -0800335mkdir -p "$FLAGS_chroot"
336
337echo
Mike Frysinger51930072014-04-05 13:01:45 -0400338if [[ -f ${CHROOT_STATE} ]]; then
339 info "stage3 already set up. Skipping..."
340elif [[ -z ${FLAGS_stage3_path} ]]; then
341 die_notrace "Please use --stage3_path when bootstrapping"
Brian Harringf539bc32012-02-06 00:18:37 -0800342else
Mike Frysinger51930072014-04-05 13:01:45 -0400343 info "Unpacking stage3..."
344 case ${FLAGS_stage3_path} in
Zdenek Behan074f9ef2012-05-30 01:23:59 +0200345 *.tbz2|*.tar.bz2) DECOMPRESS=$(type -p pbzip2 || echo bzip2) ;;
346 *.tar.xz) DECOMPRESS="xz" ;;
Mike Frysinger51930072014-04-05 13:01:45 -0400347 *) die "Unknown tarball compression: ${FLAGS_stage3_path}" ;;
Zdenek Behan074f9ef2012-05-30 01:23:59 +0200348 esac
Mike Frysinger51930072014-04-05 13:01:45 -0400349 ${DECOMPRESS} -dc "${FLAGS_stage3_path}" | \
David James76764882012-10-24 19:46:29 -0700350 tar -xp -C "${FLAGS_chroot}"
351 rm -f "$FLAGS_chroot/etc/"make.{globals,conf.user}
Brian Harringf539bc32012-02-06 00:18:37 -0800352fi
353
Vincent Palatin5ce24062014-02-26 12:08:23 -0800354# Ensure that we properly detect when we are inside the chroot.
355# We'll force this to the latest version at the end as needed.
356if [[ ! -e ${CHROOT_VERSION} ]]; then
357 echo "0" > "${CHROOT_VERSION}"
358fi
359
Brian Harring35767822012-02-01 23:50:45 -0800360# Set up users, if needed, before mkdir/mounts below.
Brian Harringf539bc32012-02-06 00:18:37 -0800361[ -f $CHROOT_STATE ] || init_users
362
Brian Harring2499bfb2012-12-18 16:23:31 -0800363# Reset internal vars to force them to the 'inside the chroot' value;
364# since user directories now exist, this can do the upgrade in place.
365set_chroot_trunk_dir "${FLAGS_chroot}" poppycock
366
Brian Harringf539bc32012-02-06 00:18:37 -0800367echo
Brian Harring35767822012-02-01 23:50:45 -0800368info "Setting up mounts..."
369# Set up necessary mounts and make sure we clean them up on exit.
Brian Harring2499bfb2012-12-18 16:23:31 -0800370mkdir -p "${FLAGS_chroot}/${CHROOT_TRUNK_DIR}" \
371 "${FLAGS_chroot}/${DEPOT_TOOLS_DIR}" "${FLAGS_chroot}/run"
Brian Harring35767822012-02-01 23:50:45 -0800372
J. Richard Barnettee80f6de2012-02-24 14:08:34 -0800373# Create a special /etc/make.conf.host_setup that we use to bootstrap
374# the chroot. The regular content for the file will be generated the
375# first time we invoke update_chroot (further down in this script).
376create_bootstrap_host_setup "${FLAGS_chroot}"
Brian Harringf539bc32012-02-06 00:18:37 -0800377
378if ! [ -f "$CHROOT_STATE" ];then
379 INITIALIZE_CHROOT=1
380fi
381
Mike Frysingerba758452012-04-02 13:28:31 -0400382if ! early_enter_chroot bash -c 'type -P pbzip2' >/dev/null ; then
383 # This chroot lacks pbzip2 early on, so we need to disable it.
384 early_env+=(
385 PORTAGE_BZIP2_COMMAND="bzip2"
386 PORTAGE_BUNZIP2_COMMAND="bunzip2"
387 )
388fi
389
Brian Harringf539bc32012-02-06 00:18:37 -0800390if [ -z "${INITIALIZE_CHROOT}" ];then
Brian Harring35767822012-02-01 23:50:45 -0800391 info "chroot already initialized. Skipping..."
Brian Harringf539bc32012-02-06 00:18:37 -0800392else
Brian Harring35767822012-02-01 23:50:45 -0800393 # Run all the init stuff to setup the env.
Brian Harringf539bc32012-02-06 00:18:37 -0800394 init_setup
395fi
396
Brian Harring35767822012-02-01 23:50:45 -0800397# Add file to indicate that it is a chroot.
Mike Frysinger51930072014-04-05 13:01:45 -0400398# Add version of stage3 for update checks.
399echo "STAGE3=${FLAGS_stage3_path}" > "${CHROOT_STATE}"
Brian Harringf539bc32012-02-06 00:18:37 -0800400
Brian Harring35767822012-02-01 23:50:45 -0800401info "Updating portage"
Mike Frysinger66fd81f2012-02-28 13:13:20 -0500402early_enter_chroot emerge -uNv --quiet portage
Brian Harringf539bc32012-02-06 00:18:37 -0800403
Mike Frysinger9a2978c2013-08-04 11:38:43 -0400404# Clear out openrc if it's installed as we don't want it.
405if [[ -e ${FLAGS_chroot}/usr/share/openrc ]]; then
406 info "Uninstalling openrc"
407 early_enter_chroot env CLEAN_DELAY=0 emerge -qC sys-apps/openrc
Mike Frysinger47e599e2013-10-28 13:30:12 -0400408 # Now update baselayout to get our functions.sh. The unmerge
409 # above removed our copy in the process.
410 early_enter_chroot emerge -uNvq sys-apps/baselayout
Mike Frysinger9a2978c2013-08-04 11:38:43 -0400411fi
412
David James31097c32013-11-06 18:56:59 -0800413# The stage3 contains an old version of Python. Upgrade it first so that
414# parallel_emerge (and chromite libs) can use the latest Python syntax.
415info "Updating python-2.x"
416early_enter_chroot emerge -uNvq =dev-lang/python-2*
417
Mike Frysinger78753af2014-10-14 01:15:32 -0400418# New versions of the stage3 have Python 3 set as the default. Make sure we
419# default to 2.x as our scripts are only compatible with Python 2. We leave
420# Python 3 installed though as we've started including it in our SDK.
David James31097c32013-11-06 18:56:59 -0800421early_enter_chroot eselect python set 1
Mike Frysinger83520dd2013-03-22 01:37:37 -0400422
Bertrand SIMONNET85c96c72014-08-20 12:10:48 -0700423# Add chromite into python path.
424# This needs to happen after the python update or the correct /usr/lib/python2.*
425# may not exist.
426for python_path in "${FLAGS_chroot}/usr/lib/"python2.*; do
427 python_path+="/site-packages"
428 sudo mkdir -p "${python_path}"
429 sudo ln -s -fT "${CHROOT_TRUNK_DIR}"/chromite "${python_path}"/chromite
430done
431
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100432info "Updating host toolchain"
Mike Frysinger5e07dc72014-11-05 14:40:04 -0500433if [[ ! -e ${FLAGS_chroot}/usr/bin/crossdev ]]; then
434 early_enter_chroot $EMERGE_CMD -uNv crossdev
435fi
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100436TOOLCHAIN_ARGS=( --deleteold )
437if [[ ${FLAGS_usepkg} -eq ${FLAGS_FALSE} ]]; then
438 TOOLCHAIN_ARGS+=( --nousepkg )
439fi
440# Note: early_enter_chroot executes as root.
Brian Harring2499bfb2012-12-18 16:23:31 -0800441early_enter_chroot "${CHROOT_TRUNK_DIR}/chromite/bin/cros_setup_toolchains" \
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100442 --hostonly "${TOOLCHAIN_ARGS[@]}"
Brian Harringf539bc32012-02-06 00:18:37 -0800443
Mike Frysinger279f1032012-05-17 15:54:31 -0400444info "Running emerge curl sudo ..."
Mike Frysinger650bf872012-02-27 11:05:26 -0500445early_enter_chroot $EMERGE_CMD -uNv $USEPKG --select $EMERGE_JOBS \
Paul Drews8bae3b52012-10-10 11:18:13 -0700446 pbzip2 dev-libs/openssl net-misc/curl sudo
447
Brian Harringf539bc32012-02-06 00:18:37 -0800448if [ -n "${INITIALIZE_CHROOT}" ]; then
449 # If we're creating a new chroot, we also want to set it to the latest
450 # version.
Brian Harring35767822012-02-01 23:50:45 -0800451 enter_chroot \
David James47e0e8a2015-03-11 15:24:10 -0700452 "${CHROOT_TRUNK_DIR}/src/scripts/run_chroot_version_hooks" --init_latest
Brian Harringf539bc32012-02-06 00:18:37 -0800453fi
454
Brian Harring35767822012-02-01 23:50:45 -0800455# Update chroot.
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100456# Skip toolchain update because it already happened above, and the chroot is
457# not ready to emerge all cross toolchains.
458UPDATE_ARGS=( --skip_toolchain_update )
459if [[ ${FLAGS_usepkg} -eq ${FLAGS_TRUE} ]]; then
Brian Harring35767822012-02-01 23:50:45 -0800460 UPDATE_ARGS+=( --usepkg )
Brian Harringf539bc32012-02-06 00:18:37 -0800461else
Brian Harring35767822012-02-01 23:50:45 -0800462 UPDATE_ARGS+=( --nousepkg )
Brian Harringf539bc32012-02-06 00:18:37 -0800463fi
David James184e3902012-02-23 20:19:28 -0800464if [[ "${FLAGS_jobs}" -ne -1 ]]; then
465 UPDATE_ARGS+=( --jobs=${FLAGS_jobs} )
466fi
Brian Harring2499bfb2012-12-18 16:23:31 -0800467enter_chroot "${CHROOT_TRUNK_DIR}/src/scripts/update_chroot" "${UPDATE_ARGS[@]}"
Brian Harringf539bc32012-02-06 00:18:37 -0800468
Stefan Zager48c776b2013-10-09 13:01:25 -0700469# The java-config package atm does not support $ROOT. Select a default
470# VM ourselves until that gets fixed upstream.
471enter_chroot sudo java-config --set-system-vm 1
472
Brian Harring35767822012-02-01 23:50:45 -0800473CHROOT_EXAMPLE_OPT=""
474if [[ "$FLAGS_chroot" != "$DEFAULT_CHROOT_DIR" ]]; then
Brian Harringf539bc32012-02-06 00:18:37 -0800475 CHROOT_EXAMPLE_OPT="--chroot=$FLAGS_chroot"
476fi
477
Matt Tennant0a9d32d2012-07-30 16:51:37 -0700478command_completed
Brian Harringf539bc32012-02-06 00:18:37 -0800479
Brian Harring35767822012-02-01 23:50:45 -0800480cat <<EOF
Mike Frysingerbdc4fb12012-02-10 11:20:03 -0500481
482${CROS_LOG_PREFIX:-cros_sdk}: All set up. To enter the chroot, run:
483$ cros_sdk --enter $CHROOT_EXAMPLE_OPT
Brian Harring35767822012-02-01 23:50:45 -0800484
485CAUTION: Do *NOT* rm -rf the chroot directory; if there are stale bind
486mounts you may end up deleting your source tree too. To unmount and
487delete the chroot cleanly, use:
488$ cros_sdk --delete $CHROOT_EXAMPLE_OPT
Mike Frysingerbdc4fb12012-02-10 11:20:03 -0500489
Brian Harring35767822012-02-01 23:50:45 -0800490EOF
David James22dc2ba2012-11-29 15:46:58 -0800491
492warn_if_nfs "${SUDO_HOME}"