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." |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 49 | |
| 50 | # Parse command line. |
| 51 | FLAGS "$@" || exit 1 |
| 52 | eval set -- "${FLAGS_ARGV}" |
| 53 | |
| 54 | # Only now can we die on error. shflags functions leak non-zero error codes, |
| 55 | # so will die prematurely if 'set -e' is specified before now. |
| 56 | set -e |
| 57 | |
| 58 | if [ -z "$FLAGS_board" ] ; then |
| 59 | error "--board is required." |
| 60 | exit 1 |
| 61 | fi |
| 62 | |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 63 | # Determine build version. |
| 64 | . "${SCRIPTS_DIR}/chromeos_version.sh" |
| 65 | |
| 66 | # Use canonical path since some tools (e.g. mount) do not like symlinks. |
| 67 | # Append build attempt to output directory. |
| 68 | IMAGE_SUBDIR="${CHROMEOS_VERSION_STRING}-a${FLAGS_build_attempt}" |
| 69 | OUTPUT_DIR="${FLAGS_output_root}/${FLAGS_board}/${IMAGE_SUBDIR}" |
| 70 | ROOT_FS_DIR="${OUTPUT_DIR}/rootfs" |
| 71 | ROOT_FS_IMG="${OUTPUT_DIR}/rootfs.image" |
Chris Sosa | 9673f3b | 2010-05-18 13:24:40 -0700 | [diff] [blame] | 72 | |
| 73 | # If we are creating a developer image, also create a pristine image with a |
| 74 | # different name. |
| 75 | DEVELOPER_IMAGE_NAME= |
| 76 | PRISTINE_IMAGE_NAME=chromiumos_image.bin |
| 77 | if [ "$FLAGS_withdev" -eq "$FLAGS_TRUE" ]; then |
| 78 | PRISTINE_IMAGE_NAME=chromiumos_base_image.bin |
| 79 | DEVELOPER_IMAGE_NAME=chromiumos_image.bin |
| 80 | fi |
| 81 | OUTPUT_IMG=${FLAGS_to:-${OUTPUT_DIR}/${PRISTINE_IMAGE_NAME}} |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 82 | |
| 83 | BOARD="${FLAGS_board}" |
| 84 | BOARD_ROOT="${FLAGS_build_root}/${BOARD}" |
| 85 | |
| 86 | LOOP_DEV= |
Chris Sosa | 4bffb8b | 2010-04-07 17:23:54 -0700 | [diff] [blame] | 87 | STATEFUL_LOOP_DEV= |
Bill Richardson | a81df76 | 2010-04-09 08:12:05 -0700 | [diff] [blame] | 88 | ESP_LOOP_DEV= |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 89 | |
| 90 | # What cross-build are we targeting? |
| 91 | . "${BOARD_ROOT}/etc/make.conf.board_setup" |
| 92 | LIBC_VERSION=${LIBC_VERSION:-"2.10.1-r1"} |
| 93 | |
| 94 | # Figure out ARCH from the given toolchain. |
| 95 | # TODO: Move to common.sh as a function after scripts are switched over. |
| 96 | TC_ARCH=$(echo "$CHOST" | awk -F'-' '{ print $1 }') |
| 97 | case "$TC_ARCH" in |
| 98 | arm*) |
| 99 | ARCH="arm" |
| 100 | ;; |
| 101 | *86) |
| 102 | ARCH="x86" |
| 103 | ;; |
| 104 | *) |
| 105 | error "Unable to determine ARCH from toolchain: $CHOST" |
| 106 | exit 1 |
| 107 | esac |
| 108 | |
| 109 | # Hack to fix bug where x86_64 CHOST line gets incorrectly added. |
| 110 | # ToDo(msb): remove this hack. |
| 111 | PACKAGES_FILE="${BOARD_ROOT}/packages/Packages" |
| 112 | sudo sed -e "s/CHOST: x86_64-pc-linux-gnu//" -i "${PACKAGES_FILE}" |
| 113 | |
| 114 | # Handle existing directory. |
| 115 | if [[ -e "$OUTPUT_DIR" ]]; then |
| 116 | if [[ $FLAGS_replace -eq $FLAGS_TRUE ]]; then |
| 117 | sudo rm -rf "$OUTPUT_DIR" |
| 118 | else |
| 119 | echo "Directory $OUTPUT_DIR already exists." |
| 120 | echo "Use --build_attempt option to specify an unused attempt." |
| 121 | echo "Or use --replace if you want to overwrite this directory." |
| 122 | exit 1 |
| 123 | fi |
| 124 | fi |
| 125 | |
| 126 | # Create the output directory. |
| 127 | mkdir -p "$OUTPUT_DIR" |
| 128 | |
| 129 | cleanup_rootfs_loop() { |
Chris Sosa | bde8c1b | 2010-04-29 14:02:35 -0700 | [diff] [blame] | 130 | sudo umount -d "$ROOT_FS_DIR" |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | cleanup_stateful_fs_loop() { |
Chris Sosa | 4bffb8b | 2010-04-07 17:23:54 -0700 | [diff] [blame] | 134 | sudo umount "${ROOT_FS_DIR}/usr/local" |
| 135 | sudo umount "${ROOT_FS_DIR}/var" |
Chris Sosa | bde8c1b | 2010-04-29 14:02:35 -0700 | [diff] [blame] | 136 | sudo umount -d "${STATEFUL_DIR}" |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Bill Richardson | 8b3bd10 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 139 | cleanup_esp_loop() { |
Chris Sosa | bde8c1b | 2010-04-29 14:02:35 -0700 | [diff] [blame] | 140 | sudo umount -d "$ESP_DIR" |
Bill Richardson | 8b3bd10 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 143 | cleanup() { |
| 144 | # Disable die on error. |
| 145 | set +e |
| 146 | |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 147 | if [[ -n "$STATEFUL_LOOP_DEV" ]]; then |
| 148 | cleanup_stateful_fs_loop |
| 149 | fi |
| 150 | |
| 151 | if [[ -n "$LOOP_DEV" ]]; then |
| 152 | cleanup_rootfs_loop |
| 153 | fi |
| 154 | |
Bill Richardson | a81df76 | 2010-04-09 08:12:05 -0700 | [diff] [blame] | 155 | if [[ -n "$ESP_LOOP_DEV" ]]; then |
Bill Richardson | 8b3bd10 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 156 | cleanup_esp_loop |
| 157 | fi |
| 158 | |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 159 | # Turn die on error back on. |
| 160 | set -e |
| 161 | } |
| 162 | |
Chris Sosa | bde8c1b | 2010-04-29 14:02:35 -0700 | [diff] [blame] | 163 | delete_prompt() { |
| 164 | echo "An error occurred in your build so your latest output directory" \ |
| 165 | "is invalid." |
| 166 | read -p "Would you like to delete the output directory (y/N)? " SURE |
| 167 | SURE="${SURE:0:1}" # Get just the first character |
| 168 | if [ "${SURE}" == "y" ] ; then |
| 169 | sudo rm -rf "$OUTPUT_DIR" |
| 170 | echo "Deleted $OUTPUT_DIR" |
| 171 | else |
| 172 | echo "Not deleting $OUTPUT_DIR. Note dev server updates will not work" \ |
| 173 | "until you successfully build another image or delete this directory" |
| 174 | fi |
| 175 | } |
| 176 | |
Chris Sosa | 9673f3b | 2010-05-18 13:24:40 -0700 | [diff] [blame] | 177 | # $1 - Directory where developer rootfs is mounted. |
| 178 | # $2 - Directory where developer stateful_partition is mounted. |
| 179 | developer_cleanup() { |
| 180 | "$SCRIPTS_DIR/mount_gpt_image.sh" -u -r "$1" -s "$2" |
| 181 | delete_prompt |
| 182 | } |
| 183 | |
| 184 | # Creates a new image based on $OUTPUT_IMG with additional developer packages. |
| 185 | create_developer_image() { |
| 186 | local root_fs_dir="${OUTPUT_DIR}/rootfs_dev" |
| 187 | local root_fs_img="${OUTPUT_DIR}/rootfs_dev.image" |
| 188 | local output_img="${OUTPUT_DIR}/${DEVELOPER_IMAGE_NAME}" |
| 189 | |
| 190 | # Create stateful partition of the same size as the rootfs. |
| 191 | local stateful_img="$OUTPUT_DIR/stateful_partition_dev.image" |
| 192 | local stateful_dir="$OUTPUT_DIR/stateful_partition_dev" |
| 193 | |
| 194 | trap "developer_cleanup \"$root_fs_dir\" \"$stateful_dir\"" EXIT |
| 195 | |
| 196 | # Mount a new copy of the base image. |
| 197 | echo "Creating developer image from base image $OUTPUT_IMG" |
| 198 | cp "$OUTPUT_IMG" "$output_img" |
| 199 | $SCRIPTS_DIR/mount_gpt_image.sh --from "$OUTPUT_DIR" \ |
| 200 | --image "$DEVELOPER_IMAGE_NAME" -r "$root_fs_dir" -s "$stateful_dir" |
| 201 | |
| 202 | # Determine the root dir for developer packages. |
| 203 | local root_dev_dir="$root_fs_dir" |
| 204 | [ $FLAGS_statefuldev -eq $FLAGS_TRUE ] && \ |
| 205 | root_dev_dir="$root_fs_dir/usr/local" |
| 206 | |
| 207 | # Install developer packages described in chromeos-dev. |
| 208 | sudo INSTALL_MASK="$INSTALL_MASK" emerge-${BOARD} \ |
| 209 | --root="$root_dev_dir" --root-deps=rdeps \ |
Anush Elangovan | 2991604 | 2010-05-21 10:09:04 -0700 | [diff] [blame] | 210 | --getbinpkg --usepkg chromeos-dev $EMERGE_JOBS |
Chris Sosa | 9673f3b | 2010-05-18 13:24:40 -0700 | [diff] [blame] | 211 | |
| 212 | # Re-run ldconfig to fix /etc/ldconfig.so.cache. |
| 213 | sudo /sbin/ldconfig -r "$root_fs_dir" |
| 214 | |
| 215 | # Mark the image as a developer image (input to chromeos_startup). |
| 216 | sudo mkdir -p "$root_fs_dir/root" |
| 217 | sudo touch "$root_fs_dir/root/.dev_mode" |
| 218 | |
| 219 | # Additional changes to developer image. |
| 220 | |
| 221 | # The ldd tool is a useful shell script but lives in glibc; just copy it. |
| 222 | sudo cp -a "$(which ldd)" "${root_dev_dir}/usr/bin" |
| 223 | |
Chris Sosa | b689577 | 2010-05-25 17:47:51 -0700 | [diff] [blame] | 224 | # TODO: Temporarily create fake xterm symlink until we do proper xinitrc |
| 225 | local aterm="$root_fs_dir/usr/local/bin/aterm" |
| 226 | if [[ -f "${aterm}" ]]; then |
| 227 | sudo chmod 0755 "$aterm" |
| 228 | sudo ln -s aterm "${root_fs_dir}/usr/local/bin/xterm" |
| 229 | fi |
| 230 | |
Chris Sosa | 9673f3b | 2010-05-18 13:24:40 -0700 | [diff] [blame] | 231 | # If vim is installed, then a vi symlink would probably help. |
Chris Sosa | b689577 | 2010-05-25 17:47:51 -0700 | [diff] [blame] | 232 | if [[ -x "${root_fs_dir}/usr/local/bin/vim" ]]; then |
| 233 | sudo ln -sf vim "${root_fs_dir}/usr/local/bin/vi" |
Chris Sosa | 9673f3b | 2010-05-18 13:24:40 -0700 | [diff] [blame] | 234 | fi |
| 235 | |
Girts Folkmanis | 7a8a838 | 2010-05-18 22:52:25 -0700 | [diff] [blame] | 236 | # Check that the image has been correctly created. Only do it if not |
| 237 | # building a factory install image, as the INSTALL_MASK for it will |
| 238 | # make test_image fail. |
| 239 | if [[ $FLAGS_factory_install -eq ${FLAGS_FALSE} ]] ; then |
| 240 | "${SCRIPTS_DIR}/test_image" \ |
| 241 | --root="$root_fs_dir" \ |
| 242 | --target="$ARCH" |
| 243 | fi |
Chris Sosa | 9673f3b | 2010-05-18 13:24:40 -0700 | [diff] [blame] | 244 | |
| 245 | trap - EXIT |
| 246 | $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] | 247 | sudo rm -rf "$root_fs_dir" "$stateful_dir" |
Chris Sosa | 9673f3b | 2010-05-18 13:24:40 -0700 | [diff] [blame] | 248 | echo "Developer image built and stored at $output_img" |
| 249 | } |
| 250 | |
Chris Sosa | 4bffb8b | 2010-04-07 17:23:54 -0700 | [diff] [blame] | 251 | # ${DEV_IMAGE_ROOT} specifies the location of where developer packages will |
| 252 | # be installed on the stateful dir. On a Chromium OS system, this will |
| 253 | # translate to /usr/local |
| 254 | DEV_IMAGE_ROOT= |
| 255 | |
Chris Sosa | bde8c1b | 2010-04-29 14:02:35 -0700 | [diff] [blame] | 256 | trap "cleanup && delete_prompt" EXIT |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 257 | |
| 258 | mkdir -p "$ROOT_FS_DIR" |
| 259 | |
| 260 | # Create and format the root file system. |
| 261 | |
| 262 | # Check for loop device before creating image. |
| 263 | LOOP_DEV=$(sudo losetup -f) |
| 264 | if [ -z "$LOOP_DEV" ] ; then |
| 265 | echo "No free loop device. Free up a loop device or reboot. exiting. " |
| 266 | exit 1 |
| 267 | fi |
| 268 | |
| 269 | # Create root file system disk image to fit on a 1GB memory stick. |
| 270 | # 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] | 271 | if [[ $FLAGS_factory_install -eq ${FLAGS_TRUE} ]] ; then |
Girts Folkmanis | 7a8a838 | 2010-05-18 22:52:25 -0700 | [diff] [blame] | 272 | ROOT_SIZE_BYTES=$((1024 * 1024 * 300)) |
Tom Wai-Hong Tam | f87a367 | 2010-05-17 16:06:33 +0800 | [diff] [blame] | 273 | else |
| 274 | ROOT_SIZE_BYTES=$((1024 * 1024 * 720)) |
| 275 | fi |
| 276 | |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 277 | dd if=/dev/zero of="$ROOT_FS_IMG" bs=1 count=1 seek=$((ROOT_SIZE_BYTES - 1)) |
| 278 | sudo losetup "$LOOP_DEV" "$ROOT_FS_IMG" |
| 279 | sudo mkfs.ext3 "$LOOP_DEV" |
| 280 | |
| 281 | # Tune and mount rootfs. |
| 282 | UUID=$(uuidgen) |
| 283 | DISK_LABEL="C-KEYFOB" |
| 284 | sudo tune2fs -L "$DISK_LABEL" -U "$UUID" -c 0 -i 0 "$LOOP_DEV" |
| 285 | sudo mount "$LOOP_DEV" "$ROOT_FS_DIR" |
| 286 | |
| 287 | # Create stateful partition of the same size as the rootfs. |
| 288 | STATEFUL_IMG="$OUTPUT_DIR/stateful_partition.image" |
| 289 | STATEFUL_DIR="$OUTPUT_DIR/stateful_partition" |
| 290 | STATEFUL_LOOP_DEV=$(sudo losetup -f) |
| 291 | if [ -z "$STATEFUL_LOOP_DEV" ] ; then |
| 292 | echo "No free loop device. Free up a loop device or reboot. exiting. " |
| 293 | exit 1 |
| 294 | fi |
| 295 | dd if=/dev/zero of="$STATEFUL_IMG" bs=1 count=1 seek=$((ROOT_SIZE_BYTES - 1)) |
| 296 | sudo losetup "$STATEFUL_LOOP_DEV" "$STATEFUL_IMG" |
| 297 | sudo mkfs.ext3 "$STATEFUL_LOOP_DEV" |
| 298 | sudo tune2fs -L "C-STATE" -U "$UUID" -c 0 -i 0 \ |
| 299 | "$STATEFUL_LOOP_DEV" |
| 300 | |
| 301 | # Mount the stateful partition. |
| 302 | mkdir -p "$STATEFUL_DIR" |
| 303 | sudo mount "$STATEFUL_LOOP_DEV" "$STATEFUL_DIR" |
| 304 | |
Chris Sosa | 4bffb8b | 2010-04-07 17:23:54 -0700 | [diff] [blame] | 305 | # Set dev image root now that we have mounted the stateful partition we created |
| 306 | DEV_IMAGE_ROOT="$STATEFUL_DIR/dev_image" |
| 307 | |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 308 | # Turn root file system into bootable image. |
| 309 | if [[ "$ARCH" = "x86" ]]; then |
| 310 | # Setup extlinux configuration. |
| 311 | # TODO: For some reason the /dev/disk/by-uuid is not being generated by udev |
| 312 | # in the initramfs. When we figure that out, switch to root=UUID=$UUID. |
| 313 | sudo mkdir -p "$ROOT_FS_DIR"/boot |
| 314 | # TODO(adlr): use initramfs for booting. |
| 315 | cat <<EOF | sudo dd of="$ROOT_FS_DIR"/boot/extlinux.conf |
| 316 | DEFAULT chromeos-usb |
| 317 | PROMPT 0 |
| 318 | TIMEOUT 0 |
| 319 | |
| 320 | label chromeos-usb |
| 321 | menu label chromeos-usb |
| 322 | kernel vmlinuz |
| 323 | append quiet console=tty2 init=/sbin/init boot=local rootwait root=/dev/sdb3 ro noresume noswap i915.modeset=1 loglevel=1 |
| 324 | |
| 325 | label chromeos-hd |
| 326 | menu label chromeos-hd |
| 327 | kernel vmlinuz |
| 328 | append quiet console=tty2 init=/sbin/init boot=local rootwait root=HDROOT ro noresume noswap i915.modeset=1 loglevel=1 |
| 329 | EOF |
| 330 | |
| 331 | # Make partition bootable and label it. |
| 332 | sudo extlinux -z --install "${ROOT_FS_DIR}/boot" |
| 333 | fi |
| 334 | |
| 335 | # -- Install packages into the root file system -- |
| 336 | |
| 337 | # We need to install libc manually from the cross toolchain. |
| 338 | # TODO: Improve this? We only want libc and not the whole toolchain. |
| 339 | PKGDIR="/var/lib/portage/pkgs/cross/" |
| 340 | sudo tar jxvpf \ |
| 341 | "${PKGDIR}/${CHOST}/cross-${CHOST}"/glibc-${LIBC_VERSION}.tbz2 \ |
| 342 | -C "$ROOT_FS_DIR" --strip-components=3 \ |
| 343 | --exclude=usr/include --exclude=sys-include --exclude=*.a --exclude=*.o |
| 344 | |
| 345 | # We need to install libstdc++ manually from the cross toolchain. |
| 346 | # TODO: Figure out a better way of doing this? |
| 347 | sudo cp -a "${BOARD_ROOT}"/lib/libgcc_s.so* "${ROOT_FS_DIR}/lib" |
| 348 | sudo cp -a "${BOARD_ROOT}"/usr/lib/libstdc++.so* "${ROOT_FS_DIR}/usr/lib" |
| 349 | |
| 350 | INSTALL_MASK="" |
Chris Sosa | 3d9a10b | 2010-04-13 15:00:46 -0700 | [diff] [blame] | 351 | if [[ $FLAGS_installmask -eq ${FLAGS_TRUE} ]] ; then |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 352 | INSTALL_MASK="$DEFAULT_INSTALL_MASK" |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 353 | fi |
| 354 | |
Tom Wai-Hong Tam | f87a367 | 2010-05-17 16:06:33 +0800 | [diff] [blame] | 355 | # Reduce the size of factory install shim. |
| 356 | # TODO: Build a separated ebuild for the factory install shim to reduce size. |
| 357 | if [[ $FLAGS_factory_install -eq ${FLAGS_TRUE} ]] ; then |
| 358 | INSTALL_MASK="$INSTALL_MASK $FACTORY_INSTALL_MASK" |
| 359 | fi |
| 360 | |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 361 | if [[ $FLAGS_jobs -ne -1 ]]; then |
| 362 | EMERGE_JOBS="--jobs=$FLAGS_jobs" |
| 363 | fi |
| 364 | |
Chris Sosa | 4bffb8b | 2010-04-07 17:23:54 -0700 | [diff] [blame] | 365 | # Prepare stateful partition with some pre-created directories |
| 366 | sudo mkdir -p "${DEV_IMAGE_ROOT}" |
| 367 | sudo mkdir -p "${STATEFUL_DIR}/var" |
Bill Richardson | 8b3bd10 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 368 | |
Chris Sosa | 4bffb8b | 2010-04-07 17:23:54 -0700 | [diff] [blame] | 369 | # Create symlinks so that /usr/local/usr based directories are symlinked to |
| 370 | # /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] | 371 | setup_symlinks_on_root "${DEV_IMAGE_ROOT}" "${STATEFUL_DIR}/var" \ |
| 372 | "${STATEFUL_DIR}" |
Bill Richardson | 8b3bd10 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 373 | |
Chris Sosa | 4bffb8b | 2010-04-07 17:23:54 -0700 | [diff] [blame] | 374 | # Perform binding rather than symlinking because directories must exist |
| 375 | # on rootfs so that we can bind at run-time since rootfs is read-only |
| 376 | echo "Binding directories from stateful partition onto the rootfs" |
| 377 | sudo mkdir -p "${ROOT_FS_DIR}/usr/local" |
| 378 | sudo mount --bind "${DEV_IMAGE_ROOT}" "${ROOT_FS_DIR}/usr/local" |
| 379 | sudo mkdir -p "${ROOT_FS_DIR}/var" |
| 380 | sudo mount --bind "${STATEFUL_DIR}/var" "${ROOT_FS_DIR}/var" |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 381 | |
| 382 | # We "emerge --root=$ROOT_FS_DIR --root-deps=rdeps --usepkgonly" all of the |
| 383 | # runtime packages for chrome os. This builds up a chrome os image from binary |
| 384 | # packages with runtime dependencies only. We use INSTALL_MASK to trim the |
| 385 | # image size as much as possible. |
Ken Mixter | d5c4b17 | 2010-04-16 10:11:02 -0700 | [diff] [blame] | 386 | sudo INSTALL_MASK="$INSTALL_MASK" emerge-${BOARD} \ |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 387 | --root="$ROOT_FS_DIR" --root-deps=rdeps \ |
Anush Elangovan | 2991604 | 2010-05-21 10:09:04 -0700 | [diff] [blame] | 388 | --getbinpkg --usepkg chromeos $EMERGE_JOBS |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 389 | |
Bill Richardson | 4364a2e | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 390 | # Extract the kernel from the root filesystem for use by the GPT image. Legacy |
Bill Richardson | 8b3bd10 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 391 | # BIOS will use the kernel in the rootfs (via syslinux), Chrome OS BIOS will |
| 392 | # use the kernel partition. |
Bill Richardson | 6dcea16 | 2010-03-31 19:20:24 -0700 | [diff] [blame] | 393 | sudo cp -f "${ROOT_FS_DIR}/boot/vmlinuz" "${OUTPUT_DIR}/vmlinuz.image" |
Bill Richardson | 4364a2e | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 394 | |
Bill Richardson | 8b3bd10 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 395 | # Create EFI System Partition to boot stock EFI BIOS (but not ChromeOS EFI |
| 396 | # BIOS). We only need this for x86, but it's simpler and safer to keep the disk |
| 397 | # images the same for both x86 and ARM. |
| 398 | ESP_IMG=${OUTPUT_DIR}/esp.image |
| 399 | # NOTE: The size argument for mkfs.vfat is in 1024-byte blocks. We'll hard-code |
| 400 | # it to 16M for now. |
| 401 | ESP_BLOCKS=16384 |
| 402 | /usr/sbin/mkfs.vfat -C ${OUTPUT_DIR}/esp.image ${ESP_BLOCKS} |
| 403 | ESP_DIR=${OUTPUT_DIR}/esp |
Bill Richardson | a81df76 | 2010-04-09 08:12:05 -0700 | [diff] [blame] | 404 | ESP_LOOP_DEV=$(sudo losetup -f) |
| 405 | if [ -z "$ESP_LOOP_DEV" ] ; then |
| 406 | echo "No free loop device. Free up a loop device or reboot. exiting. " |
| 407 | exit 1 |
| 408 | fi |
| 409 | mkdir -p "${ESP_DIR}" |
| 410 | sudo losetup "${ESP_LOOP_DEV}" "${ESP_IMG}" |
| 411 | sudo mount "${ESP_LOOP_DEV}" "${ESP_DIR}" |
| 412 | sudo mkdir -p "${ESP_DIR}/efi/boot" |
| 413 | 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] | 414 | part_gpt fat ext2 normal boot sh chain configfile linux |
Bill Richardson | 9c5e5ec | 2010-05-14 17:00:21 -0700 | [diff] [blame] | 415 | cat <<'EOF' | sudo dd of="${ESP_DIR}/efi/boot/grub.cfg" |
Bill Richardson | 8b3bd10 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 416 | set default=0 |
Bill Richardson | 9c5e5ec | 2010-05-14 17:00:21 -0700 | [diff] [blame] | 417 | set timeout=2 |
Bill Richardson | 8b3bd10 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 418 | |
Bill Richardson | 9c5e5ec | 2010-05-14 17:00:21 -0700 | [diff] [blame] | 419 | # NOTE: These magic grub variables are a Chrome OS hack. They are not portable. |
| 420 | |
| 421 | menuentry "local image A" { |
| 422 | 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] | 423 | } |
| 424 | |
Bill Richardson | 9c5e5ec | 2010-05-14 17:00:21 -0700 | [diff] [blame] | 425 | menuentry "local image B" { |
| 426 | 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] | 427 | } |
| 428 | |
Bill Richardson | 8b3bd10 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 429 | EOF |
| 430 | |
Chris Sosa | 3f21b99 | 2010-05-10 16:34:47 -0700 | [diff] [blame] | 431 | # Perform any customizations on the root file system that are needed. |
Chris Sosa | 503efe1 | 2010-04-08 10:05:46 -0700 | [diff] [blame] | 432 | "${SCRIPTS_DIR}/customize_rootfs" \ |
| 433 | --root="$ROOT_FS_DIR" \ |
| 434 | --target="$ARCH" \ |
Chris Sosa | 3f21b99 | 2010-05-10 16:34:47 -0700 | [diff] [blame] | 435 | --board="$BOARD" |
Chris Sosa | 503efe1 | 2010-04-08 10:05:46 -0700 | [diff] [blame] | 436 | |
Tom Wai-Hong Tam | f87a367 | 2010-05-17 16:06:33 +0800 | [diff] [blame] | 437 | # Don't test the factory install shim. |
| 438 | if [[ $FLAGS_factory_install -eq ${FLAGS_FALSE} ]] ; then |
| 439 | # Check that the image has been correctly created. |
| 440 | "${SCRIPTS_DIR}/test_image" \ |
| 441 | --root="$ROOT_FS_DIR" \ |
| 442 | --target="$ARCH" |
| 443 | fi |
Chris Sosa | 503efe1 | 2010-04-08 10:05:46 -0700 | [diff] [blame] | 444 | |
Chris Sosa | 4bffb8b | 2010-04-07 17:23:54 -0700 | [diff] [blame] | 445 | # Clean up symlinks so they work on a running target rooted at "/". |
| 446 | # Here development packages are rooted at /usr/local. However, do not |
| 447 | # create /usr/local or /var on host (already exist on target). |
Chris Sosa | 702618f | 2010-05-14 12:52:32 -0700 | [diff] [blame] | 448 | setup_symlinks_on_root "/usr/local" "/var" "${STATEFUL_DIR}" |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 449 | |
| 450 | # Cleanup loop devices. |
Chris Sosa | 4bffb8b | 2010-04-07 17:23:54 -0700 | [diff] [blame] | 451 | cleanup |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 452 | |
Chris Sosa | bde8c1b | 2010-04-29 14:02:35 -0700 | [diff] [blame] | 453 | trap delete_prompt EXIT |
| 454 | |
Tan Gao | 6df5aee | 2010-05-19 14:19:55 -0700 | [diff] [blame] | 455 | RECOVERY="--norecovery" |
| 456 | if [[ ${FLAGS_recovery} -eq $FLAGS_TRUE ]]; then |
| 457 | RECOVERY="--recovery" |
| 458 | fi |
| 459 | |
Bill Richardson | 4364a2e | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 460 | # Create the GPT-formatted image |
| 461 | ${SCRIPTS_DIR}/build_gpt.sh \ |
robotboy | b75eee3 | 2010-04-30 09:51:23 -0700 | [diff] [blame] | 462 | --arch=${ARCH} \ |
| 463 | --board=${FLAGS_board} \ |
| 464 | --arm_extra_bootargs="${FLAGS_arm_extra_bootargs}" \ |
Tan Gao | 6df5aee | 2010-05-19 14:19:55 -0700 | [diff] [blame] | 465 | ${RECOVERY} \ |
robotboy | b75eee3 | 2010-04-30 09:51:23 -0700 | [diff] [blame] | 466 | "${OUTPUT_DIR}" \ |
| 467 | "${OUTPUT_IMG}" |
Bill Richardson | 4364a2e | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 468 | |
Bill Richardson | 6dcea16 | 2010-03-31 19:20:24 -0700 | [diff] [blame] | 469 | # Clean up temporary files. |
Bill Richardson | 8b3bd10 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 470 | rm -f "${ROOT_FS_IMG}" "${STATEFUL_IMG}" "${OUTPUT_DIR}/vmlinuz.image" \ |
| 471 | "${ESP_IMG}" |
| 472 | rmdir "${ROOT_FS_DIR}" "${STATEFUL_DIR}" "${ESP_DIR}" |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 473 | |
| 474 | OUTSIDE_OUTPUT_DIR="../build/images/${FLAGS_board}/${IMAGE_SUBDIR}" |
Chris Sosa | 9673f3b | 2010-05-18 13:24:40 -0700 | [diff] [blame] | 475 | |
| 476 | # Create a developer image based on the chromium os base image |
| 477 | [ "$FLAGS_withdev" -eq "$FLAGS_TRUE" ] && create_developer_image |
| 478 | trap - EXIT |
| 479 | |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 480 | echo "Done. Image created in ${OUTPUT_DIR}" |
Chris Sosa | 9673f3b | 2010-05-18 13:24:40 -0700 | [diff] [blame] | 481 | echo "Chromium OS image created as $PRISTINE_IMAGE_NAME" |
| 482 | if [ "$FLAGS_withdev" -eq "$FLAGS_TRUE" ]; then |
| 483 | echo "Developer image created as $DEVELOPER_IMAGE_NAME" |
| 484 | fi |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 485 | echo "To copy to USB keyfob, OUTSIDE the chroot, do something like:" |
Daniel Erat | dd9818a | 2010-04-26 09:16:44 -0700 | [diff] [blame] | 486 | echo " ./image_to_usb.sh --from=${OUTSIDE_OUTPUT_DIR} --to=/dev/sdX" |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 487 | echo "To convert to VMWare image, OUTSIDE the chroot, do something like:" |
| 488 | echo " ./image_to_vmware.sh --from=${OUTSIDE_OUTPUT_DIR}" |
| 489 | echo "from the scripts directory where you entered the chroot." |