| OLD | NEW |
| 1 #!/bin/bash | 1 #!/bin/bash |
| 2 | 2 |
| 3 # Copyright (c) 2009 The Chromium OS Authors. All rights reserved. | 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 | 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
| 6 | 6 |
| 7 # Script to build a bootable keyfob-based chromeos system image. | 7 # Script to build a bootable keyfob-based chromeos system image. |
| 8 # It uses debootstrap (see https://wiki.ubuntu.com/DebootstrapChroot) to | 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 | 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 | 10 # Ubuntu and chromeos specific packages. Finally, it creates a bootable USB |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 "Root of build output" | 31 "Root of build output" |
| 32 DEFINE_boolean replace $FLAGS_FALSE "Overwrite existing output, if any." | 32 DEFINE_boolean replace $FLAGS_FALSE "Overwrite existing output, if any." |
| 33 DEFINE_boolean increment $FLAGS_FALSE \ | 33 DEFINE_boolean increment $FLAGS_FALSE \ |
| 34 "Picks the latest build and increments the minor version by one." | 34 "Picks the latest build and increments the minor version by one." |
| 35 | 35 |
| 36 DEFINE_string mirror "$DEFAULT_IMG_MIRROR" "Repository mirror to use." | 36 DEFINE_string mirror "$DEFAULT_IMG_MIRROR" "Repository mirror to use." |
| 37 DEFINE_string suite "$DEFAULT_IMG_SUITE" "Repository suite to base image on." | 37 DEFINE_string suite "$DEFAULT_IMG_SUITE" "Repository suite to base image on." |
| 38 DEFINE_string pkglist "$DEFAULT_PKGLIST" \ | 38 DEFINE_string pkglist "$DEFAULT_PKGLIST" \ |
| 39 "Name of file listing packages to install from repository." | 39 "Name of file listing packages to install from repository." |
| 40 | 40 |
| 41 KERNEL_DEB_PATH=$(find "${FLAGS_build_root}/x86/local_packages" -name "linux-ima
ge-*.deb") | |
| 42 KERNEL_DEB=$(basename "${KERNEL_DEB_PATH}" .deb | sed -e 's/linux-image-//' -e '
s/_.*//') | |
| 43 KERNEL_VERSION=${KERNEL_VERSION:-${KERNEL_DEB}} | |
| 44 | |
| 45 # Parse command line | 41 # Parse command line |
| 46 FLAGS "$@" || exit 1 | 42 FLAGS "$@" || exit 1 |
| 47 eval set -- "${FLAGS_ARGV}" | 43 eval set -- "${FLAGS_ARGV}" |
| 48 | 44 |
| 49 # Die on any errors. | 45 # Die on any errors. |
| 50 set -e | 46 set -e |
| 51 | 47 |
| 52 # Determine build version | 48 # Determine build version |
| 53 . "${SCRIPTS_DIR}/chromeos_version.sh" | 49 . "${SCRIPTS_DIR}/chromeos_version.sh" |
| 54 | 50 |
| 55 # Use canonical path since some tools (e.g. mount) do not like symlinks | 51 # Use canonical path since some tools (e.g. mount) do not like symlinks |
| 56 # Append build attempt to output directory | 52 # Append build attempt to output directory |
| 57 IMAGE_SUBDIR="${CHROMEOS_VERSION_STRING}-a${FLAGS_build_attempt}" | 53 IMAGE_SUBDIR="${CHROMEOS_VERSION_STRING}-a${FLAGS_build_attempt}" |
| 58 OUTPUT_DIR="${FLAGS_output_root}/${IMAGE_SUBDIR}" | 54 OUTPUT_DIR="${FLAGS_output_root}/${IMAGE_SUBDIR}" |
| 59 ROOT_FS_DIR="${OUTPUT_DIR}/rootfs" | 55 ROOT_FS_DIR="${OUTPUT_DIR}/rootfs" |
| 60 ROOT_FS_IMG="${OUTPUT_DIR}/rootfs.image" | 56 ROOT_FS_IMG="${OUTPUT_DIR}/rootfs.image" |
| 61 MBR_IMG="${OUTPUT_DIR}/mbr.image" | 57 MBR_IMG="${OUTPUT_DIR}/mbr.image" |
| 62 OUTPUT_IMG="${OUTPUT_DIR}/usb.img" | 58 OUTPUT_IMG="${OUTPUT_DIR}/usb.img" |
| 63 SETUP_DIR="${OUTPUT_DIR}/tmp" | |
| 64 | 59 |
| 65 LOOP_DEV= | 60 LOOP_DEV= |
| 66 | 61 |
| 67 # Handle existing directory | 62 # Handle existing directory |
| 68 if [ -e "$OUTPUT_DIR" ] | 63 if [ -e "$OUTPUT_DIR" ] |
| 69 then | 64 then |
| 70 if [ $FLAGS_replace -eq $FLAGS_TRUE ] | 65 if [ $FLAGS_replace -eq $FLAGS_TRUE ] |
| 71 then | 66 then |
| 72 sudo rm -rf "$OUTPUT_DIR" | 67 sudo rm -rf "$OUTPUT_DIR" |
| 73 else | 68 else |
| 74 echo "Directory $OUTPUT_DIR already exists." | 69 echo "Directory $OUTPUT_DIR already exists." |
| 75 echo "Use --build_attempt option to specify an unused attempt." | 70 echo "Use --build_attempt option to specify an unused attempt." |
| 76 echo "Or use --replace if you want to overwrite this directory." | 71 echo "Or use --replace if you want to overwrite this directory." |
| 77 exit 1 | 72 exit 1 |
| 78 fi | 73 fi |
| 79 fi | 74 fi |
| 80 | 75 |
| 81 # create the output directory | 76 # create the output directory |
| 82 mkdir -p "$OUTPUT_DIR" | 77 mkdir -p "$OUTPUT_DIR" |
| 83 | 78 |
| 84 # Make sure anything mounted in the rootfs is cleaned up ok on exit. | |
| 85 cleanup_rootfs_mounts() { | |
| 86 # Occasionally there are some daemons left hanging around that have our | |
| 87 # root image file system open. We do a best effort attempt to kill them. | |
| 88 PIDS=`sudo lsof -t "$ROOT_FS_DIR" | sort | uniq` | |
| 89 for pid in $PIDS | |
| 90 do | |
| 91 local cmdline=`cat /proc/$pid/cmdline` | |
| 92 echo "Killing process that has open file on our rootfs: $cmdline" | |
| 93 ! sudo kill $pid # Preceded by ! to disable ERR trap. | |
| 94 done | |
| 95 | |
| 96 # Sometimes the volatile directory is left mounted and sometimes it is not, | |
| 97 # so we precede by '!' to disable the ERR trap. | |
| 98 ! sudo umount "$ROOT_FS_DIR"/lib/modules/2.6.*/volatile/ | |
| 99 | |
| 100 sudo umount "${ROOT_FS_DIR}/proc" | |
| 101 sudo umount "${ROOT_FS_DIR}/sys" | |
| 102 } | |
| 103 | |
| 104 cleanup_rootfs_loop() { | 79 cleanup_rootfs_loop() { |
| 105 sudo umount "$LOOP_DEV" | 80 sudo umount "$LOOP_DEV" |
| 106 sleep 1 # in case $LOOP_DEV is in use | 81 sleep 1 # in case $LOOP_DEV is in use |
| 107 sudo losetup -d "$LOOP_DEV" | 82 sudo losetup -d "$LOOP_DEV" |
| 83 LOOP_DEV="" |
| 108 } | 84 } |
| 109 | 85 |
| 110 cleanup() { | 86 cleanup() { |
| 111 # Disable die on error. | 87 # Disable die on error. |
| 112 set +e | 88 set +e |
| 113 | |
| 114 cleanup_rootfs_mounts | |
| 115 if [ -n "$LOOP_DEV" ] | 89 if [ -n "$LOOP_DEV" ] |
| 116 then | 90 then |
| 117 cleanup_rootfs_loop | 91 cleanup_rootfs_loop |
| 118 fi | 92 fi |
| 119 | 93 |
| 120 # Turn die on error back on. | 94 # Turn die on error back on. |
| 121 set -e | 95 set -e |
| 122 } | 96 } |
| 123 trap cleanup EXIT | 97 trap cleanup EXIT |
| 124 | 98 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 135 sudo touch /etc/mtab | 109 sudo touch /etc/mtab |
| 136 fi | 110 fi |
| 137 UUID=`uuidgen` | 111 UUID=`uuidgen` |
| 138 DISK_LABEL=C-ROOT | 112 DISK_LABEL=C-ROOT |
| 139 LOOP_DEV=`sudo losetup -f` | 113 LOOP_DEV=`sudo losetup -f` |
| 140 sudo losetup "$LOOP_DEV" "$ROOT_FS_IMG" | 114 sudo losetup "$LOOP_DEV" "$ROOT_FS_IMG" |
| 141 sudo mkfs.ext3 "$LOOP_DEV" | 115 sudo mkfs.ext3 "$LOOP_DEV" |
| 142 sudo tune2fs -L "$DISK_LABEL" -U "$UUID" -c 0 -i 0 "$LOOP_DEV" | 116 sudo tune2fs -L "$DISK_LABEL" -U "$UUID" -c 0 -i 0 "$LOOP_DEV" |
| 143 sudo mount "$LOOP_DEV" "$ROOT_FS_DIR" | 117 sudo mount "$LOOP_DEV" "$ROOT_FS_DIR" |
| 144 | 118 |
| 145 # Add debootstrap link for the suite, if it doesn't exist. | 119 # -- Install packages and customize root file system. -- |
| 146 if [ ! -e "/usr/share/debootstrap/scripts/$FLAGS_suite" ] | |
| 147 then | |
| 148 sudo ln -s /usr/share/debootstrap/scripts/jaunty \ | |
| 149 "/usr/share/debootstrap/scripts/$FLAGS_suite" | |
| 150 fi | |
| 151 | 120 |
| 152 # Bootstrap the base debian file system | |
| 153 # TODO: Switch to --variant=minbase | |
| 154 sudo debootstrap --arch=i386 $FLAGS_suite "$ROOT_FS_DIR" "${FLAGS_mirror}" | |
| 155 | |
| 156 # -- Customize the root file system -- | |
| 157 | |
| 158 # Set up mounts for working within the chroot. We copy some basic | |
| 159 # network information from the host so that the chroot can access | |
| 160 # repositories on the network as needed. | |
| 161 sudo mount -t proc proc "${ROOT_FS_DIR}/proc" | |
| 162 sudo mount -t sysfs sysfs "${ROOT_FS_DIR}/sys" # TODO: Do we need sysfs? | |
| 163 sudo cp /etc/hosts "${ROOT_FS_DIR}/etc" | |
| 164 | |
| 165 # Create setup directory and copy over scripts, config files, and locally | |
| 166 # built packages. | |
| 167 mkdir -p "$SETUP_DIR" | |
| 168 mkdir -p "${SETUP_DIR}/local_packages" | |
| 169 cp "${FLAGS_build_root}/x86/local_packages"/* "${SETUP_DIR}/local_packages" | |
| 170 | |
| 171 # Set up repository for local packages to install in the rootfs via apt-get. | |
| 172 cd "$SETUP_DIR" | |
| 173 dpkg-scanpackages local_packages/ /dev/null | \ | |
| 174 gzip > local_packages/Packages.gz | |
| 175 cd - | |
| 176 | |
| 177 # Run the package install script | |
| 178 "${SCRIPTS_DIR}/install_packages.sh" \ | 121 "${SCRIPTS_DIR}/install_packages.sh" \ |
| 122 --build_root="${FLAGS_build_root}" \ |
| 179 --root="$ROOT_FS_DIR" \ | 123 --root="$ROOT_FS_DIR" \ |
| 180 --output_dir="${OUTPUT_DIR}" \ | 124 --output_dir="${OUTPUT_DIR}" \ |
| 181 --setup_dir="${SETUP_DIR}" \ | |
| 182 --package_list="$FLAGS_pkglist" \ | 125 --package_list="$FLAGS_pkglist" \ |
| 183 --server="$FLAGS_mirror" \ | 126 --server="$FLAGS_mirror" \ |
| 184 --suite="$FLAGS_suite" \ | 127 --suite="$FLAGS_suite" |
| 185 --kernel_version="$KERNEL_VERSION" | |
| 186 | 128 |
| 187 # Run the script to customize the resulting root file system. | |
| 188 "${SCRIPTS_DIR}/customize_rootfs.sh" --root="${ROOT_FS_DIR}" | 129 "${SCRIPTS_DIR}/customize_rootfs.sh" --root="${ROOT_FS_DIR}" |
| 189 | 130 |
| 190 # Unmount mounts within the rootfs so it is ready to be imaged. | |
| 191 cleanup_rootfs_mounts | |
| 192 | |
| 193 # -- Turn root file system into bootable image -- | 131 # -- Turn root file system into bootable image -- |
| 194 | 132 |
| 195 # Setup extlinux configuration. | 133 # Setup extlinux configuration. |
| 196 # TODO: For some reason the /dev/disk/by-uuid is not being generated by udev | 134 # TODO: For some reason the /dev/disk/by-uuid is not being generated by udev |
| 197 # in the initramfs. When we figure that out, switch to root=UUID=$UUID. | 135 # in the initramfs. When we figure that out, switch to root=UUID=$UUID. |
| 198 cat <<EOF | sudo dd of="$ROOT_FS_DIR"/boot/extlinux.conf | 136 cat <<EOF | sudo dd of="$ROOT_FS_DIR"/boot/extlinux.conf |
| 199 DEFAULT chromeos-usb | 137 DEFAULT chromeos-usb |
| 200 PROMPT 0 | 138 PROMPT 0 |
| 201 TIMEOUT 0 | 139 TIMEOUT 0 |
| 202 | 140 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 231 EOF | 169 EOF |
| 232 | 170 |
| 233 OUTSIDE_OUTPUT_DIR="${EXTERNAL_TRUNK_PATH}/src/build/images/${IMAGE_SUBDIR}" | 171 OUTSIDE_OUTPUT_DIR="${EXTERNAL_TRUNK_PATH}/src/build/images/${IMAGE_SUBDIR}" |
| 234 echo "Done. Image created in ${OUTPUT_DIR}" | 172 echo "Done. Image created in ${OUTPUT_DIR}" |
| 235 echo "To copy to USB keyfob, outside the chroot, do something like:" | 173 echo "To copy to USB keyfob, outside the chroot, do something like:" |
| 236 echo " ./image_to_usb.sh --from=${OUTSIDE_OUTPUT_DIR} --to=/dev/sdb" | 174 echo " ./image_to_usb.sh --from=${OUTSIDE_OUTPUT_DIR} --to=/dev/sdb" |
| 237 echo "To convert to VMWare image, outside the chroot, do something like:" | 175 echo "To convert to VMWare image, outside the chroot, do something like:" |
| 238 echo " ./image_to_vmware.sh --from=${OUTSIDE_OUTPUT_DIR}" | 176 echo " ./image_to_vmware.sh --from=${OUTSIDE_OUTPUT_DIR}" |
| 239 | 177 |
| 240 trap - EXIT | 178 trap - EXIT |
| OLD | NEW |