blob: dccb8726ad848c38aecef4f33b11960d19b41a12 [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."
Gilad Arnold8c8e0852015-06-03 16:19:16 -070046DEFINE_string toolchains_overlay_path "" \
47 "Use the toolchains overlay 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
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
Brian Harringf539bc32012-02-06 00:18:37 -080079USEPKG=""
80if [[ $FLAGS_usepkg -eq $FLAGS_TRUE ]]; then
81 # Use binary packages. Include all build-time dependencies,
82 # so as to avoid unnecessary differences between source
83 # and binary builds.
84 USEPKG="--getbinpkg --usepkg --with-bdeps y"
85fi
86
Bertrand SIMONNET4dda4f52015-03-19 13:40:58 -070087EMERGE_CMD="${CHROOT_TRUNK_DIR}/chromite/bin/parallel_emerge"
Brian Harringf539bc32012-02-06 00:18:37 -080088
Brian Harring35767822012-02-01 23:50:45 -080089ENTER_CHROOT_ARGS=(
90 CROS_WORKON_SRCROOT="$CHROOT_TRUNK"
David James76764882012-10-24 19:46:29 -070091 PORTAGE_USERNAME="${SUDO_USER}"
Brian Harring35767822012-02-01 23:50:45 -080092 IGNORE_PREFLIGHT_BINHOST="$IGNORE_PREFLIGHT_BINHOST"
93)
94
95# Invoke enter_chroot. This can only be used after sudo has been installed.
Mike Frysinger6b1abb22012-05-11 13:44:06 -040096enter_chroot() {
Brian Harring7b6f3772012-09-23 14:01:13 -070097 "$ENTER_CHROOT" --cache_dir "${FLAGS_cache_dir}" --chroot "$FLAGS_chroot" \
98 -- "${ENTER_CHROOT_ARGS[@]}" "$@"
Brian Harringf539bc32012-02-06 00:18:37 -080099}
100
Brian Harring35767822012-02-01 23:50:45 -0800101# Invoke enter_chroot running the command as root, and w/out sudo.
102# This should be used prior to sudo being merged.
Mike Frysingerba758452012-04-02 13:28:31 -0400103early_env=()
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400104early_enter_chroot() {
Brian Harring35767822012-02-01 23:50:45 -0800105 "$ENTER_CHROOT" --chroot "$FLAGS_chroot" --early_make_chroot \
Brian Harring7b6f3772012-09-23 14:01:13 -0700106 --cache_dir "${FLAGS_cache_dir}" \
Mike Frysingerba758452012-04-02 13:28:31 -0400107 -- "${ENTER_CHROOT_ARGS[@]}" "${early_env[@]}" "$@"
Brian Harringf539bc32012-02-06 00:18:37 -0800108}
109
Mike Frysinger4bb86452013-08-02 13:44:18 -0400110# Run a command within the chroot. The main usage of this is to avoid the
111# overhead of enter_chroot. It's when we do not need access to the source
112# tree, don't need the actual chroot profile env, and can run the command as
113# root. We do have to make sure PATH includes all the right programs as
114# found inside of the chroot since the environment outside of the chroot
115# might be insufficient (like distros with merged /bin /sbin and /usr).
David James76764882012-10-24 19:46:29 -0700116bare_chroot() {
David Purselld35d7af2015-05-04 16:37:08 -0700117 PATH="/bin:/sbin:/usr/bin:/usr/sbin:${PATH}" \
Mike Frysinger4bb86452013-08-02 13:44:18 -0400118 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..."
David James76764882012-10-24 19:46:29 -0700134 rm -rf "$FLAGS_chroot"
Brian Harring35767822012-02-01 23:50:45 -0800135 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.
David James76764882012-10-24 19:46:29 -0700141 rm -f "${FLAGS_chroot}/etc/localtime"
Brian Harringf539bc32012-02-06 00:18:37 -0800142 if [ -f /etc/localtime ] ; then
David James76764882012-10-24 19:46:29 -0700143 cp /etc/localtime "${FLAGS_chroot}/etc"
Brian Harringf539bc32012-02-06 00:18:37 -0800144 else
David James76764882012-10-24 19:46:29 -0700145 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..."
David Purselld35d7af2015-05-04 16:37:08 -0700148 # Add the necessary groups to the chroot.
149 # Duplicate GIDs are allowed here in order to ensure that the required
150 # groups are the same inside and outside the chroot.
151 # TODO(dpursell): Handle when PRIMARY_GROUP exists in the chroot already
152 # with a different GID; groupadd will not create the new GID in that case.
153 bare_chroot groupadd -f -o -g "${PRIMARY_GROUP_ID}" "${PRIMARY_GROUP}"
Brian Harringf539bc32012-02-06 00:18:37 -0800154 # Add ourselves as a user inside the chroot.
Brian Harringf539bc32012-02-06 00:18:37 -0800155 # We need the UID to match the host user's. This can conflict with
156 # a particular chroot UID. At the same time, the added user has to
157 # be a primary user for the given UID for sudo to work, which is
158 # determined by the order in /etc/passwd. Let's put ourselves on top
159 # of the file.
David Purselld35d7af2015-05-04 16:37:08 -0700160 bare_chroot useradd -o -G "${DEFGROUPS}" -g "${PRIMARY_GROUP}" \
161 -u "${SUDO_UID}" -s /bin/bash -m -c "${FULLNAME}" "${SUDO_USER}"
Brian Harringf539bc32012-02-06 00:18:37 -0800162 # Because passwd generally isn't sorted and the entry ended up at the
163 # bottom, it is safe to just take it and move it to top instead.
David James76764882012-10-24 19:46:29 -0700164 sed -e '1{h;d};$!{H;d};$G' -i "${FLAGS_chroot}/etc/passwd"
Brian Harringf539bc32012-02-06 00:18:37 -0800165}
166
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400167init_setup () {
Brian Harring35767822012-02-01 23:50:45 -0800168 info "Running init_setup()..."
David James76764882012-10-24 19:46:29 -0700169 mkdir -p -m 755 "${FLAGS_chroot}/usr" \
Mike Frysingerb45dbb52013-12-13 21:14:09 -0500170 "${FLAGS_chroot}${OVERLAYS_ROOT}" \
Mike Frysinger36beaaa2014-09-25 15:52:27 -0400171 "${FLAGS_chroot}"/"${CROSSDEV_OVERLAY}/metadata"
172 # Newer portage complains about bare overlays. Create the file that crossdev
173 # will also create later on.
174 cat <<EOF > "${FLAGS_chroot}/${CROSSDEV_OVERLAY}/metadata/layout.conf"
175# Autogenerated and managed by crossdev
176# Delete the above line if you want to manage this file yourself
177masters = portage-stable chromiumos
178repo-name = crossdev
179use-manifests = true
180thin-manifests = true
181EOF
Mike Frysingerb45dbb52013-12-13 21:14:09 -0500182 ln -sf "${CHROOT_TRUNK_DIR}/src/third_party/eclass-overlay" \
183 "${FLAGS_chroot}"/"${ECLASS_OVERLAY}"
Brian Harring2499bfb2012-12-18 16:23:31 -0800184 ln -sf "${CHROOT_TRUNK_DIR}/src/third_party/chromiumos-overlay" \
Brian Harringf539bc32012-02-06 00:18:37 -0800185 "${FLAGS_chroot}"/"${CHROOT_OVERLAY}"
Brian Harring2499bfb2012-12-18 16:23:31 -0800186 ln -sf "${CHROOT_TRUNK_DIR}/src/third_party/portage-stable" \
Brian Harringf539bc32012-02-06 00:18:37 -0800187 "${FLAGS_chroot}"/"${PORTAGE_STABLE_OVERLAY}"
Brian Harringf539bc32012-02-06 00:18:37 -0800188
Brian Harring35767822012-02-01 23:50:45 -0800189 # Some operations need an mtab.
Mike Frysingerb47a4ff2013-02-20 17:29:05 -0500190 ln -sfT /proc/mounts "${FLAGS_chroot}/etc/mtab"
Brian Harringf539bc32012-02-06 00:18:37 -0800191
192 # Set up sudoers. Inside the chroot, the user can sudo without a password.
193 # (Safe enough, since the only way into the chroot is to 'sudo chroot', so
194 # the user's already typed in one sudo password...)
195 # Make sure the sudoers.d subdir exists as older stage3 base images lack it.
David James76764882012-10-24 19:46:29 -0700196 mkdir -p "${FLAGS_chroot}/etc/sudoers.d"
Brian Harring06d3c2e2012-08-23 07:35:43 -0700197
198 # Use the standardized upgrade script to setup proxied vars.
David James76764882012-10-24 19:46:29 -0700199 load_environment_whitelist
200 bash -e "${SCRIPT_ROOT}/chroot_version_hooks.d/45_rewrite_sudoers.d" \
201 "${FLAGS_chroot}" "${SUDO_USER}" "${ENVIRONMENT_WHITELIST[@]}"
Brian Harring06d3c2e2012-08-23 07:35:43 -0700202
David James76764882012-10-24 19:46:29 -0700203 find "${FLAGS_chroot}/etc/"sudoers* -type f -exec chmod 0440 {} +
Brian Harring35767822012-02-01 23:50:45 -0800204 # Fix bad group for some.
David James76764882012-10-24 19:46:29 -0700205 chown -R root:root "${FLAGS_chroot}/etc/"sudoers*
Brian Harringf539bc32012-02-06 00:18:37 -0800206
Brian Harring35767822012-02-01 23:50:45 -0800207 info "Setting up hosts/resolv..."
208 # Copy config from outside chroot into chroot.
David James76764882012-10-24 19:46:29 -0700209 cp /etc/{hosts,resolv.conf} "$FLAGS_chroot/etc/"
210 chmod 0644 "$FLAGS_chroot"/etc/{hosts,resolv.conf}
Brian Harringf539bc32012-02-06 00:18:37 -0800211
212 # Setup host make.conf. This includes any overlay that we may be using
213 # and a pointer to pre-built packages.
Brian Harring35767822012-02-01 23:50:45 -0800214 # TODO: This should really be part of a profile in the portage.
215 info "Setting up /etc/make.*..."
Mike Frysinger53f79bb2014-04-05 22:27:52 -0400216 rm -f "${FLAGS_chroot}"/etc/{,portage/}make.{conf,profile}{,.catalyst}
Mike Frysingerc6e1ace2013-03-22 00:49:30 -0400217 mkdir -p "${FLAGS_chroot}/etc/portage"
David James76764882012-10-24 19:46:29 -0700218 ln -sf "${CHROOT_CONFIG}/make.conf.amd64-host" \
Brian Harringf539bc32012-02-06 00:18:37 -0800219 "${FLAGS_chroot}/etc/make.conf"
Mike Frysinger411b2422014-06-04 11:49:17 -0400220 ln -sf "${CHROOT_OVERLAY}/profiles/default/linux/amd64/10.0/sdk" \
Mike Frysingere9b40072015-05-19 05:07:04 -0400221 "${FLAGS_chroot}/etc/portage/make.profile"
Brian Harringf539bc32012-02-06 00:18:37 -0800222
Brian Harring35767822012-02-01 23:50:45 -0800223 # Create make.conf.user .
David James76764882012-10-24 19:46:29 -0700224 touch "${FLAGS_chroot}"/etc/make.conf.user
225 chmod 0644 "${FLAGS_chroot}"/etc/make.conf.user
Brian Harringf539bc32012-02-06 00:18:37 -0800226
227 # Create directories referred to by our conf files.
David James76764882012-10-24 19:46:29 -0700228 mkdir -p -m 775 "${FLAGS_chroot}/var/lib/portage/pkgs" \
Brian Harring7b6f3772012-09-23 14:01:13 -0700229 "${FLAGS_chroot}/var/cache/"chromeos-{cache,chrome} \
230 "${FLAGS_chroot}/etc/profile.d"
231
David James76764882012-10-24 19:46:29 -0700232 echo "export CHROMEOS_CACHEDIR=/var/cache/chromeos-cache" > \
233 "${FLAGS_chroot}/etc/profile.d/chromeos-cachedir.sh"
234 chmod 0644 "${FLAGS_chroot}/etc/profile.d/chromeos-cachedir.sh"
235 rm -rf "${FLAGS_chroot}/var/cache/distfiles"
236 ln -s chromeos-cache/distfiles "${FLAGS_chroot}/var/cache/distfiles"
Brian Harring36b102b2012-02-06 23:34:25 -0800237
238 # Run this from w/in the chroot so we use whatever uid/gid
239 # these are defined as w/in the chroot.
David James76764882012-10-24 19:46:29 -0700240 bare_chroot chown "${SUDO_USER}:portage" /var/cache/chromeos-chrome
Brian Harring7ee892d2012-02-02 09:33:10 -0800241
242 # These are created for compatibility while transitioning
243 # make.conf and friends over to the new location.
Brian Harring7b6f3772012-09-23 14:01:13 -0700244 # TODO(ferringb): remove this 01/13 or so.
David James76764882012-10-24 19:46:29 -0700245 ln -s ../../cache/chromeos-cache/distfiles/host \
Brian Harring7ee892d2012-02-02 09:33:10 -0800246 "${FLAGS_chroot}/var/lib/portage/distfiles"
David James76764882012-10-24 19:46:29 -0700247 ln -s ../../cache/chromeos-cache/distfiles/target \
Brian Harring7ee892d2012-02-02 09:33:10 -0800248 "${FLAGS_chroot}/var/lib/portage/distfiles-target"
Brian Harringf539bc32012-02-06 00:18:37 -0800249
Brian Harringf539bc32012-02-06 00:18:37 -0800250 # Add chromite/bin and depot_tools into the path globally; note that the
251 # chromite wrapper itself might also be found in depot_tools.
252 # We rely on 'env-update' getting called below.
253 target="${FLAGS_chroot}/etc/env.d/99chromiumos"
David James76764882012-10-24 19:46:29 -0700254 cat <<EOF > "${target}"
David Purselld35d7af2015-05-04 16:37:08 -0700255PATH="${CHROOT_TRUNK_DIR}/chromite/bin:${DEPOT_TOOLS_DIR}"
Brian Harring2499bfb2012-12-18 16:23:31 -0800256CROS_WORKON_SRCROOT="${CHROOT_TRUNK_DIR}"
David Purselld35d7af2015-05-04 16:37:08 -0700257PORTAGE_USERNAME="${SUDO_USER}"
Brian Harringf539bc32012-02-06 00:18:37 -0800258EOF
259
260 # TODO(zbehan): Configure stuff that is usually configured in postinst's,
Mike Frysingereb1a9b42012-03-28 16:21:04 -0400261 # but wasn't. Fix the postinst's.
Brian Harring35767822012-02-01 23:50:45 -0800262 info "Running post-inst configuration hacks"
263 early_enter_chroot env-update
Brian Harringf539bc32012-02-06 00:18:37 -0800264
Brian Harring35767822012-02-01 23:50:45 -0800265 # This is basically a sanity check of our chroot. If any of these
266 # don't exist, then either bind mounts have failed, an invocation
267 # from above is broke, or some assumption about the stage3 is no longer
268 # true.
Mike Frysingere9b40072015-05-19 05:07:04 -0400269 early_enter_chroot ls -l /etc/make.conf /etc/portage/make.profile \
Brian Harring35767822012-02-01 23:50:45 -0800270 /usr/local/portage/chromiumos/profiles/default/linux/amd64/10.0
Brian Harringf539bc32012-02-06 00:18:37 -0800271
272 target="${FLAGS_chroot}/etc/profile.d"
David James76764882012-10-24 19:46:29 -0700273 mkdir -p "${target}"
274 cat << EOF > "${target}/chromiumos-niceties.sh"
Brian Harringf539bc32012-02-06 00:18:37 -0800275# Niceties for interactive logins. (cr) denotes this is a chroot, the
276# __git_branch_ps1 prints current git branch in ./ . The $r behavior is to
277# make sure we don't reset the previous $? value which later formats in
278# $PS1 might rely on.
279PS1='\$(r=\$?; __git_branch_ps1 "(%s) "; exit \$r)'"\${PS1}"
280PS1="(cr) \${PS1}"
281EOF
282
283 # Select a small set of locales for the user if they haven't done so
284 # already. This makes glibc upgrades cheap by only generating a small
285 # set of locales. The ones listed here are basically for the buildbots
286 # which always assume these are available. This works in conjunction
287 # with `cros_sdk --enter`.
288 # http://crosbug.com/20378
289 local localegen="$FLAGS_chroot/etc/locale.gen"
290 if ! grep -q -v -e '^#' -e '^$' "${localegen}" ; then
David James76764882012-10-24 19:46:29 -0700291 cat <<EOF >> "${localegen}"
Brian Harringf539bc32012-02-06 00:18:37 -0800292en_US ISO-8859-1
293en_US.UTF-8 UTF-8
294EOF
295 fi
296
Brian Harring35767822012-02-01 23:50:45 -0800297 # Automatically change to scripts directory.
Brian Harringf539bc32012-02-06 00:18:37 -0800298 echo 'cd ${CHROOT_CWD:-~/trunk/src/scripts}' \
David Purselld35d7af2015-05-04 16:37:08 -0700299 | user_append "${FLAGS_chroot}/home/${SUDO_USER}/.bash_profile"
Brian Harringf539bc32012-02-06 00:18:37 -0800300
Brian Harring35767822012-02-01 23:50:45 -0800301 # Enable bash completion for build scripts.
Brian Harringf539bc32012-02-06 00:18:37 -0800302 echo ". ~/trunk/src/scripts/bash_completion" \
David Purselld35d7af2015-05-04 16:37:08 -0700303 | user_append "${FLAGS_chroot}/home/${SUDO_USER}/.bashrc"
Brian Harringf539bc32012-02-06 00:18:37 -0800304
Peter Mayoe18c7d42013-06-06 13:41:03 -0400305 if [[ "${SUDO_USER}" = "chrome-bot" && -d "${SUDO_HOME}/.ssh" ]]; then
Brian Harringf539bc32012-02-06 00:18:37 -0800306 # Copy ssh keys, so chroot'd chrome-bot can scp files from chrome-web.
Peter Mayo31056952013-06-06 16:22:55 -0400307 user_cp -rp "${SUDO_HOME}/.ssh" "$FLAGS_chroot/home/${SUDO_USER}/"
Brian Harringf539bc32012-02-06 00:18:37 -0800308 fi
309
David Purselld35d7af2015-05-04 16:37:08 -0700310 if [[ -f "${SUDO_HOME}/.cros_chroot_init" ]]; then
311 sudo -u "${SUDO_USER}" -- /bin/bash "${SUDO_HOME}/.cros_chroot_init" \
David James76764882012-10-24 19:46:29 -0700312 "${FLAGS_chroot}"
Luigi Semenzato2443fdd2012-05-29 10:34:04 -0700313 fi
Brian Harringf539bc32012-02-06 00:18:37 -0800314}
315
Gilad Arnold01d458b2015-05-28 06:41:08 -0700316unpack_tarball() {
317 local tarball_path="$1"
318 local dest_dir="$2"
319 local decompress
320 case "${tarball_path}" in
321 *.tbz2|*.tar.bz2) decompress=$(type -p pbzip2 || echo bzip2) ;;
322 *.tar.xz) decompress="xz" ;;
323 *) die "Unknown tarball compression: ${tarball_path}" ;;
324 esac
325 ${decompress} -dc "${tarball_path}" | tar -xp -C "${dest_dir}"
326}
327
Brian Harring35767822012-02-01 23:50:45 -0800328# Handle deleting an existing environment.
Brian Harringf539bc32012-02-06 00:18:37 -0800329if [[ $FLAGS_delete -eq $FLAGS_TRUE || \
Brian Harring35767822012-02-01 23:50:45 -0800330 $FLAGS_replace -eq $FLAGS_TRUE ]]; then
Brian Harringf539bc32012-02-06 00:18:37 -0800331 delete_existing
332 [[ $FLAGS_delete -eq $FLAGS_TRUE ]] && exit 0
333fi
334
335CHROOT_TRUNK="${CHROOT_TRUNK_DIR}"
336PORTAGE="${SRC_ROOT}/third_party/portage"
337OVERLAY="${SRC_ROOT}/third_party/chromiumos-overlay"
338CONFIG_DIR="${OVERLAY}/chromeos/config"
Brian Harring2499bfb2012-12-18 16:23:31 -0800339CHROOT_CONFIG="${CHROOT_TRUNK_DIR}/src/third_party/chromiumos-overlay/chromeos/config"
Mike Frysingerb45dbb52013-12-13 21:14:09 -0500340OVERLAYS_ROOT="/usr/local/portage"
341ECLASS_OVERLAY="${OVERLAYS_ROOT}/eclass-overlay"
342PORTAGE_STABLE_OVERLAY="${OVERLAYS_ROOT}/stable"
343CROSSDEV_OVERLAY="${OVERLAYS_ROOT}/crossdev"
344CHROOT_OVERLAY="${OVERLAYS_ROOT}/chromiumos"
Brian Harringf539bc32012-02-06 00:18:37 -0800345CHROOT_STATE="${FLAGS_chroot}/etc/debian_chroot"
Vincent Palatin5ce24062014-02-26 12:08:23 -0800346CHROOT_VERSION="${FLAGS_chroot}/etc/cros_chroot_version"
Brian Harringf539bc32012-02-06 00:18:37 -0800347
348# Pass proxy variables into the environment.
349for type in http ftp all; do
David Purselld35d7af2015-05-04 16:37:08 -0700350 value=$(env | grep "${type}_proxy" || true)
Brian Harringf539bc32012-02-06 00:18:37 -0800351 if [ -n "${value}" ]; then
352 CHROOT_PASSTHRU+=("$value")
353 fi
354done
355
Brian Harring35767822012-02-01 23:50:45 -0800356# Create the destination directory.
Brian Harringf539bc32012-02-06 00:18:37 -0800357mkdir -p "$FLAGS_chroot"
358
359echo
David Purselld35d7af2015-05-04 16:37:08 -0700360if [[ -f "${CHROOT_STATE}" ]]; then
Mike Frysinger51930072014-04-05 13:01:45 -0400361 info "stage3 already set up. Skipping..."
David Purselld35d7af2015-05-04 16:37:08 -0700362elif [[ -z "${FLAGS_stage3_path}" ]]; then
Mike Frysinger51930072014-04-05 13:01:45 -0400363 die_notrace "Please use --stage3_path when bootstrapping"
Brian Harringf539bc32012-02-06 00:18:37 -0800364else
Mike Frysinger51930072014-04-05 13:01:45 -0400365 info "Unpacking stage3..."
Gilad Arnold01d458b2015-05-28 06:41:08 -0700366 unpack_tarball "${FLAGS_stage3_path}" "${FLAGS_chroot}"
Gilad Arnold8c8e0852015-06-03 16:19:16 -0700367 if [[ -n "${FLAGS_toolchains_overlay_path}" ]]; then
368 info "Unpacking toolchains overlay..."
369 unpack_tarball "${FLAGS_toolchains_overlay_path}" "${FLAGS_chroot}"
Gilad Arnold01d458b2015-05-28 06:41:08 -0700370 fi
David James76764882012-10-24 19:46:29 -0700371 rm -f "$FLAGS_chroot/etc/"make.{globals,conf.user}
Brian Harringf539bc32012-02-06 00:18:37 -0800372fi
373
Vincent Palatin5ce24062014-02-26 12:08:23 -0800374# Ensure that we properly detect when we are inside the chroot.
375# We'll force this to the latest version at the end as needed.
David Purselld35d7af2015-05-04 16:37:08 -0700376if [[ ! -e "${CHROOT_VERSION}" ]]; then
Vincent Palatin5ce24062014-02-26 12:08:23 -0800377 echo "0" > "${CHROOT_VERSION}"
378fi
379
Brian Harring35767822012-02-01 23:50:45 -0800380# Set up users, if needed, before mkdir/mounts below.
Brian Harringf539bc32012-02-06 00:18:37 -0800381[ -f $CHROOT_STATE ] || init_users
382
Brian Harring2499bfb2012-12-18 16:23:31 -0800383# Reset internal vars to force them to the 'inside the chroot' value;
384# since user directories now exist, this can do the upgrade in place.
385set_chroot_trunk_dir "${FLAGS_chroot}" poppycock
386
Brian Harringf539bc32012-02-06 00:18:37 -0800387echo
Brian Harring35767822012-02-01 23:50:45 -0800388info "Setting up mounts..."
389# Set up necessary mounts and make sure we clean them up on exit.
Brian Harring2499bfb2012-12-18 16:23:31 -0800390mkdir -p "${FLAGS_chroot}/${CHROOT_TRUNK_DIR}" \
391 "${FLAGS_chroot}/${DEPOT_TOOLS_DIR}" "${FLAGS_chroot}/run"
Brian Harring35767822012-02-01 23:50:45 -0800392
J. Richard Barnettee80f6de2012-02-24 14:08:34 -0800393# Create a special /etc/make.conf.host_setup that we use to bootstrap
394# the chroot. The regular content for the file will be generated the
395# first time we invoke update_chroot (further down in this script).
396create_bootstrap_host_setup "${FLAGS_chroot}"
Brian Harringf539bc32012-02-06 00:18:37 -0800397
398if ! [ -f "$CHROOT_STATE" ];then
399 INITIALIZE_CHROOT=1
400fi
401
Mike Frysingerba758452012-04-02 13:28:31 -0400402if ! early_enter_chroot bash -c 'type -P pbzip2' >/dev/null ; then
403 # This chroot lacks pbzip2 early on, so we need to disable it.
404 early_env+=(
405 PORTAGE_BZIP2_COMMAND="bzip2"
406 PORTAGE_BUNZIP2_COMMAND="bunzip2"
407 )
408fi
409
Brian Harringf539bc32012-02-06 00:18:37 -0800410if [ -z "${INITIALIZE_CHROOT}" ];then
Brian Harring35767822012-02-01 23:50:45 -0800411 info "chroot already initialized. Skipping..."
Brian Harringf539bc32012-02-06 00:18:37 -0800412else
Brian Harring35767822012-02-01 23:50:45 -0800413 # Run all the init stuff to setup the env.
Brian Harringf539bc32012-02-06 00:18:37 -0800414 init_setup
415fi
416
Brian Harring35767822012-02-01 23:50:45 -0800417# Add file to indicate that it is a chroot.
Mike Frysinger51930072014-04-05 13:01:45 -0400418# Add version of stage3 for update checks.
419echo "STAGE3=${FLAGS_stage3_path}" > "${CHROOT_STATE}"
Brian Harringf539bc32012-02-06 00:18:37 -0800420
Brian Harring35767822012-02-01 23:50:45 -0800421info "Updating portage"
Mike Frysinger66fd81f2012-02-28 13:13:20 -0500422early_enter_chroot emerge -uNv --quiet portage
Brian Harringf539bc32012-02-06 00:18:37 -0800423
Mike Frysinger9a2978c2013-08-04 11:38:43 -0400424# Clear out openrc if it's installed as we don't want it.
David Purselld35d7af2015-05-04 16:37:08 -0700425if [[ -e "${FLAGS_chroot}/usr/share/openrc" ]]; then
Mike Frysinger9a2978c2013-08-04 11:38:43 -0400426 info "Uninstalling openrc"
427 early_enter_chroot env CLEAN_DELAY=0 emerge -qC sys-apps/openrc
Mike Frysinger47e599e2013-10-28 13:30:12 -0400428 # Now update baselayout to get our functions.sh. The unmerge
429 # above removed our copy in the process.
430 early_enter_chroot emerge -uNvq sys-apps/baselayout
Mike Frysinger9a2978c2013-08-04 11:38:43 -0400431fi
432
David James31097c32013-11-06 18:56:59 -0800433# The stage3 contains an old version of Python. Upgrade it first so that
434# parallel_emerge (and chromite libs) can use the latest Python syntax.
435info "Updating python-2.x"
436early_enter_chroot emerge -uNvq =dev-lang/python-2*
437
Mike Frysinger78753af2014-10-14 01:15:32 -0400438# New versions of the stage3 have Python 3 set as the default. Make sure we
439# default to 2.x as our scripts are only compatible with Python 2. We leave
440# Python 3 installed though as we've started including it in our SDK.
David James31097c32013-11-06 18:56:59 -0800441early_enter_chroot eselect python set 1
Mike Frysinger83520dd2013-03-22 01:37:37 -0400442
Bertrand SIMONNET85c96c72014-08-20 12:10:48 -0700443# Add chromite into python path.
444# This needs to happen after the python update or the correct /usr/lib/python2.*
445# may not exist.
446for python_path in "${FLAGS_chroot}/usr/lib/"python2.*; do
447 python_path+="/site-packages"
448 sudo mkdir -p "${python_path}"
449 sudo ln -s -fT "${CHROOT_TRUNK_DIR}"/chromite "${python_path}"/chromite
450done
451
Chirantan Ekboteaf1b9502016-07-14 11:29:16 -0700452# The stage3 contains an old version of ncurses, which causes a slot conflict
453# later when we try to setup the toolchains. Update it here to the latest
454# version, which gracefully handles the slot issues.
455info "Updating ncurses"
456early_enter_chroot emerge -uNvq sys-libs/ncurses
457
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100458info "Updating host toolchain"
David Purselld35d7af2015-05-04 16:37:08 -0700459if [[ ! -e "${FLAGS_chroot}/usr/bin/crossdev" ]]; then
Mike Frysinger5e07dc72014-11-05 14:40:04 -0500460 early_enter_chroot $EMERGE_CMD -uNv crossdev
461fi
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100462TOOLCHAIN_ARGS=( --deleteold )
David Purselld35d7af2015-05-04 16:37:08 -0700463if [[ "${FLAGS_usepkg}" == "${FLAGS_FALSE}" ]]; then
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100464 TOOLCHAIN_ARGS+=( --nousepkg )
465fi
466# Note: early_enter_chroot executes as root.
Brian Harring2499bfb2012-12-18 16:23:31 -0800467early_enter_chroot "${CHROOT_TRUNK_DIR}/chromite/bin/cros_setup_toolchains" \
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100468 --hostonly "${TOOLCHAIN_ARGS[@]}"
Brian Harringf539bc32012-02-06 00:18:37 -0800469
Ben Chan0c9da662017-05-15 17:29:51 -0700470info "Running emerge curl sudo gentoolkit ..."
Mike Frysinger650bf872012-02-27 11:05:26 -0500471early_enter_chroot $EMERGE_CMD -uNv $USEPKG --select $EMERGE_JOBS \
Ben Chan0c9da662017-05-15 17:29:51 -0700472 pbzip2 dev-libs/openssl net-misc/curl sudo app-portage/gentoolkit
473
474info "Updating Perl modules"
475early_enter_chroot \
476 "${CHROOT_TRUNK_DIR}/src/scripts/build_library/perl_rebuild.sh"
Paul Drews8bae3b52012-10-10 11:18:13 -0700477
Brian Harringf539bc32012-02-06 00:18:37 -0800478if [ -n "${INITIALIZE_CHROOT}" ]; then
479 # If we're creating a new chroot, we also want to set it to the latest
480 # version.
Brian Harring35767822012-02-01 23:50:45 -0800481 enter_chroot \
David James47e0e8a2015-03-11 15:24:10 -0700482 "${CHROOT_TRUNK_DIR}/src/scripts/run_chroot_version_hooks" --init_latest
Brian Harringf539bc32012-02-06 00:18:37 -0800483fi
484
Brian Harring35767822012-02-01 23:50:45 -0800485# Update chroot.
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100486# Skip toolchain update because it already happened above, and the chroot is
487# not ready to emerge all cross toolchains.
488UPDATE_ARGS=( --skip_toolchain_update )
David Purselld35d7af2015-05-04 16:37:08 -0700489if [[ "${FLAGS_usepkg}" == "${FLAGS_TRUE}" ]]; then
Brian Harring35767822012-02-01 23:50:45 -0800490 UPDATE_ARGS+=( --usepkg )
Brian Harringf539bc32012-02-06 00:18:37 -0800491else
Brian Harring35767822012-02-01 23:50:45 -0800492 UPDATE_ARGS+=( --nousepkg )
Brian Harringf539bc32012-02-06 00:18:37 -0800493fi
David James184e3902012-02-23 20:19:28 -0800494if [[ "${FLAGS_jobs}" -ne -1 ]]; then
David Purselld35d7af2015-05-04 16:37:08 -0700495 UPDATE_ARGS+=( --jobs="${FLAGS_jobs}" )
David James184e3902012-02-23 20:19:28 -0800496fi
Brian Harring2499bfb2012-12-18 16:23:31 -0800497enter_chroot "${CHROOT_TRUNK_DIR}/src/scripts/update_chroot" "${UPDATE_ARGS[@]}"
Brian Harringf539bc32012-02-06 00:18:37 -0800498
Stefan Zager48c776b2013-10-09 13:01:25 -0700499# The java-config package atm does not support $ROOT. Select a default
500# VM ourselves until that gets fixed upstream.
501enter_chroot sudo java-config --set-system-vm 1
502
Brian Harring35767822012-02-01 23:50:45 -0800503CHROOT_EXAMPLE_OPT=""
504if [[ "$FLAGS_chroot" != "$DEFAULT_CHROOT_DIR" ]]; then
Brian Harringf539bc32012-02-06 00:18:37 -0800505 CHROOT_EXAMPLE_OPT="--chroot=$FLAGS_chroot"
506fi
507
Matt Tennant0a9d32d2012-07-30 16:51:37 -0700508command_completed
Brian Harringf539bc32012-02-06 00:18:37 -0800509
Brian Harring35767822012-02-01 23:50:45 -0800510cat <<EOF
Mike Frysingerbdc4fb12012-02-10 11:20:03 -0500511
512${CROS_LOG_PREFIX:-cros_sdk}: All set up. To enter the chroot, run:
513$ cros_sdk --enter $CHROOT_EXAMPLE_OPT
Brian Harring35767822012-02-01 23:50:45 -0800514
515CAUTION: Do *NOT* rm -rf the chroot directory; if there are stale bind
516mounts you may end up deleting your source tree too. To unmount and
517delete the chroot cleanly, use:
518$ cros_sdk --delete $CHROOT_EXAMPLE_OPT
Mike Frysingerbdc4fb12012-02-10 11:20:03 -0500519
Brian Harring35767822012-02-01 23:50:45 -0800520EOF
David James22dc2ba2012-11-29 15:46:58 -0800521
522warn_if_nfs "${SUDO_HOME}"