blob: fd73176b201676fd8de758b48ec08a483d92400f [file] [log] [blame]
Bill Richardsonc09b94f2010-03-15 11:40:30 -07001#!/bin/bash
2
3# Copyright (c) 2009 The Chromium OS Authors. All rights reserved.
4# 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
12# Load common constants. This should be the first executable line.
13# The path to common.sh should be relative to your script's location.
14. "$(dirname "$0")/common.sh"
15
16# Script must be run inside the chroot.
Don Garrett640a0582010-05-04 16:54:28 -070017restart_in_chroot_if_needed $*
Bill Richardsonc09b94f2010-03-15 11:40:30 -070018
19get_default_board
20
21# Flags.
Don Garrette0020b12010-06-17 15:55:35 -070022DEFINE_string board "${DEFAULT_BOARD}" \
Bill Richardsonc09b94f2010-03-15 11:40:30 -070023 "The board to build an image for."
24DEFINE_string build_root "/build" \
25 "The root location for board sysroots."
26DEFINE_integer build_attempt 1 \
27 "The build attempt for this image build."
28DEFINE_string output_root "${DEFAULT_BUILD_ROOT}/images" \
29 "Directory in which to place image result directories (named by version)"
Don Garrette0020b12010-06-17 15:55:35 -070030DEFINE_boolean replace ${FLAGS_FALSE} \
Bill Richardsonc09b94f2010-03-15 11:40:30 -070031 "Overwrite existing output, if any."
Don Garrette0020b12010-06-17 15:55:35 -070032DEFINE_boolean withdev ${FLAGS_TRUE} \
Bill Richardsonc09b94f2010-03-15 11:40:30 -070033 "Include useful developer friendly utilities in the image."
Don Garrette0020b12010-06-17 15:55:35 -070034DEFINE_boolean installmask ${FLAGS_TRUE} \
Bill Richardsonc09b94f2010-03-15 11:40:30 -070035 "Use INSTALL_MASK to shrink the resulting image."
36DEFINE_integer jobs -1 \
37 "How many packages to build in parallel at maximum."
Don Garrette0020b12010-06-17 15:55:35 -070038DEFINE_boolean statefuldev ${FLAGS_TRUE} \
Chris Sosa4bffb8b2010-04-07 17:23:54 -070039 "Install development packages on stateful partition rather than the rootfs"
Antoine Laboure9e585f2010-04-01 15:57:57 -070040DEFINE_string to "" \
41 "The target image file or device"
Don Garrette0020b12010-06-17 15:55:35 -070042DEFINE_boolean factory_install ${FLAGS_FALSE} \
Tom Wai-Hong Tamf87a3672010-05-17 16:06:33 +080043 "Build a smaller image to overlay the factory install shim on; this argument \
44is also required in image_to_usb."
robotboyb75eee32010-04-30 09:51:23 -070045DEFINE_string arm_extra_bootargs "" \
46 "Additional command line options to pass to the ARM kernel."
Don Garrette0020b12010-06-17 15:55:35 -070047DEFINE_boolean recovery ${FLAGS_FALSE} \
Tan Gao6df5aee2010-05-19 14:19:55 -070048 "Build a recovery image. Default: False."
Zelidrag Hornung1d12c1a2010-06-02 10:20:29 -070049DEFINE_integer rootfs_partition_size 1024 \
50 "rootfs parition size in MBs."
51DEFINE_integer rootfs_size 720 \
52 "rootfs filesystem size in MBs."
Bill Richardsonc09b94f2010-03-15 11:40:30 -070053
54# Parse command line.
55FLAGS "$@" || exit 1
56eval set -- "${FLAGS_ARGV}"
57
58# Only now can we die on error. shflags functions leak non-zero error codes,
59# so will die prematurely if 'set -e' is specified before now.
60set -e
61
Don Garrette0020b12010-06-17 15:55:35 -070062if [ -z "${FLAGS_board}" ] ; then
Bill Richardsonc09b94f2010-03-15 11:40:30 -070063 error "--board is required."
64 exit 1
65fi
66
Don Garrette0020b12010-06-17 15:55:35 -070067if [ "${FLAGS_rootfs_size}" -gt "${FLAGS_rootfs_partition_size}" ] ; then
Zelidrag Hornung1d12c1a2010-06-02 10:20:29 -070068 error "rootfs (${FLAGS_rootfs_size} MB) is bigger than partition (${FLAGS_rootfs_partition_size} MB)."
69 exit 1
70fi
71
Nick Sanders8ab729a2010-06-16 03:15:17 -070072EMERGE_CMD="emerge"
73EMERGE_BOARD_CMD="emerge-${FLAGS_board}"
74TOP_SCRIPTS_DIR="$(dirname $0)"
75if [ -e "${TOP_SCRIPTS_DIR}/.emerge" ]; then
76 echo "Using alternate emerge"
77 . "${TOP_SCRIPTS_DIR}/.emerge"
78fi
79
Bill Richardsonc09b94f2010-03-15 11:40:30 -070080# Determine build version.
81. "${SCRIPTS_DIR}/chromeos_version.sh"
82
83# Use canonical path since some tools (e.g. mount) do not like symlinks.
84# Append build attempt to output directory.
85IMAGE_SUBDIR="${CHROMEOS_VERSION_STRING}-a${FLAGS_build_attempt}"
86OUTPUT_DIR="${FLAGS_output_root}/${FLAGS_board}/${IMAGE_SUBDIR}"
Don Garrett3f41e152010-06-21 14:54:34 -070087
88OUTSIDE_OUTPUT_DIR="../build/images/${FLAGS_board}/${IMAGE_SUBDIR}"
Chris Sosa9673f3b2010-05-18 13:24:40 -070089
90# If we are creating a developer image, also create a pristine image with a
91# different name.
92DEVELOPER_IMAGE_NAME=
93PRISTINE_IMAGE_NAME=chromiumos_image.bin
Don Garrette0020b12010-06-17 15:55:35 -070094if [ "${FLAGS_withdev}" -eq "${FLAGS_TRUE}" ]; then
Chris Sosa9673f3b2010-05-18 13:24:40 -070095 PRISTINE_IMAGE_NAME=chromiumos_base_image.bin
96 DEVELOPER_IMAGE_NAME=chromiumos_image.bin
97fi
Tan Gaoa40ed442010-06-02 15:45:19 -070098
Don Garrette0020b12010-06-17 15:55:35 -070099# If we are creating a recovery image, rename pristine image.
100if [ "${FLAGS_recovery}" -eq "${FLAGS_TRUE}" ]; then
Tan Gaoa40ed442010-06-02 15:45:19 -0700101 PRISTINE_IMAGE_NAME=recovery_image.bin
102fi
103
Chris Sosa9673f3b2010-05-18 13:24:40 -0700104OUTPUT_IMG=${FLAGS_to:-${OUTPUT_DIR}/${PRISTINE_IMAGE_NAME}}
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700105
106BOARD="${FLAGS_board}"
107BOARD_ROOT="${FLAGS_build_root}/${BOARD}"
108
Don Garrett3f41e152010-06-21 14:54:34 -0700109ROOT_FS_IMG="${OUTPUT_DIR}/rootfs.image"
110ROOT_FS_DIR="${OUTPUT_DIR}/rootfs"
111
112STATEFUL_FS_IMG="${OUTPUT_DIR}/stateful_partition.image"
113STATEFUL_FS_DIR="${OUTPUT_DIR}/stateful_partition"
114
115ESP_FS_IMG=${OUTPUT_DIR}/esp.image
116ESP_FS_DIR=${OUTPUT_DIR}/esp
117
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700118LOOP_DEV=
Chris Sosa4bffb8b2010-04-07 17:23:54 -0700119STATEFUL_LOOP_DEV=
Bill Richardsona81df762010-04-09 08:12:05 -0700120ESP_LOOP_DEV=
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700121
Don Garrett3f41e152010-06-21 14:54:34 -0700122# ${DEV_IMAGE_ROOT} specifies the location of where developer packages will
123# be installed on the stateful dir. On a Chromium OS system, this will
124# translate to /usr/local.
125DEV_IMAGE_ROOT="${STATEFUL_FS_DIR}/dev_image"
126
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700127# What cross-build are we targeting?
128. "${BOARD_ROOT}/etc/make.conf.board_setup"
129LIBC_VERSION=${LIBC_VERSION:-"2.10.1-r1"}
130
Don Garrett3f41e152010-06-21 14:54:34 -0700131INSTALL_MASK=""
132if [[ ${FLAGS_installmask} -eq ${FLAGS_TRUE} ]] ; then
133 INSTALL_MASK="${DEFAULT_INSTALL_MASK}"
134fi
135
136# Reduce the size of factory install shim.
137# TODO: Build a separated ebuild for the factory install shim to reduce size.
138if [[ ${FLAGS_factory_install} -eq ${FLAGS_TRUE} ]] ; then
139 INSTALL_MASK="${INSTALL_MASK} ${FACTORY_INSTALL_MASK}"
140fi
141
142if [[ ${FLAGS_jobs} -ne -1 ]]; then
143 EMERGE_JOBS="--jobs=${FLAGS_jobs}"
144fi
145
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700146# Figure out ARCH from the given toolchain.
147# TODO: Move to common.sh as a function after scripts are switched over.
Don Garrette0020b12010-06-17 15:55:35 -0700148TC_ARCH=$(echo "${CHOST}" | awk -F'-' '{ print $1 }')
149case "${TC_ARCH}" in
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700150 arm*)
151 ARCH="arm"
152 ;;
153 *86)
154 ARCH="x86"
155 ;;
156 *)
Don Garrette0020b12010-06-17 15:55:35 -0700157 error "Unable to determine ARCH from toolchain: ${CHOST}"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700158 exit 1
159esac
160
161# Hack to fix bug where x86_64 CHOST line gets incorrectly added.
162# ToDo(msb): remove this hack.
163PACKAGES_FILE="${BOARD_ROOT}/packages/Packages"
164sudo sed -e "s/CHOST: x86_64-pc-linux-gnu//" -i "${PACKAGES_FILE}"
165
166# Handle existing directory.
Don Garrette0020b12010-06-17 15:55:35 -0700167if [[ -e "${OUTPUT_DIR}" ]]; then
168 if [[ ${FLAGS_replace} -eq ${FLAGS_TRUE} ]]; then
169 sudo rm -rf "${OUTPUT_DIR}"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700170 else
Don Garrette0020b12010-06-17 15:55:35 -0700171 echo "Directory ${OUTPUT_DIR} already exists."
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700172 echo "Use --build_attempt option to specify an unused attempt."
173 echo "Or use --replace if you want to overwrite this directory."
174 exit 1
175 fi
176fi
177
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700178cleanup_rootfs_loop() {
Don Garrette0020b12010-06-17 15:55:35 -0700179 sudo umount -d "${ROOT_FS_DIR}"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700180}
181
182cleanup_stateful_fs_loop() {
Chris Sosa4bffb8b2010-04-07 17:23:54 -0700183 sudo umount "${ROOT_FS_DIR}/usr/local"
184 sudo umount "${ROOT_FS_DIR}/var"
Don Garrett3f41e152010-06-21 14:54:34 -0700185 sudo umount -d "${STATEFUL_FS_DIR}"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700186}
187
Bill Richardson8b3bd102010-04-06 15:00:10 -0700188cleanup_esp_loop() {
Don Garrett3f41e152010-06-21 14:54:34 -0700189 sudo umount -d "${ESP_FS_DIR}"
Bill Richardson8b3bd102010-04-06 15:00:10 -0700190}
191
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700192cleanup() {
193 # Disable die on error.
194 set +e
195
Don Garrette0020b12010-06-17 15:55:35 -0700196 if [[ -n "${STATEFUL_LOOP_DEV}" ]]; then
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700197 cleanup_stateful_fs_loop
198 fi
199
Don Garrette0020b12010-06-17 15:55:35 -0700200 if [[ -n "${LOOP_DEV}" ]]; then
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700201 cleanup_rootfs_loop
202 fi
203
Don Garrette0020b12010-06-17 15:55:35 -0700204 if [[ -n "${ESP_LOOP_DEV}" ]]; then
Bill Richardson8b3bd102010-04-06 15:00:10 -0700205 cleanup_esp_loop
206 fi
207
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700208 # Turn die on error back on.
209 set -e
210}
211
Chris Sosabde8c1b2010-04-29 14:02:35 -0700212delete_prompt() {
213 echo "An error occurred in your build so your latest output directory" \
214 "is invalid."
215 read -p "Would you like to delete the output directory (y/N)? " SURE
Don Garrette0020b12010-06-17 15:55:35 -0700216 SURE="${SURE:0:1}" # Get just the first character.
Chris Sosabde8c1b2010-04-29 14:02:35 -0700217 if [ "${SURE}" == "y" ] ; then
Don Garrette0020b12010-06-17 15:55:35 -0700218 sudo rm -rf "${OUTPUT_DIR}"
219 echo "Deleted ${OUTPUT_DIR}"
Chris Sosabde8c1b2010-04-29 14:02:35 -0700220 else
Don Garrette0020b12010-06-17 15:55:35 -0700221 echo "Not deleting ${OUTPUT_DIR}. Note dev server updates will not work" \
Chris Sosabde8c1b2010-04-29 14:02:35 -0700222 "until you successfully build another image or delete this directory"
223 fi
224}
225
Chris Sosa9673f3b2010-05-18 13:24:40 -0700226# $1 - Directory where developer rootfs is mounted.
227# $2 - Directory where developer stateful_partition is mounted.
Don Garrett3f41e152010-06-21 14:54:34 -0700228mount_gpt_cleanup() {
Don Garrette0020b12010-06-17 15:55:35 -0700229 "${SCRIPTS_DIR}/mount_gpt_image.sh" -u -r "$1" -s "$2"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700230 delete_prompt
231}
232
Don Garrett3f41e152010-06-21 14:54:34 -0700233# Modifies an existing image to add development packages
234update_dev_packages() {
235 local image_name=$1
Chris Sosa9673f3b2010-05-18 13:24:40 -0700236
Don Garrett3f41e152010-06-21 14:54:34 -0700237 echo "Adding developer packages to ${image_name}"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700238
Don Garrett3f41e152010-06-21 14:54:34 -0700239 trap "mount_gpt_cleanup \"${ROOT_FS_DIR}\" \"${STATEFUL_FS_DIR}\"" EXIT
Tan Gaoa40ed442010-06-02 15:45:19 -0700240
Don Garrette0020b12010-06-17 15:55:35 -0700241 ${SCRIPTS_DIR}/mount_gpt_image.sh --from "${OUTPUT_DIR}" \
Don Garrett3f41e152010-06-21 14:54:34 -0700242 --image "${image_name}" -r "${ROOT_FS_DIR}" -s "${STATEFUL_FS_DIR}"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700243
Don Garrett3f41e152010-06-21 14:54:34 -0700244 # Determine the root dir for developer packages.
245 local root_dev_dir="${ROOT_FS_DIR}"
246 [ ${FLAGS_statefuldev} -eq ${FLAGS_TRUE} ] && \
247 root_dev_dir="${ROOT_FS_DIR}/usr/local"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700248
Don Garrett3f41e152010-06-21 14:54:34 -0700249 # Install developer packages described in chromeos-dev.
250 sudo INSTALL_MASK="${INSTALL_MASK}" ${EMERGE_BOARD_CMD} \
251 --root="${root_dev_dir}" --root-deps=rdeps \
252 --usepkgonly chromeos-dev ${EMERGE_JOBS}
Chris Sosa9673f3b2010-05-18 13:24:40 -0700253
254 # Re-run ldconfig to fix /etc/ldconfig.so.cache.
Don Garrett3f41e152010-06-21 14:54:34 -0700255 sudo /sbin/ldconfig -r "${ROOT_FS_DIR}"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700256
257 # Mark the image as a developer image (input to chromeos_startup).
Don Garrett3f41e152010-06-21 14:54:34 -0700258 sudo mkdir -p "${ROOT_FS_DIR}/root"
259 sudo touch "${ROOT_FS_DIR}/root/.dev_mode"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700260
Don Garrett3f41e152010-06-21 14:54:34 -0700261 # Additional changes to developer image.
Chris Sosa9673f3b2010-05-18 13:24:40 -0700262
Don Garrett3f41e152010-06-21 14:54:34 -0700263 # The ldd tool is a useful shell script but lives in glibc; just copy it.
264 sudo cp -a "$(which ldd)" "${root_dev_dir}/usr/bin"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700265
Don Garrett3f41e152010-06-21 14:54:34 -0700266 # If vim is installed, then a vi symlink would probably help.
267 if [[ -x "${ROOT_FS_DIR}/usr/local/bin/vim" ]]; then
268 sudo ln -sf vim "${ROOT_FS_DIR}/usr/local/bin/vi"
Girts Folkmanis7a8a8382010-05-18 22:52:25 -0700269 fi
Chris Sosa9673f3b2010-05-18 13:24:40 -0700270
Don Garrett3f41e152010-06-21 14:54:34 -0700271 # Check that the image has been correctly created. Only do it if not
272 # building a factory install image, as the INSTALL_MASK for it will
273 # make test_image fail.
274 if [[ ${FLAGS_factory_install} -eq ${FLAGS_FALSE} ]] ; then
275 "${SCRIPTS_DIR}/test_image" \
276 --root="${ROOT_FS_DIR}" \
277 --target="${ARCH}"
278 fi
279 echo "Developer image built and stored at ${image_name}"
280
Chris Sosa9673f3b2010-05-18 13:24:40 -0700281 trap - EXIT
Don Garrett3f41e152010-06-21 14:54:34 -0700282 ${SCRIPTS_DIR}/mount_gpt_image.sh -u -r "${ROOT_FS_DIR}" -s "${STATEFUL_FS_DIR}"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700283}
284
Chris Sosa4bffb8b2010-04-07 17:23:54 -0700285
Don Garrett3f41e152010-06-21 14:54:34 -0700286# Modifies an existing image to add recovery packages
287update_recovery_packages() {
288 local image_name=$1
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700289
Don Garrett3f41e152010-06-21 14:54:34 -0700290 echo "Adding recovery packages to ${image_name}"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700291
Don Garrett3f41e152010-06-21 14:54:34 -0700292 # Create stateful partition of the same size as the rootfs.
293 trap "mount_gpt_cleanup \"${ROOT_FS_DIR}\" \"${STATEFUL_FS_DIR}\"" EXIT
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700294
Don Garrett3f41e152010-06-21 14:54:34 -0700295 ${SCRIPTS_DIR}/mount_gpt_image.sh --from "${OUTPUT_DIR}" \
296 --image "${image_name}" -r "${ROOT_FS_DIR}" -s "${STATEFUL_FS_DIR}"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700297
Don Garrett3f41e152010-06-21 14:54:34 -0700298 # Install recovery installer.
299 sudo ${EMERGE_BOARD_CMD} --root=${ROOT_FS_DIR} --usepkgonly \
300 --root-deps=rdeps --nodeps chromeos-recovery
Tom Wai-Hong Tamf87a3672010-05-17 16:06:33 +0800301
Don Garrett3f41e152010-06-21 14:54:34 -0700302 # Re-run ldconfig to fix /etc/ldconfig.so.cache.
303 sudo /sbin/ldconfig -r "${ROOT_FS_DIR}"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700304
Don Garrett3f41e152010-06-21 14:54:34 -0700305 # Mark the image as a developer image (input to chromeos_startup).
306 sudo mkdir -p "${ROOT_FS_DIR}/root"
307 sudo touch "${ROOT_FS_DIR}/root/.recovery_installer"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700308
Don Garrett3f41e152010-06-21 14:54:34 -0700309 trap - EXIT
310 ${SCRIPTS_DIR}/mount_gpt_image.sh -u -r "${ROOT_FS_DIR}" -s "${STATEFUL_FS_DIR}"
311}
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700312
Don Garrett3f41e152010-06-21 14:54:34 -0700313create_base_image() {
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700314
Don Garrett3f41e152010-06-21 14:54:34 -0700315 trap "cleanup && delete_prompt" EXIT
Chris Sosa4bffb8b2010-04-07 17:23:54 -0700316
Don Garrett3f41e152010-06-21 14:54:34 -0700317 # Create and format the root file system.
318
319 # Check for loop device before creating image.
320 LOOP_DEV=$(sudo losetup -f)
321 if [ -z "${LOOP_DEV}" ] ; then
322 echo "No free loop device. Free up a loop device or reboot. exiting. "
323 exit 1
324 fi
325
326 # Create root file system disk image to fit on a 1GB memory stick.
327 # 1 GB in hard-drive-manufacturer-speak is 10^9, not 2^30. 950MB < 10^9 bytes.
328 if [[ ${FLAGS_factory_install} -eq ${FLAGS_TRUE} ]] ; then
329 ROOT_SIZE_BYTES=$((1024 * 1024 * 300))
330 else
331 ROOT_SIZE_BYTES=$((1024 * 1024 * ${FLAGS_rootfs_size}))
332 fi
333
334 dd if=/dev/zero of="${ROOT_FS_IMG}" bs=1 count=1 seek=$((ROOT_SIZE_BYTES - 1))
335 sudo losetup "${LOOP_DEV}" "${ROOT_FS_IMG}"
336 sudo mkfs.ext3 "${LOOP_DEV}"
337
338 # Tune and mount rootfs.
339 UUID=$(uuidgen)
340 DISK_LABEL="C-KEYFOB"
341 sudo tune2fs -L "${DISK_LABEL}" -U "${UUID}" -c 0 -i 0 "${LOOP_DEV}"
342 sudo mount "${LOOP_DEV}" "${ROOT_FS_DIR}"
343
344 # Create stateful partition of the same size as the rootfs.
345 STATEFUL_LOOP_DEV=$(sudo losetup -f)
346 if [ -z "${STATEFUL_LOOP_DEV}" ] ; then
347 echo "No free loop device. Free up a loop device or reboot. exiting. "
348 exit 1
349 fi
350 dd if=/dev/zero of="${STATEFUL_FS_IMG}" bs=1 count=1 seek=$((ROOT_SIZE_BYTES - 1))
351 sudo losetup "${STATEFUL_LOOP_DEV}" "${STATEFUL_FS_IMG}"
352 sudo mkfs.ext3 "${STATEFUL_LOOP_DEV}"
353 sudo tune2fs -L "C-STATE" -U "${UUID}" -c 0 -i 0 \
354 "${STATEFUL_LOOP_DEV}"
355
356 # Mount the stateful partition.
357 sudo mount "${STATEFUL_LOOP_DEV}" "${STATEFUL_FS_DIR}"
358
359 # Turn root file system into bootable image.
360 if [[ "${ARCH}" = "x86" ]]; then
361 # Setup extlinux configuration.
362 # TODO: For some reason the /dev/disk/by-uuid is not being generated by udev
363 # in the initramfs. When we figure that out, switch to root=UUID=${UUID}.
364 sudo mkdir -p "${ROOT_FS_DIR}"/boot
365 # TODO(adlr): use initramfs for booting.
366 cat <<EOF | sudo dd of="${ROOT_FS_DIR}"/boot/extlinux.conf
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700367DEFAULT chromeos-usb
368PROMPT 0
369TIMEOUT 0
370
371label chromeos-usb
372 menu label chromeos-usb
373 kernel vmlinuz
Bill Richardson25e4c182010-06-17 12:50:49 -0700374 append quiet console=tty2 init=/sbin/init boot=local rootwait root=/dev/sdb3 ro noresume noswap i915.modeset=1 loglevel=1 cros_legacy
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700375
376label chromeos-hd
377 menu label chromeos-hd
378 kernel vmlinuz
Bill Richardson25e4c182010-06-17 12:50:49 -0700379 append quiet console=tty2 init=/sbin/init boot=local rootwait root=HDROOT ro noresume noswap i915.modeset=1 loglevel=1 cros_legacy
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700380EOF
381
Don Garrett3f41e152010-06-21 14:54:34 -0700382 # Make partition bootable and label it.
383 sudo extlinux -z --install "${ROOT_FS_DIR}/boot"
384 fi
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700385
Don Garrett3f41e152010-06-21 14:54:34 -0700386 # -- Install packages into the root file system --
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700387
Don Garrett3f41e152010-06-21 14:54:34 -0700388 # We need to install libc manually from the cross toolchain.
389 # TODO: Improve this? We only want libc and not the whole toolchain.
390 PKGDIR="/var/lib/portage/pkgs/cross/"
391 sudo tar jxvpf \
392 "${PKGDIR}/${CHOST}/cross-${CHOST}"/glibc-${LIBC_VERSION}.tbz2 \
393 -C "${ROOT_FS_DIR}" --strip-components=3 \
394 --exclude=usr/include --exclude=sys-include --exclude=*.a --exclude=*.o
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700395
Don Garrett3f41e152010-06-21 14:54:34 -0700396 # We need to install libstdc++ manually from the cross toolchain.
397 # TODO: Figure out a better way of doing this?
398 sudo cp -a "${BOARD_ROOT}"/lib/libgcc_s.so* "${ROOT_FS_DIR}/lib"
399 sudo cp -a "${BOARD_ROOT}"/usr/lib/libstdc++.so* "${ROOT_FS_DIR}/usr/lib"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700400
Don Garrett3f41e152010-06-21 14:54:34 -0700401 # Prepare stateful partition with some pre-created directories.
402 sudo mkdir -p "${DEV_IMAGE_ROOT}"
403 sudo mkdir -p "${STATEFUL_FS_DIR}/var"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700404
Don Garrett3f41e152010-06-21 14:54:34 -0700405 # Create symlinks so that /usr/local/usr based directories are symlinked to
406 # /usr/local/ directories e.g. /usr/local/usr/bin -> /usr/local/bin, etc.
407 setup_symlinks_on_root "${DEV_IMAGE_ROOT}" "${STATEFUL_FS_DIR}/var" \
408 "${STATEFUL_FS_DIR}"
Tom Wai-Hong Tamf87a3672010-05-17 16:06:33 +0800409
Don Garrett3f41e152010-06-21 14:54:34 -0700410 # Perform binding rather than symlinking because directories must exist
411 # on rootfs so that we can bind at run-time since rootfs is read-only.
412 echo "Binding directories from stateful partition onto the rootfs"
413 sudo mkdir -p "${ROOT_FS_DIR}/usr/local"
414 sudo mount --bind "${DEV_IMAGE_ROOT}" "${ROOT_FS_DIR}/usr/local"
415 sudo mkdir -p "${ROOT_FS_DIR}/var"
416 sudo mount --bind "${STATEFUL_FS_DIR}/var" "${ROOT_FS_DIR}/var"
417 sudo mkdir -p "${ROOT_FS_DIR}/dev"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700418
Don Garrett3f41e152010-06-21 14:54:34 -0700419 # We "emerge --root=${ROOT_FS_DIR} --root-deps=rdeps --usepkgonly" all of the
420 # runtime packages for chrome os. This builds up a chrome os image from
421 # binary packages with runtime dependencies only. We use INSTALL_MASK to
422 # trim the image size as much as possible.
423 sudo INSTALL_MASK="${INSTALL_MASK}" ${EMERGE_BOARD_CMD} \
424 --root="${ROOT_FS_DIR}" --root-deps=rdeps \
425 --usepkgonly chromeos ${EMERGE_JOBS}
Bill Richardson8b3bd102010-04-06 15:00:10 -0700426
Don Garrett3f41e152010-06-21 14:54:34 -0700427 # Create EFI System Partition to boot stock EFI BIOS (but not ChromeOS EFI
428 # BIOS). We only need this for x86, but it's simpler and safer to keep the
429 # disk images the same for both x86 and ARM.
430 # NOTE: The size argument for mkfs.vfat is in 1024-byte blocks.
431 # We'll hard-code it to 16M for now.
432 ESP_BLOCKS=16384
433 /usr/sbin/mkfs.vfat -C ${OUTPUT_DIR}/esp.image ${ESP_BLOCKS}
434 ESP_LOOP_DEV=$(sudo losetup -f)
435 if [ -z "${ESP_LOOP_DEV}" ] ; then
436 echo "No free loop device. Free up a loop device or reboot. exiting. "
437 exit 1
438 fi
439 sudo losetup "${ESP_LOOP_DEV}" "${ESP_FS_IMG}"
440 sudo mount "${ESP_LOOP_DEV}" "${ESP_FS_DIR}"
441 sudo mkdir -p "${ESP_FS_DIR}/efi/boot"
442 sudo grub-mkimage -p /efi/boot -o "${ESP_FS_DIR}/efi/boot/bootx64.efi" \
443 part_gpt fat ext2 normal boot sh chain configfile linux
444 cat <<'EOF' | sudo dd of="${ESP_FS_DIR}/efi/boot/grub.cfg"
Bill Richardson8b3bd102010-04-06 15:00:10 -0700445set default=0
Bill Richardson9c5e5ec2010-05-14 17:00:21 -0700446set timeout=2
Bill Richardson8b3bd102010-04-06 15:00:10 -0700447
Bill Richardson9c5e5ec2010-05-14 17:00:21 -0700448# NOTE: These magic grub variables are a Chrome OS hack. They are not portable.
449
450menuentry "local image A" {
Bill Richardson25e4c182010-06-17 12:50:49 -0700451 linux $grubpartA/boot/vmlinuz quiet console=tty2 init=/sbin/init boot=local rootwait root=/dev/$linuxpartA ro noresume noswap i915.modeset=1 loglevel=1 cros_efi
Bill Richardsona81df762010-04-09 08:12:05 -0700452}
453
Bill Richardson9c5e5ec2010-05-14 17:00:21 -0700454menuentry "local image B" {
Bill Richardson25e4c182010-06-17 12:50:49 -0700455 linux $grubpartB/boot/vmlinuz quiet console=tty2 init=/sbin/init boot=local rootwait root=/dev/$linuxpartB ro noresume noswap i915.modeset=1 loglevel=1 cros_efi
Bill Richardson041bd712010-04-27 09:36:14 -0700456}
457
Nick Sanders9e305db2010-06-11 15:18:35 -0700458menuentry "Alternate USB Boot" {
Bill Richardson25e4c182010-06-17 12:50:49 -0700459 linux (hd0,3)/boot/vmlinuz quiet console=tty2 init=/sbin/init boot=local rootwait root=/dev/sdb3 ro noresume noswap i915.modeset=1 loglevel=1 cros_efi
Nick Sanders9e305db2010-06-11 15:18:35 -0700460}
461
Bill Richardson8b3bd102010-04-06 15:00:10 -0700462EOF
463
Don Garrett3f41e152010-06-21 14:54:34 -0700464 # FIXME: At the moment, we're working on signed images for x86 only. ARM will
465 # support this before shipping, but at the moment they don't.
466 if [[ "${ARCH}" = "x86" ]]; then
Don Garrette0020b12010-06-17 15:55:35 -0700467
Don Garrett3f41e152010-06-21 14:54:34 -0700468 # Legacy BIOS will use the kernel in the rootfs (via syslinux), as will
469 # standard EFI BIOS (via grub, from the EFI System Partition). Chrome OS
470 # BIOS will use a separate signed kernel partition, which we'll create now.
471 # FIXME: remove serial output, debugging messages.
472 cat <<'EOF' > "${OUTPUT_DIR}/config.txt"
Bill Richardson67956222010-05-28 15:38:56 -0700473earlyprintk=serial,ttyS0,115200
474console=ttyS0,115200
475init=/sbin/init
476add_efi_memmap
477boot=local
478rootwait
479root=/dev/sd%D%P
480ro
481noresume
482noswap
483i915.modeset=1
484loglevel=7
Bill Richardson25e4c182010-06-17 12:50:49 -0700485cros_secure
Bill Richardson67956222010-05-28 15:38:56 -0700486EOF
Don Garrette0020b12010-06-17 15:55:35 -0700487
Don Garrett3f41e152010-06-21 14:54:34 -0700488 # FIXME: We need to specify the real keys and certs here!
489 SIG_DIR="${SRC_ROOT}/platform/vboot_reference/tests/testkeys"
Bill Richardson6ed13582010-06-16 21:38:15 -0700490
Don Garrett3f41e152010-06-21 14:54:34 -0700491 # Wrap the public keys with VbPublicKey headers.
492 vbutil_key --pack \
493 --in "${SIG_DIR}/key_rsa2048.keyb" \
494 --version 1 --algorithm 4 \
495 --out "${OUTPUT_DIR}/key_alg4.vbpubk"
Bill Richardson6ed13582010-06-16 21:38:15 -0700496
Don Garrett3f41e152010-06-21 14:54:34 -0700497 vbutil_key --pack \
498 --in "${SIG_DIR}/key_rsa4096.keyb" \
499 --version 1 --algorithm 8 \
500 --out "${OUTPUT_DIR}/key_alg8.vbpubk"
Bill Richardson6ed13582010-06-16 21:38:15 -0700501
Don Garrett3f41e152010-06-21 14:54:34 -0700502 vbutil_keyblock --pack "${OUTPUT_DIR}/data4_sign8.keyblock" \
503 --datapubkey "${OUTPUT_DIR}/key_alg4.vbpubk" \
504 --signprivate "${SIG_DIR}/key_rsa4096.pem" \
505 --algorithm 8 --flags 3
Don Garrette0020b12010-06-17 15:55:35 -0700506
Don Garrett3f41e152010-06-21 14:54:34 -0700507 # Verify the keyblock.
508 vbutil_keyblock --unpack "${OUTPUT_DIR}/data4_sign8.keyblock" \
509 --signpubkey "${OUTPUT_DIR}/key_alg8.vbpubk"
Bill Richardson6ed13582010-06-16 21:38:15 -0700510
Don Garrett3f41e152010-06-21 14:54:34 -0700511 # Sign the kernel:
512 vbutil_kernel --pack "${OUTPUT_DIR}/vmlinuz.image" \
513 --keyblock "${OUTPUT_DIR}/data4_sign8.keyblock" \
514 --signprivate "${SIG_DIR}/key_rsa2048.pem" \
515 --version 1 \
516 --config "${OUTPUT_DIR}/config.txt" \
517 --bootloader /lib64/bootstub/bootstub.efi \
518 --vmlinuz "${ROOT_FS_DIR}/boot/vmlinuz"
Don Garrette0020b12010-06-17 15:55:35 -0700519
Don Garrett3f41e152010-06-21 14:54:34 -0700520 # And verify it.
521 vbutil_kernel --verify "${OUTPUT_DIR}/vmlinuz.image" \
522 --signpubkey "${OUTPUT_DIR}/key_alg8.vbpubk"
Bill Richardson6ed13582010-06-16 21:38:15 -0700523
Don Garrett3f41e152010-06-21 14:54:34 -0700524 else
525 # FIXME: For now, ARM just uses the unsigned kernel by itself.
526 cp -f "${ROOT_FS_DIR}/boot/vmlinuz" "${OUTPUT_DIR}/vmlinuz.image"
527 fi
Bill Richardson67956222010-05-28 15:38:56 -0700528
Bill Richardson67956222010-05-28 15:38:56 -0700529
Don Garrett3f41e152010-06-21 14:54:34 -0700530 # Perform any customizations on the root file system that are needed.
531 "${SCRIPTS_DIR}/customize_rootfs" \
Don Garrette0020b12010-06-17 15:55:35 -0700532 --root="${ROOT_FS_DIR}" \
Don Garrett3f41e152010-06-21 14:54:34 -0700533 --target="${ARCH}" \
534 --board="${BOARD}"
Chris Sosa503efe12010-04-08 10:05:46 -0700535
Don Garrett3f41e152010-06-21 14:54:34 -0700536 # Don't test the factory install shim.
537 if [[ ${FLAGS_factory_install} -eq ${FLAGS_FALSE} ]] ; then
538 # Check that the image has been correctly created.
539 "${SCRIPTS_DIR}/test_image" \
540 --root="${ROOT_FS_DIR}" \
541 --target="${ARCH}"
542 fi
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700543
Don Garrett3f41e152010-06-21 14:54:34 -0700544 # Clean up symlinks so they work on a running target rooted at "/".
545 # Here development packages are rooted at /usr/local. However, do not
546 # create /usr/local or /var on host (already exist on target).
547 setup_symlinks_on_root "/usr/local" "/var" "${STATEFUL_FS_DIR}"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700548
Don Garrett3f41e152010-06-21 14:54:34 -0700549 # Cleanup loop devices.
550 cleanup
Chris Sosabde8c1b2010-04-29 14:02:35 -0700551
Don Garrett3f41e152010-06-21 14:54:34 -0700552 trap delete_prompt EXIT
Tan Gao6df5aee2010-05-19 14:19:55 -0700553
Don Garrett3f41e152010-06-21 14:54:34 -0700554 RECOVERY="--norecovery"
555 if [[ ${FLAGS_recovery} -eq ${FLAGS_TRUE} ]]; then
556 RECOVERY="--recovery"
557 fi
558
559 # Create the GPT-formatted image.
560 ${SCRIPTS_DIR}/build_gpt.sh \
561 --arch=${ARCH} \
562 --board=${FLAGS_board} \
563 --arm_extra_bootargs="${FLAGS_arm_extra_bootargs}" \
564 --rootfs_partition_size=${FLAGS_rootfs_partition_size} \
565 ${RECOVERY} \
566 "${OUTPUT_DIR}" \
567 "${OUTPUT_IMG}"
568}
569
570# Create the output directory.
571mkdir -p "${OUTPUT_DIR}"
572mkdir -p "${ROOT_FS_DIR}"
573mkdir -p "${STATEFUL_FS_DIR}"
574mkdir -p "${ESP_FS_DIR}"
575
576create_base_image ${OUTPUT_DIR}/${PRISTINE_IMAGE_NAME}
Bill Richardson4364a2e2010-03-30 14:17:34 -0700577
Don Garrette0020b12010-06-17 15:55:35 -0700578# Create a recovery image based on the chromium os base image.
Don Garrett3f41e152010-06-21 14:54:34 -0700579if [ "${FLAGS_recovery}" -eq "${FLAGS_TRUE}" ] ; then
580 update_recovery_packages ${OUTPUT_DIR}/${PRISTINE_IMAGE_NAME}
581fi
Tan Gaoa40ed442010-06-02 15:45:19 -0700582trap - EXIT
583
Don Garrett3f41e152010-06-21 14:54:34 -0700584
Don Garrette0020b12010-06-17 15:55:35 -0700585# Create a developer image based on the chromium os base image.
Don Garrett3f41e152010-06-21 14:54:34 -0700586if [ "${FLAGS_withdev}" -eq "${FLAGS_TRUE}" ] ; then
587 echo "Creating developer image from base image ${OUTPUT_IMG}"
588 cp ${OUTPUT_DIR}/${PRISTINE_IMAGE_NAME} ${OUTPUT_DIR}/${DEVELOPER_IMAGE_NAME}
589 update_dev_packages ${OUTPUT_DIR}/${DEVELOPER_IMAGE_NAME}
590fi
591
Chris Sosa9673f3b2010-05-18 13:24:40 -0700592trap - EXIT
593
Bill Richardson6ed13582010-06-16 21:38:15 -0700594# FIXME: only signing things for x86 right now.
Don Garrette0020b12010-06-17 15:55:35 -0700595if [[ "${ARCH}" = "x86" ]]; then
596 # Verify the final image.
Bill Richardson6ed13582010-06-16 21:38:15 -0700597 load_kernel_test "${OUTPUT_IMG}" "${OUTPUT_DIR}/key_alg8.vbpubk"
598fi
599
600# Clean up temporary files.
Don Garrett3f41e152010-06-21 14:54:34 -0700601rm -f "${ROOT_FS_IMG}" "${STATEFUL_FS_IMG}" "${OUTPUT_DIR}/vmlinuz.image" \
602 "${ESP_FS_IMG}" "${OUTPUT_DIR}/data4_sign8.keyblock" \
Don Garrette0020b12010-06-17 15:55:35 -0700603 "${OUTPUT_DIR}/key_alg4.vbpubk" "${OUTPUT_DIR}/key_alg8.vbpubk"
Don Garrett3f41e152010-06-21 14:54:34 -0700604rmdir "${ROOT_FS_DIR}" "${STATEFUL_FS_DIR}" "${ESP_FS_DIR}"
Bill Richardson67956222010-05-28 15:38:56 -0700605
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700606echo "Done. Image created in ${OUTPUT_DIR}"
Don Garrette0020b12010-06-17 15:55:35 -0700607echo "Chromium OS image created as ${PRISTINE_IMAGE_NAME}"
608if [ "${FLAGS_recovery}" -eq "${FLAGS_TRUE}" ]; then
609 echo "Recovery image created as ${PRISTINE_IMAGE_NAME}"
Tan Gaoa40ed442010-06-02 15:45:19 -0700610fi
Don Garrette0020b12010-06-17 15:55:35 -0700611if [ "${FLAGS_withdev}" -eq "${FLAGS_TRUE}" ]; then
612 echo "Developer image created as ${DEVELOPER_IMAGE_NAME}"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700613fi
Nick Sanders8ab729a2010-06-16 03:15:17 -0700614
Nick Sandersd2509272010-06-16 03:50:04 -0700615print_time_elapsed
616
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700617echo "To copy to USB keyfob, OUTSIDE the chroot, do something like:"
Daniel Eratdd9818a2010-04-26 09:16:44 -0700618echo " ./image_to_usb.sh --from=${OUTSIDE_OUTPUT_DIR} --to=/dev/sdX"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700619echo "To convert to VMWare image, OUTSIDE the chroot, do something like:"
620echo " ./image_to_vmware.sh --from=${OUTSIDE_OUTPUT_DIR}"
621echo "from the scripts directory where you entered the chroot."