blob: 2d160b912fa44c61d55bef8b3a7b4a14d52b48fd [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."
42DEFINE_boolean fast ${DEFAULT_FAST} "Call many emerges in parallel"
43DEFINE_string stage3_date "2010.03.09" \
44 "Use the stage3 with the given date."
45DEFINE_string stage3_path "" \
46 "Use the stage3 located on this path."
Brian Harring7b6f3772012-09-23 14:01:13 -070047DEFINE_string cache_dir "" "Directory to store caches within."
Brian Harringf539bc32012-02-06 00:18:37 -080048
Brian Harring35767822012-02-01 23:50:45 -080049# Parse command line flags.
Brian Harringf539bc32012-02-06 00:18:37 -080050FLAGS_HELP="usage: $SCRIPT_NAME [flags]"
51FLAGS "$@" || exit 1
52eval set -- "${FLAGS_ARGV}"
53check_flags_only_and_allow_null_arg "$@" && set --
54
Peter Mayo4411efe2012-09-21 04:41:27 -040055CROS_LOG_PREFIX=cros_sdk:make_chroot
David James76764882012-10-24 19:46:29 -070056SUDO_HOME=$(eval echo ~${SUDO_USER})
Brian Harringf539bc32012-02-06 00:18:37 -080057
Brian Harringf539bc32012-02-06 00:18:37 -080058# Set the right umask for chroot creation.
59umask 022
60
61# Only now can we die on error. shflags functions leak non-zero error codes,
Brian Harring7f175a52012-03-02 05:37:00 -080062# so will die prematurely if 'switch_to_strict_mode' is specified before now.
Brian Harringf539bc32012-02-06 00:18:37 -080063# TODO: replace shflags with something less error-prone, or contribute a fix.
Brian Harring7f175a52012-03-02 05:37:00 -080064switch_to_strict_mode
Brian Harringf539bc32012-02-06 00:18:37 -080065
Brian Harring7b6f3772012-09-23 14:01:13 -070066[[ "${FLAGS_delete}" == "${FLAGS_FALSE}" ]] && \
67 [[ -z "${FLAGS_cache_dir}" ]] && \
68 die "--cache_dir is required"
69
J. Richard Barnettee80f6de2012-02-24 14:08:34 -080070. "${SCRIPT_ROOT}"/sdk_lib/make_conf_util.sh
71
Brian Harringf539bc32012-02-06 00:18:37 -080072FULLNAME="ChromeOS Developer"
73DEFGROUPS="eng,adm,cdrom,floppy,audio,video,portage"
74PASSWORD=chronos
75CRYPTED_PASSWD=$(perl -e 'print crypt($ARGV[0], "foo")', $PASSWORD)
76
77USEPKG=""
78if [[ $FLAGS_usepkg -eq $FLAGS_TRUE ]]; then
79 # Use binary packages. Include all build-time dependencies,
80 # so as to avoid unnecessary differences between source
81 # and binary builds.
82 USEPKG="--getbinpkg --usepkg --with-bdeps y"
83fi
84
85# Support faster build if necessary.
86EMERGE_CMD="emerge"
87if [ "$FLAGS_fast" -eq "${FLAGS_TRUE}" ]; then
Brian Harring2499bfb2012-12-18 16:23:31 -080088 CHROOT_CHROMITE_DIR="${CHROOT_TRUNK_DIR}/chromite"
Brian Harringf539bc32012-02-06 00:18:37 -080089 EMERGE_CMD="${CHROOT_CHROMITE_DIR}/bin/parallel_emerge"
90fi
91
Brian Harring35767822012-02-01 23:50:45 -080092ENTER_CHROOT_ARGS=(
93 CROS_WORKON_SRCROOT="$CHROOT_TRUNK"
David James76764882012-10-24 19:46:29 -070094 PORTAGE_USERNAME="${SUDO_USER}"
Brian Harring35767822012-02-01 23:50:45 -080095 IGNORE_PREFLIGHT_BINHOST="$IGNORE_PREFLIGHT_BINHOST"
96)
97
98# Invoke enter_chroot. This can only be used after sudo has been installed.
Mike Frysinger6b1abb22012-05-11 13:44:06 -040099enter_chroot() {
Brian Harring7b6f3772012-09-23 14:01:13 -0700100 "$ENTER_CHROOT" --cache_dir "${FLAGS_cache_dir}" --chroot "$FLAGS_chroot" \
101 -- "${ENTER_CHROOT_ARGS[@]}" "$@"
Brian Harringf539bc32012-02-06 00:18:37 -0800102}
103
Brian Harring35767822012-02-01 23:50:45 -0800104# Invoke enter_chroot running the command as root, and w/out sudo.
105# This should be used prior to sudo being merged.
Mike Frysingerba758452012-04-02 13:28:31 -0400106early_env=()
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400107early_enter_chroot() {
Brian Harring35767822012-02-01 23:50:45 -0800108 "$ENTER_CHROOT" --chroot "$FLAGS_chroot" --early_make_chroot \
Brian Harring7b6f3772012-09-23 14:01:13 -0700109 --cache_dir "${FLAGS_cache_dir}" \
Mike Frysingerba758452012-04-02 13:28:31 -0400110 -- "${ENTER_CHROOT_ARGS[@]}" "${early_env[@]}" "$@"
Brian Harringf539bc32012-02-06 00:18:37 -0800111}
112
Mike Frysinger4bb86452013-08-02 13:44:18 -0400113# Run a command within the chroot. The main usage of this is to avoid the
114# overhead of enter_chroot. It's when we do not need access to the source
115# tree, don't need the actual chroot profile env, and can run the command as
116# root. We do have to make sure PATH includes all the right programs as
117# found inside of the chroot since the environment outside of the chroot
118# might be insufficient (like distros with merged /bin /sbin and /usr).
David James76764882012-10-24 19:46:29 -0700119bare_chroot() {
Mike Frysinger4bb86452013-08-02 13:44:18 -0400120 PATH=/bin:/sbin:/usr/bin:/usr/sbin:${PATH} \
121 chroot "${FLAGS_chroot}" "$@"
Brian Harringf539bc32012-02-06 00:18:37 -0800122}
123
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400124cleanup() {
Brian Harringf539bc32012-02-06 00:18:37 -0800125 # Clean up mounts
126 safe_umount_tree "${FLAGS_chroot}"
127}
128
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400129delete_existing() {
Brian Harring35767822012-02-01 23:50:45 -0800130 # Delete old chroot dir.
131 if [[ ! -e "$FLAGS_chroot" ]]; then
132 return
Brian Harringf539bc32012-02-06 00:18:37 -0800133 fi
Brian Harring35767822012-02-01 23:50:45 -0800134 info "Cleaning up old mount points..."
135 cleanup
136 info "Deleting $FLAGS_chroot..."
David James76764882012-10-24 19:46:29 -0700137 rm -rf "$FLAGS_chroot"
Brian Harring35767822012-02-01 23:50:45 -0800138 info "Done."
Brian Harringf539bc32012-02-06 00:18:37 -0800139}
140
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400141init_users () {
Brian Harring35767822012-02-01 23:50:45 -0800142 info "Set timezone..."
143 # date +%Z has trouble with daylight time, so use host's info.
David James76764882012-10-24 19:46:29 -0700144 rm -f "${FLAGS_chroot}/etc/localtime"
Brian Harringf539bc32012-02-06 00:18:37 -0800145 if [ -f /etc/localtime ] ; then
David James76764882012-10-24 19:46:29 -0700146 cp /etc/localtime "${FLAGS_chroot}/etc"
Brian Harringf539bc32012-02-06 00:18:37 -0800147 else
David James76764882012-10-24 19:46:29 -0700148 ln -sf /usr/share/zoneinfo/PST8PDT "${FLAGS_chroot}/etc/localtime"
Brian Harringf539bc32012-02-06 00:18:37 -0800149 fi
Brian Harring35767822012-02-01 23:50:45 -0800150 info "Adding user/group..."
Brian Harringf539bc32012-02-06 00:18:37 -0800151 # Add ourselves as a user inside the chroot.
David James76764882012-10-24 19:46:29 -0700152 bare_chroot groupadd -g 5000 eng
Brian Harringf539bc32012-02-06 00:18:37 -0800153 # We need the UID to match the host user's. This can conflict with
154 # a particular chroot UID. At the same time, the added user has to
155 # be a primary user for the given UID for sudo to work, which is
156 # determined by the order in /etc/passwd. Let's put ourselves on top
157 # of the file.
David James76764882012-10-24 19:46:29 -0700158 bare_chroot useradd -o -G ${DEFGROUPS} -g eng -u ${SUDO_UID} -s \
159 /bin/bash -m -c "${FULLNAME}" -p ${CRYPTED_PASSWD} ${SUDO_USER}
Brian Harringf539bc32012-02-06 00:18:37 -0800160 # Because passwd generally isn't sorted and the entry ended up at the
161 # bottom, it is safe to just take it and move it to top instead.
David James76764882012-10-24 19:46:29 -0700162 sed -e '1{h;d};$!{H;d};$G' -i "${FLAGS_chroot}/etc/passwd"
Brian Harringf539bc32012-02-06 00:18:37 -0800163}
164
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400165init_setup () {
Brian Harring35767822012-02-01 23:50:45 -0800166 info "Running init_setup()..."
David James76764882012-10-24 19:46:29 -0700167 mkdir -p -m 755 "${FLAGS_chroot}/usr" \
Mike Frysingerb45dbb52013-12-13 21:14:09 -0500168 "${FLAGS_chroot}${OVERLAYS_ROOT}" \
Mike Frysinger36beaaa2014-09-25 15:52:27 -0400169 "${FLAGS_chroot}"/"${CROSSDEV_OVERLAY}/metadata"
170 # Newer portage complains about bare overlays. Create the file that crossdev
171 # will also create later on.
172 cat <<EOF > "${FLAGS_chroot}/${CROSSDEV_OVERLAY}/metadata/layout.conf"
173# Autogenerated and managed by crossdev
174# Delete the above line if you want to manage this file yourself
175masters = portage-stable chromiumos
176repo-name = crossdev
177use-manifests = true
178thin-manifests = true
179EOF
Mike Frysingerb45dbb52013-12-13 21:14:09 -0500180 ln -sf "${CHROOT_TRUNK_DIR}/src/third_party/eclass-overlay" \
181 "${FLAGS_chroot}"/"${ECLASS_OVERLAY}"
Brian Harring2499bfb2012-12-18 16:23:31 -0800182 ln -sf "${CHROOT_TRUNK_DIR}/src/third_party/chromiumos-overlay" \
Brian Harringf539bc32012-02-06 00:18:37 -0800183 "${FLAGS_chroot}"/"${CHROOT_OVERLAY}"
Brian Harring2499bfb2012-12-18 16:23:31 -0800184 ln -sf "${CHROOT_TRUNK_DIR}/src/third_party/portage-stable" \
Brian Harringf539bc32012-02-06 00:18:37 -0800185 "${FLAGS_chroot}"/"${PORTAGE_STABLE_OVERLAY}"
Brian Harringf539bc32012-02-06 00:18:37 -0800186
Brian Harring35767822012-02-01 23:50:45 -0800187 # Some operations need an mtab.
Mike Frysingerb47a4ff2013-02-20 17:29:05 -0500188 ln -sfT /proc/mounts "${FLAGS_chroot}/etc/mtab"
Brian Harringf539bc32012-02-06 00:18:37 -0800189
190 # Set up sudoers. Inside the chroot, the user can sudo without a password.
191 # (Safe enough, since the only way into the chroot is to 'sudo chroot', so
192 # the user's already typed in one sudo password...)
193 # Make sure the sudoers.d subdir exists as older stage3 base images lack it.
David James76764882012-10-24 19:46:29 -0700194 mkdir -p "${FLAGS_chroot}/etc/sudoers.d"
Brian Harring06d3c2e2012-08-23 07:35:43 -0700195
196 # Use the standardized upgrade script to setup proxied vars.
David James76764882012-10-24 19:46:29 -0700197 load_environment_whitelist
198 bash -e "${SCRIPT_ROOT}/chroot_version_hooks.d/45_rewrite_sudoers.d" \
199 "${FLAGS_chroot}" "${SUDO_USER}" "${ENVIRONMENT_WHITELIST[@]}"
Brian Harring06d3c2e2012-08-23 07:35:43 -0700200
David James76764882012-10-24 19:46:29 -0700201 find "${FLAGS_chroot}/etc/"sudoers* -type f -exec chmod 0440 {} +
Brian Harring35767822012-02-01 23:50:45 -0800202 # Fix bad group for some.
David James76764882012-10-24 19:46:29 -0700203 chown -R root:root "${FLAGS_chroot}/etc/"sudoers*
Brian Harringf539bc32012-02-06 00:18:37 -0800204
Brian Harring35767822012-02-01 23:50:45 -0800205 info "Setting up hosts/resolv..."
206 # Copy config from outside chroot into chroot.
David James76764882012-10-24 19:46:29 -0700207 cp /etc/{hosts,resolv.conf} "$FLAGS_chroot/etc/"
208 chmod 0644 "$FLAGS_chroot"/etc/{hosts,resolv.conf}
Brian Harringf539bc32012-02-06 00:18:37 -0800209
210 # Setup host make.conf. This includes any overlay that we may be using
211 # and a pointer to pre-built packages.
Brian Harring35767822012-02-01 23:50:45 -0800212 # TODO: This should really be part of a profile in the portage.
213 info "Setting up /etc/make.*..."
Mike Frysingerc6e1ace2013-03-22 00:49:30 -0400214 rm -f "${FLAGS_chroot}"/etc/{,portage/}make.{conf,profile}
215 mkdir -p "${FLAGS_chroot}/etc/portage"
David James76764882012-10-24 19:46:29 -0700216 ln -sf "${CHROOT_CONFIG}/make.conf.amd64-host" \
Brian Harringf539bc32012-02-06 00:18:37 -0800217 "${FLAGS_chroot}/etc/make.conf"
David James76764882012-10-24 19:46:29 -0700218 ln -sf "${CHROOT_OVERLAY}/profiles/default/linux/amd64/10.0" \
Brian Harringf539bc32012-02-06 00:18:37 -0800219 "${FLAGS_chroot}/etc/make.profile"
220
Brian Harring35767822012-02-01 23:50:45 -0800221 # Create make.conf.user .
David James76764882012-10-24 19:46:29 -0700222 touch "${FLAGS_chroot}"/etc/make.conf.user
223 chmod 0644 "${FLAGS_chroot}"/etc/make.conf.user
Brian Harringf539bc32012-02-06 00:18:37 -0800224
225 # Create directories referred to by our conf files.
David James76764882012-10-24 19:46:29 -0700226 mkdir -p -m 775 "${FLAGS_chroot}/var/lib/portage/pkgs" \
Brian Harring7b6f3772012-09-23 14:01:13 -0700227 "${FLAGS_chroot}/var/cache/"chromeos-{cache,chrome} \
228 "${FLAGS_chroot}/etc/profile.d"
229
David James76764882012-10-24 19:46:29 -0700230 echo "export CHROMEOS_CACHEDIR=/var/cache/chromeos-cache" > \
231 "${FLAGS_chroot}/etc/profile.d/chromeos-cachedir.sh"
232 chmod 0644 "${FLAGS_chroot}/etc/profile.d/chromeos-cachedir.sh"
233 rm -rf "${FLAGS_chroot}/var/cache/distfiles"
234 ln -s chromeos-cache/distfiles "${FLAGS_chroot}/var/cache/distfiles"
Brian Harring36b102b2012-02-06 23:34:25 -0800235
236 # Run this from w/in the chroot so we use whatever uid/gid
237 # these are defined as w/in the chroot.
David James76764882012-10-24 19:46:29 -0700238 bare_chroot chown "${SUDO_USER}:portage" /var/cache/chromeos-chrome
Brian Harring7ee892d2012-02-02 09:33:10 -0800239
240 # These are created for compatibility while transitioning
241 # make.conf and friends over to the new location.
Brian Harring7b6f3772012-09-23 14:01:13 -0700242 # TODO(ferringb): remove this 01/13 or so.
David James76764882012-10-24 19:46:29 -0700243 ln -s ../../cache/chromeos-cache/distfiles/host \
Brian Harring7ee892d2012-02-02 09:33:10 -0800244 "${FLAGS_chroot}/var/lib/portage/distfiles"
David James76764882012-10-24 19:46:29 -0700245 ln -s ../../cache/chromeos-cache/distfiles/target \
Brian Harring7ee892d2012-02-02 09:33:10 -0800246 "${FLAGS_chroot}/var/lib/portage/distfiles-target"
Brian Harringf539bc32012-02-06 00:18:37 -0800247
Brian Harringf539bc32012-02-06 00:18:37 -0800248 # Add chromite/bin and depot_tools into the path globally; note that the
249 # chromite wrapper itself might also be found in depot_tools.
250 # We rely on 'env-update' getting called below.
251 target="${FLAGS_chroot}/etc/env.d/99chromiumos"
David James76764882012-10-24 19:46:29 -0700252 cat <<EOF > "${target}"
Brian Harring2499bfb2012-12-18 16:23:31 -0800253PATH=${CHROOT_TRUNK_DIR}/chromite/bin:${DEPOT_TOOLS_DIR}
254CROS_WORKON_SRCROOT="${CHROOT_TRUNK_DIR}"
David James76764882012-10-24 19:46:29 -0700255PORTAGE_USERNAME=${SUDO_USER}
Brian Harringf539bc32012-02-06 00:18:37 -0800256EOF
257
258 # TODO(zbehan): Configure stuff that is usually configured in postinst's,
Mike Frysingereb1a9b42012-03-28 16:21:04 -0400259 # but wasn't. Fix the postinst's.
Brian Harring35767822012-02-01 23:50:45 -0800260 info "Running post-inst configuration hacks"
261 early_enter_chroot env-update
Brian Harringf539bc32012-02-06 00:18:37 -0800262
Brian Harring35767822012-02-01 23:50:45 -0800263 # This is basically a sanity check of our chroot. If any of these
264 # don't exist, then either bind mounts have failed, an invocation
265 # from above is broke, or some assumption about the stage3 is no longer
266 # true.
267 early_enter_chroot ls -l /etc/make.{conf,profile} \
268 /usr/local/portage/chromiumos/profiles/default/linux/amd64/10.0
Brian Harringf539bc32012-02-06 00:18:37 -0800269
270 target="${FLAGS_chroot}/etc/profile.d"
David James76764882012-10-24 19:46:29 -0700271 mkdir -p "${target}"
272 cat << EOF > "${target}/chromiumos-niceties.sh"
Brian Harringf539bc32012-02-06 00:18:37 -0800273# Niceties for interactive logins. (cr) denotes this is a chroot, the
274# __git_branch_ps1 prints current git branch in ./ . The $r behavior is to
275# make sure we don't reset the previous $? value which later formats in
276# $PS1 might rely on.
277PS1='\$(r=\$?; __git_branch_ps1 "(%s) "; exit \$r)'"\${PS1}"
278PS1="(cr) \${PS1}"
279EOF
280
281 # Select a small set of locales for the user if they haven't done so
282 # already. This makes glibc upgrades cheap by only generating a small
283 # set of locales. The ones listed here are basically for the buildbots
284 # which always assume these are available. This works in conjunction
285 # with `cros_sdk --enter`.
286 # http://crosbug.com/20378
287 local localegen="$FLAGS_chroot/etc/locale.gen"
288 if ! grep -q -v -e '^#' -e '^$' "${localegen}" ; then
David James76764882012-10-24 19:46:29 -0700289 cat <<EOF >> "${localegen}"
Brian Harringf539bc32012-02-06 00:18:37 -0800290en_US ISO-8859-1
291en_US.UTF-8 UTF-8
292EOF
293 fi
294
Brian Harring35767822012-02-01 23:50:45 -0800295 # Automatically change to scripts directory.
Brian Harringf539bc32012-02-06 00:18:37 -0800296 echo 'cd ${CHROOT_CWD:-~/trunk/src/scripts}' \
David James76764882012-10-24 19:46:29 -0700297 | user_append "$FLAGS_chroot/home/${SUDO_USER}/.bash_profile"
Brian Harringf539bc32012-02-06 00:18:37 -0800298
Brian Harring35767822012-02-01 23:50:45 -0800299 # Enable bash completion for build scripts.
Brian Harringf539bc32012-02-06 00:18:37 -0800300 echo ". ~/trunk/src/scripts/bash_completion" \
David James76764882012-10-24 19:46:29 -0700301 | user_append "$FLAGS_chroot/home/${SUDO_USER}/.bashrc"
Brian Harringf539bc32012-02-06 00:18:37 -0800302
Peter Mayoe18c7d42013-06-06 13:41:03 -0400303 if [[ "${SUDO_USER}" = "chrome-bot" && -d "${SUDO_HOME}/.ssh" ]]; then
Brian Harringf539bc32012-02-06 00:18:37 -0800304 # Copy ssh keys, so chroot'd chrome-bot can scp files from chrome-web.
Peter Mayo31056952013-06-06 16:22:55 -0400305 user_cp -rp "${SUDO_HOME}/.ssh" "$FLAGS_chroot/home/${SUDO_USER}/"
Brian Harringf539bc32012-02-06 00:18:37 -0800306 fi
307
David James76764882012-10-24 19:46:29 -0700308 if [[ -f ${SUDO_HOME}/.gitconfig ]]; then
Brian Harring35767822012-02-01 23:50:45 -0800309 # Copy .gitconfig into chroot so repo and git can be used from inside.
310 # This is required for repo to work since it validates the email address.
Brian Harringf539bc32012-02-06 00:18:37 -0800311 echo "Copying ~/.gitconfig into chroot"
David James22dc2ba2012-11-29 15:46:58 -0800312 user_cp "${SUDO_HOME}/.gitconfig" "$FLAGS_chroot/home/${SUDO_USER}/"
Brian Harringf539bc32012-02-06 00:18:37 -0800313 fi
Luigi Semenzato2443fdd2012-05-29 10:34:04 -0700314
David James76764882012-10-24 19:46:29 -0700315 # If the user didn't set up their username in their gitconfig, look
316 # at the default git settings for the user.
317 if ! git config -f "${SUDO_HOME}/.gitconfig" user.email >& /dev/null; then
318 ident=$(cd /; sudo -u ${SUDO_USER} -- git var GIT_COMMITTER_IDENT || :)
319 ident_name=${ident%% <*}
320 ident_email=${ident%%>*}; ident_email=${ident_email##*<}
321 gitconfig=${FLAGS_chroot}/home/${SUDO_USER}/.gitconfig
322 git config -f ${gitconfig} --replace-all user.name "${ident_name}" || :
323 git config -f ${gitconfig} --replace-all user.email "${ident_email}" || :
324 chown ${SUDO_UID}:${SUDO_GID} ${FLAGS_chroot}/home/${SUDO_USER}/.gitconfig
325 fi
326
327 if [[ -f ${SUDO_HOME}/.cros_chroot_init ]]; then
328 sudo -u ${SUDO_USER} -- /bin/bash "${SUDO_HOME}/.cros_chroot_init" \
329 "${FLAGS_chroot}"
Luigi Semenzato2443fdd2012-05-29 10:34:04 -0700330 fi
Brian Harringf539bc32012-02-06 00:18:37 -0800331}
332
Brian Harring35767822012-02-01 23:50:45 -0800333# Handle deleting an existing environment.
Brian Harringf539bc32012-02-06 00:18:37 -0800334if [[ $FLAGS_delete -eq $FLAGS_TRUE || \
Brian Harring35767822012-02-01 23:50:45 -0800335 $FLAGS_replace -eq $FLAGS_TRUE ]]; then
Brian Harringf539bc32012-02-06 00:18:37 -0800336 delete_existing
337 [[ $FLAGS_delete -eq $FLAGS_TRUE ]] && exit 0
338fi
339
340CHROOT_TRUNK="${CHROOT_TRUNK_DIR}"
341PORTAGE="${SRC_ROOT}/third_party/portage"
342OVERLAY="${SRC_ROOT}/third_party/chromiumos-overlay"
343CONFIG_DIR="${OVERLAY}/chromeos/config"
Brian Harring2499bfb2012-12-18 16:23:31 -0800344CHROOT_CONFIG="${CHROOT_TRUNK_DIR}/src/third_party/chromiumos-overlay/chromeos/config"
Mike Frysingerb45dbb52013-12-13 21:14:09 -0500345OVERLAYS_ROOT="/usr/local/portage"
346ECLASS_OVERLAY="${OVERLAYS_ROOT}/eclass-overlay"
347PORTAGE_STABLE_OVERLAY="${OVERLAYS_ROOT}/stable"
348CROSSDEV_OVERLAY="${OVERLAYS_ROOT}/crossdev"
349CHROOT_OVERLAY="${OVERLAYS_ROOT}/chromiumos"
Brian Harringf539bc32012-02-06 00:18:37 -0800350CHROOT_STATE="${FLAGS_chroot}/etc/debian_chroot"
Vincent Palatin5ce24062014-02-26 12:08:23 -0800351CHROOT_VERSION="${FLAGS_chroot}/etc/cros_chroot_version"
Brian Harringf539bc32012-02-06 00:18:37 -0800352
353# Pass proxy variables into the environment.
354for type in http ftp all; do
355 value=$(env | grep ${type}_proxy || true)
356 if [ -n "${value}" ]; then
357 CHROOT_PASSTHRU+=("$value")
358 fi
359done
360
Brian Harring35767822012-02-01 23:50:45 -0800361# Create the destination directory.
Brian Harringf539bc32012-02-06 00:18:37 -0800362mkdir -p "$FLAGS_chroot"
363
364echo
Mike Frysinger51930072014-04-05 13:01:45 -0400365if [[ -f ${CHROOT_STATE} ]]; then
366 info "stage3 already set up. Skipping..."
367elif [[ -z ${FLAGS_stage3_path} ]]; then
368 die_notrace "Please use --stage3_path when bootstrapping"
Brian Harringf539bc32012-02-06 00:18:37 -0800369else
Mike Frysinger51930072014-04-05 13:01:45 -0400370 info "Unpacking stage3..."
371 case ${FLAGS_stage3_path} in
Zdenek Behan074f9ef2012-05-30 01:23:59 +0200372 *.tbz2|*.tar.bz2) DECOMPRESS=$(type -p pbzip2 || echo bzip2) ;;
373 *.tar.xz) DECOMPRESS="xz" ;;
Mike Frysinger51930072014-04-05 13:01:45 -0400374 *) die "Unknown tarball compression: ${FLAGS_stage3_path}" ;;
Zdenek Behan074f9ef2012-05-30 01:23:59 +0200375 esac
Mike Frysinger51930072014-04-05 13:01:45 -0400376 ${DECOMPRESS} -dc "${FLAGS_stage3_path}" | \
David James76764882012-10-24 19:46:29 -0700377 tar -xp -C "${FLAGS_chroot}"
378 rm -f "$FLAGS_chroot/etc/"make.{globals,conf.user}
Brian Harringf539bc32012-02-06 00:18:37 -0800379fi
380
Vincent Palatin5ce24062014-02-26 12:08:23 -0800381# Ensure that we properly detect when we are inside the chroot.
382# We'll force this to the latest version at the end as needed.
383if [[ ! -e ${CHROOT_VERSION} ]]; then
384 echo "0" > "${CHROOT_VERSION}"
385fi
386
Brian Harring35767822012-02-01 23:50:45 -0800387# Set up users, if needed, before mkdir/mounts below.
Brian Harringf539bc32012-02-06 00:18:37 -0800388[ -f $CHROOT_STATE ] || init_users
389
Brian Harring2499bfb2012-12-18 16:23:31 -0800390# Reset internal vars to force them to the 'inside the chroot' value;
391# since user directories now exist, this can do the upgrade in place.
392set_chroot_trunk_dir "${FLAGS_chroot}" poppycock
393
Brian Harringf539bc32012-02-06 00:18:37 -0800394echo
Brian Harring35767822012-02-01 23:50:45 -0800395info "Setting up mounts..."
396# Set up necessary mounts and make sure we clean them up on exit.
Brian Harring2499bfb2012-12-18 16:23:31 -0800397mkdir -p "${FLAGS_chroot}/${CHROOT_TRUNK_DIR}" \
398 "${FLAGS_chroot}/${DEPOT_TOOLS_DIR}" "${FLAGS_chroot}/run"
Brian Harring35767822012-02-01 23:50:45 -0800399
J. Richard Barnettee80f6de2012-02-24 14:08:34 -0800400# Create a special /etc/make.conf.host_setup that we use to bootstrap
401# the chroot. The regular content for the file will be generated the
402# first time we invoke update_chroot (further down in this script).
403create_bootstrap_host_setup "${FLAGS_chroot}"
Brian Harringf539bc32012-02-06 00:18:37 -0800404
405if ! [ -f "$CHROOT_STATE" ];then
406 INITIALIZE_CHROOT=1
407fi
408
Mike Frysingerba758452012-04-02 13:28:31 -0400409if ! early_enter_chroot bash -c 'type -P pbzip2' >/dev/null ; then
410 # This chroot lacks pbzip2 early on, so we need to disable it.
411 early_env+=(
412 PORTAGE_BZIP2_COMMAND="bzip2"
413 PORTAGE_BUNZIP2_COMMAND="bunzip2"
414 )
415fi
416
Brian Harringf539bc32012-02-06 00:18:37 -0800417if [ -z "${INITIALIZE_CHROOT}" ];then
Brian Harring35767822012-02-01 23:50:45 -0800418 info "chroot already initialized. Skipping..."
Brian Harringf539bc32012-02-06 00:18:37 -0800419else
Brian Harring35767822012-02-01 23:50:45 -0800420 # Run all the init stuff to setup the env.
Brian Harringf539bc32012-02-06 00:18:37 -0800421 init_setup
422fi
423
Brian Harring35767822012-02-01 23:50:45 -0800424# Add file to indicate that it is a chroot.
Mike Frysinger51930072014-04-05 13:01:45 -0400425# Add version of stage3 for update checks.
426echo "STAGE3=${FLAGS_stage3_path}" > "${CHROOT_STATE}"
Brian Harringf539bc32012-02-06 00:18:37 -0800427
Brian Harring35767822012-02-01 23:50:45 -0800428info "Updating portage"
Mike Frysinger66fd81f2012-02-28 13:13:20 -0500429early_enter_chroot emerge -uNv --quiet portage
Brian Harringf539bc32012-02-06 00:18:37 -0800430
Mike Frysinger9a2978c2013-08-04 11:38:43 -0400431# Clear out openrc if it's installed as we don't want it.
432if [[ -e ${FLAGS_chroot}/usr/share/openrc ]]; then
433 info "Uninstalling openrc"
434 early_enter_chroot env CLEAN_DELAY=0 emerge -qC sys-apps/openrc
Mike Frysinger47e599e2013-10-28 13:30:12 -0400435 # Now update baselayout to get our functions.sh. The unmerge
436 # above removed our copy in the process.
437 early_enter_chroot emerge -uNvq sys-apps/baselayout
Mike Frysinger9a2978c2013-08-04 11:38:43 -0400438fi
439
David James31097c32013-11-06 18:56:59 -0800440# The stage3 contains an old version of Python. Upgrade it first so that
441# parallel_emerge (and chromite libs) can use the latest Python syntax.
442info "Updating python-2.x"
443early_enter_chroot emerge -uNvq =dev-lang/python-2*
444
445# New versions of the stage3 have Python 3 set as the default. Get rid of it,
446# as our scripts are only compatible with Python 2.
447early_enter_chroot eselect python set 1
448early_enter_chroot env CLEAN_DELAY=0 emerge -qC =dev-lang/python-3* || true
Mike Frysinger83520dd2013-03-22 01:37:37 -0400449
Bertrand SIMONNET85c96c72014-08-20 12:10:48 -0700450# Add chromite into python path.
451# This needs to happen after the python update or the correct /usr/lib/python2.*
452# may not exist.
453for python_path in "${FLAGS_chroot}/usr/lib/"python2.*; do
454 python_path+="/site-packages"
455 sudo mkdir -p "${python_path}"
456 sudo ln -s -fT "${CHROOT_TRUNK_DIR}"/chromite "${python_path}"/chromite
457done
458
Paul Drews8bae3b52012-10-10 11:18:13 -0700459# Packages that inherit cros-workon commonly get a circular dependency
460# curl->openssl->git->curl that is broken by emerging an early version of git
461# without curl (and webdav that depends on it).
Mike Frysinger96c5c1c2012-10-30 18:59:00 -0400462# We also need to do this before the toolchain as those will sometimes also
463# fetch via remote git trees (for some bot configs).
Paul Drews8bae3b52012-10-10 11:18:13 -0700464if [[ ! -e "${FLAGS_chroot}/usr/bin/git" ]]; then
Paul Drews8bae3b52012-10-10 11:18:13 -0700465 info "Updating early git"
Mike Frysinger96c5c1c2012-10-30 18:59:00 -0400466 USE="-curl -webdav" early_enter_chroot $EMERGE_CMD -uNv $USEPKG dev-vcs/git
467
Mike Frysinger96c5c1c2012-10-30 18:59:00 -0400468 early_enter_chroot $EMERGE_CMD -uNv $USEPKG --select $EMERGE_JOBS \
469 dev-libs/openssl net-misc/curl
470
471 # (Re-)emerge the full version of git.
472 info "Updating full version of git"
473 early_enter_chroot $EMERGE_CMD -uNv $USEPKG dev-vcs/git
Paul Drews8bae3b52012-10-10 11:18:13 -0700474fi
475
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100476info "Updating host toolchain"
Mike Frysinger96c5c1c2012-10-30 18:59:00 -0400477early_enter_chroot $EMERGE_CMD -uNv crossdev
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100478TOOLCHAIN_ARGS=( --deleteold )
479if [[ ${FLAGS_usepkg} -eq ${FLAGS_FALSE} ]]; then
480 TOOLCHAIN_ARGS+=( --nousepkg )
481fi
482# Note: early_enter_chroot executes as root.
Brian Harring2499bfb2012-12-18 16:23:31 -0800483early_enter_chroot "${CHROOT_TRUNK_DIR}/chromite/bin/cros_setup_toolchains" \
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100484 --hostonly "${TOOLCHAIN_ARGS[@]}"
Brian Harringf539bc32012-02-06 00:18:37 -0800485
Mike Frysinger279f1032012-05-17 15:54:31 -0400486info "Running emerge curl sudo ..."
Mike Frysinger650bf872012-02-27 11:05:26 -0500487early_enter_chroot $EMERGE_CMD -uNv $USEPKG --select $EMERGE_JOBS \
Paul Drews8bae3b52012-10-10 11:18:13 -0700488 pbzip2 dev-libs/openssl net-misc/curl sudo
489
Brian Harringf539bc32012-02-06 00:18:37 -0800490if [ -n "${INITIALIZE_CHROOT}" ]; then
491 # If we're creating a new chroot, we also want to set it to the latest
492 # version.
Brian Harring35767822012-02-01 23:50:45 -0800493 enter_chroot \
Brian Harring2499bfb2012-12-18 16:23:31 -0800494 "${CHROOT_TRUNK_DIR}/src/scripts/run_chroot_version_hooks" --force_latest
Brian Harringf539bc32012-02-06 00:18:37 -0800495fi
496
Brian Harring35767822012-02-01 23:50:45 -0800497# Update chroot.
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100498# Skip toolchain update because it already happened above, and the chroot is
499# not ready to emerge all cross toolchains.
500UPDATE_ARGS=( --skip_toolchain_update )
501if [[ ${FLAGS_usepkg} -eq ${FLAGS_TRUE} ]]; then
Brian Harring35767822012-02-01 23:50:45 -0800502 UPDATE_ARGS+=( --usepkg )
Brian Harringf539bc32012-02-06 00:18:37 -0800503else
Brian Harring35767822012-02-01 23:50:45 -0800504 UPDATE_ARGS+=( --nousepkg )
Brian Harringf539bc32012-02-06 00:18:37 -0800505fi
506if [[ ${FLAGS_fast} -eq ${FLAGS_TRUE} ]]; then
Brian Harring35767822012-02-01 23:50:45 -0800507 UPDATE_ARGS+=( --fast )
Brian Harringf539bc32012-02-06 00:18:37 -0800508else
Brian Harring35767822012-02-01 23:50:45 -0800509 UPDATE_ARGS+=( --nofast )
Brian Harringf539bc32012-02-06 00:18:37 -0800510fi
David James184e3902012-02-23 20:19:28 -0800511if [[ "${FLAGS_jobs}" -ne -1 ]]; then
512 UPDATE_ARGS+=( --jobs=${FLAGS_jobs} )
513fi
Brian Harring2499bfb2012-12-18 16:23:31 -0800514enter_chroot "${CHROOT_TRUNK_DIR}/src/scripts/update_chroot" "${UPDATE_ARGS[@]}"
Brian Harringf539bc32012-02-06 00:18:37 -0800515
Stefan Zager48c776b2013-10-09 13:01:25 -0700516# The java-config package atm does not support $ROOT. Select a default
517# VM ourselves until that gets fixed upstream.
518enter_chroot sudo java-config --set-system-vm 1
519
Brian Harring35767822012-02-01 23:50:45 -0800520CHROOT_EXAMPLE_OPT=""
521if [[ "$FLAGS_chroot" != "$DEFAULT_CHROOT_DIR" ]]; then
Brian Harringf539bc32012-02-06 00:18:37 -0800522 CHROOT_EXAMPLE_OPT="--chroot=$FLAGS_chroot"
523fi
524
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100525# As a final pass, build all desired cross-toolchains.
526info "Updating toolchains"
Brian Harring2499bfb2012-12-18 16:23:31 -0800527enter_chroot sudo -E "${CHROOT_TRUNK_DIR}/chromite/bin/cros_setup_toolchains" \
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100528 "${TOOLCHAIN_ARGS[@]}"
529
Matt Tennant0a9d32d2012-07-30 16:51:37 -0700530command_completed
Brian Harringf539bc32012-02-06 00:18:37 -0800531
Brian Harring35767822012-02-01 23:50:45 -0800532cat <<EOF
Mike Frysingerbdc4fb12012-02-10 11:20:03 -0500533
534${CROS_LOG_PREFIX:-cros_sdk}: All set up. To enter the chroot, run:
535$ cros_sdk --enter $CHROOT_EXAMPLE_OPT
Brian Harring35767822012-02-01 23:50:45 -0800536
537CAUTION: Do *NOT* rm -rf the chroot directory; if there are stale bind
538mounts you may end up deleting your source tree too. To unmount and
539delete the chroot cleanly, use:
540$ cros_sdk --delete $CHROOT_EXAMPLE_OPT
Mike Frysingerbdc4fb12012-02-10 11:20:03 -0500541
Brian Harring35767822012-02-01 23:50:45 -0800542EOF
David James22dc2ba2012-11-29 15:46:58 -0800543
544warn_if_nfs "${SUDO_HOME}"