blob: 1c28a454a5d8733936f588102b48d522aea99acf [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
Brian Harringaa13ea42012-03-15 18:31:03 -070012SCRIPT_ROOT=$(dirname $(readlink -f "$0"))
J. Richard Barnette84477772011-07-25 13:41:33 -070013. "${SCRIPT_ROOT}/build_library/build_common.sh" || exit 1
Bill Richardsonc09b94f2010-03-15 11:40:30 -070014
David James855afb72012-03-14 20:04:59 -070015# Developer-visible flags.
Liam McLoughlin12a9a842012-10-10 10:37:06 -040016DEFINE_string adjust_part "" \
Chris Sosa36b4e302013-10-17 11:54:50 -070017 "Adjustments to apply to partition table (LABEL:[+-=]SIZE) e.g. ROOT-A:+1G"
Don Garrette0020b12010-06-17 15:55:35 -070018DEFINE_string board "${DEFAULT_BOARD}" \
Bill Richardsonc09b94f2010-03-15 11:40:30 -070019 "The board to build an image for."
David James855afb72012-03-14 20:04:59 -070020DEFINE_string boot_args "noinitrd" \
21 "Additional boot arguments to pass to the commandline"
Paul Taysom00df6f62012-11-20 12:35:40 -080022DEFINE_boolean enable_bootcache ${FLAGS_FALSE} \
23 "Default all bootloaders to NOT use boot cache."
David James855afb72012-03-14 20:04:59 -070024DEFINE_boolean enable_rootfs_verification ${FLAGS_TRUE} \
25 "Default all bootloaders to use kernel-based root fs integrity checking."
Bill Richardsonc09b94f2010-03-15 11:40:30 -070026DEFINE_string output_root "${DEFAULT_BUILD_ROOT}/images" \
27 "Directory in which to place image result directories (named by version)"
Liam McLoughlin5b37c542012-08-16 11:09:37 -070028DEFINE_string disk_layout "default" \
Liam McLoughlinb78a7c32012-11-06 20:19:05 -050029 "The disk layout type to use for this image."
Paul Taysom5e39bb62013-01-18 07:39:25 -080030DEFINE_string enable_serial "" \
31 "Enable serial port for printks. Example values: ttyS0"
Zach Reiznerf09d3d12014-09-24 10:45:19 -070032DEFINE_integer loglevel 7 \
33 "The loglevel to add to the kernel command line."
Che-Liang Chiou611d5b72011-07-27 16:03:26 +080034
Chris Sosaef4a6452011-10-27 16:01:07 -070035FLAGS_HELP="USAGE: build_image [flags] [list of images to build].
36This script is used to build a Chromium OS image. Chromium OS comes in many
37different forms. This scripts can be used to build the following:
38
39base - Pristine Chromium OS image. As similar to Chrome OS as possible.
Marc Herbertbc4ed812015-01-21 11:41:14 -080040dev [default] - Developer image. Like base but with additional dev packages.
Chris Sosaef4a6452011-10-27 16:01:07 -070041test - Like dev, but with additional test specific packages and can be easily
Christopher Wiley5cfcf952015-05-19 09:50:35 -070042 used for automated testing using scripts like test_that, etc.
Chris Sosaef4a6452011-10-27 16:01:07 -070043factory_test - Like test but with extra packages and modifications used to
44 test images in a factory setting. Cannot be built along with a test image.
45factory_install - Install shim for bootstrapping the factory test process.
46 Cannot be built along with any other image.
47
David James855afb72012-03-14 20:04:59 -070048Examples:
Chris Sosaef4a6452011-10-27 16:01:07 -070049
50build_image --board=<board> dev test - builds developer and test images.
51build_image --board=<board> factory_install - builds a factory install shim.
Chris Sosa36b4e302013-10-17 11:54:50 -070052
53Note if you want to build an image with custom size partitions, either consider
54adding a new disk layout in build_library/legacy_disk_layout.json OR use
Chris Sosa1cffeee2013-11-25 14:37:36 -080055adjust_part. See the help above but here are a few examples:
Chris Sosa36b4e302013-10-17 11:54:50 -070056
Chris Sosa1cffeee2013-11-25 14:37:36 -080057adjust_part='STATE:+1G' -- add one GB to the size the stateful partition
58adjust_part='ROOT-A:-1G' -- remove one GB from the primary rootfs partition
59adjust_part='STATE:=1G' -- make the stateful partition 1 GB
Chris Sosaef4a6452011-10-27 16:01:07 -070060...
61"
David James855afb72012-03-14 20:04:59 -070062show_help_if_requested "$@"
63
64# The following options are advanced options, only available to those willing
65# to read the source code. They are not shown in help output, since they are
66# not needed for the typical developer workflow.
David James855afb72012-03-14 20:04:59 -070067DEFINE_integer build_attempt 1 \
68 "The build attempt for this image build."
Steve Fung817a1ef2015-05-06 17:23:17 -070069DEFINE_string build_root "${DEFAULT_BUILD_ROOT}/images" \
70 "Directory in which to compose the image, before copying it to output_root."
David James855afb72012-03-14 20:04:59 -070071DEFINE_integer jobs -1 \
72 "How many packages to build in parallel at maximum."
Ralph Nathana5ba50e2015-04-21 13:41:12 -070073DEFINE_boolean progress_bar ${FLAGS_FALSE} \
74 "Display helper messages to allow progress to be easily measured."
David James855afb72012-03-14 20:04:59 -070075DEFINE_boolean replace ${FLAGS_FALSE} \
76 "Overwrite existing output, if any."
77DEFINE_string symlink "latest" \
78 "Symlink name to use for this image."
Ralph Nathane28ff1b2015-06-05 17:28:59 -070079DEFINE_string toolset "cros" \
80 "Toolset that this script is being used with."
Liam McLoughlin07279032012-06-25 17:31:11 -070081DEFINE_string version "" \
82 "Overrides version number in name to this version."
Chris Sosaef4a6452011-10-27 16:01:07 -070083
Bill Richardsonc09b94f2010-03-15 11:40:30 -070084# Parse command line.
85FLAGS "$@" || exit 1
Mike Frysinger454c5262012-12-01 14:18:51 -050086
87# See if we want to default the bootcache flag before we clobber
88# the user's command line. We want to default to false if the
89# user explicitly disabled rootfs verification otherwise they
90# have to manually specify both.
91FLAGS_bootcache_use_board_default=${FLAGS_enable_rootfs_verification}
92case " $* " in
93 *" --enable_bootcache "*|\
94 *" --noenable_bootcache "*)
95 FLAGS_bootcache_use_board_default=${FLAGS_FALSE}
96 ;;
97esac
98
Bill Richardsonc09b94f2010-03-15 11:40:30 -070099eval set -- "${FLAGS_ARGV}"
100
Ralph Nathana5ba50e2015-04-21 13:41:12 -0700101# Only now can we die on error. shflags functions leak non-zero error codes,
Brian Harring7f175a52012-03-02 05:37:00 -0800102# so will die prematurely if 'switch_to_strict_mode' is specified before now.
103switch_to_strict_mode
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700104
J. Richard Barnettee4e3dec2011-08-08 16:35:02 -0700105# Determine build version.
106OVERLAY_CHROMEOS_DIR="${SRC_ROOT}/third_party/chromiumos-overlay/chromeos"
107. "${OVERLAY_CHROMEOS_DIR}/config/chromeos_version.sh" || exit 1
J. Richard Barnettea308b392011-08-29 13:53:34 -0700108# N.B. Ordering matters for some of the libraries below, because
109# some of the files contain initialization used by later files.
J. Richard Barnetteaaef7612011-08-11 17:10:28 -0700110. "${BUILD_LIBRARY_DIR}/board_options.sh" || exit 1
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700111. "${BUILD_LIBRARY_DIR}/disk_layout_util.sh" || exit 1
J. Richard Barnette5f9dbe42011-08-22 14:31:33 -0700112. "${BUILD_LIBRARY_DIR}/mount_gpt_util.sh" || exit 1
J. Richard Barnettea308b392011-08-29 13:53:34 -0700113. "${BUILD_LIBRARY_DIR}/build_image_util.sh" || exit 1
114. "${BUILD_LIBRARY_DIR}/base_image_util.sh" || exit 1
115. "${BUILD_LIBRARY_DIR}/dev_image_util.sh" || exit 1
116. "${BUILD_LIBRARY_DIR}/test_image_util.sh" || exit 1
J. Richard Barnetteaaef7612011-08-11 17:10:28 -0700117. "${BUILD_LIBRARY_DIR}/test_image_content.sh" || exit 1
J. Richard Barnettee4e3dec2011-08-08 16:35:02 -0700118
Chris Sosab0f57322011-10-25 03:07:23 +0000119parse_build_image_args
Peter Mayo82975f92012-05-01 11:03:31 -0400120
Steve Fung88897cc2015-03-25 13:38:38 -0700121load_board_specific_script "board_specific_setup.sh"
Liam McLoughlind606ee22012-07-03 16:48:57 -0700122
Chris Masonebbccc242014-02-08 16:23:53 -0800123sudo_clear_shadow_locks "/build/${FLAGS_board}"
124
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700125# TODO: <prebuild hook>
Peter Mayo82975f92012-05-01 11:03:31 -0400126
Mike Frysinger22621952014-04-01 17:41:20 -0400127BASE_PACKAGE="virtual/target-os"
Vic Yang8e0cd072012-12-18 22:31:46 +0800128# Tweak flags, configure extra USE flags, and set base packages for the factory
Chris Sosa4299eb92011-11-01 12:54:01 -0700129# install shim.
Chris Sosac9422fa2012-03-05 15:58:07 -0800130if should_build_image ${CHROMEOS_FACTORY_INSTALL_SHIM_NAME}; then
Nick Sandersae26a5c2010-08-30 21:59:50 -0700131 # TODO: Build a separated ebuild for the install shim to reduce size.
Hung-Te Lind32c59f2012-01-19 19:54:01 +0800132 INSTALL_MASK="${FACTORY_SHIM_INSTALL_MASK}"
Nick Sandersae26a5c2010-08-30 21:59:50 -0700133
Jon Salz67145df2011-10-20 14:05:06 +0800134 # Add the cros_factory_install boot arg.
135 FLAGS_boot_args="${FLAGS_boot_args} cros_factory_install"
Chris Sosa4299eb92011-11-01 12:54:01 -0700136
Hung-Te Lin9b43ced2016-01-14 20:29:47 +0800137 BASE_PACKAGE="virtual/target-os-factory-shim"
Vic Yang8e0cd072012-12-18 22:31:46 +0800138
Hung-Te Linf6f180c2015-02-11 20:09:02 +0800139 export USE="${USE} fbconsole vtconsole factory_shim_ramfs tpm i2cdev vfat"
Nick Sandersae26a5c2010-08-30 21:59:50 -0700140fi
141
Kees Cook8df86b22012-09-28 18:15:49 -0700142if should_build_image ${CHROMEOS_FACTORY_TEST_IMAGE_NAME}; then
143 # Disable module restrictions on factory test image to allow for
144 # external third party drivers in /usr/local.
145 FLAGS_boot_args="${FLAGS_boot_args} lsm.module_locking=0"
146fi
147
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700148# TODO: </prebuild hook>
Tan Gao843b70a2010-08-17 09:41:48 -0700149
Chris Sosa9673f3b2010-05-18 13:24:40 -0700150# If we are creating a developer image, also create a pristine image with a
151# different name.
Chris Sosa93e7b782012-01-03 15:52:12 -0800152PRISTINE_IMAGE_NAME=
Chris Sosac9422fa2012-03-05 15:58:07 -0800153if should_build_image ${CHROMEOS_FACTORY_INSTALL_SHIM_NAME}; then
Chris Sosab0f57322011-10-25 03:07:23 +0000154 PRISTINE_IMAGE_NAME=${CHROMEOS_FACTORY_INSTALL_SHIM_NAME}
Chris Sosa93e7b782012-01-03 15:52:12 -0800155else
Chris Sosac9422fa2012-03-05 15:58:07 -0800156 PRISTINE_IMAGE_NAME=${CHROMEOS_BASE_IMAGE_NAME}
Tan Gao843b70a2010-08-17 09:41:48 -0700157fi
158
Will Drewry7ab698d2010-08-05 13:57:52 -0500159DEVKEYSDIR="/usr/share/vboot/devkeys"
160
Bertrand SIMONNET5ba8adf2015-04-16 11:58:52 -0700161"${BOARD_ROOT}/build/bin/eclean" -d packages
Will Drewry12f14ce2010-08-17 17:27:16 -0500162
Ralph Nathan89157bf2015-05-14 14:06:13 -0700163if [[ ${skip_blacklist_check} -ne ${FLAGS_FALSE} ]]; then
Liam McLoughlind606ee22012-07-03 16:48:57 -0700164 check_blacklist
165fi
J. Richard Barnettea308b392011-08-29 13:53:34 -0700166
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700167# Handle existing directory.
Chris Sosab0f57322011-10-25 03:07:23 +0000168if [[ -e "${BUILD_DIR}" ]]; then
Don Garrette0020b12010-06-17 15:55:35 -0700169 if [[ ${FLAGS_replace} -eq ${FLAGS_TRUE} ]]; then
Chris Sosab0f57322011-10-25 03:07:23 +0000170 sudo rm -rf "${BUILD_DIR}"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700171 else
Chris Sosab0f57322011-10-25 03:07:23 +0000172 error "Directory ${BUILD_DIR} already exists."
173 error "Use --build_attempt option to specify an unused attempt."
Brian Harring7f175a52012-03-02 05:37:00 -0800174 error "Or use --replace if you want to overwrite this directory."
175 die "Unwilling to overwrite ${BUILD_DIR}."
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700176 fi
177fi
178
J. Richard Barnettea308b392011-08-29 13:53:34 -0700179# Create the output directory and temporary mount points.
Chris Sosab0f57322011-10-25 03:07:23 +0000180mkdir -p "${BUILD_DIR}"
Don Garrett3f41e152010-06-21 14:54:34 -0700181
Ralph Nathan89157bf2015-05-14 14:06:13 -0700182if [[ ${FLAGS_progress_bar} -eq ${FLAGS_TRUE} ]]; then
Ralph Nathan954641c2015-06-05 16:17:28 -0700183 info 'operation: creating base image'
Ralph Nathana5ba50e2015-04-21 13:41:12 -0700184fi
185
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700186# Create the base image.
Paul Taysom00df6f62012-11-20 12:35:40 -0800187create_base_image ${PRISTINE_IMAGE_NAME} ${FLAGS_enable_rootfs_verification} \
188 ${FLAGS_enable_bootcache}
Tan Gao843b70a2010-08-17 09:41:48 -0700189
Ralph Nathan89157bf2015-05-14 14:06:13 -0700190if [[ ${FLAGS_progress_bar} -eq ${FLAGS_TRUE} ]]; then
Ralph Nathan954641c2015-06-05 16:17:28 -0700191 info 'operation: done creating base image'
Ralph Nathana5ba50e2015-04-21 13:41:12 -0700192fi
193
Liam McLoughlind606ee22012-07-03 16:48:57 -0700194# Running board-specific setup if any exists.
195if type board_setup &>/dev/null; then
196 board_setup "${BUILD_DIR}/${PRISTINE_IMAGE_NAME}"
197fi
198
Ralph Nathan89157bf2015-05-14 14:06:13 -0700199if [[ ${FLAGS_progress_bar} -eq ${FLAGS_TRUE} ]]; then
Ralph Nathan954641c2015-06-05 16:17:28 -0700200 info 'operation: creating developer image'
Ralph Nathana5ba50e2015-04-21 13:41:12 -0700201fi
202
Chris Sosac9422fa2012-03-05 15:58:07 -0800203# Create a developer image if an image that is based on it is requested.
204if should_build_image ${CHROMEOS_DEVELOPER_IMAGE_NAME} \
205 ${CHROMEOS_TEST_IMAGE_NAME} ${CHROMEOS_FACTORY_TEST_IMAGE_NAME}; then
Chris Sosab0f57322011-10-25 03:07:23 +0000206 copy_image ${CHROMEOS_BASE_IMAGE_NAME} ${CHROMEOS_DEVELOPER_IMAGE_NAME}
Chris Sosac9422fa2012-03-05 15:58:07 -0800207 install_dev_packages ${CHROMEOS_DEVELOPER_IMAGE_NAME}
Bill Richardson6ed13582010-06-16 21:38:15 -0700208fi
209
Ralph Nathan89157bf2015-05-14 14:06:13 -0700210if [[ ${FLAGS_progress_bar} -eq ${FLAGS_TRUE} ]]; then
Ralph Nathan954641c2015-06-05 16:17:28 -0700211 info 'operation: done creating developer image'
212 info 'operation: creating test image'
Ralph Nathana5ba50e2015-04-21 13:41:12 -0700213fi
214
Chris Sosac9422fa2012-03-05 15:58:07 -0800215# From a developer image create a test|factory_test image.
216if should_build_image ${CHROMEOS_TEST_IMAGE_NAME}; then
217 copy_image ${CHROMEOS_DEVELOPER_IMAGE_NAME} ${CHROMEOS_TEST_IMAGE_NAME}
218 mod_image_for_test ${CHROMEOS_TEST_IMAGE_NAME}
219fi
220if should_build_image ${CHROMEOS_FACTORY_TEST_IMAGE_NAME}; then
Chris Sosab0f57322011-10-25 03:07:23 +0000221 copy_image ${CHROMEOS_DEVELOPER_IMAGE_NAME} \
222 ${CHROMEOS_FACTORY_TEST_IMAGE_NAME}
223 mod_image_for_test ${CHROMEOS_FACTORY_TEST_IMAGE_NAME}
Vince Laviano6aebf312011-06-01 15:27:31 -0700224fi
225
Ralph Nathan89157bf2015-05-14 14:06:13 -0700226if [[ ${FLAGS_progress_bar} -eq ${FLAGS_TRUE} ]]; then
Ralph Nathan954641c2015-06-05 16:17:28 -0700227 info 'operation: done creating test image'
Ralph Nathana5ba50e2015-04-21 13:41:12 -0700228fi
229
Steve Fung5d7a0ae2015-05-08 15:16:12 -0700230# Move the completed image to the output_root.
231move_image "${BUILD_DIR}" "${OUTPUT_DIR}"
Steve Fung817a1ef2015-05-06 17:23:17 -0700232
David James4dd4c542011-08-10 10:19:47 -0700233# Create a named symlink.
234LINK_NAME="${FLAGS_output_root}/${BOARD}/${FLAGS_symlink}"
Steve Fung817a1ef2015-05-06 17:23:17 -0700235ln -sfT $(basename ${OUTPUT_DIR}) ${LINK_NAME}
Olof Johansson58f25cc2010-08-04 21:18:49 -0700236
Steve Fung1fcece02015-04-06 17:54:50 -0700237WORKSPACE_PREFIX="//"
Steve Fung817a1ef2015-05-06 17:23:17 -0700238OUTPUT_DIR_NAME="${OUTPUT_DIR/"${_WORKSPACE_DIRS[1]}/"/${WORKSPACE_PREFIX}}"
Steve Fung1fcece02015-04-06 17:54:50 -0700239
240info "Done. Image(s) created in ${OUTPUT_DIR_NAME}"
Mike Frysingerb86854a2014-11-25 18:43:58 -0500241echo
Chris Sosac9422fa2012-03-05 15:58:07 -0800242
243# Print out the images we generated.
Mike Frysingerb86854a2014-11-25 18:43:58 -0500244summarize() {
Ralph Nathan89157bf2015-05-14 14:06:13 -0700245
246 if [[ ${FLAGS_progress_bar} -eq ${FLAGS_TRUE} ]]; then
Ralph Nathan954641c2015-06-05 16:17:28 -0700247 info 'operation: summarize'
Ralph Nathan89157bf2015-05-14 14:06:13 -0700248 fi
249
Mike Frysingerb86854a2014-11-25 18:43:58 -0500250 local name="$1" img="$2"
Steve Fung1fcece02015-04-06 17:54:50 -0700251 local dir_path
252
253 if [[ "${FLAGS_output_root}" == "${__flags_output_root_default}" ]]; then
254 dir_path=${OUTSIDE_OUTPUT_DIR}
255 else
Steve Fung817a1ef2015-05-06 17:23:17 -0700256 dir_path="${OUTPUT_DIR}"
Steve Fung1fcece02015-04-06 17:54:50 -0700257 fi
Mike Frysingerb86854a2014-11-25 18:43:58 -0500258
259 info "${name} image created as ${img}"
260 info "To copy the image to a USB key, use:"
Ralph Nathane28ff1b2015-06-05 17:28:59 -0700261 if [[ "${FLAGS_toolset}" == "brillo" ]]; then
262 info " brillo flash usb:// ${OUTPUT_DIR_NAME}/${img}"
263 else
264 info " cros flash usb:// ${dir_path}/${img}"
265 if [[ $# -ge 3 ]]; then
266 info "To convert it to a VM image, use:"
267 info " ./image_to_vm.sh --from=${dir_path} --board=${BOARD} $3"
268 fi
Mike Frysingerb86854a2014-11-25 18:43:58 -0500269 fi
270 echo
Ralph Nathan89157bf2015-05-14 14:06:13 -0700271
272 if [[ ${FLAGS_progress_bar} -eq ${FLAGS_TRUE} ]]; then
Ralph Nathan954641c2015-06-05 16:17:28 -0700273 info 'operation: done summarize'
Ralph Nathan89157bf2015-05-14 14:06:13 -0700274 fi
Mike Frysingerb86854a2014-11-25 18:43:58 -0500275}
Chris Sosac9422fa2012-03-05 15:58:07 -0800276if should_build_image ${CHROMEOS_BASE_IMAGE_NAME}; then
Mike Frysingerb86854a2014-11-25 18:43:58 -0500277 summarize "Non-developer Chromium OS" "${PRISTINE_IMAGE_NAME}"
Chris Sosac9422fa2012-03-05 15:58:07 -0800278fi
279if should_build_image ${CHROMEOS_FACTORY_SHIM_NAME}; then
Mike Frysingerb86854a2014-11-25 18:43:58 -0500280 summarize "Chromium OS Factory install shim" "${PRISTINE_IMAGE_NAME}"
Chris Sosac9422fa2012-03-05 15:58:07 -0800281fi
282if should_build_image ${CHROMEOS_DEVELOPER_IMAGE_NAME}; then
Mike Frysingerb86854a2014-11-25 18:43:58 -0500283 summarize "Developer" "${CHROMEOS_DEVELOPER_IMAGE_NAME}" ""
Chris Sosa9673f3b2010-05-18 13:24:40 -0700284fi
Chris Sosac9422fa2012-03-05 15:58:07 -0800285if should_build_image ${CHROMEOS_FACTORY_TEST_IMAGE_NAME}; then
Mike Frysingerb86854a2014-11-25 18:43:58 -0500286 summarize "Factory test" "${CHROMEOS_FACTORY_TEST_IMAGE_NAME}" "--factory"
Chris Sosac9422fa2012-03-05 15:58:07 -0800287fi
288if should_build_image ${CHROMEOS_TEST_IMAGE_NAME}; then
Mike Frysingerb86854a2014-11-25 18:43:58 -0500289 summarize "Test" "${CHROMEOS_TEST_IMAGE_NAME}" "--test"
Vince Laviano6aebf312011-06-01 15:27:31 -0700290fi
Nick Sanders8ab729a2010-06-16 03:15:17 -0700291
Matt Tennant0a9d32d2012-07-30 16:51:37 -0700292command_completed