Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 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 | |
Gaurav Shah | 550edca | 2014-09-25 11:13:55 -0700 | [diff] [blame] | 7 | # Script to convert the output of build_image.sh to a QEMU image. |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 8 | |
Kees Cook | 84a4c7a | 2011-10-18 13:21:41 -0700 | [diff] [blame] | 9 | # Helper scripts should be run from the same location as this script. |
| 10 | SCRIPT_ROOT=$(dirname "$(readlink -f "$0")") |
David James | 359d3e1 | 2012-07-10 13:09:48 -0700 | [diff] [blame] | 11 | . "${SCRIPT_ROOT}/common.sh" || exit 1 |
Liam McLoughlin | 5b37c54 | 2012-08-16 11:09:37 -0700 | [diff] [blame] | 12 | . "${SCRIPT_ROOT}/build_library/build_common.sh" || exit 1 |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 13 | |
| 14 | # Need to be inside the chroot to load chromeos-common.sh |
| 15 | assert_inside_chroot |
| 16 | |
| 17 | # Load functions and constants for chromeos-install |
Gwendal Grignou | 0f61849 | 2014-04-07 20:05:58 +0000 | [diff] [blame] | 18 | . /usr/share/misc/chromeos-common.sh || exit 1 |
Mike Frysinger | 566c943 | 2019-03-02 14:18:29 -0500 | [diff] [blame] | 19 | |
| 20 | # Default values for creating VM's. |
| 21 | DEFAULT_QEMU_IMAGE="chromiumos_qemu_image.bin" |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 22 | |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 23 | # Flags |
Liam McLoughlin | 12a9a84 | 2012-10-10 10:37:06 -0400 | [diff] [blame] | 24 | DEFINE_string adjust_part "" \ |
| 25 | "Adjustments to apply to the partition table" |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 26 | DEFINE_string board "${DEFAULT_BOARD}" \ |
| 27 | "Board for which the image was built" |
| 28 | DEFINE_boolean factory $FLAGS_FALSE \ |
| 29 | "Modify the image for manufacturing testing" |
| 30 | DEFINE_boolean factory_install $FLAGS_FALSE \ |
| 31 | "Modify the image for factory install shim" |
Simon Glass | 142ca06 | 2011-02-09 13:39:43 -0800 | [diff] [blame] | 32 | |
Gabe Black | 1d6dc25 | 2014-08-18 17:02:09 -0700 | [diff] [blame] | 33 | # We default to TRUE so the buildbot gets its image. |
Chris Sosa | 8dad50d | 2011-02-14 15:29:32 -0800 | [diff] [blame] | 34 | DEFINE_boolean force_copy ${FLAGS_FALSE} "Always rebuild test image" |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 35 | DEFINE_string from "" \ |
| 36 | "Directory containing rootfs.image and mbr.image" |
Don Garrett | 7937ef8 | 2014-08-25 18:04:05 -0700 | [diff] [blame] | 37 | DEFINE_string disk_layout "2gb-rootfs-updatable" \ |
Liam McLoughlin | b78a7c3 | 2012-11-06 20:19:05 -0500 | [diff] [blame] | 38 | "The disk layout type to use for this image." |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 39 | DEFINE_string state_image "" \ |
| 40 | "Stateful partition image (defaults to creating new statful partition)" |
| 41 | DEFINE_boolean test_image "${FLAGS_FALSE}" \ |
Jacob Kopczynski | 75d465c | 2018-07-19 12:54:17 -0700 | [diff] [blame] | 42 | "Acquires image from ${CHROMEOS_TEST_IMAGE_NAME} instead of " \ |
| 43 | "${CHROMEOS_IMAGE_NAME}." |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 44 | DEFINE_string to "" \ |
| 45 | "Destination folder for VM output file(s)" |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 46 | |
| 47 | # Parse command line |
| 48 | FLAGS "$@" || exit 1 |
| 49 | eval set -- "${FLAGS_ARGV}" |
| 50 | |
| 51 | # Die on any errors. |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 52 | switch_to_strict_mode |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 53 | |
Gabe Black | 067b354 | 2014-09-23 01:15:14 -0700 | [diff] [blame] | 54 | TEMP_DIR=$(mktemp -d) |
| 55 | TEMP_MNT="" |
| 56 | TEMP_ESP_MNT="" |
| 57 | SRC_DEV="" |
| 58 | DST_DEV="" |
| 59 | cleanup() { |
| 60 | if [[ -n "${TEMP_MNT}" ]]; then |
| 61 | safe_umount "${TEMP_MNT}" || true |
| 62 | rmdir "${TEMP_MNT}" || true |
| 63 | fi |
| 64 | if [[ -n "${TEMP_ESP_MNT}" ]]; then |
| 65 | safe_umount "${TEMP_ESP_MNT}" || true |
| 66 | rmdir "${TEMP_ESP_MNT}" || true |
| 67 | fi |
| 68 | |
| 69 | if [[ -n "${SRC_DEV}" ]]; then |
| 70 | loopback_detach "${SRC_DEV}" || true |
| 71 | fi |
| 72 | if [[ -n "${DST_DEV}" ]]; then |
| 73 | loopback_detach "${DST_DEV}" || true |
| 74 | fi |
| 75 | rm -rf "${TEMP_DIR}" |
| 76 | } |
Mike Frysinger | 9ebc8ce | 2015-04-06 13:15:01 -0400 | [diff] [blame] | 77 | trap 'ret=$?; cleanup; die_err_trap ${ret}' INT TERM EXIT |
Gabe Black | 067b354 | 2014-09-23 01:15:14 -0700 | [diff] [blame] | 78 | |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 79 | # Default to the most recent image |
| 80 | if [ -z "${FLAGS_from}" ] ; then |
Alex Klein | 999443c | 2018-10-17 12:02:05 -0600 | [diff] [blame] | 81 | FLAGS_from="${IMAGES_DIR}/${FLAGS_board}/latest" |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 82 | fi |
| 83 | if [ -z "${FLAGS_to}" ] ; then |
| 84 | FLAGS_to="${FLAGS_from}" |
| 85 | fi |
| 86 | |
Mike Frysinger | b86854a | 2014-11-25 18:43:58 -0500 | [diff] [blame] | 87 | # Convert args to full paths. Use echo here on the unquoted value to process all |
| 88 | # shell level expansions like ~ and *. |
| 89 | if ! resolved=$(readlink -f "$(echo ${FLAGS_from})"); then |
| 90 | die_notrace "image_to_vm: processing --from failed." \ |
| 91 | "Verify the path exists: ${FLAGS_from}" \ |
| 92 | " cwd: ${PWD}" |
| 93 | fi |
| 94 | FLAGS_from=${resolved} |
| 95 | if ! resolved=$(readlink -f "$(echo ${FLAGS_to})"); then |
| 96 | die_notrace "image_to_vm: Processing --to failed." \ |
| 97 | "Verify the path exists: ${FLAGS_to}" \ |
| 98 | " cwd: ${PWD}" |
| 99 | fi |
| 100 | FLAGS_to=${resolved} |
| 101 | |
Hung-Te Lin | 269680c | 2016-05-30 14:47:37 +0800 | [diff] [blame] | 102 | if [ ${FLAGS_test_image} -eq ${FLAGS_TRUE} ]; then |
David James | 7efb76c | 2013-01-09 15:25:58 -0800 | [diff] [blame] | 103 | SRC_IMAGE="${FLAGS_from}/${CHROMEOS_TEST_IMAGE_NAME}" |
Simon Glass | 142ca06 | 2011-02-09 13:39:43 -0800 | [diff] [blame] | 104 | else |
| 105 | # Use the standard image |
| 106 | SRC_IMAGE="${FLAGS_from}/${CHROMEOS_IMAGE_NAME}" |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 107 | fi |
Mike Frysinger | b86854a | 2014-11-25 18:43:58 -0500 | [diff] [blame] | 108 | if [[ ! -e ${SRC_IMAGE} ]]; then |
| 109 | die_notrace "image_to_vm: src image does not exist: ${SRC_IMAGE}" \ |
| 110 | "Please verify you have selected the right input." \ |
| 111 | "Note: only dev/test/factory images can be used as inputs." |
| 112 | fi |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 113 | |
Prathmesh Prabhu | aa96e57 | 2017-08-31 22:42:37 -0700 | [diff] [blame] | 114 | if [[ -z "${FLAGS_board}" ]] && [[ -n "${FLAGS_from}" ]]; then |
| 115 | # The user may not know the board of in the input image, so infer it for them. |
| 116 | # TODO(pprabhu): This will fail if the user's chroot has a default_board set |
| 117 | # which is different from the image provided, because FLAGS_board will use |
| 118 | # that. Fix this project-wide by respecting the --board flag when provided, |
| 119 | # but preferring the board inferred from FLAGS_from over the default, |
| 120 | # everywhere. |
| 121 | FLAGS_board="$( |
| 122 | . "${BUILD_LIBRARY_DIR}/mount_gpt_util.sh" |
| 123 | get_board_from_image "${SRC_IMAGE}" |
| 124 | )" |
| 125 | fi |
| 126 | |
| 127 | if [[ ! -d "/build/${FLAGS_board}" ]]; then |
| 128 | # Using board options and overrides requires that the board sysroot be setup |
| 129 | # (in order to read kernel and disk-image options). |
| 130 | # OTOH, we don't actually need to build any packages / update the host |
| 131 | # sysroot. |
Alex Klein | d65766f | 2019-01-11 14:57:34 -0700 | [diff] [blame^] | 132 | setup_board --quiet --board="${FLAGS_board}" \ |
| 133 | --skip-toolchain-update --skip-chroot-upgrade --skip-board-pkg-init |
Prathmesh Prabhu | aa96e57 | 2017-08-31 22:42:37 -0700 | [diff] [blame] | 134 | fi |
Bertrand SIMONNET | 1e77c19 | 2015-06-03 15:22:01 -0700 | [diff] [blame] | 135 | . "${BUILD_LIBRARY_DIR}/board_options.sh" || exit 1 |
| 136 | . "${SCRIPT_ROOT}/build_library/disk_layout_util.sh" || exit 1 |
| 137 | |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 138 | # Memory units are in MBs |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 139 | TEMP_IMG="$(dirname "${SRC_IMAGE}")/vm_temp_image.bin" |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 140 | |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 141 | # Split apart the partitions and make some new ones |
Gabe Black | 067b354 | 2014-09-23 01:15:14 -0700 | [diff] [blame] | 142 | SRC_DEV=$(loopback_partscan "${SRC_IMAGE}") |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 143 | |
| 144 | # Fix the kernel command line |
Gabe Black | 067b354 | 2014-09-23 01:15:14 -0700 | [diff] [blame] | 145 | SRC_STATE="${SRC_DEV}"p1 |
| 146 | SRC_ROOTFS="${SRC_DEV}"p3 |
| 147 | SRC_KERN="${SRC_DEV}"p4 |
| 148 | SRC_OEM="${SRC_DEV}"p8 |
| 149 | SRC_ESP="${SRC_DEV}"p12 |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 150 | if [ -n "${FLAGS_state_image}" ]; then |
| 151 | TEMP_STATE="${FLAGS_state_image}" |
Rahul Chaturvedi | e770e16 | 2010-07-15 00:35:11 +0530 | [diff] [blame] | 152 | else |
Liam McLoughlin | b78a7c3 | 2012-11-06 20:19:05 -0500 | [diff] [blame] | 153 | STATEFUL_SIZE_BYTES=$(get_filesystem_size "${FLAGS_disk_layout}" 1) |
Liam McLoughlin | 5b37c54 | 2012-08-16 11:09:37 -0700 | [diff] [blame] | 154 | STATEFUL_SIZE_MEGABYTES=$(( STATEFUL_SIZE_BYTES / 1024 / 1024 )) |
Gabe Black | 067b354 | 2014-09-23 01:15:14 -0700 | [diff] [blame] | 155 | original_image_size=$(bd_safe_size "${SRC_STATE}") |
Liam McLoughlin | 5b37c54 | 2012-08-16 11:09:37 -0700 | [diff] [blame] | 156 | if [ "${original_image_size}" -gt "${STATEFUL_SIZE_BYTES}" ]; then |
Sam Hurst | c94b7f9 | 2018-06-07 07:14:54 -0700 | [diff] [blame] | 157 | if [ $(( original_image_size - STATEFUL_SIZE_BYTES )) -lt \ |
| 158 | $(( 1024 * 1024 )) ]; then |
| 159 | # cgpt.py sometimes makes the stateful a tiny bit larger to |
| 160 | # counteract alignment losses. |
| 161 | # This is fine -- just keep using the slightly larger partition as it is. |
| 162 | TEMP_STATE="${SRC_STATE}" |
| 163 | else |
| 164 | die "Cannot resize stateful image to smaller than original. Exiting." |
| 165 | fi |
| 166 | else |
| 167 | echo "Resizing stateful partition to ${STATEFUL_SIZE_MEGABYTES}MB" |
| 168 | # Extend the original file size to the new size. |
| 169 | TEMP_STATE="${TEMP_DIR}"/stateful |
| 170 | # Create TEMP_STATE as a regular user so a regular user can delete it. |
| 171 | sudo dd if="${SRC_STATE}" bs=16M status=none > "${TEMP_STATE}" |
| 172 | sudo e2fsck -pf "${TEMP_STATE}" |
| 173 | sudo resize2fs "${TEMP_STATE}" ${STATEFUL_SIZE_MEGABYTES}M |
Rahul Chaturvedi | e770e16 | 2010-07-15 00:35:11 +0530 | [diff] [blame] | 174 | fi |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 175 | fi |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 176 | TEMP_PMBR="${TEMP_DIR}"/pmbr |
| 177 | dd if="${SRC_IMAGE}" of="${TEMP_PMBR}" bs=512 count=1 |
| 178 | |
Gabe Black | 067b354 | 2014-09-23 01:15:14 -0700 | [diff] [blame] | 179 | # Set up a new partition table. |
| 180 | PARTITION_SCRIPT_PATH=$(mktemp) |
| 181 | write_partition_script "${FLAGS_disk_layout}" "${PARTITION_SCRIPT_PATH}" |
| 182 | . "${PARTITION_SCRIPT_PATH}" |
| 183 | write_partition_table "${TEMP_IMG}" "${TEMP_PMBR}" |
| 184 | rm "${PARTITION_SCRIPT_PATH}" |
| 185 | |
| 186 | DST_DEV=$(loopback_partscan "${TEMP_IMG}") |
| 187 | DST_STATE="${DST_DEV}"p1 |
| 188 | DST_ROOTFS="${DST_DEV}"p3 |
| 189 | DST_KERN="${DST_DEV}"p4 |
| 190 | DST_OEM="${DST_DEV}"p8 |
| 191 | DST_ESP="${DST_DEV}"p12 |
| 192 | |
| 193 | # Copy into the partition parts of the file. |
Gabe Black | 1dd9522 | 2014-10-04 02:59:14 -0700 | [diff] [blame] | 194 | sudo cp "${SRC_ROOTFS}" "${DST_ROOTFS}" |
| 195 | sudo cp "${TEMP_STATE}" "${DST_STATE}" |
| 196 | sudo cp "${SRC_ESP}" "${DST_ESP}" |
| 197 | sudo cp "${SRC_OEM}" "${DST_OEM}" |
Gabe Black | 067b354 | 2014-09-23 01:15:14 -0700 | [diff] [blame] | 198 | |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 199 | TEMP_MNT=$(mktemp -d) |
Will Drewry | efce668 | 2010-07-23 19:43:27 -0500 | [diff] [blame] | 200 | TEMP_ESP_MNT=$(mktemp -d) |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 201 | mkdir -p "${TEMP_MNT}" |
Gabe Black | 067b354 | 2014-09-23 01:15:14 -0700 | [diff] [blame] | 202 | enable_rw_mount "${DST_ROOTFS}" |
| 203 | sudo mount "${DST_ROOTFS}" "${TEMP_MNT}" |
Will Drewry | efce668 | 2010-07-23 19:43:27 -0500 | [diff] [blame] | 204 | mkdir -p "${TEMP_ESP_MNT}" |
Gabe Black | 067b354 | 2014-09-23 01:15:14 -0700 | [diff] [blame] | 205 | sudo mount "${DST_ESP}" "${TEMP_ESP_MNT}" |
Will Drewry | efce668 | 2010-07-23 19:43:27 -0500 | [diff] [blame] | 206 | |
Will Drewry | 537caa9 | 2010-08-20 20:30:56 -0500 | [diff] [blame] | 207 | # Unmount everything prior to building a final image |
Mike Frysinger | 9ebc8ce | 2015-04-06 13:15:01 -0400 | [diff] [blame] | 208 | trap 'die_err_trap' INT TERM EXIT |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 209 | cleanup |
| 210 | |
Gabe Black | b982759 | 2014-09-03 21:58:11 -0700 | [diff] [blame] | 211 | # Make the built-image bootable. |
Will Drewry | 537caa9 | 2010-08-20 20:30:56 -0500 | [diff] [blame] | 212 | # NOTE: The TEMP_IMG must live in the same image dir as the original image |
| 213 | # to operate automatically below. |
| 214 | ${SCRIPTS_DIR}/bin/cros_make_image_bootable $(dirname "${TEMP_IMG}") \ |
| 215 | $(basename "${TEMP_IMG}") \ |
Arkaitz Ruiz Alvarez | 2bf8859 | 2011-07-07 15:47:31 -0700 | [diff] [blame] | 216 | --force_developer_mode |
Will Drewry | 537caa9 | 2010-08-20 20:30:56 -0500 | [diff] [blame] | 217 | |
Gabe Black | 34bbc10 | 2014-09-10 16:35:56 -0700 | [diff] [blame] | 218 | IMAGE_DEV="" |
Gabe Black | fbdb1d5 | 2014-09-22 23:43:35 -0700 | [diff] [blame] | 219 | detach_loopback() { |
Gabe Black | 34bbc10 | 2014-09-10 16:35:56 -0700 | [diff] [blame] | 220 | if [ -n "${IMAGE_DEV}" ]; then |
Gabe Black | fbdb1d5 | 2014-09-22 23:43:35 -0700 | [diff] [blame] | 221 | loopback_detach "${IMAGE_DEV}" |
Gabe Black | 34bbc10 | 2014-09-10 16:35:56 -0700 | [diff] [blame] | 222 | fi |
| 223 | } |
Mike Frysinger | 9ebc8ce | 2015-04-06 13:15:01 -0400 | [diff] [blame] | 224 | trap 'ret=$?; detach_loopback; die_err_trap ${ret}' INT TERM EXIT |
Gabe Black | 34bbc10 | 2014-09-10 16:35:56 -0700 | [diff] [blame] | 225 | |
Gabe Black | b982759 | 2014-09-03 21:58:11 -0700 | [diff] [blame] | 226 | # cros_make_image_bootable made the kernel in slot A recovery signed. We want |
| 227 | # it to be normally signed like the one in slot B, so copy B into A. |
Gabe Black | fbdb1d5 | 2014-09-22 23:43:35 -0700 | [diff] [blame] | 228 | IMAGE_DEV=$(loopback_partscan "${TEMP_IMG}") |
Gabe Black | b982759 | 2014-09-03 21:58:11 -0700 | [diff] [blame] | 229 | sudo cp ${IMAGE_DEV}p4 ${IMAGE_DEV}p2 |
| 230 | |
Mike Frysinger | 9ebc8ce | 2015-04-06 13:15:01 -0400 | [diff] [blame] | 231 | trap 'die_err_trap' INT TERM EXIT |
| 232 | switch_to_strict_mode |
Gabe Black | fbdb1d5 | 2014-09-22 23:43:35 -0700 | [diff] [blame] | 233 | loopback_detach "${IMAGE_DEV}" |
Gabe Black | b982759 | 2014-09-03 21:58:11 -0700 | [diff] [blame] | 234 | |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 235 | echo Creating final image |
Gaurav Shah | 550edca | 2014-09-25 11:13:55 -0700 | [diff] [blame] | 236 | mv "${TEMP_IMG}" "${FLAGS_to}/${DEFAULT_QEMU_IMAGE}" |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 237 | |
Gabe Black | 067b354 | 2014-09-23 01:15:14 -0700 | [diff] [blame] | 238 | rm -rf "${TEMP_IMG}" |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 239 | |
| 240 | echo "Created image at ${FLAGS_to}" |
| 241 | |
Nicolas Norvez | 82f51ec | 2018-01-26 10:10:37 -0800 | [diff] [blame] | 242 | echo "You can start the image with:" |
| 243 | echo "cros_vm --start --image-path ${FLAGS_to}/${DEFAULT_QEMU_IMAGE}" |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 244 | |