blob: 3639662ab18dce4b1df1d723c2d80c5ba688c787 [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 Arnold01d458b2015-05-28 06:41:08 -070046DEFINE_string board_overlay_path "" \
47 "Use the board overlay located on this path."
David Purselld35d7af2015-05-04 16:37:08 -070048DEFINE_string workspace_root "" \
49 "The root of your workspace."
Brian Harring7b6f3772012-09-23 14:01:13 -070050DEFINE_string cache_dir "" "Directory to store caches within."
Brian Harringf539bc32012-02-06 00:18:37 -080051
Brian Harring35767822012-02-01 23:50:45 -080052# Parse command line flags.
Brian Harringf539bc32012-02-06 00:18:37 -080053FLAGS_HELP="usage: $SCRIPT_NAME [flags]"
54FLAGS "$@" || exit 1
55eval set -- "${FLAGS_ARGV}"
56check_flags_only_and_allow_null_arg "$@" && set --
57
Peter Mayo4411efe2012-09-21 04:41:27 -040058CROS_LOG_PREFIX=cros_sdk:make_chroot
David Purselld35d7af2015-05-04 16:37:08 -070059SUDO_HOME=$(eval echo ~"${SUDO_USER}")
Brian Harringf539bc32012-02-06 00:18:37 -080060
Brian Harringf539bc32012-02-06 00:18:37 -080061# Set the right umask for chroot creation.
62umask 022
63
64# Only now can we die on error. shflags functions leak non-zero error codes,
Brian Harring7f175a52012-03-02 05:37:00 -080065# so will die prematurely if 'switch_to_strict_mode' is specified before now.
Brian Harringf539bc32012-02-06 00:18:37 -080066# TODO: replace shflags with something less error-prone, or contribute a fix.
Brian Harring7f175a52012-03-02 05:37:00 -080067switch_to_strict_mode
Brian Harringf539bc32012-02-06 00:18:37 -080068
Brian Harring7b6f3772012-09-23 14:01:13 -070069[[ "${FLAGS_delete}" == "${FLAGS_FALSE}" ]] && \
70 [[ -z "${FLAGS_cache_dir}" ]] && \
71 die "--cache_dir is required"
72
J. Richard Barnettee80f6de2012-02-24 14:08:34 -080073. "${SCRIPT_ROOT}"/sdk_lib/make_conf_util.sh
74
David Purselld35d7af2015-05-04 16:37:08 -070075PRIMARY_GROUP=$(id -g -n "${SUDO_USER}")
76PRIMARY_GROUP_ID=$(id -g "${SUDO_USER}")
77
Brian Harringf539bc32012-02-06 00:18:37 -080078FULLNAME="ChromeOS Developer"
David Purselld35d7af2015-05-04 16:37:08 -070079DEFGROUPS="${PRIMARY_GROUP},adm,cdrom,floppy,audio,video,portage"
80
81# If a workspace was specified we want to make sure the user is in its group.
82if [[ -n "${FLAGS_workspace_root}" ]]; then
83 WORKSPACE_GROUP=$(stat -c "%G" "${FLAGS_workspace_root}")
84 WORKSPACE_GROUP_ID=$(getent group "${WORKSPACE_GROUP}" | cut -d: -f3)
85 # Duplicates in DEFGROUPS are fine, the useradd call handles them properly.
86 DEFGROUPS+=",${WORKSPACE_GROUP}"
87fi
Brian Harringf539bc32012-02-06 00:18:37 -080088
89USEPKG=""
90if [[ $FLAGS_usepkg -eq $FLAGS_TRUE ]]; then
91 # Use binary packages. Include all build-time dependencies,
92 # so as to avoid unnecessary differences between source
93 # and binary builds.
94 USEPKG="--getbinpkg --usepkg --with-bdeps y"
95fi
96
Bertrand SIMONNET4dda4f52015-03-19 13:40:58 -070097EMERGE_CMD="${CHROOT_TRUNK_DIR}/chromite/bin/parallel_emerge"
Brian Harringf539bc32012-02-06 00:18:37 -080098
Brian Harring35767822012-02-01 23:50:45 -080099ENTER_CHROOT_ARGS=(
100 CROS_WORKON_SRCROOT="$CHROOT_TRUNK"
David James76764882012-10-24 19:46:29 -0700101 PORTAGE_USERNAME="${SUDO_USER}"
Brian Harring35767822012-02-01 23:50:45 -0800102 IGNORE_PREFLIGHT_BINHOST="$IGNORE_PREFLIGHT_BINHOST"
103)
104
105# Invoke enter_chroot. This can only be used after sudo has been installed.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400106enter_chroot() {
Brian Harring7b6f3772012-09-23 14:01:13 -0700107 "$ENTER_CHROOT" --cache_dir "${FLAGS_cache_dir}" --chroot "$FLAGS_chroot" \
108 -- "${ENTER_CHROOT_ARGS[@]}" "$@"
Brian Harringf539bc32012-02-06 00:18:37 -0800109}
110
Brian Harring35767822012-02-01 23:50:45 -0800111# Invoke enter_chroot running the command as root, and w/out sudo.
112# This should be used prior to sudo being merged.
Mike Frysingerba758452012-04-02 13:28:31 -0400113early_env=()
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400114early_enter_chroot() {
Brian Harring35767822012-02-01 23:50:45 -0800115 "$ENTER_CHROOT" --chroot "$FLAGS_chroot" --early_make_chroot \
Brian Harring7b6f3772012-09-23 14:01:13 -0700116 --cache_dir "${FLAGS_cache_dir}" \
Mike Frysingerba758452012-04-02 13:28:31 -0400117 -- "${ENTER_CHROOT_ARGS[@]}" "${early_env[@]}" "$@"
Brian Harringf539bc32012-02-06 00:18:37 -0800118}
119
Mike Frysinger4bb86452013-08-02 13:44:18 -0400120# Run a command within the chroot. The main usage of this is to avoid the
121# overhead of enter_chroot. It's when we do not need access to the source
122# tree, don't need the actual chroot profile env, and can run the command as
123# root. We do have to make sure PATH includes all the right programs as
124# found inside of the chroot since the environment outside of the chroot
125# might be insufficient (like distros with merged /bin /sbin and /usr).
David James76764882012-10-24 19:46:29 -0700126bare_chroot() {
David Purselld35d7af2015-05-04 16:37:08 -0700127 PATH="/bin:/sbin:/usr/bin:/usr/sbin:${PATH}" \
Mike Frysinger4bb86452013-08-02 13:44:18 -0400128 chroot "${FLAGS_chroot}" "$@"
Brian Harringf539bc32012-02-06 00:18:37 -0800129}
130
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400131cleanup() {
Brian Harringf539bc32012-02-06 00:18:37 -0800132 # Clean up mounts
133 safe_umount_tree "${FLAGS_chroot}"
134}
135
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400136delete_existing() {
Brian Harring35767822012-02-01 23:50:45 -0800137 # Delete old chroot dir.
138 if [[ ! -e "$FLAGS_chroot" ]]; then
139 return
Brian Harringf539bc32012-02-06 00:18:37 -0800140 fi
Brian Harring35767822012-02-01 23:50:45 -0800141 info "Cleaning up old mount points..."
142 cleanup
143 info "Deleting $FLAGS_chroot..."
David James76764882012-10-24 19:46:29 -0700144 rm -rf "$FLAGS_chroot"
Brian Harring35767822012-02-01 23:50:45 -0800145 info "Done."
Brian Harringf539bc32012-02-06 00:18:37 -0800146}
147
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400148init_users () {
Brian Harring35767822012-02-01 23:50:45 -0800149 info "Set timezone..."
150 # date +%Z has trouble with daylight time, so use host's info.
David James76764882012-10-24 19:46:29 -0700151 rm -f "${FLAGS_chroot}/etc/localtime"
Brian Harringf539bc32012-02-06 00:18:37 -0800152 if [ -f /etc/localtime ] ; then
David James76764882012-10-24 19:46:29 -0700153 cp /etc/localtime "${FLAGS_chroot}/etc"
Brian Harringf539bc32012-02-06 00:18:37 -0800154 else
David James76764882012-10-24 19:46:29 -0700155 ln -sf /usr/share/zoneinfo/PST8PDT "${FLAGS_chroot}/etc/localtime"
Brian Harringf539bc32012-02-06 00:18:37 -0800156 fi
Brian Harring35767822012-02-01 23:50:45 -0800157 info "Adding user/group..."
David Purselld35d7af2015-05-04 16:37:08 -0700158 # Add the necessary groups to the chroot.
159 # Duplicate GIDs are allowed here in order to ensure that the required
160 # groups are the same inside and outside the chroot.
161 # TODO(dpursell): Handle when PRIMARY_GROUP exists in the chroot already
162 # with a different GID; groupadd will not create the new GID in that case.
163 bare_chroot groupadd -f -o -g "${PRIMARY_GROUP_ID}" "${PRIMARY_GROUP}"
164 if [[ -n "${WORKSPACE_GROUP}" ]]; then
165 # groupadd -f is fine (has no effect) if this group+ID already exists.
166 # TODO(dpursell): Handle when WORKSPACE_GROUP exists in the chroot already
167 # with a different GID; groupadd will not create the new GID in that case.
168 bare_chroot groupadd -f -o -g "${WORKSPACE_GROUP_ID}" "${WORKSPACE_GROUP}"
169 fi
Brian Harringf539bc32012-02-06 00:18:37 -0800170 # Add ourselves as a user inside the chroot.
Brian Harringf539bc32012-02-06 00:18:37 -0800171 # We need the UID to match the host user's. This can conflict with
172 # a particular chroot UID. At the same time, the added user has to
173 # be a primary user for the given UID for sudo to work, which is
174 # determined by the order in /etc/passwd. Let's put ourselves on top
175 # of the file.
David Purselld35d7af2015-05-04 16:37:08 -0700176 bare_chroot useradd -o -G "${DEFGROUPS}" -g "${PRIMARY_GROUP}" \
177 -u "${SUDO_UID}" -s /bin/bash -m -c "${FULLNAME}" "${SUDO_USER}"
Brian Harringf539bc32012-02-06 00:18:37 -0800178 # Because passwd generally isn't sorted and the entry ended up at the
179 # bottom, it is safe to just take it and move it to top instead.
David James76764882012-10-24 19:46:29 -0700180 sed -e '1{h;d};$!{H;d};$G' -i "${FLAGS_chroot}/etc/passwd"
Brian Harringf539bc32012-02-06 00:18:37 -0800181}
182
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400183init_setup () {
Brian Harring35767822012-02-01 23:50:45 -0800184 info "Running init_setup()..."
David James76764882012-10-24 19:46:29 -0700185 mkdir -p -m 755 "${FLAGS_chroot}/usr" \
Mike Frysingerb45dbb52013-12-13 21:14:09 -0500186 "${FLAGS_chroot}${OVERLAYS_ROOT}" \
Mike Frysinger36beaaa2014-09-25 15:52:27 -0400187 "${FLAGS_chroot}"/"${CROSSDEV_OVERLAY}/metadata"
188 # Newer portage complains about bare overlays. Create the file that crossdev
189 # will also create later on.
190 cat <<EOF > "${FLAGS_chroot}/${CROSSDEV_OVERLAY}/metadata/layout.conf"
191# Autogenerated and managed by crossdev
192# Delete the above line if you want to manage this file yourself
193masters = portage-stable chromiumos
194repo-name = crossdev
195use-manifests = true
196thin-manifests = true
197EOF
Mike Frysingerb45dbb52013-12-13 21:14:09 -0500198 ln -sf "${CHROOT_TRUNK_DIR}/src/third_party/eclass-overlay" \
199 "${FLAGS_chroot}"/"${ECLASS_OVERLAY}"
Brian Harring2499bfb2012-12-18 16:23:31 -0800200 ln -sf "${CHROOT_TRUNK_DIR}/src/third_party/chromiumos-overlay" \
Brian Harringf539bc32012-02-06 00:18:37 -0800201 "${FLAGS_chroot}"/"${CHROOT_OVERLAY}"
Brian Harring2499bfb2012-12-18 16:23:31 -0800202 ln -sf "${CHROOT_TRUNK_DIR}/src/third_party/portage-stable" \
Brian Harringf539bc32012-02-06 00:18:37 -0800203 "${FLAGS_chroot}"/"${PORTAGE_STABLE_OVERLAY}"
Brian Harringf539bc32012-02-06 00:18:37 -0800204
Brian Harring35767822012-02-01 23:50:45 -0800205 # Some operations need an mtab.
Mike Frysingerb47a4ff2013-02-20 17:29:05 -0500206 ln -sfT /proc/mounts "${FLAGS_chroot}/etc/mtab"
Brian Harringf539bc32012-02-06 00:18:37 -0800207
208 # Set up sudoers. Inside the chroot, the user can sudo without a password.
209 # (Safe enough, since the only way into the chroot is to 'sudo chroot', so
210 # the user's already typed in one sudo password...)
211 # Make sure the sudoers.d subdir exists as older stage3 base images lack it.
David James76764882012-10-24 19:46:29 -0700212 mkdir -p "${FLAGS_chroot}/etc/sudoers.d"
Brian Harring06d3c2e2012-08-23 07:35:43 -0700213
214 # Use the standardized upgrade script to setup proxied vars.
David James76764882012-10-24 19:46:29 -0700215 load_environment_whitelist
216 bash -e "${SCRIPT_ROOT}/chroot_version_hooks.d/45_rewrite_sudoers.d" \
217 "${FLAGS_chroot}" "${SUDO_USER}" "${ENVIRONMENT_WHITELIST[@]}"
Brian Harring06d3c2e2012-08-23 07:35:43 -0700218
David James76764882012-10-24 19:46:29 -0700219 find "${FLAGS_chroot}/etc/"sudoers* -type f -exec chmod 0440 {} +
Brian Harring35767822012-02-01 23:50:45 -0800220 # Fix bad group for some.
David James76764882012-10-24 19:46:29 -0700221 chown -R root:root "${FLAGS_chroot}/etc/"sudoers*
Brian Harringf539bc32012-02-06 00:18:37 -0800222
Brian Harring35767822012-02-01 23:50:45 -0800223 info "Setting up hosts/resolv..."
224 # Copy config from outside chroot into chroot.
David James76764882012-10-24 19:46:29 -0700225 cp /etc/{hosts,resolv.conf} "$FLAGS_chroot/etc/"
226 chmod 0644 "$FLAGS_chroot"/etc/{hosts,resolv.conf}
Brian Harringf539bc32012-02-06 00:18:37 -0800227
228 # Setup host make.conf. This includes any overlay that we may be using
229 # and a pointer to pre-built packages.
Brian Harring35767822012-02-01 23:50:45 -0800230 # TODO: This should really be part of a profile in the portage.
231 info "Setting up /etc/make.*..."
Mike Frysinger53f79bb2014-04-05 22:27:52 -0400232 rm -f "${FLAGS_chroot}"/etc/{,portage/}make.{conf,profile}{,.catalyst}
Mike Frysingerc6e1ace2013-03-22 00:49:30 -0400233 mkdir -p "${FLAGS_chroot}/etc/portage"
David James76764882012-10-24 19:46:29 -0700234 ln -sf "${CHROOT_CONFIG}/make.conf.amd64-host" \
Brian Harringf539bc32012-02-06 00:18:37 -0800235 "${FLAGS_chroot}/etc/make.conf"
Mike Frysinger411b2422014-06-04 11:49:17 -0400236 ln -sf "${CHROOT_OVERLAY}/profiles/default/linux/amd64/10.0/sdk" \
Mike Frysingere9b40072015-05-19 05:07:04 -0400237 "${FLAGS_chroot}/etc/portage/make.profile"
Brian Harringf539bc32012-02-06 00:18:37 -0800238
Brian Harring35767822012-02-01 23:50:45 -0800239 # Create make.conf.user .
David James76764882012-10-24 19:46:29 -0700240 touch "${FLAGS_chroot}"/etc/make.conf.user
241 chmod 0644 "${FLAGS_chroot}"/etc/make.conf.user
Brian Harringf539bc32012-02-06 00:18:37 -0800242
243 # Create directories referred to by our conf files.
David James76764882012-10-24 19:46:29 -0700244 mkdir -p -m 775 "${FLAGS_chroot}/var/lib/portage/pkgs" \
Brian Harring7b6f3772012-09-23 14:01:13 -0700245 "${FLAGS_chroot}/var/cache/"chromeos-{cache,chrome} \
246 "${FLAGS_chroot}/etc/profile.d"
247
David James76764882012-10-24 19:46:29 -0700248 echo "export CHROMEOS_CACHEDIR=/var/cache/chromeos-cache" > \
249 "${FLAGS_chroot}/etc/profile.d/chromeos-cachedir.sh"
250 chmod 0644 "${FLAGS_chroot}/etc/profile.d/chromeos-cachedir.sh"
251 rm -rf "${FLAGS_chroot}/var/cache/distfiles"
252 ln -s chromeos-cache/distfiles "${FLAGS_chroot}/var/cache/distfiles"
Brian Harring36b102b2012-02-06 23:34:25 -0800253
254 # Run this from w/in the chroot so we use whatever uid/gid
255 # these are defined as w/in the chroot.
David James76764882012-10-24 19:46:29 -0700256 bare_chroot chown "${SUDO_USER}:portage" /var/cache/chromeos-chrome
Brian Harring7ee892d2012-02-02 09:33:10 -0800257
258 # These are created for compatibility while transitioning
259 # make.conf and friends over to the new location.
Brian Harring7b6f3772012-09-23 14:01:13 -0700260 # TODO(ferringb): remove this 01/13 or so.
David James76764882012-10-24 19:46:29 -0700261 ln -s ../../cache/chromeos-cache/distfiles/host \
Brian Harring7ee892d2012-02-02 09:33:10 -0800262 "${FLAGS_chroot}/var/lib/portage/distfiles"
David James76764882012-10-24 19:46:29 -0700263 ln -s ../../cache/chromeos-cache/distfiles/target \
Brian Harring7ee892d2012-02-02 09:33:10 -0800264 "${FLAGS_chroot}/var/lib/portage/distfiles-target"
Brian Harringf539bc32012-02-06 00:18:37 -0800265
Brian Harringf539bc32012-02-06 00:18:37 -0800266 # Add chromite/bin and depot_tools into the path globally; note that the
267 # chromite wrapper itself might also be found in depot_tools.
268 # We rely on 'env-update' getting called below.
269 target="${FLAGS_chroot}/etc/env.d/99chromiumos"
David James76764882012-10-24 19:46:29 -0700270 cat <<EOF > "${target}"
David Purselld35d7af2015-05-04 16:37:08 -0700271PATH="${CHROOT_TRUNK_DIR}/chromite/bin:${DEPOT_TOOLS_DIR}"
Brian Harring2499bfb2012-12-18 16:23:31 -0800272CROS_WORKON_SRCROOT="${CHROOT_TRUNK_DIR}"
David Purselld35d7af2015-05-04 16:37:08 -0700273PORTAGE_USERNAME="${SUDO_USER}"
Brian Harringf539bc32012-02-06 00:18:37 -0800274EOF
275
276 # TODO(zbehan): Configure stuff that is usually configured in postinst's,
Mike Frysingereb1a9b42012-03-28 16:21:04 -0400277 # but wasn't. Fix the postinst's.
Brian Harring35767822012-02-01 23:50:45 -0800278 info "Running post-inst configuration hacks"
279 early_enter_chroot env-update
Brian Harringf539bc32012-02-06 00:18:37 -0800280
Brian Harring35767822012-02-01 23:50:45 -0800281 # This is basically a sanity check of our chroot. If any of these
282 # don't exist, then either bind mounts have failed, an invocation
283 # from above is broke, or some assumption about the stage3 is no longer
284 # true.
Mike Frysingere9b40072015-05-19 05:07:04 -0400285 early_enter_chroot ls -l /etc/make.conf /etc/portage/make.profile \
Brian Harring35767822012-02-01 23:50:45 -0800286 /usr/local/portage/chromiumos/profiles/default/linux/amd64/10.0
Brian Harringf539bc32012-02-06 00:18:37 -0800287
288 target="${FLAGS_chroot}/etc/profile.d"
David James76764882012-10-24 19:46:29 -0700289 mkdir -p "${target}"
290 cat << EOF > "${target}/chromiumos-niceties.sh"
Brian Harringf539bc32012-02-06 00:18:37 -0800291# Niceties for interactive logins. (cr) denotes this is a chroot, the
292# __git_branch_ps1 prints current git branch in ./ . The $r behavior is to
293# make sure we don't reset the previous $? value which later formats in
294# $PS1 might rely on.
295PS1='\$(r=\$?; __git_branch_ps1 "(%s) "; exit \$r)'"\${PS1}"
296PS1="(cr) \${PS1}"
297EOF
298
299 # Select a small set of locales for the user if they haven't done so
300 # already. This makes glibc upgrades cheap by only generating a small
301 # set of locales. The ones listed here are basically for the buildbots
302 # which always assume these are available. This works in conjunction
303 # with `cros_sdk --enter`.
304 # http://crosbug.com/20378
305 local localegen="$FLAGS_chroot/etc/locale.gen"
306 if ! grep -q -v -e '^#' -e '^$' "${localegen}" ; then
David James76764882012-10-24 19:46:29 -0700307 cat <<EOF >> "${localegen}"
Brian Harringf539bc32012-02-06 00:18:37 -0800308en_US ISO-8859-1
309en_US.UTF-8 UTF-8
310EOF
311 fi
312
Brian Harring35767822012-02-01 23:50:45 -0800313 # Automatically change to scripts directory.
Brian Harringf539bc32012-02-06 00:18:37 -0800314 echo 'cd ${CHROOT_CWD:-~/trunk/src/scripts}' \
David Purselld35d7af2015-05-04 16:37:08 -0700315 | user_append "${FLAGS_chroot}/home/${SUDO_USER}/.bash_profile"
Brian Harringf539bc32012-02-06 00:18:37 -0800316
Brian Harring35767822012-02-01 23:50:45 -0800317 # Enable bash completion for build scripts.
Brian Harringf539bc32012-02-06 00:18:37 -0800318 echo ". ~/trunk/src/scripts/bash_completion" \
David Purselld35d7af2015-05-04 16:37:08 -0700319 | user_append "${FLAGS_chroot}/home/${SUDO_USER}/.bashrc"
Brian Harringf539bc32012-02-06 00:18:37 -0800320
Peter Mayoe18c7d42013-06-06 13:41:03 -0400321 if [[ "${SUDO_USER}" = "chrome-bot" && -d "${SUDO_HOME}/.ssh" ]]; then
Brian Harringf539bc32012-02-06 00:18:37 -0800322 # Copy ssh keys, so chroot'd chrome-bot can scp files from chrome-web.
Peter Mayo31056952013-06-06 16:22:55 -0400323 user_cp -rp "${SUDO_HOME}/.ssh" "$FLAGS_chroot/home/${SUDO_USER}/"
Brian Harringf539bc32012-02-06 00:18:37 -0800324 fi
325
David Purselld35d7af2015-05-04 16:37:08 -0700326 if [[ -f "${SUDO_HOME}/.cros_chroot_init" ]]; then
327 sudo -u "${SUDO_USER}" -- /bin/bash "${SUDO_HOME}/.cros_chroot_init" \
David James76764882012-10-24 19:46:29 -0700328 "${FLAGS_chroot}"
Luigi Semenzato2443fdd2012-05-29 10:34:04 -0700329 fi
Brian Harringf539bc32012-02-06 00:18:37 -0800330}
331
Gilad Arnold01d458b2015-05-28 06:41:08 -0700332unpack_tarball() {
333 local tarball_path="$1"
334 local dest_dir="$2"
335 local decompress
336 case "${tarball_path}" in
337 *.tbz2|*.tar.bz2) decompress=$(type -p pbzip2 || echo bzip2) ;;
338 *.tar.xz) decompress="xz" ;;
339 *) die "Unknown tarball compression: ${tarball_path}" ;;
340 esac
341 ${decompress} -dc "${tarball_path}" | tar -xp -C "${dest_dir}"
342}
343
Brian Harring35767822012-02-01 23:50:45 -0800344# Handle deleting an existing environment.
Brian Harringf539bc32012-02-06 00:18:37 -0800345if [[ $FLAGS_delete -eq $FLAGS_TRUE || \
Brian Harring35767822012-02-01 23:50:45 -0800346 $FLAGS_replace -eq $FLAGS_TRUE ]]; then
Brian Harringf539bc32012-02-06 00:18:37 -0800347 delete_existing
348 [[ $FLAGS_delete -eq $FLAGS_TRUE ]] && exit 0
349fi
350
351CHROOT_TRUNK="${CHROOT_TRUNK_DIR}"
352PORTAGE="${SRC_ROOT}/third_party/portage"
353OVERLAY="${SRC_ROOT}/third_party/chromiumos-overlay"
354CONFIG_DIR="${OVERLAY}/chromeos/config"
Brian Harring2499bfb2012-12-18 16:23:31 -0800355CHROOT_CONFIG="${CHROOT_TRUNK_DIR}/src/third_party/chromiumos-overlay/chromeos/config"
Mike Frysingerb45dbb52013-12-13 21:14:09 -0500356OVERLAYS_ROOT="/usr/local/portage"
357ECLASS_OVERLAY="${OVERLAYS_ROOT}/eclass-overlay"
358PORTAGE_STABLE_OVERLAY="${OVERLAYS_ROOT}/stable"
359CROSSDEV_OVERLAY="${OVERLAYS_ROOT}/crossdev"
360CHROOT_OVERLAY="${OVERLAYS_ROOT}/chromiumos"
Brian Harringf539bc32012-02-06 00:18:37 -0800361CHROOT_STATE="${FLAGS_chroot}/etc/debian_chroot"
Vincent Palatin5ce24062014-02-26 12:08:23 -0800362CHROOT_VERSION="${FLAGS_chroot}/etc/cros_chroot_version"
Brian Harringf539bc32012-02-06 00:18:37 -0800363
364# Pass proxy variables into the environment.
365for type in http ftp all; do
David Purselld35d7af2015-05-04 16:37:08 -0700366 value=$(env | grep "${type}_proxy" || true)
Brian Harringf539bc32012-02-06 00:18:37 -0800367 if [ -n "${value}" ]; then
368 CHROOT_PASSTHRU+=("$value")
369 fi
370done
371
Brian Harring35767822012-02-01 23:50:45 -0800372# Create the destination directory.
Brian Harringf539bc32012-02-06 00:18:37 -0800373mkdir -p "$FLAGS_chroot"
374
375echo
David Purselld35d7af2015-05-04 16:37:08 -0700376if [[ -f "${CHROOT_STATE}" ]]; then
Mike Frysinger51930072014-04-05 13:01:45 -0400377 info "stage3 already set up. Skipping..."
David Purselld35d7af2015-05-04 16:37:08 -0700378elif [[ -z "${FLAGS_stage3_path}" ]]; then
Mike Frysinger51930072014-04-05 13:01:45 -0400379 die_notrace "Please use --stage3_path when bootstrapping"
Brian Harringf539bc32012-02-06 00:18:37 -0800380else
Mike Frysinger51930072014-04-05 13:01:45 -0400381 info "Unpacking stage3..."
Gilad Arnold01d458b2015-05-28 06:41:08 -0700382 unpack_tarball "${FLAGS_stage3_path}" "${FLAGS_chroot}"
383 if [[ -n "${FLAGS_board_overlay_path}" ]]; then
384 info "Unpacking board overlay..."
385 unpack_tarball "${FLAGS_board_overlay_path}" "${FLAGS_chroot}"
386 fi
David James76764882012-10-24 19:46:29 -0700387 rm -f "$FLAGS_chroot/etc/"make.{globals,conf.user}
Brian Harringf539bc32012-02-06 00:18:37 -0800388fi
389
Vincent Palatin5ce24062014-02-26 12:08:23 -0800390# Ensure that we properly detect when we are inside the chroot.
391# We'll force this to the latest version at the end as needed.
David Purselld35d7af2015-05-04 16:37:08 -0700392if [[ ! -e "${CHROOT_VERSION}" ]]; then
Vincent Palatin5ce24062014-02-26 12:08:23 -0800393 echo "0" > "${CHROOT_VERSION}"
394fi
395
Brian Harring35767822012-02-01 23:50:45 -0800396# Set up users, if needed, before mkdir/mounts below.
Brian Harringf539bc32012-02-06 00:18:37 -0800397[ -f $CHROOT_STATE ] || init_users
398
Brian Harring2499bfb2012-12-18 16:23:31 -0800399# Reset internal vars to force them to the 'inside the chroot' value;
400# since user directories now exist, this can do the upgrade in place.
401set_chroot_trunk_dir "${FLAGS_chroot}" poppycock
402
Brian Harringf539bc32012-02-06 00:18:37 -0800403echo
Brian Harring35767822012-02-01 23:50:45 -0800404info "Setting up mounts..."
405# Set up necessary mounts and make sure we clean them up on exit.
Brian Harring2499bfb2012-12-18 16:23:31 -0800406mkdir -p "${FLAGS_chroot}/${CHROOT_TRUNK_DIR}" \
407 "${FLAGS_chroot}/${DEPOT_TOOLS_DIR}" "${FLAGS_chroot}/run"
Brian Harring35767822012-02-01 23:50:45 -0800408
J. Richard Barnettee80f6de2012-02-24 14:08:34 -0800409# Create a special /etc/make.conf.host_setup that we use to bootstrap
410# the chroot. The regular content for the file will be generated the
411# first time we invoke update_chroot (further down in this script).
412create_bootstrap_host_setup "${FLAGS_chroot}"
Brian Harringf539bc32012-02-06 00:18:37 -0800413
414if ! [ -f "$CHROOT_STATE" ];then
415 INITIALIZE_CHROOT=1
416fi
417
Mike Frysingerba758452012-04-02 13:28:31 -0400418if ! early_enter_chroot bash -c 'type -P pbzip2' >/dev/null ; then
419 # This chroot lacks pbzip2 early on, so we need to disable it.
420 early_env+=(
421 PORTAGE_BZIP2_COMMAND="bzip2"
422 PORTAGE_BUNZIP2_COMMAND="bunzip2"
423 )
424fi
425
Brian Harringf539bc32012-02-06 00:18:37 -0800426if [ -z "${INITIALIZE_CHROOT}" ];then
Brian Harring35767822012-02-01 23:50:45 -0800427 info "chroot already initialized. Skipping..."
Brian Harringf539bc32012-02-06 00:18:37 -0800428else
Brian Harring35767822012-02-01 23:50:45 -0800429 # Run all the init stuff to setup the env.
Brian Harringf539bc32012-02-06 00:18:37 -0800430 init_setup
431fi
432
Brian Harring35767822012-02-01 23:50:45 -0800433# Add file to indicate that it is a chroot.
Mike Frysinger51930072014-04-05 13:01:45 -0400434# Add version of stage3 for update checks.
435echo "STAGE3=${FLAGS_stage3_path}" > "${CHROOT_STATE}"
Brian Harringf539bc32012-02-06 00:18:37 -0800436
Brian Harring35767822012-02-01 23:50:45 -0800437info "Updating portage"
Mike Frysinger66fd81f2012-02-28 13:13:20 -0500438early_enter_chroot emerge -uNv --quiet portage
Brian Harringf539bc32012-02-06 00:18:37 -0800439
Mike Frysinger9a2978c2013-08-04 11:38:43 -0400440# Clear out openrc if it's installed as we don't want it.
David Purselld35d7af2015-05-04 16:37:08 -0700441if [[ -e "${FLAGS_chroot}/usr/share/openrc" ]]; then
Mike Frysinger9a2978c2013-08-04 11:38:43 -0400442 info "Uninstalling openrc"
443 early_enter_chroot env CLEAN_DELAY=0 emerge -qC sys-apps/openrc
Mike Frysinger47e599e2013-10-28 13:30:12 -0400444 # Now update baselayout to get our functions.sh. The unmerge
445 # above removed our copy in the process.
446 early_enter_chroot emerge -uNvq sys-apps/baselayout
Mike Frysinger9a2978c2013-08-04 11:38:43 -0400447fi
448
David James31097c32013-11-06 18:56:59 -0800449# The stage3 contains an old version of Python. Upgrade it first so that
450# parallel_emerge (and chromite libs) can use the latest Python syntax.
451info "Updating python-2.x"
452early_enter_chroot emerge -uNvq =dev-lang/python-2*
453
Mike Frysinger78753af2014-10-14 01:15:32 -0400454# New versions of the stage3 have Python 3 set as the default. Make sure we
455# default to 2.x as our scripts are only compatible with Python 2. We leave
456# Python 3 installed though as we've started including it in our SDK.
David James31097c32013-11-06 18:56:59 -0800457early_enter_chroot eselect python set 1
Mike Frysinger83520dd2013-03-22 01:37:37 -0400458
Bertrand SIMONNET85c96c72014-08-20 12:10:48 -0700459# Add chromite into python path.
460# This needs to happen after the python update or the correct /usr/lib/python2.*
461# may not exist.
462for python_path in "${FLAGS_chroot}/usr/lib/"python2.*; do
463 python_path+="/site-packages"
464 sudo mkdir -p "${python_path}"
465 sudo ln -s -fT "${CHROOT_TRUNK_DIR}"/chromite "${python_path}"/chromite
466done
467
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100468info "Updating host toolchain"
David Purselld35d7af2015-05-04 16:37:08 -0700469if [[ ! -e "${FLAGS_chroot}/usr/bin/crossdev" ]]; then
Mike Frysinger5e07dc72014-11-05 14:40:04 -0500470 early_enter_chroot $EMERGE_CMD -uNv crossdev
471fi
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100472TOOLCHAIN_ARGS=( --deleteold )
David Purselld35d7af2015-05-04 16:37:08 -0700473if [[ "${FLAGS_usepkg}" == "${FLAGS_FALSE}" ]]; then
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100474 TOOLCHAIN_ARGS+=( --nousepkg )
475fi
476# Note: early_enter_chroot executes as root.
Brian Harring2499bfb2012-12-18 16:23:31 -0800477early_enter_chroot "${CHROOT_TRUNK_DIR}/chromite/bin/cros_setup_toolchains" \
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100478 --hostonly "${TOOLCHAIN_ARGS[@]}"
Brian Harringf539bc32012-02-06 00:18:37 -0800479
Mike Frysinger279f1032012-05-17 15:54:31 -0400480info "Running emerge curl sudo ..."
Mike Frysinger650bf872012-02-27 11:05:26 -0500481early_enter_chroot $EMERGE_CMD -uNv $USEPKG --select $EMERGE_JOBS \
Paul Drews8bae3b52012-10-10 11:18:13 -0700482 pbzip2 dev-libs/openssl net-misc/curl sudo
483
Brian Harringf539bc32012-02-06 00:18:37 -0800484if [ -n "${INITIALIZE_CHROOT}" ]; then
485 # If we're creating a new chroot, we also want to set it to the latest
486 # version.
Brian Harring35767822012-02-01 23:50:45 -0800487 enter_chroot \
David James47e0e8a2015-03-11 15:24:10 -0700488 "${CHROOT_TRUNK_DIR}/src/scripts/run_chroot_version_hooks" --init_latest
Brian Harringf539bc32012-02-06 00:18:37 -0800489fi
490
Brian Harring35767822012-02-01 23:50:45 -0800491# Update chroot.
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100492# Skip toolchain update because it already happened above, and the chroot is
493# not ready to emerge all cross toolchains.
494UPDATE_ARGS=( --skip_toolchain_update )
David Purselld35d7af2015-05-04 16:37:08 -0700495if [[ "${FLAGS_usepkg}" == "${FLAGS_TRUE}" ]]; then
Brian Harring35767822012-02-01 23:50:45 -0800496 UPDATE_ARGS+=( --usepkg )
Brian Harringf539bc32012-02-06 00:18:37 -0800497else
Brian Harring35767822012-02-01 23:50:45 -0800498 UPDATE_ARGS+=( --nousepkg )
Brian Harringf539bc32012-02-06 00:18:37 -0800499fi
David James184e3902012-02-23 20:19:28 -0800500if [[ "${FLAGS_jobs}" -ne -1 ]]; then
David Purselld35d7af2015-05-04 16:37:08 -0700501 UPDATE_ARGS+=( --jobs="${FLAGS_jobs}" )
David James184e3902012-02-23 20:19:28 -0800502fi
Brian Harring2499bfb2012-12-18 16:23:31 -0800503enter_chroot "${CHROOT_TRUNK_DIR}/src/scripts/update_chroot" "${UPDATE_ARGS[@]}"
Brian Harringf539bc32012-02-06 00:18:37 -0800504
Stefan Zager48c776b2013-10-09 13:01:25 -0700505# The java-config package atm does not support $ROOT. Select a default
506# VM ourselves until that gets fixed upstream.
507enter_chroot sudo java-config --set-system-vm 1
508
Brian Harring35767822012-02-01 23:50:45 -0800509CHROOT_EXAMPLE_OPT=""
510if [[ "$FLAGS_chroot" != "$DEFAULT_CHROOT_DIR" ]]; then
Brian Harringf539bc32012-02-06 00:18:37 -0800511 CHROOT_EXAMPLE_OPT="--chroot=$FLAGS_chroot"
512fi
513
Matt Tennant0a9d32d2012-07-30 16:51:37 -0700514command_completed
Brian Harringf539bc32012-02-06 00:18:37 -0800515
Brian Harring35767822012-02-01 23:50:45 -0800516cat <<EOF
Mike Frysingerbdc4fb12012-02-10 11:20:03 -0500517
518${CROS_LOG_PREFIX:-cros_sdk}: All set up. To enter the chroot, run:
519$ cros_sdk --enter $CHROOT_EXAMPLE_OPT
Brian Harring35767822012-02-01 23:50:45 -0800520
521CAUTION: Do *NOT* rm -rf the chroot directory; if there are stale bind
522mounts you may end up deleting your source tree too. To unmount and
523delete the chroot cleanly, use:
524$ cros_sdk --delete $CHROOT_EXAMPLE_OPT
Mike Frysingerbdc4fb12012-02-10 11:20:03 -0500525
Brian Harring35767822012-02-01 23:50:45 -0800526EOF
David James22dc2ba2012-11-29 15:46:58 -0800527
528warn_if_nfs "${SUDO_HOME}"