Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 1 | #!/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 Garrett | 640a058 | 2010-05-04 16:54:28 -0700 | [diff] [blame] | 17 | restart_in_chroot_if_needed $* |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 18 | |
| 19 | get_default_board |
| 20 | |
| 21 | # Flags. |
| 22 | DEFINE_string board "$DEFAULT_BOARD" \ |
| 23 | "The board to build an image for." |
| 24 | DEFINE_string build_root "/build" \ |
| 25 | "The root location for board sysroots." |
| 26 | DEFINE_integer build_attempt 1 \ |
| 27 | "The build attempt for this image build." |
| 28 | DEFINE_string output_root "${DEFAULT_BUILD_ROOT}/images" \ |
| 29 | "Directory in which to place image result directories (named by version)" |
| 30 | DEFINE_boolean replace $FLAGS_FALSE \ |
| 31 | "Overwrite existing output, if any." |
| 32 | DEFINE_boolean withdev $FLAGS_TRUE \ |
| 33 | "Include useful developer friendly utilities in the image." |
| 34 | DEFINE_boolean installmask $FLAGS_TRUE \ |
| 35 | "Use INSTALL_MASK to shrink the resulting image." |
| 36 | DEFINE_integer jobs -1 \ |
| 37 | "How many packages to build in parallel at maximum." |
Chris Sosa | b9f2d2e | 2010-05-05 16:42:44 -0700 | [diff] [blame] | 38 | DEFINE_boolean statefuldev $FLAGS_TRUE \ |
Chris Sosa | 4bffb8b | 2010-04-07 17:23:54 -0700 | [diff] [blame] | 39 | "Install development packages on stateful partition rather than the rootfs" |
Antoine Labour | e9e585f | 2010-04-01 15:57:57 -0700 | [diff] [blame] | 40 | DEFINE_string to "" \ |
| 41 | "The target image file or device" |
Tom Wai-Hong Tam | f87a367 | 2010-05-17 16:06:33 +0800 | [diff] [blame] | 42 | DEFINE_boolean factory_install $FLAGS_FALSE \ |
| 43 | "Build a smaller image to overlay the factory install shim on; this argument \ |
| 44 | is also required in image_to_usb." |
robotboy | b75eee3 | 2010-04-30 09:51:23 -0700 | [diff] [blame] | 45 | DEFINE_string arm_extra_bootargs "" \ |
| 46 | "Additional command line options to pass to the ARM kernel." |
Tan Gao | 6df5aee | 2010-05-19 14:19:55 -0700 | [diff] [blame] | 47 | DEFINE_boolean recovery $FLAGS_FALSE \ |
| 48 | "Build a recovery image. Default: False." |
Zelidrag Hornung | 1d12c1a | 2010-06-02 10:20:29 -0700 | [diff] [blame] | 49 | DEFINE_integer rootfs_partition_size 1024 \ |
| 50 | "rootfs parition size in MBs." |
| 51 | DEFINE_integer rootfs_size 720 \ |
| 52 | "rootfs filesystem size in MBs." |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 53 | |
| 54 | # Parse command line. |
| 55 | FLAGS "$@" || exit 1 |
| 56 | eval 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. |
| 60 | set -e |
| 61 | |
| 62 | if [ -z "$FLAGS_board" ] ; then |
| 63 | error "--board is required." |
| 64 | exit 1 |
| 65 | fi |
| 66 | |
Zelidrag Hornung | 1d12c1a | 2010-06-02 10:20:29 -0700 | [diff] [blame] | 67 | if [ "$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 |
| 70 | fi |
| 71 | |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 72 | # 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. |
| 77 | IMAGE_SUBDIR="${CHROMEOS_VERSION_STRING}-a${FLAGS_build_attempt}" |
| 78 | OUTPUT_DIR="${FLAGS_output_root}/${FLAGS_board}/${IMAGE_SUBDIR}" |
| 79 | ROOT_FS_DIR="${OUTPUT_DIR}/rootfs" |
| 80 | ROOT_FS_IMG="${OUTPUT_DIR}/rootfs.image" |
Chris Sosa | 9673f3b | 2010-05-18 13:24:40 -0700 | [diff] [blame] | 81 | |
| 82 | # If we are creating a developer image, also create a pristine image with a |
| 83 | # different name. |
| 84 | DEVELOPER_IMAGE_NAME= |
| 85 | PRISTINE_IMAGE_NAME=chromiumos_image.bin |
| 86 | if [ "$FLAGS_withdev" -eq "$FLAGS_TRUE" ]; then |
| 87 | PRISTINE_IMAGE_NAME=chromiumos_base_image.bin |
| 88 | DEVELOPER_IMAGE_NAME=chromiumos_image.bin |
| 89 | fi |
Tan Gao | a40ed44 | 2010-06-02 15:45:19 -0700 | [diff] [blame] | 90 | |
| 91 | # If we are creating a recovery image, rename pristine image |
| 92 | if [ "$FLAGS_recovery" -eq "$FLAGS_TRUE" ]; then |
| 93 | PRISTINE_IMAGE_NAME=recovery_image.bin |
| 94 | fi |
| 95 | |
Chris Sosa | 9673f3b | 2010-05-18 13:24:40 -0700 | [diff] [blame] | 96 | OUTPUT_IMG=${FLAGS_to:-${OUTPUT_DIR}/${PRISTINE_IMAGE_NAME}} |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 97 | |
| 98 | BOARD="${FLAGS_board}" |
| 99 | BOARD_ROOT="${FLAGS_build_root}/${BOARD}" |
| 100 | |
| 101 | LOOP_DEV= |
Chris Sosa | 4bffb8b | 2010-04-07 17:23:54 -0700 | [diff] [blame] | 102 | STATEFUL_LOOP_DEV= |
Bill Richardson | a81df76 | 2010-04-09 08:12:05 -0700 | [diff] [blame] | 103 | ESP_LOOP_DEV= |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 104 | |
| 105 | # What cross-build are we targeting? |
| 106 | . "${BOARD_ROOT}/etc/make.conf.board_setup" |
| 107 | LIBC_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. |
| 111 | TC_ARCH=$(echo "$CHOST" | awk -F'-' '{ print $1 }') |
| 112 | case "$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 |
| 122 | esac |
| 123 | |
| 124 | # Hack to fix bug where x86_64 CHOST line gets incorrectly added. |
| 125 | # ToDo(msb): remove this hack. |
| 126 | PACKAGES_FILE="${BOARD_ROOT}/packages/Packages" |
| 127 | sudo sed -e "s/CHOST: x86_64-pc-linux-gnu//" -i "${PACKAGES_FILE}" |
| 128 | |
| 129 | # Handle existing directory. |
| 130 | if [[ -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 |
| 139 | fi |
| 140 | |
Bill Richardson | 6795622 | 2010-05-28 15:38:56 -0700 | [diff] [blame] | 141 | # Be verbose to help debug failures. |
| 142 | set -x |
| 143 | |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 144 | # Create the output directory. |
| 145 | mkdir -p "$OUTPUT_DIR" |
| 146 | |
| 147 | cleanup_rootfs_loop() { |
Chris Sosa | bde8c1b | 2010-04-29 14:02:35 -0700 | [diff] [blame] | 148 | sudo umount -d "$ROOT_FS_DIR" |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | cleanup_stateful_fs_loop() { |
Chris Sosa | 4bffb8b | 2010-04-07 17:23:54 -0700 | [diff] [blame] | 152 | sudo umount "${ROOT_FS_DIR}/usr/local" |
| 153 | sudo umount "${ROOT_FS_DIR}/var" |
Chris Sosa | bde8c1b | 2010-04-29 14:02:35 -0700 | [diff] [blame] | 154 | sudo umount -d "${STATEFUL_DIR}" |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Bill Richardson | 8b3bd10 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 157 | cleanup_esp_loop() { |
Chris Sosa | bde8c1b | 2010-04-29 14:02:35 -0700 | [diff] [blame] | 158 | sudo umount -d "$ESP_DIR" |
Bill Richardson | 8b3bd10 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 161 | cleanup() { |
| 162 | # Disable die on error. |
| 163 | set +e |
| 164 | |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 165 | 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 Richardson | a81df76 | 2010-04-09 08:12:05 -0700 | [diff] [blame] | 173 | if [[ -n "$ESP_LOOP_DEV" ]]; then |
Bill Richardson | 8b3bd10 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 174 | cleanup_esp_loop |
| 175 | fi |
| 176 | |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 177 | # Turn die on error back on. |
| 178 | set -e |
| 179 | } |
| 180 | |
Chris Sosa | bde8c1b | 2010-04-29 14:02:35 -0700 | [diff] [blame] | 181 | delete_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 Sosa | 9673f3b | 2010-05-18 13:24:40 -0700 | [diff] [blame] | 195 | # $1 - Directory where developer rootfs is mounted. |
| 196 | # $2 - Directory where developer stateful_partition is mounted. |
| 197 | developer_cleanup() { |
| 198 | "$SCRIPTS_DIR/mount_gpt_image.sh" -u -r "$1" -s "$2" |
| 199 | delete_prompt |
| 200 | } |
| 201 | |
Tan Gao | a40ed44 | 2010-06-02 15:45:19 -0700 | [diff] [blame] | 202 | # Creates a modified image based on $OUTPUT_IMG with additional packages. |
| 203 | create_mod_image() { |
| 204 | local image_type=$1 |
Chris Sosa | 9673f3b | 2010-05-18 13:24:40 -0700 | [diff] [blame] | 205 | local root_fs_dir="${OUTPUT_DIR}/rootfs_dev" |
| 206 | local root_fs_img="${OUTPUT_DIR}/rootfs_dev.image" |
Tan Gao | a40ed44 | 2010-06-02 15:45:19 -0700 | [diff] [blame] | 207 | local image_to_mount=${DEVELOPER_IMAGE_NAME} |
| 208 | local output_img="${OUTPUT_DIR}/${image_to_mount}" |
Chris Sosa | 9673f3b | 2010-05-18 13:24:40 -0700 | [diff] [blame] | 209 | |
| 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 Gao | a40ed44 | 2010-06-02 15:45:19 -0700 | [diff] [blame] | 213 | local file_to_touch=".dev_mode" |
Chris Sosa | 9673f3b | 2010-05-18 13:24:40 -0700 | [diff] [blame] | 214 | |
| 215 | trap "developer_cleanup \"$root_fs_dir\" \"$stateful_dir\"" EXIT |
| 216 | |
Tan Gao | a40ed44 | 2010-06-02 15:45:19 -0700 | [diff] [blame] | 217 | 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 Sosa | 9673f3b | 2010-05-18 13:24:40 -0700 | [diff] [blame] | 226 | $SCRIPTS_DIR/mount_gpt_image.sh --from "$OUTPUT_DIR" \ |
Tan Gao | a40ed44 | 2010-06-02 15:45:19 -0700 | [diff] [blame] | 227 | --image "$image_to_mount" -r "$root_fs_dir" -s "$stateful_dir" |
Chris Sosa | 9673f3b | 2010-05-18 13:24:40 -0700 | [diff] [blame] | 228 | |
Tan Gao | a40ed44 | 2010-06-02 15:45:19 -0700 | [diff] [blame] | 229 | 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 Sosa | 9673f3b | 2010-05-18 13:24:40 -0700 | [diff] [blame] | 234 | |
Tan Gao | a40ed44 | 2010-06-02 15:45:19 -0700 | [diff] [blame] | 235 | # 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 Sosa | 9673f3b | 2010-05-18 13:24:40 -0700 | [diff] [blame] | 244 | |
| 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 Gao | a40ed44 | 2010-06-02 15:45:19 -0700 | [diff] [blame] | 250 | sudo touch "$root_fs_dir/root/$file_to_touch" |
Chris Sosa | 9673f3b | 2010-05-18 13:24:40 -0700 | [diff] [blame] | 251 | |
Tan Gao | a40ed44 | 2010-06-02 15:45:19 -0700 | [diff] [blame] | 252 | if [ "$image_type" == "dev" ]; then |
| 253 | # Additional changes to developer image. |
Chris Sosa | 9673f3b | 2010-05-18 13:24:40 -0700 | [diff] [blame] | 254 | |
Tan Gao | a40ed44 | 2010-06-02 15:45:19 -0700 | [diff] [blame] | 255 | # 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 Sosa | 9673f3b | 2010-05-18 13:24:40 -0700 | [diff] [blame] | 257 | |
Tan Gao | a40ed44 | 2010-06-02 15:45:19 -0700 | [diff] [blame] | 258 | # If vim is installed, then a vi symlink would probably help. |
| 259 | if [[ -x "${root_fs_dir}/usr/local/bin/vim" ]]; then |
| 260 | sudo ln -sf vim "${root_fs_dir}/usr/local/bin/vi" |
| 261 | fi |
Chris Sosa | 9673f3b | 2010-05-18 13:24:40 -0700 | [diff] [blame] | 262 | |
Tan Gao | a40ed44 | 2010-06-02 15:45:19 -0700 | [diff] [blame] | 263 | # Check that the image has been correctly created. Only do it if not |
| 264 | # building a factory install image, as the INSTALL_MASK for it will |
| 265 | # make test_image fail. |
| 266 | if [[ $FLAGS_factory_install -eq ${FLAGS_FALSE} ]] ; then |
| 267 | "${SCRIPTS_DIR}/test_image" \ |
| 268 | --root="$root_fs_dir" \ |
| 269 | --target="$ARCH" |
| 270 | fi |
| 271 | echo "Developer image built and stored at $output_img" |
Girts Folkmanis | 7a8a838 | 2010-05-18 22:52:25 -0700 | [diff] [blame] | 272 | fi |
Chris Sosa | 9673f3b | 2010-05-18 13:24:40 -0700 | [diff] [blame] | 273 | |
| 274 | trap - EXIT |
| 275 | $SCRIPTS_DIR/mount_gpt_image.sh -u -r "$root_fs_dir" -s "$stateful_dir" |
Chris Sosa | d445502 | 2010-05-20 10:14:06 -0700 | [diff] [blame] | 276 | sudo rm -rf "$root_fs_dir" "$stateful_dir" |
Chris Sosa | 9673f3b | 2010-05-18 13:24:40 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Chris Sosa | 4bffb8b | 2010-04-07 17:23:54 -0700 | [diff] [blame] | 279 | # ${DEV_IMAGE_ROOT} specifies the location of where developer packages will |
| 280 | # be installed on the stateful dir. On a Chromium OS system, this will |
| 281 | # translate to /usr/local |
| 282 | DEV_IMAGE_ROOT= |
| 283 | |
Chris Sosa | bde8c1b | 2010-04-29 14:02:35 -0700 | [diff] [blame] | 284 | trap "cleanup && delete_prompt" EXIT |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 285 | |
| 286 | mkdir -p "$ROOT_FS_DIR" |
| 287 | |
| 288 | # Create and format the root file system. |
| 289 | |
| 290 | # Check for loop device before creating image. |
| 291 | LOOP_DEV=$(sudo losetup -f) |
| 292 | if [ -z "$LOOP_DEV" ] ; then |
| 293 | echo "No free loop device. Free up a loop device or reboot. exiting. " |
| 294 | exit 1 |
| 295 | fi |
| 296 | |
| 297 | # Create root file system disk image to fit on a 1GB memory stick. |
| 298 | # 1 GB in hard-drive-manufacturer-speak is 10^9, not 2^30. 950MB < 10^9 bytes. |
Tom Wai-Hong Tam | f87a367 | 2010-05-17 16:06:33 +0800 | [diff] [blame] | 299 | if [[ $FLAGS_factory_install -eq ${FLAGS_TRUE} ]] ; then |
Girts Folkmanis | 7a8a838 | 2010-05-18 22:52:25 -0700 | [diff] [blame] | 300 | ROOT_SIZE_BYTES=$((1024 * 1024 * 300)) |
Tom Wai-Hong Tam | f87a367 | 2010-05-17 16:06:33 +0800 | [diff] [blame] | 301 | else |
Zelidrag Hornung | 1d12c1a | 2010-06-02 10:20:29 -0700 | [diff] [blame] | 302 | ROOT_SIZE_BYTES=$((1024 * 1024 * ${FLAGS_rootfs_size})) |
Tom Wai-Hong Tam | f87a367 | 2010-05-17 16:06:33 +0800 | [diff] [blame] | 303 | fi |
| 304 | |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 305 | dd if=/dev/zero of="$ROOT_FS_IMG" bs=1 count=1 seek=$((ROOT_SIZE_BYTES - 1)) |
| 306 | sudo losetup "$LOOP_DEV" "$ROOT_FS_IMG" |
| 307 | sudo mkfs.ext3 "$LOOP_DEV" |
| 308 | |
| 309 | # Tune and mount rootfs. |
| 310 | UUID=$(uuidgen) |
| 311 | DISK_LABEL="C-KEYFOB" |
| 312 | sudo tune2fs -L "$DISK_LABEL" -U "$UUID" -c 0 -i 0 "$LOOP_DEV" |
| 313 | sudo mount "$LOOP_DEV" "$ROOT_FS_DIR" |
| 314 | |
| 315 | # Create stateful partition of the same size as the rootfs. |
| 316 | STATEFUL_IMG="$OUTPUT_DIR/stateful_partition.image" |
| 317 | STATEFUL_DIR="$OUTPUT_DIR/stateful_partition" |
| 318 | STATEFUL_LOOP_DEV=$(sudo losetup -f) |
| 319 | if [ -z "$STATEFUL_LOOP_DEV" ] ; then |
| 320 | echo "No free loop device. Free up a loop device or reboot. exiting. " |
| 321 | exit 1 |
| 322 | fi |
| 323 | dd if=/dev/zero of="$STATEFUL_IMG" bs=1 count=1 seek=$((ROOT_SIZE_BYTES - 1)) |
| 324 | sudo losetup "$STATEFUL_LOOP_DEV" "$STATEFUL_IMG" |
| 325 | sudo mkfs.ext3 "$STATEFUL_LOOP_DEV" |
| 326 | sudo tune2fs -L "C-STATE" -U "$UUID" -c 0 -i 0 \ |
| 327 | "$STATEFUL_LOOP_DEV" |
| 328 | |
| 329 | # Mount the stateful partition. |
| 330 | mkdir -p "$STATEFUL_DIR" |
| 331 | sudo mount "$STATEFUL_LOOP_DEV" "$STATEFUL_DIR" |
| 332 | |
Chris Sosa | 4bffb8b | 2010-04-07 17:23:54 -0700 | [diff] [blame] | 333 | # Set dev image root now that we have mounted the stateful partition we created |
| 334 | DEV_IMAGE_ROOT="$STATEFUL_DIR/dev_image" |
| 335 | |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 336 | # Turn root file system into bootable image. |
| 337 | if [[ "$ARCH" = "x86" ]]; then |
| 338 | # Setup extlinux configuration. |
| 339 | # TODO: For some reason the /dev/disk/by-uuid is not being generated by udev |
| 340 | # in the initramfs. When we figure that out, switch to root=UUID=$UUID. |
| 341 | sudo mkdir -p "$ROOT_FS_DIR"/boot |
| 342 | # TODO(adlr): use initramfs for booting. |
| 343 | cat <<EOF | sudo dd of="$ROOT_FS_DIR"/boot/extlinux.conf |
| 344 | DEFAULT chromeos-usb |
| 345 | PROMPT 0 |
| 346 | TIMEOUT 0 |
| 347 | |
| 348 | label chromeos-usb |
| 349 | menu label chromeos-usb |
| 350 | kernel vmlinuz |
| 351 | append quiet console=tty2 init=/sbin/init boot=local rootwait root=/dev/sdb3 ro noresume noswap i915.modeset=1 loglevel=1 |
| 352 | |
| 353 | label chromeos-hd |
| 354 | menu label chromeos-hd |
| 355 | kernel vmlinuz |
| 356 | append quiet console=tty2 init=/sbin/init boot=local rootwait root=HDROOT ro noresume noswap i915.modeset=1 loglevel=1 |
| 357 | EOF |
| 358 | |
| 359 | # Make partition bootable and label it. |
| 360 | sudo extlinux -z --install "${ROOT_FS_DIR}/boot" |
| 361 | fi |
| 362 | |
| 363 | # -- Install packages into the root file system -- |
| 364 | |
| 365 | # We need to install libc manually from the cross toolchain. |
| 366 | # TODO: Improve this? We only want libc and not the whole toolchain. |
| 367 | PKGDIR="/var/lib/portage/pkgs/cross/" |
| 368 | sudo tar jxvpf \ |
| 369 | "${PKGDIR}/${CHOST}/cross-${CHOST}"/glibc-${LIBC_VERSION}.tbz2 \ |
| 370 | -C "$ROOT_FS_DIR" --strip-components=3 \ |
| 371 | --exclude=usr/include --exclude=sys-include --exclude=*.a --exclude=*.o |
| 372 | |
| 373 | # We need to install libstdc++ manually from the cross toolchain. |
| 374 | # TODO: Figure out a better way of doing this? |
| 375 | sudo cp -a "${BOARD_ROOT}"/lib/libgcc_s.so* "${ROOT_FS_DIR}/lib" |
| 376 | sudo cp -a "${BOARD_ROOT}"/usr/lib/libstdc++.so* "${ROOT_FS_DIR}/usr/lib" |
| 377 | |
| 378 | INSTALL_MASK="" |
Chris Sosa | 3d9a10b | 2010-04-13 15:00:46 -0700 | [diff] [blame] | 379 | if [[ $FLAGS_installmask -eq ${FLAGS_TRUE} ]] ; then |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 380 | INSTALL_MASK="$DEFAULT_INSTALL_MASK" |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 381 | fi |
| 382 | |
Tom Wai-Hong Tam | f87a367 | 2010-05-17 16:06:33 +0800 | [diff] [blame] | 383 | # Reduce the size of factory install shim. |
| 384 | # TODO: Build a separated ebuild for the factory install shim to reduce size. |
| 385 | if [[ $FLAGS_factory_install -eq ${FLAGS_TRUE} ]] ; then |
| 386 | INSTALL_MASK="$INSTALL_MASK $FACTORY_INSTALL_MASK" |
| 387 | fi |
| 388 | |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 389 | if [[ $FLAGS_jobs -ne -1 ]]; then |
| 390 | EMERGE_JOBS="--jobs=$FLAGS_jobs" |
| 391 | fi |
| 392 | |
Chris Sosa | 4bffb8b | 2010-04-07 17:23:54 -0700 | [diff] [blame] | 393 | # Prepare stateful partition with some pre-created directories |
| 394 | sudo mkdir -p "${DEV_IMAGE_ROOT}" |
| 395 | sudo mkdir -p "${STATEFUL_DIR}/var" |
Bill Richardson | 8b3bd10 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 396 | |
Chris Sosa | 4bffb8b | 2010-04-07 17:23:54 -0700 | [diff] [blame] | 397 | # Create symlinks so that /usr/local/usr based directories are symlinked to |
| 398 | # /usr/local/ directories e.g. /usr/local/usr/bin -> /usr/local/bin, etc. |
Chris Sosa | 702618f | 2010-05-14 12:52:32 -0700 | [diff] [blame] | 399 | setup_symlinks_on_root "${DEV_IMAGE_ROOT}" "${STATEFUL_DIR}/var" \ |
| 400 | "${STATEFUL_DIR}" |
Bill Richardson | 8b3bd10 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 401 | |
Chris Sosa | 4bffb8b | 2010-04-07 17:23:54 -0700 | [diff] [blame] | 402 | # Perform binding rather than symlinking because directories must exist |
| 403 | # on rootfs so that we can bind at run-time since rootfs is read-only |
| 404 | echo "Binding directories from stateful partition onto the rootfs" |
| 405 | sudo mkdir -p "${ROOT_FS_DIR}/usr/local" |
| 406 | sudo mount --bind "${DEV_IMAGE_ROOT}" "${ROOT_FS_DIR}/usr/local" |
| 407 | sudo mkdir -p "${ROOT_FS_DIR}/var" |
| 408 | sudo mount --bind "${STATEFUL_DIR}/var" "${ROOT_FS_DIR}/var" |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 409 | |
| 410 | # We "emerge --root=$ROOT_FS_DIR --root-deps=rdeps --usepkgonly" all of the |
| 411 | # runtime packages for chrome os. This builds up a chrome os image from binary |
| 412 | # packages with runtime dependencies only. We use INSTALL_MASK to trim the |
| 413 | # image size as much as possible. |
Ken Mixter | d5c4b17 | 2010-04-16 10:11:02 -0700 | [diff] [blame] | 414 | sudo INSTALL_MASK="$INSTALL_MASK" emerge-${BOARD} \ |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 415 | --root="$ROOT_FS_DIR" --root-deps=rdeps \ |
Anush Elangovan | bb85f8f | 2010-05-27 11:00:57 -0700 | [diff] [blame] | 416 | --usepkgonly chromeos $EMERGE_JOBS |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 417 | |
Bill Richardson | 8b3bd10 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 418 | # Create EFI System Partition to boot stock EFI BIOS (but not ChromeOS EFI |
| 419 | # BIOS). We only need this for x86, but it's simpler and safer to keep the disk |
| 420 | # images the same for both x86 and ARM. |
| 421 | ESP_IMG=${OUTPUT_DIR}/esp.image |
| 422 | # NOTE: The size argument for mkfs.vfat is in 1024-byte blocks. We'll hard-code |
| 423 | # it to 16M for now. |
| 424 | ESP_BLOCKS=16384 |
| 425 | /usr/sbin/mkfs.vfat -C ${OUTPUT_DIR}/esp.image ${ESP_BLOCKS} |
| 426 | ESP_DIR=${OUTPUT_DIR}/esp |
Bill Richardson | a81df76 | 2010-04-09 08:12:05 -0700 | [diff] [blame] | 427 | ESP_LOOP_DEV=$(sudo losetup -f) |
| 428 | if [ -z "$ESP_LOOP_DEV" ] ; then |
| 429 | echo "No free loop device. Free up a loop device or reboot. exiting. " |
| 430 | exit 1 |
| 431 | fi |
| 432 | mkdir -p "${ESP_DIR}" |
| 433 | sudo losetup "${ESP_LOOP_DEV}" "${ESP_IMG}" |
| 434 | sudo mount "${ESP_LOOP_DEV}" "${ESP_DIR}" |
| 435 | sudo mkdir -p "${ESP_DIR}/efi/boot" |
| 436 | sudo grub-mkimage -p /efi/boot -o "${ESP_DIR}/efi/boot/bootx64.efi" \ |
Bill Richardson | 8b3bd10 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 437 | part_gpt fat ext2 normal boot sh chain configfile linux |
Bill Richardson | 9c5e5ec | 2010-05-14 17:00:21 -0700 | [diff] [blame] | 438 | cat <<'EOF' | sudo dd of="${ESP_DIR}/efi/boot/grub.cfg" |
Bill Richardson | 8b3bd10 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 439 | set default=0 |
Bill Richardson | 9c5e5ec | 2010-05-14 17:00:21 -0700 | [diff] [blame] | 440 | set timeout=2 |
Bill Richardson | 8b3bd10 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 441 | |
Bill Richardson | 9c5e5ec | 2010-05-14 17:00:21 -0700 | [diff] [blame] | 442 | # NOTE: These magic grub variables are a Chrome OS hack. They are not portable. |
| 443 | |
| 444 | menuentry "local image A" { |
| 445 | 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 Richardson | a81df76 | 2010-04-09 08:12:05 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Bill Richardson | 9c5e5ec | 2010-05-14 17:00:21 -0700 | [diff] [blame] | 448 | menuentry "local image B" { |
| 449 | 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 Richardson | 041bd71 | 2010-04-27 09:36:14 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Bill Richardson | 8b3bd10 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 452 | EOF |
| 453 | |
Bill Richardson | 3fefd2a | 2010-06-03 11:03:27 -0700 | [diff] [blame] | 454 | # FIXME: At the moment, we're working on signed images for x86 only. ARM will |
| 455 | # support this before shipping, but at the moment they don't. |
| 456 | if [[ "$ARCH" = "x86" ]]; then |
| 457 | |
| 458 | # Legacy BIOS will use the kernel in the rootfs (via syslinux), as will |
| 459 | # standard EFI BIOS (via grub, from the EFI System Partition). Chrome OS BIOS |
| 460 | # will use a separate signed kernel partition, which we'll create now. |
| 461 | # FIXME: remove serial output, debugging messages |
| 462 | cat <<'EOF' > "${OUTPUT_DIR}/config.txt" |
Bill Richardson | 6795622 | 2010-05-28 15:38:56 -0700 | [diff] [blame] | 463 | earlyprintk=serial,ttyS0,115200 |
| 464 | console=ttyS0,115200 |
| 465 | init=/sbin/init |
| 466 | add_efi_memmap |
| 467 | boot=local |
| 468 | rootwait |
| 469 | root=/dev/sd%D%P |
| 470 | ro |
| 471 | noresume |
| 472 | noswap |
| 473 | i915.modeset=1 |
| 474 | loglevel=7 |
| 475 | Hi_Mom |
| 476 | EOF |
Bill Richardson | 3fefd2a | 2010-06-03 11:03:27 -0700 | [diff] [blame] | 477 | |
| 478 | # FIXME: We need to specify the real keys and certs here! |
| 479 | SIG_DIR="${SRC_ROOT}/platform/vboot_reference/tests/testkeys" |
| 480 | |
| 481 | # Create the kernel partition image. |
| 482 | kernel_utility --generate \ |
| 483 | --firmware_key "${SIG_DIR}/key_rsa4096.pem" \ |
| 484 | --kernel_key "${SIG_DIR}/key_rsa1024.pem" \ |
| 485 | --kernel_key_pub "${SIG_DIR}/key_rsa1024.keyb" \ |
| 486 | --firmware_sign_algorithm 8 \ |
| 487 | --kernel_sign_algorithm 2 \ |
| 488 | --kernel_key_version 1 \ |
| 489 | --kernel_version 1 \ |
| 490 | --config "${OUTPUT_DIR}/config.txt" \ |
| 491 | --bootloader /lib64/bootstub/bootstub.efi \ |
| 492 | --vmlinuz "${ROOT_FS_DIR}/boot/vmlinuz" \ |
| 493 | --out "${OUTPUT_DIR}/vmlinuz.image" |
| 494 | |
| 495 | else |
| 496 | # FIXME: For now, ARM just uses the unsigned kernel by itself. |
| 497 | cp -f "${ROOT_FS_DIR}/boot/vmlinuz" "${OUTPUT_DIR}/vmlinuz.image" |
| 498 | fi |
Bill Richardson | 6795622 | 2010-05-28 15:38:56 -0700 | [diff] [blame] | 499 | |
Bill Richardson | 6795622 | 2010-05-28 15:38:56 -0700 | [diff] [blame] | 500 | |
Chris Sosa | 3f21b99 | 2010-05-10 16:34:47 -0700 | [diff] [blame] | 501 | # Perform any customizations on the root file system that are needed. |
Chris Sosa | 503efe1 | 2010-04-08 10:05:46 -0700 | [diff] [blame] | 502 | "${SCRIPTS_DIR}/customize_rootfs" \ |
| 503 | --root="$ROOT_FS_DIR" \ |
| 504 | --target="$ARCH" \ |
Chris Sosa | 3f21b99 | 2010-05-10 16:34:47 -0700 | [diff] [blame] | 505 | --board="$BOARD" |
Chris Sosa | 503efe1 | 2010-04-08 10:05:46 -0700 | [diff] [blame] | 506 | |
Tom Wai-Hong Tam | f87a367 | 2010-05-17 16:06:33 +0800 | [diff] [blame] | 507 | # Don't test the factory install shim. |
| 508 | if [[ $FLAGS_factory_install -eq ${FLAGS_FALSE} ]] ; then |
| 509 | # Check that the image has been correctly created. |
| 510 | "${SCRIPTS_DIR}/test_image" \ |
| 511 | --root="$ROOT_FS_DIR" \ |
| 512 | --target="$ARCH" |
| 513 | fi |
Chris Sosa | 503efe1 | 2010-04-08 10:05:46 -0700 | [diff] [blame] | 514 | |
Chris Sosa | 4bffb8b | 2010-04-07 17:23:54 -0700 | [diff] [blame] | 515 | # Clean up symlinks so they work on a running target rooted at "/". |
| 516 | # Here development packages are rooted at /usr/local. However, do not |
| 517 | # create /usr/local or /var on host (already exist on target). |
Chris Sosa | 702618f | 2010-05-14 12:52:32 -0700 | [diff] [blame] | 518 | setup_symlinks_on_root "/usr/local" "/var" "${STATEFUL_DIR}" |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 519 | |
| 520 | # Cleanup loop devices. |
Chris Sosa | 4bffb8b | 2010-04-07 17:23:54 -0700 | [diff] [blame] | 521 | cleanup |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 522 | |
Chris Sosa | bde8c1b | 2010-04-29 14:02:35 -0700 | [diff] [blame] | 523 | trap delete_prompt EXIT |
| 524 | |
Tan Gao | 6df5aee | 2010-05-19 14:19:55 -0700 | [diff] [blame] | 525 | RECOVERY="--norecovery" |
| 526 | if [[ ${FLAGS_recovery} -eq $FLAGS_TRUE ]]; then |
| 527 | RECOVERY="--recovery" |
| 528 | fi |
| 529 | |
Bill Richardson | 4364a2e | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 530 | # Create the GPT-formatted image |
| 531 | ${SCRIPTS_DIR}/build_gpt.sh \ |
robotboy | b75eee3 | 2010-04-30 09:51:23 -0700 | [diff] [blame] | 532 | --arch=${ARCH} \ |
| 533 | --board=${FLAGS_board} \ |
| 534 | --arm_extra_bootargs="${FLAGS_arm_extra_bootargs}" \ |
Zelidrag Hornung | 1d12c1a | 2010-06-02 10:20:29 -0700 | [diff] [blame] | 535 | --rootfs_partition_size=${FLAGS_rootfs_partition_size} \ |
Tan Gao | 6df5aee | 2010-05-19 14:19:55 -0700 | [diff] [blame] | 536 | ${RECOVERY} \ |
robotboy | b75eee3 | 2010-04-30 09:51:23 -0700 | [diff] [blame] | 537 | "${OUTPUT_DIR}" \ |
| 538 | "${OUTPUT_IMG}" |
Bill Richardson | 4364a2e | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 539 | |
Bill Richardson | 6dcea16 | 2010-03-31 19:20:24 -0700 | [diff] [blame] | 540 | # Clean up temporary files. |
Bill Richardson | 8b3bd10 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 541 | rm -f "${ROOT_FS_IMG}" "${STATEFUL_IMG}" "${OUTPUT_DIR}/vmlinuz.image" \ |
| 542 | "${ESP_IMG}" |
| 543 | rmdir "${ROOT_FS_DIR}" "${STATEFUL_DIR}" "${ESP_DIR}" |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 544 | |
| 545 | OUTSIDE_OUTPUT_DIR="../build/images/${FLAGS_board}/${IMAGE_SUBDIR}" |
Chris Sosa | 9673f3b | 2010-05-18 13:24:40 -0700 | [diff] [blame] | 546 | |
Tan Gao | a40ed44 | 2010-06-02 15:45:19 -0700 | [diff] [blame] | 547 | # Create a recovery image based on the chromium os base image |
| 548 | [ "$FLAGS_recovery" -eq "$FLAGS_TRUE" ] && create_mod_image "recovery" |
| 549 | trap - EXIT |
| 550 | |
Chris Sosa | 9673f3b | 2010-05-18 13:24:40 -0700 | [diff] [blame] | 551 | # Create a developer image based on the chromium os base image |
Tan Gao | a40ed44 | 2010-06-02 15:45:19 -0700 | [diff] [blame] | 552 | [ "$FLAGS_withdev" -eq "$FLAGS_TRUE" ] && create_mod_image "dev" |
Chris Sosa | 9673f3b | 2010-05-18 13:24:40 -0700 | [diff] [blame] | 553 | trap - EXIT |
| 554 | |
Bill Richardson | 6795622 | 2010-05-28 15:38:56 -0700 | [diff] [blame] | 555 | # be quiet again |
| 556 | set +x |
| 557 | |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 558 | echo "Done. Image created in ${OUTPUT_DIR}" |
Chris Sosa | 9673f3b | 2010-05-18 13:24:40 -0700 | [diff] [blame] | 559 | echo "Chromium OS image created as $PRISTINE_IMAGE_NAME" |
Tan Gao | a40ed44 | 2010-06-02 15:45:19 -0700 | [diff] [blame] | 560 | if [ "$FLAGS_recovery" -eq "$FLAGS_TRUE" ]; then |
| 561 | echo "Recovery image created as $PRISTINE_IMAGE_NAME" |
| 562 | fi |
Chris Sosa | 9673f3b | 2010-05-18 13:24:40 -0700 | [diff] [blame] | 563 | if [ "$FLAGS_withdev" -eq "$FLAGS_TRUE" ]; then |
| 564 | echo "Developer image created as $DEVELOPER_IMAGE_NAME" |
| 565 | fi |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 566 | echo "To copy to USB keyfob, OUTSIDE the chroot, do something like:" |
Daniel Erat | dd9818a | 2010-04-26 09:16:44 -0700 | [diff] [blame] | 567 | echo " ./image_to_usb.sh --from=${OUTSIDE_OUTPUT_DIR} --to=/dev/sdX" |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 568 | echo "To convert to VMWare image, OUTSIDE the chroot, do something like:" |
| 569 | echo " ./image_to_vmware.sh --from=${OUTSIDE_OUTPUT_DIR}" |
| 570 | echo "from the scripts directory where you entered the chroot." |