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. |
| 17 | assert_inside_chroot |
| 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." |
| 38 | DEFINE_boolean statefuldev $FLAGS_FALSE \ |
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" |
Chris Sosa | 77a2963 | 2010-03-15 14:12:15 -0700 | [diff] [blame] | 42 | DEFINE_boolean withtest $FLAGS_FALSE \ |
Chris Sosa | 4bccd3c | 2010-03-15 13:23:14 -0700 | [diff] [blame] | 43 | "Include packages required for testing and prepare image for testing" |
Chris Sosa | 1e5fe62 | 2010-04-08 20:51:53 -0700 | [diff] [blame] | 44 | DEFINE_string factory_server "" \ |
Andrew de los Reyes | e7a04ad | 2010-04-08 15:58:17 -0700 | [diff] [blame] | 45 | "Build a factory install image pointing to given server." |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 46 | |
| 47 | # Parse command line. |
| 48 | FLAGS "$@" || exit 1 |
| 49 | eval set -- "${FLAGS_ARGV}" |
| 50 | |
| 51 | # Only now can we die on error. shflags functions leak non-zero error codes, |
| 52 | # so will die prematurely if 'set -e' is specified before now. |
| 53 | set -e |
| 54 | |
| 55 | if [ -z "$FLAGS_board" ] ; then |
| 56 | error "--board is required." |
| 57 | exit 1 |
| 58 | fi |
| 59 | |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 60 | # Determine build version. |
| 61 | . "${SCRIPTS_DIR}/chromeos_version.sh" |
| 62 | |
| 63 | # Use canonical path since some tools (e.g. mount) do not like symlinks. |
| 64 | # Append build attempt to output directory. |
| 65 | IMAGE_SUBDIR="${CHROMEOS_VERSION_STRING}-a${FLAGS_build_attempt}" |
| 66 | OUTPUT_DIR="${FLAGS_output_root}/${FLAGS_board}/${IMAGE_SUBDIR}" |
| 67 | ROOT_FS_DIR="${OUTPUT_DIR}/rootfs" |
| 68 | ROOT_FS_IMG="${OUTPUT_DIR}/rootfs.image" |
Antoine Labour | e9e585f | 2010-04-01 15:57:57 -0700 | [diff] [blame] | 69 | OUTPUT_IMG=${FLAGS_to:-${OUTPUT_DIR}/chromiumos_image.bin} |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 70 | |
| 71 | BOARD="${FLAGS_board}" |
| 72 | BOARD_ROOT="${FLAGS_build_root}/${BOARD}" |
| 73 | |
| 74 | LOOP_DEV= |
Chris Sosa | 4bffb8b | 2010-04-07 17:23:54 -0700 | [diff] [blame] | 75 | STATEFUL_LOOP_DEV= |
Bill Richardson | a81df76 | 2010-04-09 08:12:05 -0700 | [diff] [blame] | 76 | ESP_LOOP_DEV= |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 77 | |
| 78 | # What cross-build are we targeting? |
| 79 | . "${BOARD_ROOT}/etc/make.conf.board_setup" |
| 80 | LIBC_VERSION=${LIBC_VERSION:-"2.10.1-r1"} |
| 81 | |
| 82 | # Figure out ARCH from the given toolchain. |
| 83 | # TODO: Move to common.sh as a function after scripts are switched over. |
| 84 | TC_ARCH=$(echo "$CHOST" | awk -F'-' '{ print $1 }') |
| 85 | case "$TC_ARCH" in |
| 86 | arm*) |
| 87 | ARCH="arm" |
| 88 | ;; |
| 89 | *86) |
| 90 | ARCH="x86" |
| 91 | ;; |
| 92 | *) |
| 93 | error "Unable to determine ARCH from toolchain: $CHOST" |
| 94 | exit 1 |
| 95 | esac |
| 96 | |
| 97 | # Hack to fix bug where x86_64 CHOST line gets incorrectly added. |
| 98 | # ToDo(msb): remove this hack. |
| 99 | PACKAGES_FILE="${BOARD_ROOT}/packages/Packages" |
| 100 | sudo sed -e "s/CHOST: x86_64-pc-linux-gnu//" -i "${PACKAGES_FILE}" |
| 101 | |
| 102 | # Handle existing directory. |
| 103 | if [[ -e "$OUTPUT_DIR" ]]; then |
| 104 | if [[ $FLAGS_replace -eq $FLAGS_TRUE ]]; then |
| 105 | sudo rm -rf "$OUTPUT_DIR" |
| 106 | else |
| 107 | echo "Directory $OUTPUT_DIR already exists." |
| 108 | echo "Use --build_attempt option to specify an unused attempt." |
| 109 | echo "Or use --replace if you want to overwrite this directory." |
| 110 | exit 1 |
| 111 | fi |
| 112 | fi |
| 113 | |
| 114 | # Create the output directory. |
| 115 | mkdir -p "$OUTPUT_DIR" |
| 116 | |
| 117 | cleanup_rootfs_loop() { |
| 118 | sudo umount "$LOOP_DEV" |
Chris Sosa | 4bffb8b | 2010-04-07 17:23:54 -0700 | [diff] [blame] | 119 | sleep 1 # in case $LOOP_DEV is in use. |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 120 | sudo losetup -d "$LOOP_DEV" |
| 121 | } |
| 122 | |
| 123 | cleanup_stateful_fs_loop() { |
Chris Sosa | 4bffb8b | 2010-04-07 17:23:54 -0700 | [diff] [blame] | 124 | sudo umount "${ROOT_FS_DIR}/usr/local" |
| 125 | sudo umount "${ROOT_FS_DIR}/var" |
| 126 | sudo umount "${STATEFUL_DIR}" |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 127 | sleep 1 # follows from cleanup_root_fs_loop. |
| 128 | sudo losetup -d "$STATEFUL_LOOP_DEV" |
| 129 | } |
| 130 | |
Bill Richardson | 8b3bd10 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 131 | cleanup_esp_loop() { |
| 132 | sudo umount "$ESP_DIR" |
Bill Richardson | a81df76 | 2010-04-09 08:12:05 -0700 | [diff] [blame] | 133 | sleep 1 |
| 134 | sudo losetup -d "$ESP_LOOP_DEV" |
Bill Richardson | 8b3bd10 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 137 | cleanup() { |
| 138 | # Disable die on error. |
| 139 | set +e |
| 140 | |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 141 | if [[ -n "$STATEFUL_LOOP_DEV" ]]; then |
| 142 | cleanup_stateful_fs_loop |
| 143 | fi |
| 144 | |
| 145 | if [[ -n "$LOOP_DEV" ]]; then |
| 146 | cleanup_rootfs_loop |
| 147 | fi |
| 148 | |
Bill Richardson | a81df76 | 2010-04-09 08:12:05 -0700 | [diff] [blame] | 149 | if [[ -n "$ESP_LOOP_DEV" ]]; then |
Bill Richardson | 8b3bd10 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 150 | cleanup_esp_loop |
| 151 | fi |
| 152 | |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 153 | # Turn die on error back on. |
| 154 | set -e |
| 155 | } |
| 156 | |
Chris Sosa | 4bffb8b | 2010-04-07 17:23:54 -0700 | [diff] [blame] | 157 | # ${DEV_IMAGE_ROOT} specifies the location of where developer packages will |
| 158 | # be installed on the stateful dir. On a Chromium OS system, this will |
| 159 | # translate to /usr/local |
| 160 | DEV_IMAGE_ROOT= |
| 161 | |
| 162 | # Sets up symlinks for the stateful partition based on the root specified by |
| 163 | # ${1} and var directory specified by ${2}. |
| 164 | setup_symlinks_on_root() { |
| 165 | echo "Setting up symlinks on the stateful partition rooted at ${1} with"\ |
| 166 | "var directory located at ${2}" |
| 167 | |
| 168 | for path in usr local; do |
| 169 | if [ -h "${DEV_IMAGE_ROOT}/${path}" ] ; then |
| 170 | sudo unlink "${DEV_IMAGE_ROOT}/${path}" |
| 171 | elif [ -e "${DEV_IMAGE_ROOT}/${path}" ] ; then |
| 172 | echo "*** ERROR: ${DEV_IMAGE_ROOT}/${path} should be a symlink if exists" |
| 173 | return 1 |
| 174 | fi |
| 175 | sudo ln -s ${1} "${DEV_IMAGE_ROOT}/${path}" |
| 176 | done |
| 177 | |
| 178 | # Setup var. Var is on the stateful partition at /var for both non-developer |
| 179 | # builds and developer builds. |
| 180 | if [ -h "${DEV_IMAGE_ROOT}/var" ] ; then |
| 181 | sudo unlink "${DEV_IMAGE_ROOT}/var" |
| 182 | elif [ -e "${DEV_IMAGE_ROOT}/var" ] ; then |
| 183 | echo "*** ERROR: ${DEV_IMAGE_ROOT}/var should be a symlink if it exists" |
| 184 | return 1 |
| 185 | fi |
| 186 | |
| 187 | sudo ln -s "${2}" "${DEV_IMAGE_ROOT}/var" |
| 188 | } |
| 189 | |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 190 | trap cleanup EXIT |
| 191 | |
| 192 | mkdir -p "$ROOT_FS_DIR" |
| 193 | |
| 194 | # Create and format the root file system. |
| 195 | |
| 196 | # Check for loop device before creating image. |
| 197 | LOOP_DEV=$(sudo losetup -f) |
| 198 | if [ -z "$LOOP_DEV" ] ; then |
| 199 | echo "No free loop device. Free up a loop device or reboot. exiting. " |
| 200 | exit 1 |
| 201 | fi |
| 202 | |
| 203 | # Create root file system disk image to fit on a 1GB memory stick. |
| 204 | # 1 GB in hard-drive-manufacturer-speak is 10^9, not 2^30. 950MB < 10^9 bytes. |
Chris Masone | 35a83f9 | 2010-04-05 16:51:53 -0700 | [diff] [blame] | 205 | ROOT_SIZE_BYTES=$((1024 * 1024 * 720)) |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 206 | dd if=/dev/zero of="$ROOT_FS_IMG" bs=1 count=1 seek=$((ROOT_SIZE_BYTES - 1)) |
| 207 | sudo losetup "$LOOP_DEV" "$ROOT_FS_IMG" |
| 208 | sudo mkfs.ext3 "$LOOP_DEV" |
| 209 | |
| 210 | # Tune and mount rootfs. |
| 211 | UUID=$(uuidgen) |
| 212 | DISK_LABEL="C-KEYFOB" |
| 213 | sudo tune2fs -L "$DISK_LABEL" -U "$UUID" -c 0 -i 0 "$LOOP_DEV" |
| 214 | sudo mount "$LOOP_DEV" "$ROOT_FS_DIR" |
| 215 | |
| 216 | # Create stateful partition of the same size as the rootfs. |
| 217 | STATEFUL_IMG="$OUTPUT_DIR/stateful_partition.image" |
| 218 | STATEFUL_DIR="$OUTPUT_DIR/stateful_partition" |
| 219 | STATEFUL_LOOP_DEV=$(sudo losetup -f) |
| 220 | if [ -z "$STATEFUL_LOOP_DEV" ] ; then |
| 221 | echo "No free loop device. Free up a loop device or reboot. exiting. " |
| 222 | exit 1 |
| 223 | fi |
| 224 | dd if=/dev/zero of="$STATEFUL_IMG" bs=1 count=1 seek=$((ROOT_SIZE_BYTES - 1)) |
| 225 | sudo losetup "$STATEFUL_LOOP_DEV" "$STATEFUL_IMG" |
| 226 | sudo mkfs.ext3 "$STATEFUL_LOOP_DEV" |
| 227 | sudo tune2fs -L "C-STATE" -U "$UUID" -c 0 -i 0 \ |
| 228 | "$STATEFUL_LOOP_DEV" |
| 229 | |
| 230 | # Mount the stateful partition. |
| 231 | mkdir -p "$STATEFUL_DIR" |
| 232 | sudo mount "$STATEFUL_LOOP_DEV" "$STATEFUL_DIR" |
| 233 | |
Chris Sosa | 4bffb8b | 2010-04-07 17:23:54 -0700 | [diff] [blame] | 234 | # Set dev image root now that we have mounted the stateful partition we created |
| 235 | DEV_IMAGE_ROOT="$STATEFUL_DIR/dev_image" |
| 236 | |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 237 | # Turn root file system into bootable image. |
| 238 | if [[ "$ARCH" = "x86" ]]; then |
| 239 | # Setup extlinux configuration. |
| 240 | # TODO: For some reason the /dev/disk/by-uuid is not being generated by udev |
| 241 | # in the initramfs. When we figure that out, switch to root=UUID=$UUID. |
| 242 | sudo mkdir -p "$ROOT_FS_DIR"/boot |
| 243 | # TODO(adlr): use initramfs for booting. |
| 244 | cat <<EOF | sudo dd of="$ROOT_FS_DIR"/boot/extlinux.conf |
| 245 | DEFAULT chromeos-usb |
| 246 | PROMPT 0 |
| 247 | TIMEOUT 0 |
| 248 | |
| 249 | label chromeos-usb |
| 250 | menu label chromeos-usb |
| 251 | kernel vmlinuz |
| 252 | append quiet console=tty2 init=/sbin/init boot=local rootwait root=/dev/sdb3 ro noresume noswap i915.modeset=1 loglevel=1 |
| 253 | |
| 254 | label chromeos-hd |
| 255 | menu label chromeos-hd |
| 256 | kernel vmlinuz |
| 257 | append quiet console=tty2 init=/sbin/init boot=local rootwait root=HDROOT ro noresume noswap i915.modeset=1 loglevel=1 |
| 258 | EOF |
| 259 | |
| 260 | # Make partition bootable and label it. |
| 261 | sudo extlinux -z --install "${ROOT_FS_DIR}/boot" |
| 262 | fi |
| 263 | |
| 264 | # -- Install packages into the root file system -- |
| 265 | |
| 266 | # We need to install libc manually from the cross toolchain. |
| 267 | # TODO: Improve this? We only want libc and not the whole toolchain. |
| 268 | PKGDIR="/var/lib/portage/pkgs/cross/" |
| 269 | sudo tar jxvpf \ |
| 270 | "${PKGDIR}/${CHOST}/cross-${CHOST}"/glibc-${LIBC_VERSION}.tbz2 \ |
| 271 | -C "$ROOT_FS_DIR" --strip-components=3 \ |
| 272 | --exclude=usr/include --exclude=sys-include --exclude=*.a --exclude=*.o |
| 273 | |
| 274 | # We need to install libstdc++ manually from the cross toolchain. |
| 275 | # TODO: Figure out a better way of doing this? |
| 276 | sudo cp -a "${BOARD_ROOT}"/lib/libgcc_s.so* "${ROOT_FS_DIR}/lib" |
| 277 | sudo cp -a "${BOARD_ROOT}"/usr/lib/libstdc++.so* "${ROOT_FS_DIR}/usr/lib" |
| 278 | |
| 279 | INSTALL_MASK="" |
Chris Sosa | 3d9a10b | 2010-04-13 15:00:46 -0700 | [diff] [blame] | 280 | if [[ $FLAGS_installmask -eq ${FLAGS_TRUE} ]] ; then |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 281 | INSTALL_MASK="$DEFAULT_INSTALL_MASK" |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 282 | fi |
| 283 | |
| 284 | if [[ $FLAGS_jobs -ne -1 ]]; then |
| 285 | EMERGE_JOBS="--jobs=$FLAGS_jobs" |
| 286 | fi |
| 287 | |
Chris Sosa | 4bffb8b | 2010-04-07 17:23:54 -0700 | [diff] [blame] | 288 | # Prepare stateful partition with some pre-created directories |
| 289 | sudo mkdir -p "${DEV_IMAGE_ROOT}" |
| 290 | sudo mkdir -p "${STATEFUL_DIR}/var" |
Bill Richardson | 8b3bd10 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 291 | |
Chris Sosa | 4bffb8b | 2010-04-07 17:23:54 -0700 | [diff] [blame] | 292 | # Create symlinks so that /usr/local/usr based directories are symlinked to |
| 293 | # /usr/local/ directories e.g. /usr/local/usr/bin -> /usr/local/bin, etc. |
| 294 | setup_symlinks_on_root "${DEV_IMAGE_ROOT}" "${STATEFUL_DIR}/var" |
Bill Richardson | 8b3bd10 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 295 | |
Chris Sosa | 4bffb8b | 2010-04-07 17:23:54 -0700 | [diff] [blame] | 296 | # Perform binding rather than symlinking because directories must exist |
| 297 | # on rootfs so that we can bind at run-time since rootfs is read-only |
| 298 | echo "Binding directories from stateful partition onto the rootfs" |
| 299 | sudo mkdir -p "${ROOT_FS_DIR}/usr/local" |
| 300 | sudo mount --bind "${DEV_IMAGE_ROOT}" "${ROOT_FS_DIR}/usr/local" |
| 301 | sudo mkdir -p "${ROOT_FS_DIR}/var" |
| 302 | sudo mount --bind "${STATEFUL_DIR}/var" "${ROOT_FS_DIR}/var" |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 303 | |
| 304 | # We "emerge --root=$ROOT_FS_DIR --root-deps=rdeps --usepkgonly" all of the |
| 305 | # runtime packages for chrome os. This builds up a chrome os image from binary |
| 306 | # packages with runtime dependencies only. We use INSTALL_MASK to trim the |
| 307 | # image size as much as possible. |
Ken Mixter | d5c4b17 | 2010-04-16 10:11:02 -0700 | [diff] [blame] | 308 | sudo INSTALL_MASK="$INSTALL_MASK" emerge-${BOARD} \ |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 309 | --root="$ROOT_FS_DIR" --root-deps=rdeps \ |
| 310 | --usepkgonly chromeos $EMERGE_JOBS |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 311 | |
Chris Sosa | 4bccd3c | 2010-03-15 13:23:14 -0700 | [diff] [blame] | 312 | # Determine the root dir for development packages. |
| 313 | ROOT_DEV_DIR="$ROOT_FS_DIR" |
| 314 | [ $FLAGS_statefuldev -eq $FLAGS_TRUE ] && ROOT_DEV_DIR="$ROOT_FS_DIR/usr/local" |
| 315 | |
| 316 | # Install development packages. |
| 317 | if [[ $FLAGS_withdev -eq $FLAGS_TRUE ]] ; then |
Ken Mixter | d5c4b17 | 2010-04-16 10:11:02 -0700 | [diff] [blame] | 318 | sudo INSTALL_MASK="$INSTALL_MASK" emerge-${BOARD} \ |
Chris Sosa | 4bccd3c | 2010-03-15 13:23:14 -0700 | [diff] [blame] | 319 | --root="$ROOT_DEV_DIR" --root-deps=rdeps \ |
| 320 | --usepkgonly chromeos-dev $EMERGE_JOBS |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 321 | |
Chris Sosa | 3d9a10b | 2010-04-13 15:00:46 -0700 | [diff] [blame] | 322 | # TODO(sosa@chromium.org) - Re-hide under statefuldev after switch |
| 323 | # Flag will mount /usr/local on target device |
| 324 | sudo mkdir -p "$ROOT_FS_DIR/root" |
| 325 | sudo touch "$ROOT_FS_DIR/root/.dev_mode" |
| 326 | |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 327 | # The ldd tool is a useful shell script but lives in glibc; just copy it. |
Chris Sosa | 4bccd3c | 2010-03-15 13:23:14 -0700 | [diff] [blame] | 328 | sudo cp -a "$(which ldd)" "${ROOT_DEV_DIR}/usr/bin" |
| 329 | fi |
| 330 | |
Andrew de los Reyes | e7a04ad | 2010-04-08 15:58:17 -0700 | [diff] [blame] | 331 | if [ -n "$FLAGS_factory_server" ]; then |
Ken Mixter | d5c4b17 | 2010-04-16 10:11:02 -0700 | [diff] [blame] | 332 | sudo INSTALL_MASK="$INSTALL_MASK" emerge-${BOARD} \ |
Andrew de los Reyes | e7a04ad | 2010-04-08 15:58:17 -0700 | [diff] [blame] | 333 | --root="$ROOT_DEV_DIR" --root-deps=rdeps \ |
| 334 | --usepkgonly chromeos-factoryinstall $EMERGE_JOBS |
| 335 | fi |
| 336 | |
Chris Sosa | 4bccd3c | 2010-03-15 13:23:14 -0700 | [diff] [blame] | 337 | # Install packages required for testing. |
| 338 | if [[ $FLAGS_withtest -eq $FLAGS_TRUE ]] ; then |
Ken Mixter | d5c4b17 | 2010-04-16 10:11:02 -0700 | [diff] [blame] | 339 | sudo INSTALL_MASK="$INSTALL_MASK" emerge-${BOARD} \ |
Chris Sosa | 4bccd3c | 2010-03-15 13:23:14 -0700 | [diff] [blame] | 340 | --root="$ROOT_DEV_DIR" --root-deps=rdeps \ |
| 341 | --usepkgonly chromeos-test $EMERGE_JOBS |
| 342 | fi |
| 343 | |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 344 | # Perform any customizations on the root file system that are needed. |
Andrew de los Reyes | e7a04ad | 2010-04-08 15:58:17 -0700 | [diff] [blame] | 345 | EXTRA_CUSTOMIZE_ROOTFS_FLAGS="" |
| 346 | if [ $FLAGS_withdev -eq $FLAGS_TRUE ]; then |
| 347 | EXTRA_CUSTOMIZE_ROOTFS_FLAGS="--withdev" |
| 348 | fi |
| 349 | if [ -n "$FLAGS_factory_server" ]; then |
| 350 | # Indentation off b/c of long line |
| 351 | EXTRA_CUSTOMIZE_ROOTFS_FLAGS="$EXTRA_CUSTOMIZE_ROOTFS_FLAGS"\ |
| 352 | " --factory_server=$FLAGS_factory_server" |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 353 | fi |
| 354 | |
Bill Richardson | 4364a2e | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 355 | # 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] | 356 | # BIOS will use the kernel in the rootfs (via syslinux), Chrome OS BIOS will |
| 357 | # use the kernel partition. |
Bill Richardson | 6dcea16 | 2010-03-31 19:20:24 -0700 | [diff] [blame] | 358 | sudo cp -f "${ROOT_FS_DIR}/boot/vmlinuz" "${OUTPUT_DIR}/vmlinuz.image" |
Bill Richardson | 4364a2e | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 359 | |
Bill Richardson | 8b3bd10 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 360 | # Create EFI System Partition to boot stock EFI BIOS (but not ChromeOS EFI |
| 361 | # BIOS). We only need this for x86, but it's simpler and safer to keep the disk |
| 362 | # images the same for both x86 and ARM. |
| 363 | ESP_IMG=${OUTPUT_DIR}/esp.image |
| 364 | # NOTE: The size argument for mkfs.vfat is in 1024-byte blocks. We'll hard-code |
| 365 | # it to 16M for now. |
| 366 | ESP_BLOCKS=16384 |
| 367 | /usr/sbin/mkfs.vfat -C ${OUTPUT_DIR}/esp.image ${ESP_BLOCKS} |
| 368 | ESP_DIR=${OUTPUT_DIR}/esp |
Bill Richardson | a81df76 | 2010-04-09 08:12:05 -0700 | [diff] [blame] | 369 | ESP_LOOP_DEV=$(sudo losetup -f) |
| 370 | if [ -z "$ESP_LOOP_DEV" ] ; then |
| 371 | echo "No free loop device. Free up a loop device or reboot. exiting. " |
| 372 | exit 1 |
| 373 | fi |
| 374 | mkdir -p "${ESP_DIR}" |
| 375 | sudo losetup "${ESP_LOOP_DEV}" "${ESP_IMG}" |
| 376 | sudo mount "${ESP_LOOP_DEV}" "${ESP_DIR}" |
| 377 | sudo mkdir -p "${ESP_DIR}/efi/boot" |
| 378 | 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] | 379 | part_gpt fat ext2 normal boot sh chain configfile linux |
Bill Richardson | a81df76 | 2010-04-09 08:12:05 -0700 | [diff] [blame] | 380 | sudo cp "${ROOT_FS_DIR}/boot/vmlinuz" "${ESP_DIR}/efi/boot/vmlinuz" |
| 381 | cat <<EOF | sudo dd of="${ESP_DIR}/efi/boot/grub.cfg" |
Bill Richardson | 8b3bd10 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 382 | set timeout=2 |
| 383 | set default=0 |
| 384 | |
Bill Richardson | 041bd71 | 2010-04-27 09:36:14 -0700 | [diff] [blame] | 385 | menuentry "boot from disk" { |
Bill Richardson | a81df76 | 2010-04-09 08:12:05 -0700 | [diff] [blame] | 386 | linux /efi/boot/vmlinuz quiet console=tty2 init=/sbin/init boot=local rootwait root=/dev/sda3 ro noresume noswap i915.modeset=1 loglevel=1 |
| 387 | } |
| 388 | |
Bill Richardson | 041bd71 | 2010-04-27 09:36:14 -0700 | [diff] [blame] | 389 | menuentry "boot from disk with serial debug" { |
Bill Richardson | ba9682a | 2010-04-13 13:02:32 -0700 | [diff] [blame] | 390 | linux /efi/boot/vmlinuz earlyprintk=serial,ttyS0,115200 console=ttyS0,115200 init=/sbin/init boot=local rootwait root=/dev/sda3 ro noresume noswap i915.modeset=1 loglevel=7 |
| 391 | } |
| 392 | |
Bill Richardson | 041bd71 | 2010-04-27 09:36:14 -0700 | [diff] [blame] | 393 | menuentry "boot from usb" { |
| 394 | linux /efi/boot/vmlinuz quiet console=tty2 init=/sbin/init boot=local rootwait root=/dev/sdb3 ro noresume noswap i915.modeset=1 loglevel=1 |
| 395 | } |
| 396 | |
| 397 | menuentry "boot from usb with serial debug" { |
| 398 | linux /efi/boot/vmlinuz earlyprintk=serial,ttyS0,115200 console=ttyS0,115200 init=/sbin/init boot=local rootwait root=/dev/sdb3 ro noresume noswap i915.modeset=1 loglevel=7 |
| 399 | } |
| 400 | |
Bill Richardson | 8b3bd10 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 401 | EOF |
| 402 | |
Chris Sosa | 3d9a10b | 2010-04-13 15:00:46 -0700 | [diff] [blame] | 403 | # Run ldconfig for rootfs's ld.so.cache |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 404 | if [ $FLAGS_statefuldev -eq $FLAGS_TRUE ] ; then |
Chris Sosa | 4bffb8b | 2010-04-07 17:23:54 -0700 | [diff] [blame] | 405 | # Re-run ldconfig to fix /etc/ldconfig.so.cache |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 406 | sudo /sbin/ldconfig -r "$ROOT_FS_DIR" |
Chris Sosa | 4bffb8b | 2010-04-07 17:23:54 -0700 | [diff] [blame] | 407 | |
| 408 | #TODO(sosa@chromium.org) - /usr/bin/xterm symlink not created in stateful. |
| 409 | sudo ln -sf "/usr/local/bin/aterm" "/usr/bin/xterm" |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 410 | fi |
| 411 | |
Chris Sosa | 503efe1 | 2010-04-08 10:05:46 -0700 | [diff] [blame] | 412 | "${SCRIPTS_DIR}/customize_rootfs" \ |
| 413 | --root="$ROOT_FS_DIR" \ |
| 414 | --target="$ARCH" \ |
| 415 | --board="$BOARD" \ |
Andrew de los Reyes | e7a04ad | 2010-04-08 15:58:17 -0700 | [diff] [blame] | 416 | $EXTRA_CUSTOMIZE_ROOTFS_FLAGS |
Chris Sosa | 503efe1 | 2010-04-08 10:05:46 -0700 | [diff] [blame] | 417 | |
| 418 | # Check that the image has been correctly created. |
| 419 | "${SCRIPTS_DIR}/test_image" \ |
| 420 | --root="$ROOT_FS_DIR" \ |
| 421 | --target="$ARCH" |
| 422 | |
Chris Sosa | 4bffb8b | 2010-04-07 17:23:54 -0700 | [diff] [blame] | 423 | # Clean up symlinks so they work on a running target rooted at "/". |
| 424 | # Here development packages are rooted at /usr/local. However, do not |
| 425 | # create /usr/local or /var on host (already exist on target). |
| 426 | setup_symlinks_on_root "/usr/local" "/var" |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 427 | |
| 428 | # Cleanup loop devices. |
Chris Sosa | 4bffb8b | 2010-04-07 17:23:54 -0700 | [diff] [blame] | 429 | cleanup |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 430 | |
Bill Richardson | 4364a2e | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 431 | # Create the GPT-formatted image |
| 432 | ${SCRIPTS_DIR}/build_gpt.sh \ |
Jie Sun | 96e2b09 | 2010-04-09 11:05:49 -0700 | [diff] [blame] | 433 | --arch=${ARCH} --board=${FLAGS_board} "${OUTPUT_DIR}" "${OUTPUT_IMG}" |
Bill Richardson | 4364a2e | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 434 | |
Bill Richardson | 6dcea16 | 2010-03-31 19:20:24 -0700 | [diff] [blame] | 435 | # Clean up temporary files. |
Bill Richardson | 8b3bd10 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 436 | rm -f "${ROOT_FS_IMG}" "${STATEFUL_IMG}" "${OUTPUT_DIR}/vmlinuz.image" \ |
| 437 | "${ESP_IMG}" |
| 438 | rmdir "${ROOT_FS_DIR}" "${STATEFUL_DIR}" "${ESP_DIR}" |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 439 | |
| 440 | OUTSIDE_OUTPUT_DIR="../build/images/${FLAGS_board}/${IMAGE_SUBDIR}" |
| 441 | echo "Done. Image created in ${OUTPUT_DIR}" |
| 442 | echo "To copy to USB keyfob, OUTSIDE the chroot, do something like:" |
Daniel Erat | dd9818a | 2010-04-26 09:16:44 -0700 | [diff] [blame] | 443 | echo " ./image_to_usb.sh --from=${OUTSIDE_OUTPUT_DIR} --to=/dev/sdX" |
Bill Richardson | c09b94f | 2010-03-15 11:40:30 -0700 | [diff] [blame] | 444 | echo "To convert to VMWare image, OUTSIDE the chroot, do something like:" |
| 445 | echo " ./image_to_vmware.sh --from=${OUTSIDE_OUTPUT_DIR}" |
| 446 | echo "from the scripts directory where you entered the chroot." |
| 447 | |
| 448 | trap - EXIT |