blob: d97f75c88596639ab6722bc1d3e26b791881cd65 [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
Will Drewry4a675e12010-07-03 13:35:02 -050016. "$(dirname "$0")/chromeos-common.sh" # for partoffset
17locate_gpt
18
Bill Richardsonc09b94f2010-03-15 11:40:30 -070019# Script must be run inside the chroot.
Don Garrett640a0582010-05-04 16:54:28 -070020restart_in_chroot_if_needed $*
Bill Richardsonc09b94f2010-03-15 11:40:30 -070021
22get_default_board
23
24# Flags.
Don Garrette0020b12010-06-17 15:55:35 -070025DEFINE_string board "${DEFAULT_BOARD}" \
Bill Richardsonc09b94f2010-03-15 11:40:30 -070026 "The board to build an image for."
27DEFINE_string build_root "/build" \
28 "The root location for board sysroots."
29DEFINE_integer build_attempt 1 \
30 "The build attempt for this image build."
31DEFINE_string output_root "${DEFAULT_BUILD_ROOT}/images" \
32 "Directory in which to place image result directories (named by version)"
Don Garrette0020b12010-06-17 15:55:35 -070033DEFINE_boolean replace ${FLAGS_FALSE} \
Bill Richardsonc09b94f2010-03-15 11:40:30 -070034 "Overwrite existing output, if any."
Don Garrette0020b12010-06-17 15:55:35 -070035DEFINE_boolean withdev ${FLAGS_TRUE} \
Bill Richardsonc09b94f2010-03-15 11:40:30 -070036 "Include useful developer friendly utilities in the image."
Don Garrette0020b12010-06-17 15:55:35 -070037DEFINE_boolean installmask ${FLAGS_TRUE} \
Bill Richardsonc09b94f2010-03-15 11:40:30 -070038 "Use INSTALL_MASK to shrink the resulting image."
39DEFINE_integer jobs -1 \
40 "How many packages to build in parallel at maximum."
Don Garrette0020b12010-06-17 15:55:35 -070041DEFINE_boolean statefuldev ${FLAGS_TRUE} \
Chris Sosa4bffb8b2010-04-07 17:23:54 -070042 "Install development packages on stateful partition rather than the rootfs"
Antoine Laboure9e585f2010-04-01 15:57:57 -070043DEFINE_string to "" \
44 "The target image file or device"
Don Garrette0020b12010-06-17 15:55:35 -070045DEFINE_boolean factory_install ${FLAGS_FALSE} \
Tom Wai-Hong Tamf87a3672010-05-17 16:06:33 +080046 "Build a smaller image to overlay the factory install shim on; this argument \
47is also required in image_to_usb."
robotboyb75eee32010-04-30 09:51:23 -070048DEFINE_string arm_extra_bootargs "" \
49 "Additional command line options to pass to the ARM kernel."
Zelidrag Hornung1d12c1a2010-06-02 10:20:29 -070050DEFINE_integer rootfs_partition_size 1024 \
51 "rootfs parition size in MBs."
52DEFINE_integer rootfs_size 720 \
53 "rootfs filesystem size in MBs."
Will Drewry78992a32010-07-21 14:02:20 -050054# ceil(0.1 * rootfs_size) is a good minimum.
55DEFINE_integer rootfs_hash_pad 8 \
56 "MBs reserved at the end of the rootfs image."
Eric Li5cf288f2010-06-28 12:46:26 -070057DEFINE_integer statefulfs_size 1024 \
58 "stateful filesystem size in MBs."
David James868d7bb2010-06-24 21:48:14 -070059DEFINE_boolean preserve ${FLAGS_FALSE} \
60 "Attempt to preserve the previous build image if one can be found (unstable, \
61kernel/firmware not updated)"
Nick Sandersf2dee6c2010-07-01 00:21:32 -070062DEFINE_boolean fast ${FLAGS_FALSE} \
63 "Call many emerges in parallel (unstable)"
64
Will Drewry4a675e12010-07-03 13:35:02 -050065DEFINE_string usb_disk /dev/sdb3 \
66 "Path syslinux should use to do a usb boot. Default: /dev/sdb3"
67
Will Drewry1670d482010-07-09 13:08:38 -070068DEFINE_boolean enable_rootfs_verification ${FLAGS_FALSE} \
69 "Default all bootloaders to use kernel-based root fs integrity checking."
70DEFINE_integer verity_error_behavior 2 \
71 "Kernel verified boot error behavior (0: I/O errors, 1: reboot, 2: nothing) \
Will Drewry4a675e12010-07-03 13:35:02 -050072Default: 2"
Will Drewry1670d482010-07-09 13:08:38 -070073DEFINE_integer verity_depth 1 \
74 "Kernel verified boot hash tree depth. Default: 1"
75DEFINE_integer verity_max_ios 1024 \
Will Drewry4a675e12010-07-03 13:35:02 -050076 "Number of outstanding I/O operations dm-verity caps at. Default: 1024"
Will Drewry1670d482010-07-09 13:08:38 -070077DEFINE_string verity_algorithm "sha1" \
78 "Cryptographic hash algorithm used for kernel vboot. Default : sha1"
Bill Richardsonc09b94f2010-03-15 11:40:30 -070079
Denis Romanov95bdf472010-07-12 22:44:42 +040080DEFINE_string oem_customization \
81 "$GCLIENT_ROOT/src/platform/assets/oem_customization" \
82 "Path to directory containing OEM partner partition contents"
83
Bill Richardsonc09b94f2010-03-15 11:40:30 -070084# Parse command line.
85FLAGS "$@" || exit 1
86eval set -- "${FLAGS_ARGV}"
87
88# Only now can we die on error. shflags functions leak non-zero error codes,
89# so will die prematurely if 'set -e' is specified before now.
90set -e
91
Don Garrette0020b12010-06-17 15:55:35 -070092if [ -z "${FLAGS_board}" ] ; then
Bill Richardsonc09b94f2010-03-15 11:40:30 -070093 error "--board is required."
94 exit 1
95fi
96
Will Drewry78992a32010-07-21 14:02:20 -050097if [ "$((FLAGS_rootfs_size + FLAGS_rootfs_hash_pad))" -gt \
98 "${FLAGS_rootfs_partition_size}" ] ; then
99 error "rootfs ($((FLAGS_rootfs_size + FLAGS_rootfs_hash_pad)) MB) is \
100bigger than partition (${FLAGS_rootfs_partition_size} MB)."
Zelidrag Hornung1d12c1a2010-06-02 10:20:29 -0700101 exit 1
102fi
103
Nick Sanders8ab729a2010-06-16 03:15:17 -0700104EMERGE_BOARD_CMD="emerge-${FLAGS_board}"
Nick Sandersf2dee6c2010-07-01 00:21:32 -0700105if [ "${FLAGS_fast}" -eq "${FLAGS_TRUE}" ]; then
Nick Sanders8ab729a2010-06-16 03:15:17 -0700106 echo "Using alternate emerge"
Nick Sandersf2dee6c2010-07-01 00:21:32 -0700107 EMERGE_BOARD_CMD="${SCRIPTS_DIR}/parallel_emerge --board=${FLAGS_board}"
Nick Sanders8ab729a2010-06-16 03:15:17 -0700108fi
109
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700110# Determine build version.
111. "${SCRIPTS_DIR}/chromeos_version.sh"
112
113# Use canonical path since some tools (e.g. mount) do not like symlinks.
114# Append build attempt to output directory.
115IMAGE_SUBDIR="${CHROMEOS_VERSION_STRING}-a${FLAGS_build_attempt}"
116OUTPUT_DIR="${FLAGS_output_root}/${FLAGS_board}/${IMAGE_SUBDIR}"
Don Garrett3f41e152010-06-21 14:54:34 -0700117
118OUTSIDE_OUTPUT_DIR="../build/images/${FLAGS_board}/${IMAGE_SUBDIR}"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700119
120# If we are creating a developer image, also create a pristine image with a
121# different name.
122DEVELOPER_IMAGE_NAME=
123PRISTINE_IMAGE_NAME=chromiumos_image.bin
Don Garrette0020b12010-06-17 15:55:35 -0700124if [ "${FLAGS_withdev}" -eq "${FLAGS_TRUE}" ]; then
Chris Sosa9673f3b2010-05-18 13:24:40 -0700125 PRISTINE_IMAGE_NAME=chromiumos_base_image.bin
126 DEVELOPER_IMAGE_NAME=chromiumos_image.bin
127fi
Tan Gaoa40ed442010-06-02 15:45:19 -0700128
David James868d7bb2010-06-24 21:48:14 -0700129PRISTINE_IMG="${OUTPUT_DIR}/${PRISTINE_IMAGE_NAME}"
130DEVELOPER_IMG="${OUTPUT_DIR}/${DEVELOPER_IMAGE_NAME}"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700131
132BOARD="${FLAGS_board}"
133BOARD_ROOT="${FLAGS_build_root}/${BOARD}"
134
Don Garrett3f41e152010-06-21 14:54:34 -0700135ROOT_FS_IMG="${OUTPUT_DIR}/rootfs.image"
136ROOT_FS_DIR="${OUTPUT_DIR}/rootfs"
Will Drewry4a675e12010-07-03 13:35:02 -0500137ROOT_FS_HASH="${OUTPUT_DIR}/rootfs.hash"
Don Garrett3f41e152010-06-21 14:54:34 -0700138
139STATEFUL_FS_IMG="${OUTPUT_DIR}/stateful_partition.image"
140STATEFUL_FS_DIR="${OUTPUT_DIR}/stateful_partition"
141
Denis Romanov11061542010-06-29 04:23:53 +0400142OEM_FS_IMG="${OUTPUT_DIR}/partner_partition.image"
143OEM_FS_DIR="${OUTPUT_DIR}/partner_partition"
144
Don Garrett3f41e152010-06-21 14:54:34 -0700145ESP_FS_IMG=${OUTPUT_DIR}/esp.image
146ESP_FS_DIR=${OUTPUT_DIR}/esp
147
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700148LOOP_DEV=
Chris Sosa4bffb8b2010-04-07 17:23:54 -0700149STATEFUL_LOOP_DEV=
Denis Romanov11061542010-06-29 04:23:53 +0400150OEM_LOOP_DEV=
Bill Richardsona81df762010-04-09 08:12:05 -0700151ESP_LOOP_DEV=
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700152
Don Garrett3f41e152010-06-21 14:54:34 -0700153# ${DEV_IMAGE_ROOT} specifies the location of where developer packages will
154# be installed on the stateful dir. On a Chromium OS system, this will
155# translate to /usr/local.
156DEV_IMAGE_ROOT="${STATEFUL_FS_DIR}/dev_image"
157
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700158# What cross-build are we targeting?
159. "${BOARD_ROOT}/etc/make.conf.board_setup"
160LIBC_VERSION=${LIBC_VERSION:-"2.10.1-r1"}
161
Don Garrett3f41e152010-06-21 14:54:34 -0700162INSTALL_MASK=""
163if [[ ${FLAGS_installmask} -eq ${FLAGS_TRUE} ]] ; then
164 INSTALL_MASK="${DEFAULT_INSTALL_MASK}"
165fi
166
167# Reduce the size of factory install shim.
168# TODO: Build a separated ebuild for the factory install shim to reduce size.
169if [[ ${FLAGS_factory_install} -eq ${FLAGS_TRUE} ]] ; then
170 INSTALL_MASK="${INSTALL_MASK} ${FACTORY_INSTALL_MASK}"
171fi
172
David Jamesfd7403a2010-07-09 12:00:17 -0700173if [[ ${FLAGS_jobs} -ne -1 ]]; then
174 EMERGE_JOBS="--jobs=${FLAGS_jobs}"
175fi
176
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700177# Figure out ARCH from the given toolchain.
178# TODO: Move to common.sh as a function after scripts are switched over.
Don Garrette0020b12010-06-17 15:55:35 -0700179TC_ARCH=$(echo "${CHOST}" | awk -F'-' '{ print $1 }')
180case "${TC_ARCH}" in
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700181 arm*)
182 ARCH="arm"
183 ;;
184 *86)
185 ARCH="x86"
186 ;;
187 *)
Don Garrette0020b12010-06-17 15:55:35 -0700188 error "Unable to determine ARCH from toolchain: ${CHOST}"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700189 exit 1
190esac
191
192# Hack to fix bug where x86_64 CHOST line gets incorrectly added.
193# ToDo(msb): remove this hack.
194PACKAGES_FILE="${BOARD_ROOT}/packages/Packages"
195sudo sed -e "s/CHOST: x86_64-pc-linux-gnu//" -i "${PACKAGES_FILE}"
196
197# Handle existing directory.
Don Garrette0020b12010-06-17 15:55:35 -0700198if [[ -e "${OUTPUT_DIR}" ]]; then
199 if [[ ${FLAGS_replace} -eq ${FLAGS_TRUE} ]]; then
200 sudo rm -rf "${OUTPUT_DIR}"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700201 else
Don Garrette0020b12010-06-17 15:55:35 -0700202 echo "Directory ${OUTPUT_DIR} already exists."
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700203 echo "Use --build_attempt option to specify an unused attempt."
204 echo "Or use --replace if you want to overwrite this directory."
205 exit 1
206 fi
207fi
208
David James868d7bb2010-06-24 21:48:14 -0700209# Find previous build, if any...
210PREVIOUS_DIR=$($SCRIPTS_DIR/get_latest_image.sh --board="$BOARD")
211
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700212cleanup_rootfs_loop() {
Don Garrette0020b12010-06-17 15:55:35 -0700213 sudo umount -d "${ROOT_FS_DIR}"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700214}
215
216cleanup_stateful_fs_loop() {
Chris Sosa4bffb8b2010-04-07 17:23:54 -0700217 sudo umount "${ROOT_FS_DIR}/usr/local"
218 sudo umount "${ROOT_FS_DIR}/var"
Don Garrett3f41e152010-06-21 14:54:34 -0700219 sudo umount -d "${STATEFUL_FS_DIR}"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700220}
221
Denis Romanov11061542010-06-29 04:23:53 +0400222cleanup_oem_fs_loop() {
223 sudo umount -d "${OEM_FS_DIR}"
224}
225
226
Bill Richardson8b3bd102010-04-06 15:00:10 -0700227cleanup_esp_loop() {
Don Garrett3f41e152010-06-21 14:54:34 -0700228 sudo umount -d "${ESP_FS_DIR}"
Bill Richardson8b3bd102010-04-06 15:00:10 -0700229}
230
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700231cleanup() {
232 # Disable die on error.
233 set +e
234
Don Garrette0020b12010-06-17 15:55:35 -0700235 if [[ -n "${STATEFUL_LOOP_DEV}" ]]; then
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700236 cleanup_stateful_fs_loop
Will Drewry4a675e12010-07-03 13:35:02 -0500237 STATEFUL_LOOP_DEV=
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700238 fi
239
Denis Romanov11061542010-06-29 04:23:53 +0400240 if [[ -n "${OEM_LOOP_DEV}" ]]; then
241 cleanup_oem_fs_loop
242 fi
243
Don Garrette0020b12010-06-17 15:55:35 -0700244 if [[ -n "${LOOP_DEV}" ]]; then
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700245 cleanup_rootfs_loop
Will Drewry4a675e12010-07-03 13:35:02 -0500246 LOOP_DEV=
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700247 fi
248
Don Garrette0020b12010-06-17 15:55:35 -0700249 if [[ -n "${ESP_LOOP_DEV}" ]]; then
Bill Richardson8b3bd102010-04-06 15:00:10 -0700250 cleanup_esp_loop
Will Drewry4a675e12010-07-03 13:35:02 -0500251 ESP_LOOP_DEV=
Bill Richardson8b3bd102010-04-06 15:00:10 -0700252 fi
253
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700254 # Turn die on error back on.
255 set -e
256}
257
Chris Sosabde8c1b2010-04-29 14:02:35 -0700258delete_prompt() {
259 echo "An error occurred in your build so your latest output directory" \
260 "is invalid."
261 read -p "Would you like to delete the output directory (y/N)? " SURE
Don Garrette0020b12010-06-17 15:55:35 -0700262 SURE="${SURE:0:1}" # Get just the first character.
Chris Sosabde8c1b2010-04-29 14:02:35 -0700263 if [ "${SURE}" == "y" ] ; then
Don Garrette0020b12010-06-17 15:55:35 -0700264 sudo rm -rf "${OUTPUT_DIR}"
265 echo "Deleted ${OUTPUT_DIR}"
Chris Sosabde8c1b2010-04-29 14:02:35 -0700266 else
Don Garrette0020b12010-06-17 15:55:35 -0700267 echo "Not deleting ${OUTPUT_DIR}. Note dev server updates will not work" \
Chris Sosabde8c1b2010-04-29 14:02:35 -0700268 "until you successfully build another image or delete this directory"
269 fi
270}
271
Chris Sosa9673f3b2010-05-18 13:24:40 -0700272# $1 - Directory where developer rootfs is mounted.
273# $2 - Directory where developer stateful_partition is mounted.
Will Drewry4a675e12010-07-03 13:35:02 -0500274# $3 - Directory where the ESP partition is mounted.
Don Garrett3f41e152010-06-21 14:54:34 -0700275mount_gpt_cleanup() {
Will Drewry4a675e12010-07-03 13:35:02 -0500276 local rootfs="${1-$ROOT_FS_DIR}"
277 local statefs="${2-$STATEFUL_FS_DIR}"
278 local espfs="${3-$ESP_FS_DIR}"
279 "${SCRIPTS_DIR}/mount_gpt_image.sh" \
280 -u -r "${rootfs}" -s "${statefs}" -e "${espfs}"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700281 delete_prompt
282}
283
Will Drewry4a675e12010-07-03 13:35:02 -0500284make_image_bootable() {
285 local image_name="$1"
286 cros_root=/dev/sd%D%P
287 if [[ "${ARCH}" = "arm" ]]; then
288 # TODO(wad) assumed like in build_gpt for now.
289 cros_root=/dev/mmcblk1p3
290 fi
Will Drewry1670d482010-07-09 13:08:38 -0700291 if [[ ${FLAGS_enable_rootfs_verification} -eq ${FLAGS_TRUE} ]]; then
Will Drewry4a675e12010-07-03 13:35:02 -0500292 cros_root=/dev/dm-0
293 fi
294
295 # TODO(wad) mount the root fs to LOOP_DEV from the image
296 trap "mount_gpt_cleanup" EXIT
297 ${SCRIPTS_DIR}/mount_gpt_image.sh --from "${OUTPUT_DIR}" \
298 --image "${image_name}" -r "${ROOT_FS_DIR}" \
Will Drewry721d94f2010-07-16 14:39:45 -0500299 -s "${STATEFUL_FS_DIR}"
Will Drewry4a675e12010-07-03 13:35:02 -0500300
Will Drewry78992a32010-07-21 14:02:20 -0500301 # The rootfs should never be mounted rw again after this point without
302 # re-calling make_image_bootable.
Will Drewry4a675e12010-07-03 13:35:02 -0500303 sudo mount -o remount,ro "${ROOT_FS_DIR}"
Will Drewry78992a32010-07-21 14:02:20 -0500304 root_dev=$(mount | grep -- "on ${ROOT_FS_DIR} type" | cut -f1 -d' ' | tail -1)
Will Drewry4a675e12010-07-03 13:35:02 -0500305
Mandeep Singh Baines87b16942010-07-09 20:52:41 -0700306 DEVKEYSDIR="/usr/share/vboot/devkeys"
Louis Yung-Chieh Lo36020402010-07-05 13:23:34 +0800307
Will Drewry4a675e12010-07-03 13:35:02 -0500308 # Builds the kernel partition image. The temporary files are kept around
309 # so that we can perform a load_kernel_test later on the final image.
310 ${SCRIPTS_DIR}/build_kernel_image.sh \
311 --arch="${ARCH}" \
312 --to="${OUTPUT_DIR}/vmlinuz.image" \
Louis Yung-Chieh Lo36020402010-07-05 13:23:34 +0800313 --hd_vblock="${OUTPUT_DIR}/vmlinuz_hd.vblock" \
Will Drewry4a675e12010-07-03 13:35:02 -0500314 --vmlinuz="${OUTPUT_DIR}/boot/vmlinuz" \
315 --working_dir="${OUTPUT_DIR}" \
316 --keep_work \
317 --rootfs_image=${root_dev} \
Will Drewry78992a32010-07-21 14:02:20 -0500318 --rootfs_hash=${ROOT_FS_HASH} \
Will Drewry1670d482010-07-09 13:08:38 -0700319 --verity_hash_alg=${FLAGS_verity_algorithm} \
320 --verity_tree_depth=${FLAGS_verity_depth} \
321 --verity_max_ios=${FLAGS_verity_max_ios} \
322 --verity_error_behavior=${FLAGS_verity_error_behavior} \
Will Drewry4a675e12010-07-03 13:35:02 -0500323 --root=${cros_root} \
Louis Yung-Chieh Lo36020402010-07-05 13:23:34 +0800324 --keys_dir="${DEVKEYSDIR}"
Will Drewry4a675e12010-07-03 13:35:02 -0500325
Will Drewry78992a32010-07-21 14:02:20 -0500326 local rootfs_hash_size=$(stat -c '%s' ${ROOT_FS_HASH})
327 info "Appending rootfs.hash (${rootfs_hash_size} bytes) to the root fs"
328 if [[ ${rootfs_hash_size} -gt $((FLAGS_rootfs_hash_pad * 1024 * 1024)) ]]
329 then
330 die "--rootfs_hash_pad reserves less than the needed ${rootfs_hash_size}"
331 fi
332 # Unfortunately, mount_gpt_image uses mount and not losetup to create the
333 # loop devices. This means that they are not the correct size. We have to
334 # write directly to the image to append the hash tree data.
335 local hash_offset="$(partoffset ${OUTPUT_DIR}/${image_name} 3)"
336 hash_offset=$((hash_offset + ((1024 * 1024 * ${FLAGS_rootfs_size}) / 512)))
337 sudo dd bs=512 \
338 seek=${hash_offset} \
339 if="${ROOT_FS_HASH}" \
340 of="${OUTPUT_DIR}/${image_name}" \
341 conv=notrunc
342 # We don't need to keep the file around anymore.
343 sudo rm "${ROOT_FS_HASH}"
344
Louis Yung-Chieh Loca1c2b02010-07-05 17:37:05 +0800345 # Move the verification block needed for the hard disk install to the
346 # stateful partition. Mount stateful fs, copy file, and umount fs.
347 # In original CL: http://codereview.chromium.org/2868044, this was done in
348 # create_base_image(). However, it could break the build if it is a clean
349 # build because vmlinuz_hd.vblock hasn't been created by build_kernel_image.sh
Louis Yung-Chieh Lo85d49fc2010-07-05 22:55:31 +0800350 if [[ "${ARCH}" = "x86" ]]; then
Louis Yung-Chieh Lo85d49fc2010-07-05 22:55:31 +0800351 sudo cp "${OUTPUT_DIR}/vmlinuz_hd.vblock" "${STATEFUL_FS_DIR}"
Louis Yung-Chieh Loca1c2b02010-07-05 17:37:05 +0800352 fi
Louis Yung-Chieh Loca1c2b02010-07-05 17:37:05 +0800353
Will Drewry4a675e12010-07-03 13:35:02 -0500354 # START_KERN_A is set by the first call to install the gpt.
355 local koffset="$(partoffset ${OUTPUT_DIR}/${image_name} 2)"
356 sudo dd if="${OUTPUT_DIR}/vmlinuz.image" of="${OUTPUT_DIR}/${image_name}" \
357 conv=notrunc bs=512 seek=${koffset}
358
Will Drewry07e35052010-07-09 14:50:42 -0700359 # Update the bootloaders. For legacy/efi x86, the EFI system partition
360 # will be updated and for arm, the mbr will be updated (for u-boot).
Will Drewry721d94f2010-07-16 14:39:45 -0500361 local kernel_part=
362 local bootloader_to=
363 local bootloader_to_flags=
Will Drewry4a675e12010-07-03 13:35:02 -0500364 local usb_disk="${FLAGS_usb_disk}"
Will Drewry721d94f2010-07-16 14:39:45 -0500365
366 if [[ "${ARCH}" = "x86" ]]; then
367 # x86 should update the esp in place in the image.
368 bootloader_to="${OUTPUT_DIR}/${image_name}"
369 local esp_offset="$(partoffset ${OUTPUT_DIR}/${image_name} 12)"
370 esp_offset=$((esp_offset * 512)) # sectors to bytes
371 local esp_size="$(partsize ${OUTPUT_DIR}/${image_name} 12)"
372 esp_size=$((esp_size * 512)) # sectors to bytes
373 bootloader_to_flags="--to_offset=${esp_offset} --to_size=${esp_size}"
374 # Use the kernel partition to acquire configuration flags.
375 kernel_part="--kernel_partition='${OUTPUT_DIR}/vmlinuz.image'"
376 # Install syslinux on the EFI System Partition.
377 kernel_part="${kernel_part} --install_syslinux"
378 elif [[ "${ARCH}" = "arm" ]]; then
Will Drewry4a675e12010-07-03 13:35:02 -0500379 # TODO(wad) mmcblk1p3 is hardcoded for arm for now!
380 usb_disk="/dev/mmcblk1p3"
Will Drewry721d94f2010-07-16 14:39:45 -0500381 # ARM doesn't support using the kernel image for kernel cmdline flags yet.
Will Drewry82780e52010-07-03 18:27:10 -0700382 kernel_part="--kernel_cmdline=\"${FLAGS_arm_extra_bootargs}\" "
383 # TODO(wad) Integrate dmtable extraction into the arm build
384 # E.g. $(cat ${OUTPUT_DIR}/boot.config | tr -s '\n' ' ')"
Will Drewry4a675e12010-07-03 13:35:02 -0500385 local kpart_offset="--kernel_partition_offset=${koffset}"
Will Drewry82780e52010-07-03 18:27:10 -0700386 local kpart_size="--kernel_partition_sectors="
387 kpart_size="${kpart_size}$(partsize ${OUTPUT_DIR}/${image_name} 2)"
Will Drewry4a675e12010-07-03 13:35:02 -0500388 kernel_part="${kernel_part} ${kpart_size} ${kpart_offset}"
Will Drewry82780e52010-07-03 18:27:10 -0700389 info "Using addition bootloader arguments: ${kernel_part}"
Will Drewry4a675e12010-07-03 13:35:02 -0500390 bootloader_to="${OUTPUT_DIR}/arm.mbr"
391 fi
392
393 # Update partition 12 / legacy bootloaders and arm.
394 ${SCRIPTS_DIR}/update_bootloaders.sh \
395 --arch=${ARCH} \
396 --to="${bootloader_to}" \
397 --from="${OUTPUT_DIR}"/boot \
398 --vmlinuz="${OUTPUT_DIR}"/boot/vmlinuz \
399 --usb_disk="${usb_disk}" \
Will Drewry721d94f2010-07-16 14:39:45 -0500400 ${bootloader_to_flags} \
Will Drewry4a675e12010-07-03 13:35:02 -0500401 $kernel_part
402
403 if [[ "${ARCH}" == "arm" ]]; then
404 sudo dd bs=1 conv=notrunc if="${bootloader_to}" \
405 of="${OUTPUT_DIR}/${image_name}"
406 sudo rm "${bootloader_to}"
407 fi
408
409 trap - EXIT
410 ${SCRIPTS_DIR}/mount_gpt_image.sh -u -r "${ROOT_FS_DIR}" \
Andrew de los Reyes3172b7e2010-07-15 22:10:47 -0700411 -s "${STATEFUL_FS_DIR}"
Will Drewry4a675e12010-07-03 13:35:02 -0500412}
413
Don Garrett3f41e152010-06-21 14:54:34 -0700414# Modifies an existing image to add development packages
415update_dev_packages() {
416 local image_name=$1
Chris Sosa9673f3b2010-05-18 13:24:40 -0700417
Don Garrett3f41e152010-06-21 14:54:34 -0700418 echo "Adding developer packages to ${image_name}"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700419
Will Drewry4a675e12010-07-03 13:35:02 -0500420 trap "mount_gpt_cleanup" EXIT
Tan Gaoa40ed442010-06-02 15:45:19 -0700421
Don Garrette0020b12010-06-17 15:55:35 -0700422 ${SCRIPTS_DIR}/mount_gpt_image.sh --from "${OUTPUT_DIR}" \
David James868d7bb2010-06-24 21:48:14 -0700423 --image "${image_name}" -r "${ROOT_FS_DIR}" \
Will Drewry4a675e12010-07-03 13:35:02 -0500424 -s "${STATEFUL_FS_DIR}" -e "${ESP_FS_DIR}"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700425
Don Garrett3f41e152010-06-21 14:54:34 -0700426 # Determine the root dir for developer packages.
427 local root_dev_dir="${ROOT_FS_DIR}"
428 [ ${FLAGS_statefuldev} -eq ${FLAGS_TRUE} ] && \
429 root_dev_dir="${ROOT_FS_DIR}/usr/local"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700430
Don Garrett3f41e152010-06-21 14:54:34 -0700431 # Install developer packages described in chromeos-dev.
432 sudo INSTALL_MASK="${INSTALL_MASK}" ${EMERGE_BOARD_CMD} \
433 --root="${root_dev_dir}" --root-deps=rdeps \
David James868d7bb2010-06-24 21:48:14 -0700434 --usepkg -uDNv chromeos-dev ${EMERGE_JOBS}
435
436 if [[ $FLAGS_preserve -eq ${FLAGS_TRUE} ]] ; then
437 # Clean out unused packages
438 sudo INSTALL_MASK="${INSTALL_MASK}" ${EMERGE_BOARD_CMD} \
439 --root="${ROOT_FS_DIR}" --root-deps=rdeps \
440 --usepkg --depclean ${EMERGE_JOBS}
441 fi
Chris Sosa9673f3b2010-05-18 13:24:40 -0700442
443 # Re-run ldconfig to fix /etc/ldconfig.so.cache.
Don Garrett3f41e152010-06-21 14:54:34 -0700444 sudo /sbin/ldconfig -r "${ROOT_FS_DIR}"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700445
446 # Mark the image as a developer image (input to chromeos_startup).
Don Garrett3f41e152010-06-21 14:54:34 -0700447 sudo mkdir -p "${ROOT_FS_DIR}/root"
448 sudo touch "${ROOT_FS_DIR}/root/.dev_mode"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700449
Don Garrett3f41e152010-06-21 14:54:34 -0700450 # Additional changes to developer image.
Chris Sosa9673f3b2010-05-18 13:24:40 -0700451
Don Garrett3f41e152010-06-21 14:54:34 -0700452 # The ldd tool is a useful shell script but lives in glibc; just copy it.
453 sudo cp -a "$(which ldd)" "${root_dev_dir}/usr/bin"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700454
Don Garrett3f41e152010-06-21 14:54:34 -0700455 # If vim is installed, then a vi symlink would probably help.
456 if [[ -x "${ROOT_FS_DIR}/usr/local/bin/vim" ]]; then
457 sudo ln -sf vim "${ROOT_FS_DIR}/usr/local/bin/vi"
Girts Folkmanis7a8a8382010-05-18 22:52:25 -0700458 fi
Chris Sosa9673f3b2010-05-18 13:24:40 -0700459
Tammo Spalinkcfc0c642010-07-07 10:51:21 +0800460 # If pygtk is installed in stateful-dev, then install a path.
461 if [[ -d \
462 "${ROOT_FS_DIR}/usr/local/lib/python2.6/site-packages/gtk-2.0" ]]; then
463 sudo bash -c "\
464 echo gtk-2.0 > \
465 ${ROOT_FS_DIR}/usr/local/lib/python2.6/site-packages/pygtk.pth"
466 fi
467
Don Garrett3f41e152010-06-21 14:54:34 -0700468 # Check that the image has been correctly created. Only do it if not
469 # building a factory install image, as the INSTALL_MASK for it will
470 # make test_image fail.
471 if [[ ${FLAGS_factory_install} -eq ${FLAGS_FALSE} ]] ; then
472 "${SCRIPTS_DIR}/test_image" \
473 --root="${ROOT_FS_DIR}" \
474 --target="${ARCH}"
475 fi
476 echo "Developer image built and stored at ${image_name}"
477
Chris Sosa9673f3b2010-05-18 13:24:40 -0700478 trap - EXIT
David James868d7bb2010-06-24 21:48:14 -0700479 ${SCRIPTS_DIR}/mount_gpt_image.sh -u -r "${ROOT_FS_DIR}" \
Will Drewry4a675e12010-07-03 13:35:02 -0500480 -s "${STATEFUL_FS_DIR}" -e "${ESP_FS_DIR}"
David James868d7bb2010-06-24 21:48:14 -0700481}
482
483# Update the base package on an existing image.
484update_base_packages() {
485 local image_name=$1
486
487 echo "Updating base packages on ${image_name}"
488
489 # Create stateful partition of the same size as the rootfs.
Will Drewry4a675e12010-07-03 13:35:02 -0500490 trap "mount_gpt_cleanup" EXIT
David James868d7bb2010-06-24 21:48:14 -0700491
492 ${SCRIPTS_DIR}/mount_gpt_image.sh --from "${OUTPUT_DIR}" \
493 --image "${image_name}" -r "${ROOT_FS_DIR}" \
Will Drewry4a675e12010-07-03 13:35:02 -0500494 -s "${STATEFUL_FS_DIR}" -e "${ESP_FS_DIR}"
David James868d7bb2010-06-24 21:48:14 -0700495
496 # Emerge updated packages, exactly like when creating base image
497 sudo INSTALL_MASK="${INSTALL_MASK}" ${EMERGE_BOARD_CMD} \
498 --root="${ROOT_FS_DIR}" --root-deps=rdeps \
499 --usepkg -uDNv chromeos ${EMERGE_JOBS}
500
501 # Clean out unused packages
502 sudo INSTALL_MASK="${INSTALL_MASK}" ${EMERGE_BOARD_CMD} \
503 --root="${ROOT_FS_DIR}" --root-deps=rdeps \
504 --usepkg --depclean ${EMERGE_JOBS}
505
506 trap - EXIT
507 ${SCRIPTS_DIR}/mount_gpt_image.sh -u -r "${ROOT_FS_DIR}" \
Will Drewry4a675e12010-07-03 13:35:02 -0500508 -s "${STATEFUL_FS_DIR}" -e "${ESP_FS_DIR}"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700509}
510
Don Garrett3f41e152010-06-21 14:54:34 -0700511create_base_image() {
David James868d7bb2010-06-24 21:48:14 -0700512 local image_name=$1
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700513
Don Garrett3f41e152010-06-21 14:54:34 -0700514 trap "cleanup && delete_prompt" EXIT
Chris Sosa4bffb8b2010-04-07 17:23:54 -0700515
David James868d7bb2010-06-24 21:48:14 -0700516 UUID=$(uuidgen)
517
Don Garrett3f41e152010-06-21 14:54:34 -0700518 # Create and format the root file system.
519
520 # Check for loop device before creating image.
521 LOOP_DEV=$(sudo losetup -f)
522 if [ -z "${LOOP_DEV}" ] ; then
523 echo "No free loop device. Free up a loop device or reboot. exiting. "
524 exit 1
525 fi
526
527 # Create root file system disk image to fit on a 1GB memory stick.
528 # 1 GB in hard-drive-manufacturer-speak is 10^9, not 2^30. 950MB < 10^9 bytes.
529 if [[ ${FLAGS_factory_install} -eq ${FLAGS_TRUE} ]] ; then
530 ROOT_SIZE_BYTES=$((1024 * 1024 * 300))
531 else
532 ROOT_SIZE_BYTES=$((1024 * 1024 * ${FLAGS_rootfs_size}))
533 fi
534
Will Drewry9926ee22010-07-21 15:11:10 -0500535 # Pad out for the hash tree.
Will Drewry78992a32010-07-21 14:02:20 -0500536 ROOT_HASH_PAD=$((FLAGS_rootfs_hash_pad * 1024 * 1024))
537 info "Padding the rootfs image by ${ROOT_HASH_PAD} bytes for hash data"
Will Drewry9926ee22010-07-21 15:11:10 -0500538
Will Drewry78992a32010-07-21 14:02:20 -0500539 dd if=/dev/zero of="${ROOT_FS_IMG}" bs=1 count=1 \
540 seek=$((ROOT_SIZE_BYTES + ROOT_HASH_PAD - 1))
Will Drewry9926ee22010-07-21 15:11:10 -0500541 sudo losetup "${LOOP_DEV}" "${ROOT_FS_IMG}"
542 # Specify a block size and block count to avoid using the hash pad.
543 sudo mkfs.ext3 -b 4096 "${LOOP_DEV}" "$((ROOT_SIZE_BYTES / 4096))"
Will Drewry78992a32010-07-21 14:02:20 -0500544
Don Garrett3f41e152010-06-21 14:54:34 -0700545 # Tune and mount rootfs.
Will Drewry78992a32010-07-21 14:02:20 -0500546 # TODO(wad) rename the disk label to match the GPT since we
547 # can't change it later.
Don Garrett3f41e152010-06-21 14:54:34 -0700548 DISK_LABEL="C-KEYFOB"
549 sudo tune2fs -L "${DISK_LABEL}" -U "${UUID}" -c 0 -i 0 "${LOOP_DEV}"
550 sudo mount "${LOOP_DEV}" "${ROOT_FS_DIR}"
551
552 # Create stateful partition of the same size as the rootfs.
553 STATEFUL_LOOP_DEV=$(sudo losetup -f)
554 if [ -z "${STATEFUL_LOOP_DEV}" ] ; then
555 echo "No free loop device. Free up a loop device or reboot. exiting. "
556 exit 1
557 fi
Eric Li5cf288f2010-06-28 12:46:26 -0700558 STATEFUL_SIZE_BYTES=$((1024 * 1024 * ${FLAGS_statefulfs_size}))
559 dd if=/dev/zero of="${STATEFUL_FS_IMG}" bs=1 count=1 \
560 seek=$((STATEFUL_SIZE_BYTES - 1))
Denis Romanov11061542010-06-29 04:23:53 +0400561
562 # Tune and mount the stateful partition.
563 UUID=$(uuidgen)
564 DISK_LABEL="C-STATE"
Don Garrett3f41e152010-06-21 14:54:34 -0700565 sudo losetup "${STATEFUL_LOOP_DEV}" "${STATEFUL_FS_IMG}"
566 sudo mkfs.ext3 "${STATEFUL_LOOP_DEV}"
Denis Romanov11061542010-06-29 04:23:53 +0400567 sudo tune2fs -L "${DISK_LABEL}" -U "${UUID}" -c 0 -i 0 "${STATEFUL_LOOP_DEV}"
Don Garrett3f41e152010-06-21 14:54:34 -0700568 sudo mount "${STATEFUL_LOOP_DEV}" "${STATEFUL_FS_DIR}"
569
Denis Romanov11061542010-06-29 04:23:53 +0400570 # Create OEM partner partition.
571 OEM_LOOP_DEV=$(sudo losetup -f)
572 if [ -z "${OEM_LOOP_DEV}" ] ; then
573 echo "No free loop device. Free up a loop device or reboot. exiting. "
574 exit 1
575 fi
576 OEM_SIZE_BYTES=$((1024 * 1024 * 16))
577 dd if=/dev/zero of="${OEM_FS_IMG}" bs=1 count=1 seek=$((OEM_SIZE_BYTES - 1))
578
579 # Tune and mount OEM partner partition.
580 UUID=$(uuidgen)
581 DISK_LABEL="C-OEM"
582 sudo losetup "${OEM_LOOP_DEV}" "${OEM_FS_IMG}"
583 sudo mkfs.ext3 "${OEM_LOOP_DEV}"
584 sudo tune2fs -L "${DISK_LABEL}" -U "${UUID}" -c 0 -i 0 "${OEM_LOOP_DEV}"
585 sudo mount "${OEM_LOOP_DEV}" "${OEM_FS_DIR}"
586
Denis Romanov95bdf472010-07-12 22:44:42 +0400587 # Populate OEM partner partition.
588 if [ -n ${FLAGS_oem_customization} ]; then
589 if [ ! -d ${FLAGS_oem_customization} ]; then
590 echo "Specified OEM content directory does not exist. exiting."
591 exit 1
592 fi
593 for ITEM in `ls -A ${FLAGS_oem_customization}`
594 do sudo cp -a "${FLAGS_oem_customization}/$ITEM" "${OEM_FS_DIR}"
595 done
596 sudo find "${OEM_FS_DIR}" -type d -exec chmod 755 "{}" \;
597 sudo find "${OEM_FS_DIR}" -type f -exec chmod 644 "{}" \;
598 sudo chown -R root:root "${OEM_FS_DIR}"
599 fi
600
Don Garrett3f41e152010-06-21 14:54:34 -0700601 # -- Install packages into the root file system --
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700602
Don Garrett3f41e152010-06-21 14:54:34 -0700603 # We need to install libc manually from the cross toolchain.
604 # TODO: Improve this? We only want libc and not the whole toolchain.
605 PKGDIR="/var/lib/portage/pkgs/cross/"
606 sudo tar jxvpf \
607 "${PKGDIR}/${CHOST}/cross-${CHOST}"/glibc-${LIBC_VERSION}.tbz2 \
608 -C "${ROOT_FS_DIR}" --strip-components=3 \
609 --exclude=usr/include --exclude=sys-include --exclude=*.a --exclude=*.o
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700610
Don Garrett3f41e152010-06-21 14:54:34 -0700611 # We need to install libstdc++ manually from the cross toolchain.
612 # TODO: Figure out a better way of doing this?
613 sudo cp -a "${BOARD_ROOT}"/lib/libgcc_s.so* "${ROOT_FS_DIR}/lib"
614 sudo cp -a "${BOARD_ROOT}"/usr/lib/libstdc++.so* "${ROOT_FS_DIR}/usr/lib"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700615
Don Garrett3f41e152010-06-21 14:54:34 -0700616 # Prepare stateful partition with some pre-created directories.
617 sudo mkdir -p "${DEV_IMAGE_ROOT}"
618 sudo mkdir -p "${STATEFUL_FS_DIR}/var"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700619
Don Garrett3f41e152010-06-21 14:54:34 -0700620 # Create symlinks so that /usr/local/usr based directories are symlinked to
621 # /usr/local/ directories e.g. /usr/local/usr/bin -> /usr/local/bin, etc.
622 setup_symlinks_on_root "${DEV_IMAGE_ROOT}" "${STATEFUL_FS_DIR}/var" \
623 "${STATEFUL_FS_DIR}"
Tom Wai-Hong Tamf87a3672010-05-17 16:06:33 +0800624
Don Garrett3f41e152010-06-21 14:54:34 -0700625 # Perform binding rather than symlinking because directories must exist
626 # on rootfs so that we can bind at run-time since rootfs is read-only.
627 echo "Binding directories from stateful partition onto the rootfs"
628 sudo mkdir -p "${ROOT_FS_DIR}/usr/local"
629 sudo mount --bind "${DEV_IMAGE_ROOT}" "${ROOT_FS_DIR}/usr/local"
630 sudo mkdir -p "${ROOT_FS_DIR}/var"
631 sudo mount --bind "${STATEFUL_FS_DIR}/var" "${ROOT_FS_DIR}/var"
632 sudo mkdir -p "${ROOT_FS_DIR}/dev"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700633
Mandeep Singh Baines3dfa8522010-06-24 15:00:49 -0700634 # We "emerge --root=${ROOT_FS_DIR} --root-deps=rdeps --usepkg" all of the
Don Garrett3f41e152010-06-21 14:54:34 -0700635 # runtime packages for chrome os. This builds up a chrome os image from
636 # binary packages with runtime dependencies only. We use INSTALL_MASK to
637 # trim the image size as much as possible.
638 sudo INSTALL_MASK="${INSTALL_MASK}" ${EMERGE_BOARD_CMD} \
639 --root="${ROOT_FS_DIR}" --root-deps=rdeps \
Mandeep Singh Baines3dfa8522010-06-24 15:00:49 -0700640 --usepkg chromeos ${EMERGE_JOBS}
Bill Richardson8b3bd102010-04-06 15:00:10 -0700641
Will Drewry4a675e12010-07-03 13:35:02 -0500642 # Perform any customizations on the root file system that are needed.
643 "${SCRIPTS_DIR}/customize_rootfs" \
644 --root="${ROOT_FS_DIR}" \
645 --target="${ARCH}" \
646 --board="${BOARD}"
Bill Richardson8b3bd102010-04-06 15:00:10 -0700647
Will Drewry1ee156f2010-07-03 13:27:19 -0500648 # Populates the root filesystem with legacy bootloader templates
649 # appropriate for the platform. The autoupdater and installer will
650 # use those templates to update the legacy boot partition (12/ESP)
651 # on update.
652 # (This script does not populate vmlinuz.A and .B needed by syslinux.)
Will Drewry1670d482010-07-09 13:08:38 -0700653 enable_rootfs_verification=
654 if [[ ${FLAGS_enable_rootfs_verification} -eq ${FLAGS_TRUE} ]]; then
655 enable_rootfs_verification="--enable_rootfs_verification"
656 fi
657
Will Drewry1ee156f2010-07-03 13:27:19 -0500658 ${SCRIPTS_DIR}/create_legacy_bootloader_templates.sh \
659 --arch=${ARCH} \
660 --to="${ROOT_FS_DIR}"/boot \
661 --install \
Will Drewry1670d482010-07-09 13:08:38 -0700662 ${enable_rootfs_verification}
Bill Richardson9c5e5ec2010-05-14 17:00:21 -0700663
Will Drewry1ee156f2010-07-03 13:27:19 -0500664 # Create a working copy so we don't need the rootfs mounted
665 sudo mkdir -p "${OUTPUT_DIR}"/boot
666 # This will include any built files dropped in /boot as well.
667 # Like the current vmlinuz.
668 sudo cp -r "${ROOT_FS_DIR}"/boot/. "${OUTPUT_DIR}"/boot/
Will Drewry25861ee2010-07-04 05:09:41 -0700669 sudo chmod -R a+r "${OUTPUT_DIR}"/boot/
Bill Richardsona81df762010-04-09 08:12:05 -0700670
Don Garrett3f41e152010-06-21 14:54:34 -0700671 # Don't test the factory install shim.
672 if [[ ${FLAGS_factory_install} -eq ${FLAGS_FALSE} ]] ; then
673 # Check that the image has been correctly created.
674 "${SCRIPTS_DIR}/test_image" \
675 --root="${ROOT_FS_DIR}" \
676 --target="${ARCH}"
677 fi
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700678
Don Garrett3f41e152010-06-21 14:54:34 -0700679 # Clean up symlinks so they work on a running target rooted at "/".
680 # Here development packages are rooted at /usr/local. However, do not
681 # create /usr/local or /var on host (already exist on target).
682 setup_symlinks_on_root "/usr/local" "/var" "${STATEFUL_FS_DIR}"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700683
Will Drewry4a675e12010-07-03 13:35:02 -0500684 # make_image_bootable will clobber vmlinuz.image for x86.
685 # Until then, just copy the kernel to vmlinuz.image. It is
686 # expected in build_gpt.sh and needed by ARM until it supports the
687 # full, signed kernel partition format.
688 cp "${ROOT_FS_DIR}/boot/vmlinuz" "${OUTPUT_DIR}/vmlinuz.image"
689
690 # Create an empty esp image to be updated in by update_bootloaders.sh.
691 ${SCRIPTS_DIR}/create_esp.sh --to="${ESP_FS_IMG}"
692
Don Garrett3f41e152010-06-21 14:54:34 -0700693 cleanup
Chris Sosabde8c1b2010-04-29 14:02:35 -0700694
Don Garrett3f41e152010-06-21 14:54:34 -0700695 trap delete_prompt EXIT
Tan Gao6df5aee2010-05-19 14:19:55 -0700696
Don Garrett3f41e152010-06-21 14:54:34 -0700697 # Create the GPT-formatted image.
698 ${SCRIPTS_DIR}/build_gpt.sh \
699 --arch=${ARCH} \
700 --board=${FLAGS_board} \
701 --arm_extra_bootargs="${FLAGS_arm_extra_bootargs}" \
702 --rootfs_partition_size=${FLAGS_rootfs_partition_size} \
Don Garrett3f41e152010-06-21 14:54:34 -0700703 "${OUTPUT_DIR}" \
David James868d7bb2010-06-24 21:48:14 -0700704 "${OUTPUT_DIR}/${image_name}"
705
706 trap - EXIT
Don Garrett3f41e152010-06-21 14:54:34 -0700707}
708
709# Create the output directory.
710mkdir -p "${OUTPUT_DIR}"
711mkdir -p "${ROOT_FS_DIR}"
712mkdir -p "${STATEFUL_FS_DIR}"
Denis Romanov11061542010-06-29 04:23:53 +0400713mkdir -p "${OEM_FS_DIR}"
Don Garrett3f41e152010-06-21 14:54:34 -0700714mkdir -p "${ESP_FS_DIR}"
715
David James868d7bb2010-06-24 21:48:14 -0700716# Preserve old images by copying them forward for --preserve.
717if [[ $FLAGS_preserve -eq ${FLAGS_TRUE} ]] ; then
718 if [[ -f ${PREVIOUS_DIR}/${PRISTINE_IMAGE_NAME} ]] ; then
719 # Copy forward pristine image, and associated files
720 cp ${PREVIOUS_DIR}/*.sh ${PREVIOUS_DIR}/config.txt ${OUTPUT_DIR}
721 cp ${PREVIOUS_DIR}/${PRISTINE_IMAGE_NAME} ${OUTPUT_DIR}
Will Drewry4a675e12010-07-03 13:35:02 -0500722 cp -r ${PREVIOUS_DIR}/boot ${OUTPUT_DIR}/boot
David James868d7bb2010-06-24 21:48:14 -0700723
724 # Copy forward the developer image, if we already copied forward the base.
725 if [[ ${FLAGS_withdev} -eq ${FLAGS_TRUE} ]] && \
726 [[ -f ${PREVIOUS_DIR}/${DEVELOPER_IMAGE_NAME} ]] ; then
727 cp ${PREVIOUS_DIR}/${DEVELOPER_IMAGE_NAME} ${OUTPUT_DIR}
728 fi
729 fi
730fi
731
732if [[ -f ${PRISTINE_IMG} ]] ; then
733 update_base_packages ${PRISTINE_IMAGE_NAME}
734else
735 create_base_image ${PRISTINE_IMAGE_NAME}
736fi
Will Drewry4a675e12010-07-03 13:35:02 -0500737make_image_bootable ${PRISTINE_IMAGE_NAME}
738
739# FIXME: only signing things for x86 right now.
740if [[ "${ARCH}" = "x86" ]]; then
741 # Verify the final image.
742 load_kernel_test "${OUTPUT_DIR}/${PRISTINE_IMAGE_NAME}" \
Louis Yung-Chieh Lo36020402010-07-05 13:23:34 +0800743 "${DEVKEYSDIR}/recovery_key.vbpubk"
Will Drewry4a675e12010-07-03 13:35:02 -0500744fi
Bill Richardson4364a2e2010-03-30 14:17:34 -0700745
Don Garrette0020b12010-06-17 15:55:35 -0700746# Create a developer image based on the chromium os base image.
Don Garrett3f41e152010-06-21 14:54:34 -0700747if [ "${FLAGS_withdev}" -eq "${FLAGS_TRUE}" ] ; then
David James868d7bb2010-06-24 21:48:14 -0700748 if [[ ! -f ${DEVELOPER_IMG} ]] ; then
749 echo "Creating developer image from base image ${PRISTINE_IMAGE_NAME}"
750 cp ${PRISTINE_IMG} ${DEVELOPER_IMG}
751 fi
Don Garrett3f41e152010-06-21 14:54:34 -0700752
David James868d7bb2010-06-24 21:48:14 -0700753 update_dev_packages ${DEVELOPER_IMAGE_NAME}
Will Drewry4a675e12010-07-03 13:35:02 -0500754 make_image_bootable ${DEVELOPER_IMAGE_NAME}
Bill Richardson6ed13582010-06-16 21:38:15 -0700755fi
756
757# Clean up temporary files.
Don Garrett3f41e152010-06-21 14:54:34 -0700758rm -f "${ROOT_FS_IMG}" "${STATEFUL_FS_IMG}" "${OUTPUT_DIR}/vmlinuz.image" \
Louis Yung-Chieh Lo36020402010-07-05 13:23:34 +0800759 "${ESP_FS_IMG}" "${OEM_FS_IMG}" "${OUTPUT_DIR}/vmlinuz_hd.vblock"
Denis Romanov11061542010-06-29 04:23:53 +0400760rmdir "${ROOT_FS_DIR}" "${STATEFUL_FS_DIR}" "${OEM_FS_DIR}" "${ESP_FS_DIR}"
Bill Richardson67956222010-05-28 15:38:56 -0700761
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700762echo "Done. Image created in ${OUTPUT_DIR}"
Don Garrette0020b12010-06-17 15:55:35 -0700763echo "Chromium OS image created as ${PRISTINE_IMAGE_NAME}"
Don Garrette0020b12010-06-17 15:55:35 -0700764if [ "${FLAGS_withdev}" -eq "${FLAGS_TRUE}" ]; then
765 echo "Developer image created as ${DEVELOPER_IMAGE_NAME}"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700766fi
Nick Sanders8ab729a2010-06-16 03:15:17 -0700767
Nick Sandersd2509272010-06-16 03:50:04 -0700768print_time_elapsed
769
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700770echo "To copy to USB keyfob, OUTSIDE the chroot, do something like:"
Daniel Eratdd9818a2010-04-26 09:16:44 -0700771echo " ./image_to_usb.sh --from=${OUTSIDE_OUTPUT_DIR} --to=/dev/sdX"
Will Drewryefce6682010-07-23 19:43:27 -0500772echo "To convert to VMWare image, INSIDE the chroot, do something like:"
Zelidrag Hornung478a0d42010-07-14 11:47:39 -0700773echo " ./image_to_vm.sh --from=${OUTSIDE_OUTPUT_DIR}"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700774echo "from the scripts directory where you entered the chroot."