Refactor make_chroot.sh to use enter_chroot, and cleanup complaints.

Now that make_chroot.sh lives alongside enter_chroot, we're able
to invoke it directly and use internal options not exposed through
cros_sdk.  Thus enter_chroot grows a --early_make_chroot flag.

This flag bypasses the normal sudo usage, and runs the command
as root.  It is needed and used by make_chroot for when sudo
may not yet exist (--bootstrap via stage3 lacks sudo).

For where sudo exists and we need access to the source tree,
distfiles, or profile sourcing, enter_chroot is directly used.

For all other invocations we use a single sudo chroot call to
bypass the overhead of enter_chroot.

This is a complete fix to chromium-os:25697 while addressing
all refactoring requests that came up in I8a6c5a26.

Finally, convert all comments into complete sentences, upper
case the leading word, etc to keep reviewers happy.

BUG=chromium-os:25697
TEST=Within a trybot, cros_sdk --bootstrap --replace
TEST=cros_sdk --replace
TEST=cros_sdk --delete
TEST=cros_sdk
Change-Id: I270ee7fc325ef0bea74c61505d25cdbb49a9a333
Reviewed-on: https://gerrit.chromium.org/gerrit/15322
Commit-Ready: Brian Harring <ferringb@chromium.org>
Reviewed-by: Brian Harring <ferringb@chromium.org>
Tested-by: Brian Harring <ferringb@chromium.org>
This commit is contained in:
Brian Harring 2012-02-01 23:50:45 -08:00 committed by Gerrit
parent f539bc3407
commit 357678296a
2 changed files with 174 additions and 170 deletions

View File

@ -31,6 +31,8 @@ DEFINE_boolean official_build $FLAGS_FALSE \
DEFINE_boolean mount $FLAGS_FALSE "Only set up mounts."
DEFINE_boolean unmount $FLAGS_FALSE "Only tear down mounts."
DEFINE_boolean ssh_agent $FLAGS_TRUE "Import ssh agent."
DEFINE_boolean early_make_chroot $FLAGS_FALSE \
"Internal flag. If set, the command is run as root without sudo."
DEFINE_boolean verbose $FLAGS_FALSE "Print out actions taken"
# More useful help
@ -502,33 +504,47 @@ fi
trap teardown_env EXIT
setup_env
CHROOT_PASSTHRU="BUILDBOT_BUILD=$FLAGS_build_number \
CHROMEOS_OFFICIAL=$CHROMEOS_OFFICIAL"
CHROOT_PASSTHRU="${CHROOT_PASSTHRU} \
CHROMEOS_RELEASE_APPID=${CHROMEOS_RELEASE_APPID:-"{DEV-BUILD}"}"
CHROOT_PASSTHRU=(
"BUILDBOT_BUILD=$FLAGS_build_number"
"CHROMEOS_OFFICIAL=$CHROMEOS_OFFICIAL"
"CHROMEOS_RELEASE_APPID=${CHROMEOS_RELEASE_APPID:-{DEV-BUILD}}"
# Set CHROMEOS_VERSION_TRACK, CHROMEOS_VERSION_AUSERVER,
# CHROMEOS_VERSION_DEVSERVER as environment variables to override the default
# assumptions (local AU server). These are used in cros_set_lsb_release, and
# are used by external Chromium OS builders.
CHROOT_PASSTHRU="${CHROOT_PASSTHRU} \
CHROMEOS_VERSION_TRACK=${CHROMEOS_VERSION_TRACK} \
CHROMEOS_VERSION_AUSERVER=${CHROMEOS_VERSION_AUSERVER} \
CHROMEOS_VERSION_DEVSERVER=${CHROMEOS_VERSION_DEVSERVER}"
# Set CHROMEOS_VERSION_TRACK, CHROMEOS_VERSION_AUSERVER,
# CHROMEOS_VERSION_DEVSERVER as environment variables to override the default
# assumptions (local AU server). These are used in cros_set_lsb_release, and
# are used by external Chromium OS builders.
"CHROMEOS_VERSION_TRACK=${CHROMEOS_VERSION_TRACK}"
"CHROMEOS_VERSION_AUSERVER=${CHROMEOS_VERSION_AUSERVER}"
"CHROMEOS_VERSION_DEVSERVER=${CHROMEOS_VERSION_DEVSERVER}"
"EXTERNAL_TRUNK_PATH=${FLAGS_trunk}"
"SSH_AGENT_PID=${SSH_AGENT_PID}"
"SSH_AUTH_SOCK=${SSH_AUTH_SOCK}"
)
# Pass proxy variables into the environment.
for type in http_proxy ftp_proxy all_proxy GIT_PROXY_COMMAND GIT_SSH; do
if [ -n "${!type}" ]; then
CHROOT_PASSTHRU="${CHROOT_PASSTHRU} ${type}=${!type}"
fi
if [ -n "${!type}" ]; then
CHROOT_PASSTHRU+=( "${type}=${!type}" )
fi
done
# Run command or interactive shell. Also include the non-chrooted path to
# the source trunk for scripts that may need to print it (e.g.
# build_image.sh).
sudo -- chroot "$FLAGS_chroot" sudo -i -u $USER $CHROOT_PASSTHRU \
EXTERNAL_TRUNK_PATH="${FLAGS_trunk}" SSH_AGENT_PID="${SSH_AGENT_PID}" \
SSH_AUTH_SOCK="${SSH_AUTH_SOCK}" "$@"
if [ $FLAGS_early_make_chroot -eq $FLAGS_TRUE ]; then
cmd=( /bin/bash -l -c 'env "$@"' -- )
elif [ ! -x "${FLAGS_chroot}/usr/bin/sudo" ]; then
# Complain that sudo is missing.
error "Failing since the chroot lacks sudo."
error "Requested enter_chroot command was: $@"
exit 127
else
cmd=( sudo -i -u "$USER" )
fi
sudo -- chroot "${FLAGS_chroot}" "${cmd[@]}" "${CHROOT_PASSTHRU[@]}" "$@"
# Remove trap and explicitly unmount
trap - EXIT

