blob: f926a5dc703b8169bd530967e8c3c369d16b8072 [file] [log] [blame]
J. Richard Barnette84477772011-07-25 13:41:33 -07001#!/bin/bash
Bill Richardsonc09b94f2010-03-15 11:40:30 -07002
Peter Mayo82975f92012-05-01 11:03:31 -04003# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Bill Richardsonc09b94f2010-03-15 11:40:30 -07004# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7# Script to build a bootable keyfob-based chromeos system image from within
8# a chromiumos setup. This assumes that all needed packages have been built into
9# the given target's root with binary packages turned on. This script will
10# build the Chrome OS image using only pre-built binary packages.
11
Benjamin Gordonedef43c2019-05-31 13:05:00 -060012SCRIPT_ROOT="$(dirname "$(readlink -f "$0")")"
13# shellcheck source=build_library/build_common.sh
J. Richard Barnette84477772011-07-25 13:41:33 -070014. "${SCRIPT_ROOT}/build_library/build_common.sh" || exit 1
Bill Richardsonc09b94f2010-03-15 11:40:30 -070015
David James855afb72012-03-14 20:04:59 -070016# Developer-visible flags.
Liam McLoughlin12a9a842012-10-10 10:37:06 -040017DEFINE_string adjust_part "" \
Chris Sosa36b4e302013-10-17 11:54:50 -070018 "Adjustments to apply to partition table (LABEL:[+-=]SIZE) e.g. ROOT-A:+1G"
Don Garrette0020b12010-06-17 15:55:35 -070019DEFINE_string board "${DEFAULT_BOARD}" \
Bill Richardsonc09b94f2010-03-15 11:40:30 -070020 "The board to build an image for."
David James855afb72012-03-14 20:04:59 -070021DEFINE_string boot_args "noinitrd" \
22 "Additional boot arguments to pass to the commandline"
Paul Taysom00df6f62012-11-20 12:35:40 -080023DEFINE_boolean enable_bootcache ${FLAGS_FALSE} \
24 "Default all bootloaders to NOT use boot cache."
David James855afb72012-03-14 20:04:59 -070025DEFINE_boolean enable_rootfs_verification ${FLAGS_TRUE} \
Simon Glass00fec7f2018-03-06 10:04:44 -070026 "Default all bootloaders to use kernel-based root fs integrity checking." \
27 r
Bill Richardsonc09b94f2010-03-15 11:40:30 -070028DEFINE_string output_root "${DEFAULT_BUILD_ROOT}/images" \
29 "Directory in which to place image result directories (named by version)"
Liam McLoughlin5b37c542012-08-16 11:09:37 -070030DEFINE_string disk_layout "default" \
Liam McLoughlinb78a7c32012-11-06 20:19:05 -050031 "The disk layout type to use for this image."
Paul Taysom5e39bb62013-01-18 07:39:25 -080032DEFINE_string enable_serial "" \
33 "Enable serial port for printks. Example values: ttyS0"
Zach Reiznerf09d3d12014-09-24 10:45:19 -070034DEFINE_integer loglevel 7 \
35 "The loglevel to add to the kernel command line."
xixuan4a5af052016-08-03 19:20:38 -070036DEFINE_string builder_path "" \
37 "The build_name to be installed on DUT during hwtest."
38
Che-Liang Chiou611d5b72011-07-27 16:03:26 +080039
Chris Sosaef4a6452011-10-27 16:01:07 -070040FLAGS_HELP="USAGE: build_image [flags] [list of images to build].
41This script is used to build a Chromium OS image. Chromium OS comes in many
42different forms. This scripts can be used to build the following:
43
44base - Pristine Chromium OS image. As similar to Chrome OS as possible.
Marc Herbertbc4ed812015-01-21 11:41:14 -080045dev [default] - Developer image. Like base but with additional dev packages.
Chris Sosaef4a6452011-10-27 16:01:07 -070046test - Like dev, but with additional test specific packages and can be easily
Christopher Wiley5cfcf952015-05-19 09:50:35 -070047 used for automated testing using scripts like test_that, etc.
Chris Sosaef4a6452011-10-27 16:01:07 -070048factory_install - Install shim for bootstrapping the factory test process.
49 Cannot be built along with any other image.
50
David James855afb72012-03-14 20:04:59 -070051Examples:
Chris Sosaef4a6452011-10-27 16:01:07 -070052
53build_image --board=<board> dev test - builds developer and test images.
54build_image --board=<board> factory_install - builds a factory install shim.
Chris Sosa36b4e302013-10-17 11:54:50 -070055
56Note if you want to build an image with custom size partitions, either consider
57adding a new disk layout in build_library/legacy_disk_layout.json OR use
Chris Sosa1cffeee2013-11-25 14:37:36 -080058adjust_part. See the help above but here are a few examples:
Chris Sosa36b4e302013-10-17 11:54:50 -070059
Chris Sosa1cffeee2013-11-25 14:37:36 -080060adjust_part='STATE:+1G' -- add one GB to the size the stateful partition
61adjust_part='ROOT-A:-1G' -- remove one GB from the primary rootfs partition
62adjust_part='STATE:=1G' -- make the stateful partition 1 GB
Chris Sosaef4a6452011-10-27 16:01:07 -070063...
64"
David James855afb72012-03-14 20:04:59 -070065
66# The following options are advanced options, only available to those willing
67# to read the source code. They are not shown in help output, since they are
68# not needed for the typical developer workflow.
David James855afb72012-03-14 20:04:59 -070069DEFINE_integer build_attempt 1 \
70 "The build attempt for this image build."
Steve Fung817a1ef2015-05-06 17:23:17 -070071DEFINE_string build_root "${DEFAULT_BUILD_ROOT}/images" \
72 "Directory in which to compose the image, before copying it to output_root."
David James855afb72012-03-14 20:04:59 -070073DEFINE_integer jobs -1 \
74 "How many packages to build in parallel at maximum."
75DEFINE_boolean replace ${FLAGS_FALSE} \
76 "Overwrite existing output, if any."
77DEFINE_string symlink "latest" \
78 "Symlink name to use for this image."
Liam McLoughlin07279032012-06-25 17:31:11 -070079DEFINE_string version "" \
80 "Overrides version number in name to this version."
Waldemar Rymarkiewiczc9df4a82016-09-08 15:53:43 +020081DEFINE_string output_suffix "" \
82 "Add custom suffix to output directory."
Caroline Tice128c97b2016-05-12 11:11:41 -070083DEFINE_boolean eclean ${FLAGS_TRUE} \
84 "Do NOT call eclean before building the image (default is to call eclean)."
Chris Sosaef4a6452011-10-27 16:01:07 -070085
Bill Richardsonc09b94f2010-03-15 11:40:30 -070086# Parse command line.
87FLAGS "$@" || exit 1
Mike Frysinger454c5262012-12-01 14:18:51 -050088
89# See if we want to default the bootcache flag before we clobber
90# the user's command line. We want to default to false if the
91# user explicitly disabled rootfs verification otherwise they
92# have to manually specify both.
93FLAGS_bootcache_use_board_default=${FLAGS_enable_rootfs_verification}
94case " $* " in
95 *" --enable_bootcache "*|\
96 *" --noenable_bootcache "*)
97 FLAGS_bootcache_use_board_default=${FLAGS_FALSE}
98 ;;
99esac
100
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700101eval set -- "${FLAGS_ARGV}"
102
Ralph Nathana5ba50e2015-04-21 13:41:12 -0700103# Only now can we die on error. shflags functions leak non-zero error codes,
Brian Harring7f175a52012-03-02 05:37:00 -0800104# so will die prematurely if 'switch_to_strict_mode' is specified before now.
105switch_to_strict_mode
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700106
J. Richard Barnettee4e3dec2011-08-08 16:35:02 -0700107# Determine build version.
108OVERLAY_CHROMEOS_DIR="${SRC_ROOT}/third_party/chromiumos-overlay/chromeos"
Benjamin Gordonedef43c2019-05-31 13:05:00 -0600109# shellcheck source=../third_party/chromiumos-overlay/chromeos/config/chromeos_version.sh
J. Richard Barnettee4e3dec2011-08-08 16:35:02 -0700110. "${OVERLAY_CHROMEOS_DIR}/config/chromeos_version.sh" || exit 1
J. Richard Barnettea308b392011-08-29 13:53:34 -0700111# N.B. Ordering matters for some of the libraries below, because
112# some of the files contain initialization used by later files.
Benjamin Gordonedef43c2019-05-31 13:05:00 -0600113# shellcheck source=build_library/board_options.sh
J. Richard Barnetteaaef7612011-08-11 17:10:28 -0700114. "${BUILD_LIBRARY_DIR}/board_options.sh" || exit 1
Benjamin Gordonedef43c2019-05-31 13:05:00 -0600115# shellcheck source=build_library/disk_layout_util.sh
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700116. "${BUILD_LIBRARY_DIR}/disk_layout_util.sh" || exit 1
Benjamin Gordonedef43c2019-05-31 13:05:00 -0600117# shellcheck source=build_library/mount_gpt_util.sh
J. Richard Barnette5f9dbe42011-08-22 14:31:33 -0700118. "${BUILD_LIBRARY_DIR}/mount_gpt_util.sh" || exit 1
Benjamin Gordonedef43c2019-05-31 13:05:00 -0600119# shellcheck source=build_library/build_image_util.sh
J. Richard Barnettea308b392011-08-29 13:53:34 -0700120. "${BUILD_LIBRARY_DIR}/build_image_util.sh" || exit 1
Benjamin Gordonedef43c2019-05-31 13:05:00 -0600121# shellcheck source=build_library/base_image_util.sh
J. Richard Barnettea308b392011-08-29 13:53:34 -0700122. "${BUILD_LIBRARY_DIR}/base_image_util.sh" || exit 1
Benjamin Gordonedef43c2019-05-31 13:05:00 -0600123# shellcheck source=build_library/dev_image_util.sh
J. Richard Barnettea308b392011-08-29 13:53:34 -0700124. "${BUILD_LIBRARY_DIR}/dev_image_util.sh" || exit 1
Benjamin Gordonedef43c2019-05-31 13:05:00 -0600125# shellcheck source=build_library/test_image_util.sh
J. Richard Barnettea308b392011-08-29 13:53:34 -0700126. "${BUILD_LIBRARY_DIR}/test_image_util.sh" || exit 1
Benjamin Gordonedef43c2019-05-31 13:05:00 -0600127# shellcheck source=build_library/selinux_util.sh
Qijiang Fana41058a2018-08-15 11:41:46 +0900128. "${BUILD_LIBRARY_DIR}/selinux_util.sh" || exit 1
J. Richard Barnettee4e3dec2011-08-08 16:35:02 -0700129
Chris Sosab0f57322011-10-25 03:07:23 +0000130parse_build_image_args
Peter Mayo82975f92012-05-01 11:03:31 -0400131
Steve Fung88897cc2015-03-25 13:38:38 -0700132load_board_specific_script "board_specific_setup.sh"
Liam McLoughlind606ee22012-07-03 16:48:57 -0700133
Chris Masonebbccc242014-02-08 16:23:53 -0800134sudo_clear_shadow_locks "/build/${FLAGS_board}"
135
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700136# TODO: <prebuild hook>
Peter Mayo82975f92012-05-01 11:03:31 -0400137
Mike Frysinger22621952014-04-01 17:41:20 -0400138BASE_PACKAGE="virtual/target-os"
Vic Yang8e0cd072012-12-18 22:31:46 +0800139# Tweak flags, configure extra USE flags, and set base packages for the factory
Chris Sosa4299eb92011-11-01 12:54:01 -0700140# install shim.
Benjamin Gordonedef43c2019-05-31 13:05:00 -0600141if should_build_image "${CHROMEOS_FACTORY_INSTALL_SHIM_NAME}"; then
Nick Sandersae26a5c2010-08-30 21:59:50 -0700142 # TODO: Build a separated ebuild for the install shim to reduce size.
Hung-Te Lind32c59f2012-01-19 19:54:01 +0800143 INSTALL_MASK="${FACTORY_SHIM_INSTALL_MASK}"
Nick Sandersae26a5c2010-08-30 21:59:50 -0700144
Jon Salz67145df2011-10-20 14:05:06 +0800145 # Add the cros_factory_install boot arg.
146 FLAGS_boot_args="${FLAGS_boot_args} cros_factory_install"
Chris Sosa4299eb92011-11-01 12:54:01 -0700147
Hung-Te Lin9b43ced2016-01-14 20:29:47 +0800148 BASE_PACKAGE="virtual/target-os-factory-shim"
Vic Yang8e0cd072012-12-18 22:31:46 +0800149
Andrey Pronin6a214702016-08-01 18:18:01 -0700150 export USE="${USE} fbconsole vtconsole factory_shim_ramfs i2cdev vfat"
Nick Sandersae26a5c2010-08-30 21:59:50 -0700151fi
152
Sabin Floarese790ea72016-11-21 14:56:41 +0200153# Mask systemd directories if this is not a systemd image.
Benjamin Gordonedef43c2019-05-31 13:05:00 -0600154if ! has "systemd" "$("portageq-${FLAGS_board}" envvar USE)"; then
Sabin Floarese790ea72016-11-21 14:56:41 +0200155 COMMON_INSTALL_MASK+=" ${SYSTEMD_INSTALL_MASK}"
156 DEFAULT_INSTALL_MASK+=" ${SYSTEMD_INSTALL_MASK}"
Sabin Floarese790ea72016-11-21 14:56:41 +0200157 FACTORY_SHIM_INSTALL_MASK+=" ${SYSTEMD_INSTALL_MASK}"
158 INSTALL_MASK+=" ${SYSTEMD_INSTALL_MASK}"
159fi
160
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700161# TODO: </prebuild hook>
Tan Gao843b70a2010-08-17 09:41:48 -0700162
Chris Sosa9673f3b2010-05-18 13:24:40 -0700163# If we are creating a developer image, also create a pristine image with a
164# different name.
Chris Sosa93e7b782012-01-03 15:52:12 -0800165PRISTINE_IMAGE_NAME=
Benjamin Gordonedef43c2019-05-31 13:05:00 -0600166if should_build_image "${CHROMEOS_FACTORY_INSTALL_SHIM_NAME}"; then
167 PRISTINE_IMAGE_NAME="${CHROMEOS_FACTORY_INSTALL_SHIM_NAME}"
Chris Sosa93e7b782012-01-03 15:52:12 -0800168else
Benjamin Gordonedef43c2019-05-31 13:05:00 -0600169 PRISTINE_IMAGE_NAME="${CHROMEOS_BASE_IMAGE_NAME}"
Tan Gao843b70a2010-08-17 09:41:48 -0700170fi
171
Caroline Tice128c97b2016-05-12 11:11:41 -0700172if [[ ${FLAGS_eclean} -eq ${FLAGS_TRUE} ]]; then
173 "${BOARD_ROOT}/build/bin/eclean" -d packages
174fi
Will Drewry12f14ce2010-08-17 17:27:16 -0500175
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700176# Handle existing directory.
Chris Sosab0f57322011-10-25 03:07:23 +0000177if [[ -e "${BUILD_DIR}" ]]; then
Don Garrette0020b12010-06-17 15:55:35 -0700178 if [[ ${FLAGS_replace} -eq ${FLAGS_TRUE} ]]; then
Chris Sosab0f57322011-10-25 03:07:23 +0000179 sudo rm -rf "${BUILD_DIR}"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700180 else
Chris Sosab0f57322011-10-25 03:07:23 +0000181 error "Directory ${BUILD_DIR} already exists."
182 error "Use --build_attempt option to specify an unused attempt."
Brian Harring7f175a52012-03-02 05:37:00 -0800183 error "Or use --replace if you want to overwrite this directory."
184 die "Unwilling to overwrite ${BUILD_DIR}."
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700185 fi
186fi
187
J. Richard Barnettea308b392011-08-29 13:53:34 -0700188# Create the output directory and temporary mount points.
Chris Sosab0f57322011-10-25 03:07:23 +0000189mkdir -p "${BUILD_DIR}"
Don Garrett3f41e152010-06-21 14:54:34 -0700190
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700191# Create the base image.
Benjamin Gordonedef43c2019-05-31 13:05:00 -0600192create_base_image "${PRISTINE_IMAGE_NAME}" \
193 "${FLAGS_enable_rootfs_verification}" "${FLAGS_enable_bootcache}"
Tan Gao843b70a2010-08-17 09:41:48 -0700194
Liam McLoughlind606ee22012-07-03 16:48:57 -0700195# Running board-specific setup if any exists.
196if type board_setup &>/dev/null; then
197 board_setup "${BUILD_DIR}/${PRISTINE_IMAGE_NAME}"
198fi
199
Chris Sosac9422fa2012-03-05 15:58:07 -0800200# Create a developer image if an image that is based on it is requested.
Benjamin Gordonedef43c2019-05-31 13:05:00 -0600201if should_build_image "${CHROMEOS_DEVELOPER_IMAGE_NAME}" \
202 "${CHROMEOS_TEST_IMAGE_NAME}"; then
203 copy_image "${CHROMEOS_BASE_IMAGE_NAME}" "${CHROMEOS_DEVELOPER_IMAGE_NAME}"
204 install_dev_packages "${CHROMEOS_DEVELOPER_IMAGE_NAME}"
Bill Richardson6ed13582010-06-16 21:38:15 -0700205fi
206
Hung-Te Lin269680c2016-05-30 14:47:37 +0800207# From a developer image create a test image.
Benjamin Gordonedef43c2019-05-31 13:05:00 -0600208if should_build_image "${CHROMEOS_TEST_IMAGE_NAME}"; then
209 copy_image "${CHROMEOS_DEVELOPER_IMAGE_NAME}" "${CHROMEOS_TEST_IMAGE_NAME}"
210 mod_image_for_test "${CHROMEOS_TEST_IMAGE_NAME}"
Chris Sosac9422fa2012-03-05 15:58:07 -0800211fi
Vince Laviano6aebf312011-06-01 15:27:31 -0700212
Steve Fung5d7a0ae2015-05-08 15:16:12 -0700213# Move the completed image to the output_root.
214move_image "${BUILD_DIR}" "${OUTPUT_DIR}"
Steve Fung817a1ef2015-05-06 17:23:17 -0700215
Colin Howesf4131092018-09-28 14:08:48 -0700216# Move DLC images to the output_root directory.
Jae Hoon Kim184a5742019-12-03 17:44:22 -0800217build_dlc --sysroot="${BOARD_ROOT}" --install-root-dir="${OUTPUT_DIR}/dlc"
Colin Howesf4131092018-09-28 14:08:48 -0700218
David James4dd4c542011-08-10 10:19:47 -0700219# Create a named symlink.
220LINK_NAME="${FLAGS_output_root}/${BOARD}/${FLAGS_symlink}"
Benjamin Gordonedef43c2019-05-31 13:05:00 -0600221ln -sfT "$(basename "${OUTPUT_DIR}")" "${LINK_NAME}"
Olof Johansson58f25cc2010-08-04 21:18:49 -0700222
Mike Frysinger33482c82019-02-22 15:50:39 -0500223info "Done. Image(s) created in ${OUTPUT_DIR}"
Mike Frysingerb86854a2014-11-25 18:43:58 -0500224echo
Chris Sosac9422fa2012-03-05 15:58:07 -0800225
226# Print out the images we generated.
Mike Frysingerb86854a2014-11-25 18:43:58 -0500227summarize() {
228 local name="$1" img="$2"
Steve Fung1fcece02015-04-06 17:54:50 -0700229 local dir_path
230
Benjamin Gordonedef43c2019-05-31 13:05:00 -0600231 # __flags_output_root_default defined in shflags.
232 # shellcheck disable=2154
Steve Fung1fcece02015-04-06 17:54:50 -0700233 if [[ "${FLAGS_output_root}" == "${__flags_output_root_default}" ]]; then
234 dir_path=${OUTSIDE_OUTPUT_DIR}
235 else
Steve Fung817a1ef2015-05-06 17:23:17 -0700236 dir_path="${OUTPUT_DIR}"
Steve Fung1fcece02015-04-06 17:54:50 -0700237 fi
Mike Frysingerb86854a2014-11-25 18:43:58 -0500238
239 info "${name} image created as ${img}"
240 info "To copy the image to a USB key, use:"
Mike Frysinger4fc1d372019-02-22 15:45:24 -0500241 info " cros flash usb:// ${dir_path}/${img}"
Enrico Granatae9d9f9c2019-06-17 15:43:30 -0700242 info "To flash the image to a Chrome OS device, use:"
243 info " cros flash YOUR_DEVICE_IP ${dir_path}/${img}"
244 info "Note that the device must be accessible over the network."
245 info "A base image will not work in this mode, but a test or dev image will."
Mike Frysinger4fc1d372019-02-22 15:45:24 -0500246 if [[ $# -ge 3 ]]; then
247 info "To convert it to a VM image, use:"
248 info " ./image_to_vm.sh --from=${dir_path} --board=${BOARD} $3"
Mike Frysingerb86854a2014-11-25 18:43:58 -0500249 fi
250 echo
251}
Benjamin Gordonedef43c2019-05-31 13:05:00 -0600252if should_build_image "${CHROMEOS_BASE_IMAGE_NAME}"; then
Mike Frysingerb86854a2014-11-25 18:43:58 -0500253 summarize "Non-developer Chromium OS" "${PRISTINE_IMAGE_NAME}"
Chris Sosac9422fa2012-03-05 15:58:07 -0800254fi
Benjamin Gordonedef43c2019-05-31 13:05:00 -0600255if should_build_image "${CHROMEOS_FACTORY_SHIM_NAME}"; then
Mike Frysingerb86854a2014-11-25 18:43:58 -0500256 summarize "Chromium OS Factory install shim" "${PRISTINE_IMAGE_NAME}"
Chris Sosac9422fa2012-03-05 15:58:07 -0800257fi
Benjamin Gordonedef43c2019-05-31 13:05:00 -0600258if should_build_image "${CHROMEOS_DEVELOPER_IMAGE_NAME}"; then
Mike Frysingerb86854a2014-11-25 18:43:58 -0500259 summarize "Developer" "${CHROMEOS_DEVELOPER_IMAGE_NAME}" ""
Chris Sosa9673f3b2010-05-18 13:24:40 -0700260fi
Benjamin Gordonedef43c2019-05-31 13:05:00 -0600261if should_build_image "${CHROMEOS_TEST_IMAGE_NAME}"; then
Mike Frysingerb86854a2014-11-25 18:43:58 -0500262 summarize "Test" "${CHROMEOS_TEST_IMAGE_NAME}" "--test"
Vince Laviano6aebf312011-06-01 15:27:31 -0700263fi
Nick Sanders8ab729a2010-06-16 03:15:17 -0700264
Matt Tennant0a9d32d2012-07-30 16:51:37 -0700265command_completed