Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
Matt Tennant | 0a9d32d | 2012-07-30 16:51:37 -0700 | [diff] [blame] | 3 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 4 | # 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 | |
| 12 | SCRIPT_ROOT=$(readlink -f $(dirname "$0")/..) |
| 13 | . "${SCRIPT_ROOT}/common.sh" || exit 1 |
| 14 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 15 | ENTER_CHROOT=$(readlink -f $(dirname "$0")/enter_chroot.sh) |
| 16 | |
Zdenek Behan | 4d21a29 | 2012-08-17 04:02:29 +0200 | [diff] [blame] | 17 | if [ -n "${USE}" ]; then |
| 18 | echo "$SCRIPT_NAME: Building with a non-empty USE: ${USE}" |
| 19 | echo "This modifies the expected behaviour and can fail." |
| 20 | fi |
| 21 | |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 22 | # Check if the host machine architecture is supported. |
| 23 | ARCHITECTURE="$(uname -m)" |
| 24 | if [[ "$ARCHITECTURE" != "x86_64" ]]; then |
| 25 | echo "$SCRIPT_NAME: $ARCHITECTURE is not supported as a host machine architecture." |
| 26 | exit 1 |
| 27 | fi |
| 28 | |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 29 | # Script must be run outside the chroot and as root. |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 30 | assert_outside_chroot |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 31 | assert_root_user |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 32 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 33 | # Define command line flags. |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 34 | # See http://code.google.com/p/shflags/wiki/Documentation10x |
| 35 | |
| 36 | DEFINE_string chroot "$DEFAULT_CHROOT_DIR" \ |
| 37 | "Destination dir for the chroot environment." |
| 38 | DEFINE_boolean usepkg $FLAGS_TRUE "Use binary packages to bootstrap." |
| 39 | DEFINE_boolean delete $FLAGS_FALSE "Delete an existing chroot." |
| 40 | DEFINE_boolean replace $FLAGS_FALSE "Overwrite existing chroot, if any." |
| 41 | DEFINE_integer jobs -1 "How many packages to build in parallel at maximum." |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 42 | DEFINE_string stage3_date "2010.03.09" \ |
| 43 | "Use the stage3 with the given date." |
| 44 | DEFINE_string stage3_path "" \ |
| 45 | "Use the stage3 located on this path." |
Gilad Arnold | 8c8e085 | 2015-06-03 16:19:16 -0700 | [diff] [blame] | 46 | DEFINE_string toolchains_overlay_path "" \ |
| 47 | "Use the toolchains overlay located on this path." |
Brian Harring | 7b6f377 | 2012-09-23 14:01:13 -0700 | [diff] [blame] | 48 | DEFINE_string cache_dir "" "Directory to store caches within." |
Benjamin Gordon | 1d5afed | 2017-06-14 16:35:32 -0600 | [diff] [blame] | 49 | DEFINE_boolean useimage $FLAGS_FALSE "Mount the chroot on a loopback image." |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 50 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 51 | # Parse command line flags. |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 52 | FLAGS_HELP="usage: $SCRIPT_NAME [flags]" |
| 53 | FLAGS "$@" || exit 1 |
| 54 | eval set -- "${FLAGS_ARGV}" |
| 55 | check_flags_only_and_allow_null_arg "$@" && set -- |
| 56 | |
Peter Mayo | 4411efe | 2012-09-21 04:41:27 -0400 | [diff] [blame] | 57 | CROS_LOG_PREFIX=cros_sdk:make_chroot |
David Pursell | d35d7af | 2015-05-04 16:37:08 -0700 | [diff] [blame] | 58 | SUDO_HOME=$(eval echo ~"${SUDO_USER}") |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 59 | |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 60 | # Set the right umask for chroot creation. |
| 61 | umask 022 |
| 62 | |
| 63 | # Only now can we die on error. shflags functions leak non-zero error codes, |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 64 | # so will die prematurely if 'switch_to_strict_mode' is specified before now. |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 65 | # TODO: replace shflags with something less error-prone, or contribute a fix. |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 66 | switch_to_strict_mode |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 67 | |
Brian Harring | 7b6f377 | 2012-09-23 14:01:13 -0700 | [diff] [blame] | 68 | [[ "${FLAGS_delete}" == "${FLAGS_FALSE}" ]] && \ |
| 69 | [[ -z "${FLAGS_cache_dir}" ]] && \ |
| 70 | die "--cache_dir is required" |
| 71 | |
J. Richard Barnette | e80f6de | 2012-02-24 14:08:34 -0800 | [diff] [blame] | 72 | . "${SCRIPT_ROOT}"/sdk_lib/make_conf_util.sh |
| 73 | |
David Pursell | d35d7af | 2015-05-04 16:37:08 -0700 | [diff] [blame] | 74 | PRIMARY_GROUP=$(id -g -n "${SUDO_USER}") |
| 75 | PRIMARY_GROUP_ID=$(id -g "${SUDO_USER}") |
| 76 | |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 77 | FULLNAME="ChromeOS Developer" |
David Pursell | d35d7af | 2015-05-04 16:37:08 -0700 | [diff] [blame] | 78 | DEFGROUPS="${PRIMARY_GROUP},adm,cdrom,floppy,audio,video,portage" |
| 79 | |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 80 | USEPKG="" |
| 81 | if [[ $FLAGS_usepkg -eq $FLAGS_TRUE ]]; then |
| 82 | # Use binary packages. Include all build-time dependencies, |
| 83 | # so as to avoid unnecessary differences between source |
| 84 | # and binary builds. |
| 85 | USEPKG="--getbinpkg --usepkg --with-bdeps y" |
| 86 | fi |
| 87 | |
Bertrand SIMONNET | 4dda4f5 | 2015-03-19 13:40:58 -0700 | [diff] [blame] | 88 | EMERGE_CMD="${CHROOT_TRUNK_DIR}/chromite/bin/parallel_emerge" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 89 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 90 | ENTER_CHROOT_ARGS=( |
| 91 | CROS_WORKON_SRCROOT="$CHROOT_TRUNK" |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 92 | PORTAGE_USERNAME="${SUDO_USER}" |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 93 | IGNORE_PREFLIGHT_BINHOST="$IGNORE_PREFLIGHT_BINHOST" |
| 94 | ) |
| 95 | |
| 96 | # Invoke enter_chroot. This can only be used after sudo has been installed. |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 97 | enter_chroot() { |
Brian Harring | 7b6f377 | 2012-09-23 14:01:13 -0700 | [diff] [blame] | 98 | "$ENTER_CHROOT" --cache_dir "${FLAGS_cache_dir}" --chroot "$FLAGS_chroot" \ |
| 99 | -- "${ENTER_CHROOT_ARGS[@]}" "$@" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 102 | # Invoke enter_chroot running the command as root, and w/out sudo. |
| 103 | # This should be used prior to sudo being merged. |
Mike Frysinger | ba75845 | 2012-04-02 13:28:31 -0400 | [diff] [blame] | 104 | early_env=() |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 105 | early_enter_chroot() { |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 106 | "$ENTER_CHROOT" --chroot "$FLAGS_chroot" --early_make_chroot \ |
Brian Harring | 7b6f377 | 2012-09-23 14:01:13 -0700 | [diff] [blame] | 107 | --cache_dir "${FLAGS_cache_dir}" \ |
Mike Frysinger | ba75845 | 2012-04-02 13:28:31 -0400 | [diff] [blame] | 108 | -- "${ENTER_CHROOT_ARGS[@]}" "${early_env[@]}" "$@" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Mike Frysinger | 4bb8645 | 2013-08-02 13:44:18 -0400 | [diff] [blame] | 111 | # 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 James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 117 | bare_chroot() { |
David Pursell | d35d7af | 2015-05-04 16:37:08 -0700 | [diff] [blame] | 118 | PATH="/bin:/sbin:/usr/bin:/usr/sbin:${PATH}" \ |
Mike Frysinger | 4bb8645 | 2013-08-02 13:44:18 -0400 | [diff] [blame] | 119 | chroot "${FLAGS_chroot}" "$@" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 122 | cleanup() { |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 123 | # Clean up mounts |
| 124 | safe_umount_tree "${FLAGS_chroot}" |
Benjamin Gordon | 1d5afed | 2017-06-14 16:35:32 -0600 | [diff] [blame] | 125 | |
| 126 | # Destroy LVM loopback setup if we can find a VG associated with our path. |
| 127 | local chroot_img="${FLAGS_chroot}.img" |
| 128 | [[ -f "$chroot_img" ]] || return 0 |
| 129 | |
| 130 | local chroot_dev=$(losetup -j "$chroot_img" | cut -f1 -d:) |
| 131 | local chroot_vg=$(find_vg_name "$FLAGS_chroot" "$chroot_dev") |
| 132 | if [ -n "$chroot_vg" ] && vgs "$chroot_vg" >&/dev/null; then |
| 133 | info "Removing VG $chroot_vg." |
| 134 | vgremove -f "$chroot_vg" --noudevsync |
| 135 | fi |
| 136 | if [ -n "$chroot_dev" ]; then |
| 137 | info "Detaching $chroot_dev." |
| 138 | losetup -d "$chroot_dev" |
| 139 | fi |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 142 | delete_existing() { |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 143 | # Delete old chroot dir. |
Benjamin Gordon | 1d5afed | 2017-06-14 16:35:32 -0600 | [diff] [blame] | 144 | local chroot_img="${FLAGS_chroot}.img" |
| 145 | if [[ ! -e "$FLAGS_chroot" && ! -f "$chroot_img" ]]; then |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 146 | return |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 147 | fi |
Benjamin Gordon | 1d5afed | 2017-06-14 16:35:32 -0600 | [diff] [blame] | 148 | info "Cleaning up old mount points and loopback device..." |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 149 | cleanup |
| 150 | info "Deleting $FLAGS_chroot..." |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 151 | rm -rf "$FLAGS_chroot" |
Benjamin Gordon | 1d5afed | 2017-06-14 16:35:32 -0600 | [diff] [blame] | 152 | if [[ -f "$chroot_img" ]]; then |
| 153 | info "Deleting $chroot_img..." |
| 154 | rm -f "$chroot_img" |
| 155 | fi |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 156 | info "Done." |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 157 | } |
| 158 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 159 | init_users () { |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 160 | info "Set timezone..." |
| 161 | # date +%Z has trouble with daylight time, so use host's info. |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 162 | rm -f "${FLAGS_chroot}/etc/localtime" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 163 | if [ -f /etc/localtime ] ; then |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 164 | cp /etc/localtime "${FLAGS_chroot}/etc" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 165 | else |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 166 | ln -sf /usr/share/zoneinfo/PST8PDT "${FLAGS_chroot}/etc/localtime" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 167 | fi |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 168 | info "Adding user/group..." |
David Pursell | d35d7af | 2015-05-04 16:37:08 -0700 | [diff] [blame] | 169 | # Add the necessary groups to the chroot. |
| 170 | # Duplicate GIDs are allowed here in order to ensure that the required |
| 171 | # groups are the same inside and outside the chroot. |
| 172 | # TODO(dpursell): Handle when PRIMARY_GROUP exists in the chroot already |
| 173 | # with a different GID; groupadd will not create the new GID in that case. |
| 174 | bare_chroot groupadd -f -o -g "${PRIMARY_GROUP_ID}" "${PRIMARY_GROUP}" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 175 | # Add ourselves as a user inside the chroot. |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 176 | # We need the UID to match the host user's. This can conflict with |
| 177 | # a particular chroot UID. At the same time, the added user has to |
| 178 | # be a primary user for the given UID for sudo to work, which is |
| 179 | # determined by the order in /etc/passwd. Let's put ourselves on top |
| 180 | # of the file. |
David Pursell | d35d7af | 2015-05-04 16:37:08 -0700 | [diff] [blame] | 181 | bare_chroot useradd -o -G "${DEFGROUPS}" -g "${PRIMARY_GROUP}" \ |
| 182 | -u "${SUDO_UID}" -s /bin/bash -m -c "${FULLNAME}" "${SUDO_USER}" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 183 | # Because passwd generally isn't sorted and the entry ended up at the |
| 184 | # bottom, it is safe to just take it and move it to top instead. |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 185 | sed -e '1{h;d};$!{H;d};$G' -i "${FLAGS_chroot}/etc/passwd" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 188 | init_setup () { |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 189 | info "Running init_setup()..." |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 190 | mkdir -p -m 755 "${FLAGS_chroot}/usr" \ |
Mike Frysinger | b45dbb5 | 2013-12-13 21:14:09 -0500 | [diff] [blame] | 191 | "${FLAGS_chroot}${OVERLAYS_ROOT}" \ |
Mike Frysinger | 36beaaa | 2014-09-25 15:52:27 -0400 | [diff] [blame] | 192 | "${FLAGS_chroot}"/"${CROSSDEV_OVERLAY}/metadata" |
| 193 | # Newer portage complains about bare overlays. Create the file that crossdev |
| 194 | # will also create later on. |
| 195 | cat <<EOF > "${FLAGS_chroot}/${CROSSDEV_OVERLAY}/metadata/layout.conf" |
| 196 | # Autogenerated and managed by crossdev |
| 197 | # Delete the above line if you want to manage this file yourself |
| 198 | masters = portage-stable chromiumos |
| 199 | repo-name = crossdev |
| 200 | use-manifests = true |
| 201 | thin-manifests = true |
| 202 | EOF |
Mike Frysinger | b45dbb5 | 2013-12-13 21:14:09 -0500 | [diff] [blame] | 203 | ln -sf "${CHROOT_TRUNK_DIR}/src/third_party/eclass-overlay" \ |
| 204 | "${FLAGS_chroot}"/"${ECLASS_OVERLAY}" |
Brian Harring | 2499bfb | 2012-12-18 16:23:31 -0800 | [diff] [blame] | 205 | ln -sf "${CHROOT_TRUNK_DIR}/src/third_party/chromiumos-overlay" \ |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 206 | "${FLAGS_chroot}"/"${CHROOT_OVERLAY}" |
Brian Harring | 2499bfb | 2012-12-18 16:23:31 -0800 | [diff] [blame] | 207 | ln -sf "${CHROOT_TRUNK_DIR}/src/third_party/portage-stable" \ |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 208 | "${FLAGS_chroot}"/"${PORTAGE_STABLE_OVERLAY}" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 209 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 210 | # Some operations need an mtab. |
Mike Frysinger | b47a4ff | 2013-02-20 17:29:05 -0500 | [diff] [blame] | 211 | ln -sfT /proc/mounts "${FLAGS_chroot}/etc/mtab" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 212 | |
| 213 | # Set up sudoers. Inside the chroot, the user can sudo without a password. |
| 214 | # (Safe enough, since the only way into the chroot is to 'sudo chroot', so |
| 215 | # the user's already typed in one sudo password...) |
| 216 | # Make sure the sudoers.d subdir exists as older stage3 base images lack it. |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 217 | mkdir -p "${FLAGS_chroot}/etc/sudoers.d" |
Brian Harring | 06d3c2e | 2012-08-23 07:35:43 -0700 | [diff] [blame] | 218 | |
| 219 | # Use the standardized upgrade script to setup proxied vars. |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 220 | load_environment_whitelist |
| 221 | bash -e "${SCRIPT_ROOT}/chroot_version_hooks.d/45_rewrite_sudoers.d" \ |
| 222 | "${FLAGS_chroot}" "${SUDO_USER}" "${ENVIRONMENT_WHITELIST[@]}" |
Brian Harring | 06d3c2e | 2012-08-23 07:35:43 -0700 | [diff] [blame] | 223 | |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 224 | find "${FLAGS_chroot}/etc/"sudoers* -type f -exec chmod 0440 {} + |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 225 | # Fix bad group for some. |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 226 | chown -R root:root "${FLAGS_chroot}/etc/"sudoers* |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 227 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 228 | info "Setting up hosts/resolv..." |
| 229 | # Copy config from outside chroot into chroot. |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 230 | cp /etc/{hosts,resolv.conf} "$FLAGS_chroot/etc/" |
| 231 | chmod 0644 "$FLAGS_chroot"/etc/{hosts,resolv.conf} |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 232 | |
| 233 | # Setup host make.conf. This includes any overlay that we may be using |
| 234 | # and a pointer to pre-built packages. |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 235 | # TODO: This should really be part of a profile in the portage. |
| 236 | info "Setting up /etc/make.*..." |
Mike Frysinger | 53f79bb | 2014-04-05 22:27:52 -0400 | [diff] [blame] | 237 | rm -f "${FLAGS_chroot}"/etc/{,portage/}make.{conf,profile}{,.catalyst} |
Mike Frysinger | c6e1ace | 2013-03-22 00:49:30 -0400 | [diff] [blame] | 238 | mkdir -p "${FLAGS_chroot}/etc/portage" |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 239 | ln -sf "${CHROOT_CONFIG}/make.conf.amd64-host" \ |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 240 | "${FLAGS_chroot}/etc/make.conf" |
Mike Frysinger | 411b242 | 2014-06-04 11:49:17 -0400 | [diff] [blame] | 241 | ln -sf "${CHROOT_OVERLAY}/profiles/default/linux/amd64/10.0/sdk" \ |
Mike Frysinger | e9b4007 | 2015-05-19 05:07:04 -0400 | [diff] [blame] | 242 | "${FLAGS_chroot}/etc/portage/make.profile" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 243 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 244 | # Create make.conf.user . |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 245 | touch "${FLAGS_chroot}"/etc/make.conf.user |
| 246 | chmod 0644 "${FLAGS_chroot}"/etc/make.conf.user |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 247 | |
| 248 | # Create directories referred to by our conf files. |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 249 | mkdir -p -m 775 "${FLAGS_chroot}/var/lib/portage/pkgs" \ |
Brian Harring | 7b6f377 | 2012-09-23 14:01:13 -0700 | [diff] [blame] | 250 | "${FLAGS_chroot}/var/cache/"chromeos-{cache,chrome} \ |
| 251 | "${FLAGS_chroot}/etc/profile.d" |
| 252 | |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 253 | echo "export CHROMEOS_CACHEDIR=/var/cache/chromeos-cache" > \ |
| 254 | "${FLAGS_chroot}/etc/profile.d/chromeos-cachedir.sh" |
| 255 | chmod 0644 "${FLAGS_chroot}/etc/profile.d/chromeos-cachedir.sh" |
| 256 | rm -rf "${FLAGS_chroot}/var/cache/distfiles" |
| 257 | ln -s chromeos-cache/distfiles "${FLAGS_chroot}/var/cache/distfiles" |
Brian Harring | 36b102b | 2012-02-06 23:34:25 -0800 | [diff] [blame] | 258 | |
| 259 | # Run this from w/in the chroot so we use whatever uid/gid |
| 260 | # these are defined as w/in the chroot. |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 261 | bare_chroot chown "${SUDO_USER}:portage" /var/cache/chromeos-chrome |
Brian Harring | 7ee892d | 2012-02-02 09:33:10 -0800 | [diff] [blame] | 262 | |
| 263 | # These are created for compatibility while transitioning |
| 264 | # make.conf and friends over to the new location. |
Brian Harring | 7b6f377 | 2012-09-23 14:01:13 -0700 | [diff] [blame] | 265 | # TODO(ferringb): remove this 01/13 or so. |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 266 | ln -s ../../cache/chromeos-cache/distfiles/host \ |
Brian Harring | 7ee892d | 2012-02-02 09:33:10 -0800 | [diff] [blame] | 267 | "${FLAGS_chroot}/var/lib/portage/distfiles" |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 268 | ln -s ../../cache/chromeos-cache/distfiles/target \ |
Brian Harring | 7ee892d | 2012-02-02 09:33:10 -0800 | [diff] [blame] | 269 | "${FLAGS_chroot}/var/lib/portage/distfiles-target" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 270 | |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 271 | # Add chromite/bin and depot_tools into the path globally; note that the |
| 272 | # chromite wrapper itself might also be found in depot_tools. |
| 273 | # We rely on 'env-update' getting called below. |
| 274 | target="${FLAGS_chroot}/etc/env.d/99chromiumos" |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 275 | cat <<EOF > "${target}" |
David Pursell | d35d7af | 2015-05-04 16:37:08 -0700 | [diff] [blame] | 276 | PATH="${CHROOT_TRUNK_DIR}/chromite/bin:${DEPOT_TOOLS_DIR}" |
Brian Harring | 2499bfb | 2012-12-18 16:23:31 -0800 | [diff] [blame] | 277 | CROS_WORKON_SRCROOT="${CHROOT_TRUNK_DIR}" |
David Pursell | d35d7af | 2015-05-04 16:37:08 -0700 | [diff] [blame] | 278 | PORTAGE_USERNAME="${SUDO_USER}" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 279 | EOF |
| 280 | |
| 281 | # TODO(zbehan): Configure stuff that is usually configured in postinst's, |
Mike Frysinger | eb1a9b4 | 2012-03-28 16:21:04 -0400 | [diff] [blame] | 282 | # but wasn't. Fix the postinst's. |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 283 | info "Running post-inst configuration hacks" |
| 284 | early_enter_chroot env-update |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 285 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 286 | # This is basically a sanity check of our chroot. If any of these |
| 287 | # don't exist, then either bind mounts have failed, an invocation |
| 288 | # from above is broke, or some assumption about the stage3 is no longer |
| 289 | # true. |
Mike Frysinger | e9b4007 | 2015-05-19 05:07:04 -0400 | [diff] [blame] | 290 | early_enter_chroot ls -l /etc/make.conf /etc/portage/make.profile \ |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 291 | /usr/local/portage/chromiumos/profiles/default/linux/amd64/10.0 |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 292 | |
| 293 | target="${FLAGS_chroot}/etc/profile.d" |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 294 | mkdir -p "${target}" |
| 295 | cat << EOF > "${target}/chromiumos-niceties.sh" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 296 | # Niceties for interactive logins. (cr) denotes this is a chroot, the |
| 297 | # __git_branch_ps1 prints current git branch in ./ . The $r behavior is to |
| 298 | # make sure we don't reset the previous $? value which later formats in |
| 299 | # $PS1 might rely on. |
| 300 | PS1='\$(r=\$?; __git_branch_ps1 "(%s) "; exit \$r)'"\${PS1}" |
| 301 | PS1="(cr) \${PS1}" |
| 302 | EOF |
| 303 | |
| 304 | # Select a small set of locales for the user if they haven't done so |
| 305 | # already. This makes glibc upgrades cheap by only generating a small |
| 306 | # set of locales. The ones listed here are basically for the buildbots |
| 307 | # which always assume these are available. This works in conjunction |
| 308 | # with `cros_sdk --enter`. |
| 309 | # http://crosbug.com/20378 |
| 310 | local localegen="$FLAGS_chroot/etc/locale.gen" |
| 311 | if ! grep -q -v -e '^#' -e '^$' "${localegen}" ; then |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 312 | cat <<EOF >> "${localegen}" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 313 | en_US ISO-8859-1 |
| 314 | en_US.UTF-8 UTF-8 |
| 315 | EOF |
| 316 | fi |
| 317 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 318 | # Automatically change to scripts directory. |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 319 | echo 'cd ${CHROOT_CWD:-~/trunk/src/scripts}' \ |
David Pursell | d35d7af | 2015-05-04 16:37:08 -0700 | [diff] [blame] | 320 | | user_append "${FLAGS_chroot}/home/${SUDO_USER}/.bash_profile" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 321 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 322 | # Enable bash completion for build scripts. |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 323 | echo ". ~/trunk/src/scripts/bash_completion" \ |
David Pursell | d35d7af | 2015-05-04 16:37:08 -0700 | [diff] [blame] | 324 | | user_append "${FLAGS_chroot}/home/${SUDO_USER}/.bashrc" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 325 | |
Peter Mayo | e18c7d4 | 2013-06-06 13:41:03 -0400 | [diff] [blame] | 326 | if [[ "${SUDO_USER}" = "chrome-bot" && -d "${SUDO_HOME}/.ssh" ]]; then |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 327 | # Copy ssh keys, so chroot'd chrome-bot can scp files from chrome-web. |
Peter Mayo | 3105695 | 2013-06-06 16:22:55 -0400 | [diff] [blame] | 328 | user_cp -rp "${SUDO_HOME}/.ssh" "$FLAGS_chroot/home/${SUDO_USER}/" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 329 | fi |
| 330 | |
David Pursell | d35d7af | 2015-05-04 16:37:08 -0700 | [diff] [blame] | 331 | if [[ -f "${SUDO_HOME}/.cros_chroot_init" ]]; then |
| 332 | sudo -u "${SUDO_USER}" -- /bin/bash "${SUDO_HOME}/.cros_chroot_init" \ |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 333 | "${FLAGS_chroot}" |
Luigi Semenzato | 2443fdd | 2012-05-29 10:34:04 -0700 | [diff] [blame] | 334 | fi |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 335 | } |
| 336 | |
Gilad Arnold | 01d458b | 2015-05-28 06:41:08 -0700 | [diff] [blame] | 337 | unpack_tarball() { |
| 338 | local tarball_path="$1" |
| 339 | local dest_dir="$2" |
| 340 | local decompress |
| 341 | case "${tarball_path}" in |
| 342 | *.tbz2|*.tar.bz2) decompress=$(type -p pbzip2 || echo bzip2) ;; |
| 343 | *.tar.xz) decompress="xz" ;; |
| 344 | *) die "Unknown tarball compression: ${tarball_path}" ;; |
| 345 | esac |
| 346 | ${decompress} -dc "${tarball_path}" | tar -xp -C "${dest_dir}" |
| 347 | } |
| 348 | |
Benjamin Gordon | 1d5afed | 2017-06-14 16:35:32 -0600 | [diff] [blame] | 349 | # Find a usable VG name for a given path and device. If there is an existing |
| 350 | # VG associated with the device, it will be returned. If not, find an unused |
| 351 | # name in the format cros_<safe_path>_NNN, where safe_path is an escaped version |
| 352 | # of the last 90 characters of the path and NNN is a counter. Example: |
| 353 | # /home/user/chromiumos/chroot/ -> cros_home+user+chromiumos+chroot_000. |
| 354 | # If no unused name with this pattern can be found, return an empty string. |
| 355 | find_vg_name() { |
| 356 | local chroot_path="$1" |
| 357 | local chroot_dev="$2" |
| 358 | chroot_path=${chroot_path##/} |
| 359 | chroot_path=${chroot_path%%/} |
| 360 | chroot_path=${chroot_path//[^A-Za-z0-9_+.-]/+} |
| 361 | chroot_path=${chroot_path: -$((${#chroot_path} < 90 ? ${#chroot_path} : 90))} |
| 362 | local vg_name="" |
| 363 | if [ -n "$chroot_dev" ]; then |
| 364 | vg_name=$(pvs -q --noheadings -o vg_name "$chroot_dev" 2>/dev/null | \ |
| 365 | sed -e 's/^ *//') |
| 366 | fi |
| 367 | if [ -z "$vg_name" ]; then |
| 368 | local counter=0 |
| 369 | vg_name=$(printf "cros_%s_%03d" "$chroot_path" "$counter") |
| 370 | while [ "$counter" -lt 1000 ] && vgs "$vg_name" >&/dev/null; do |
| 371 | counter=$((counter + 1)) |
| 372 | vg_name=$(printf "cros_%s_%03d" "$chroot_path" "$counter") |
| 373 | done |
| 374 | if [ "$counter" -gt 999 ]; then |
| 375 | vg_name="" |
| 376 | fi |
| 377 | fi |
| 378 | echo "$vg_name" |
| 379 | } |
| 380 | |
| 381 | # Create a loopback image and mount it on the chroot path so that we can take |
| 382 | # snapshots before building. If an image already exists, try to mount it. The |
| 383 | # chroot is initially mounted inside a temporary shared chroot.build subtree |
| 384 | # that should have already been set up by the parent process, and then bind |
| 385 | # mounted into the correct final location. The purpose of this indirection is |
| 386 | # so that processes outside our mount namespace can see the top-level chroot |
| 387 | # after we finish. |
| 388 | mount_chroot_image() { |
| 389 | local chroot_image="$1" |
| 390 | local mount_path="$2" |
| 391 | |
| 392 | # Make sure there's an image. |
| 393 | local existing_chroot=0 |
| 394 | local chroot_dev="" |
| 395 | if [ -f "$chroot_image" ]; then |
| 396 | info "Attempting to reuse existing image file ${chroot_image}" |
| 397 | chroot_dev=$(losetup -j "$chroot_image" | cut -f1 -d:) |
| 398 | existing_chroot=1 |
| 399 | else |
| 400 | dd if=/dev/null of="$chroot_image" bs=1G seek=500 >&/dev/null |
| 401 | fi |
| 402 | |
| 403 | # Get/scan a loopback device attached to our image. |
| 404 | if [ -n "$chroot_dev" ]; then |
| 405 | pvscan -q "$chroot_dev" >&/dev/null |
| 406 | else |
| 407 | chroot_dev=$(losetup -f "$chroot_image" --show) |
| 408 | fi |
| 409 | |
| 410 | # Find/create a VG on the loopback device. |
| 411 | chroot_vg=$(find_vg_name "$mount_path" "$chroot_dev") |
| 412 | if [ -z "$chroot_vg" ]; then |
| 413 | die_notrace "Unable to find usable VG name for ${mount_path}." |
| 414 | fi |
| 415 | if vgs "$chroot_vg" >&/dev/null; then |
Benjamin Gordon | f68aa94 | 2017-07-21 17:00:20 -0600 | [diff] [blame] | 416 | vgchange -q -a y --noudevsync "$chroot_vg" >/dev/null || : |
Benjamin Gordon | 1d5afed | 2017-06-14 16:35:32 -0600 | [diff] [blame] | 417 | else |
| 418 | vgcreate -q "$chroot_vg" "$chroot_dev" >/dev/null |
| 419 | fi |
| 420 | |
| 421 | # Find/create an LV inside our VG. If the LV is new, also create the FS. |
| 422 | # We need to pass --noudevsync to lvcreate because we're running inside |
| 423 | # a separate IPC namespace from the udev process. |
| 424 | if lvs "$chroot_vg/chroot" >&/dev/null; then |
Benjamin Gordon | f68aa94 | 2017-07-21 17:00:20 -0600 | [diff] [blame] | 425 | lvchange -q -ay "$chroot_vg/chroot" --noudevsync >/dev/null || : |
Benjamin Gordon | 1d5afed | 2017-06-14 16:35:32 -0600 | [diff] [blame] | 426 | else |
| 427 | lvcreate -q -L 499G -T "${chroot_vg}/thinpool" -V500G -n chroot \ |
| 428 | --noudevsync >/dev/null |
| 429 | mke2fs -q -m 0 -t ext4 "/dev/${chroot_vg}/chroot" |
| 430 | fi |
| 431 | |
| 432 | # Mount the FS into a directory that should have been set up as a shared |
| 433 | # subtree by our parent process, then bind mount it into the place where |
| 434 | # it belongs. The parent will take care of moving the mount to the correct |
| 435 | # final place on the outside of our mount namespace after we exit. |
| 436 | local temp_chroot="${FLAGS_chroot}.build/chroot" |
| 437 | if ! mount -text4 -onoatime "/dev/${chroot_vg}/chroot" "$temp_chroot"; then |
| 438 | local chroot_example_opt="" |
| 439 | if [[ "$mount_path" != "$DEFAULT_CHROOT_DIR" ]]; then |
| 440 | chroot_example_opt="--chroot=$FLAGS_chroot" |
| 441 | fi |
| 442 | |
| 443 | die_notrace <<EOF |
| 444 | |
| 445 | Unable to mount ${chroot_vg}/chroot on ${temp_chroot}. Check for corrupted |
| 446 | image ${chroot_image}, or run |
| 447 | |
| 448 | cros_sdk --delete $chroot_example_opt |
| 449 | |
| 450 | to clean up an old chroot first. |
| 451 | |
| 452 | EOF |
| 453 | fi |
| 454 | mount --make-private "$temp_chroot" |
| 455 | mount --bind "$temp_chroot" "$mount_path" |
| 456 | mount --make-private "$mount_path" |
| 457 | if [ "$existing_chroot" = "1" ]; then |
| 458 | info "Mounted existing chroot image." |
| 459 | fi |
| 460 | } |
| 461 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 462 | # Handle deleting an existing environment. |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 463 | if [[ $FLAGS_delete -eq $FLAGS_TRUE || \ |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 464 | $FLAGS_replace -eq $FLAGS_TRUE ]]; then |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 465 | delete_existing |
| 466 | [[ $FLAGS_delete -eq $FLAGS_TRUE ]] && exit 0 |
| 467 | fi |
| 468 | |
| 469 | CHROOT_TRUNK="${CHROOT_TRUNK_DIR}" |
| 470 | PORTAGE="${SRC_ROOT}/third_party/portage" |
| 471 | OVERLAY="${SRC_ROOT}/third_party/chromiumos-overlay" |
| 472 | CONFIG_DIR="${OVERLAY}/chromeos/config" |
Brian Harring | 2499bfb | 2012-12-18 16:23:31 -0800 | [diff] [blame] | 473 | CHROOT_CONFIG="${CHROOT_TRUNK_DIR}/src/third_party/chromiumos-overlay/chromeos/config" |
Mike Frysinger | b45dbb5 | 2013-12-13 21:14:09 -0500 | [diff] [blame] | 474 | OVERLAYS_ROOT="/usr/local/portage" |
| 475 | ECLASS_OVERLAY="${OVERLAYS_ROOT}/eclass-overlay" |
| 476 | PORTAGE_STABLE_OVERLAY="${OVERLAYS_ROOT}/stable" |
| 477 | CROSSDEV_OVERLAY="${OVERLAYS_ROOT}/crossdev" |
| 478 | CHROOT_OVERLAY="${OVERLAYS_ROOT}/chromiumos" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 479 | CHROOT_STATE="${FLAGS_chroot}/etc/debian_chroot" |
Vincent Palatin | 5ce2406 | 2014-02-26 12:08:23 -0800 | [diff] [blame] | 480 | CHROOT_VERSION="${FLAGS_chroot}/etc/cros_chroot_version" |
Benjamin Gordon | 1d5afed | 2017-06-14 16:35:32 -0600 | [diff] [blame] | 481 | CHROOT_IMAGE="${FLAGS_chroot}.img" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 482 | |
| 483 | # Pass proxy variables into the environment. |
| 484 | for type in http ftp all; do |
David Pursell | d35d7af | 2015-05-04 16:37:08 -0700 | [diff] [blame] | 485 | value=$(env | grep "${type}_proxy" || true) |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 486 | if [ -n "${value}" ]; then |
| 487 | CHROOT_PASSTHRU+=("$value") |
| 488 | fi |
| 489 | done |
| 490 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 491 | # Create the destination directory. |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 492 | mkdir -p "$FLAGS_chroot" |
| 493 | |
Benjamin Gordon | 1d5afed | 2017-06-14 16:35:32 -0600 | [diff] [blame] | 494 | [[ $FLAGS_useimage -eq $FLAGS_TRUE ]] && \ |
| 495 | mount_chroot_image "$CHROOT_IMAGE" "$FLAGS_chroot" |
| 496 | |
| 497 | # If the version contains something non-zero, we were already created and this |
| 498 | # is just a re-mount. |
| 499 | [[ -f "$CHROOT_VERSION" && "$(<$CHROOT_VERSION)" != "0" ]] && exit 0 |
| 500 | |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 501 | echo |
David Pursell | d35d7af | 2015-05-04 16:37:08 -0700 | [diff] [blame] | 502 | if [[ -f "${CHROOT_STATE}" ]]; then |
Mike Frysinger | 5193007 | 2014-04-05 13:01:45 -0400 | [diff] [blame] | 503 | info "stage3 already set up. Skipping..." |
David Pursell | d35d7af | 2015-05-04 16:37:08 -0700 | [diff] [blame] | 504 | elif [[ -z "${FLAGS_stage3_path}" ]]; then |
Mike Frysinger | 5193007 | 2014-04-05 13:01:45 -0400 | [diff] [blame] | 505 | die_notrace "Please use --stage3_path when bootstrapping" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 506 | else |
Mike Frysinger | 5193007 | 2014-04-05 13:01:45 -0400 | [diff] [blame] | 507 | info "Unpacking stage3..." |
Gilad Arnold | 01d458b | 2015-05-28 06:41:08 -0700 | [diff] [blame] | 508 | unpack_tarball "${FLAGS_stage3_path}" "${FLAGS_chroot}" |
Gilad Arnold | 8c8e085 | 2015-06-03 16:19:16 -0700 | [diff] [blame] | 509 | if [[ -n "${FLAGS_toolchains_overlay_path}" ]]; then |
| 510 | info "Unpacking toolchains overlay..." |
| 511 | unpack_tarball "${FLAGS_toolchains_overlay_path}" "${FLAGS_chroot}" |
Gilad Arnold | 01d458b | 2015-05-28 06:41:08 -0700 | [diff] [blame] | 512 | fi |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 513 | rm -f "$FLAGS_chroot/etc/"make.{globals,conf.user} |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 514 | fi |
| 515 | |
Vincent Palatin | 5ce2406 | 2014-02-26 12:08:23 -0800 | [diff] [blame] | 516 | # Ensure that we properly detect when we are inside the chroot. |
| 517 | # We'll force this to the latest version at the end as needed. |
David Pursell | d35d7af | 2015-05-04 16:37:08 -0700 | [diff] [blame] | 518 | if [[ ! -e "${CHROOT_VERSION}" ]]; then |
Vincent Palatin | 5ce2406 | 2014-02-26 12:08:23 -0800 | [diff] [blame] | 519 | echo "0" > "${CHROOT_VERSION}" |
| 520 | fi |
| 521 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 522 | # Set up users, if needed, before mkdir/mounts below. |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 523 | [ -f $CHROOT_STATE ] || init_users |
| 524 | |
Brian Harring | 2499bfb | 2012-12-18 16:23:31 -0800 | [diff] [blame] | 525 | # Reset internal vars to force them to the 'inside the chroot' value; |
| 526 | # since user directories now exist, this can do the upgrade in place. |
| 527 | set_chroot_trunk_dir "${FLAGS_chroot}" poppycock |
| 528 | |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 529 | echo |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 530 | info "Setting up mounts..." |
| 531 | # Set up necessary mounts and make sure we clean them up on exit. |
Brian Harring | 2499bfb | 2012-12-18 16:23:31 -0800 | [diff] [blame] | 532 | mkdir -p "${FLAGS_chroot}/${CHROOT_TRUNK_DIR}" \ |
| 533 | "${FLAGS_chroot}/${DEPOT_TOOLS_DIR}" "${FLAGS_chroot}/run" |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 534 | |
J. Richard Barnette | e80f6de | 2012-02-24 14:08:34 -0800 | [diff] [blame] | 535 | # Create a special /etc/make.conf.host_setup that we use to bootstrap |
| 536 | # the chroot. The regular content for the file will be generated the |
| 537 | # first time we invoke update_chroot (further down in this script). |
| 538 | create_bootstrap_host_setup "${FLAGS_chroot}" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 539 | |
| 540 | if ! [ -f "$CHROOT_STATE" ];then |
| 541 | INITIALIZE_CHROOT=1 |
| 542 | fi |
| 543 | |
Mike Frysinger | ba75845 | 2012-04-02 13:28:31 -0400 | [diff] [blame] | 544 | if ! early_enter_chroot bash -c 'type -P pbzip2' >/dev/null ; then |
| 545 | # This chroot lacks pbzip2 early on, so we need to disable it. |
| 546 | early_env+=( |
| 547 | PORTAGE_BZIP2_COMMAND="bzip2" |
| 548 | PORTAGE_BUNZIP2_COMMAND="bunzip2" |
| 549 | ) |
| 550 | fi |
| 551 | |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 552 | if [ -z "${INITIALIZE_CHROOT}" ];then |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 553 | info "chroot already initialized. Skipping..." |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 554 | else |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 555 | # Run all the init stuff to setup the env. |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 556 | init_setup |
| 557 | fi |
| 558 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 559 | # Add file to indicate that it is a chroot. |
Mike Frysinger | 5193007 | 2014-04-05 13:01:45 -0400 | [diff] [blame] | 560 | # Add version of stage3 for update checks. |
| 561 | echo "STAGE3=${FLAGS_stage3_path}" > "${CHROOT_STATE}" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 562 | |
Daniel Kurtz | 5124484 | 2017-07-25 01:32:04 +0800 | [diff] [blame^] | 563 | # TODO(crbug.com/747810): Remove this once stage3 is updated past 2015-Q1. |
| 564 | # The 2014.09.18 stage3 uses app-admin/eselect-python, but in 2015-Q1 it moved |
| 565 | # to app-eselect/eselect-python. Since portage depends on python-exec, and |
| 566 | # python-exec depends on eselect-python, update python and related packages |
| 567 | # before portage. |
| 568 | info "Updating python" |
| 569 | early_enter_chroot emerge -uNv --quiet python:2.7 python:3.3 |
| 570 | |
| 571 | # New versions of the stage3 have Python 3 set as the default. Make sure we |
| 572 | # default to 2.x as our scripts are only compatible with Python 2. We leave |
| 573 | # Python 3 installed though as we've started including it in our SDK. |
| 574 | # We can't just "set 1" because the option order changes for different stage3 |
| 575 | # tarballs. |
| 576 | early_enter_chroot eselect python update --ignore "3*" |
| 577 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 578 | info "Updating portage" |
Mike Frysinger | 66fd81f | 2012-02-28 13:13:20 -0500 | [diff] [blame] | 579 | early_enter_chroot emerge -uNv --quiet portage |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 580 | |
Mike Frysinger | 9a2978c | 2013-08-04 11:38:43 -0400 | [diff] [blame] | 581 | # Clear out openrc if it's installed as we don't want it. |
David Pursell | d35d7af | 2015-05-04 16:37:08 -0700 | [diff] [blame] | 582 | if [[ -e "${FLAGS_chroot}/usr/share/openrc" ]]; then |
Mike Frysinger | 9a2978c | 2013-08-04 11:38:43 -0400 | [diff] [blame] | 583 | info "Uninstalling openrc" |
| 584 | early_enter_chroot env CLEAN_DELAY=0 emerge -qC sys-apps/openrc |
Mike Frysinger | 47e599e | 2013-10-28 13:30:12 -0400 | [diff] [blame] | 585 | # Now update baselayout to get our functions.sh. The unmerge |
| 586 | # above removed our copy in the process. |
| 587 | early_enter_chroot emerge -uNvq sys-apps/baselayout |
Mike Frysinger | 9a2978c | 2013-08-04 11:38:43 -0400 | [diff] [blame] | 588 | fi |
| 589 | |
Bertrand SIMONNET | 85c96c7 | 2014-08-20 12:10:48 -0700 | [diff] [blame] | 590 | # Add chromite into python path. |
Bertrand SIMONNET | 85c96c7 | 2014-08-20 12:10:48 -0700 | [diff] [blame] | 591 | for python_path in "${FLAGS_chroot}/usr/lib/"python2.*; do |
| 592 | python_path+="/site-packages" |
| 593 | sudo mkdir -p "${python_path}" |
| 594 | sudo ln -s -fT "${CHROOT_TRUNK_DIR}"/chromite "${python_path}"/chromite |
| 595 | done |
| 596 | |
Chirantan Ekbote | af1b950 | 2016-07-14 11:29:16 -0700 | [diff] [blame] | 597 | # The stage3 contains an old version of ncurses, which causes a slot conflict |
| 598 | # later when we try to setup the toolchains. Update it here to the latest |
| 599 | # version, which gracefully handles the slot issues. |
| 600 | info "Updating ncurses" |
| 601 | early_enter_chroot emerge -uNvq sys-libs/ncurses |
| 602 | |
Zdenek Behan | 2fbd5af | 2012-03-12 19:38:50 +0100 | [diff] [blame] | 603 | info "Updating host toolchain" |
David Pursell | d35d7af | 2015-05-04 16:37:08 -0700 | [diff] [blame] | 604 | if [[ ! -e "${FLAGS_chroot}/usr/bin/crossdev" ]]; then |
Mike Frysinger | 5e07dc7 | 2014-11-05 14:40:04 -0500 | [diff] [blame] | 605 | early_enter_chroot $EMERGE_CMD -uNv crossdev |
| 606 | fi |
Zdenek Behan | 2fbd5af | 2012-03-12 19:38:50 +0100 | [diff] [blame] | 607 | TOOLCHAIN_ARGS=( --deleteold ) |
David Pursell | d35d7af | 2015-05-04 16:37:08 -0700 | [diff] [blame] | 608 | if [[ "${FLAGS_usepkg}" == "${FLAGS_FALSE}" ]]; then |
Zdenek Behan | 2fbd5af | 2012-03-12 19:38:50 +0100 | [diff] [blame] | 609 | TOOLCHAIN_ARGS+=( --nousepkg ) |
| 610 | fi |
| 611 | # Note: early_enter_chroot executes as root. |
Brian Harring | 2499bfb | 2012-12-18 16:23:31 -0800 | [diff] [blame] | 612 | early_enter_chroot "${CHROOT_TRUNK_DIR}/chromite/bin/cros_setup_toolchains" \ |
Zdenek Behan | 2fbd5af | 2012-03-12 19:38:50 +0100 | [diff] [blame] | 613 | --hostonly "${TOOLCHAIN_ARGS[@]}" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 614 | |
Ben Chan | 0c9da66 | 2017-05-15 17:29:51 -0700 | [diff] [blame] | 615 | info "Running emerge curl sudo gentoolkit ..." |
Mike Frysinger | 650bf87 | 2012-02-27 11:05:26 -0500 | [diff] [blame] | 616 | early_enter_chroot $EMERGE_CMD -uNv $USEPKG --select $EMERGE_JOBS \ |
Ben Chan | 0c9da66 | 2017-05-15 17:29:51 -0700 | [diff] [blame] | 617 | pbzip2 dev-libs/openssl net-misc/curl sudo app-portage/gentoolkit |
| 618 | |
| 619 | info "Updating Perl modules" |
| 620 | early_enter_chroot \ |
| 621 | "${CHROOT_TRUNK_DIR}/src/scripts/build_library/perl_rebuild.sh" |
Paul Drews | 8bae3b5 | 2012-10-10 11:18:13 -0700 | [diff] [blame] | 622 | |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 623 | if [ -n "${INITIALIZE_CHROOT}" ]; then |
| 624 | # If we're creating a new chroot, we also want to set it to the latest |
| 625 | # version. |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 626 | enter_chroot \ |
David James | 47e0e8a | 2015-03-11 15:24:10 -0700 | [diff] [blame] | 627 | "${CHROOT_TRUNK_DIR}/src/scripts/run_chroot_version_hooks" --init_latest |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 628 | fi |
| 629 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 630 | # Update chroot. |
Zdenek Behan | 2fbd5af | 2012-03-12 19:38:50 +0100 | [diff] [blame] | 631 | # Skip toolchain update because it already happened above, and the chroot is |
| 632 | # not ready to emerge all cross toolchains. |
| 633 | UPDATE_ARGS=( --skip_toolchain_update ) |
David Pursell | d35d7af | 2015-05-04 16:37:08 -0700 | [diff] [blame] | 634 | if [[ "${FLAGS_usepkg}" == "${FLAGS_TRUE}" ]]; then |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 635 | UPDATE_ARGS+=( --usepkg ) |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 636 | else |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 637 | UPDATE_ARGS+=( --nousepkg ) |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 638 | fi |
David James | 184e390 | 2012-02-23 20:19:28 -0800 | [diff] [blame] | 639 | if [[ "${FLAGS_jobs}" -ne -1 ]]; then |
David Pursell | d35d7af | 2015-05-04 16:37:08 -0700 | [diff] [blame] | 640 | UPDATE_ARGS+=( --jobs="${FLAGS_jobs}" ) |
David James | 184e390 | 2012-02-23 20:19:28 -0800 | [diff] [blame] | 641 | fi |
Brian Harring | 2499bfb | 2012-12-18 16:23:31 -0800 | [diff] [blame] | 642 | enter_chroot "${CHROOT_TRUNK_DIR}/src/scripts/update_chroot" "${UPDATE_ARGS[@]}" |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 643 | |
Stefan Zager | 48c776b | 2013-10-09 13:01:25 -0700 | [diff] [blame] | 644 | # The java-config package atm does not support $ROOT. Select a default |
| 645 | # VM ourselves until that gets fixed upstream. |
| 646 | enter_chroot sudo java-config --set-system-vm 1 |
| 647 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 648 | CHROOT_EXAMPLE_OPT="" |
| 649 | if [[ "$FLAGS_chroot" != "$DEFAULT_CHROOT_DIR" ]]; then |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 650 | CHROOT_EXAMPLE_OPT="--chroot=$FLAGS_chroot" |
| 651 | fi |
| 652 | |
Matt Tennant | 0a9d32d | 2012-07-30 16:51:37 -0700 | [diff] [blame] | 653 | command_completed |
Brian Harring | f539bc3 | 2012-02-06 00:18:37 -0800 | [diff] [blame] | 654 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 655 | cat <<EOF |
Mike Frysinger | bdc4fb1 | 2012-02-10 11:20:03 -0500 | [diff] [blame] | 656 | |
| 657 | ${CROS_LOG_PREFIX:-cros_sdk}: All set up. To enter the chroot, run: |
| 658 | $ cros_sdk --enter $CHROOT_EXAMPLE_OPT |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 659 | |
| 660 | CAUTION: Do *NOT* rm -rf the chroot directory; if there are stale bind |
| 661 | mounts you may end up deleting your source tree too. To unmount and |
| 662 | delete the chroot cleanly, use: |
| 663 | $ cros_sdk --delete $CHROOT_EXAMPLE_OPT |
Mike Frysinger | bdc4fb1 | 2012-02-10 11:20:03 -0500 | [diff] [blame] | 664 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 665 | EOF |
David James | 22dc2ba | 2012-11-29 15:46:58 -0800 | [diff] [blame] | 666 | |
| 667 | warn_if_nfs "${SUDO_HOME}" |