OLD | NEW |
(Empty) | |
| 1 #!/bin/bash |
| 2 |
| 3 # Copyright (c) 2010 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 create a Chrome OS dev recovery image using a dev install shim |
| 8 |
| 9 # Source constants and utility functions |
| 10 . "$(dirname "$0")/common.sh" |
| 11 . "$(dirname "$0")/chromeos-common.sh" |
| 12 |
| 13 get_default_board |
| 14 |
| 15 DEFINE_string board "$DEFAULT_BOARD" "Board for which the image was built \ |
| 16 Default: ${DEFAULT_BOARD}" |
| 17 DEFINE_string dev_install_shim "" "Path of the developer install shim. \ |
| 18 Default: (empty)" |
| 19 DEFINE_string payload_dir "" "Directory containing developer payload and \ |
| 20 (optionally) a custom install script. Default: (empty)" |
| 21 |
| 22 # Parse command line |
| 23 FLAGS "$@" || exit 1 |
| 24 eval set -- "${FLAGS_ARGV}" |
| 25 |
| 26 set -e |
| 27 |
| 28 # No board set and no default set then we can't proceed |
| 29 if [ -z $FLAGS_board ] ; then |
| 30 setup_board_warning |
| 31 die "No board set" |
| 32 fi |
| 33 |
| 34 # Abort early if --payload_dir is not set, invalid or empty |
| 35 if [ -z $FLAGS_payload_dir ] ; then |
| 36 die "flag --payload_dir not set" |
| 37 fi |
| 38 |
| 39 if [ ! -d "${FLAGS_payload_dir}" ] ; then |
| 40 die "${FLAGS_payload_dir} is not a directory" |
| 41 fi |
| 42 |
| 43 PAYLOAD_DIR_SIZE= |
| 44 if [ -z "$(ls -A $FLAGS_payload_dir)" ] ; then |
| 45 die "${FLAGS_payload_dir} is empty" |
| 46 else |
| 47 # Get directory size in 512-byte sectors so we can resize stateful partition |
| 48 PAYLOAD_DIR_SIZE=\ |
| 49 "$(du --apparent-size -B 512 ${FLAGS_payload_dir} | awk '{print $1}')" |
| 50 info "${FLAGS_payload_dir} has ${PAYLOAD_DIR_SIZE} 512-byte sectors" |
| 51 fi |
| 52 |
| 53 DEV_INSTALL_SHIM="dev_install_shim.bin" |
| 54 # We have a board name but no dev_install_shim set. Try find a recent one |
| 55 if [ -z $FLAGS_dev_install_shim ] ; then |
| 56 IMAGES_DIR="${DEFAULT_BUILD_ROOT}/images/${FLAGS_board}" |
| 57 FLAGS_dev_install_shim=\ |
| 58 "${IMAGES_DIR}/$(ls -t $IMAGES_DIR 2>&-| head -1)/${DEV_INSTALL_SHIM}" |
| 59 fi |
| 60 |
| 61 # Turn relative path into an absolute path. |
| 62 FLAGS_dev_install_shim=$(eval readlink -f ${FLAGS_dev_install_shim}) |
| 63 |
| 64 # Abort early if we can't find the install shim |
| 65 if [ ! -f $FLAGS_dev_install_shim ] ; then |
| 66 die "No dev install shim found at $FLAGS_dev_install_shim" |
| 67 else |
| 68 info "Using a recent dev install shim at ${FLAGS_dev_install_shim}" |
| 69 fi |
| 70 |
| 71 # Constants |
| 72 INSTALL_SHIM_DIR="$(dirname "$FLAGS_dev_install_shim")" |
| 73 DEV_RECOVERY_IMAGE="dev_recovery_image.bin" |
| 74 |
| 75 # Resize stateful partition of install shim to hold payload content |
| 76 # Due to this resize, we can't just re-pack the modified part back into an |
| 77 # image using pack_partition.sh generated for the dev install shim. Instead, |
| 78 # a revised partition table and a new image is needed |
| 79 # (see update_partition_table() for details) |
| 80 resize_statefulfs() { |
| 81 local source_part=$1 # source stateful partition |
| 82 local num_sectors=$2 # number of 512-byte sectors to be added |
| 83 |
| 84 source_image_sectors=$(roundup $(numsectors ${source_part})) |
| 85 info "source stateful fs has $((512 * $(expr $source_image_sectors))) bytes" |
| 86 resized_image_bytes=$((512 * $(expr $source_image_sectors + $num_sectors))) |
| 87 info "resized stateful fs has $resized_image_bytes bytes" |
| 88 |
| 89 STATEFUL_LOOP_DEV=$(sudo losetup -f) |
| 90 if [ -z "${STATEFUL_LOOP_DEV}" ]; then |
| 91 die "No free loop device. Free up a loop device or reboot. Exiting." |
| 92 fi |
| 93 |
| 94 # Extend the source file size to the new size. |
| 95 dd if=/dev/zero of="${source_part}" bs=1 count=1 \ |
| 96 seek=$((resized_image_bytes - 1)) |
| 97 |
| 98 # Resize the partition. |
| 99 sudo losetup "${STATEFUL_LOOP_DEV}" "${source_part}" |
| 100 sudo e2fsck -f "${STATEFUL_LOOP_DEV}" |
| 101 sudo resize2fs "${STATEFUL_LOOP_DEV}" |
| 102 sudo losetup -d "${STATEFUL_LOOP_DEV}" |
| 103 } |
| 104 |
| 105 # Update partition table with resized stateful partition and create the final |
| 106 # dev recovery image |
| 107 update_partition_table() { |
| 108 TEMP_IMG=$(mktemp) |
| 109 |
| 110 TEMP_KERN="${TEMP_DIR}"/part_2 |
| 111 TEMP_ROOTFS="${TEMP_DIR}"/part_3 |
| 112 TEMP_OEM="${TEMP_DIR}"/part_8 |
| 113 TEMP_ESP="${TEMP_DIR}"/part_12 |
| 114 TEMP_PMBR="${TEMP_DIR}"/pmbr |
| 115 dd if="${FLAGS_dev_install_shim}" of="${TEMP_PMBR}" bs=512 count=1 |
| 116 |
| 117 # Set up a new partition table |
| 118 install_gpt "${TEMP_IMG}" "${TEMP_ROOTFS}" "${TEMP_STATE}" "${TEMP_PMBR}" \ |
| 119 "${TEMP_ESP}" false $(roundup $(numsectors ${TEMP_ROOTFS})) |
| 120 |
| 121 # Copy into the partition parts of the file |
| 122 dd if="${TEMP_ROOTFS}" of="${TEMP_IMG}" conv=notrunc bs=512 \ |
| 123 seek="${START_ROOTFS_A}" |
| 124 dd if="${TEMP_STATE}" of="${TEMP_IMG}" conv=notrunc bs=512 \ |
| 125 seek="${START_STATEFUL}" |
| 126 # Copy the full kernel (i.e. with vboot sections) |
| 127 dd if="${TEMP_KERN}" of="${TEMP_IMG}" conv=notrunc bs=512 \ |
| 128 seek="${START_KERN_A}" |
| 129 dd if="${TEMP_OEM}" of="${TEMP_IMG}" conv=notrunc bs=512 \ |
| 130 seek="${START_OEM}" |
| 131 dd if="${TEMP_ESP}" of="${TEMP_IMG}" conv=notrunc bs=512 \ |
| 132 seek="${START_ESP}" |
| 133 } |
| 134 |
| 135 # Creates a dev recovery image using an existing dev install shim |
| 136 # If successful, content of --payload_dir is copied to a directory named |
| 137 # "dev_payload" under the root of stateful partition. |
| 138 create_dev_recovery_image() { |
| 139 # Split apart the partitions so we can make modifications |
| 140 TEMP_DIR=$(mktemp -d) |
| 141 (cd "${TEMP_DIR}" && |
| 142 "${INSTALL_SHIM_DIR}/unpack_partitions.sh" "${FLAGS_dev_install_shim}") |
| 143 |
| 144 TEMP_STATE="${TEMP_DIR}"/part_1 |
| 145 |
| 146 resize_statefulfs $TEMP_STATE $PAYLOAD_DIR_SIZE |
| 147 |
| 148 # Mount resized stateful FS and copy payload content to its root directory |
| 149 TEMP_MNT_STATE=$(mktemp -d) |
| 150 mkdir -p "${TEMP_MNT_STATE}" |
| 151 sudo mount -o loop "${TEMP_STATE}" "${TEMP_MNT_STATE}" |
| 152 sudo cp -R "${FLAGS_payload_dir}" "${TEMP_MNT_STATE}" |
| 153 sudo mv "${TEMP_MNT_STATE}/$(basename ${FLAGS_payload_dir})" \ |
| 154 "${TEMP_MNT_STATE}/dev_payload" |
| 155 # Mark image as dev recovery |
| 156 sudo touch "${TEMP_MNT_STATE}/.recovery" |
| 157 sudo touch "${TEMP_MNT_STATE}/.dev_recovery" |
| 158 |
| 159 # TODO(tgao): handle install script (for default and custom cases) |
| 160 update_partition_table |
| 161 |
| 162 sudo umount "${TEMP_MNT_STATE}" |
| 163 trap - EXIT |
| 164 } |
| 165 |
| 166 # Main |
| 167 DST_PATH="${INSTALL_SHIM_DIR}/${DEV_RECOVERY_IMAGE}" |
| 168 info "Attempting to create dev recovery image using dev install shim \ |
| 169 ${FLAGS_dev_install_shim}" |
| 170 create_dev_recovery_image |
| 171 mv -f $TEMP_IMG $DST_PATH |
| 172 info "Dev recovery image created at ${DST_PATH}" |
OLD | NEW |