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