View File

@ -12,6 +12,8 @@
SCRIPT_ROOT=$(readlink -f $(dirname "$0")/..)
. "${SCRIPT_ROOT}/common.sh" || exit 1
ENTER_CHROOT=$(readlink -f $(dirname "$0")/enter_chroot.sh)
# Check if the host machine architecture is supported.
ARCHITECTURE="$(uname -m)"
if [[ "$ARCHITECTURE" != "x86_64" ]]; then
@ -19,10 +21,10 @@ if [[ "$ARCHITECTURE" != "x86_64" ]]; then
exit 1
fi
# Script must be run outside the chroot
# Script must be run outside the chroot.
assert_outside_chroot
# Define command line flags
# Define command line flags.
# See http://code.google.com/p/shflags/wiki/Documentation10x
DEFINE_string chroot "$DEFAULT_CHROOT_DIR" \
@ -36,18 +38,14 @@ DEFINE_string stage3_date "2010.03.09" \
"Use the stage3 with the given date."
DEFINE_string stage3_path "" \
"Use the stage3 located on this path."
DEFINE_boolean cros_sdk $FLAGS_FALSE "Internal: we're called from cros_sdk"
# Parse command line flags
# Parse command line flags.
FLAGS_HELP="usage: $SCRIPT_NAME [flags]"
FLAGS "$@" || exit 1
eval set -- "${FLAGS_ARGV}"
check_flags_only_and_allow_null_arg "$@" && set --
if [ "${FLAGS_cros_sdk}" == "${FLAGS_TRUE}" ]; then
# HACK: If we're being called by cros_sdk, change the messages.
SCRIPT_NAME=cros_sdk
fi
CROS_LOG_PREFIX=cros_sdk
assert_not_root_user
# Set the right umask for chroot creation.
@ -78,19 +76,29 @@ if [ "$FLAGS_fast" -eq "${FLAGS_TRUE}" ]; then
EMERGE_CMD="${CHROOT_CHROMITE_DIR}/bin/parallel_emerge"
fi
function in_chroot {
sudo "${CHROOT_PASSTHRU[@]}" chroot "$FLAGS_chroot" "$@"
ENTER_CHROOT_ARGS=(
CROS_WORKON_SRCROOT="$CHROOT_TRUNK"
PORTAGE_USERNAME="$USER"
IGNORE_PREFLIGHT_BINHOST="$IGNORE_PREFLIGHT_BINHOST"
)
# Invoke enter_chroot. This can only be used after sudo has been installed.
function enter_chroot {
"$ENTER_CHROOT" --chroot "$FLAGS_chroot" -- "${ENTER_CHROOT_ARGS[@]}" "$@"
}
function bash_chroot {
# Use $* not $@ since 'bash -c' needs a single arg
# Use -l to force source of /etc/profile (login shell)
sudo "${CHROOT_PASSTHRU[@]}" chroot "$FLAGS_chroot" bash -l -c "$*"
# Invoke enter_chroot running the command as root, and w/out sudo.
# This should be used prior to sudo being merged.
function early_enter_chroot() {
"$ENTER_CHROOT" --chroot "$FLAGS_chroot" --early_make_chroot \
-- "${ENTER_CHROOT_ARGS[@]}" "$@"
}
function sudo_chroot {
# The same as bash_chroot, except ran as the normal user.
sudo chroot "$FLAGS_chroot" sudo -i -u ${USER} "${CHROOT_PASSTHRU[@]}" -- "$@"
# Run a command within the chroot. The main usage of this is to avoid
# the overhead of enter_chroot, and do not need access to the source tree,
# don't need the actual chroot profile env, and can run the command as root.
sudo_chroot() {
sudo chroot "${FLAGS_chroot}" "$@"
}
function cleanup {
@ -99,56 +107,55 @@ function cleanup {
}
function delete_existing {
# Delete old chroot dir
if [[ -e "$FLAGS_chroot" ]]; then
echo "$SCRIPT_NAME: Cleaning up old mount points..."
cleanup
echo "$SCRIPT_NAME: Deleting $FLAGS_chroot..."
sudo rm -rf "$FLAGS_chroot"
echo "$SCRIPT_NAME: Done."
# Delete old chroot dir.
if [[ ! -e "$FLAGS_chroot" ]]; then
return
fi
info "Cleaning up old mount points..."
cleanup
info "Deleting $FLAGS_chroot..."
sudo rm -rf "$FLAGS_chroot"
info "Done."
}
function init_users () {
echo "$SCRIPT_NAME: Set timezone..."
# date +%Z has trouble with daylight time, so use host's info
in_chroot rm -f /etc/localtime
info "Set timezone..."
# date +%Z has trouble with daylight time, so use host's info.
sudo rm -f "${FLAGS_chroot}/etc/localtime"
if [ -f /etc/localtime ] ; then
sudo cp /etc/localtime "${FLAGS_chroot}/etc"
else
in_chroot ln -sf /usr/share/zoneinfo/PST8PDT /etc/localtime
sudo ln -sf /usr/share/zoneinfo/PST8PDT "${FLAGS_chroot}/etc/localtime"
fi
echo "$SCRIPT_NAME: Adding user/group..."
info "Adding user/group..."
# Add ourselves as a user inside the chroot.
in_chroot groupadd -g 5000 eng
sudo_chroot groupadd -g 5000 eng
# We need the UID to match the host user's. This can conflict with
# a particular chroot UID. At the same time, the added user has to
# be a primary user for the given UID for sudo to work, which is
# determined by the order in /etc/passwd. Let's put ourselves on top
# of the file.
in_chroot useradd -o -G ${DEFGROUPS} -g eng -u `id -u` -s \
sudo_chroot useradd -o -G ${DEFGROUPS} -g eng -u `id -u` -s \
/bin/bash -m -c "${FULLNAME}" -p ${CRYPTED_PASSWD} ${USER}
# Because passwd generally isn't sorted and the entry ended up at the
# bottom, it is safe to just take it and move it to top instead.
in_chroot sed -e '1{h;d};$!{H;d};$G' -i /etc/passwd
sudo sed -e '1{h;d};$!{H;d};$G' -i "${FLAGS_chroot}/etc/passwd"
}
function init_setup () {
echo "$SCRIPT_NAME: Running init_setup()..."
sudo mkdir -p "${FLAGS_chroot}/usr"
info "Running init_setup()..."
sudo mkdir -p -m 755 "${FLAGS_chroot}/usr" \
"${FLAGS_chroot}/usr/local/portage" \
"${FLAGS_chroot}"/"${CROSSDEV_OVERLAY}"
sudo ln -sf "${CHROOT_TRUNK}/src/third_party/portage" \
"${FLAGS_chroot}/usr/portage"
sudo mkdir -p "${FLAGS_chroot}/usr/local/portage"
sudo chmod 755 "${FLAGS_chroot}/usr/local/portage"
sudo ln -sf "${CHROOT_TRUNK}/src/third_party/chromiumos-overlay" \
"${FLAGS_chroot}"/"${CHROOT_OVERLAY}"
sudo ln -sf "${CHROOT_TRUNK}/src/third_party/portage-stable" \
"${FLAGS_chroot}"/"${PORTAGE_STABLE_OVERLAY}"
sudo mkdir -p "${FLAGS_chroot}"/"${CROSSDEV_OVERLAY}"
sudo chmod 755 "${FLAGS_chroot}"/"${CROSSDEV_OVERLAY}"
# Some operations need an mtab
in_chroot ln -s /proc/mounts /etc/mtab
# Some operations need an mtab.
sudo ln -s /proc/mounts "${FLAGS_chroot}/etc/mtab"
# Set up sudoers. Inside the chroot, the user can sudo without a password.
# (Safe enough, since the only way into the chroot is to 'sudo chroot', so
@ -166,20 +173,19 @@ Defaults env_keep += all_proxy
root ALL=(ALL) ALL
$USER ALL=NOPASSWD: ALL
EOF
bash_chroot "find /etc/sudoers* -type f -exec chmod 0440 {} +"
bash_chroot "chown -R root:root /etc/sudoers*" # Fix bad group for some.
sudo find "${FLAGS_chroot}/etc/"sudoers* -type f -exec chmod 0440 {} +
# Fix bad group for some.
sudo chown -R root:root "${FLAGS_chroot}/etc/"sudoers*
echo "$SCRIPT_NAME: Setting up hosts/resolv..."
# Copy config from outside chroot into chroot
sudo cp /etc/hosts "$FLAGS_chroot/etc/hosts"
sudo chmod 0644 "$FLAGS_chroot/etc/hosts"
sudo cp /etc/resolv.conf "$FLAGS_chroot/etc/resolv.conf"
sudo chmod 0644 "$FLAGS_chroot/etc/resolv.conf"
info "Setting up hosts/resolv..."
# Copy config from outside chroot into chroot.
sudo cp /etc/{hosts,resolv.conf} "$FLAGS_chroot/etc/"
sudo chmod 0644 "$FLAGS_chroot"/etc/{hosts,resolv.conf}
# Setup host make.conf. This includes any overlay that we may be using
# and a pointer to pre-built packages.
# TODO: This should really be part of a profile in the portage
echo "$SCRIPT_NAME: Setting up /etc/make.*..."
# TODO: This should really be part of a profile in the portage.
info "Setting up /etc/make.*..."
sudo mv "${FLAGS_chroot}"/etc/make.conf{,.orig}
sudo ln -sf "${CHROOT_CONFIG}/make.conf.amd64-host" \
"${FLAGS_chroot}/etc/make.conf"
@ -187,14 +193,14 @@ EOF
sudo ln -sf "${CHROOT_OVERLAY}/profiles/default/linux/amd64/10.0" \
"${FLAGS_chroot}/etc/make.profile"
# Create make.conf.user
# Create make.conf.user .
sudo touch "${FLAGS_chroot}"/etc/make.conf.user
sudo chmod 0644 "${FLAGS_chroot}"/etc/make.conf.user
# Create directories referred to by our conf files.
sudo mkdir -p -m 775 "${FLAGS_chroot}/var/lib/portage/distfiles"
sudo mkdir -p -m 775 "${FLAGS_chroot}/var/lib/portage/distfiles-target"
sudo mkdir -p -m 775 "${FLAGS_chroot}/var/lib/portage/pkgs"
sudo mkdir -p -m 775 "${FLAGS_chroot}/var/lib/portage/distfiles" \
"${FLAGS_chroot}/var/lib/portage/distfiles-target" \
"${FLAGS_chroot}/var/lib/portage/pkgs"
if [[ $FLAGS_jobs -ne -1 ]]; then
EMERGE_JOBS="--jobs=$FLAGS_jobs"
@ -212,19 +218,22 @@ EOF
# TODO(zbehan): Configure stuff that is usually configured in postinst's,
# but wasn't. Fix the postinst's. crosbug.com/18036
echo "Running post-inst configuration hacks"
in_chroot env-update
info "Running post-inst configuration hacks"
early_enter_chroot env-update
if [ -f ${FLAGS_chroot}/usr/bin/build-docbook-catalog ]; then
# For too ancient chroots that didn't have build-docbook-catalog, this
# is not relevant, and will get installed during update.
in_chroot build-docbook-catalog
early_enter_chroot build-docbook-catalog
# Configure basic stuff needed.
early_enter_chroot env-update
fi
# Configure basic stuff needed
in_chroot env-update
bash_chroot ls -l /etc/make.conf
bash_chroot ls -l /etc/make.profile
bash_chroot ls -l /usr/local/portage/chromiumos/profiles/default/linux/amd64/10.0
# This is basically a sanity check of our chroot. If any of these
# don't exist, then either bind mounts have failed, an invocation
# from above is broke, or some assumption about the stage3 is no longer
# true.
early_enter_chroot ls -l /etc/make.{conf,profile} \
/usr/local/portage/chromiumos/profiles/default/linux/amd64/10.0
target="${FLAGS_chroot}/etc/profile.d"
sudo mkdir -p "${target}"
@ -257,15 +266,15 @@ EOF
"${FLAGS_chroot}/home/$USER/.local/lib/python2.6/site-packages/"
chmod a+x "$FLAGS_chroot/home/$USER/.bashrc"
# Automatically change to scripts directory
# Automatically change to scripts directory.
echo 'cd ${CHROOT_CWD:-~/trunk/src/scripts}' \
>> "$FLAGS_chroot/home/$USER/.bash_profile"
# Enable bash completion for build scripts
# Enable bash completion for build scripts.
echo ". ~/trunk/src/scripts/bash_completion" \
>> "$FLAGS_chroot/home/$USER/.bashrc"
# Warn if attempting to use source control commands inside the chroot
# Warn if attempting to use source control commands inside the chroot.
for NOUSE in svn gcl gclient
do
echo "alias $NOUSE='echo In the chroot, it is a bad idea to run $NOUSE'" \
@ -278,16 +287,16 @@ EOF
fi
if [[ -f $HOME/.gitconfig ]]; then
# Copy .gitconfig into chroot so repo and git can be used from inside
# This is required for repo to work since it validates the email address
# Copy .gitconfig into chroot so repo and git can be used from inside.
# This is required for repo to work since it validates the email address.
echo "Copying ~/.gitconfig into chroot"
cp $HOME/.gitconfig "$FLAGS_chroot/home/$USER/"
fi
}
# Handle deleting an existing environment
# Handle deleting an existing environment.
if [[ $FLAGS_delete -eq $FLAGS_TRUE || \
$FLAGS_replace -eq $FLAGS_TRUE ]]; then
$FLAGS_replace -eq $FLAGS_TRUE ]]; then
delete_existing
[[ $FLAGS_delete -eq $FLAGS_TRUE ]] && exit 0
fi
@ -301,8 +310,6 @@ PORTAGE_STABLE_OVERLAY="/usr/local/portage/stable"
CROSSDEV_OVERLAY="/usr/local/portage/crossdev"
CHROOT_OVERLAY="/usr/local/portage/chromiumos"
CHROOT_STATE="${FLAGS_chroot}/etc/debian_chroot"
CHROOT_PASSTHRU=(CROS_WORKON_SRCROOT="$CHROOT_TRUNK" PORTAGE_USERNAME="$USER"
IGNORE_PREFLIGHT_BINHOST="$IGNORE_PREFLIGHT_BINHOST")
# Pass proxy variables into the environment.
for type in http ftp all; do
@ -312,81 +319,68 @@ for type in http ftp all; do
fi
done
# Create the base Gentoo stage3 based on last version put in chroot
# Create the base Gentoo stage3 based on last version put in chroot.
STAGE3="${OVERLAY}/chromeos/stage3/stage3-amd64-${FLAGS_stage3_date}.tar.bz2"
if [ -f $CHROOT_STATE ] && \
! sudo egrep -q "^STAGE3=$STAGE3" $CHROOT_STATE >/dev/null 2>&1
then
echo "$SCRIPT_NAME: STAGE3 version has changed."
info "STAGE3 version has changed."
delete_existing
fi
if [ -n "${FLAGS_stage3_path}" ]; then
if [ -f "${FLAGS_stage3_path}" ]; then
STAGE3="${FLAGS_stage3_path}"
else
if [ ! -f "${FLAGS_stage3_path}" ]; then
error "Invalid stage3!"
exit 1;
fi
STAGE3="${FLAGS_stage3_path}"
fi
# Create the destination directory
# Create the destination directory.
mkdir -p "$FLAGS_chroot"
echo
if [ -f $CHROOT_STATE ]
then
echo "$SCRIPT_NAME: STAGE3 already set up. Skipping..."
info "STAGE3 already set up. Skipping..."
else
echo "$SCRIPT_NAME: Unpacking STAGE3..."
info "Unpacking STAGE3..."
sudo tar -xp -I $(type -p pbzip2 || echo bzip2) \
-C "${FLAGS_chroot}" -f "${STAGE3}"
sudo rm -f $FLAGS_chroot/etc/make.globals
sudo rm -f $FLAGS_chroot/etc/make.conf.user
-C "${FLAGS_chroot}" -f "${STAGE3}"
sudo rm -f "$FLAGS_chroot/etc/"make.{globals,conf.user}
fi
# Set up users, if needed, before mkdir/mounts below
# Set up users, if needed, before mkdir/mounts below.
[ -f $CHROOT_STATE ] || init_users
echo
echo "$SCRIPT_NAME: Setting up mounts..."
# Set up necessary mounts and make sure we clean them up on exit
trap cleanup EXIT
sudo mkdir -p "${FLAGS_chroot}/${CHROOT_TRUNK}"
sudo mount --bind /dev "${FLAGS_chroot}/dev"
sudo mount --bind "${GCLIENT_ROOT}" "${FLAGS_chroot}/${CHROOT_TRUNK}"
sudo mount none -t proc "$FLAGS_chroot/proc"
sudo mount none -t devpts "$FLAGS_chroot/dev/pts"
sudo mkdir -p "${FLAGS_chroot}/run"
if [ -d /run ]; then
sudo mount --bind /run "$FLAGS_chroot/run"
if [ -d /run/shm ]; then
sudo mount --bind /run/shm "$FLAGS_chroot/run/shm"
fi
fi
info "Setting up mounts..."
# Set up necessary mounts and make sure we clean them up on exit.
sudo mkdir -p "${FLAGS_chroot}/${CHROOT_TRUNK}" "${FLAGS_chroot}/run"
PREBUILT_SETUP="$FLAGS_chroot/etc/make.conf.prebuilt_setup"
if [[ -z "$IGNORE_PREFLIGHT_BINHOST" ]]; then
echo 'PORTAGE_BINHOST="$FULL_BINHOST"' | sudo_clobber "$PREBUILT_SETUP"
else
sudo_clobber "$PREBUILT_SETUP" < /dev/null
fi
echo 'PORTAGE_BINHOST="$FULL_BINHOST"'
fi | sudo_clobber "$PREBUILT_SETUP"
sudo chmod 0644 "$PREBUILT_SETUP"
# For bootstrapping from old wget, disable certificate checking. Once we've
# upgraded to new curl (below), certificate checking is re-enabled. See
# http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=409938
bash_chroot 'cat > /etc/make.conf.fetchcommand_setup' <<'EOF'
sudo_clobber "${FLAGS_chroot}/etc/make.conf.fetchcommand_setup" <<'EOF'
FETCHCOMMAND="/usr/bin/wget -t 5 -T 60 --no-check-certificate --passive-ftp -O \"\${DISTDIR}/\${FILE}\" \"\${URI}\""
RESUMECOMMAND="/usr/bin/wget -c -t 5 -T 60 --no-check-certificate --passive-ftp -O \"\${DISTDIR}/\${FILE}\" \"\${URI}\""
EOF
sudo chmod 0644 "${FLAGS_chroot}"/etc/make.conf.fetchcommand_setup
bash_chroot 'cat > /etc/make.conf.host_setup' <<EOF
# Created by make_chroot
sudo_clobber "${FLAGS_chroot}/etc/make.conf.host_setup" <<EOF
# Created by make_chroot.
source make.conf.prebuilt_setup
source make.conf.fetchcommand_setup
MAKEOPTS="-j${NUM_JOBS}"
EOF
sudo chmod 0644 "${FLAGS_chroot}"/etc/make.conf.host_setup
sudo chmod 0644 "${FLAGS_chroot}"/etc/make.conf.host_setup \
"${FLAGS_chroot}"/etc/make.conf.fetchcommand_setup
if ! [ -f "$CHROOT_STATE" ];then
INITIALIZE_CHROOT=1
@ -394,42 +388,43 @@ fi
if [ -z "${INITIALIZE_CHROOT}" ];then
echo "$SCRIPT_NAME: chroot already initialized. Skipping..."
info "chroot already initialized. Skipping..."
else
# run all the init stuff to setup the env
# Run all the init stuff to setup the env.
init_setup
fi
# Add file to indicate that it is a chroot
# Add version of $STAGE3 for update checks
# Add file to indicate that it is a chroot.
# Add version of $STAGE3 for update checks.
sudo sh -c "echo STAGE3=$STAGE3 > $CHROOT_STATE"
echo "$SCRIPT_NAME: Updating portage"
in_chroot emerge -uNv portage
info "Updating portage"
early_enter_chroot emerge -uNv portage
echo "$SCRIPT_NAME: Updating toolchain"
in_chroot emerge -uNv $USEPKG '>=sys-devel/gcc-4.4' sys-libs/glibc \
sys-devel/binutils sys-kernel/linux-headers
info "Updating toolchain"
early_enter_chroot emerge -uNv $USEPKG '>=sys-devel/gcc-4.4' sys-libs/glibc \
sys-devel/binutils sys-kernel/linux-headers
# HACK: Select the latest toolchain. We're assuming that when this is
# ran, the chroot has no experimental versions of new toolchains, just
# one that is very old, and one that was just emerged.
CHOST="$(in_chroot portageq envvar CHOST)"
LATEST="$(in_chroot gcc-config -l | grep "${CHOST}" | tail -n1 | \
CHOST="$(early_enter_chroot portageq envvar CHOST)"
LATEST="$(early_enter_chroot gcc-config -l | grep "${CHOST}" | tail -n1 | \
cut -f3 -d' ')"
in_chroot gcc-config "${LATEST}"
in_chroot emerge --unmerge "<sys-devel/gcc-${LATEST/${CHOST}-/}"
early_enter_chroot gcc-config "${LATEST}"
early_enter_chroot emerge --unmerge "<sys-devel/gcc-${LATEST/${CHOST}-/}"
# dhcpcd is included in 'world' by the stage3 that we pull in for some reason.
# We have no need to install it in our host environment, so pull it out here.
echo "$SCRIPT_NAME: Deselecting dhcpcd"
in_chroot $EMERGE_CMD --deselect dhcpcd
info "Deselecting dhcpcd"
early_enter_chroot $EMERGE_CMD --deselect dhcpcd
echo "$SCRIPT_NAME: Running emerge ccache curl sudo ..."
in_chroot $EMERGE_CMD -uNv $USEPKG ccache net-misc/curl sudo $EMERGE_JOBS
info "Running emerge ccache curl sudo ..."
early_enter_chroot $EMERGE_CMD -uNv $USEPKG $EMERGE_JOS \
ccache net-misc/curl sudo
# Curl is now installed, so we can depend on it now.
bash_chroot 'cat > /etc/make.conf.fetchcommand_setup' <<'EOF'
sudo_clobber "${FLAGS_chroot}/etc/make.conf.fetchcommand_setup" <<'EOF'
FETCHCOMMAND='curl -f -y 30 --retry 9 -L --output \${DISTDIR}/\${FILE} \${URI}'
RESUMECOMMAND='curl -f -y 30 -C - --retry 9 -L --output \${DISTDIR}/\${FILE} \${URI}'
EOF
@ -438,44 +433,37 @@ sudo chmod 0644 "${FLAGS_chroot}"/etc/make.conf.fetchcommand_setup
if [ -n "${INITIALIZE_CHROOT}" ]; then
# If we're creating a new chroot, we also want to set it to the latest
# version.
sudo_chroot \
"${CHROOT_TRUNK}/src/scripts/run_chroot_version_hooks" --force_latest
enter_chroot \
"${CHROOT_TRUNK}/src/scripts/run_chroot_version_hooks" --force_latest
fi
# Update chroot
UPDATE_ARGS=""
# Update chroot.
UPDATE_ARGS=()
if [[ $FLAGS_usepkg -eq $FLAGS_TRUE ]]; then
UPDATE_ARGS+=" --usepkg"
UPDATE_ARGS+=( --usepkg )
else
UPDATE_ARGS+=" --nousepkg"
UPDATE_ARGS+=( --nousepkg )
fi
if [[ ${FLAGS_fast} -eq ${FLAGS_TRUE} ]]; then
UPDATE_ARGS+=" --fast"
UPDATE_ARGS+=( --fast )
else
UPDATE_ARGS+=" --nofast"
UPDATE_ARGS+=( --nofast )
fi
sudo_chroot "${CHROOT_TRUNK}/src/scripts/update_chroot" ${UPDATE_ARGS}
enter_chroot "${CHROOT_TRUNK}/src/scripts/update_chroot" "${UPDATE_ARGS[@]}"
# Unmount trunk
sudo umount "${FLAGS_chroot}/${CHROOT_TRUNK}"
# Clean up the chroot mounts
trap - EXIT
cleanup
if [[ "$FLAGS_chroot" = "$DEFAULT_CHROOT_DIR" ]]; then
CHROOT_EXAMPLE_OPT=""
else
CHROOT_EXAMPLE_OPT=""
if [[ "$FLAGS_chroot" != "$DEFAULT_CHROOT_DIR" ]]; then
CHROOT_EXAMPLE_OPT="--chroot=$FLAGS_chroot"
fi
print_time_elapsed
echo
echo "$SCRIPT_NAME: All set up. To enter the chroot, run:"
echo "$SCRIPT_NAME: $ cros_sdk --enter $CHROOT_EXAMPLE_OPT"
echo ""
echo "CAUTION: Do *NOT* rm -rf the chroot directory; if there are stale bind"
echo "mounts you may end up deleting your source tree too. To unmount and"
echo "delete the chroot cleanly, use:"
echo "$ $SCRIPT_NAME --delete $CHROOT_EXAMPLE_OPT"
cat <<EOF
${CROS_LOG_PREFIX:-cros_sdk}: All set up. To enter the chroot, run:"
${CROS_LOG_PREFIX:-cros_sdk}: $ cros_sdk --enter $CHROOT_EXAMPLE_OPT"
CAUTION: Do *NOT* rm -rf the chroot directory; if there are stale bind
mounts you may end up deleting your source tree too. To unmount and
delete the chroot cleanly, use:
$ cros_sdk --delete $CHROOT_EXAMPLE_OPT
EOF