blob: 9c9e70dbc977d33377c233808a02b26fccd02c96 [file] [log] [blame]
Bill Richardsonc09b94f2010-03-15 11:40:30 -07001#!/bin/bash
2
Darin Petkov61173f62011-04-29 10:24:26 -07003# Copyright (c) 2011 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
Greg Spencer798d75f2011-02-01 22:04:49 -080012# Load common CrOS utilities. Inside the chroot this file is installed in
13# /usr/lib/crosutils. Outside the chroot we find it relative to the script's
14# location.
15find_common_sh() {
David James17d44e32011-02-08 14:48:51 -080016 local common_paths=(/usr/lib/crosutils $(dirname "$0"))
Greg Spencer798d75f2011-02-01 22:04:49 -080017 local path
Bill Richardsonc09b94f2010-03-15 11:40:30 -070018
Greg Spencer798d75f2011-02-01 22:04:49 -080019 SCRIPT_ROOT=
20 for path in "${common_paths[@]}"; do
David James17d44e32011-02-08 14:48:51 -080021 local common="${path}/common.sh"
22 if ([ -r "${common}" ] && . "${common}" && [ -d "${SCRIPTS_DIR}" ]); then
Greg Spencer798d75f2011-02-01 22:04:49 -080023 SCRIPT_ROOT=${path}
24 break
25 fi
26 done
27}
28
29find_common_sh
David James17d44e32011-02-08 14:48:51 -080030. "${SCRIPT_ROOT}/common.sh" || ! echo "Unable to load common.sh" || exit 1
Greg Spencer798d75f2011-02-01 22:04:49 -080031
32# Load functions and constants for chromeos-install
33[ -f /usr/lib/installer/chromeos-common.sh ] && \
34 INSTALLER_ROOT=/usr/lib/installer || \
35 INSTALLER_ROOT=$(dirname "$(readlink -f "$0")")
36
37. "${INSTALLER_ROOT}/chromeos-common.sh" || \
38 die "Unable to load chromeos-common.sh"
39
Will Drewry4a675e12010-07-03 13:35:02 -050040locate_gpt
41
Bill Richardsonc09b94f2010-03-15 11:40:30 -070042# Script must be run inside the chroot.
Greg Spencer798d75f2011-02-01 22:04:49 -080043restart_in_chroot_if_needed "$@"
Bill Richardsonc09b94f2010-03-15 11:40:30 -070044
45get_default_board
46
47# Flags.
Don Garrette0020b12010-06-17 15:55:35 -070048DEFINE_string board "${DEFAULT_BOARD}" \
Bill Richardsonc09b94f2010-03-15 11:40:30 -070049 "The board to build an image for."
50DEFINE_string build_root "/build" \
51 "The root location for board sysroots."
52DEFINE_integer build_attempt 1 \
53 "The build attempt for this image build."
54DEFINE_string output_root "${DEFAULT_BUILD_ROOT}/images" \
55 "Directory in which to place image result directories (named by version)"
David Jamesffede3b2010-09-07 14:22:38 -070056DEFINE_boolean eclean ${FLAGS_TRUE} \
57 "Perform eclean-<board> -d as part of this script to remove obsolete packages"
Don Garrette0020b12010-06-17 15:55:35 -070058DEFINE_boolean replace ${FLAGS_FALSE} \
Bill Richardsonc09b94f2010-03-15 11:40:30 -070059 "Overwrite existing output, if any."
Don Garrette0020b12010-06-17 15:55:35 -070060DEFINE_boolean withdev ${FLAGS_TRUE} \
Bill Richardsonc09b94f2010-03-15 11:40:30 -070061 "Include useful developer friendly utilities in the image."
Don Garrette0020b12010-06-17 15:55:35 -070062DEFINE_boolean installmask ${FLAGS_TRUE} \
Bill Richardsonc09b94f2010-03-15 11:40:30 -070063 "Use INSTALL_MASK to shrink the resulting image."
64DEFINE_integer jobs -1 \
65 "How many packages to build in parallel at maximum."
Don Garrette0020b12010-06-17 15:55:35 -070066DEFINE_boolean statefuldev ${FLAGS_TRUE} \
Chris Sosa4bffb8b2010-04-07 17:23:54 -070067 "Install development packages on stateful partition rather than the rootfs"
Antoine Laboure9e585f2010-04-01 15:57:57 -070068DEFINE_string to "" \
69 "The target image file or device"
Vince Laviano6aebf312011-06-01 15:27:31 -070070DEFINE_boolean test ${FLAGS_FALSE} \
71 "Modify the imnage for automated testing"
72DEFINE_boolean factory ${FLAGS_FALSE} \
73 "Modify the image for manufacturing testing"
74DEFINE_string qualdb "" \
75 "Location of qualified component file"
Don Garrette0020b12010-06-17 15:55:35 -070076DEFINE_boolean factory_install ${FLAGS_FALSE} \
Tom Wai-Hong Tamf87a3672010-05-17 16:06:33 +080077 "Build a smaller image to overlay the factory install shim on; this argument \
78is also required in image_to_usb."
Zelidrag Hornung1d12c1a2010-06-02 10:20:29 -070079DEFINE_integer rootfs_partition_size 1024 \
Will Drewryd253bad2010-08-17 14:09:41 -050080 "rootfs partition size in MiBs."
Andrew de los Reyes0eb6d762010-11-01 18:27:53 -070081DEFINE_integer rootfs_size 850 \
Will Drewryd253bad2010-08-17 14:09:41 -050082 "rootfs filesystem size in MiBs."
Will Drewry78992a32010-07-21 14:02:20 -050083# ceil(0.1 * rootfs_size) is a good minimum.
84DEFINE_integer rootfs_hash_pad 8 \
Will Drewryd253bad2010-08-17 14:09:41 -050085 "MiBs reserved at the end of the rootfs image."
Eric Li5cf288f2010-06-28 12:46:26 -070086DEFINE_integer statefulfs_size 1024 \
Will Drewryd253bad2010-08-17 14:09:41 -050087 "stateful filesystem size in MiBs."
David James868d7bb2010-06-24 21:48:14 -070088DEFINE_boolean preserve ${FLAGS_FALSE} \
89 "Attempt to preserve the previous build image if one can be found (unstable, \
90kernel/firmware not updated)"
David James03668642010-07-28 17:08:29 -070091DEFINE_boolean fast ${DEFAULT_FAST} \
92 "Call many emerges in parallel"
Will Drewry32d8c112010-10-11 15:37:17 -050093DEFINE_string boot_args "noinitrd" \
94 "Additional boot arguments to pass to the commandline"
Nick Sandersf2dee6c2010-07-01 00:21:32 -070095
Will Drewry4a675e12010-07-03 13:35:02 -050096DEFINE_string usb_disk /dev/sdb3 \
97 "Path syslinux should use to do a usb boot. Default: /dev/sdb3"
98
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +080099# TODO(clchiou): Remove this flag after arm verified boot is stable
Nick Sandersb795deb2011-05-20 17:11:53 -0700100DEFINE_boolean crosbug12352_arm_kernel_signing ${FLAGS_TRUE} \
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800101 "Sign kernel partition for ARM images (temporary hack)."
102
Will Drewry12f14ce2010-08-17 17:27:16 -0500103DEFINE_boolean enable_rootfs_verification ${FLAGS_TRUE} \
Will Drewry1670d482010-07-09 13:08:38 -0700104 "Default all bootloaders to use kernel-based root fs integrity checking."
Will Drewryd07f61d2011-01-12 10:39:54 -0600105DEFINE_integer verity_error_behavior 3 \
Will Drewry137eab62010-11-04 22:06:41 -0500106 "Kernel verified boot error behavior (0: I/O errors, 1: panic, 2: nothing, \
Will Drewryd07f61d2011-01-12 10:39:54 -06001073: cros) Default: 3"
Will Drewry52c40f82010-10-06 14:54:38 -0500108DEFINE_integer verity_max_ios -1 \
109 "Number of outstanding I/O operations dm-verity caps at. Default: -1"
Will Drewry1670d482010-07-09 13:08:38 -0700110DEFINE_string verity_algorithm "sha1" \
111 "Cryptographic hash algorithm used for kernel vboot. Default : sha1"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700112
113# Parse command line.
114FLAGS "$@" || exit 1
115eval set -- "${FLAGS_ARGV}"
116
117# Only now can we die on error. shflags functions leak non-zero error codes,
118# so will die prematurely if 'set -e' is specified before now.
119set -e
120
Don Garrette0020b12010-06-17 15:55:35 -0700121if [ -z "${FLAGS_board}" ] ; then
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700122 error "--board is required."
123 exit 1
124fi
125
David James35269d22011-05-19 17:59:58 -0700126# Perform an eclean to remove packages which are not installed
127if [[ ${FLAGS_eclean} -eq ${FLAGS_TRUE} ]]; then
128 eclean-${FLAGS_board} -d packages
129fi
130
Chris Sosae38f90f2010-10-20 16:04:23 -0700131check_blacklist() {
132 info "Verifying that the base image does not contain a blacklisted package."
133 info "Generating list of packages for chromeos-base/chromeos."
134 local package_blacklist_file="${SCRIPTS_DIR}/chromeos_blacklist"
135 if [ ! -e "${package_blacklist_file}" ]; then
136 warn "Missing blacklist file."
137 return
138 fi
139 local blacklisted_packages=$(${SCRIPTS_DIR}/get_package_list \
140 --board="${FLAGS_board}" chromeos-base/chromeos \
141 | grep -x -f "${package_blacklist_file}")
142 if [ -n "${blacklisted_packages}" ]; then
143 die "Blacklisted packages found: ${blacklisted_packages}."
144 fi
145 info "No blacklisted packages found."
146}
147
148check_blacklist
149
Vince Laviano6aebf312011-06-01 15:27:31 -0700150# Look at flags to determine which image types we should build
J. Richard Barnetteeb5246f2011-04-08 10:34:31 -0700151if [ ${FLAGS_factory_install} -eq ${FLAGS_TRUE} ]; then
Vince Laviano6aebf312011-06-01 15:27:31 -0700152 if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ]; then
153 info "Incompatible flags: --factory and --factory_install cannot both be \
154set to True. Resetting --factory to False."
155 FLAGS_factory=${FLAGS_FALSE}
156 fi
157 if [ ${FLAGS_test} -eq ${FLAGS_TRUE} ]; then
158 info "Incompatible flags: --test and --factory_install cannot both be \
159set to True. Resetting --test to False."
160 FLAGS_test=${FLAGS_FALSE}
161 fi
Vince Laviano96d116d2011-03-17 11:32:07 -0700162 # Disable --withdev flag when --factory_install is set to True. Otherwise, the
Nick Sandersae26a5c2010-08-30 21:59:50 -0700163 # dev image produced will be based on install shim, rather than a pristine
164 # image
J. Richard Barnetteeb5246f2011-04-08 10:34:31 -0700165 if [ ${FLAGS_withdev} -eq ${FLAGS_TRUE} ]; then
Vince Laviano96d116d2011-03-17 11:32:07 -0700166 info "Incompatible flags: --withdev and --factory_install cannot both be \
167set to True. Resetting --withdev to False."
Nick Sandersae26a5c2010-08-30 21:59:50 -0700168 FLAGS_withdev=${FLAGS_FALSE}
169 fi
Vince Laviano6aebf312011-06-01 15:27:31 -0700170fi
171if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ]; then
172 if [ ${FLAGS_test} -eq ${FLAGS_FALSE} ]; then
173 info "Incompatible flags: --factory implies --test. Resetting --test to \
174True."
175 FLAGS_test=${FLAGS_TRUE}
176 fi
177fi
178if [ ${FLAGS_test} -eq ${FLAGS_TRUE} ]; then
179 if [ ${FLAGS_withdev} -eq ${FLAGS_FALSE} ]; then
180 info "Incompatible flags: --test implies --withdev. Resetting --withdev \
181to True."
182 FLAGS_withdev=${FLAGS_TRUE}
183 fi
184fi
Nick Sandersae26a5c2010-08-30 21:59:50 -0700185
Vince Laviano6aebf312011-06-01 15:27:31 -0700186export INSTALL_MASK=""
187if [ ${FLAGS_installmask} -eq ${FLAGS_TRUE} ] ; then
188 INSTALL_MASK="${DEFAULT_INSTALL_MASK}"
189fi
190
191# Reduce the size of factory install shim.
192if [ ${FLAGS_factory_install} -eq ${FLAGS_TRUE} ]; then
Nick Sandersae26a5c2010-08-30 21:59:50 -0700193 # TODO: Build a separated ebuild for the install shim to reduce size.
194 INSTALL_MASK="${INSTALL_MASK} ${FACTORY_INSTALL_MASK}"
195
196 info "Fixing the rootfs size at 300 MiB for install shim"
197 FLAGS_rootfs_size=280
198 FLAGS_rootfs_partition_size=300
199 info "Fixing the statefulfs size at 140 MiB for install shim"
200 FLAGS_statefulfs_size=140
201fi
202
J. Richard Barnetteeb5246f2011-04-08 10:34:31 -0700203if [ $((FLAGS_rootfs_size + FLAGS_rootfs_hash_pad)) -gt \
204 ${FLAGS_rootfs_partition_size} ] ; then
Nick Sandersae26a5c2010-08-30 21:59:50 -0700205 die "rootfs ($((FLAGS_rootfs_size + FLAGS_rootfs_hash_pad)) MiB) is \
206bigger than partition (${FLAGS_rootfs_partition_size} MiB)."
Tan Gao843b70a2010-08-17 09:41:48 -0700207fi
208
Nick Sanders8ab729a2010-06-16 03:15:17 -0700209EMERGE_BOARD_CMD="emerge-${FLAGS_board}"
J. Richard Barnetteeb5246f2011-04-08 10:34:31 -0700210if [ ${FLAGS_fast} -eq ${FLAGS_TRUE} ]; then
Nick Sanders8ab729a2010-06-16 03:15:17 -0700211 echo "Using alternate emerge"
David James1ed7fe62011-02-14 14:49:25 -0800212 EMERGE_CMD="${GCLIENT_ROOT}/chromite/bin/parallel_emerge"
213 EMERGE_BOARD_CMD="${EMERGE_CMD} --board=${FLAGS_board}"
Nick Sanders8ab729a2010-06-16 03:15:17 -0700214fi
215
Darin Petkova5869302011-03-01 17:15:59 -0800216OVERLAY_CHROMEOS_DIR="${SRC_ROOT}/third_party/chromiumos-overlay/chromeos/"
217
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700218# Determine build version.
Darin Petkova5869302011-03-01 17:15:59 -0800219. "${OVERLAY_CHROMEOS_DIR}/config/chromeos_version.sh"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700220
J. Richard Barnetteb2903662011-04-28 17:14:34 -0700221BOARD="${FLAGS_board}"
222BOARD_ROOT="${FLAGS_build_root}/${BOARD}"
223
224# What cross-build are we targeting?
225. "${BOARD_ROOT}/etc/make.conf.board_setup"
226LIBC_VERSION=${LIBC_VERSION}
227
228# Figure out ARCH from the given toolchain.
229# TODO: Move to common.sh as a function after scripts are switched over.
230TC_ARCH=$(echo "${CHOST}" | awk -F'-' '{ print $1 }')
231case "${TC_ARCH}" in
232 arm*)
233 ARCH="arm"
234 ;;
235 *86)
236 ARCH="x86"
237 ;;
Stéphane Marchesin2d331402011-05-17 20:19:30 -0700238 *x86_64)
239 ARCH="amd64"
240 ;;
J. Richard Barnetteb2903662011-04-28 17:14:34 -0700241 *)
242 error "Unable to determine ARCH from toolchain: ${CHOST}"
243 exit 1
244esac
245
Nick Sandersc8cb3362010-09-01 21:31:30 -0700246# Configure extra USE or packages for this type of build.
247EXTRA_PACKAGES=""
J. Richard Barnetteeb5246f2011-04-08 10:34:31 -0700248if [ ${FLAGS_factory_install} -eq ${FLAGS_TRUE} ] ; then
Tom Wai-Hong Tama6af2012011-04-28 12:12:08 +0800249 # Factory install needs to have the factory installer added.
Nick Sandersebce8542010-09-02 14:57:34 -0700250 EXTRA_PACKAGES="${EXTRA_PACKAGES} chromeos-base/chromeos-factoryinstall"
J. Richard Barnetteb2903662011-04-28 17:14:34 -0700251 # On x86, we boot the factory install shim from an SD card using
252 # initramfs for our root. On ARM, we boot the factory install shim
253 # over the network, so we don't require initramfs, but we do require
254 # fbconsole to fix a display driver bug.
255 if [ "${ARCH}" = "x86" ] ; then
Tom Wai-Hong Tama6af2012011-04-28 12:12:08 +0800256 export USE="${USE} initramfs"
257 fi
Rong Changc291a482011-05-30 14:56:48 +0800258 # CONFIG_BLK_DEV_RAM is disabled by default.
259 # But tftp install needs it to mount rootfs in ram
J. Richard Barnetteb2903662011-04-28 17:14:34 -0700260 if [ "${ARCH}" = "arm" ] ; then
Rong Changc291a482011-05-30 14:56:48 +0800261 export USE="${USE} fbconsole blkdevram"
J. Richard Barnetteb2903662011-04-28 17:14:34 -0700262 fi
Nick Sandersc8cb3362010-09-01 21:31:30 -0700263fi
264
J. Richard Barnetteeb5246f2011-04-08 10:34:31 -0700265emerge_to_image() {
266 sudo -E ${EMERGE_BOARD_CMD} --root-deps=rdeps --usepkgonly \
267 "$@" ${EMERGE_JOBS}
268}
269
David Jamesf86703d2011-05-10 13:54:04 -0700270# Check that the build root is sane.
271"${SCRIPTS_DIR}/test_build_root" --root="${BOARD_ROOT}"
272
Nick Sandersc8cb3362010-09-01 21:31:30 -0700273# Freshen kernel with correct USE flags. This is a noop if we have
274# the right kernel prebuilt. Factory install uses USE="initramfs".
275# We don't allow building from source with the image as a target,
276# and it's not possible to store prebuilts for the same package
277# with different use flags.
J. Richard Barnetteeb5246f2011-04-08 10:34:31 -0700278sudo -E ${EMERGE_BOARD_CMD} -uDNv -g virtual/kernel
Nick Sandersc8cb3362010-09-01 21:31:30 -0700279
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700280# Use canonical path since some tools (e.g. mount) do not like symlinks.
281# Append build attempt to output directory.
282IMAGE_SUBDIR="${CHROMEOS_VERSION_STRING}-a${FLAGS_build_attempt}"
283OUTPUT_DIR="${FLAGS_output_root}/${FLAGS_board}/${IMAGE_SUBDIR}"
Don Garrett3f41e152010-06-21 14:54:34 -0700284
285OUTSIDE_OUTPUT_DIR="../build/images/${FLAGS_board}/${IMAGE_SUBDIR}"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700286
287# If we are creating a developer image, also create a pristine image with a
288# different name.
Vince Laviano6aebf312011-06-01 15:27:31 -0700289# TODO(vlaviano): fix all image names to match those in uploaded archive
Chris Sosa9673f3b2010-05-18 13:24:40 -0700290DEVELOPER_IMAGE_NAME=
291PRISTINE_IMAGE_NAME=chromiumos_image.bin
J. Richard Barnetteeb5246f2011-04-08 10:34:31 -0700292if [ ${FLAGS_withdev} -eq ${FLAGS_TRUE} ]; then
Chris Sosa9673f3b2010-05-18 13:24:40 -0700293 PRISTINE_IMAGE_NAME=chromiumos_base_image.bin
294 DEVELOPER_IMAGE_NAME=chromiumos_image.bin
Vince Laviano96d116d2011-03-17 11:32:07 -0700295# Rename pristine image for factory install shim
J. Richard Barnetteeb5246f2011-04-08 10:34:31 -0700296elif [ ${FLAGS_factory_install} -eq ${FLAGS_TRUE} ]; then
Nick Sandersae26a5c2010-08-30 21:59:50 -0700297 PRISTINE_IMAGE_NAME=factory_install_shim.bin
Tan Gao843b70a2010-08-17 09:41:48 -0700298fi
299
David James868d7bb2010-06-24 21:48:14 -0700300PRISTINE_IMG="${OUTPUT_DIR}/${PRISTINE_IMAGE_NAME}"
301DEVELOPER_IMG="${OUTPUT_DIR}/${DEVELOPER_IMAGE_NAME}"
Vince Laviano6aebf312011-06-01 15:27:31 -0700302TEST_IMG="${OUTPUT_DIR}/${CHROMEOS_TEST_IMAGE_NAME}"
303FACTORY_IMG="${OUTPUT_DIR}/${CHROMEOS_FACTORY_TEST_IMAGE_NAME}"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700304
Don Garrett3f41e152010-06-21 14:54:34 -0700305ROOT_FS_IMG="${OUTPUT_DIR}/rootfs.image"
306ROOT_FS_DIR="${OUTPUT_DIR}/rootfs"
Will Drewry4a675e12010-07-03 13:35:02 -0500307ROOT_FS_HASH="${OUTPUT_DIR}/rootfs.hash"
Don Garrett3f41e152010-06-21 14:54:34 -0700308
309STATEFUL_FS_IMG="${OUTPUT_DIR}/stateful_partition.image"
310STATEFUL_FS_DIR="${OUTPUT_DIR}/stateful_partition"
311
312ESP_FS_IMG=${OUTPUT_DIR}/esp.image
313ESP_FS_DIR=${OUTPUT_DIR}/esp
314
Will Drewry7ab698d2010-08-05 13:57:52 -0500315DEVKEYSDIR="/usr/share/vboot/devkeys"
316
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700317LOOP_DEV=
Chris Sosa4bffb8b2010-04-07 17:23:54 -0700318STATEFUL_LOOP_DEV=
Bill Richardsona81df762010-04-09 08:12:05 -0700319ESP_LOOP_DEV=
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700320
Don Garrett3f41e152010-06-21 14:54:34 -0700321# ${DEV_IMAGE_ROOT} specifies the location of where developer packages will
322# be installed on the stateful dir. On a Chromium OS system, this will
323# translate to /usr/local.
324DEV_IMAGE_ROOT="${STATEFUL_FS_DIR}/dev_image"
325
Nick Sandersae26a5c2010-08-30 21:59:50 -0700326if [ ${FLAGS_jobs} -ne -1 ]; then
David Jamesfd7403a2010-07-09 12:00:17 -0700327 EMERGE_JOBS="--jobs=${FLAGS_jobs}"
328fi
329
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800330if [[ ${FLAGS_crosbug12352_arm_kernel_signing} -eq ${FLAGS_TRUE} ]]; then
331 crosbug12352_flag="--crosbug12352_arm_kernel_signing"
332else
333 crosbug12352_flag="--nocrosbug12352_arm_kernel_signing"
334fi
Will Drewry12f14ce2010-08-17 17:27:16 -0500335if [[ ${FLAGS_enable_rootfs_verification} -eq ${FLAGS_TRUE} ]]; then
336 enable_rootfs_verification_flag="--enable_rootfs_verification"
Will Drewry12f14ce2010-08-17 17:27:16 -0500337fi
338
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700339# Hack to fix bug where x86_64 CHOST line gets incorrectly added.
340# ToDo(msb): remove this hack.
341PACKAGES_FILE="${BOARD_ROOT}/packages/Packages"
342sudo sed -e "s/CHOST: x86_64-pc-linux-gnu//" -i "${PACKAGES_FILE}"
343
344# Handle existing directory.
Don Garrette0020b12010-06-17 15:55:35 -0700345if [[ -e "${OUTPUT_DIR}" ]]; then
346 if [[ ${FLAGS_replace} -eq ${FLAGS_TRUE} ]]; then
347 sudo rm -rf "${OUTPUT_DIR}"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700348 else
Don Garrette0020b12010-06-17 15:55:35 -0700349 echo "Directory ${OUTPUT_DIR} already exists."
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700350 echo "Use --build_attempt option to specify an unused attempt."
351 echo "Or use --replace if you want to overwrite this directory."
352 exit 1
353 fi
354fi
355
David James868d7bb2010-06-24 21:48:14 -0700356# Find previous build, if any...
357PREVIOUS_DIR=$($SCRIPTS_DIR/get_latest_image.sh --board="$BOARD")
358
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700359cleanup_rootfs_loop() {
Don Garrette0020b12010-06-17 15:55:35 -0700360 sudo umount -d "${ROOT_FS_DIR}"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700361}
362
363cleanup_stateful_fs_loop() {
Chris Sosa4bffb8b2010-04-07 17:23:54 -0700364 sudo umount "${ROOT_FS_DIR}/usr/local"
365 sudo umount "${ROOT_FS_DIR}/var"
Don Garrett3f41e152010-06-21 14:54:34 -0700366 sudo umount -d "${STATEFUL_FS_DIR}"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700367}
368
Bill Richardson8b3bd102010-04-06 15:00:10 -0700369cleanup_esp_loop() {
Don Garrett3f41e152010-06-21 14:54:34 -0700370 sudo umount -d "${ESP_FS_DIR}"
Bill Richardson8b3bd102010-04-06 15:00:10 -0700371}
372
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700373cleanup() {
374 # Disable die on error.
375 set +e
376
Don Garrette0020b12010-06-17 15:55:35 -0700377 if [[ -n "${STATEFUL_LOOP_DEV}" ]]; then
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700378 cleanup_stateful_fs_loop
Will Drewry4a675e12010-07-03 13:35:02 -0500379 STATEFUL_LOOP_DEV=
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700380 fi
381
Don Garrette0020b12010-06-17 15:55:35 -0700382 if [[ -n "${LOOP_DEV}" ]]; then
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700383 cleanup_rootfs_loop
Will Drewry4a675e12010-07-03 13:35:02 -0500384 LOOP_DEV=
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700385 fi
386
Don Garrette0020b12010-06-17 15:55:35 -0700387 if [[ -n "${ESP_LOOP_DEV}" ]]; then
Bill Richardson8b3bd102010-04-06 15:00:10 -0700388 cleanup_esp_loop
Will Drewry4a675e12010-07-03 13:35:02 -0500389 ESP_LOOP_DEV=
Bill Richardson8b3bd102010-04-06 15:00:10 -0700390 fi
391
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700392 # Turn die on error back on.
393 set -e
394}
395
Chris Sosabde8c1b2010-04-29 14:02:35 -0700396delete_prompt() {
397 echo "An error occurred in your build so your latest output directory" \
398 "is invalid."
Tan Gao74a07ca2010-09-09 11:41:30 -0700399
400 # Only prompt if both stdin and stdout are a tty. If either is not a tty,
401 # then the user may not be present, so we shouldn't bother prompting.
Zdenek Behan23bc6ca2011-02-10 02:04:58 +0100402 if tty -s && tty -s <&1 && [ "${USER}" != 'chrome-bot' ]; then
Tan Gao74a07ca2010-09-09 11:41:30 -0700403 read -p "Would you like to delete the output directory (y/N)? " SURE
404 SURE="${SURE:0:1}" # Get just the first character.
405 else
406 SURE="y"
407 echo "Running in non-interactive mode so deleting output directory."
408 fi
Chris Sosabde8c1b2010-04-29 14:02:35 -0700409 if [ "${SURE}" == "y" ] ; then
Don Garrette0020b12010-06-17 15:55:35 -0700410 sudo rm -rf "${OUTPUT_DIR}"
411 echo "Deleted ${OUTPUT_DIR}"
Chris Sosabde8c1b2010-04-29 14:02:35 -0700412 else
Tan Gao74a07ca2010-09-09 11:41:30 -0700413 echo "Not deleting ${OUTPUT_DIR}."
Chris Sosabde8c1b2010-04-29 14:02:35 -0700414 fi
415}
416
Chris Sosa9673f3b2010-05-18 13:24:40 -0700417# $1 - Directory where developer rootfs is mounted.
418# $2 - Directory where developer stateful_partition is mounted.
Will Drewry4a675e12010-07-03 13:35:02 -0500419# $3 - Directory where the ESP partition is mounted.
Don Garrett3f41e152010-06-21 14:54:34 -0700420mount_gpt_cleanup() {
Will Drewry4a675e12010-07-03 13:35:02 -0500421 local rootfs="${1-$ROOT_FS_DIR}"
422 local statefs="${2-$STATEFUL_FS_DIR}"
423 local espfs="${3-$ESP_FS_DIR}"
424 "${SCRIPTS_DIR}/mount_gpt_image.sh" \
425 -u -r "${rootfs}" -s "${statefs}" -e "${espfs}"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700426 delete_prompt
427}
428
Will Drewry7ab698d2010-08-05 13:57:52 -0500429# Takes no arguments and populates the configuration for
430# cros_make_image_bootable.
431create_boot_desc() {
432 cat <<EOF > ${OUTPUT_DIR}/boot.desc
433 --arch="${ARCH}"
434 --output_dir="${OUTPUT_DIR}"
Will Drewry32d8c112010-10-11 15:37:17 -0500435 --boot_args="${FLAGS_boot_args}"
Will Drewry7ab698d2010-08-05 13:57:52 -0500436 --rootfs_size="${FLAGS_rootfs_size}"
437 --rootfs_hash_pad="${FLAGS_rootfs_hash_pad}"
438 --rootfs_hash="${ROOT_FS_HASH}"
439 --rootfs_mountpoint="${ROOT_FS_DIR}"
440 --statefulfs_mountpoint="${STATEFUL_FS_DIR}"
441 --espfs_mountpoint="${ESP_FS_DIR}"
442 --verity_error_behavior="${FLAGS_verity_error_behavior}"
Will Drewry7ab698d2010-08-05 13:57:52 -0500443 --verity_max_ios="${FLAGS_verity_max_ios}"
444 --verity_algorithm="${FLAGS_verity_algorithm}"
445 --keys_dir="${DEVKEYSDIR}"
446 --usb_disk="${FLAGS_usb_disk}"
447 --nocleanup_dirs
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800448 ${crosbug12352_flag}
Will Drewry7ab698d2010-08-05 13:57:52 -0500449 ${enable_rootfs_verification_flag}
450EOF
Will Drewry4a675e12010-07-03 13:35:02 -0500451}
452
Don Garrett3f41e152010-06-21 14:54:34 -0700453# Modifies an existing image to add development packages
454update_dev_packages() {
455 local image_name=$1
Chris Sosa9673f3b2010-05-18 13:24:40 -0700456
Don Garrett3f41e152010-06-21 14:54:34 -0700457 echo "Adding developer packages to ${image_name}"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700458
Will Drewry4a675e12010-07-03 13:35:02 -0500459 trap "mount_gpt_cleanup" EXIT
Tan Gaoa40ed442010-06-02 15:45:19 -0700460
Don Garrette0020b12010-06-17 15:55:35 -0700461 ${SCRIPTS_DIR}/mount_gpt_image.sh --from "${OUTPUT_DIR}" \
David James868d7bb2010-06-24 21:48:14 -0700462 --image "${image_name}" -r "${ROOT_FS_DIR}" \
Will Drewry4a675e12010-07-03 13:35:02 -0500463 -s "${STATEFUL_FS_DIR}" -e "${ESP_FS_DIR}"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700464
Don Garrett3f41e152010-06-21 14:54:34 -0700465 # Determine the root dir for developer packages.
466 local root_dev_dir="${ROOT_FS_DIR}"
467 [ ${FLAGS_statefuldev} -eq ${FLAGS_TRUE} ] && \
468 root_dev_dir="${ROOT_FS_DIR}/usr/local"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700469
Don Garrett3f41e152010-06-21 14:54:34 -0700470 # Install developer packages described in chromeos-dev.
J. Richard Barnetteeb5246f2011-04-08 10:34:31 -0700471 emerge_to_image --root="${root_dev_dir}" -uDNv chromeos-dev
David James868d7bb2010-06-24 21:48:14 -0700472
473 if [[ $FLAGS_preserve -eq ${FLAGS_TRUE} ]] ; then
474 # Clean out unused packages
J. Richard Barnetteeb5246f2011-04-08 10:34:31 -0700475 emerge_to_image --root="${ROOT_FS_DIR}" --depclean
David James868d7bb2010-06-24 21:48:14 -0700476 fi
Chris Sosa9673f3b2010-05-18 13:24:40 -0700477
Paul Stewart12973e52010-09-09 20:04:11 -0700478 # Install the bare necessary files so that the "emerge" command works
David James49523432011-02-17 15:19:16 -0800479 if [ ${FLAGS_statefuldev} -eq ${FLAGS_TRUE} ]; then
David Jamese8875482011-02-23 15:24:14 -0800480 sudo cp -a ${root_dev_dir}/share/portage ${ROOT_FS_DIR}/usr/share
David James49523432011-02-17 15:19:16 -0800481 sudo sed -i s,/usr/bin/wget,wget, \
David Jamese8875482011-02-23 15:24:14 -0800482 ${ROOT_FS_DIR}/usr/share/portage/config/make.globals
David James49523432011-02-17 15:19:16 -0800483 fi
Paul Stewart12973e52010-09-09 20:04:11 -0700484 sudo mkdir -p ${ROOT_FS_DIR}/etc/make.profile
485
Chris Sosa9673f3b2010-05-18 13:24:40 -0700486 # Re-run ldconfig to fix /etc/ldconfig.so.cache.
Don Garrett3f41e152010-06-21 14:54:34 -0700487 sudo /sbin/ldconfig -r "${ROOT_FS_DIR}"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700488
489 # Mark the image as a developer image (input to chromeos_startup).
Don Garrett3f41e152010-06-21 14:54:34 -0700490 sudo mkdir -p "${ROOT_FS_DIR}/root"
491 sudo touch "${ROOT_FS_DIR}/root/.dev_mode"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700492
Don Garrett3f41e152010-06-21 14:54:34 -0700493 # Additional changes to developer image.
Chris Sosa9673f3b2010-05-18 13:24:40 -0700494
Ken Mixter13347882010-08-12 16:51:52 -0700495 # Leave core files for developers to inspect.
Ken Mixter0d115632010-08-13 15:51:07 -0700496 sudo touch "${ROOT_FS_DIR}/root/.leave_core"
Ken Mixter13347882010-08-12 16:51:52 -0700497
David James72759e82011-05-23 10:38:11 -0700498 # This hack is only needed for devs who have old versions of glibc, which
499 # filtered out ldd when cross-compiling. TODO(davidjames): Remove this hack
500 # once everybody has upgraded to a new version of glibc.
501 if [[ ! -x "${ROOT_FS_DIR}/usr/bin/ldd" ]]; then
502 sudo cp -a "$(which ldd)" "${ROOT_FS_DIR}/usr/bin"
503 fi
Chris Sosa9673f3b2010-05-18 13:24:40 -0700504
Don Garrett3f41e152010-06-21 14:54:34 -0700505 # If vim is installed, then a vi symlink would probably help.
506 if [[ -x "${ROOT_FS_DIR}/usr/local/bin/vim" ]]; then
507 sudo ln -sf vim "${ROOT_FS_DIR}/usr/local/bin/vi"
Girts Folkmanis7a8a8382010-05-18 22:52:25 -0700508 fi
Chris Sosa9673f3b2010-05-18 13:24:40 -0700509
Tammo Spalinkcfc0c642010-07-07 10:51:21 +0800510 # If pygtk is installed in stateful-dev, then install a path.
511 if [[ -d \
512 "${ROOT_FS_DIR}/usr/local/lib/python2.6/site-packages/gtk-2.0" ]]; then
513 sudo bash -c "\
514 echo gtk-2.0 > \
515 ${ROOT_FS_DIR}/usr/local/lib/python2.6/site-packages/pygtk.pth"
516 fi
517
Chris Sosa6304adc2010-10-20 13:40:33 -0700518 # If python is installed on stateful-dev, fix python symlinks.
519 local python_path="/usr/local/bin/python2.6"
520 if [ -e "${ROOT_FS_DIR}${python_path}" ]; then
521 info "Fixing python symlinks for developer and test images."
522 local python_paths="/usr/bin/python /usr/local/bin/python \
523 /usr/bin/python2 /usr/local/bin/python2"
524 for path in ${python_paths}; do
525 sudo rm -f "${ROOT_FS_DIR}${path}"
526 sudo ln -s ${python_path} "${ROOT_FS_DIR}${path}"
527 done
528 fi
529
Don Garrett3f41e152010-06-21 14:54:34 -0700530 # Check that the image has been correctly created. Only do it if not
Vince Laviano96d116d2011-03-17 11:32:07 -0700531 # building a factory install shim, as the INSTALL_MASK for it will make
532 # test_image fail.
533 if [ ${FLAGS_factory_install} -eq ${FLAGS_FALSE} ]; then
Don Garrett3f41e152010-06-21 14:54:34 -0700534 "${SCRIPTS_DIR}/test_image" \
535 --root="${ROOT_FS_DIR}" \
536 --target="${ARCH}"
537 fi
538 echo "Developer image built and stored at ${image_name}"
539
Chris Sosa9673f3b2010-05-18 13:24:40 -0700540 trap - EXIT
David James868d7bb2010-06-24 21:48:14 -0700541 ${SCRIPTS_DIR}/mount_gpt_image.sh -u -r "${ROOT_FS_DIR}" \
Will Drewry4a675e12010-07-03 13:35:02 -0500542 -s "${STATEFUL_FS_DIR}" -e "${ESP_FS_DIR}"
David James868d7bb2010-06-24 21:48:14 -0700543}
544
545# Update the base package on an existing image.
546update_base_packages() {
547 local image_name=$1
548
549 echo "Updating base packages on ${image_name}"
550
551 # Create stateful partition of the same size as the rootfs.
Will Drewry4a675e12010-07-03 13:35:02 -0500552 trap "mount_gpt_cleanup" EXIT
David James868d7bb2010-06-24 21:48:14 -0700553
554 ${SCRIPTS_DIR}/mount_gpt_image.sh --from "${OUTPUT_DIR}" \
555 --image "${image_name}" -r "${ROOT_FS_DIR}" \
Will Drewry4a675e12010-07-03 13:35:02 -0500556 -s "${STATEFUL_FS_DIR}" -e "${ESP_FS_DIR}"
David James868d7bb2010-06-24 21:48:14 -0700557
558 # Emerge updated packages, exactly like when creating base image
J. Richard Barnetteeb5246f2011-04-08 10:34:31 -0700559 emerge_to_image --root="${ROOT_FS_DIR}" -uDNv chromeos ${EXTRA_PACKAGES}
David James868d7bb2010-06-24 21:48:14 -0700560
561 # Clean out unused packages
J. Richard Barnetteeb5246f2011-04-08 10:34:31 -0700562 emerge_to_image --root="${ROOT_FS_DIR}" --depclean
David James868d7bb2010-06-24 21:48:14 -0700563
564 trap - EXIT
565 ${SCRIPTS_DIR}/mount_gpt_image.sh -u -r "${ROOT_FS_DIR}" \
Will Drewry4a675e12010-07-03 13:35:02 -0500566 -s "${STATEFUL_FS_DIR}" -e "${ESP_FS_DIR}"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700567}
568
Thieu Le171321f2010-12-13 11:37:42 -0800569zero_free_space() {
570 local fs_mount_point=$1
571 info "Zeroing freespace in ${fs_mount_point}"
Zdenek Behanaeb83522011-03-25 01:19:16 +0100572 # dd is a silly thing and will produce a "No space left on device" message
573 # that cannot be turned off and is confusing to unsuspecting victims.
574 ( sudo dd if=/dev/zero of="${fs_mount_point}/filler" oflag=sync bs=4096 \
575 || true ) 2>&1 |grep -v "No space left on device"
Thieu Le171321f2010-12-13 11:37:42 -0800576 sudo rm -f "${fs_mount_point}/filler"
577 sudo sync
578}
579
Don Garrett3f41e152010-06-21 14:54:34 -0700580create_base_image() {
David James868d7bb2010-06-24 21:48:14 -0700581 local image_name=$1
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700582
Don Garrett3f41e152010-06-21 14:54:34 -0700583 trap "cleanup && delete_prompt" EXIT
Chris Sosa4bffb8b2010-04-07 17:23:54 -0700584
Don Garrett3f41e152010-06-21 14:54:34 -0700585 # Create and format the root file system.
586
587 # Check for loop device before creating image.
588 LOOP_DEV=$(sudo losetup -f)
589 if [ -z "${LOOP_DEV}" ] ; then
590 echo "No free loop device. Free up a loop device or reboot. exiting. "
591 exit 1
592 fi
593
Will Drewryd253bad2010-08-17 14:09:41 -0500594 # Create root file system disk image.
595 ROOT_SIZE_BYTES=$((1024 * 1024 * ${FLAGS_rootfs_size}))
Don Garrett3f41e152010-06-21 14:54:34 -0700596
Will Drewry9926ee22010-07-21 15:11:10 -0500597 # Pad out for the hash tree.
Will Drewry78992a32010-07-21 14:02:20 -0500598 ROOT_HASH_PAD=$((FLAGS_rootfs_hash_pad * 1024 * 1024))
599 info "Padding the rootfs image by ${ROOT_HASH_PAD} bytes for hash data"
Will Drewry9926ee22010-07-21 15:11:10 -0500600
Will Drewry78992a32010-07-21 14:02:20 -0500601 dd if=/dev/zero of="${ROOT_FS_IMG}" bs=1 count=1 \
602 seek=$((ROOT_SIZE_BYTES + ROOT_HASH_PAD - 1))
Will Drewry9926ee22010-07-21 15:11:10 -0500603 sudo losetup "${LOOP_DEV}" "${ROOT_FS_IMG}"
604 # Specify a block size and block count to avoid using the hash pad.
Will Drewry55b42c92010-10-20 15:44:11 -0500605 sudo mkfs.ext2 -b 4096 "${LOOP_DEV}" "$((ROOT_SIZE_BYTES / 4096))"
Will Drewry78992a32010-07-21 14:02:20 -0500606
Don Garrett3f41e152010-06-21 14:54:34 -0700607 # Tune and mount rootfs.
Will Drewry55b42c92010-10-20 15:44:11 -0500608 DISK_LABEL="C-ROOT"
609 # Disable checking and minimize metadata differences across builds
610 # and wasted reserved space.
611 sudo tune2fs -L "${DISK_LABEL}" \
612 -U clear \
613 -T 20091119110000 \
614 -c 0 \
615 -i 0 \
616 -m 0 \
617 -r 0 \
618 -e remount-ro \
619 "${LOOP_DEV}"
620 # TODO(wad) call tune2fs prior to finalization to set the mount count to 0.
Zdenek Behan7454a5e2011-03-25 01:25:41 +0100621 sudo mount -t ext2 "${LOOP_DEV}" "${ROOT_FS_DIR}"
Don Garrett3f41e152010-06-21 14:54:34 -0700622
623 # Create stateful partition of the same size as the rootfs.
624 STATEFUL_LOOP_DEV=$(sudo losetup -f)
625 if [ -z "${STATEFUL_LOOP_DEV}" ] ; then
626 echo "No free loop device. Free up a loop device or reboot. exiting. "
627 exit 1
628 fi
Eric Li5cf288f2010-06-28 12:46:26 -0700629 STATEFUL_SIZE_BYTES=$((1024 * 1024 * ${FLAGS_statefulfs_size}))
630 dd if=/dev/zero of="${STATEFUL_FS_IMG}" bs=1 count=1 \
631 seek=$((STATEFUL_SIZE_BYTES - 1))
Denis Romanov11061542010-06-29 04:23:53 +0400632
633 # Tune and mount the stateful partition.
634 UUID=$(uuidgen)
635 DISK_LABEL="C-STATE"
Don Garrett3f41e152010-06-21 14:54:34 -0700636 sudo losetup "${STATEFUL_LOOP_DEV}" "${STATEFUL_FS_IMG}"
637 sudo mkfs.ext3 "${STATEFUL_LOOP_DEV}"
Denis Romanov11061542010-06-29 04:23:53 +0400638 sudo tune2fs -L "${DISK_LABEL}" -U "${UUID}" -c 0 -i 0 "${STATEFUL_LOOP_DEV}"
Zdenek Behan7454a5e2011-03-25 01:25:41 +0100639 sudo mount -t ext3 "${STATEFUL_LOOP_DEV}" "${STATEFUL_FS_DIR}"
Don Garrett3f41e152010-06-21 14:54:34 -0700640
Don Garrett3f41e152010-06-21 14:54:34 -0700641 # -- Install packages into the root file system --
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700642
Don Garrett3f41e152010-06-21 14:54:34 -0700643 # We need to install libc manually from the cross toolchain.
644 # TODO: Improve this? We only want libc and not the whole toolchain.
Raymes Khoury9e1084c2011-02-25 14:01:10 -0800645 # TODO(raymes): Remove this check after some time which ensures
646 # backward compatibility with the crossdev location change.
647 PKGDIR="/var/lib/portage/pkgs"
648 LIBC_TAR="glibc-${LIBC_VERSION}.tbz2"
649 NEW_LIBC_PATH="${PKGDIR}/cross-${CHOST}/${LIBC_TAR}"
650 OLD_LIBC_PATH="${PKGDIR}/cross/${CHOST}/cross-${CHOST}/${LIBC_TAR}"
651 if [ -e "${NEW_LIBC_PATH}" ] ; then
652 LIBC_PATH="${NEW_LIBC_PATH}"
653 else
654 LIBC_PATH="${OLD_LIBC_PATH}"
655 fi
656
657 sudo tar jxvpf "${LIBC_PATH}" -C "${ROOT_FS_DIR}" --strip-components=3 \
Don Garrett3f41e152010-06-21 14:54:34 -0700658 --exclude=usr/include --exclude=sys-include --exclude=*.a --exclude=*.o
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700659
Don Garrett3f41e152010-06-21 14:54:34 -0700660 # We need to install libstdc++ manually from the cross toolchain.
661 # TODO: Figure out a better way of doing this?
662 sudo cp -a "${BOARD_ROOT}"/lib/libgcc_s.so* "${ROOT_FS_DIR}/lib"
663 sudo cp -a "${BOARD_ROOT}"/usr/lib/libstdc++.so* "${ROOT_FS_DIR}/usr/lib"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700664
Don Garrett3f41e152010-06-21 14:54:34 -0700665 # Prepare stateful partition with some pre-created directories.
666 sudo mkdir -p "${DEV_IMAGE_ROOT}"
667 sudo mkdir -p "${STATEFUL_FS_DIR}/var"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700668
Don Garrett3f41e152010-06-21 14:54:34 -0700669 # Create symlinks so that /usr/local/usr based directories are symlinked to
670 # /usr/local/ directories e.g. /usr/local/usr/bin -> /usr/local/bin, etc.
671 setup_symlinks_on_root "${DEV_IMAGE_ROOT}" "${STATEFUL_FS_DIR}/var" \
672 "${STATEFUL_FS_DIR}"
Tom Wai-Hong Tamf87a3672010-05-17 16:06:33 +0800673
Don Garrett3f41e152010-06-21 14:54:34 -0700674 # Perform binding rather than symlinking because directories must exist
675 # on rootfs so that we can bind at run-time since rootfs is read-only.
676 echo "Binding directories from stateful partition onto the rootfs"
677 sudo mkdir -p "${ROOT_FS_DIR}/usr/local"
678 sudo mount --bind "${DEV_IMAGE_ROOT}" "${ROOT_FS_DIR}/usr/local"
679 sudo mkdir -p "${ROOT_FS_DIR}/var"
680 sudo mount --bind "${STATEFUL_FS_DIR}/var" "${ROOT_FS_DIR}/var"
681 sudo mkdir -p "${ROOT_FS_DIR}/dev"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700682
David Jamesffede3b2010-09-07 14:22:38 -0700683 # We "emerge --root=${ROOT_FS_DIR} --root-deps=rdeps --usepkgonly" all of the
Don Garrett3f41e152010-06-21 14:54:34 -0700684 # runtime packages for chrome os. This builds up a chrome os image from
685 # binary packages with runtime dependencies only. We use INSTALL_MASK to
686 # trim the image size as much as possible.
J. Richard Barnetteeb5246f2011-04-08 10:34:31 -0700687 emerge_to_image --root="${ROOT_FS_DIR}" chromeos ${EXTRA_PACKAGES}
Bill Richardson8b3bd102010-04-06 15:00:10 -0700688
Darin Petkova5869302011-03-01 17:15:59 -0800689 # Set /etc/lsb-release on the image.
690 "${OVERLAY_CHROMEOS_DIR}/scripts/cros_set_lsb_release" \
Will Drewry4a675e12010-07-03 13:35:02 -0500691 --root="${ROOT_FS_DIR}" \
Will Drewry4a675e12010-07-03 13:35:02 -0500692 --board="${BOARD}"
Bill Richardson8b3bd102010-04-06 15:00:10 -0700693
Will Drewry1ee156f2010-07-03 13:27:19 -0500694 # Populates the root filesystem with legacy bootloader templates
695 # appropriate for the platform. The autoupdater and installer will
696 # use those templates to update the legacy boot partition (12/ESP)
697 # on update.
698 # (This script does not populate vmlinuz.A and .B needed by syslinux.)
Will Drewry1670d482010-07-09 13:08:38 -0700699 enable_rootfs_verification=
700 if [[ ${FLAGS_enable_rootfs_verification} -eq ${FLAGS_TRUE} ]]; then
701 enable_rootfs_verification="--enable_rootfs_verification"
702 fi
703
Will Drewry1ee156f2010-07-03 13:27:19 -0500704 ${SCRIPTS_DIR}/create_legacy_bootloader_templates.sh \
705 --arch=${ARCH} \
706 --to="${ROOT_FS_DIR}"/boot \
Will Drewry32d8c112010-10-11 15:37:17 -0500707 --boot_args="${FLAGS_boot_args}" \
Will Drewry1ee156f2010-07-03 13:27:19 -0500708 --install \
Will Drewry1670d482010-07-09 13:08:38 -0700709 ${enable_rootfs_verification}
Bill Richardson9c5e5ec2010-05-14 17:00:21 -0700710
Vince Laviano96d116d2011-03-17 11:32:07 -0700711 # Don't test the factory install shim
712 if [ ${FLAGS_factory_install} -eq ${FLAGS_FALSE} ]; then
Don Garrett3f41e152010-06-21 14:54:34 -0700713 # Check that the image has been correctly created.
714 "${SCRIPTS_DIR}/test_image" \
715 --root="${ROOT_FS_DIR}" \
716 --target="${ARCH}"
717 fi
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700718
Don Garrett3f41e152010-06-21 14:54:34 -0700719 # Clean up symlinks so they work on a running target rooted at "/".
720 # Here development packages are rooted at /usr/local. However, do not
721 # create /usr/local or /var on host (already exist on target).
722 setup_symlinks_on_root "/usr/local" "/var" "${STATEFUL_FS_DIR}"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700723
Will Drewry7ab698d2010-08-05 13:57:52 -0500724 # cros_make_image_bootable will clobber vmlinuz.image for x86.
Will Drewry4a675e12010-07-03 13:35:02 -0500725 # Until then, just copy the kernel to vmlinuz.image. It is
Kenneth Waterse3049de2010-09-30 14:20:34 -0700726 # expected in build_gpt.sh.
Will Drewry4a675e12010-07-03 13:35:02 -0500727 cp "${ROOT_FS_DIR}/boot/vmlinuz" "${OUTPUT_DIR}/vmlinuz.image"
728
729 # Create an empty esp image to be updated in by update_bootloaders.sh.
730 ${SCRIPTS_DIR}/create_esp.sh --to="${ESP_FS_IMG}"
731
Thieu Le171321f2010-12-13 11:37:42 -0800732 # Zero rootfs free space to make it more compressible so auto-update
733 # payloads become smaller
734 zero_free_space "${ROOT_FS_DIR}"
735
Don Garrett3f41e152010-06-21 14:54:34 -0700736 cleanup
Chris Sosabde8c1b2010-04-29 14:02:35 -0700737
Don Garrett3f41e152010-06-21 14:54:34 -0700738 trap delete_prompt EXIT
Tan Gao6df5aee2010-05-19 14:19:55 -0700739
Don Garrett3f41e152010-06-21 14:54:34 -0700740 # Create the GPT-formatted image.
741 ${SCRIPTS_DIR}/build_gpt.sh \
742 --arch=${ARCH} \
743 --board=${FLAGS_board} \
Don Garrett3f41e152010-06-21 14:54:34 -0700744 --rootfs_partition_size=${FLAGS_rootfs_partition_size} \
Don Garrett3f41e152010-06-21 14:54:34 -0700745 "${OUTPUT_DIR}" \
David James868d7bb2010-06-24 21:48:14 -0700746 "${OUTPUT_DIR}/${image_name}"
747
Nick Sanders446c2112010-09-03 18:15:35 -0700748 # Pre-set "sucessful" bit in gpt, so we will never mark-for-death
749 # a partition on an SDCard/USB stick.
750 ${GPT} add -i 2 -S 1 "${OUTPUT_DIR}/${image_name}"
751
David James868d7bb2010-06-24 21:48:14 -0700752 trap - EXIT
Don Garrett3f41e152010-06-21 14:54:34 -0700753}
754
Raja Aluriaeb10482010-12-10 13:03:09 -0800755generate_au_zip () {
756 local lgenerateauzip="${SCRIPTS_DIR}/generate_au_zip.py"
757 local largs="-o ${OUTPUT_DIR}"
Raja Aluri888e07c2010-12-14 10:36:34 -0800758 test ! -d "${OUTPUT_DIR}" && mkdir -p "${OUTPUT_DIR}"
Raja Aluriaeb10482010-12-10 13:03:09 -0800759 info "Running ${lgenerateauzip} ${largs} for generating AU updater zip file"
760 $lgenerateauzip $largs
761}
762
Vince Laviano6aebf312011-06-01 15:27:31 -0700763mod_image_for_test_cleanup() {
764 "${SCRIPTS_DIR}/mount_gpt_image.sh" -u -r "${ROOT_FS_DIR}" \
765 -s "${STATEFUL_FS_DIR}"
766}
767
768# Emerges chromeos-test onto the image.
769emerge_chromeos_test() {
770 # Determine the root dir for test packages.
771 local root_dev_dir="${ROOT_FS_DIR}/usr/local"
772
773 emerge_to_image --root="${root_dev_dir}" chromeos-test
774}
775
776install_autotest_for_factory() {
777 local autotest_src="${FLAGS_build_root}/${FLAGS_board}/usr/local/autotest"
778 local stateful_root="${ROOT_FS_DIR}/usr/local"
779 local autotest_client="${stateful_root}/autotest"
780
781 echo "Install autotest into stateful partition from ${autotest_src}"
782
783 sudo mkdir -p "${autotest_client}"
784
785 # Remove excess files from stateful partition.
786 sudo rm -rf "${autotest_client}/"*
787 sudo rm -rf "${stateful_root}/autotest-pkgs"
788 sudo rm -rf "${stateful_root}/lib/icedtea6"
789
790 sudo rsync --delete --delete-excluded -auv \
791 --exclude=deps/realtimecomm_playground \
792 --exclude=tests/ltp \
793 --exclude=site_tests/graphics_O3DSelenium \
794 --exclude=site_tests/realtimecomm_GTalk\* \
795 --exclude=site_tests/platform_StackProtector \
796 --exclude=deps/chrome_test \
797 --exclude=site_tests/desktopui_BrowserTest \
798 --exclude=site_tests/desktopui_PageCyclerTests \
799 --exclude=site_tests/desktopui_UITest \
800 --exclude=.svn \
801 "${autotest_src}/client/"* "${autotest_client}"
802
803 sudo chmod 755 "${autotest_client}"
804 sudo chown -R 1000:1000 "${autotest_client}"
805}
806
807# convert a dev image into a test or factory test image
808mod_image_for_test () {
809 local image_to_mod=$1
810
811 # Copy the image to a test location before modifying it
812 local test_pathname="${TEST_IMG}"
813 local typename="test"
814 if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ]; then
815 test_pathname="${FACTORY_IMG}"
816 typename="factory"
817 fi
818 echo "Creating ${typename} image from original..."
819 ${COMMON_PV_CAT} ${image_to_mod} > ${test_pathname} ||
820 die "Cannot copy ${image_to_mod} to ${typename} image"
821 # Abort early if we can't find the image
822 if [ ! -f ${test_pathname} ] ; then
823 die "No image found at ${test_pathname} to modify"
824 fi
825 echo "Modifying image ${test_pathname} for ${typename}..."
826
827 local image_dir=$(dirname ${test_pathname})
828 local image_name=$(basename ${test_pathname})
829
830 trap mod_image_for_test_cleanup EXIT
831
832 # Mounts gpt image and sets up var, /usr/local and symlinks.
833 "${SCRIPTS_DIR}/mount_gpt_image.sh" -i "${image_name}" -f "${image_dir}" \
834 -r "${ROOT_FS_DIR}" -s "${STATEFUL_FS_DIR}"
835
836 emerge_chromeos_test
837
838 local mod_test_script="${SCRIPTS_DIR}/mod_for_test_scripts/test_setup.sh"
839 # Run test setup script to modify the image
840 sudo -E GCLIENT_ROOT="${GCLIENT_ROOT}" ROOT_FS_DIR="${ROOT_FS_DIR}" \
841 STATEFUL_DIR="${STATEFUL_FS_DIR}" ARCH="${ARCH}" "${mod_test_script}"
842
843 if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ]; then
844 emerge_to_image --root="${ROOT_FS_DIR}" factorytest-init
845
846 install_autotest_for_factory
847
848 local mod_factory_script
849 mod_factory_script="${SCRIPTS_DIR}/mod_for_factory_scripts/factory_setup.sh"
850 # Run factory setup script to modify the image
851 sudo -E GCLIENT_ROOT="${GCLIENT_ROOT}" ROOT_FS_DIR="${ROOT_FS_DIR}" \
852 QUALDB="${FLAGS_qualdb}" BOARD="${FLAGS_board}" "${mod_factory_script}"
853 fi
854
855 # Re-run ldconfig to fix /etc/ldconfig.so.cache.
856 sudo ldconfig -r "${ROOT_FS_DIR}"
857
858 mod_image_for_test_cleanup
859
860 # Now make it bootable with the flags from build_image
861 "${SCRIPTS_DIR}/bin/cros_make_image_bootable" "${image_dir}" "${image_name}" \
862 ${crosbug12352_flag}
863
864 print_time_elapsed
865
866 trap - EXIT
867}
868
Don Garrett3f41e152010-06-21 14:54:34 -0700869# Create the output directory.
870mkdir -p "${OUTPUT_DIR}"
871mkdir -p "${ROOT_FS_DIR}"
872mkdir -p "${STATEFUL_FS_DIR}"
873mkdir -p "${ESP_FS_DIR}"
874
David James868d7bb2010-06-24 21:48:14 -0700875# Preserve old images by copying them forward for --preserve.
Vince Laviano6aebf312011-06-01 15:27:31 -0700876# TODO(vlaviano): Do we want --preserve to copy other image types also?
877if [[ ${FLAGS_preserve} -eq ${FLAGS_TRUE} ]] ; then
David James868d7bb2010-06-24 21:48:14 -0700878 if [[ -f ${PREVIOUS_DIR}/${PRISTINE_IMAGE_NAME} ]] ; then
879 # Copy forward pristine image, and associated files
880 cp ${PREVIOUS_DIR}/*.sh ${PREVIOUS_DIR}/config.txt ${OUTPUT_DIR}
881 cp ${PREVIOUS_DIR}/${PRISTINE_IMAGE_NAME} ${OUTPUT_DIR}
882
883 # Copy forward the developer image, if we already copied forward the base.
884 if [[ ${FLAGS_withdev} -eq ${FLAGS_TRUE} ]] && \
885 [[ -f ${PREVIOUS_DIR}/${DEVELOPER_IMAGE_NAME} ]] ; then
886 cp ${PREVIOUS_DIR}/${DEVELOPER_IMAGE_NAME} ${OUTPUT_DIR}
887 fi
888 fi
889fi
890
Will Drewry7ab698d2010-08-05 13:57:52 -0500891# Create the boot.desc file which stores the build-time configuration
892# information needed for making the image bootable after creation with
893# cros_make_image_bootable.
894create_boot_desc
895
David James868d7bb2010-06-24 21:48:14 -0700896if [[ -f ${PRISTINE_IMG} ]] ; then
897 update_base_packages ${PRISTINE_IMAGE_NAME}
898else
899 create_base_image ${PRISTINE_IMAGE_NAME}
900fi
Tan Gao843b70a2010-08-17 09:41:48 -0700901
902USE_DEV_KEYS=
J. Richard Barnetteeb5246f2011-04-08 10:34:31 -0700903if [ ${FLAGS_factory_install} -eq ${FLAGS_TRUE} ]; then
Tan Gao843b70a2010-08-17 09:41:48 -0700904 USE_DEV_KEYS="--use_dev_keys"
905fi
906
907# Place flags before positional args
Will Drewry7ab698d2010-08-05 13:57:52 -0500908${SCRIPTS_DIR}/bin/cros_make_image_bootable "${OUTPUT_DIR}" \
Tan Gao843b70a2010-08-17 09:41:48 -0700909 "${PRISTINE_IMAGE_NAME}" \
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800910 ${USE_DEV_KEYS} \
911 ${crosbug12352_flag}
Will Drewry4a675e12010-07-03 13:35:02 -0500912
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800913# FIXME Test x86 image, and test arm image if enabled;
914# should unconditionally test an image after crosbug12352 is fixed
915if [[ "${ARCH}" = "x86" ]] ||
916 [[ "${ARCH}" = "arm" &&
J. Richard Barnetteeb5246f2011-04-08 10:34:31 -0700917 ${FLAGS_crosbug12352_arm_kernel_signing} -eq ${FLAGS_TRUE} ]]; then
Tan Gao843b70a2010-08-17 09:41:48 -0700918 BOOT_FLAG=
J. Richard Barnetteeb5246f2011-04-08 10:34:31 -0700919 if [ ${FLAGS_factory_install} -eq ${FLAGS_TRUE} ]; then
Tan Gao843b70a2010-08-17 09:41:48 -0700920 BOOT_FLAG="-b 1" # BOOT_FLAG_DEVELOPER value defined in load_kernel_fw.h
Vince Laviano96d116d2011-03-17 11:32:07 -0700921 info "--factory_install set, pass BOOT_FLAG_DEVELOPER flag to \
922load_kernel_test"
Tan Gao843b70a2010-08-17 09:41:48 -0700923 fi
924
Will Drewry4a675e12010-07-03 13:35:02 -0500925 # Verify the final image.
926 load_kernel_test "${OUTPUT_DIR}/${PRISTINE_IMAGE_NAME}" \
Tan Gao843b70a2010-08-17 09:41:48 -0700927 "${DEVKEYSDIR}/recovery_key.vbpubk" ${BOOT_FLAG}
Will Drewry4a675e12010-07-03 13:35:02 -0500928fi
Bill Richardson4364a2e2010-03-30 14:17:34 -0700929
Don Garrette0020b12010-06-17 15:55:35 -0700930# Create a developer image based on the chromium os base image.
J. Richard Barnetteeb5246f2011-04-08 10:34:31 -0700931if [ ${FLAGS_withdev} -eq ${FLAGS_TRUE} ] ; then
David James868d7bb2010-06-24 21:48:14 -0700932 if [[ ! -f ${DEVELOPER_IMG} ]] ; then
933 echo "Creating developer image from base image ${PRISTINE_IMAGE_NAME}"
934 cp ${PRISTINE_IMG} ${DEVELOPER_IMG}
935 fi
Don Garrett3f41e152010-06-21 14:54:34 -0700936
David James868d7bb2010-06-24 21:48:14 -0700937 update_dev_packages ${DEVELOPER_IMAGE_NAME}
Will Drewry7ab698d2010-08-05 13:57:52 -0500938 ${SCRIPTS_DIR}/bin/cros_make_image_bootable "${OUTPUT_DIR}" \
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800939 "${DEVELOPER_IMAGE_NAME}" \
940 ${crosbug12352_flag}
Bill Richardson6ed13582010-06-16 21:38:15 -0700941fi
942
Vince Laviano6aebf312011-06-01 15:27:31 -0700943# Create a test or factory test image if desired
944if [ ${FLAGS_test} -eq ${FLAGS_TRUE} ] ; then
945 mod_image_for_test ${DEVELOPER_IMG}
946fi
947
Bill Richardson6ed13582010-06-16 21:38:15 -0700948# Clean up temporary files.
Don Garrett3f41e152010-06-21 14:54:34 -0700949rm -f "${ROOT_FS_IMG}" "${STATEFUL_FS_IMG}" "${OUTPUT_DIR}/vmlinuz.image" \
Dmitry Polukhin0a649b52011-02-24 13:02:52 +0300950 "${ESP_FS_IMG}" "${OUTPUT_DIR}/vmlinuz_hd.vblock"
951rmdir "${ROOT_FS_DIR}" "${STATEFUL_FS_DIR}" "${ESP_FS_DIR}"
Bill Richardson67956222010-05-28 15:38:56 -0700952
Raja Aluriaeb10482010-12-10 13:03:09 -0800953# Generating AU generator zip file to run outside chroot
954generate_au_zip || echo "Failed generating AU zip file - ignoring Error..."
Olof Johansson58f25cc2010-08-04 21:18:49 -0700955# Create a 'latest' link
956rm -f ${FLAGS_output_root}/${FLAGS_board}/latest
957ln -s $(basename ${OUTPUT_DIR}) ${FLAGS_output_root}/${FLAGS_board}/latest
958
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700959echo "Done. Image created in ${OUTPUT_DIR}"
Don Garrette0020b12010-06-17 15:55:35 -0700960echo "Chromium OS image created as ${PRISTINE_IMAGE_NAME}"
J. Richard Barnetteeb5246f2011-04-08 10:34:31 -0700961if [ ${FLAGS_withdev} -eq ${FLAGS_TRUE} ]; then
Don Garrette0020b12010-06-17 15:55:35 -0700962 echo "Developer image created as ${DEVELOPER_IMAGE_NAME}"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700963fi
Vince Laviano6aebf312011-06-01 15:27:31 -0700964if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ]; then
965 echo "Factory test image created as ${CHROMEOS_FACTORY_TEST_IMAGE_NAME}"
966elif [ ${FLAGS_test} -eq ${FLAGS_TRUE} ]; then
967 echo "Test image created as ${CHROMEOS_TEST_IMAGE_NAME}"
968fi
Nick Sanders8ab729a2010-06-16 03:15:17 -0700969
Nick Sandersd2509272010-06-16 03:50:04 -0700970print_time_elapsed
971
Hung-Te Lin2ffe2be2010-10-05 08:15:40 +0800972echo "To copy to USB keyfob, do something like:"
Daniel Eratdd9818a2010-04-26 09:16:44 -0700973echo " ./image_to_usb.sh --from=${OUTSIDE_OUTPUT_DIR} --to=/dev/sdX"
Will Drewryefce6682010-07-23 19:43:27 -0500974echo "To convert to VMWare image, INSIDE the chroot, do something like:"
Olof Johansson1f5e84d2010-08-24 20:26:49 -0500975echo " ./image_to_vm.sh --from=${OUTSIDE_OUTPUT_DIR} --board=${BOARD}"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700976echo "from the scripts directory where you entered the chroot."