hack(build_container): add a script to build containers

this is a bit of a hack but I wanted to see if it had any utility during
development before making it all pretty. Essentially this is a copy of
build_image but instead of building up an entire image it simply puts
the files into directories on disk to be ran with systemd-nspawn/lxc/etc
This commit is contained in:
Brandon Philips 2013-03-11 18:55:27 -07:00
parent 7d9acd0de1
commit 6bb635d51c
2 changed files with 386 additions and 0 deletions

199
build_container Executable file
View File

@ -0,0 +1,199 @@
#!/bin/bash
# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# Script to build a bootable keyfob-based chromeos system image from within
# a chromiumos setup. This assumes that all needed packages have been built into
# the given target's root with binary packages turned on. This script will
# build the Chrome OS image using only pre-built binary packages.
SCRIPT_ROOT=$(dirname $(readlink -f "$0"))
. "${SCRIPT_ROOT}/build_library/build_common.sh" || exit 1
# Developer-visible flags.
DEFINE_string board "${DEFAULT_BOARD}" \
"The board to build an image for."
DEFINE_string output_root "${DEFAULT_BUILD_ROOT}/containers" \
"Directory in which to place image result directories (named by version)"
FLAGS_HELP="USAGE: build_image [flags] [list of images to build].
This script is used to build a CoreOS container. CoreOS comes in many
different forms. This scripts can be used to build the following:
base - Pristine Chromium OS image. As similar to Chrome OS as possible.
dev - Developer image. Like base but with additional developer packages.
test - Like dev, but with additional test specific packages and can be easily
used for automated testing using scripts like run_remote_tests, etc.
Examples:
build_image --board=<board> dev test - builds developer and test images.
build_image --board=<board> factory_install - builds a factory install shim.
...
"
show_help_if_requested "$@"
# The following options are advanced options, only available to those willing
# to read the source code. They are not shown in help output, since they are
# not needed for the typical developer workflow.
DEFINE_integer build_attempt 1 \
"The build attempt for this image build."
DEFINE_integer jobs -1 \
"How many packages to build in parallel at maximum."
DEFINE_boolean replace ${FLAGS_FALSE} \
"Overwrite existing output, if any."
DEFINE_string symlink "latest" \
"Symlink name to use for this image."
DEFINE_string version "" \
"Overrides version number in name to this version."
# Parse command line.
FLAGS "$@" || exit 1
eval set -- "${FLAGS_ARGV}"
# Only now can we die on error. shflags functions leak non-zero error codes,
# so will die prematurely if 'switch_to_strict_mode' is specified before now.
switch_to_strict_mode
# Determine build version.
OVERLAY_CHROMEOS_DIR="${SRC_ROOT}/third_party/coreos-overlay/coreos"
. "${OVERLAY_CHROMEOS_DIR}/config/coreos_version.sh" || exit 1
# N.B. Ordering matters for some of the libraries below, because
# some of the files contain initialization used by later files.
. "${BUILD_LIBRARY_DIR}/board_options.sh" || exit 1
. "${BUILD_LIBRARY_DIR}/disk_layout_util.sh" || exit 1
. "${BUILD_LIBRARY_DIR}/mount_gpt_util.sh" || exit 1
. "${BUILD_LIBRARY_DIR}/build_image_util.sh" || exit 1
. "${BUILD_LIBRARY_DIR}/base_image_util.sh" || exit 1
. "${BUILD_LIBRARY_DIR}/dev_image_util.sh" || exit 1
. "${BUILD_LIBRARY_DIR}/test_image_util.sh" || exit 1
. "${BUILD_LIBRARY_DIR}/test_image_content.sh" || exit 1
. "${BUILD_LIBRARY_DIR}/base_container_util.sh" || exit 1
parse_build_image_args
for overlay in $(cros_list_overlays --board "$BOARD"); do
setup_sh="${overlay}/scripts/board_specific_setup.sh"
if [[ -e ${setup_sh} ]]; then
source "${setup_sh}"
fi
done
# TODO: <prebuild hook>
BASE_PACKAGE="coreos-base/coreos"
# Tweak flags, configure extra USE flags, and set base packages for the factory
# install shim.
if should_build_image ${CHROMEOS_FACTORY_INSTALL_SHIM_NAME}; then
# TODO: Build a separated ebuild for the install shim to reduce size.
INSTALL_MASK="${FACTORY_SHIM_INSTALL_MASK}"
# Add the cros_factory_install boot arg.
FLAGS_boot_args="${FLAGS_boot_args} cros_factory_install"
BASE_PACKAGE="coreos-base/chromeos-installshim"
export USE="${USE} initramfs vfat tpm i2cdev"
# On ARM, fbconsole is required to fix a display driver bug.
if [ "${ARCH}" = "arm" ] ; then
export USE="${USE} fbconsole"
fi
fi
if should_build_image ${CHROMEOS_FACTORY_TEST_IMAGE_NAME}; then
# Disable module restrictions on factory test image to allow for
# external third party drivers in /usr/local.
FLAGS_boot_args="${FLAGS_boot_args} lsm.module_locking=0"
fi
# TODO: </prebuild hook>
# If we are creating a developer image, also create a pristine image with a
# different name.
PRISTINE_IMAGE_NAME=
if should_build_image ${CHROMEOS_FACTORY_INSTALL_SHIM_NAME}; then
PRISTINE_IMAGE_NAME=${CHROMEOS_FACTORY_INSTALL_SHIM_NAME}
else
PRISTINE_IMAGE_NAME=${CHROMEOS_BASE_IMAGE_NAME}
fi
DEVKEYSDIR="/usr/share/vboot/devkeys"
eclean-$BOARD -d packages
if [[ ${skip_blacklist_check} -ne 1 ]]; then
check_blacklist
fi
# Check that the build root is sane.
if [[ ${skip_test_build_root} -ne 1 ]]; then
"${BUILD_LIBRARY_DIR}/test_build_root" --root="${BOARD_ROOT}"
fi
# Hack to fix bug where x86_64 CHOST line gets incorrectly added.
# ToDo(msb): remove this hack.
PACKAGES_FILE="${BOARD_ROOT}/packages/Packages"
sudo sed -e "s/CHOST: x86_64-pc-linux-gnu//" -i "${PACKAGES_FILE}"
# Handle existing directory.
if [[ -e "${BUILD_DIR}" ]]; then
if [[ ${FLAGS_replace} -eq ${FLAGS_TRUE} ]]; then
sudo rm -rf "${BUILD_DIR}"
else
error "Directory ${BUILD_DIR} already exists."
error "Use --build_attempt option to specify an unused attempt."
error "Or use --replace if you want to overwrite this directory."
die "Unwilling to overwrite ${BUILD_DIR}."
fi
fi
# Create the output directory and temporary mount points.
mkdir -p "${BUILD_DIR}"
# Create the base image.
create_base_container ${PRISTINE_IMAGE_NAME} ${FLAGS_enable_rootfs_verification} \
${FLAGS_enable_bootcache}
# Running board-specific setup if any exists.
if type board_setup &>/dev/null; then
board_setup "${BUILD_DIR}/${PRISTINE_IMAGE_NAME}"
fi
# Create a developer image if an image that is based on it is requested.
if should_build_image ${CHROMEOS_DEVELOPER_IMAGE_NAME} \
${CHROMEOS_TEST_IMAGE_NAME} ${CHROMEOS_FACTORY_TEST_IMAGE_NAME}; then
install_dev_packages ${CHROMEOS_DEVELOPER_IMAGE_NAME}
fi
# Generating AU generator zip file to run outside chroot
generate_au_zip || echo "Failed generating AU zip file - ignoring Error..."
# Create a named symlink.
LINK_NAME="${FLAGS_output_root}/${BOARD}/${FLAGS_symlink}"
ln -sfT $(basename ${BUILD_DIR}) ${LINK_NAME}
echo "Done. Image(s) created in ${BUILD_DIR}"
# Print out the images we generated.
if should_build_image ${CHROMEOS_BASE_IMAGE_NAME}; then
echo "Non-developer Chromium OS container created as ${PRISTINE_IMAGE_NAME}"
fi
if should_build_image ${CHROMEOS_DEVELOPER_IMAGE_NAME}; then
echo "Developer container created as ${CHROMEOS_DEVELOPER_IMAGE_NAME}"
fi
if should_build_image ${CHROMEOS_FACTORY_TEST_IMAGE_NAME}; then
echo "Factory test container created as ${CHROMEOS_FACTORY_TEST_IMAGE_NAME}"
fi
if should_build_image ${CHROMEOS_TEST_IMAGE_NAME}; then
echo "Test container created as ${CHROMEOS_TEST_IMAGE_NAME}"
fi
command_completed
cat << EOF
systemd-nspawn -b -D ${OUTSIDE_OUTPUT_DIR}
EOF

