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