blob: 7d763db2ff2eff60146a3241b1b3998c841e2d26 [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."
David Purselld35d7af2015-05-04 16:37:08 -070046DEFINE_string workspace_root "" \
47 "The root of your workspace."
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
David Purselld35d7af2015-05-04 16:37:08 -070057SUDO_HOME=$(eval echo ~"${SUDO_USER}")
Brian Harringf539bc32012-02-06 00:18:37 -080058
Brian Harringf539bc32012-02-06 00:18:37 -080059# 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
David Purselld35d7af2015-05-04 16:37:08 -070073PRIMARY_GROUP=$(id -g -n "${SUDO_USER}")
74PRIMARY_GROUP_ID=$(id -g "${SUDO_USER}")
75
Brian Harringf539bc32012-02-06 00:18:37 -080076FULLNAME="ChromeOS Developer"
David Purselld35d7af2015-05-04 16:37:08 -070077DEFGROUPS="${PRIMARY_GROUP},adm,cdrom,floppy,audio,video,portage"
78
79# If a workspace was specified we want to make sure the user is in its group.
80if [[ -n "${FLAGS_workspace_root}" ]]; then
81 WORKSPACE_GROUP=$(stat -c "%G" "${FLAGS_workspace_root}")
82 WORKSPACE_GROUP_ID=$(getent group "${WORKSPACE_GROUP}" | cut -d: -f3)
83 # Duplicates in DEFGROUPS are fine, the useradd call handles them properly.
84 DEFGROUPS+=",${WORKSPACE_GROUP}"
85fi
Brian Harringf539bc32012-02-06 00:18:37 -080086
87USEPKG=""
88if [[ $FLAGS_usepkg -eq $FLAGS_TRUE ]]; then
89 # Use binary packages. Include all build-time dependencies,
90 # so as to avoid unnecessary differences between source
91 # and binary builds.
92 USEPKG="--getbinpkg --usepkg --with-bdeps y"
93fi
94
Bertrand SIMONNET4dda4f52015-03-19 13:40:58 -070095EMERGE_CMD="${CHROOT_TRUNK_DIR}/chromite/bin/parallel_emerge"
Brian Harringf539bc32012-02-06 00:18:37 -080096
Brian Harring35767822012-02-01 23:50:45 -080097ENTER_CHROOT_ARGS=(
98 CROS_WORKON_SRCROOT="$CHROOT_TRUNK"
David James76764882012-10-24 19:46:29 -070099 PORTAGE_USERNAME="${SUDO_USER}"
Brian Harring35767822012-02-01 23:50:45 -0800100 IGNORE_PREFLIGHT_BINHOST="$IGNORE_PREFLIGHT_BINHOST"
101)
102
103# Invoke enter_chroot. This can only be used after sudo has been installed.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400104enter_chroot() {
Brian Harring7b6f3772012-09-23 14:01:13 -0700105 "$ENTER_CHROOT" --cache_dir "${FLAGS_cache_dir}" --chroot "$FLAGS_chroot" \
106 -- "${ENTER_CHROOT_ARGS[@]}" "$@"
Brian Harringf539bc32012-02-06 00:18:37 -0800107}
108
Brian Harring35767822012-02-01 23:50:45 -0800109# Invoke enter_chroot running the command as root, and w/out sudo.
110# This should be used prior to sudo being merged.
Mike Frysingerba758452012-04-02 13:28:31 -0400111early_env=()
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400112early_enter_chroot() {
Brian Harring35767822012-02-01 23:50:45 -0800113 "$ENTER_CHROOT" --chroot "$FLAGS_chroot" --early_make_chroot \
Brian Harring7b6f3772012-09-23 14:01:13 -0700114 --cache_dir "${FLAGS_cache_dir}" \
Mike Frysingerba758452012-04-02 13:28:31 -0400115 -- "${ENTER_CHROOT_ARGS[@]}" "${early_env[@]}" "$@"
Brian Harringf539bc32012-02-06 00:18:37 -0800116}
117
Mike Frysinger4bb86452013-08-02 13:44:18 -0400118# Run a command within the chroot. The main usage of this is to avoid the
119# overhead of enter_chroot. It's when we do not need access to the source
120# tree, don't need the actual chroot profile env, and can run the command as
121# root. We do have to make sure PATH includes all the right programs as
122# found inside of the chroot since the environment outside of the chroot
123# might be insufficient (like distros with merged /bin /sbin and /usr).
David James76764882012-10-24 19:46:29 -0700124bare_chroot() {
David Purselld35d7af2015-05-04 16:37:08 -0700125 PATH="/bin:/sbin:/usr/bin:/usr/sbin:${PATH}" \
Mike Frysinger4bb86452013-08-02 13:44:18 -0400126 chroot "${FLAGS_chroot}" "$@"
Brian Harringf539bc32012-02-06 00:18:37 -0800127}
128
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400129cleanup() {
Brian Harringf539bc32012-02-06 00:18:37 -0800130 # Clean up mounts
131 safe_umount_tree "${FLAGS_chroot}"
132}
133
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400134delete_existing() {
Brian Harring35767822012-02-01 23:50:45 -0800135 # Delete old chroot dir.
136 if [[ ! -e "$FLAGS_chroot" ]]; then
137 return
Brian Harringf539bc32012-02-06 00:18:37 -0800138 fi
Brian Harring35767822012-02-01 23:50:45 -0800139 info "Cleaning up old mount points..."
140 cleanup
141 info "Deleting $FLAGS_chroot..."
David James76764882012-10-24 19:46:29 -0700142 rm -rf "$FLAGS_chroot"
Brian Harring35767822012-02-01 23:50:45 -0800143 info "Done."
Brian Harringf539bc32012-02-06 00:18:37 -0800144}
145
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400146init_users () {
Brian Harring35767822012-02-01 23:50:45 -0800147 info "Set timezone..."
148 # date +%Z has trouble with daylight time, so use host's info.
David James76764882012-10-24 19:46:29 -0700149 rm -f "${FLAGS_chroot}/etc/localtime"
Brian Harringf539bc32012-02-06 00:18:37 -0800150 if [ -f /etc/localtime ] ; then
David James76764882012-10-24 19:46:29 -0700151 cp /etc/localtime "${FLAGS_chroot}/etc"
Brian Harringf539bc32012-02-06 00:18:37 -0800152 else
David James76764882012-10-24 19:46:29 -0700153 ln -sf /usr/share/zoneinfo/PST8PDT "${FLAGS_chroot}/etc/localtime"
Brian Harringf539bc32012-02-06 00:18:37 -0800154 fi
Brian Harring35767822012-02-01 23:50:45 -0800155 info "Adding user/group..."
David Purselld35d7af2015-05-04 16:37:08 -0700156 # Add the necessary groups to the chroot.
157 # Duplicate GIDs are allowed here in order to ensure that the required
158 # groups are the same inside and outside the chroot.
159 # TODO(dpursell): Handle when PRIMARY_GROUP exists in the chroot already
160 # with a different GID; groupadd will not create the new GID in that case.
161 bare_chroot groupadd -f -o -g "${PRIMARY_GROUP_ID}" "${PRIMARY_GROUP}"
162 if [[ -n "${WORKSPACE_GROUP}" ]]; then
163 # groupadd -f is fine (has no effect) if this group+ID already exists.
164 # TODO(dpursell): Handle when WORKSPACE_GROUP exists in the chroot already
165 # with a different GID; groupadd will not create the new GID in that case.
166 bare_chroot groupadd -f -o -g "${WORKSPACE_GROUP_ID}" "${WORKSPACE_GROUP}"
167 fi
Brian Harringf539bc32012-02-06 00:18:37 -0800168 # Add ourselves as a user inside the chroot.
Brian Harringf539bc32012-02-06 00:18:37 -0800169 # We need the UID to match the host user's. This can conflict with
170 # a particular chroot UID. At the same time, the added user has to
171 # be a primary user for the given UID for sudo to work, which is
172 # determined by the order in /etc/passwd. Let's put ourselves on top
173 # of the file.
David Purselld35d7af2015-05-04 16:37:08 -0700174 bare_chroot useradd -o -G "${DEFGROUPS}" -g "${PRIMARY_GROUP}" \
175 -u "${SUDO_UID}" -s /bin/bash -m -c "${FULLNAME}" "${SUDO_USER}"
Brian Harringf539bc32012-02-06 00:18:37 -0800176 # Because passwd generally isn't sorted and the entry ended up at the
177 # bottom, it is safe to just take it and move it to top instead.
David James76764882012-10-24 19:46:29 -0700178 sed -e '1{h;d};$!{H;d};$G' -i "${FLAGS_chroot}/etc/passwd"
Brian Harringf539bc32012-02-06 00:18:37 -0800179}
180
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400181init_setup () {
Brian Harring35767822012-02-01 23:50:45 -0800182 info "Running init_setup()..."
David James76764882012-10-24 19:46:29 -0700183 mkdir -p -m 755 "${FLAGS_chroot}/usr" \
Mike Frysingerb45dbb52013-12-13 21:14:09 -0500184 "${FLAGS_chroot}${OVERLAYS_ROOT}" \
Mike Frysinger36beaaa2014-09-25 15:52:27 -0400185 "${FLAGS_chroot}"/"${CROSSDEV_OVERLAY}/metadata"
186 # Newer portage complains about bare overlays. Create the file that crossdev
187 # will also create later on.
188 cat <<EOF > "${FLAGS_chroot}/${CROSSDEV_OVERLAY}/metadata/layout.conf"
189# Autogenerated and managed by crossdev
190# Delete the above line if you want to manage this file yourself
191masters = portage-stable chromiumos
192repo-name = crossdev
193use-manifests = true
194thin-manifests = true
195EOF
Mike Frysingerb45dbb52013-12-13 21:14:09 -0500196 ln -sf "${CHROOT_TRUNK_DIR}/src/third_party/eclass-overlay" \
197 "${FLAGS_chroot}"/"${ECLASS_OVERLAY}"
Brian Harring2499bfb2012-12-18 16:23:31 -0800198 ln -sf "${CHROOT_TRUNK_DIR}/src/third_party/chromiumos-overlay" \
Brian Harringf539bc32012-02-06 00:18:37 -0800199 "${FLAGS_chroot}"/"${CHROOT_OVERLAY}"
Brian Harring2499bfb2012-12-18 16:23:31 -0800200 ln -sf "${CHROOT_TRUNK_DIR}/src/third_party/portage-stable" \
Brian Harringf539bc32012-02-06 00:18:37 -0800201 "${FLAGS_chroot}"/"${PORTAGE_STABLE_OVERLAY}"
Brian Harringf539bc32012-02-06 00:18:37 -0800202
Brian Harring35767822012-02-01 23:50:45 -0800203 # Some operations need an mtab.
Mike Frysingerb47a4ff2013-02-20 17:29:05 -0500204 ln -sfT /proc/mounts "${FLAGS_chroot}/etc/mtab"
Brian Harringf539bc32012-02-06 00:18:37 -0800205
206 # Set up sudoers. Inside the chroot, the user can sudo without a password.
207 # (Safe enough, since the only way into the chroot is to 'sudo chroot', so
208 # the user's already typed in one sudo password...)
209 # Make sure the sudoers.d subdir exists as older stage3 base images lack it.
David James76764882012-10-24 19:46:29 -0700210 mkdir -p "${FLAGS_chroot}/etc/sudoers.d"
Brian Harring06d3c2e2012-08-23 07:35:43 -0700211
212 # Use the standardized upgrade script to setup proxied vars.
David James76764882012-10-24 19:46:29 -0700213 load_environment_whitelist
214 bash -e "${SCRIPT_ROOT}/chroot_version_hooks.d/45_rewrite_sudoers.d" \
215 "${FLAGS_chroot}" "${SUDO_USER}" "${ENVIRONMENT_WHITELIST[@]}"
Brian Harring06d3c2e2012-08-23 07:35:43 -0700216
David James76764882012-10-24 19:46:29 -0700217 find "${FLAGS_chroot}/etc/"sudoers* -type f -exec chmod 0440 {} +
Brian Harring35767822012-02-01 23:50:45 -0800218 # Fix bad group for some.
David James76764882012-10-24 19:46:29 -0700219 chown -R root:root "${FLAGS_chroot}/etc/"sudoers*
Brian Harringf539bc32012-02-06 00:18:37 -0800220
Brian Harring35767822012-02-01 23:50:45 -0800221 info "Setting up hosts/resolv..."
222 # Copy config from outside chroot into chroot.
David James76764882012-10-24 19:46:29 -0700223 cp /etc/{hosts,resolv.conf} "$FLAGS_chroot/etc/"
224 chmod 0644 "$FLAGS_chroot"/etc/{hosts,resolv.conf}
Brian Harringf539bc32012-02-06 00:18:37 -0800225
226 # Setup host make.conf. This includes any overlay that we may be using
227 # and a pointer to pre-built packages.
Brian Harring35767822012-02-01 23:50:45 -0800228 # TODO: This should really be part of a profile in the portage.
229 info "Setting up /etc/make.*..."
Mike Frysinger53f79bb2014-04-05 22:27:52 -0400230 rm -f "${FLAGS_chroot}"/etc/{,portage/}make.{conf,profile}{,.catalyst}
Mike Frysingerc6e1ace2013-03-22 00:49:30 -0400231 mkdir -p "${FLAGS_chroot}/etc/portage"
David James76764882012-10-24 19:46:29 -0700232 ln -sf "${CHROOT_CONFIG}/make.conf.amd64-host" \
Brian Harringf539bc32012-02-06 00:18:37 -0800233 "${FLAGS_chroot}/etc/make.conf"
Mike Frysinger411b2422014-06-04 11:49:17 -0400234 ln -sf "${CHROOT_OVERLAY}/profiles/default/linux/amd64/10.0/sdk" \
Brian Harringf539bc32012-02-06 00:18:37 -0800235 "${FLAGS_chroot}/etc/make.profile"
236
Brian Harring35767822012-02-01 23:50:45 -0800237 # Create make.conf.user .
David James76764882012-10-24 19:46:29 -0700238 touch "${FLAGS_chroot}"/etc/make.conf.user
239 chmod 0644 "${FLAGS_chroot}"/etc/make.conf.user
Brian Harringf539bc32012-02-06 00:18:37 -0800240
241 # Create directories referred to by our conf files.
David James76764882012-10-24 19:46:29 -0700242 mkdir -p -m 775 "${FLAGS_chroot}/var/lib/portage/pkgs" \
Brian Harring7b6f3772012-09-23 14:01:13 -0700243 "${FLAGS_chroot}/var/cache/"chromeos-{cache,chrome} \
244 "${FLAGS_chroot}/etc/profile.d"
245
David James76764882012-10-24 19:46:29 -0700246 echo "export CHROMEOS_CACHEDIR=/var/cache/chromeos-cache" > \
247 "${FLAGS_chroot}/etc/profile.d/chromeos-cachedir.sh"
248 chmod 0644 "${FLAGS_chroot}/etc/profile.d/chromeos-cachedir.sh"
249 rm -rf "${FLAGS_chroot}/var/cache/distfiles"
250 ln -s chromeos-cache/distfiles "${FLAGS_chroot}/var/cache/distfiles"
Brian Harring36b102b2012-02-06 23:34:25 -0800251
252 # Run this from w/in the chroot so we use whatever uid/gid
253 # these are defined as w/in the chroot.
David James76764882012-10-24 19:46:29 -0700254 bare_chroot chown "${SUDO_USER}:portage" /var/cache/chromeos-chrome
Brian Harring7ee892d2012-02-02 09:33:10 -0800255
256 # These are created for compatibility while transitioning
257 # make.conf and friends over to the new location.
Brian Harring7b6f3772012-09-23 14:01:13 -0700258 # TODO(ferringb): remove this 01/13 or so.
David James76764882012-10-24 19:46:29 -0700259 ln -s ../../cache/chromeos-cache/distfiles/host \
Brian Harring7ee892d2012-02-02 09:33:10 -0800260 "${FLAGS_chroot}/var/lib/portage/distfiles"
David James76764882012-10-24 19:46:29 -0700261 ln -s ../../cache/chromeos-cache/distfiles/target \
Brian Harring7ee892d2012-02-02 09:33:10 -0800262 "${FLAGS_chroot}/var/lib/portage/distfiles-target"
Brian Harringf539bc32012-02-06 00:18:37 -0800263
Brian Harringf539bc32012-02-06 00:18:37 -0800264 # Add chromite/bin and depot_tools into the path globally; note that the
265 # chromite wrapper itself might also be found in depot_tools.
266 # We rely on 'env-update' getting called below.
267 target="${FLAGS_chroot}/etc/env.d/99chromiumos"
David James76764882012-10-24 19:46:29 -0700268 cat <<EOF > "${target}"
David Purselld35d7af2015-05-04 16:37:08 -0700269PATH="${CHROOT_TRUNK_DIR}/chromite/bin:${DEPOT_TOOLS_DIR}"
Brian Harring2499bfb2012-12-18 16:23:31 -0800270CROS_WORKON_SRCROOT="${CHROOT_TRUNK_DIR}"
David Purselld35d7af2015-05-04 16:37:08 -0700271PORTAGE_USERNAME="${SUDO_USER}"
Brian Harringf539bc32012-02-06 00:18:37 -0800272EOF
273
274 # TODO(zbehan): Configure stuff that is usually configured in postinst's,
Mike Frysingereb1a9b42012-03-28 16:21:04 -0400275 # but wasn't. Fix the postinst's.
Brian Harring35767822012-02-01 23:50:45 -0800276 info "Running post-inst configuration hacks"
277 early_enter_chroot env-update
Brian Harringf539bc32012-02-06 00:18:37 -0800278
Brian Harring35767822012-02-01 23:50:45 -0800279 # This is basically a sanity check of our chroot. If any of these
280 # don't exist, then either bind mounts have failed, an invocation
281 # from above is broke, or some assumption about the stage3 is no longer
282 # true.
283 early_enter_chroot ls -l /etc/make.{conf,profile} \
284 /usr/local/portage/chromiumos/profiles/default/linux/amd64/10.0
Brian Harringf539bc32012-02-06 00:18:37 -0800285
286 target="${FLAGS_chroot}/etc/profile.d"
David James76764882012-10-24 19:46:29 -0700287 mkdir -p "${target}"
288 cat << EOF > "${target}/chromiumos-niceties.sh"
Brian Harringf539bc32012-02-06 00:18:37 -0800289# Niceties for interactive logins. (cr) denotes this is a chroot, the
290# __git_branch_ps1 prints current git branch in ./ . The $r behavior is to
291# make sure we don't reset the previous $? value which later formats in
292# $PS1 might rely on.
293PS1='\$(r=\$?; __git_branch_ps1 "(%s) "; exit \$r)'"\${PS1}"
294PS1="(cr) \${PS1}"
295EOF
296
297 # Select a small set of locales for the user if they haven't done so
298 # already. This makes glibc upgrades cheap by only generating a small
299 # set of locales. The ones listed here are basically for the buildbots
300 # which always assume these are available. This works in conjunction
301 # with `cros_sdk --enter`.
302 # http://crosbug.com/20378
303 local localegen="$FLAGS_chroot/etc/locale.gen"
304 if ! grep -q -v -e '^#' -e '^$' "${localegen}" ; then
David James76764882012-10-24 19:46:29 -0700305 cat <<EOF >> "${localegen}"
Brian Harringf539bc32012-02-06 00:18:37 -0800306en_US ISO-8859-1
307en_US.UTF-8 UTF-8
308EOF
309 fi
310
Brian Harring35767822012-02-01 23:50:45 -0800311 # Automatically change to scripts directory.
Brian Harringf539bc32012-02-06 00:18:37 -0800312 echo 'cd ${CHROOT_CWD:-~/trunk/src/scripts}' \
David Purselld35d7af2015-05-04 16:37:08 -0700313 | user_append "${FLAGS_chroot}/home/${SUDO_USER}/.bash_profile"
Brian Harringf539bc32012-02-06 00:18:37 -0800314
Brian Harring35767822012-02-01 23:50:45 -0800315 # Enable bash completion for build scripts.
Brian Harringf539bc32012-02-06 00:18:37 -0800316 echo ". ~/trunk/src/scripts/bash_completion" \
David Purselld35d7af2015-05-04 16:37:08 -0700317 | user_append "${FLAGS_chroot}/home/${SUDO_USER}/.bashrc"
Brian Harringf539bc32012-02-06 00:18:37 -0800318
Peter Mayoe18c7d42013-06-06 13:41:03 -0400319 if [[ "${SUDO_USER}" = "chrome-bot" && -d "${SUDO_HOME}/.ssh" ]]; then
Brian Harringf539bc32012-02-06 00:18:37 -0800320 # Copy ssh keys, so chroot'd chrome-bot can scp files from chrome-web.
Peter Mayo31056952013-06-06 16:22:55 -0400321 user_cp -rp "${SUDO_HOME}/.ssh" "$FLAGS_chroot/home/${SUDO_USER}/"
Brian Harringf539bc32012-02-06 00:18:37 -0800322 fi
323
David Purselld35d7af2015-05-04 16:37:08 -0700324 if [[ -f "${SUDO_HOME}/.cros_chroot_init" ]]; then
325 sudo -u "${SUDO_USER}" -- /bin/bash "${SUDO_HOME}/.cros_chroot_init" \
David James76764882012-10-24 19:46:29 -0700326 "${FLAGS_chroot}"
Luigi Semenzato2443fdd2012-05-29 10:34:04 -0700327 fi
Brian Harringf539bc32012-02-06 00:18:37 -0800328}
329
Brian Harring35767822012-02-01 23:50:45 -0800330# Handle deleting an existing environment.
Brian Harringf539bc32012-02-06 00:18:37 -0800331if [[ $FLAGS_delete -eq $FLAGS_TRUE || \
Brian Harring35767822012-02-01 23:50:45 -0800332 $FLAGS_replace -eq $FLAGS_TRUE ]]; then
Brian Harringf539bc32012-02-06 00:18:37 -0800333 delete_existing
334 [[ $FLAGS_delete -eq $FLAGS_TRUE ]] && exit 0
335fi
336
337CHROOT_TRUNK="${CHROOT_TRUNK_DIR}"
338PORTAGE="${SRC_ROOT}/third_party/portage"
339OVERLAY="${SRC_ROOT}/third_party/chromiumos-overlay"
340CONFIG_DIR="${OVERLAY}/chromeos/config"
Brian Harring2499bfb2012-12-18 16:23:31 -0800341CHROOT_CONFIG="${CHROOT_TRUNK_DIR}/src/third_party/chromiumos-overlay/chromeos/config"
Mike Frysingerb45dbb52013-12-13 21:14:09 -0500342OVERLAYS_ROOT="/usr/local/portage"
343ECLASS_OVERLAY="${OVERLAYS_ROOT}/eclass-overlay"
344PORTAGE_STABLE_OVERLAY="${OVERLAYS_ROOT}/stable"
345CROSSDEV_OVERLAY="${OVERLAYS_ROOT}/crossdev"
346CHROOT_OVERLAY="${OVERLAYS_ROOT}/chromiumos"
Brian Harringf539bc32012-02-06 00:18:37 -0800347CHROOT_STATE="${FLAGS_chroot}/etc/debian_chroot"
Vincent Palatin5ce24062014-02-26 12:08:23 -0800348CHROOT_VERSION="${FLAGS_chroot}/etc/cros_chroot_version"
Brian Harringf539bc32012-02-06 00:18:37 -0800349
350# Pass proxy variables into the environment.
351for type in http ftp all; do
David Purselld35d7af2015-05-04 16:37:08 -0700352 value=$(env | grep "${type}_proxy" || true)
Brian Harringf539bc32012-02-06 00:18:37 -0800353 if [ -n "${value}" ]; then
354 CHROOT_PASSTHRU+=("$value")
355 fi
356done
357
Brian Harring35767822012-02-01 23:50:45 -0800358# Create the destination directory.
Brian Harringf539bc32012-02-06 00:18:37 -0800359mkdir -p "$FLAGS_chroot"
360
361echo
David Purselld35d7af2015-05-04 16:37:08 -0700362if [[ -f "${CHROOT_STATE}" ]]; then
Mike Frysinger51930072014-04-05 13:01:45 -0400363 info "stage3 already set up. Skipping..."
David Purselld35d7af2015-05-04 16:37:08 -0700364elif [[ -z "${FLAGS_stage3_path}" ]]; then
Mike Frysinger51930072014-04-05 13:01:45 -0400365 die_notrace "Please use --stage3_path when bootstrapping"
Brian Harringf539bc32012-02-06 00:18:37 -0800366else
Mike Frysinger51930072014-04-05 13:01:45 -0400367 info "Unpacking stage3..."
David Purselld35d7af2015-05-04 16:37:08 -0700368 case "${FLAGS_stage3_path}" in
Zdenek Behan074f9ef2012-05-30 01:23:59 +0200369 *.tbz2|*.tar.bz2) DECOMPRESS=$(type -p pbzip2 || echo bzip2) ;;
370 *.tar.xz) DECOMPRESS="xz" ;;
Mike Frysinger51930072014-04-05 13:01:45 -0400371 *) die "Unknown tarball compression: ${FLAGS_stage3_path}" ;;
Zdenek Behan074f9ef2012-05-30 01:23:59 +0200372 esac
Mike Frysinger51930072014-04-05 13:01:45 -0400373 ${DECOMPRESS} -dc "${FLAGS_stage3_path}" | \
David James76764882012-10-24 19:46:29 -0700374 tar -xp -C "${FLAGS_chroot}"
375 rm -f "$FLAGS_chroot/etc/"make.{globals,conf.user}
Brian Harringf539bc32012-02-06 00:18:37 -0800376fi
377
Vincent Palatin5ce24062014-02-26 12:08:23 -0800378# Ensure that we properly detect when we are inside the chroot.
379# We'll force this to the latest version at the end as needed.
David Purselld35d7af2015-05-04 16:37:08 -0700380if [[ ! -e "${CHROOT_VERSION}" ]]; then
Vincent Palatin5ce24062014-02-26 12:08:23 -0800381 echo "0" > "${CHROOT_VERSION}"
382fi
383
Brian Harring35767822012-02-01 23:50:45 -0800384# Set up users, if needed, before mkdir/mounts below.
Brian Harringf539bc32012-02-06 00:18:37 -0800385[ -f $CHROOT_STATE ] || init_users
386
Brian Harring2499bfb2012-12-18 16:23:31 -0800387# Reset internal vars to force them to the 'inside the chroot' value;
388# since user directories now exist, this can do the upgrade in place.
389set_chroot_trunk_dir "${FLAGS_chroot}" poppycock
390
Brian Harringf539bc32012-02-06 00:18:37 -0800391echo
Brian Harring35767822012-02-01 23:50:45 -0800392info "Setting up mounts..."
393# Set up necessary mounts and make sure we clean them up on exit.
Brian Harring2499bfb2012-12-18 16:23:31 -0800394mkdir -p "${FLAGS_chroot}/${CHROOT_TRUNK_DIR}" \
395 "${FLAGS_chroot}/${DEPOT_TOOLS_DIR}" "${FLAGS_chroot}/run"
Brian Harring35767822012-02-01 23:50:45 -0800396
J. Richard Barnettee80f6de2012-02-24 14:08:34 -0800397# Create a special /etc/make.conf.host_setup that we use to bootstrap
398# the chroot. The regular content for the file will be generated the
399# first time we invoke update_chroot (further down in this script).
400create_bootstrap_host_setup "${FLAGS_chroot}"
Brian Harringf539bc32012-02-06 00:18:37 -0800401
402if ! [ -f "$CHROOT_STATE" ];then
403 INITIALIZE_CHROOT=1
404fi
405
Mike Frysingerba758452012-04-02 13:28:31 -0400406if ! early_enter_chroot bash -c 'type -P pbzip2' >/dev/null ; then
407 # This chroot lacks pbzip2 early on, so we need to disable it.
408 early_env+=(
409 PORTAGE_BZIP2_COMMAND="bzip2"
410 PORTAGE_BUNZIP2_COMMAND="bunzip2"
411 )
412fi
413
Brian Harringf539bc32012-02-06 00:18:37 -0800414if [ -z "${INITIALIZE_CHROOT}" ];then
Brian Harring35767822012-02-01 23:50:45 -0800415 info "chroot already initialized. Skipping..."
Brian Harringf539bc32012-02-06 00:18:37 -0800416else
Brian Harring35767822012-02-01 23:50:45 -0800417 # Run all the init stuff to setup the env.
Brian Harringf539bc32012-02-06 00:18:37 -0800418 init_setup
419fi
420
Brian Harring35767822012-02-01 23:50:45 -0800421# Add file to indicate that it is a chroot.
Mike Frysinger51930072014-04-05 13:01:45 -0400422# Add version of stage3 for update checks.
423echo "STAGE3=${FLAGS_stage3_path}" > "${CHROOT_STATE}"
Brian Harringf539bc32012-02-06 00:18:37 -0800424
Brian Harring35767822012-02-01 23:50:45 -0800425info "Updating portage"
Mike Frysinger66fd81f2012-02-28 13:13:20 -0500426early_enter_chroot emerge -uNv --quiet portage
Brian Harringf539bc32012-02-06 00:18:37 -0800427
Mike Frysinger9a2978c2013-08-04 11:38:43 -0400428# Clear out openrc if it's installed as we don't want it.
David Purselld35d7af2015-05-04 16:37:08 -0700429if [[ -e "${FLAGS_chroot}/usr/share/openrc" ]]; then
Mike Frysinger9a2978c2013-08-04 11:38:43 -0400430 info "Uninstalling openrc"
431 early_enter_chroot env CLEAN_DELAY=0 emerge -qC sys-apps/openrc
Mike Frysinger47e599e2013-10-28 13:30:12 -0400432 # Now update baselayout to get our functions.sh. The unmerge
433 # above removed our copy in the process.
434 early_enter_chroot emerge -uNvq sys-apps/baselayout
Mike Frysinger9a2978c2013-08-04 11:38:43 -0400435fi
436
David James31097c32013-11-06 18:56:59 -0800437# The stage3 contains an old version of Python. Upgrade it first so that
438# parallel_emerge (and chromite libs) can use the latest Python syntax.
439info "Updating python-2.x"
440early_enter_chroot emerge -uNvq =dev-lang/python-2*
441
Mike Frysinger78753af2014-10-14 01:15:32 -0400442# New versions of the stage3 have Python 3 set as the default. Make sure we
443# default to 2.x as our scripts are only compatible with Python 2. We leave
444# Python 3 installed though as we've started including it in our SDK.
David James31097c32013-11-06 18:56:59 -0800445early_enter_chroot eselect python set 1
Mike Frysinger83520dd2013-03-22 01:37:37 -0400446
Bertrand SIMONNET85c96c72014-08-20 12:10:48 -0700447# Add chromite into python path.
448# This needs to happen after the python update or the correct /usr/lib/python2.*
449# may not exist.
450for python_path in "${FLAGS_chroot}/usr/lib/"python2.*; do
451 python_path+="/site-packages"
452 sudo mkdir -p "${python_path}"
453 sudo ln -s -fT "${CHROOT_TRUNK_DIR}"/chromite "${python_path}"/chromite
454done
455
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100456info "Updating host toolchain"
David Purselld35d7af2015-05-04 16:37:08 -0700457if [[ ! -e "${FLAGS_chroot}/usr/bin/crossdev" ]]; then
Mike Frysinger5e07dc72014-11-05 14:40:04 -0500458 early_enter_chroot $EMERGE_CMD -uNv crossdev
459fi
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100460TOOLCHAIN_ARGS=( --deleteold )
David Purselld35d7af2015-05-04 16:37:08 -0700461if [[ "${FLAGS_usepkg}" == "${FLAGS_FALSE}" ]]; then
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100462 TOOLCHAIN_ARGS+=( --nousepkg )
463fi
464# Note: early_enter_chroot executes as root.
Brian Harring2499bfb2012-12-18 16:23:31 -0800465early_enter_chroot "${CHROOT_TRUNK_DIR}/chromite/bin/cros_setup_toolchains" \
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100466 --hostonly "${TOOLCHAIN_ARGS[@]}"
Brian Harringf539bc32012-02-06 00:18:37 -0800467
Mike Frysinger279f1032012-05-17 15:54:31 -0400468info "Running emerge curl sudo ..."
Mike Frysinger650bf872012-02-27 11:05:26 -0500469early_enter_chroot $EMERGE_CMD -uNv $USEPKG --select $EMERGE_JOBS \
Paul Drews8bae3b52012-10-10 11:18:13 -0700470 pbzip2 dev-libs/openssl net-misc/curl sudo
471
Brian Harringf539bc32012-02-06 00:18:37 -0800472if [ -n "${INITIALIZE_CHROOT}" ]; then
473 # If we're creating a new chroot, we also want to set it to the latest
474 # version.
Brian Harring35767822012-02-01 23:50:45 -0800475 enter_chroot \
David James47e0e8a2015-03-11 15:24:10 -0700476 "${CHROOT_TRUNK_DIR}/src/scripts/run_chroot_version_hooks" --init_latest
Brian Harringf539bc32012-02-06 00:18:37 -0800477fi
478
Brian Harring35767822012-02-01 23:50:45 -0800479# Update chroot.
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100480# Skip toolchain update because it already happened above, and the chroot is
481# not ready to emerge all cross toolchains.
482UPDATE_ARGS=( --skip_toolchain_update )
David Purselld35d7af2015-05-04 16:37:08 -0700483if [[ "${FLAGS_usepkg}" == "${FLAGS_TRUE}" ]]; then
Brian Harring35767822012-02-01 23:50:45 -0800484 UPDATE_ARGS+=( --usepkg )
Brian Harringf539bc32012-02-06 00:18:37 -0800485else
Brian Harring35767822012-02-01 23:50:45 -0800486 UPDATE_ARGS+=( --nousepkg )
Brian Harringf539bc32012-02-06 00:18:37 -0800487fi
David James184e3902012-02-23 20:19:28 -0800488if [[ "${FLAGS_jobs}" -ne -1 ]]; then
David Purselld35d7af2015-05-04 16:37:08 -0700489 UPDATE_ARGS+=( --jobs="${FLAGS_jobs}" )
David James184e3902012-02-23 20:19:28 -0800490fi
Brian Harring2499bfb2012-12-18 16:23:31 -0800491enter_chroot "${CHROOT_TRUNK_DIR}/src/scripts/update_chroot" "${UPDATE_ARGS[@]}"
Brian Harringf539bc32012-02-06 00:18:37 -0800492
Stefan Zager48c776b2013-10-09 13:01:25 -0700493# The java-config package atm does not support $ROOT. Select a default
494# VM ourselves until that gets fixed upstream.
495enter_chroot sudo java-config --set-system-vm 1
496
Brian Harring35767822012-02-01 23:50:45 -0800497CHROOT_EXAMPLE_OPT=""
498if [[ "$FLAGS_chroot" != "$DEFAULT_CHROOT_DIR" ]]; then
Brian Harringf539bc32012-02-06 00:18:37 -0800499 CHROOT_EXAMPLE_OPT="--chroot=$FLAGS_chroot"
500fi
501
Matt Tennant0a9d32d2012-07-30 16:51:37 -0700502command_completed
Brian Harringf539bc32012-02-06 00:18:37 -0800503
Brian Harring35767822012-02-01 23:50:45 -0800504cat <<EOF
Mike Frysingerbdc4fb12012-02-10 11:20:03 -0500505
506${CROS_LOG_PREFIX:-cros_sdk}: All set up. To enter the chroot, run:
507$ cros_sdk --enter $CHROOT_EXAMPLE_OPT
Brian Harring35767822012-02-01 23:50:45 -0800508
509CAUTION: Do *NOT* rm -rf the chroot directory; if there are stale bind
510mounts you may end up deleting your source tree too. To unmount and
511delete the chroot cleanly, use:
512$ cros_sdk --delete $CHROOT_EXAMPLE_OPT
Mike Frysingerbdc4fb12012-02-10 11:20:03 -0500513
Brian Harring35767822012-02-01 23:50:45 -0800514EOF
David James22dc2ba2012-11-29 15:46:58 -0800515
516warn_if_nfs "${SUDO_HOME}"