blob: e95de61acbc86c2c2e65ece9bed070a211c7cd86 [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.
22DEFINE_string board "$DEFAULT_BOARD" \
23 "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)"
30DEFINE_boolean replace $FLAGS_FALSE \
31 "Overwrite existing output, if any."
32DEFINE_boolean withdev $FLAGS_TRUE \
33 "Include useful developer friendly utilities in the image."
34DEFINE_boolean installmask $FLAGS_TRUE \
35 "Use INSTALL_MASK to shrink the resulting image."
36DEFINE_integer jobs -1 \
37 "How many packages to build in parallel at maximum."
Chris Sosab9f2d2e2010-05-05 16:42:44 -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"
Tom Wai-Hong Tamf87a3672010-05-17 16:06:33 +080042DEFINE_boolean factory_install $FLAGS_FALSE \
43 "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."
Tan Gao6df5aee2010-05-19 14:19:55 -070047DEFINE_boolean recovery $FLAGS_FALSE \
48 "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
62if [ -z "$FLAGS_board" ] ; then
63 error "--board is required."
64 exit 1
65fi
66
Zelidrag Hornung1d12c1a2010-06-02 10:20:29 -070067if [ "$FLAGS_rootfs_size" -gt "$FLAGS_rootfs_partition_size" ] ; then
68 error "rootfs (${FLAGS_rootfs_size} MB) is bigger than partition (${FLAGS_rootfs_partition_size} MB)."
69 exit 1
70fi
71
Bill Richardsonc09b94f2010-03-15 11:40:30 -070072# Determine build version.
73. "${SCRIPTS_DIR}/chromeos_version.sh"
74
75# Use canonical path since some tools (e.g. mount) do not like symlinks.
76# Append build attempt to output directory.
77IMAGE_SUBDIR="${CHROMEOS_VERSION_STRING}-a${FLAGS_build_attempt}"
78OUTPUT_DIR="${FLAGS_output_root}/${FLAGS_board}/${IMAGE_SUBDIR}"
79ROOT_FS_DIR="${OUTPUT_DIR}/rootfs"
80ROOT_FS_IMG="${OUTPUT_DIR}/rootfs.image"
Chris Sosa9673f3b2010-05-18 13:24:40 -070081
82# If we are creating a developer image, also create a pristine image with a
83# different name.
84DEVELOPER_IMAGE_NAME=
85PRISTINE_IMAGE_NAME=chromiumos_image.bin
86if [ "$FLAGS_withdev" -eq "$FLAGS_TRUE" ]; then
87 PRISTINE_IMAGE_NAME=chromiumos_base_image.bin
88 DEVELOPER_IMAGE_NAME=chromiumos_image.bin
89fi
Tan Gaoa40ed442010-06-02 15:45:19 -070090
91# If we are creating a recovery image, rename pristine image
92if [ "$FLAGS_recovery" -eq "$FLAGS_TRUE" ]; then
93 PRISTINE_IMAGE_NAME=recovery_image.bin
94fi
95
Chris Sosa9673f3b2010-05-18 13:24:40 -070096OUTPUT_IMG=${FLAGS_to:-${OUTPUT_DIR}/${PRISTINE_IMAGE_NAME}}
Bill Richardsonc09b94f2010-03-15 11:40:30 -070097
98BOARD="${FLAGS_board}"
99BOARD_ROOT="${FLAGS_build_root}/${BOARD}"
100
101LOOP_DEV=
Chris Sosa4bffb8b2010-04-07 17:23:54 -0700102STATEFUL_LOOP_DEV=
Bill Richardsona81df762010-04-09 08:12:05 -0700103ESP_LOOP_DEV=
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700104
105# What cross-build are we targeting?
106. "${BOARD_ROOT}/etc/make.conf.board_setup"
107LIBC_VERSION=${LIBC_VERSION:-"2.10.1-r1"}
108
109# Figure out ARCH from the given toolchain.
110# TODO: Move to common.sh as a function after scripts are switched over.
111TC_ARCH=$(echo "$CHOST" | awk -F'-' '{ print $1 }')
112case "$TC_ARCH" in
113 arm*)
114 ARCH="arm"
115 ;;
116 *86)
117 ARCH="x86"
118 ;;
119 *)
120 error "Unable to determine ARCH from toolchain: $CHOST"
121 exit 1
122esac
123
124# Hack to fix bug where x86_64 CHOST line gets incorrectly added.
125# ToDo(msb): remove this hack.
126PACKAGES_FILE="${BOARD_ROOT}/packages/Packages"
127sudo sed -e "s/CHOST: x86_64-pc-linux-gnu//" -i "${PACKAGES_FILE}"
128
129# Handle existing directory.
130if [[ -e "$OUTPUT_DIR" ]]; then
131 if [[ $FLAGS_replace -eq $FLAGS_TRUE ]]; then
132 sudo rm -rf "$OUTPUT_DIR"
133 else
134 echo "Directory $OUTPUT_DIR already exists."
135 echo "Use --build_attempt option to specify an unused attempt."
136 echo "Or use --replace if you want to overwrite this directory."
137 exit 1
138 fi
139fi
140
Bill Richardson67956222010-05-28 15:38:56 -0700141# Be verbose to help debug failures.
142set -x
143
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700144# Create the output directory.
145mkdir -p "$OUTPUT_DIR"
146
147cleanup_rootfs_loop() {
Chris Sosabde8c1b2010-04-29 14:02:35 -0700148 sudo umount -d "$ROOT_FS_DIR"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700149}
150
151cleanup_stateful_fs_loop() {
Chris Sosa4bffb8b2010-04-07 17:23:54 -0700152 sudo umount "${ROOT_FS_DIR}/usr/local"
153 sudo umount "${ROOT_FS_DIR}/var"
Chris Sosabde8c1b2010-04-29 14:02:35 -0700154 sudo umount -d "${STATEFUL_DIR}"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700155}
156
Bill Richardson8b3bd102010-04-06 15:00:10 -0700157cleanup_esp_loop() {
Chris Sosabde8c1b2010-04-29 14:02:35 -0700158 sudo umount -d "$ESP_DIR"
Bill Richardson8b3bd102010-04-06 15:00:10 -0700159}
160
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700161cleanup() {
162 # Disable die on error.
163 set +e
164
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700165 if [[ -n "$STATEFUL_LOOP_DEV" ]]; then
166 cleanup_stateful_fs_loop
167 fi
168
169 if [[ -n "$LOOP_DEV" ]]; then
170 cleanup_rootfs_loop
171 fi
172
Bill Richardsona81df762010-04-09 08:12:05 -0700173 if [[ -n "$ESP_LOOP_DEV" ]]; then
Bill Richardson8b3bd102010-04-06 15:00:10 -0700174 cleanup_esp_loop
175 fi
176
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700177 # Turn die on error back on.
178 set -e
179}
180
Chris Sosabde8c1b2010-04-29 14:02:35 -0700181delete_prompt() {
182 echo "An error occurred in your build so your latest output directory" \
183 "is invalid."
184 read -p "Would you like to delete the output directory (y/N)? " SURE
185 SURE="${SURE:0:1}" # Get just the first character
186 if [ "${SURE}" == "y" ] ; then
187 sudo rm -rf "$OUTPUT_DIR"
188 echo "Deleted $OUTPUT_DIR"
189 else
190 echo "Not deleting $OUTPUT_DIR. Note dev server updates will not work" \
191 "until you successfully build another image or delete this directory"
192 fi
193}
194
Chris Sosa9673f3b2010-05-18 13:24:40 -0700195# $1 - Directory where developer rootfs is mounted.
196# $2 - Directory where developer stateful_partition is mounted.
197developer_cleanup() {
198 "$SCRIPTS_DIR/mount_gpt_image.sh" -u -r "$1" -s "$2"
199 delete_prompt
200}
201
Tan Gaoa40ed442010-06-02 15:45:19 -0700202# Creates a modified image based on $OUTPUT_IMG with additional packages.
203create_mod_image() {
204 local image_type=$1
Chris Sosa9673f3b2010-05-18 13:24:40 -0700205 local root_fs_dir="${OUTPUT_DIR}/rootfs_dev"
206 local root_fs_img="${OUTPUT_DIR}/rootfs_dev.image"
Tan Gaoa40ed442010-06-02 15:45:19 -0700207 local image_to_mount=${DEVELOPER_IMAGE_NAME}
208 local output_img="${OUTPUT_DIR}/${image_to_mount}"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700209
210 # Create stateful partition of the same size as the rootfs.
211 local stateful_img="$OUTPUT_DIR/stateful_partition_dev.image"
212 local stateful_dir="$OUTPUT_DIR/stateful_partition_dev"
Tan Gaoa40ed442010-06-02 15:45:19 -0700213 local file_to_touch=".dev_mode"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700214
215 trap "developer_cleanup \"$root_fs_dir\" \"$stateful_dir\"" EXIT
216
Tan Gaoa40ed442010-06-02 15:45:19 -0700217 if [ "$image_type" == "dev" ]; then
218 # Mount a new copy of the base image.
219 echo "Creating developer image from base image $OUTPUT_IMG"
220 cp "$OUTPUT_IMG" "$output_img"
221 elif [ "$image_type" == "recovery" ]; then
222 image_to_mount=$PRISTINE_IMAGE_NAME
223 file_to_touch=".recovery_installer"
224 fi
225
Chris Sosa9673f3b2010-05-18 13:24:40 -0700226 $SCRIPTS_DIR/mount_gpt_image.sh --from "$OUTPUT_DIR" \
Tan Gaoa40ed442010-06-02 15:45:19 -0700227 --image "$image_to_mount" -r "$root_fs_dir" -s "$stateful_dir"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700228
Tan Gaoa40ed442010-06-02 15:45:19 -0700229 if [ "$image_type" == "dev" ]; then
230 # Determine the root dir for developer packages.
231 local root_dev_dir="$root_fs_dir"
232 [ $FLAGS_statefuldev -eq $FLAGS_TRUE ] && \
233 root_dev_dir="$root_fs_dir/usr/local"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700234
Tan Gaoa40ed442010-06-02 15:45:19 -0700235 # Install developer packages described in chromeos-dev.
236 sudo INSTALL_MASK="$INSTALL_MASK" emerge-${BOARD} \
237 --root="$root_dev_dir" --root-deps=rdeps \
238 --usepkgonly chromeos-dev $EMERGE_JOBS
239 elif [ "$image_type" == "recovery" ]; then
240 # Install recovery installer
241 sudo emerge-${BOARD} --root=$root_fs_dir --usepkgonly \
242 --root-deps=rdeps --nodeps chromeos-recovery
243 fi
Chris Sosa9673f3b2010-05-18 13:24:40 -0700244
245 # Re-run ldconfig to fix /etc/ldconfig.so.cache.
246 sudo /sbin/ldconfig -r "$root_fs_dir"
247
248 # Mark the image as a developer image (input to chromeos_startup).
249 sudo mkdir -p "$root_fs_dir/root"
Tan Gaoa40ed442010-06-02 15:45:19 -0700250 sudo touch "$root_fs_dir/root/$file_to_touch"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700251
Tan Gaoa40ed442010-06-02 15:45:19 -0700252 if [ "$image_type" == "dev" ]; then
253 # Additional changes to developer image.
Chris Sosa9673f3b2010-05-18 13:24:40 -0700254
Tan Gaoa40ed442010-06-02 15:45:19 -0700255 # The ldd tool is a useful shell script but lives in glibc; just copy it.
256 sudo cp -a "$(which ldd)" "${root_dev_dir}/usr/bin"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700257
Tan Gaoa40ed442010-06-02 15:45:19 -0700258 # TODO: Temporarily create fake xterm symlink until we do proper xinitrc
259 local aterm="$root_fs_dir/usr/local/bin/aterm"
260 if [[ -f "${aterm}" ]]; then
261 sudo chmod 0755 "$aterm"
262 sudo ln -s aterm "${root_fs_dir}/usr/local/bin/xterm"
263 fi
Chris Sosab6895772010-05-25 17:47:51 -0700264
Tan Gaoa40ed442010-06-02 15:45:19 -0700265 # If vim is installed, then a vi symlink would probably help.
266 if [[ -x "${root_fs_dir}/usr/local/bin/vim" ]]; then
267 sudo ln -sf vim "${root_fs_dir}/usr/local/bin/vi"
268 fi
Chris Sosa9673f3b2010-05-18 13:24:40 -0700269
Tan Gaoa40ed442010-06-02 15:45:19 -0700270 # Check that the image has been correctly created. Only do it if not
271 # building a factory install image, as the INSTALL_MASK for it will
272 # make test_image fail.
273 if [[ $FLAGS_factory_install -eq ${FLAGS_FALSE} ]] ; then
274 "${SCRIPTS_DIR}/test_image" \
275 --root="$root_fs_dir" \
276 --target="$ARCH"
277 fi
278 echo "Developer image built and stored at $output_img"
Girts Folkmanis7a8a8382010-05-18 22:52:25 -0700279 fi
Chris Sosa9673f3b2010-05-18 13:24:40 -0700280
281 trap - EXIT
282 $SCRIPTS_DIR/mount_gpt_image.sh -u -r "$root_fs_dir" -s "$stateful_dir"
Chris Sosad4455022010-05-20 10:14:06 -0700283 sudo rm -rf "$root_fs_dir" "$stateful_dir"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700284}
285
Chris Sosa4bffb8b2010-04-07 17:23:54 -0700286# ${DEV_IMAGE_ROOT} specifies the location of where developer packages will
287# be installed on the stateful dir. On a Chromium OS system, this will
288# translate to /usr/local
289DEV_IMAGE_ROOT=
290
Chris Sosabde8c1b2010-04-29 14:02:35 -0700291trap "cleanup && delete_prompt" EXIT
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700292
293mkdir -p "$ROOT_FS_DIR"
294
295# Create and format the root file system.
296
297# Check for loop device before creating image.
298LOOP_DEV=$(sudo losetup -f)
299if [ -z "$LOOP_DEV" ] ; then
300 echo "No free loop device. Free up a loop device or reboot. exiting. "
301 exit 1
302fi
303
304# Create root file system disk image to fit on a 1GB memory stick.
305# 1 GB in hard-drive-manufacturer-speak is 10^9, not 2^30. 950MB < 10^9 bytes.
Tom Wai-Hong Tamf87a3672010-05-17 16:06:33 +0800306if [[ $FLAGS_factory_install -eq ${FLAGS_TRUE} ]] ; then
Girts Folkmanis7a8a8382010-05-18 22:52:25 -0700307 ROOT_SIZE_BYTES=$((1024 * 1024 * 300))
Tom Wai-Hong Tamf87a3672010-05-17 16:06:33 +0800308else
Zelidrag Hornung1d12c1a2010-06-02 10:20:29 -0700309 ROOT_SIZE_BYTES=$((1024 * 1024 * ${FLAGS_rootfs_size}))
Tom Wai-Hong Tamf87a3672010-05-17 16:06:33 +0800310fi
311
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700312dd if=/dev/zero of="$ROOT_FS_IMG" bs=1 count=1 seek=$((ROOT_SIZE_BYTES - 1))
313sudo losetup "$LOOP_DEV" "$ROOT_FS_IMG"
314sudo mkfs.ext3 "$LOOP_DEV"
315
316# Tune and mount rootfs.
317UUID=$(uuidgen)
318DISK_LABEL="C-KEYFOB"
319sudo tune2fs -L "$DISK_LABEL" -U "$UUID" -c 0 -i 0 "$LOOP_DEV"
320sudo mount "$LOOP_DEV" "$ROOT_FS_DIR"
321
322# Create stateful partition of the same size as the rootfs.
323STATEFUL_IMG="$OUTPUT_DIR/stateful_partition.image"
324STATEFUL_DIR="$OUTPUT_DIR/stateful_partition"
325STATEFUL_LOOP_DEV=$(sudo losetup -f)
326if [ -z "$STATEFUL_LOOP_DEV" ] ; then
327 echo "No free loop device. Free up a loop device or reboot. exiting. "
328 exit 1
329fi
330dd if=/dev/zero of="$STATEFUL_IMG" bs=1 count=1 seek=$((ROOT_SIZE_BYTES - 1))
331sudo losetup "$STATEFUL_LOOP_DEV" "$STATEFUL_IMG"
332sudo mkfs.ext3 "$STATEFUL_LOOP_DEV"
333sudo tune2fs -L "C-STATE" -U "$UUID" -c 0 -i 0 \
334 "$STATEFUL_LOOP_DEV"
335
336# Mount the stateful partition.
337mkdir -p "$STATEFUL_DIR"
338sudo mount "$STATEFUL_LOOP_DEV" "$STATEFUL_DIR"
339
Chris Sosa4bffb8b2010-04-07 17:23:54 -0700340# Set dev image root now that we have mounted the stateful partition we created
341DEV_IMAGE_ROOT="$STATEFUL_DIR/dev_image"
342
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700343# Turn root file system into bootable image.
344if [[ "$ARCH" = "x86" ]]; then
345 # Setup extlinux configuration.
346 # TODO: For some reason the /dev/disk/by-uuid is not being generated by udev
347 # in the initramfs. When we figure that out, switch to root=UUID=$UUID.
348 sudo mkdir -p "$ROOT_FS_DIR"/boot
349 # TODO(adlr): use initramfs for booting.
350 cat <<EOF | sudo dd of="$ROOT_FS_DIR"/boot/extlinux.conf
351DEFAULT chromeos-usb
352PROMPT 0
353TIMEOUT 0
354
355label chromeos-usb
356 menu label chromeos-usb
357 kernel vmlinuz
358 append quiet console=tty2 init=/sbin/init boot=local rootwait root=/dev/sdb3 ro noresume noswap i915.modeset=1 loglevel=1
359
360label chromeos-hd
361 menu label chromeos-hd
362 kernel vmlinuz
363 append quiet console=tty2 init=/sbin/init boot=local rootwait root=HDROOT ro noresume noswap i915.modeset=1 loglevel=1
364EOF
365
366 # Make partition bootable and label it.
367 sudo extlinux -z --install "${ROOT_FS_DIR}/boot"
368fi
369
370# -- Install packages into the root file system --
371
372# We need to install libc manually from the cross toolchain.
373# TODO: Improve this? We only want libc and not the whole toolchain.
374PKGDIR="/var/lib/portage/pkgs/cross/"
375sudo tar jxvpf \
376 "${PKGDIR}/${CHOST}/cross-${CHOST}"/glibc-${LIBC_VERSION}.tbz2 \
377 -C "$ROOT_FS_DIR" --strip-components=3 \
378 --exclude=usr/include --exclude=sys-include --exclude=*.a --exclude=*.o
379
380# We need to install libstdc++ manually from the cross toolchain.
381# TODO: Figure out a better way of doing this?
382sudo cp -a "${BOARD_ROOT}"/lib/libgcc_s.so* "${ROOT_FS_DIR}/lib"
383sudo cp -a "${BOARD_ROOT}"/usr/lib/libstdc++.so* "${ROOT_FS_DIR}/usr/lib"
384
385INSTALL_MASK=""
Chris Sosa3d9a10b2010-04-13 15:00:46 -0700386if [[ $FLAGS_installmask -eq ${FLAGS_TRUE} ]] ; then
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700387 INSTALL_MASK="$DEFAULT_INSTALL_MASK"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700388fi
389
Tom Wai-Hong Tamf87a3672010-05-17 16:06:33 +0800390# Reduce the size of factory install shim.
391# TODO: Build a separated ebuild for the factory install shim to reduce size.
392if [[ $FLAGS_factory_install -eq ${FLAGS_TRUE} ]] ; then
393 INSTALL_MASK="$INSTALL_MASK $FACTORY_INSTALL_MASK"
394fi
395
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700396if [[ $FLAGS_jobs -ne -1 ]]; then
397 EMERGE_JOBS="--jobs=$FLAGS_jobs"
398fi
399
Chris Sosa4bffb8b2010-04-07 17:23:54 -0700400# Prepare stateful partition with some pre-created directories
401sudo mkdir -p "${DEV_IMAGE_ROOT}"
402sudo mkdir -p "${STATEFUL_DIR}/var"
Bill Richardson8b3bd102010-04-06 15:00:10 -0700403
Chris Sosa4bffb8b2010-04-07 17:23:54 -0700404# Create symlinks so that /usr/local/usr based directories are symlinked to
405# /usr/local/ directories e.g. /usr/local/usr/bin -> /usr/local/bin, etc.
Chris Sosa702618f2010-05-14 12:52:32 -0700406setup_symlinks_on_root "${DEV_IMAGE_ROOT}" "${STATEFUL_DIR}/var" \
407 "${STATEFUL_DIR}"
Bill Richardson8b3bd102010-04-06 15:00:10 -0700408
Chris Sosa4bffb8b2010-04-07 17:23:54 -0700409# Perform binding rather than symlinking because directories must exist
410# on rootfs so that we can bind at run-time since rootfs is read-only
411echo "Binding directories from stateful partition onto the rootfs"
412sudo mkdir -p "${ROOT_FS_DIR}/usr/local"
413sudo mount --bind "${DEV_IMAGE_ROOT}" "${ROOT_FS_DIR}/usr/local"
414sudo mkdir -p "${ROOT_FS_DIR}/var"
415sudo mount --bind "${STATEFUL_DIR}/var" "${ROOT_FS_DIR}/var"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700416
417# We "emerge --root=$ROOT_FS_DIR --root-deps=rdeps --usepkgonly" all of the
418# runtime packages for chrome os. This builds up a chrome os image from binary
419# packages with runtime dependencies only. We use INSTALL_MASK to trim the
420# image size as much as possible.
Ken Mixterd5c4b172010-04-16 10:11:02 -0700421sudo INSTALL_MASK="$INSTALL_MASK" emerge-${BOARD} \
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700422 --root="$ROOT_FS_DIR" --root-deps=rdeps \
Anush Elangovanbb85f8f2010-05-27 11:00:57 -0700423 --usepkgonly chromeos $EMERGE_JOBS
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700424
Bill Richardson8b3bd102010-04-06 15:00:10 -0700425# Create EFI System Partition to boot stock EFI BIOS (but not ChromeOS EFI
426# BIOS). We only need this for x86, but it's simpler and safer to keep the disk
427# images the same for both x86 and ARM.
428ESP_IMG=${OUTPUT_DIR}/esp.image
429# NOTE: The size argument for mkfs.vfat is in 1024-byte blocks. We'll hard-code
430# it to 16M for now.
431ESP_BLOCKS=16384
432/usr/sbin/mkfs.vfat -C ${OUTPUT_DIR}/esp.image ${ESP_BLOCKS}
433ESP_DIR=${OUTPUT_DIR}/esp
Bill Richardsona81df762010-04-09 08:12:05 -0700434ESP_LOOP_DEV=$(sudo losetup -f)
435if [ -z "$ESP_LOOP_DEV" ] ; then
436 echo "No free loop device. Free up a loop device or reboot. exiting. "
437 exit 1
438fi
439mkdir -p "${ESP_DIR}"
440sudo losetup "${ESP_LOOP_DEV}" "${ESP_IMG}"
441sudo mount "${ESP_LOOP_DEV}" "${ESP_DIR}"
442sudo mkdir -p "${ESP_DIR}/efi/boot"
443sudo grub-mkimage -p /efi/boot -o "${ESP_DIR}/efi/boot/bootx64.efi" \
Bill Richardson8b3bd102010-04-06 15:00:10 -0700444 part_gpt fat ext2 normal boot sh chain configfile linux
Bill Richardson9c5e5ec2010-05-14 17:00:21 -0700445cat <<'EOF' | sudo dd of="${ESP_DIR}/efi/boot/grub.cfg"
Bill Richardson8b3bd102010-04-06 15:00:10 -0700446set default=0
Bill Richardson9c5e5ec2010-05-14 17:00:21 -0700447set timeout=2
Bill Richardson8b3bd102010-04-06 15:00:10 -0700448
Bill Richardson9c5e5ec2010-05-14 17:00:21 -0700449# NOTE: These magic grub variables are a Chrome OS hack. They are not portable.
450
451menuentry "local image A" {
452 linux $grubpartA/boot/vmlinuz quiet console=tty2 init=/sbin/init boot=local rootwait root=/dev/$linuxpartA ro noresume noswap i915.modeset=1 loglevel=1
Bill Richardsona81df762010-04-09 08:12:05 -0700453}
454
Bill Richardson9c5e5ec2010-05-14 17:00:21 -0700455menuentry "local image B" {
456 linux $grubpartB/boot/vmlinuz quiet console=tty2 init=/sbin/init boot=local rootwait root=/dev/$linuxpartB ro noresume noswap i915.modeset=1 loglevel=1
Bill Richardson041bd712010-04-27 09:36:14 -0700457}
458
Bill Richardson8b3bd102010-04-06 15:00:10 -0700459EOF
460
Bill Richardson3fefd2a2010-06-03 11:03:27 -0700461# FIXME: At the moment, we're working on signed images for x86 only. ARM will
462# support this before shipping, but at the moment they don't.
463if [[ "$ARCH" = "x86" ]]; then
464
465 # Legacy BIOS will use the kernel in the rootfs (via syslinux), as will
466 # standard EFI BIOS (via grub, from the EFI System Partition). Chrome OS BIOS
467 # will use a separate signed kernel partition, which we'll create now.
468 # FIXME: remove serial output, debugging messages
469 cat <<'EOF' > "${OUTPUT_DIR}/config.txt"
Bill Richardson67956222010-05-28 15:38:56 -0700470earlyprintk=serial,ttyS0,115200
471console=ttyS0,115200
472init=/sbin/init
473add_efi_memmap
474boot=local
475rootwait
476root=/dev/sd%D%P
477ro
478noresume
479noswap
480i915.modeset=1
481loglevel=7
482Hi_Mom
483EOF
Bill Richardson3fefd2a2010-06-03 11:03:27 -0700484
485 # FIXME: We need to specify the real keys and certs here!
486 SIG_DIR="${SRC_ROOT}/platform/vboot_reference/tests/testkeys"
487
488 # Create the kernel partition image.
489 kernel_utility --generate \
490 --firmware_key "${SIG_DIR}/key_rsa4096.pem" \
491 --kernel_key "${SIG_DIR}/key_rsa1024.pem" \
492 --kernel_key_pub "${SIG_DIR}/key_rsa1024.keyb" \
493 --firmware_sign_algorithm 8 \
494 --kernel_sign_algorithm 2 \
495 --kernel_key_version 1 \
496 --kernel_version 1 \
497 --config "${OUTPUT_DIR}/config.txt" \
498 --bootloader /lib64/bootstub/bootstub.efi \
499 --vmlinuz "${ROOT_FS_DIR}/boot/vmlinuz" \
500 --out "${OUTPUT_DIR}/vmlinuz.image"
501
502else
503 # FIXME: For now, ARM just uses the unsigned kernel by itself.
504 cp -f "${ROOT_FS_DIR}/boot/vmlinuz" "${OUTPUT_DIR}/vmlinuz.image"
505fi
Bill Richardson67956222010-05-28 15:38:56 -0700506
Bill Richardson67956222010-05-28 15:38:56 -0700507
Chris Sosa3f21b992010-05-10 16:34:47 -0700508# Perform any customizations on the root file system that are needed.
Chris Sosa503efe12010-04-08 10:05:46 -0700509"${SCRIPTS_DIR}/customize_rootfs" \
510 --root="$ROOT_FS_DIR" \
511 --target="$ARCH" \
Chris Sosa3f21b992010-05-10 16:34:47 -0700512 --board="$BOARD"
Chris Sosa503efe12010-04-08 10:05:46 -0700513
Tom Wai-Hong Tamf87a3672010-05-17 16:06:33 +0800514# Don't test the factory install shim.
515if [[ $FLAGS_factory_install -eq ${FLAGS_FALSE} ]] ; then
516 # Check that the image has been correctly created.
517 "${SCRIPTS_DIR}/test_image" \
518 --root="$ROOT_FS_DIR" \
519 --target="$ARCH"
520fi
Chris Sosa503efe12010-04-08 10:05:46 -0700521
Chris Sosa4bffb8b2010-04-07 17:23:54 -0700522# Clean up symlinks so they work on a running target rooted at "/".
523# Here development packages are rooted at /usr/local. However, do not
524# create /usr/local or /var on host (already exist on target).
Chris Sosa702618f2010-05-14 12:52:32 -0700525setup_symlinks_on_root "/usr/local" "/var" "${STATEFUL_DIR}"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700526
527# Cleanup loop devices.
Chris Sosa4bffb8b2010-04-07 17:23:54 -0700528cleanup
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700529
Chris Sosabde8c1b2010-04-29 14:02:35 -0700530trap delete_prompt EXIT
531
Tan Gao6df5aee2010-05-19 14:19:55 -0700532RECOVERY="--norecovery"
533if [[ ${FLAGS_recovery} -eq $FLAGS_TRUE ]]; then
534 RECOVERY="--recovery"
535fi
536
Bill Richardson4364a2e2010-03-30 14:17:34 -0700537# Create the GPT-formatted image
538${SCRIPTS_DIR}/build_gpt.sh \
robotboyb75eee32010-04-30 09:51:23 -0700539 --arch=${ARCH} \
540 --board=${FLAGS_board} \
541 --arm_extra_bootargs="${FLAGS_arm_extra_bootargs}" \
Zelidrag Hornung1d12c1a2010-06-02 10:20:29 -0700542 --rootfs_partition_size=${FLAGS_rootfs_partition_size} \
Tan Gao6df5aee2010-05-19 14:19:55 -0700543 ${RECOVERY} \
robotboyb75eee32010-04-30 09:51:23 -0700544 "${OUTPUT_DIR}" \
545 "${OUTPUT_IMG}"
Bill Richardson4364a2e2010-03-30 14:17:34 -0700546
Bill Richardson6dcea162010-03-31 19:20:24 -0700547# Clean up temporary files.
Bill Richardson8b3bd102010-04-06 15:00:10 -0700548rm -f "${ROOT_FS_IMG}" "${STATEFUL_IMG}" "${OUTPUT_DIR}/vmlinuz.image" \
549 "${ESP_IMG}"
550rmdir "${ROOT_FS_DIR}" "${STATEFUL_DIR}" "${ESP_DIR}"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700551
552OUTSIDE_OUTPUT_DIR="../build/images/${FLAGS_board}/${IMAGE_SUBDIR}"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700553
Tan Gaoa40ed442010-06-02 15:45:19 -0700554# Create a recovery image based on the chromium os base image
555[ "$FLAGS_recovery" -eq "$FLAGS_TRUE" ] && create_mod_image "recovery"
556trap - EXIT
557
Chris Sosa9673f3b2010-05-18 13:24:40 -0700558# Create a developer image based on the chromium os base image
Tan Gaoa40ed442010-06-02 15:45:19 -0700559[ "$FLAGS_withdev" -eq "$FLAGS_TRUE" ] && create_mod_image "dev"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700560trap - EXIT
561
Bill Richardson67956222010-05-28 15:38:56 -0700562# be quiet again
563set +x
564
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700565echo "Done. Image created in ${OUTPUT_DIR}"
Chris Sosa9673f3b2010-05-18 13:24:40 -0700566echo "Chromium OS image created as $PRISTINE_IMAGE_NAME"
Tan Gaoa40ed442010-06-02 15:45:19 -0700567if [ "$FLAGS_recovery" -eq "$FLAGS_TRUE" ]; then
568 echo "Recovery image created as $PRISTINE_IMAGE_NAME"
569fi
Chris Sosa9673f3b2010-05-18 13:24:40 -0700570if [ "$FLAGS_withdev" -eq "$FLAGS_TRUE" ]; then
571 echo "Developer image created as $DEVELOPER_IMAGE_NAME"
572fi
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700573echo "To copy to USB keyfob, OUTSIDE the chroot, do something like:"
Daniel Eratdd9818a2010-04-26 09:16:44 -0700574echo " ./image_to_usb.sh --from=${OUTSIDE_OUTPUT_DIR} --to=/dev/sdX"
Bill Richardsonc09b94f2010-03-15 11:40:30 -0700575echo "To convert to VMWare image, OUTSIDE the chroot, do something like:"
576echo " ./image_to_vmware.sh --from=${OUTSIDE_OUTPUT_DIR}"
577echo "from the scripts directory where you entered the chroot."