blob: 6fd3720b3e708fc5f0f4c1ed9b61219348d37387 [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."
Zelidrag Hornung1d12c1a2010-06-02 10:20:29 -070047DEFINE_integer rootfs_partition_size 1024 \
48 "rootfs parition size in MBs."
49DEFINE_integer rootfs_size 720 \
50 "rootfs filesystem size in MBs."
Eric Li5cf288f2010-06-28 12:46:26 -070051DEFINE_integer statefulfs_size 1024 \
52 "stateful filesystem size in MBs."
David James868d7bb2010-06-24 21:48:14 -070053DEFINE_boolean preserve ${FLAGS_FALSE} \
54 "Attempt to preserve the previous build image if one can be found (unstable, \
55kernel/firmware not updated)"
Nick Sandersf2dee6c2010-07-01 00:21:32 -070056DEFINE_boolean fast ${FLAGS_FALSE} \
57 "Call many emerges in parallel (unstable)"
58
Bill Richardsonc09b94f2010-03-15 11:40:30 -070059
60# Parse command line.
61FLAGS "$@" || exit 1
62eval set -- "${FLAGS_ARGV}"
63
64# Only now can we die on error. shflags functions leak non-zero error codes,
65# so will die prematurely if 'set -e' is specified before now.
66set -e
67
Don Garrette0020b12010-06-17 15:55:35 -070068if [ -z "${FLAGS_board}" ] ; then
Bill Richardsonc09b94f2010-03-15 11:40:30 -070069 error "--board is required."
70 exit 1
71fi
72
Don Garrette0020b12010-06-17 15:55:35 -070073if [ "${FLAGS_rootfs_size}" -gt "${FLAGS_rootfs_partition_size}" ] ; then
Zelidrag Hornung1d12c1a2010-06-02 10:20:29 -070074 error "rootfs (${FLAGS_rootfs_size} MB) is bigger than partition (${FLAGS_rootfs_partition_size} MB)."
75 exit 1
76fi
77
Nick Sanders8ab729a2010-06-16 03:15:17 -070078EMERGE_BOARD_CMD="emerge-${FLAGS_board}"
Nick Sandersf2dee6c2010-07-01 00:21:32 -070079if [ "${FLAGS_fast}" -eq "${FLAGS_TRUE}" ]; then
Nick Sanders8ab729a2010-06-16 03:15:17 -070080 echo "Using alternate emerge"
Nick Sandersf2dee6c2010-07-01 00:21:32 -070081 EMERGE_BOARD_CMD="${SCRIPTS_DIR}/parallel_emerge --board=${FLAGS_board}"
Nick Sanders8ab729a2010-06-16 03:15:17 -070082fi
83
Bill Richardsonc09b94f2010-03-15 11:40:30 -070084# Determine build version.
85. "${SCRIPTS_DIR}/chromeos_version.sh"
86
87# Use canonical path since some tools (e.g. mount) do not like symlinks.
88# Append build attempt to output directory.
89IMAGE_SUBDIR="${CHROMEOS_VERSION_STRING}-a${FLAGS_build_attempt}"
90OUTPUT_DIR="${FLAGS_output_root}/${FLAGS_board}/${IMAGE_SUBDIR}"
Don Garrett3f41e152010-06-21 14:54:34 -070091
92OUTSIDE_OUTPUT_DIR="../build/images/${FLAGS_board}/${IMAGE_SUBDIR}"
Chris Sosa9673f3b2010-05-18 13:24:40 -070093
94# If we are creating a developer image, also create a pristine image with a
95# different name.
96DEVELOPER_IMAGE_NAME=
97PRISTINE_IMAGE_NAME=chromiumos_image.bin
Don Garrette0020b12010-06-17 15:55:35 -070098if [ "${FLAGS_withdev}" -eq "${FLAGS_TRUE}" ]; then
Chris Sosa9673f3b2010-05-18 13:24:40 -070099 PRISTINE_IMAGE_NAME=chromiumos_base_image.bin
100 DEVELOPER_IMAGE_NAME=chromiumos_image.bin
101fi
Tan Gaoa40ed442010-06-02 15:45:19 -0700102
David James868d7bb2010-06-24 21:48:14 -0700103PRISTINE_IMG="${OUTPUT_DIR}/${PRISTINE_IMAGE_NAME}"
104DEVELOPER_IMG="${OUTPUT_DIR}/${DEVELOPER_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
Denis Romanov11061542010-06-29 04:23:53 +0400115OEM_FS_IMG="${OUTPUT_DIR}/partner_partition.image"
116OEM_FS_DIR="${OUTPUT_DIR}/partner_partition"
117
Don Garrett3f41e152010-06-21 14:54:34 -0700118ESP_FS_IMG=${OUTPUT_DIR}/esp.image
119ESP_FS_DIR=${OUTPUT_DIR}/esp
120
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700121LOOP_DEV=
Chris Sosa4bffb8b2010-04-07 17:23:54 -0700122STATEFUL_LOOP_DEV=
Denis Romanov11061542010-06-29 04:23:53 +0400123OEM_LOOP_DEV=
Bill Richardsona81df762010-04-09 08:12:05 -0700124ESP_LOOP_DEV=
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700125
Don Garrett3f41e152010-06-21 14:54:34 -0700126# ${DEV_IMAGE_ROOT} specifies the location of where developer packages will
127# be installed on the stateful dir. On a Chromium OS system, this will
128# translate to /usr/local.
129DEV_IMAGE_ROOT="${STATEFUL_FS_DIR}/dev_image"
130
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700131# What cross-build are we targeting?
132. "${BOARD_ROOT}/etc/make.conf.board_setup"
133LIBC_VERSION=${LIBC_VERSION:-"2.10.1-r1"}
134
Don Garrett3f41e152010-06-21 14:54:34 -0700135INSTALL_MASK=""
136if [[ ${FLAGS_installmask} -eq ${FLAGS_TRUE} ]] ; then
137 INSTALL_MASK="${DEFAULT_INSTALL_MASK}"
138fi
139
140# Reduce the size of factory install shim.
141# TODO: Build a separated ebuild for the factory install shim to reduce size.
142if [[ ${FLAGS_factory_install} -eq ${FLAGS_TRUE} ]] ; then
143 INSTALL_MASK="${INSTALL_MASK} ${FACTORY_INSTALL_MASK}"
144fi
145
146if [[ ${FLAGS_jobs} -ne -1 ]]; then
147 EMERGE_JOBS="--jobs=${FLAGS_jobs}"
148fi
149
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700150# Figure out ARCH from the given toolchain.
151# TODO: Move to common.sh as a function after scripts are switched over.
Don Garrette0020b12010-06-17 15:55:35 -0700152TC_ARCH=$(echo "${CHOST}" | awk -F'-' '{ print $1 }')
153case "${TC_ARCH}" in
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700154 arm*)
155 ARCH="arm"
156 ;;
157 *86)
158 ARCH="x86"
159 ;;
160 *)
Don Garrette0020b12010-06-17 15:55:35 -0700161 error "Unable to determine ARCH from toolchain: ${CHOST}"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700162 exit 1
163esac
164
165# Hack to fix bug where x86_64 CHOST line gets incorrectly added.
166# ToDo(msb): remove this hack.
167PACKAGES_FILE="${BOARD_ROOT}/packages/Packages"
168sudo sed -e "s/CHOST: x86_64-pc-linux-gnu//" -i "${PACKAGES_FILE}"
169
170# Handle existing directory.
Don Garrette0020b12010-06-17 15:55:35 -0700171if [[ -e "${OUTPUT_DIR}" ]]; then
172 if [[ ${FLAGS_replace} -eq ${FLAGS_TRUE} ]]; then
173 sudo rm -rf "${OUTPUT_DIR}"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700174 else
Don Garrette0020b12010-06-17 15:55:35 -0700175 echo "Directory ${OUTPUT_DIR} already exists."
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700176 echo "Use --build_attempt option to specify an unused attempt."
177 echo "Or use --replace if you want to overwrite this directory."
178 exit 1
179 fi
180fi
181
David James868d7bb2010-06-24 21:48:14 -0700182# Find previous build, if any...
183PREVIOUS_DIR=$($SCRIPTS_DIR/get_latest_image.sh --board="$BOARD")
184
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700185cleanup_rootfs_loop() {
Don Garrette0020b12010-06-17 15:55:35 -0700186 sudo umount -d "${ROOT_FS_DIR}"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700187}
188
189cleanup_stateful_fs_loop() {
Chris Sosa4bffb8b2010-04-07 17:23:54 -0700190 sudo umount "${ROOT_FS_DIR}/usr/local"
191 sudo umount "${ROOT_FS_DIR}/var"
Don Garrett3f41e152010-06-21 14:54:34 -0700192 sudo umount -d "${STATEFUL_FS_DIR}"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700193}
194
Denis Romanov11061542010-06-29 04:23:53 +0400195cleanup_oem_fs_loop() {
196 sudo umount -d "${OEM_FS_DIR}"
197}
198
199
Bill Richardson8b3bd102010-04-06 15:00:10 -0700200cleanup_esp_loop() {
Don Garrett3f41e152010-06-21 14:54:34 -0700201 sudo umount -d "${ESP_FS_DIR}"
Bill Richardson8b3bd102010-04-06 15:00:10 -0700202}
203
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700204cleanup() {
205 # Disable die on error.
206 set +e
207
Don Garrette0020b12010-06-17 15:55:35 -0700208 if [[ -n "${STATEFUL_LOOP_DEV}" ]]; then
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700209 cleanup_stateful_fs_loop
210 fi
211
Denis Romanov11061542010-06-29 04:23:53 +0400212 if [[ -n "${OEM_LOOP_DEV}" ]]; then
213 cleanup_oem_fs_loop
214 fi
215
Don Garrette0020b12010-06-17 15:55:35 -0700216 if [[ -n "${LOOP_DEV}" ]]; then
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700217 cleanup_rootfs_loop
218 fi
219
Don Garrette0020b12010-06-17 15:55:35 -0700220 if [[ -n "${ESP_LOOP_DEV}" ]]; then
Bill Richardson8b3bd102010-04-06 15:00:10 -0700221 cleanup_esp_loop
222 fi
223
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700224 # Turn die on error back on.
225 set -e
226}
227
Chris Sosabde8c1b2010-04-29 14:02:35 -0700228delete_prompt() {
229 echo "An error occurred in your build so your latest output directory" \
230 "is invalid."
231 read -p "Would you like to delete the output directory (y/N)? " SURE
Don Garrette0020b12010-06-17 15:55:35 -0700232 SURE="${SURE:0:1}" # Get just the first character.
Chris Sosabde8c1b2010-04-29 14:02:35 -0700233 if [ "${SURE}" == "y" ] ; then
Don Garrette0020b12010-06-17 15:55:35 -0700234 sudo rm -rf "${OUTPUT_DIR}"
235 echo "Deleted ${OUTPUT_DIR}"
Chris Sosabde8c1b2010-04-29 14:02:35 -0700236 else
Don Garrette0020b12010-06-17 15:55:35 -0700237 echo "Not deleting ${OUTPUT_DIR}. Note dev server updates will not work" \
Chris Sosabde8c1b2010-04-29 14:02:35 -0700238 "until you successfully build another image or delete this directory"
239 fi
240}
241
Chris Sosa9673f3b2010-05-18 13:24:40 -0700242# $1 - Directory where developer rootfs is mounted.
243# $2 - Directory where developer stateful_partition is mounted.
Don Garrett3f41e152010-06-21 14:54:34 -0700244mount_gpt_cleanup() {
Don Garrette0020b12010-06-17 15:55:35 -0700245 "${SCRIPTS_DIR}/mount_gpt_image.sh" -u -r "$1" -s "$2"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700246 delete_prompt
247}
248
Don Garrett3f41e152010-06-21 14:54:34 -0700249# Modifies an existing image to add development packages
250update_dev_packages() {
251 local image_name=$1
Chris Sosa9673f3b2010-05-18 13:24:40 -0700252
Don Garrett3f41e152010-06-21 14:54:34 -0700253 echo "Adding developer packages to ${image_name}"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700254
Don Garrett3f41e152010-06-21 14:54:34 -0700255 trap "mount_gpt_cleanup \"${ROOT_FS_DIR}\" \"${STATEFUL_FS_DIR}\"" EXIT
Tan Gaoa40ed442010-06-02 15:45:19 -0700256
Don Garrette0020b12010-06-17 15:55:35 -0700257 ${SCRIPTS_DIR}/mount_gpt_image.sh --from "${OUTPUT_DIR}" \
David James868d7bb2010-06-24 21:48:14 -0700258 --image "${image_name}" -r "${ROOT_FS_DIR}" \
Chris Sosa6c587b02010-06-21 17:36:18 -0700259 -s "${STATEFUL_FS_DIR}"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700260
Don Garrett3f41e152010-06-21 14:54:34 -0700261 # Determine the root dir for developer packages.
262 local root_dev_dir="${ROOT_FS_DIR}"
263 [ ${FLAGS_statefuldev} -eq ${FLAGS_TRUE} ] && \
264 root_dev_dir="${ROOT_FS_DIR}/usr/local"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700265
Don Garrett3f41e152010-06-21 14:54:34 -0700266 # Install developer packages described in chromeos-dev.
267 sudo INSTALL_MASK="${INSTALL_MASK}" ${EMERGE_BOARD_CMD} \
268 --root="${root_dev_dir}" --root-deps=rdeps \
David James868d7bb2010-06-24 21:48:14 -0700269 --usepkg -uDNv chromeos-dev ${EMERGE_JOBS}
270
271 if [[ $FLAGS_preserve -eq ${FLAGS_TRUE} ]] ; then
272 # Clean out unused packages
273 sudo INSTALL_MASK="${INSTALL_MASK}" ${EMERGE_BOARD_CMD} \
274 --root="${ROOT_FS_DIR}" --root-deps=rdeps \
275 --usepkg --depclean ${EMERGE_JOBS}
276 fi
Chris Sosa9673f3b2010-05-18 13:24:40 -0700277
278 # Re-run ldconfig to fix /etc/ldconfig.so.cache.
Don Garrett3f41e152010-06-21 14:54:34 -0700279 sudo /sbin/ldconfig -r "${ROOT_FS_DIR}"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700280
281 # Mark the image as a developer image (input to chromeos_startup).
Don Garrett3f41e152010-06-21 14:54:34 -0700282 sudo mkdir -p "${ROOT_FS_DIR}/root"
283 sudo touch "${ROOT_FS_DIR}/root/.dev_mode"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700284
Don Garrett3f41e152010-06-21 14:54:34 -0700285 # Additional changes to developer image.
Chris Sosa9673f3b2010-05-18 13:24:40 -0700286
Don Garrett3f41e152010-06-21 14:54:34 -0700287 # The ldd tool is a useful shell script but lives in glibc; just copy it.
288 sudo cp -a "$(which ldd)" "${root_dev_dir}/usr/bin"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700289
Don Garrett3f41e152010-06-21 14:54:34 -0700290 # If vim is installed, then a vi symlink would probably help.
291 if [[ -x "${ROOT_FS_DIR}/usr/local/bin/vim" ]]; then
292 sudo ln -sf vim "${ROOT_FS_DIR}/usr/local/bin/vi"
Girts Folkmanis7a8a8382010-05-18 22:52:25 -0700293 fi
Chris Sosa9673f3b2010-05-18 13:24:40 -0700294
Don Garrett3f41e152010-06-21 14:54:34 -0700295 # Check that the image has been correctly created. Only do it if not
296 # building a factory install image, as the INSTALL_MASK for it will
297 # make test_image fail.
298 if [[ ${FLAGS_factory_install} -eq ${FLAGS_FALSE} ]] ; then
299 "${SCRIPTS_DIR}/test_image" \
300 --root="${ROOT_FS_DIR}" \
301 --target="${ARCH}"
302 fi
303 echo "Developer image built and stored at ${image_name}"
304
Chris Sosa9673f3b2010-05-18 13:24:40 -0700305 trap - EXIT
David James868d7bb2010-06-24 21:48:14 -0700306 ${SCRIPTS_DIR}/mount_gpt_image.sh -u -r "${ROOT_FS_DIR}" \
307 -s "${STATEFUL_FS_DIR}"
308}
309
310# Update the base package on an existing image.
311update_base_packages() {
312 local image_name=$1
313
314 echo "Updating base packages on ${image_name}"
315
316 # Create stateful partition of the same size as the rootfs.
317 trap "mount_gpt_cleanup \"${ROOT_FS_DIR}\" \"${STATEFUL_FS_DIR}\"" EXIT
318
319 ${SCRIPTS_DIR}/mount_gpt_image.sh --from "${OUTPUT_DIR}" \
320 --image "${image_name}" -r "${ROOT_FS_DIR}" \
321 -s "${STATEFUL_FS_DIR}"
322
323 # Emerge updated packages, exactly like when creating base image
324 sudo INSTALL_MASK="${INSTALL_MASK}" ${EMERGE_BOARD_CMD} \
325 --root="${ROOT_FS_DIR}" --root-deps=rdeps \
326 --usepkg -uDNv chromeos ${EMERGE_JOBS}
327
328 # Clean out unused packages
329 sudo INSTALL_MASK="${INSTALL_MASK}" ${EMERGE_BOARD_CMD} \
330 --root="${ROOT_FS_DIR}" --root-deps=rdeps \
331 --usepkg --depclean ${EMERGE_JOBS}
332
333 trap - EXIT
334 ${SCRIPTS_DIR}/mount_gpt_image.sh -u -r "${ROOT_FS_DIR}" \
335 -s "${STATEFUL_FS_DIR}"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700336}
337
Don Garrett3f41e152010-06-21 14:54:34 -0700338create_base_image() {
David James868d7bb2010-06-24 21:48:14 -0700339 local image_name=$1
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700340
Don Garrett3f41e152010-06-21 14:54:34 -0700341 trap "cleanup && delete_prompt" EXIT
Chris Sosa4bffb8b2010-04-07 17:23:54 -0700342
David James868d7bb2010-06-24 21:48:14 -0700343 UUID=$(uuidgen)
344
Don Garrett3f41e152010-06-21 14:54:34 -0700345 # Create and format the root file system.
346
347 # Check for loop device before creating image.
348 LOOP_DEV=$(sudo losetup -f)
349 if [ -z "${LOOP_DEV}" ] ; then
350 echo "No free loop device. Free up a loop device or reboot. exiting. "
351 exit 1
352 fi
353
354 # Create root file system disk image to fit on a 1GB memory stick.
355 # 1 GB in hard-drive-manufacturer-speak is 10^9, not 2^30. 950MB < 10^9 bytes.
356 if [[ ${FLAGS_factory_install} -eq ${FLAGS_TRUE} ]] ; then
357 ROOT_SIZE_BYTES=$((1024 * 1024 * 300))
358 else
359 ROOT_SIZE_BYTES=$((1024 * 1024 * ${FLAGS_rootfs_size}))
360 fi
361
362 dd if=/dev/zero of="${ROOT_FS_IMG}" bs=1 count=1 seek=$((ROOT_SIZE_BYTES - 1))
363 sudo losetup "${LOOP_DEV}" "${ROOT_FS_IMG}"
364 sudo mkfs.ext3 "${LOOP_DEV}"
365
366 # Tune and mount rootfs.
Don Garrett3f41e152010-06-21 14:54:34 -0700367 DISK_LABEL="C-KEYFOB"
368 sudo tune2fs -L "${DISK_LABEL}" -U "${UUID}" -c 0 -i 0 "${LOOP_DEV}"
369 sudo mount "${LOOP_DEV}" "${ROOT_FS_DIR}"
370
371 # Create stateful partition of the same size as the rootfs.
372 STATEFUL_LOOP_DEV=$(sudo losetup -f)
373 if [ -z "${STATEFUL_LOOP_DEV}" ] ; then
374 echo "No free loop device. Free up a loop device or reboot. exiting. "
375 exit 1
376 fi
Eric Li5cf288f2010-06-28 12:46:26 -0700377 STATEFUL_SIZE_BYTES=$((1024 * 1024 * ${FLAGS_statefulfs_size}))
378 dd if=/dev/zero of="${STATEFUL_FS_IMG}" bs=1 count=1 \
379 seek=$((STATEFUL_SIZE_BYTES - 1))
Denis Romanov11061542010-06-29 04:23:53 +0400380
381 # Tune and mount the stateful partition.
382 UUID=$(uuidgen)
383 DISK_LABEL="C-STATE"
Don Garrett3f41e152010-06-21 14:54:34 -0700384 sudo losetup "${STATEFUL_LOOP_DEV}" "${STATEFUL_FS_IMG}"
385 sudo mkfs.ext3 "${STATEFUL_LOOP_DEV}"
Denis Romanov11061542010-06-29 04:23:53 +0400386 sudo tune2fs -L "${DISK_LABEL}" -U "${UUID}" -c 0 -i 0 "${STATEFUL_LOOP_DEV}"
Don Garrett3f41e152010-06-21 14:54:34 -0700387 sudo mount "${STATEFUL_LOOP_DEV}" "${STATEFUL_FS_DIR}"
388
Denis Romanov11061542010-06-29 04:23:53 +0400389 # Create OEM partner partition.
390 OEM_LOOP_DEV=$(sudo losetup -f)
391 if [ -z "${OEM_LOOP_DEV}" ] ; then
392 echo "No free loop device. Free up a loop device or reboot. exiting. "
393 exit 1
394 fi
395 OEM_SIZE_BYTES=$((1024 * 1024 * 16))
396 dd if=/dev/zero of="${OEM_FS_IMG}" bs=1 count=1 seek=$((OEM_SIZE_BYTES - 1))
397
398 # Tune and mount OEM partner partition.
399 UUID=$(uuidgen)
400 DISK_LABEL="C-OEM"
401 sudo losetup "${OEM_LOOP_DEV}" "${OEM_FS_IMG}"
402 sudo mkfs.ext3 "${OEM_LOOP_DEV}"
403 sudo tune2fs -L "${DISK_LABEL}" -U "${UUID}" -c 0 -i 0 "${OEM_LOOP_DEV}"
404 sudo mount "${OEM_LOOP_DEV}" "${OEM_FS_DIR}"
405
Don Garrett3f41e152010-06-21 14:54:34 -0700406 # Turn root file system into bootable image.
407 if [[ "${ARCH}" = "x86" ]]; then
408 # Setup extlinux configuration.
409 # TODO: For some reason the /dev/disk/by-uuid is not being generated by udev
410 # in the initramfs. When we figure that out, switch to root=UUID=${UUID}.
411 sudo mkdir -p "${ROOT_FS_DIR}"/boot
412 # TODO(adlr): use initramfs for booting.
413 cat <<EOF | sudo dd of="${ROOT_FS_DIR}"/boot/extlinux.conf
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700414DEFAULT chromeos-usb
415PROMPT 0
416TIMEOUT 0
417
418label chromeos-usb
419 menu label chromeos-usb
420 kernel vmlinuz
Bill Richardson25e4c182010-06-17 12:50:49 -0700421 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 -0700422
423label chromeos-hd
424 menu label chromeos-hd
425 kernel vmlinuz
Bill Richardson25e4c182010-06-17 12:50:49 -0700426 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 -0700427EOF
428
Don Garrett3f41e152010-06-21 14:54:34 -0700429 # Make partition bootable and label it.
430 sudo extlinux -z --install "${ROOT_FS_DIR}/boot"
431 fi
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700432
Don Garrett3f41e152010-06-21 14:54:34 -0700433 # -- Install packages into the root file system --
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700434
Don Garrett3f41e152010-06-21 14:54:34 -0700435 # We need to install libc manually from the cross toolchain.
436 # TODO: Improve this? We only want libc and not the whole toolchain.
437 PKGDIR="/var/lib/portage/pkgs/cross/"
438 sudo tar jxvpf \
439 "${PKGDIR}/${CHOST}/cross-${CHOST}"/glibc-${LIBC_VERSION}.tbz2 \
440 -C "${ROOT_FS_DIR}" --strip-components=3 \
441 --exclude=usr/include --exclude=sys-include --exclude=*.a --exclude=*.o
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700442
Don Garrett3f41e152010-06-21 14:54:34 -0700443 # We need to install libstdc++ manually from the cross toolchain.
444 # TODO: Figure out a better way of doing this?
445 sudo cp -a "${BOARD_ROOT}"/lib/libgcc_s.so* "${ROOT_FS_DIR}/lib"
446 sudo cp -a "${BOARD_ROOT}"/usr/lib/libstdc++.so* "${ROOT_FS_DIR}/usr/lib"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700447
Don Garrett3f41e152010-06-21 14:54:34 -0700448 # Prepare stateful partition with some pre-created directories.
449 sudo mkdir -p "${DEV_IMAGE_ROOT}"
450 sudo mkdir -p "${STATEFUL_FS_DIR}/var"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700451
Don Garrett3f41e152010-06-21 14:54:34 -0700452 # Create symlinks so that /usr/local/usr based directories are symlinked to
453 # /usr/local/ directories e.g. /usr/local/usr/bin -> /usr/local/bin, etc.
454 setup_symlinks_on_root "${DEV_IMAGE_ROOT}" "${STATEFUL_FS_DIR}/var" \
455 "${STATEFUL_FS_DIR}"
Tom Wai-Hong Tamf87a3672010-05-17 16:06:33 +0800456
Don Garrett3f41e152010-06-21 14:54:34 -0700457 # Perform binding rather than symlinking because directories must exist
458 # on rootfs so that we can bind at run-time since rootfs is read-only.
459 echo "Binding directories from stateful partition onto the rootfs"
460 sudo mkdir -p "${ROOT_FS_DIR}/usr/local"
461 sudo mount --bind "${DEV_IMAGE_ROOT}" "${ROOT_FS_DIR}/usr/local"
462 sudo mkdir -p "${ROOT_FS_DIR}/var"
463 sudo mount --bind "${STATEFUL_FS_DIR}/var" "${ROOT_FS_DIR}/var"
464 sudo mkdir -p "${ROOT_FS_DIR}/dev"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700465
Mandeep Singh Baines3dfa8522010-06-24 15:00:49 -0700466 # We "emerge --root=${ROOT_FS_DIR} --root-deps=rdeps --usepkg" all of the
Don Garrett3f41e152010-06-21 14:54:34 -0700467 # runtime packages for chrome os. This builds up a chrome os image from
468 # binary packages with runtime dependencies only. We use INSTALL_MASK to
469 # trim the image size as much as possible.
470 sudo INSTALL_MASK="${INSTALL_MASK}" ${EMERGE_BOARD_CMD} \
471 --root="${ROOT_FS_DIR}" --root-deps=rdeps \
Mandeep Singh Baines3dfa8522010-06-24 15:00:49 -0700472 --usepkg chromeos ${EMERGE_JOBS}
Bill Richardson8b3bd102010-04-06 15:00:10 -0700473
Don Garrett3f41e152010-06-21 14:54:34 -0700474 # Create EFI System Partition to boot stock EFI BIOS (but not ChromeOS EFI
475 # BIOS). We only need this for x86, but it's simpler and safer to keep the
476 # disk images the same for both x86 and ARM.
477 # NOTE: The size argument for mkfs.vfat is in 1024-byte blocks.
478 # We'll hard-code it to 16M for now.
479 ESP_BLOCKS=16384
480 /usr/sbin/mkfs.vfat -C ${OUTPUT_DIR}/esp.image ${ESP_BLOCKS}
481 ESP_LOOP_DEV=$(sudo losetup -f)
482 if [ -z "${ESP_LOOP_DEV}" ] ; then
483 echo "No free loop device. Free up a loop device or reboot. exiting. "
484 exit 1
485 fi
486 sudo losetup "${ESP_LOOP_DEV}" "${ESP_FS_IMG}"
487 sudo mount "${ESP_LOOP_DEV}" "${ESP_FS_DIR}"
488 sudo mkdir -p "${ESP_FS_DIR}/efi/boot"
489 sudo grub-mkimage -p /efi/boot -o "${ESP_FS_DIR}/efi/boot/bootx64.efi" \
490 part_gpt fat ext2 normal boot sh chain configfile linux
491 cat <<'EOF' | sudo dd of="${ESP_FS_DIR}/efi/boot/grub.cfg"
Bill Richardson8b3bd102010-04-06 15:00:10 -0700492set default=0
Bill Richardson9c5e5ec2010-05-14 17:00:21 -0700493set timeout=2
Bill Richardson8b3bd102010-04-06 15:00:10 -0700494
Bill Richardson9c5e5ec2010-05-14 17:00:21 -0700495# NOTE: These magic grub variables are a Chrome OS hack. They are not portable.
496
497menuentry "local image A" {
Bill Richardson25e4c182010-06-17 12:50:49 -0700498 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 -0700499}
500
Bill Richardson9c5e5ec2010-05-14 17:00:21 -0700501menuentry "local image B" {
Bill Richardson25e4c182010-06-17 12:50:49 -0700502 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 -0700503}
504
Nick Sanders9e305db2010-06-11 15:18:35 -0700505menuentry "Alternate USB Boot" {
Bill Richardson25e4c182010-06-17 12:50:49 -0700506 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 -0700507}
508
Bill Richardson8b3bd102010-04-06 15:00:10 -0700509EOF
Will Drewry69563b72010-06-24 16:12:58 -0500510 # TODO(wad) add baseline syslinux files to ESP and install the syslinux loader
Bill Richardson8b3bd102010-04-06 15:00:10 -0700511
Will Drewry69563b72010-06-24 16:12:58 -0500512 # Builds the kernel partition image. The temporary files are kept around
513 # so that we can perform a load_kernel_test later on the final image.
514 # TODO(wad) add dm-verity boot args (--boot_args, --root)
515 ${SCRIPTS_DIR}/build_kernel_image.sh \
516 --arch="${ARCH}" \
517 --to="${OUTPUT_DIR}/vmlinuz.image" \
518 --vmlinuz="${ROOT_FS_DIR}/boot/vmlinuz" \
519 --working_dir="${OUTPUT_DIR}" \
520 --keep_work \
521 --keys_dir="${SRC_ROOT}/platform/vboot_reference/tests/testkeys"
Bill Richardson67956222010-05-28 15:38:56 -0700522
Don Garrett3f41e152010-06-21 14:54:34 -0700523 # Perform any customizations on the root file system that are needed.
524 "${SCRIPTS_DIR}/customize_rootfs" \
Don Garrette0020b12010-06-17 15:55:35 -0700525 --root="${ROOT_FS_DIR}" \
Don Garrett3f41e152010-06-21 14:54:34 -0700526 --target="${ARCH}" \
527 --board="${BOARD}"
Chris Sosa503efe12010-04-08 10:05:46 -0700528
Don Garrett3f41e152010-06-21 14:54:34 -0700529 # Don't test the factory install shim.
530 if [[ ${FLAGS_factory_install} -eq ${FLAGS_FALSE} ]] ; then
531 # Check that the image has been correctly created.
532 "${SCRIPTS_DIR}/test_image" \
533 --root="${ROOT_FS_DIR}" \
534 --target="${ARCH}"
535 fi
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700536
Don Garrett3f41e152010-06-21 14:54:34 -0700537 # Clean up symlinks so they work on a running target rooted at "/".
538 # Here development packages are rooted at /usr/local. However, do not
539 # create /usr/local or /var on host (already exist on target).
540 setup_symlinks_on_root "/usr/local" "/var" "${STATEFUL_FS_DIR}"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700541
Don Garrett3f41e152010-06-21 14:54:34 -0700542 # Cleanup loop devices.
543 cleanup
Chris Sosabde8c1b2010-04-29 14:02:35 -0700544
Don Garrett3f41e152010-06-21 14:54:34 -0700545 trap delete_prompt EXIT
Tan Gao6df5aee2010-05-19 14:19:55 -0700546
Don Garrett3f41e152010-06-21 14:54:34 -0700547 # Create the GPT-formatted image.
548 ${SCRIPTS_DIR}/build_gpt.sh \
549 --arch=${ARCH} \
550 --board=${FLAGS_board} \
551 --arm_extra_bootargs="${FLAGS_arm_extra_bootargs}" \
552 --rootfs_partition_size=${FLAGS_rootfs_partition_size} \
Don Garrett3f41e152010-06-21 14:54:34 -0700553 "${OUTPUT_DIR}" \
David James868d7bb2010-06-24 21:48:14 -0700554 "${OUTPUT_DIR}/${image_name}"
555
556 trap - EXIT
557
558 # FIXME: only signing things for x86 right now.
559 if [[ "${ARCH}" = "x86" ]]; then
560 # Verify the final image.
561 load_kernel_test "${OUTPUT_DIR}/${image_name}" \
Bill Richardson2ace49e2010-07-01 10:23:27 -0700562 "${OUTPUT_DIR}/kernel_subkey.vbpubk"
David James868d7bb2010-06-24 21:48:14 -0700563 fi
Don Garrett3f41e152010-06-21 14:54:34 -0700564}
565
566# Create the output directory.
567mkdir -p "${OUTPUT_DIR}"
568mkdir -p "${ROOT_FS_DIR}"
569mkdir -p "${STATEFUL_FS_DIR}"
Denis Romanov11061542010-06-29 04:23:53 +0400570mkdir -p "${OEM_FS_DIR}"
Don Garrett3f41e152010-06-21 14:54:34 -0700571mkdir -p "${ESP_FS_DIR}"
572
David James868d7bb2010-06-24 21:48:14 -0700573# Preserve old images by copying them forward for --preserve.
574if [[ $FLAGS_preserve -eq ${FLAGS_TRUE} ]] ; then
575 if [[ -f ${PREVIOUS_DIR}/${PRISTINE_IMAGE_NAME} ]] ; then
576 # Copy forward pristine image, and associated files
577 cp ${PREVIOUS_DIR}/*.sh ${PREVIOUS_DIR}/config.txt ${OUTPUT_DIR}
578 cp ${PREVIOUS_DIR}/${PRISTINE_IMAGE_NAME} ${OUTPUT_DIR}
579
580 # Copy forward the developer image, if we already copied forward the base.
581 if [[ ${FLAGS_withdev} -eq ${FLAGS_TRUE} ]] && \
582 [[ -f ${PREVIOUS_DIR}/${DEVELOPER_IMAGE_NAME} ]] ; then
583 cp ${PREVIOUS_DIR}/${DEVELOPER_IMAGE_NAME} ${OUTPUT_DIR}
584 fi
585 fi
586fi
587
588if [[ -f ${PRISTINE_IMG} ]] ; then
589 update_base_packages ${PRISTINE_IMAGE_NAME}
590else
591 create_base_image ${PRISTINE_IMAGE_NAME}
592fi
Bill Richardson4364a2e2010-03-30 14:17:34 -0700593
Don Garrette0020b12010-06-17 15:55:35 -0700594# Create a developer image based on the chromium os base image.
Don Garrett3f41e152010-06-21 14:54:34 -0700595if [ "${FLAGS_withdev}" -eq "${FLAGS_TRUE}" ] ; then
David James868d7bb2010-06-24 21:48:14 -0700596 if [[ ! -f ${DEVELOPER_IMG} ]] ; then
597 echo "Creating developer image from base image ${PRISTINE_IMAGE_NAME}"
598 cp ${PRISTINE_IMG} ${DEVELOPER_IMG}
599 fi
Don Garrett3f41e152010-06-21 14:54:34 -0700600
David James868d7bb2010-06-24 21:48:14 -0700601 update_dev_packages ${DEVELOPER_IMAGE_NAME}
Bill Richardson6ed13582010-06-16 21:38:15 -0700602fi
603
604# Clean up temporary files.
Don Garrett3f41e152010-06-21 14:54:34 -0700605rm -f "${ROOT_FS_IMG}" "${STATEFUL_FS_IMG}" "${OUTPUT_DIR}/vmlinuz.image" \
Bill Richardson2ace49e2010-07-01 10:23:27 -0700606 "${ESP_FS_IMG}" "${OUTPUT_DIR}/kernel.keyblock" \
607 "${OUTPUT_DIR}/kernel_subkey.vbpubk" \
608 "${OUTPUT_DIR}/kernel_subkey.vbprivk" \
609 "${OUTPUT_DIR}/kernel_data_key.vbpubk" \
610 "${OUTPUT_DIR}/kernel_data_key.vbprivk" \
Denis Romanov11061542010-06-29 04:23:53 +0400611 "${OEM_FS_IMG}"
612rmdir "${ROOT_FS_DIR}" "${STATEFUL_FS_DIR}" "${OEM_FS_DIR}" "${ESP_FS_DIR}"
Bill Richardson67956222010-05-28 15:38:56 -0700613
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700614echo "Done. Image created in ${OUTPUT_DIR}"
Don Garrette0020b12010-06-17 15:55:35 -0700615echo "Chromium OS image created as ${PRISTINE_IMAGE_NAME}"
Don Garrette0020b12010-06-17 15:55:35 -0700616if [ "${FLAGS_withdev}" -eq "${FLAGS_TRUE}" ]; then
617 echo "Developer image created as ${DEVELOPER_IMAGE_NAME}"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700618fi
Nick Sanders8ab729a2010-06-16 03:15:17 -0700619
Nick Sandersd2509272010-06-16 03:50:04 -0700620print_time_elapsed
621
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700622echo "To copy to USB keyfob, OUTSIDE the chroot, do something like:"
Daniel Eratdd9818a2010-04-26 09:16:44 -0700623echo " ./image_to_usb.sh --from=${OUTSIDE_OUTPUT_DIR} --to=/dev/sdX"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700624echo "To convert to VMWare image, OUTSIDE the chroot, do something like:"
625echo " ./image_to_vmware.sh --from=${OUTSIDE_OUTPUT_DIR}"
626echo "from the scripts directory where you entered the chroot."