blob: d86ba25d9249dc58e3efc37043eaef706fc45051 [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"
Brian Harringf539bc32012-02-06 00:18:37 -080074
75USEPKG=""
76if [[ $FLAGS_usepkg -eq $FLAGS_TRUE ]]; then
77 # Use binary packages. Include all build-time dependencies,
78 # so as to avoid unnecessary differences between source
79 # and binary builds.
80 USEPKG="--getbinpkg --usepkg --with-bdeps y"
81fi
82
83# Support faster build if necessary.
84EMERGE_CMD="emerge"
85if [ "$FLAGS_fast" -eq "${FLAGS_TRUE}" ]; then
Brian Harring2499bfb2012-12-18 16:23:31 -080086 CHROOT_CHROMITE_DIR="${CHROOT_TRUNK_DIR}/chromite"
Brian Harringf539bc32012-02-06 00:18:37 -080087 EMERGE_CMD="${CHROOT_CHROMITE_DIR}/bin/parallel_emerge"
88fi
89
Brian Harring35767822012-02-01 23:50:45 -080090ENTER_CHROOT_ARGS=(
91 CROS_WORKON_SRCROOT="$CHROOT_TRUNK"
David James76764882012-10-24 19:46:29 -070092 PORTAGE_USERNAME="${SUDO_USER}"
Brian Harring35767822012-02-01 23:50:45 -080093 IGNORE_PREFLIGHT_BINHOST="$IGNORE_PREFLIGHT_BINHOST"
94)
95
96# Invoke enter_chroot. This can only be used after sudo has been installed.
Mike Frysinger6b1abb22012-05-11 13:44:06 -040097enter_chroot() {
Brian Harring7b6f3772012-09-23 14:01:13 -070098 "$ENTER_CHROOT" --cache_dir "${FLAGS_cache_dir}" --chroot "$FLAGS_chroot" \
99 -- "${ENTER_CHROOT_ARGS[@]}" "$@"
Brian Harringf539bc32012-02-06 00:18:37 -0800100}
101
Brian Harring35767822012-02-01 23:50:45 -0800102# Invoke enter_chroot running the command as root, and w/out sudo.
103# This should be used prior to sudo being merged.
Mike Frysingerba758452012-04-02 13:28:31 -0400104early_env=()
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400105early_enter_chroot() {
Brian Harring35767822012-02-01 23:50:45 -0800106 "$ENTER_CHROOT" --chroot "$FLAGS_chroot" --early_make_chroot \
Brian Harring7b6f3772012-09-23 14:01:13 -0700107 --cache_dir "${FLAGS_cache_dir}" \
Mike Frysingerba758452012-04-02 13:28:31 -0400108 -- "${ENTER_CHROOT_ARGS[@]}" "${early_env[@]}" "$@"
Brian Harringf539bc32012-02-06 00:18:37 -0800109}
110
Mike Frysinger4bb86452013-08-02 13:44:18 -0400111# Run a command within the chroot. The main usage of this is to avoid the
112# overhead of enter_chroot. It's when we do not need access to the source
113# tree, don't need the actual chroot profile env, and can run the command as
114# root. We do have to make sure PATH includes all the right programs as
115# found inside of the chroot since the environment outside of the chroot
116# might be insufficient (like distros with merged /bin /sbin and /usr).
David James76764882012-10-24 19:46:29 -0700117bare_chroot() {
Mike Frysinger4bb86452013-08-02 13:44:18 -0400118 PATH=/bin:/sbin:/usr/bin:/usr/sbin:${PATH} \
119 chroot "${FLAGS_chroot}" "$@"
Brian Harringf539bc32012-02-06 00:18:37 -0800120}
121
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400122cleanup() {
Brian Harringf539bc32012-02-06 00:18:37 -0800123 # Clean up mounts
124 safe_umount_tree "${FLAGS_chroot}"
125}
126
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400127delete_existing() {
Brian Harring35767822012-02-01 23:50:45 -0800128 # Delete old chroot dir.
129 if [[ ! -e "$FLAGS_chroot" ]]; then
130 return
Brian Harringf539bc32012-02-06 00:18:37 -0800131 fi
Brian Harring35767822012-02-01 23:50:45 -0800132 info "Cleaning up old mount points..."
133 cleanup
134 info "Deleting $FLAGS_chroot..."
David James76764882012-10-24 19:46:29 -0700135 rm -rf "$FLAGS_chroot"
Brian Harring35767822012-02-01 23:50:45 -0800136 info "Done."
Brian Harringf539bc32012-02-06 00:18:37 -0800137}
138
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400139init_users () {
Brian Harring35767822012-02-01 23:50:45 -0800140 info "Set timezone..."
141 # date +%Z has trouble with daylight time, so use host's info.
David James76764882012-10-24 19:46:29 -0700142 rm -f "${FLAGS_chroot}/etc/localtime"
Brian Harringf539bc32012-02-06 00:18:37 -0800143 if [ -f /etc/localtime ] ; then
David James76764882012-10-24 19:46:29 -0700144 cp /etc/localtime "${FLAGS_chroot}/etc"
Brian Harringf539bc32012-02-06 00:18:37 -0800145 else
David James76764882012-10-24 19:46:29 -0700146 ln -sf /usr/share/zoneinfo/PST8PDT "${FLAGS_chroot}/etc/localtime"
Brian Harringf539bc32012-02-06 00:18:37 -0800147 fi
Brian Harring35767822012-02-01 23:50:45 -0800148 info "Adding user/group..."
Brian Harringf539bc32012-02-06 00:18:37 -0800149 # Add ourselves as a user inside the chroot.
David James76764882012-10-24 19:46:29 -0700150 bare_chroot groupadd -g 5000 eng
Brian Harringf539bc32012-02-06 00:18:37 -0800151 # We need the UID to match the host user's. This can conflict with
152 # a particular chroot UID. At the same time, the added user has to
153 # be a primary user for the given UID for sudo to work, which is
154 # determined by the order in /etc/passwd. Let's put ourselves on top
155 # of the file.
David James76764882012-10-24 19:46:29 -0700156 bare_chroot useradd -o -G ${DEFGROUPS} -g eng -u ${SUDO_UID} -s \
Simran Basiccfc7ee2014-10-14 13:43:10 -0700157 /bin/bash -m -c "${FULLNAME}" ${SUDO_USER}
Brian Harringf539bc32012-02-06 00:18:37 -0800158 # Because passwd generally isn't sorted and the entry ended up at the
159 # bottom, it is safe to just take it and move it to top instead.
David James76764882012-10-24 19:46:29 -0700160 sed -e '1{h;d};$!{H;d};$G' -i "${FLAGS_chroot}/etc/passwd"
Brian Harringf539bc32012-02-06 00:18:37 -0800161}
162
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400163init_setup () {
Brian Harring35767822012-02-01 23:50:45 -0800164 info "Running init_setup()..."
David James76764882012-10-24 19:46:29 -0700165 mkdir -p -m 755 "${FLAGS_chroot}/usr" \
Mike Frysingerb45dbb52013-12-13 21:14:09 -0500166 "${FLAGS_chroot}${OVERLAYS_ROOT}" \
Mike Frysinger36beaaa2014-09-25 15:52:27 -0400167 "${FLAGS_chroot}"/"${CROSSDEV_OVERLAY}/metadata"
168 # Newer portage complains about bare overlays. Create the file that crossdev
169 # will also create later on.
170 cat <<EOF > "${FLAGS_chroot}/${CROSSDEV_OVERLAY}/metadata/layout.conf"
171# Autogenerated and managed by crossdev
172# Delete the above line if you want to manage this file yourself
173masters = portage-stable chromiumos
174repo-name = crossdev
175use-manifests = true
176thin-manifests = true
177EOF
Mike Frysingerb45dbb52013-12-13 21:14:09 -0500178 ln -sf "${CHROOT_TRUNK_DIR}/src/third_party/eclass-overlay" \
179 "${FLAGS_chroot}"/"${ECLASS_OVERLAY}"
Brian Harring2499bfb2012-12-18 16:23:31 -0800180 ln -sf "${CHROOT_TRUNK_DIR}/src/third_party/chromiumos-overlay" \
Brian Harringf539bc32012-02-06 00:18:37 -0800181 "${FLAGS_chroot}"/"${CHROOT_OVERLAY}"
Brian Harring2499bfb2012-12-18 16:23:31 -0800182 ln -sf "${CHROOT_TRUNK_DIR}/src/third_party/portage-stable" \
Brian Harringf539bc32012-02-06 00:18:37 -0800183 "${FLAGS_chroot}"/"${PORTAGE_STABLE_OVERLAY}"
Brian Harringf539bc32012-02-06 00:18:37 -0800184
Brian Harring35767822012-02-01 23:50:45 -0800185 # Some operations need an mtab.
Mike Frysingerb47a4ff2013-02-20 17:29:05 -0500186 ln -sfT /proc/mounts "${FLAGS_chroot}/etc/mtab"
Brian Harringf539bc32012-02-06 00:18:37 -0800187
188 # Set up sudoers. Inside the chroot, the user can sudo without a password.
189 # (Safe enough, since the only way into the chroot is to 'sudo chroot', so
190 # the user's already typed in one sudo password...)
191 # Make sure the sudoers.d subdir exists as older stage3 base images lack it.
David James76764882012-10-24 19:46:29 -0700192 mkdir -p "${FLAGS_chroot}/etc/sudoers.d"
Brian Harring06d3c2e2012-08-23 07:35:43 -0700193
194 # Use the standardized upgrade script to setup proxied vars.
David James76764882012-10-24 19:46:29 -0700195 load_environment_whitelist
196 bash -e "${SCRIPT_ROOT}/chroot_version_hooks.d/45_rewrite_sudoers.d" \
197 "${FLAGS_chroot}" "${SUDO_USER}" "${ENVIRONMENT_WHITELIST[@]}"
Brian Harring06d3c2e2012-08-23 07:35:43 -0700198
David James76764882012-10-24 19:46:29 -0700199 find "${FLAGS_chroot}/etc/"sudoers* -type f -exec chmod 0440 {} +
Brian Harring35767822012-02-01 23:50:45 -0800200 # Fix bad group for some.
David James76764882012-10-24 19:46:29 -0700201 chown -R root:root "${FLAGS_chroot}/etc/"sudoers*
Brian Harringf539bc32012-02-06 00:18:37 -0800202
Brian Harring35767822012-02-01 23:50:45 -0800203 info "Setting up hosts/resolv..."
204 # Copy config from outside chroot into chroot.
David James76764882012-10-24 19:46:29 -0700205 cp /etc/{hosts,resolv.conf} "$FLAGS_chroot/etc/"
206 chmod 0644 "$FLAGS_chroot"/etc/{hosts,resolv.conf}
Brian Harringf539bc32012-02-06 00:18:37 -0800207
208 # Setup host make.conf. This includes any overlay that we may be using
209 # and a pointer to pre-built packages.
Brian Harring35767822012-02-01 23:50:45 -0800210 # TODO: This should really be part of a profile in the portage.
211 info "Setting up /etc/make.*..."
Mike Frysingerc6e1ace2013-03-22 00:49:30 -0400212 rm -f "${FLAGS_chroot}"/etc/{,portage/}make.{conf,profile}
213 mkdir -p "${FLAGS_chroot}/etc/portage"
David James76764882012-10-24 19:46:29 -0700214 ln -sf "${CHROOT_CONFIG}/make.conf.amd64-host" \
Brian Harringf539bc32012-02-06 00:18:37 -0800215 "${FLAGS_chroot}/etc/make.conf"
David James76764882012-10-24 19:46:29 -0700216 ln -sf "${CHROOT_OVERLAY}/profiles/default/linux/amd64/10.0" \
Brian Harringf539bc32012-02-06 00:18:37 -0800217 "${FLAGS_chroot}/etc/make.profile"
218
Brian Harring35767822012-02-01 23:50:45 -0800219 # Create make.conf.user .
David James76764882012-10-24 19:46:29 -0700220 touch "${FLAGS_chroot}"/etc/make.conf.user
221 chmod 0644 "${FLAGS_chroot}"/etc/make.conf.user
Brian Harringf539bc32012-02-06 00:18:37 -0800222
223 # Create directories referred to by our conf files.
David James76764882012-10-24 19:46:29 -0700224 mkdir -p -m 775 "${FLAGS_chroot}/var/lib/portage/pkgs" \
Brian Harring7b6f3772012-09-23 14:01:13 -0700225 "${FLAGS_chroot}/var/cache/"chromeos-{cache,chrome} \
226 "${FLAGS_chroot}/etc/profile.d"
227
David James76764882012-10-24 19:46:29 -0700228 echo "export CHROMEOS_CACHEDIR=/var/cache/chromeos-cache" > \
229 "${FLAGS_chroot}/etc/profile.d/chromeos-cachedir.sh"
230 chmod 0644 "${FLAGS_chroot}/etc/profile.d/chromeos-cachedir.sh"
231 rm -rf "${FLAGS_chroot}/var/cache/distfiles"
232 ln -s chromeos-cache/distfiles "${FLAGS_chroot}/var/cache/distfiles"
Brian Harring36b102b2012-02-06 23:34:25 -0800233
234 # Run this from w/in the chroot so we use whatever uid/gid
235 # these are defined as w/in the chroot.
David James76764882012-10-24 19:46:29 -0700236 bare_chroot chown "${SUDO_USER}:portage" /var/cache/chromeos-chrome
Brian Harring7ee892d2012-02-02 09:33:10 -0800237
238 # These are created for compatibility while transitioning
239 # make.conf and friends over to the new location.
Brian Harring7b6f3772012-09-23 14:01:13 -0700240 # TODO(ferringb): remove this 01/13 or so.
David James76764882012-10-24 19:46:29 -0700241 ln -s ../../cache/chromeos-cache/distfiles/host \
Brian Harring7ee892d2012-02-02 09:33:10 -0800242 "${FLAGS_chroot}/var/lib/portage/distfiles"
David James76764882012-10-24 19:46:29 -0700243 ln -s ../../cache/chromeos-cache/distfiles/target \
Brian Harring7ee892d2012-02-02 09:33:10 -0800244 "${FLAGS_chroot}/var/lib/portage/distfiles-target"
Brian Harringf539bc32012-02-06 00:18:37 -0800245
Brian Harringf539bc32012-02-06 00:18:37 -0800246 # Add chromite/bin and depot_tools into the path globally; note that the
247 # chromite wrapper itself might also be found in depot_tools.
248 # We rely on 'env-update' getting called below.
249 target="${FLAGS_chroot}/etc/env.d/99chromiumos"
David James76764882012-10-24 19:46:29 -0700250 cat <<EOF > "${target}"
Brian Harring2499bfb2012-12-18 16:23:31 -0800251PATH=${CHROOT_TRUNK_DIR}/chromite/bin:${DEPOT_TOOLS_DIR}
252CROS_WORKON_SRCROOT="${CHROOT_TRUNK_DIR}"
David James76764882012-10-24 19:46:29 -0700253PORTAGE_USERNAME=${SUDO_USER}
Brian Harringf539bc32012-02-06 00:18:37 -0800254EOF
255
256 # TODO(zbehan): Configure stuff that is usually configured in postinst's,
Mike Frysingereb1a9b42012-03-28 16:21:04 -0400257 # but wasn't. Fix the postinst's.
Brian Harring35767822012-02-01 23:50:45 -0800258 info "Running post-inst configuration hacks"
259 early_enter_chroot env-update
Brian Harringf539bc32012-02-06 00:18:37 -0800260
Brian Harring35767822012-02-01 23:50:45 -0800261 # This is basically a sanity check of our chroot. If any of these
262 # don't exist, then either bind mounts have failed, an invocation
263 # from above is broke, or some assumption about the stage3 is no longer
264 # true.
265 early_enter_chroot ls -l /etc/make.{conf,profile} \
266 /usr/local/portage/chromiumos/profiles/default/linux/amd64/10.0
Brian Harringf539bc32012-02-06 00:18:37 -0800267
268 target="${FLAGS_chroot}/etc/profile.d"
David James76764882012-10-24 19:46:29 -0700269 mkdir -p "${target}"
270 cat << EOF > "${target}/chromiumos-niceties.sh"
Brian Harringf539bc32012-02-06 00:18:37 -0800271# Niceties for interactive logins. (cr) denotes this is a chroot, the
272# __git_branch_ps1 prints current git branch in ./ . The $r behavior is to
273# make sure we don't reset the previous $? value which later formats in
274# $PS1 might rely on.
275PS1='\$(r=\$?; __git_branch_ps1 "(%s) "; exit \$r)'"\${PS1}"
276PS1="(cr) \${PS1}"
277EOF
278
279 # Select a small set of locales for the user if they haven't done so
280 # already. This makes glibc upgrades cheap by only generating a small
281 # set of locales. The ones listed here are basically for the buildbots
282 # which always assume these are available. This works in conjunction
283 # with `cros_sdk --enter`.
284 # http://crosbug.com/20378
285 local localegen="$FLAGS_chroot/etc/locale.gen"
286 if ! grep -q -v -e '^#' -e '^$' "${localegen}" ; then
David James76764882012-10-24 19:46:29 -0700287 cat <<EOF >> "${localegen}"
Brian Harringf539bc32012-02-06 00:18:37 -0800288en_US ISO-8859-1
289en_US.UTF-8 UTF-8
290EOF
291 fi
292
Brian Harring35767822012-02-01 23:50:45 -0800293 # Automatically change to scripts directory.
Brian Harringf539bc32012-02-06 00:18:37 -0800294 echo 'cd ${CHROOT_CWD:-~/trunk/src/scripts}' \
David James76764882012-10-24 19:46:29 -0700295 | user_append "$FLAGS_chroot/home/${SUDO_USER}/.bash_profile"
Brian Harringf539bc32012-02-06 00:18:37 -0800296
Brian Harring35767822012-02-01 23:50:45 -0800297 # Enable bash completion for build scripts.
Brian Harringf539bc32012-02-06 00:18:37 -0800298 echo ". ~/trunk/src/scripts/bash_completion" \
David James76764882012-10-24 19:46:29 -0700299 | user_append "$FLAGS_chroot/home/${SUDO_USER}/.bashrc"
Brian Harringf539bc32012-02-06 00:18:37 -0800300
Peter Mayoe18c7d42013-06-06 13:41:03 -0400301 if [[ "${SUDO_USER}" = "chrome-bot" && -d "${SUDO_HOME}/.ssh" ]]; then
Brian Harringf539bc32012-02-06 00:18:37 -0800302 # Copy ssh keys, so chroot'd chrome-bot can scp files from chrome-web.
Peter Mayo31056952013-06-06 16:22:55 -0400303 user_cp -rp "${SUDO_HOME}/.ssh" "$FLAGS_chroot/home/${SUDO_USER}/"
Brian Harringf539bc32012-02-06 00:18:37 -0800304 fi
305
David James76764882012-10-24 19:46:29 -0700306 if [[ -f ${SUDO_HOME}/.gitconfig ]]; then
Brian Harring35767822012-02-01 23:50:45 -0800307 # Copy .gitconfig into chroot so repo and git can be used from inside.
308 # This is required for repo to work since it validates the email address.
Brian Harringf539bc32012-02-06 00:18:37 -0800309 echo "Copying ~/.gitconfig into chroot"
David James22dc2ba2012-11-29 15:46:58 -0800310 user_cp "${SUDO_HOME}/.gitconfig" "$FLAGS_chroot/home/${SUDO_USER}/"
Brian Harringf539bc32012-02-06 00:18:37 -0800311 fi
Luigi Semenzato2443fdd2012-05-29 10:34:04 -0700312
David James76764882012-10-24 19:46:29 -0700313 # If the user didn't set up their username in their gitconfig, look
314 # at the default git settings for the user.
315 if ! git config -f "${SUDO_HOME}/.gitconfig" user.email >& /dev/null; then
316 ident=$(cd /; sudo -u ${SUDO_USER} -- git var GIT_COMMITTER_IDENT || :)
317 ident_name=${ident%% <*}
318 ident_email=${ident%%>*}; ident_email=${ident_email##*<}
319 gitconfig=${FLAGS_chroot}/home/${SUDO_USER}/.gitconfig
320 git config -f ${gitconfig} --replace-all user.name "${ident_name}" || :
321 git config -f ${gitconfig} --replace-all user.email "${ident_email}" || :
322 chown ${SUDO_UID}:${SUDO_GID} ${FLAGS_chroot}/home/${SUDO_USER}/.gitconfig
323 fi
324
325 if [[ -f ${SUDO_HOME}/.cros_chroot_init ]]; then
326 sudo -u ${SUDO_USER} -- /bin/bash "${SUDO_HOME}/.cros_chroot_init" \
327 "${FLAGS_chroot}"
Luigi Semenzato2443fdd2012-05-29 10:34:04 -0700328 fi
Brian Harringf539bc32012-02-06 00:18:37 -0800329}
330
Brian Harring35767822012-02-01 23:50:45 -0800331# Handle deleting an existing environment.
Brian Harringf539bc32012-02-06 00:18:37 -0800332if [[ $FLAGS_delete -eq $FLAGS_TRUE || \
Brian Harring35767822012-02-01 23:50:45 -0800333 $FLAGS_replace -eq $FLAGS_TRUE ]]; then
Brian Harringf539bc32012-02-06 00:18:37 -0800334 delete_existing
335 [[ $FLAGS_delete -eq $FLAGS_TRUE ]] && exit 0
336fi
337
338CHROOT_TRUNK="${CHROOT_TRUNK_DIR}"
339PORTAGE="${SRC_ROOT}/third_party/portage"
340OVERLAY="${SRC_ROOT}/third_party/chromiumos-overlay"
341CONFIG_DIR="${OVERLAY}/chromeos/config"
Brian Harring2499bfb2012-12-18 16:23:31 -0800342CHROOT_CONFIG="${CHROOT_TRUNK_DIR}/src/third_party/chromiumos-overlay/chromeos/config"
Mike Frysingerb45dbb52013-12-13 21:14:09 -0500343OVERLAYS_ROOT="/usr/local/portage"
344ECLASS_OVERLAY="${OVERLAYS_ROOT}/eclass-overlay"
345PORTAGE_STABLE_OVERLAY="${OVERLAYS_ROOT}/stable"
346CROSSDEV_OVERLAY="${OVERLAYS_ROOT}/crossdev"
347CHROOT_OVERLAY="${OVERLAYS_ROOT}/chromiumos"
Brian Harringf539bc32012-02-06 00:18:37 -0800348CHROOT_STATE="${FLAGS_chroot}/etc/debian_chroot"
Vincent Palatin5ce24062014-02-26 12:08:23 -0800349CHROOT_VERSION="${FLAGS_chroot}/etc/cros_chroot_version"
Brian Harringf539bc32012-02-06 00:18:37 -0800350
351# Pass proxy variables into the environment.
352for type in http ftp all; do
353 value=$(env | grep ${type}_proxy || true)
354 if [ -n "${value}" ]; then
355 CHROOT_PASSTHRU+=("$value")
356 fi
357done
358
Brian Harring35767822012-02-01 23:50:45 -0800359# Create the destination directory.
Brian Harringf539bc32012-02-06 00:18:37 -0800360mkdir -p "$FLAGS_chroot"
361
362echo
Mike Frysinger51930072014-04-05 13:01:45 -0400363if [[ -f ${CHROOT_STATE} ]]; then
364 info "stage3 already set up. Skipping..."
365elif [[ -z ${FLAGS_stage3_path} ]]; then
366 die_notrace "Please use --stage3_path when bootstrapping"
Brian Harringf539bc32012-02-06 00:18:37 -0800367else
Mike Frysinger51930072014-04-05 13:01:45 -0400368 info "Unpacking stage3..."
369 case ${FLAGS_stage3_path} in
Zdenek Behan074f9ef2012-05-30 01:23:59 +0200370 *.tbz2|*.tar.bz2) DECOMPRESS=$(type -p pbzip2 || echo bzip2) ;;
371 *.tar.xz) DECOMPRESS="xz" ;;
Mike Frysinger51930072014-04-05 13:01:45 -0400372 *) die "Unknown tarball compression: ${FLAGS_stage3_path}" ;;
Zdenek Behan074f9ef2012-05-30 01:23:59 +0200373 esac
Mike Frysinger51930072014-04-05 13:01:45 -0400374 ${DECOMPRESS} -dc "${FLAGS_stage3_path}" | \
David James76764882012-10-24 19:46:29 -0700375 tar -xp -C "${FLAGS_chroot}"
376 rm -f "$FLAGS_chroot/etc/"make.{globals,conf.user}
Brian Harringf539bc32012-02-06 00:18:37 -0800377fi
378
Vincent Palatin5ce24062014-02-26 12:08:23 -0800379# Ensure that we properly detect when we are inside the chroot.
380# We'll force this to the latest version at the end as needed.
381if [[ ! -e ${CHROOT_VERSION} ]]; then
382 echo "0" > "${CHROOT_VERSION}"
383fi
384
Brian Harring35767822012-02-01 23:50:45 -0800385# Set up users, if needed, before mkdir/mounts below.
Brian Harringf539bc32012-02-06 00:18:37 -0800386[ -f $CHROOT_STATE ] || init_users
387
Brian Harring2499bfb2012-12-18 16:23:31 -0800388# Reset internal vars to force them to the 'inside the chroot' value;
389# since user directories now exist, this can do the upgrade in place.
390set_chroot_trunk_dir "${FLAGS_chroot}" poppycock
391
Brian Harringf539bc32012-02-06 00:18:37 -0800392echo
Brian Harring35767822012-02-01 23:50:45 -0800393info "Setting up mounts..."
394# Set up necessary mounts and make sure we clean them up on exit.
Brian Harring2499bfb2012-12-18 16:23:31 -0800395mkdir -p "${FLAGS_chroot}/${CHROOT_TRUNK_DIR}" \
396 "${FLAGS_chroot}/${DEPOT_TOOLS_DIR}" "${FLAGS_chroot}/run"
Brian Harring35767822012-02-01 23:50:45 -0800397
J. Richard Barnettee80f6de2012-02-24 14:08:34 -0800398# Create a special /etc/make.conf.host_setup that we use to bootstrap
399# the chroot. The regular content for the file will be generated the
400# first time we invoke update_chroot (further down in this script).
401create_bootstrap_host_setup "${FLAGS_chroot}"
Brian Harringf539bc32012-02-06 00:18:37 -0800402
403if ! [ -f "$CHROOT_STATE" ];then
404 INITIALIZE_CHROOT=1
405fi
406
Mike Frysingerba758452012-04-02 13:28:31 -0400407if ! early_enter_chroot bash -c 'type -P pbzip2' >/dev/null ; then
408 # This chroot lacks pbzip2 early on, so we need to disable it.
409 early_env+=(
410 PORTAGE_BZIP2_COMMAND="bzip2"
411 PORTAGE_BUNZIP2_COMMAND="bunzip2"
412 )
413fi
414
Brian Harringf539bc32012-02-06 00:18:37 -0800415if [ -z "${INITIALIZE_CHROOT}" ];then
Brian Harring35767822012-02-01 23:50:45 -0800416 info "chroot already initialized. Skipping..."
Brian Harringf539bc32012-02-06 00:18:37 -0800417else
Brian Harring35767822012-02-01 23:50:45 -0800418 # Run all the init stuff to setup the env.
Brian Harringf539bc32012-02-06 00:18:37 -0800419 init_setup
420fi
421
Brian Harring35767822012-02-01 23:50:45 -0800422# Add file to indicate that it is a chroot.
Mike Frysinger51930072014-04-05 13:01:45 -0400423# Add version of stage3 for update checks.
424echo "STAGE3=${FLAGS_stage3_path}" > "${CHROOT_STATE}"
Brian Harringf539bc32012-02-06 00:18:37 -0800425
Brian Harring35767822012-02-01 23:50:45 -0800426info "Updating portage"
Mike Frysinger66fd81f2012-02-28 13:13:20 -0500427early_enter_chroot emerge -uNv --quiet portage
Brian Harringf539bc32012-02-06 00:18:37 -0800428
Mike Frysinger9a2978c2013-08-04 11:38:43 -0400429# Clear out openrc if it's installed as we don't want it.
430if [[ -e ${FLAGS_chroot}/usr/share/openrc ]]; then
431 info "Uninstalling openrc"
432 early_enter_chroot env CLEAN_DELAY=0 emerge -qC sys-apps/openrc
Mike Frysinger47e599e2013-10-28 13:30:12 -0400433 # Now update baselayout to get our functions.sh. The unmerge
434 # above removed our copy in the process.
435 early_enter_chroot emerge -uNvq sys-apps/baselayout
Mike Frysinger9a2978c2013-08-04 11:38:43 -0400436fi
437
David James31097c32013-11-06 18:56:59 -0800438# The stage3 contains an old version of Python. Upgrade it first so that
439# parallel_emerge (and chromite libs) can use the latest Python syntax.
440info "Updating python-2.x"
441early_enter_chroot emerge -uNvq =dev-lang/python-2*
442
443# New versions of the stage3 have Python 3 set as the default. Get rid of it,
444# as our scripts are only compatible with Python 2.
445early_enter_chroot eselect python set 1
446early_enter_chroot env CLEAN_DELAY=0 emerge -qC =dev-lang/python-3* || true
Mike Frysinger83520dd2013-03-22 01:37:37 -0400447
Bertrand SIMONNET85c96c72014-08-20 12:10:48 -0700448# Add chromite into python path.
449# This needs to happen after the python update or the correct /usr/lib/python2.*
450# may not exist.
451for python_path in "${FLAGS_chroot}/usr/lib/"python2.*; do
452 python_path+="/site-packages"
453 sudo mkdir -p "${python_path}"
454 sudo ln -s -fT "${CHROOT_TRUNK_DIR}"/chromite "${python_path}"/chromite
455done
456
Paul Drews8bae3b52012-10-10 11:18:13 -0700457# Packages that inherit cros-workon commonly get a circular dependency
458# curl->openssl->git->curl that is broken by emerging an early version of git
459# without curl (and webdav that depends on it).
Mike Frysinger96c5c1c2012-10-30 18:59:00 -0400460# We also need to do this before the toolchain as those will sometimes also
461# fetch via remote git trees (for some bot configs).
Paul Drews8bae3b52012-10-10 11:18:13 -0700462if [[ ! -e "${FLAGS_chroot}/usr/bin/git" ]]; then
Paul Drews8bae3b52012-10-10 11:18:13 -0700463 info "Updating early git"
Mike Frysinger96c5c1c2012-10-30 18:59:00 -0400464 USE="-curl -webdav" early_enter_chroot $EMERGE_CMD -uNv $USEPKG dev-vcs/git
465
Mike Frysinger96c5c1c2012-10-30 18:59:00 -0400466 early_enter_chroot $EMERGE_CMD -uNv $USEPKG --select $EMERGE_JOBS \
467 dev-libs/openssl net-misc/curl
468
469 # (Re-)emerge the full version of git.
470 info "Updating full version of git"
471 early_enter_chroot $EMERGE_CMD -uNv $USEPKG dev-vcs/git
Paul Drews8bae3b52012-10-10 11:18:13 -0700472fi
473
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100474info "Updating host toolchain"
Mike Frysinger96c5c1c2012-10-30 18:59:00 -0400475early_enter_chroot $EMERGE_CMD -uNv crossdev
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100476TOOLCHAIN_ARGS=( --deleteold )
477if [[ ${FLAGS_usepkg} -eq ${FLAGS_FALSE} ]]; then
478 TOOLCHAIN_ARGS+=( --nousepkg )
479fi
480# Note: early_enter_chroot executes as root.
Brian Harring2499bfb2012-12-18 16:23:31 -0800481early_enter_chroot "${CHROOT_TRUNK_DIR}/chromite/bin/cros_setup_toolchains" \
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100482 --hostonly "${TOOLCHAIN_ARGS[@]}"
Brian Harringf539bc32012-02-06 00:18:37 -0800483
Mike Frysinger279f1032012-05-17 15:54:31 -0400484info "Running emerge curl sudo ..."
Mike Frysinger650bf872012-02-27 11:05:26 -0500485early_enter_chroot $EMERGE_CMD -uNv $USEPKG --select $EMERGE_JOBS \
Paul Drews8bae3b52012-10-10 11:18:13 -0700486 pbzip2 dev-libs/openssl net-misc/curl sudo
487
Brian Harringf539bc32012-02-06 00:18:37 -0800488if [ -n "${INITIALIZE_CHROOT}" ]; then
489 # If we're creating a new chroot, we also want to set it to the latest
490 # version.
Brian Harring35767822012-02-01 23:50:45 -0800491 enter_chroot \
Brian Harring2499bfb2012-12-18 16:23:31 -0800492 "${CHROOT_TRUNK_DIR}/src/scripts/run_chroot_version_hooks" --force_latest
Brian Harringf539bc32012-02-06 00:18:37 -0800493fi
494
Brian Harring35767822012-02-01 23:50:45 -0800495# Update chroot.
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100496# Skip toolchain update because it already happened above, and the chroot is
497# not ready to emerge all cross toolchains.
498UPDATE_ARGS=( --skip_toolchain_update )
499if [[ ${FLAGS_usepkg} -eq ${FLAGS_TRUE} ]]; then
Brian Harring35767822012-02-01 23:50:45 -0800500 UPDATE_ARGS+=( --usepkg )
Brian Harringf539bc32012-02-06 00:18:37 -0800501else
Brian Harring35767822012-02-01 23:50:45 -0800502 UPDATE_ARGS+=( --nousepkg )
Brian Harringf539bc32012-02-06 00:18:37 -0800503fi
504if [[ ${FLAGS_fast} -eq ${FLAGS_TRUE} ]]; then
Brian Harring35767822012-02-01 23:50:45 -0800505 UPDATE_ARGS+=( --fast )
Brian Harringf539bc32012-02-06 00:18:37 -0800506else
Brian Harring35767822012-02-01 23:50:45 -0800507 UPDATE_ARGS+=( --nofast )
Brian Harringf539bc32012-02-06 00:18:37 -0800508fi
David James184e3902012-02-23 20:19:28 -0800509if [[ "${FLAGS_jobs}" -ne -1 ]]; then
510 UPDATE_ARGS+=( --jobs=${FLAGS_jobs} )
511fi
Brian Harring2499bfb2012-12-18 16:23:31 -0800512enter_chroot "${CHROOT_TRUNK_DIR}/src/scripts/update_chroot" "${UPDATE_ARGS[@]}"
Brian Harringf539bc32012-02-06 00:18:37 -0800513
Stefan Zager48c776b2013-10-09 13:01:25 -0700514# The java-config package atm does not support $ROOT. Select a default
515# VM ourselves until that gets fixed upstream.
516enter_chroot sudo java-config --set-system-vm 1
517
Brian Harring35767822012-02-01 23:50:45 -0800518CHROOT_EXAMPLE_OPT=""
519if [[ "$FLAGS_chroot" != "$DEFAULT_CHROOT_DIR" ]]; then
Brian Harringf539bc32012-02-06 00:18:37 -0800520 CHROOT_EXAMPLE_OPT="--chroot=$FLAGS_chroot"
521fi
522
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100523# As a final pass, build all desired cross-toolchains.
524info "Updating toolchains"
Brian Harring2499bfb2012-12-18 16:23:31 -0800525enter_chroot sudo -E "${CHROOT_TRUNK_DIR}/chromite/bin/cros_setup_toolchains" \
Zdenek Behan2fbd5af2012-03-12 19:38:50 +0100526 "${TOOLCHAIN_ARGS[@]}"
527
Matt Tennant0a9d32d2012-07-30 16:51:37 -0700528command_completed
Brian Harringf539bc32012-02-06 00:18:37 -0800529
Brian Harring35767822012-02-01 23:50:45 -0800530cat <<EOF
Mike Frysingerbdc4fb12012-02-10 11:20:03 -0500531
532${CROS_LOG_PREFIX:-cros_sdk}: All set up. To enter the chroot, run:
533$ cros_sdk --enter $CHROOT_EXAMPLE_OPT
Brian Harring35767822012-02-01 23:50:45 -0800534
535CAUTION: Do *NOT* rm -rf the chroot directory; if there are stale bind
536mounts you may end up deleting your source tree too. To unmount and
537delete the chroot cleanly, use:
538$ cros_sdk --delete $CHROOT_EXAMPLE_OPT
Mike Frysingerbdc4fb12012-02-10 11:20:03 -0500539
Brian Harring35767822012-02-01 23:50:45 -0800540EOF
David James22dc2ba2012-11-29 15:46:58 -0800541
542warn_if_nfs "${SUDO_HOME}"