blob: ed9600b4ceae78b540da012c22c6afc432363e9f [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
Greg Spencer798d75f2011-02-01 22:04:49 -080015
David James855afb72012-03-14 20:04:59 -070016# Developer-visible flags.
Don Garrette0020b12010-06-17 15:55:35 -070017DEFINE_string board "${DEFAULT_BOARD}" \
Bill Richardsonc09b94f2010-03-15 11:40:30 -070018 "The board to build an image for."
David James855afb72012-03-14 20:04:59 -070019DEFINE_string boot_args "noinitrd" \
20 "Additional boot arguments to pass to the commandline"
21DEFINE_boolean enable_rootfs_verification ${FLAGS_TRUE} \
22 "Default all bootloaders to use kernel-based root fs integrity checking."
Liam McLoughlin8afcdcd2012-07-13 18:20:56 -070023DEFINE_boolean full "${FLAGS_FALSE}" "Build full image with all partitions."
Bill Richardsonc09b94f2010-03-15 11:40:30 -070024DEFINE_string output_root "${DEFAULT_BUILD_ROOT}/images" \
25 "Directory in which to place image result directories (named by version)"
David James855afb72012-03-14 20:04:59 -070026DEFINE_integer rootfs_hash_pad 8 \
27 "MiBs reserved at the end of the rootfs image. \
28 ceil(0.01 * rootfs_size) is a good minimum."
Zelidrag Hornung1d12c1a2010-06-02 10:20:29 -070029DEFINE_integer rootfs_partition_size 1024 \
Will Drewryd253bad2010-08-17 14:09:41 -050030 "rootfs partition size in MiBs."
Andrew de los Reyes0eb6d762010-11-01 18:27:53 -070031DEFINE_integer rootfs_size 850 \
Will Drewryd253bad2010-08-17 14:09:41 -050032 "rootfs filesystem size in MiBs."
Peter Mayo82975f92012-05-01 11:03:31 -040033DEFINE_integer rootfs_boost_size 0 \
34 "MiBs by which to increase the rootfs allocations."
David Rochbergcf932372011-06-30 21:25:08 -040035DEFINE_boolean standard_backdoor ${FLAGS_TRUE} \
36 "Install standard backdoor credentials for testing"
David James855afb72012-03-14 20:04:59 -070037DEFINE_integer statefulfs_size 1024 \
38 "stateful filesystem size in MiBs."
39DEFINE_string usb_disk /dev/sdb3 \
40 "Path syslinux should use to do a usb boot. Default: /dev/sdb3"
Che-Liang Chiou611d5b72011-07-27 16:03:26 +080041
Chris Sosaef4a6452011-10-27 16:01:07 -070042FLAGS_HELP="USAGE: build_image [flags] [list of images to build].
43This script is used to build a Chromium OS image. Chromium OS comes in many
44different forms. This scripts can be used to build the following:
45
46base - Pristine Chromium OS image. As similar to Chrome OS as possible.
47dev - Developer image. Like base but with additional developer packages.
48test - Like dev, but with additional test specific packages and can be easily
49 used for automated testing using scripts like run_remote_tests, etc.
50factory_test - Like test but with extra packages and modifications used to
51 test images in a factory setting. Cannot be built along with a test image.
52factory_install - Install shim for bootstrapping the factory test process.
53 Cannot be built along with any other image.
54
David James855afb72012-03-14 20:04:59 -070055Examples:
Chris Sosaef4a6452011-10-27 16:01:07 -070056
57build_image --board=<board> dev test - builds developer and test images.
58build_image --board=<board> factory_install - builds a factory install shim.
59...
60"
David James855afb72012-03-14 20:04:59 -070061show_help_if_requested "$@"
62
63# The following options are advanced options, only available to those willing
64# to read the source code. They are not shown in help output, since they are
65# not needed for the typical developer workflow.
David James855afb72012-03-14 20:04:59 -070066DEFINE_integer build_attempt 1 \
67 "The build attempt for this image build."
Liam McLoughlin07279032012-06-25 17:31:11 -070068DEFINE_boolean hybrid_mbr ${FLAGS_FALSE} \
69 "Creates a hybrid MBR rather than a protective one"
David James855afb72012-03-14 20:04:59 -070070DEFINE_integer jobs -1 \
71 "How many packages to build in parallel at maximum."
72DEFINE_boolean replace ${FLAGS_FALSE} \
73 "Overwrite existing output, if any."
74DEFINE_string symlink "latest" \
75 "Symlink name to use for this image."
76DEFINE_integer verity_error_behavior 3 \
77 "Kernel verified boot error behavior (0: I/O errors, 1: panic, 2: nothing, \
783: cros) Default: 3"
79DEFINE_integer verity_max_ios -1 \
80 "Number of outstanding I/O operations dm-verity caps at. Default: -1"
81DEFINE_string verity_algorithm "sha1" \
82 "Cryptographic hash algorithm used for kernel vboot. Default : sha1"
83DEFINE_string verity_salt "" \
84 "Root filesystem salt. Default: randomly generated."
Liam McLoughlin07279032012-06-25 17:31:11 -070085DEFINE_string version "" \
86 "Overrides version number in name to this version."
Chris Sosaef4a6452011-10-27 16:01:07 -070087
Bill Richardsonc09b94f2010-03-15 11:40:30 -070088# Parse command line.
89FLAGS "$@" || exit 1
90eval set -- "${FLAGS_ARGV}"
91
92# Only now can we die on error. shflags functions leak non-zero error codes,
Brian Harring7f175a52012-03-02 05:37:00 -080093# so will die prematurely if 'switch_to_strict_mode' is specified before now.
94switch_to_strict_mode
Bill Richardsonc09b94f2010-03-15 11:40:30 -070095
J. Richard Barnettee4e3dec2011-08-08 16:35:02 -070096# Determine build version.
97OVERLAY_CHROMEOS_DIR="${SRC_ROOT}/third_party/chromiumos-overlay/chromeos"
98. "${OVERLAY_CHROMEOS_DIR}/config/chromeos_version.sh" || exit 1
J. Richard Barnettea308b392011-08-29 13:53:34 -070099# N.B. Ordering matters for some of the libraries below, because
100# some of the files contain initialization used by later files.
J. Richard Barnetteaaef7612011-08-11 17:10:28 -0700101. "${BUILD_LIBRARY_DIR}/board_options.sh" || exit 1
102. "${BUILD_LIBRARY_DIR}/build_gpt.sh" || exit 1
J. Richard Barnette5f9dbe42011-08-22 14:31:33 -0700103. "${BUILD_LIBRARY_DIR}/mount_gpt_util.sh" || exit 1
J. Richard Barnettea308b392011-08-29 13:53:34 -0700104. "${BUILD_LIBRARY_DIR}/build_image_util.sh" || exit 1
105. "${BUILD_LIBRARY_DIR}/base_image_util.sh" || exit 1
106. "${BUILD_LIBRARY_DIR}/dev_image_util.sh" || exit 1
107. "${BUILD_LIBRARY_DIR}/test_image_util.sh" || exit 1
J. Richard Barnetteaaef7612011-08-11 17:10:28 -0700108. "${BUILD_LIBRARY_DIR}/test_image_content.sh" || exit 1
J. Richard Barnettee4e3dec2011-08-08 16:35:02 -0700109
Chris Sosab0f57322011-10-25 03:07:23 +0000110parse_build_image_args
Peter Mayo82975f92012-05-01 11:03:31 -0400111
Liam McLoughlind606ee22012-07-03 16:48:57 -0700112for overlay in $(cros_overlay_list --board "$BOARD"); do
113 setup_sh="${overlay}/scripts/board_specific_setup.sh"
114 if [[ -e ${setup_sh} ]]; then
115 source "${setup_sh}"
116 fi
117done
118
Peter Mayo82975f92012-05-01 11:03:31 -0400119# Tweak sizes defaulted or given based on any boost given (or defaulted).
120# This is syntactic sugar to separate concerns in callers, and unify
121# default values here.
122if [[ $FLAGS_rootfs_boost_size -gt 0 ]]; then
123 max() {
124 echo $(( $1 > $2 ? $1 : $2 ))
125 }
126 root_boost() {
127 echo $(( $1 + ( $1 * $FLAGS_rootfs_boost_size + $FLAGS_rootfs_size - 1 ) \
128 / $FLAGS_rootfs_size ))
129 }
130 FLAGS_rootfs_hash_pad=$(root_boost $FLAGS_rootfs_hash_pad)
131 FLAGS_rootfs_partition_size=$(root_boost $FLAGS_rootfs_partition_size)
132 FLAGS_rootfs_size=$(root_boost $FLAGS_rootfs_size)
133 FLAGS_rootfs_boost_size=0
134 # Push up to the recommended minimum if we did not get there.
135 FLAGS_rootfs_hash_pad=$(max $FLAGS_rootfs_hash_pad \
136 $(( ( $FLAGS_rootfs_size + 99 ) / 100 )))
137fi
138
Chris Sosa4299eb92011-11-01 12:54:01 -0700139# Tweak flags, configure extra USE flags, and add packages for the factory
140# install shim.
141EXTRA_PACKAGES=""
Chris Sosac9422fa2012-03-05 15:58:07 -0800142if should_build_image ${CHROMEOS_FACTORY_INSTALL_SHIM_NAME}; then
Nick Sandersae26a5c2010-08-30 21:59:50 -0700143 # TODO: Build a separated ebuild for the install shim to reduce size.
Hung-Te Lind32c59f2012-01-19 19:54:01 +0800144 INSTALL_MASK="${FACTORY_SHIM_INSTALL_MASK}"
Nick Sandersae26a5c2010-08-30 21:59:50 -0700145
Jon Salz67145df2011-10-20 14:05:06 +0800146 # Reduce the size of factory install shim.
Anush Elangovand39f1de2011-11-14 12:28:30 -0800147 info "Fixing the rootfs size at 320 MiB for install shim"
148 FLAGS_rootfs_size=300
149 FLAGS_rootfs_partition_size=320
Nick Sandersae26a5c2010-08-30 21:59:50 -0700150 info "Fixing the statefulfs size at 140 MiB for install shim"
151 FLAGS_statefulfs_size=140
Jon Salz67145df2011-10-20 14:05:06 +0800152
153 # Add the cros_factory_install boot arg.
154 FLAGS_boot_args="${FLAGS_boot_args} cros_factory_install"
Chris Sosa4299eb92011-11-01 12:54:01 -0700155
156 # Factory install needs to have the factory installer added.
157 EXTRA_PACKAGES="${EXTRA_PACKAGES} chromeos-base/chromeos-factoryinstall"
Hung-Te Lin7119cb12012-03-07 22:29:13 +0800158 # On x86/amd64, we boot the factory install shim from an SD card using
159 # initramfs for our root. On ARM, we boot the factory install shim over the
160 # network, so we don't require initramfs, but we do require fbconsole to fix
161 # a display driver bug.
162 if [ "${ARCH}" = "x86" -o "${ARCH}" = "amd64" ] ; then
Vic Yang40514052012-05-22 14:27:40 +0800163 export USE="${USE} initramfs vfat"
Chris Sosa4299eb92011-11-01 12:54:01 -0700164 fi
165 # CONFIG_BLK_DEV_RAM is disabled by default.
166 # But tftp install needs it to mount rootfs in ram
167 if [ "${ARCH}" = "arm" ] ; then
168 export USE="${USE} fbconsole blkdevram"
169 fi
Nick Sandersae26a5c2010-08-30 21:59:50 -0700170fi
171
J. Richard Barnetteeb5246f2011-04-08 10:34:31 -0700172if [ $((FLAGS_rootfs_size + FLAGS_rootfs_hash_pad)) -gt \
173 ${FLAGS_rootfs_partition_size} ] ; then
Brian Harring7f175a52012-03-02 05:37:00 -0800174 die_notrace "rootfs ($((FLAGS_rootfs_size + FLAGS_rootfs_hash_pad)) MiB) is" \
Chris Sosa131eaf52011-10-27 16:15:53 -0700175 "bigger than partition (${FLAGS_rootfs_partition_size} MiB)."
Tan Gao843b70a2010-08-17 09:41:48 -0700176fi
177
Chris Sosa9673f3b2010-05-18 13:24:40 -0700178# If we are creating a developer image, also create a pristine image with a
179# different name.
Chris Sosa93e7b782012-01-03 15:52:12 -0800180PRISTINE_IMAGE_NAME=
Chris Sosac9422fa2012-03-05 15:58:07 -0800181if should_build_image ${CHROMEOS_FACTORY_INSTALL_SHIM_NAME}; then
Chris Sosab0f57322011-10-25 03:07:23 +0000182 PRISTINE_IMAGE_NAME=${CHROMEOS_FACTORY_INSTALL_SHIM_NAME}
Chris Sosa93e7b782012-01-03 15:52:12 -0800183else
Chris Sosac9422fa2012-03-05 15:58:07 -0800184 PRISTINE_IMAGE_NAME=${CHROMEOS_BASE_IMAGE_NAME}
Tan Gao843b70a2010-08-17 09:41:48 -0700185fi
186
Chris Sosab0f57322011-10-25 03:07:23 +0000187ROOT_FS_DIR="${BUILD_DIR}/rootfs"
188STATEFUL_FS_DIR="${BUILD_DIR}/stateful_partition"
189ESP_FS_DIR=${BUILD_DIR}/esp
Don Garrett3f41e152010-06-21 14:54:34 -0700190
Will Drewry7ab698d2010-08-05 13:57:52 -0500191DEVKEYSDIR="/usr/share/vboot/devkeys"
192
Don Garrett3f41e152010-06-21 14:54:34 -0700193# ${DEV_IMAGE_ROOT} specifies the location of where developer packages will
194# be installed on the stateful dir. On a Chromium OS system, this will
195# translate to /usr/local.
196DEV_IMAGE_ROOT="${STATEFUL_FS_DIR}/dev_image"
197
J. Richard Barnette8e480952011-08-30 16:00:59 -0700198eclean-$BOARD -d packages
Will Drewry12f14ce2010-08-17 17:27:16 -0500199
Liam McLoughlind606ee22012-07-03 16:48:57 -0700200if [[ ${skip_blacklist_check} -ne 1 ]]; then
201 check_blacklist
202fi
J. Richard Barnettea308b392011-08-29 13:53:34 -0700203
204# Check that the build root is sane.
Liam McLoughlind606ee22012-07-03 16:48:57 -0700205if [[ ${skip_test_build_root} -ne 1 ]]; then
206 "${BUILD_LIBRARY_DIR}/test_build_root" --root="${BOARD_ROOT}"
207fi
J. Richard Barnettea308b392011-08-29 13:53:34 -0700208
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700209# Hack to fix bug where x86_64 CHOST line gets incorrectly added.
210# ToDo(msb): remove this hack.
211PACKAGES_FILE="${BOARD_ROOT}/packages/Packages"
212sudo sed -e "s/CHOST: x86_64-pc-linux-gnu//" -i "${PACKAGES_FILE}"
213
214# Handle existing directory.
Chris Sosab0f57322011-10-25 03:07:23 +0000215if [[ -e "${BUILD_DIR}" ]]; then
Don Garrette0020b12010-06-17 15:55:35 -0700216 if [[ ${FLAGS_replace} -eq ${FLAGS_TRUE} ]]; then
Chris Sosab0f57322011-10-25 03:07:23 +0000217 sudo rm -rf "${BUILD_DIR}"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700218 else
Chris Sosab0f57322011-10-25 03:07:23 +0000219 error "Directory ${BUILD_DIR} already exists."
220 error "Use --build_attempt option to specify an unused attempt."
Brian Harring7f175a52012-03-02 05:37:00 -0800221 error "Or use --replace if you want to overwrite this directory."
222 die "Unwilling to overwrite ${BUILD_DIR}."
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700223 fi
224fi
225
J. Richard Barnettea308b392011-08-29 13:53:34 -0700226# Create the output directory and temporary mount points.
Chris Sosab0f57322011-10-25 03:07:23 +0000227mkdir -p "${BUILD_DIR}"
J. Richard Barnettea308b392011-08-29 13:53:34 -0700228mkdir -p "${ROOT_FS_DIR}" "${STATEFUL_FS_DIR}" "${ESP_FS_DIR}"
Don Garrett3f41e152010-06-21 14:54:34 -0700229
Will Drewry7ab698d2010-08-05 13:57:52 -0500230# Create the boot.desc file which stores the build-time configuration
231# information needed for making the image bootable after creation with
232# cros_make_image_bootable.
233create_boot_desc
234
Chris Sosab0f57322011-10-25 03:07:23 +0000235create_base_image ${PRISTINE_IMAGE_NAME}
Tan Gao843b70a2010-08-17 09:41:48 -0700236
Will Drewry4a675e12010-07-03 13:35:02 -0500237
Che-Liang Chioue51bdf22011-07-26 21:19:24 +0800238BOOT_FLAG=
Chris Sosac9422fa2012-03-05 15:58:07 -0800239if should_build_image ${CHROMEOS_FACTORY_INSTALL_SHIM_NAME}; then
Che-Liang Chioue51bdf22011-07-26 21:19:24 +0800240 BOOT_FLAG="-b 1" # BOOT_FLAG_DEVELOPER value defined in load_kernel_fw.h
Chris Sosa131eaf52011-10-27 16:15:53 -0700241 info "--factory_install set, pass BOOT_FLAG_DEVELOPER flag to" \
242 "load_kernel_test"
Che-Liang Chiou451f36e2011-07-21 19:53:37 +0800243fi
Che-Liang Chiou2d2e8252011-06-18 17:38:59 -0700244
Chris Sosab0f57322011-10-25 03:07:23 +0000245# Verify pristine image if we built it.
246if should_build_image "${PRISTINE_IMAGE_NAME}"; then
247 load_kernel_test "${BUILD_DIR}/${PRISTINE_IMAGE_NAME}" \
248 "${DEVKEYSDIR}/recovery_key.vbpubk" ${BOOT_FLAG}
249fi
Che-Liang Chioue51bdf22011-07-26 21:19:24 +0800250
Liam McLoughlind606ee22012-07-03 16:48:57 -0700251# Running board-specific setup if any exists.
252if type board_setup &>/dev/null; then
253 board_setup "${BUILD_DIR}/${PRISTINE_IMAGE_NAME}"
254fi
255
Chris Sosac9422fa2012-03-05 15:58:07 -0800256# Create a developer image if an image that is based on it is requested.
257if should_build_image ${CHROMEOS_DEVELOPER_IMAGE_NAME} \
258 ${CHROMEOS_TEST_IMAGE_NAME} ${CHROMEOS_FACTORY_TEST_IMAGE_NAME}; then
Chris Sosab0f57322011-10-25 03:07:23 +0000259 copy_image ${CHROMEOS_BASE_IMAGE_NAME} ${CHROMEOS_DEVELOPER_IMAGE_NAME}
Chris Sosac9422fa2012-03-05 15:58:07 -0800260 install_dev_packages ${CHROMEOS_DEVELOPER_IMAGE_NAME}
Bill Richardson6ed13582010-06-16 21:38:15 -0700261fi
262
Chris Sosac9422fa2012-03-05 15:58:07 -0800263# From a developer image create a test|factory_test image.
264if should_build_image ${CHROMEOS_TEST_IMAGE_NAME}; then
265 copy_image ${CHROMEOS_DEVELOPER_IMAGE_NAME} ${CHROMEOS_TEST_IMAGE_NAME}
266 mod_image_for_test ${CHROMEOS_TEST_IMAGE_NAME}
267fi
268if should_build_image ${CHROMEOS_FACTORY_TEST_IMAGE_NAME}; then
Chris Sosab0f57322011-10-25 03:07:23 +0000269 copy_image ${CHROMEOS_DEVELOPER_IMAGE_NAME} \
270 ${CHROMEOS_FACTORY_TEST_IMAGE_NAME}
271 mod_image_for_test ${CHROMEOS_FACTORY_TEST_IMAGE_NAME}
Vince Laviano6aebf312011-06-01 15:27:31 -0700272fi
273
Dmitry Polukhin0a649b52011-02-24 13:02:52 +0300274rmdir "${ROOT_FS_DIR}" "${STATEFUL_FS_DIR}" "${ESP_FS_DIR}"
Bill Richardson67956222010-05-28 15:38:56 -0700275
Raja Aluriaeb10482010-12-10 13:03:09 -0800276# Generating AU generator zip file to run outside chroot
277generate_au_zip || echo "Failed generating AU zip file - ignoring Error..."
David James4dd4c542011-08-10 10:19:47 -0700278
279# Create a named symlink.
280LINK_NAME="${FLAGS_output_root}/${BOARD}/${FLAGS_symlink}"
Chris Sosab0f57322011-10-25 03:07:23 +0000281ln -sfT $(basename ${BUILD_DIR}) ${LINK_NAME}
Olof Johansson58f25cc2010-08-04 21:18:49 -0700282
Chris Sosac9422fa2012-03-05 15:58:07 -0800283echo "Done. Image(s) created in ${BUILD_DIR}"
284
285# Print out the images we generated.
286if should_build_image ${CHROMEOS_BASE_IMAGE_NAME}; then
287 echo "Non-developer Chromium OS image created as ${PRISTINE_IMAGE_NAME}"
288fi
289if should_build_image ${CHROMEOS_FACTORY_SHIM_NAME}; then
290 echo "Chromium OS Factory install shim created as ${PRISTINE_IMAGE_NAME}"
291fi
292if should_build_image ${CHROMEOS_DEVELOPER_IMAGE_NAME}; then
Chris Sosab0f57322011-10-25 03:07:23 +0000293 echo "Developer image created as ${CHROMEOS_DEVELOPER_IMAGE_NAME}"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700294fi
Chris Sosac9422fa2012-03-05 15:58:07 -0800295if should_build_image ${CHROMEOS_FACTORY_TEST_IMAGE_NAME}; then
Vince Laviano6aebf312011-06-01 15:27:31 -0700296 echo "Factory test image created as ${CHROMEOS_FACTORY_TEST_IMAGE_NAME}"
Chris Sosac9422fa2012-03-05 15:58:07 -0800297fi
298if should_build_image ${CHROMEOS_TEST_IMAGE_NAME}; then
Vince Laviano6aebf312011-06-01 15:27:31 -0700299 echo "Test image created as ${CHROMEOS_TEST_IMAGE_NAME}"
300fi
Nick Sanders8ab729a2010-06-16 03:15:17 -0700301
Matt Tennant0a9d32d2012-07-30 16:51:37 -0700302command_completed
Nick Sandersd2509272010-06-16 03:50:04 -0700303
Gilad Arnolda7536ef2012-06-20 18:43:39 -0700304cat << EOF
305To copy the image to a USB key, use:
306 ./image_to_usb.sh --from=${OUTSIDE_OUTPUT_DIR}
307To convert it to a VMWare image, use:
308 ./image_to_vm.sh --from=${OUTSIDE_OUTPUT_DIR} --board=${BOARD}
309EOF