| OLD | NEW |
| (Empty) |
| 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. | |
| 8 # It uses debootstrap (see https://wiki.ubuntu.com/DebootstrapChroot) to | |
| 9 # create a base file system. It then cusotmizes the file system and adds | |
| 10 # Ubuntu and chromeos specific packages. Finally, it creates a bootable USB | |
| 11 # image from the root fs. | |
| 12 # | |
| 13 # NOTE: This script must be run from the chromeos build chroot environment. | |
| 14 # | |
| 15 | |
| 16 # Load common constants. This should be the first executable line. | |
| 17 # The path to common.sh should be relative to your script's location. | |
| 18 . "$(dirname "$0")/common.sh" | |
| 19 | |
| 20 assert_inside_chroot | |
| 21 assert_not_root_user | |
| 22 | |
| 23 DEFAULT_PKGLIST="${SRC_ROOT}/package_repo/package-list-prod.txt" | |
| 24 | |
| 25 # Flags | |
| 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_string build_root "$DEFAULT_BUILD_ROOT" \ | |
| 31 "Root of build output" | |
| 32 DEFINE_boolean replace $FLAGS_FALSE "Overwrite existing output, if any." | |
| 33 DEFINE_boolean increment $FLAGS_FALSE \ | |
| 34 "Picks the latest build and increments the minor version by one." | |
| 35 | |
| 36 DEFINE_string arch "x86" \ | |
| 37 "The target architecture to build for. One of { x86, armel }." | |
| 38 DEFINE_string mirror "$DEFAULT_IMG_MIRROR" "Repository mirror to use." | |
| 39 DEFINE_string suite "$DEFAULT_IMG_SUITE" "Repository suite to base image on." | |
| 40 DEFINE_string mirror2 "" "Additional repository mirror to use (URL only)." | |
| 41 DEFINE_string suite2 "" "Repository suite for additional mirror." | |
| 42 DEFINE_string pkglist "$DEFAULT_PKGLIST" \ | |
| 43 "Name of file listing packages to install from repository." | |
| 44 DEFINE_boolean with_dev_pkgs $FLAGS_TRUE \ | |
| 45 "Include additional developer-friendly packages in the image." | |
| 46 | |
| 47 # Parse command line | |
| 48 FLAGS "$@" || exit 1 | |
| 49 eval set -- "${FLAGS_ARGV}" | |
| 50 | |
| 51 # Die on any errors. | |
| 52 set -e | |
| 53 | |
| 54 # Determine build version | |
| 55 . "${SCRIPTS_DIR}/chromeos_version.sh" | |
| 56 | |
| 57 # Use canonical path since some tools (e.g. mount) do not like symlinks | |
| 58 # Append build attempt to output directory | |
| 59 IMAGE_SUBDIR="${CHROMEOS_VERSION_STRING}-a${FLAGS_build_attempt}" | |
| 60 OUTPUT_DIR="${FLAGS_output_root}/${IMAGE_SUBDIR}" | |
| 61 ROOT_FS_DIR="${OUTPUT_DIR}/rootfs" | |
| 62 ROOT_FS_IMG="${OUTPUT_DIR}/rootfs.image" | |
| 63 MBR_IMG="${OUTPUT_DIR}/mbr.image" | |
| 64 OUTPUT_IMG="${OUTPUT_DIR}/usb.img" | |
| 65 | |
| 66 LOOP_DEV= | |
| 67 | |
| 68 # Handle existing directory | |
| 69 if [ -e "$OUTPUT_DIR" ] | |
| 70 then | |
| 71 if [ $FLAGS_replace -eq $FLAGS_TRUE ] | |
| 72 then | |
| 73 sudo rm -rf "$OUTPUT_DIR" | |
| 74 else | |
| 75 echo "Directory $OUTPUT_DIR already exists." | |
| 76 echo "Use --build_attempt option to specify an unused attempt." | |
| 77 echo "Or use --replace if you want to overwrite this directory." | |
| 78 exit 1 | |
| 79 fi | |
| 80 fi | |
| 81 | |
| 82 # create the output directory | |
| 83 mkdir -p "$OUTPUT_DIR" | |
| 84 | |
| 85 cleanup_rootfs_loop() { | |
| 86 sudo umount "$LOOP_DEV" | |
| 87 sleep 1 # in case $LOOP_DEV is in use | |
| 88 sudo losetup -d "$LOOP_DEV" | |
| 89 LOOP_DEV="" | |
| 90 } | |
| 91 | |
| 92 cleanup() { | |
| 93 # Disable die on error. | |
| 94 set +e | |
| 95 if [ -n "$LOOP_DEV" ] | |
| 96 then | |
| 97 cleanup_rootfs_loop | |
| 98 fi | |
| 99 | |
| 100 # Turn die on error back on. | |
| 101 set -e | |
| 102 } | |
| 103 trap cleanup EXIT | |
| 104 | |
| 105 mkdir -p "$ROOT_FS_DIR" | |
| 106 | |
| 107 # Create root file system disk image to fit on a 1GB memory stick. | |
| 108 # 1 GB in hard-drive-manufacturer-speak is 10^9, not 2^30. 700MB < 10^9 bytes. | |
| 109 ROOT_SIZE_BYTES=$((1024 * 1024 * 700)) | |
| 110 dd if=/dev/zero of="$ROOT_FS_IMG" bs=1 count=1 seek=$((ROOT_SIZE_BYTES - 1)) | |
| 111 | |
| 112 # Format, tune, and mount the rootfs. | |
| 113 # Make sure we have a mtab to keep mkfs happy. | |
| 114 if [ ! -e /etc/mtab ]; then | |
| 115 sudo touch /etc/mtab | |
| 116 fi | |
| 117 UUID=`uuidgen` | |
| 118 DISK_LABEL=C-ROOT | |
| 119 LOOP_DEV=`sudo losetup -f` | |
| 120 sudo losetup "$LOOP_DEV" "$ROOT_FS_IMG" | |
| 121 sudo mkfs.ext3 "$LOOP_DEV" | |
| 122 sudo tune2fs -L "$DISK_LABEL" -U "$UUID" -c 0 -i 0 "$LOOP_DEV" | |
| 123 sudo mount "$LOOP_DEV" "$ROOT_FS_DIR" | |
| 124 | |
| 125 # -- Install packages and customize root file system. -- | |
| 126 PKGLIST="$FLAGS_pkglist" | |
| 127 if [ $FLAGS_with_dev_pkgs -eq $FLAGS_TRUE ]; then | |
| 128 PKGLIST="$PKGLIST,${SRC_ROOT}/package_repo/package-list-debug.txt" | |
| 129 fi | |
| 130 # Add official packages for ChromeOS if the file exists | |
| 131 if [ -f ${SRC_ROOT}/package_repo/package-list-official.txt ]; then | |
| 132 PKGLIST="$PKGLIST,${SRC_ROOT}/package_repo/package-list-official.txt" | |
| 133 fi | |
| 134 "${SCRIPTS_DIR}/install_packages.sh" \ | |
| 135 --build_root="${FLAGS_build_root}" \ | |
| 136 --root="$ROOT_FS_DIR" \ | |
| 137 --output_dir="${OUTPUT_DIR}" \ | |
| 138 --package_list="$PKGLIST" \ | |
| 139 --arch="$FLAGS_arch" \ | |
| 140 --mirror="$FLAGS_mirror" \ | |
| 141 --suite="$FLAGS_suite" \ | |
| 142 --mirror2="$FLAGS_mirror2" \ | |
| 143 --suite2="$FLAGS_suite2" | |
| 144 | |
| 145 "${SCRIPTS_DIR}/customize_rootfs.sh" --root="${ROOT_FS_DIR}" | |
| 146 | |
| 147 # -- Turn root file system into bootable image -- | |
| 148 | |
| 149 if [ "$FLAGS_arch" = "x86" ]; then | |
| 150 # Setup extlinux configuration. | |
| 151 # TODO: For some reason the /dev/disk/by-uuid is not being generated by udev | |
| 152 # in the initramfs. When we figure that out, switch to root=UUID=$UUID. | |
| 153 cat <<EOF | sudo dd of="$ROOT_FS_DIR"/boot/extlinux.conf | |
| 154 DEFAULT chromeos-usb | |
| 155 PROMPT 0 | |
| 156 TIMEOUT 0 | |
| 157 | |
| 158 label chromeos-usb | |
| 159 menu label chromeos-usb | |
| 160 kernel vmlinuz | |
| 161 append quiet console=tty2 initrd=initrd.img init=/sbin/init boot=local rootwai
t root=LABEL=$DISK_LABEL ro noresume noswap i915.modeset=1 loglevel=1 | |
| 162 | |
| 163 label chromeos-hd | |
| 164 menu label chromeos-hd | |
| 165 kernel vmlinuz | |
| 166 append quiet console=tty2 init=/sbin/init boot=local rootwait root=HDROOT ro n
oresume noswap i915.modeset=1 loglevel=1 | |
| 167 EOF | |
| 168 | |
| 169 # Make partition bootable and label it. | |
| 170 sudo "$SCRIPTS_DIR/extlinux.sh" -z --install "${ROOT_FS_DIR}/boot" | |
| 171 | |
| 172 fi # --arch=x86 | |
| 173 | |
| 174 cleanup_rootfs_loop | |
| 175 | |
| 176 if [ "$FLAGS_arch" = "x86" ]; then | |
| 177 | |
| 178 # Create a master boot record. | |
| 179 # Start with the syslinux master boot record. We need to zero-pad to | |
| 180 # fill out a 512-byte sector size. | |
| 181 SYSLINUX_MBR="/usr/lib/syslinux/mbr.bin" | |
| 182 dd if="$SYSLINUX_MBR" of="$MBR_IMG" bs=512 count=1 conv=sync | |
| 183 # Create a partition table in the MBR. | |
| 184 NUM_SECTORS=$((`stat --format="%s" "$ROOT_FS_IMG"` / 512)) | |
| 185 sudo sfdisk -H64 -S32 -uS -f "$MBR_IMG" <<EOF | |
| 186 ,$NUM_SECTORS,L,-, | |
| 187 ,$NUM_SECTORS,S,-, | |
| 188 ,$NUM_SECTORS,L,*, | |
| 189 ; | |
| 190 EOF | |
| 191 | |
| 192 fi # --arch=x86 | |
| 193 | |
| 194 OUTSIDE_OUTPUT_DIR="${EXTERNAL_TRUNK_PATH}/src/build/images/${IMAGE_SUBDIR}" | |
| 195 echo "Done. Image created in ${OUTPUT_DIR}" | |
| 196 echo "To copy to USB keyfob, outside the chroot, do something like:" | |
| 197 echo " ./image_to_usb.sh --from=${OUTSIDE_OUTPUT_DIR} --to=/dev/sdb" | |
| 198 echo "To convert to VMWare image, outside the chroot, do something like:" | |
| 199 echo " ./image_to_vmware.sh --from=${OUTSIDE_OUTPUT_DIR}" | |
| 200 | |
| 201 trap - EXIT | |
| OLD | NEW |