View File

@ -0,0 +1,187 @@
. "${SRC_ROOT}/platform/dev/toolchain_utils.sh" || exit 1
OUTSIDE_OUTPUT_DIR="src/build/containers/${BOARD}/${IMAGE_SUBDIR}/rootfs"
install_dev_packages() {
local image_name=$1
info "Adding developer packages to ${image_name}"
# Determine the root dir for developer packages.
local root_dev_dir="${root_fs_dir}/usr/local"
# Install developer packages described in chromeos-dev.
emerge_to_image --root="${root_dev_dir}" coreos-base/coreos-dev
# Copy over the libc debug info so that gdb
# works with threads and also for a better debugging experience.
sudo mkdir -p "${root_fs_dir}/usr/local/usr/lib/debug"
pbzip2 -dc --ignore-trailing-garbage=1 "${LIBC_PATH}" | \
sudo tar xpf - -C "${root_fs_dir}/usr/local/usr/lib/debug" \
./usr/lib/debug/usr/${CHOST} --strip-components=6
# Since gdb only looks in /usr/lib/debug, symlink the /usr/local
# path so that it is found automatically.
sudo ln -s /usr/local/usr/lib/debug "${root_fs_dir}/usr/lib/debug"
# Install the bare necessary files so that the "emerge" command works
sudo sed -i s,/usr/bin/wget,wget, \
${root_fs_dir}/usr/share/portage/config/make.globals
sudo mkdir -p ${root_fs_dir}/etc/make.profile
# Re-run ldconfig to fix /etc/ldconfig.so.cache.
sudo /sbin/ldconfig -r "${root_fs_dir}"
# Mark the image as a developer image (input to chromeos_startup).
# TODO(arkaitzr): Remove this file when applications no longer rely on it
# (crosbug.com/16648). The preferred way of determining developer mode status
# is via crossystem cros_debug?1 (checks boot args for "cros_debug").
sudo mkdir -p "${root_fs_dir}/root"
sudo touch "${root_fs_dir}/root/.dev_mode"
# Additional changes to developer image.
# Leave core files for developers to inspect.
sudo touch "${root_fs_dir}/root/.leave_core"
# This hack is only needed for devs who have old versions of glibc, which
# filtered out ldd when cross-compiling. TODO(davidjames): Remove this hack
# once everybody has upgraded to a new version of glibc.
if [[ ! -x "${root_fs_dir}/usr/bin/ldd" ]]; then
sudo cp -a "$(which ldd)" "${root_fs_dir}/usr/bin"
fi
# If vim is installed, then a vi symlink would probably help.
if [[ -x "${root_fs_dir}/usr/local/bin/vim" ]]; then
sudo ln -sf vim "${root_fs_dir}/usr/local/bin/vi"
fi
# If pygtk is installed in stateful-dev, then install a path.
if [[ -d \
"${root_fs_dir}/usr/local/lib/python2.6/site-packages/gtk-2.0" ]]; then
sudo bash -c "\
echo gtk-2.0 > \
${root_fs_dir}/usr/local/lib/python2.6/site-packages/pygtk.pth"
fi
# File searches /usr/share by default, so add a wrapper script so it
# can find the right path in /usr/local.
local path="${root_fs_dir}/usr/local/bin/file"
if [[ -x ${path} ]]; then
sudo mv "${path}" "${path}.bin"
sudo_clobber "${path}" <<EOF
#!/bin/sh
exec file.bin -m /usr/local/share/misc/magic.mgc "\$@"
EOF
sudo chmod a+rx "${path}"
fi
# If python is installed on stateful-dev, fix python symlinks.
local python_path="/usr/local/bin/python2.6"
if [ -e "${root_fs_dir}${python_path}" ]; then
info "Fixing python symlinks for developer and test images."
local python_paths="/usr/bin/python /usr/local/bin/python \
/usr/bin/python2 /usr/local/bin/python2"
for path in ${python_paths}; do
sudo rm -f "${root_fs_dir}${path}"
sudo ln -s ${python_path} "${root_fs_dir}${path}"
done
fi
cleanup_mounts
trap - EXIT
}
create_base_container() {
local image_name=$1
local rootfs_verification_enabled=$2
local bootcache_enabled=$3 local image_type="usb"
root_fs_dir="${BUILD_DIR}/rootfs"
stateful_fs_dir="${BUILD_DIR}/stateful"
esp_fs_dir="${BUILD_DIR}/esp"
oem_fs_dir="${BUILD_DIR}/oem"
trap "cleanup_mounts && delete_prompt" EXIT
cleanup_mounts &> /dev/null
mkdir -p "${root_fs_dir}"
mkdir -p "${stateful_fs_dir}"
mkdir -p "${oem_fs_dir}"
# Prepare stateful partition with some pre-created directories.
sudo mkdir "${stateful_fs_dir}/dev_image"
sudo mkdir "${stateful_fs_dir}/var_overlay"
# Create symlinks so that /usr/local/usr based directories are symlinked to
# /usr/local/ directories e.g. /usr/local/usr/bin -> /usr/local/bin, etc.
setup_symlinks_on_root "${stateful_fs_dir}/dev_image" \
"${stateful_fs_dir}/var_overlay" "${stateful_fs_dir}"
# Perform binding rather than symlinking because directories must exist
# on rootfs so that we can bind at run-time since rootfs is read-only.
info "Binding directories from stateful partition onto the rootfs"
sudo mkdir -p "${root_fs_dir}/usr/local"
sudo mount --bind "${stateful_fs_dir}/dev_image" "${root_fs_dir}/usr/local"
sudo mkdir -p "${root_fs_dir}/var"
sudo mount --bind "${stateful_fs_dir}/var_overlay" "${root_fs_dir}/var"
sudo mkdir -p "${root_fs_dir}/dev"
info "Binding directories from OEM partition onto the rootfs"
sudo mkdir -p "${root_fs_dir}/usr/share/oem"
sudo mount --bind "${oem_fs_dir}" "${root_fs_dir}/usr/share/oem"
# We need to install libc manually from the cross toolchain.
# TODO: Improve this? It would be ideal to use emerge to do this.
PKGDIR="/var/lib/portage/pkgs"
LIBC_TAR="glibc-${LIBC_VERSION}.tbz2"
LIBC_PATH="${PKGDIR}/cross-${CHOST}/${LIBC_TAR}"
if ! [[ -e ${LIBC_PATH} ]]; then
die_notrace \
"${LIBC_PATH} does not exist. Try running ./setup_board" \
"--board=${BOARD} to update the version of libc installed on that board."
fi
# Strip out files we don't need in the final image at runtime.
local libc_excludes=(
# Compile-time headers.
'usr/include' 'sys-include'
# Link-time objects.
'*.[ao]'
)
pbzip2 -dc --ignore-trailing-garbage=1 "${LIBC_PATH}" | \
sudo tar xpf - -C "${root_fs_dir}" ./usr/${CHOST} \
--strip-components=3 "${libc_excludes[@]/#/--exclude=}"
board_ctarget=$(get_ctarget_from_board "${BOARD}")
for atom in $(portageq match / cross-$board_ctarget/gcc); do
copy_gcc_libs "${root_fs_dir}" $atom
done
# We "emerge --root=${root_fs_dir} --root-deps=rdeps --usepkgonly" all of the
# runtime packages for chrome os. This builds up a chrome os image from
# binary packages with runtime dependencies only. We use INSTALL_MASK to
# trim the image size as much as possible.
emerge_to_image --root="${root_fs_dir}" ${BASE_PACKAGE}
# Set /etc/lsb-release on the image.
"${OVERLAY_CHROMEOS_DIR}/scripts/cros_set_lsb_release" \
--root="${root_fs_dir}" \
--board="${BOARD}"
# Create the boot.desc file which stores the build-time configuration
# information needed for making the image bootable after creation with
# cros_make_image_bootable.
create_boot_desc "${image_type}"
# Clean up symlinks so they work on a running target rooted at "/".
# Here development packages are rooted at /usr/local. However, do not
# create /usr/local or /var on host (already exist on target).
setup_symlinks_on_root "/usr/local" "/var" "${stateful_fs_dir}"
USE_DEV_KEYS=
if should_build_image ${CHROMEOS_FACTORY_INSTALL_SHIM_NAME}; then
USE_DEV_KEYS="--use_dev_keys"
fi
}