blob: 4d5a0e79bdee5420698bb83bb6a98a39e57216c3 [file] [log] [blame]
Rahul Chaturvedib5643e82010-07-09 10:46:05 +05301#!/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 Shah550edca2014-09-25 11:13:55 -07007# Script to convert the output of build_image.sh to a QEMU image.
Rahul Chaturvedib5643e82010-07-09 10:46:05 +05308
Kees Cook84a4c7a2011-10-18 13:21:41 -07009# Helper scripts should be run from the same location as this script.
10SCRIPT_ROOT=$(dirname "$(readlink -f "$0")")
David James359d3e12012-07-10 13:09:48 -070011. "${SCRIPT_ROOT}/common.sh" || exit 1
Liam McLoughlin5b37c542012-08-16 11:09:37 -070012. "${SCRIPT_ROOT}/build_library/build_common.sh" || exit 1
Mike Frysingerdf7d5e62019-08-21 23:24:51 -040013. "${SCRIPT_ROOT}/build_library/ext2_sb_util.sh" || exit 1
Greg Spencer798d75f2011-02-01 22:04:49 -080014
15# Need to be inside the chroot to load chromeos-common.sh
16assert_inside_chroot
17
Mike Frysinger566c9432019-03-02 14:18:29 -050018# Default values for creating VM's.
19DEFAULT_QEMU_IMAGE="chromiumos_qemu_image.bin"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053020
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053021# Flags
Liam McLoughlin12a9a842012-10-10 10:37:06 -040022DEFINE_string adjust_part "" \
23 "Adjustments to apply to the partition table"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053024DEFINE_string board "${DEFAULT_BOARD}" \
25 "Board for which the image was built"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053026DEFINE_string from "" \
27 "Directory containing rootfs.image and mbr.image"
Don Garrett7937ef82014-08-25 18:04:05 -070028DEFINE_string disk_layout "2gb-rootfs-updatable" \
Liam McLoughlinb78a7c32012-11-06 20:19:05 -050029 "The disk layout type to use for this image."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053030DEFINE_boolean test_image "${FLAGS_FALSE}" \
Jacob Kopczynski75d465c2018-07-19 12:54:17 -070031 "Acquires image from ${CHROMEOS_TEST_IMAGE_NAME} instead of " \
32 "${CHROMEOS_IMAGE_NAME}."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053033DEFINE_string to "" \
34 "Destination folder for VM output file(s)"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053035
36# Parse command line
37FLAGS "$@" || exit 1
38eval set -- "${FLAGS_ARGV}"
39
40# Die on any errors.
Brian Harring7f175a52012-03-02 05:37:00 -080041switch_to_strict_mode
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053042
Mike Frysinger79e9dcd2019-07-29 00:48:36 -040043# Get the size of a regular file or a block device.
44#
45# $1 - The regular file or block device to get the size of.
46bd_safe_size() {
47 local file="$1"
48 if [[ -b "${file}" ]]; then
49 sudo blockdev --getsize64 "${file}"
50 else
51 stat -c%s "${file}"
52 fi
53}
54
Gabe Black067b3542014-09-23 01:15:14 -070055TEMP_DIR=$(mktemp -d)
56TEMP_MNT=""
57TEMP_ESP_MNT=""
58SRC_DEV=""
59DST_DEV=""
60cleanup() {
61 if [[ -n "${TEMP_MNT}" ]]; then
62 safe_umount "${TEMP_MNT}" || true
63 rmdir "${TEMP_MNT}" || true
64 fi
65 if [[ -n "${TEMP_ESP_MNT}" ]]; then
66 safe_umount "${TEMP_ESP_MNT}" || true
67 rmdir "${TEMP_ESP_MNT}" || true
68 fi
69
70 if [[ -n "${SRC_DEV}" ]]; then
71 loopback_detach "${SRC_DEV}" || true
72 fi
73 if [[ -n "${DST_DEV}" ]]; then
74 loopback_detach "${DST_DEV}" || true
75 fi
76 rm -rf "${TEMP_DIR}"
77}
Mike Frysinger9ebc8ce2015-04-06 13:15:01 -040078trap 'ret=$?; cleanup; die_err_trap ${ret}' INT TERM EXIT
Gabe Black067b3542014-09-23 01:15:14 -070079
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053080# Default to the most recent image
81if [ -z "${FLAGS_from}" ] ; then
Alex Klein999443c2018-10-17 12:02:05 -060082 FLAGS_from="${IMAGES_DIR}/${FLAGS_board}/latest"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053083fi
84if [ -z "${FLAGS_to}" ] ; then
85 FLAGS_to="${FLAGS_from}"
86fi
87
Mike Frysingerb86854a2014-11-25 18:43:58 -050088# Convert args to full paths. Use echo here on the unquoted value to process all
89# shell level expansions like ~ and *.
90if ! resolved=$(readlink -f "$(echo ${FLAGS_from})"); then
91 die_notrace "image_to_vm: processing --from failed." \
92 "Verify the path exists: ${FLAGS_from}" \
93 " cwd: ${PWD}"
94fi
95FLAGS_from=${resolved}
96if ! resolved=$(readlink -f "$(echo ${FLAGS_to})"); then
97 die_notrace "image_to_vm: Processing --to failed." \
98 "Verify the path exists: ${FLAGS_to}" \
99 " cwd: ${PWD}"
100fi
101FLAGS_to=${resolved}
102
Hung-Te Lin269680c2016-05-30 14:47:37 +0800103if [ ${FLAGS_test_image} -eq ${FLAGS_TRUE} ]; then
David James7efb76c2013-01-09 15:25:58 -0800104 SRC_IMAGE="${FLAGS_from}/${CHROMEOS_TEST_IMAGE_NAME}"
Simon Glass142ca062011-02-09 13:39:43 -0800105else
106 # Use the standard image
107 SRC_IMAGE="${FLAGS_from}/${CHROMEOS_IMAGE_NAME}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530108fi
Mike Frysingerb86854a2014-11-25 18:43:58 -0500109if [[ ! -e ${SRC_IMAGE} ]]; then
110 die_notrace "image_to_vm: src image does not exist: ${SRC_IMAGE}" \
111 "Please verify you have selected the right input." \
112 "Note: only dev/test/factory images can be used as inputs."
113fi
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530114
Prathmesh Prabhuaa96e572017-08-31 22:42:37 -0700115if [[ -z "${FLAGS_board}" ]] && [[ -n "${FLAGS_from}" ]]; then
116 # The user may not know the board of in the input image, so infer it for them.
117 # TODO(pprabhu): This will fail if the user's chroot has a default_board set
118 # which is different from the image provided, because FLAGS_board will use
119 # that. Fix this project-wide by respecting the --board flag when provided,
120 # but preferring the board inferred from FLAGS_from over the default,
121 # everywhere.
122 FLAGS_board="$(
123 . "${BUILD_LIBRARY_DIR}/mount_gpt_util.sh"
124 get_board_from_image "${SRC_IMAGE}"
125 )"
126fi
127
128if [[ ! -d "/build/${FLAGS_board}" ]]; then
129 # Using board options and overrides requires that the board sysroot be setup
130 # (in order to read kernel and disk-image options).
131 # OTOH, we don't actually need to build any packages / update the host
132 # sysroot.
Alex Kleind65766f2019-01-11 14:57:34 -0700133 setup_board --quiet --board="${FLAGS_board}" \
134 --skip-toolchain-update --skip-chroot-upgrade --skip-board-pkg-init
Prathmesh Prabhuaa96e572017-08-31 22:42:37 -0700135fi
Bertrand SIMONNET1e77c192015-06-03 15:22:01 -0700136. "${BUILD_LIBRARY_DIR}/board_options.sh" || exit 1
137. "${SCRIPT_ROOT}/build_library/disk_layout_util.sh" || exit 1
138
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530139# Memory units are in MBs
Greg Spencer798d75f2011-02-01 22:04:49 -0800140TEMP_IMG="$(dirname "${SRC_IMAGE}")/vm_temp_image.bin"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530141
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530142# Split apart the partitions and make some new ones
Gabe Black067b3542014-09-23 01:15:14 -0700143SRC_DEV=$(loopback_partscan "${SRC_IMAGE}")
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530144
145# Fix the kernel command line
Gabe Black067b3542014-09-23 01:15:14 -0700146SRC_STATE="${SRC_DEV}"p1
147SRC_ROOTFS="${SRC_DEV}"p3
148SRC_KERN="${SRC_DEV}"p4
149SRC_OEM="${SRC_DEV}"p8
150SRC_ESP="${SRC_DEV}"p12
Mike Frysinger629b6ad2021-01-25 23:23:31 -0500151STATEFUL_SIZE_BYTES=$(get_filesystem_size "${FLAGS_disk_layout}" 1)
152STATEFUL_SIZE_MEGABYTES=$(( STATEFUL_SIZE_BYTES / 1024 / 1024 ))
153original_image_size=$(bd_safe_size "${SRC_STATE}")
154if [ "${original_image_size}" -gt "${STATEFUL_SIZE_BYTES}" ]; then
155 if [ $(( original_image_size - STATEFUL_SIZE_BYTES )) -lt \
156 $(( 1024 * 1024 )) ]; then
157 # cgpt.py sometimes makes the stateful a tiny bit larger to
158 # counteract alignment losses.
159 # This is fine -- just keep using the slightly larger partition as it is.
160 TEMP_STATE="${SRC_STATE}"
Sam Hurstc94b7f92018-06-07 07:14:54 -0700161 else
Mike Frysinger629b6ad2021-01-25 23:23:31 -0500162 die "Cannot resize stateful image to smaller than original. Exiting."
Rahul Chaturvedie770e162010-07-15 00:35:11 +0530163 fi
Mike Frysinger629b6ad2021-01-25 23:23:31 -0500164else
165 echo "Resizing stateful partition to ${STATEFUL_SIZE_MEGABYTES}MB"
166 # Extend the original file size to the new size.
167 TEMP_STATE="${TEMP_DIR}"/stateful
168 # Create TEMP_STATE as a regular user so a regular user can delete it.
169 sudo dd if="${SRC_STATE}" bs=16M status=none > "${TEMP_STATE}"
170 sudo e2fsck -pf "${TEMP_STATE}"
171 sudo resize2fs "${TEMP_STATE}" ${STATEFUL_SIZE_MEGABYTES}M
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530172fi
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530173TEMP_PMBR="${TEMP_DIR}"/pmbr
174dd if="${SRC_IMAGE}" of="${TEMP_PMBR}" bs=512 count=1
175
Gabe Black067b3542014-09-23 01:15:14 -0700176# Set up a new partition table.
177PARTITION_SCRIPT_PATH=$(mktemp)
178write_partition_script "${FLAGS_disk_layout}" "${PARTITION_SCRIPT_PATH}"
179. "${PARTITION_SCRIPT_PATH}"
180write_partition_table "${TEMP_IMG}" "${TEMP_PMBR}"
181rm "${PARTITION_SCRIPT_PATH}"
182
183DST_DEV=$(loopback_partscan "${TEMP_IMG}")
184DST_STATE="${DST_DEV}"p1
185DST_ROOTFS="${DST_DEV}"p3
186DST_KERN="${DST_DEV}"p4
187DST_OEM="${DST_DEV}"p8
188DST_ESP="${DST_DEV}"p12
189
190# Copy into the partition parts of the file.
LaMont Jonesb24a4bd2019-06-17 09:00:34 -0600191# When copying to (or from) a non-regular file, cp ignores --sparse. Since we
192# have created a collection of empty partitions for the new image above, we can
193# use 'dd conv=sparse' to both speed up the copy, and (apparently) avoid
194# b/135292499. See also crbug.com/957712. This only works because we know that
195# the destination partition is all zeros.
196sudo dd if="${SRC_ROOTFS}" of="${DST_ROOTFS}" conv=sparse bs=2M
197sudo dd if="${TEMP_STATE}" of="${DST_STATE}" conv=sparse bs=2M
198sudo dd if="${SRC_ESP}" of="${DST_ESP}" conv=sparse bs=2M
199sudo dd if="${SRC_OEM}" of="${DST_OEM}" conv=sparse bs=2M
Allen Webb6b80db62019-06-12 10:06:01 -0700200sync
Gabe Black067b3542014-09-23 01:15:14 -0700201
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530202TEMP_MNT=$(mktemp -d)
Will Drewryefce6682010-07-23 19:43:27 -0500203TEMP_ESP_MNT=$(mktemp -d)
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530204mkdir -p "${TEMP_MNT}"
Gabe Black067b3542014-09-23 01:15:14 -0700205enable_rw_mount "${DST_ROOTFS}"
206sudo mount "${DST_ROOTFS}" "${TEMP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500207mkdir -p "${TEMP_ESP_MNT}"
Gabe Black067b3542014-09-23 01:15:14 -0700208sudo mount "${DST_ESP}" "${TEMP_ESP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500209
Will Drewry537caa92010-08-20 20:30:56 -0500210# Unmount everything prior to building a final image
Mike Frysinger9ebc8ce2015-04-06 13:15:01 -0400211trap 'die_err_trap' INT TERM EXIT
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530212cleanup
213
Gabe Blackb9827592014-09-03 21:58:11 -0700214# Make the built-image bootable.
Will Drewry537caa92010-08-20 20:30:56 -0500215# NOTE: The TEMP_IMG must live in the same image dir as the original image
216# to operate automatically below.
217${SCRIPTS_DIR}/bin/cros_make_image_bootable $(dirname "${TEMP_IMG}") \
218 $(basename "${TEMP_IMG}") \
Arkaitz Ruiz Alvarez2bf88592011-07-07 15:47:31 -0700219 --force_developer_mode
Will Drewry537caa92010-08-20 20:30:56 -0500220
Gabe Black34bbc102014-09-10 16:35:56 -0700221IMAGE_DEV=""
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700222detach_loopback() {
Gabe Black34bbc102014-09-10 16:35:56 -0700223 if [ -n "${IMAGE_DEV}" ]; then
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700224 loopback_detach "${IMAGE_DEV}"
Gabe Black34bbc102014-09-10 16:35:56 -0700225 fi
226}
Mike Frysinger9ebc8ce2015-04-06 13:15:01 -0400227trap 'ret=$?; detach_loopback; die_err_trap ${ret}' INT TERM EXIT
Gabe Black34bbc102014-09-10 16:35:56 -0700228
Gabe Blackb9827592014-09-03 21:58:11 -0700229# cros_make_image_bootable made the kernel in slot A recovery signed. We want
230# it to be normally signed like the one in slot B, so copy B into A.
LaMont Jonesb24a4bd2019-06-17 09:00:34 -0600231# Because cros_make_image_bootable overwrote p2 above, we cannot do a sparse
232# copy.
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700233IMAGE_DEV=$(loopback_partscan "${TEMP_IMG}")
LaMont Jonesb24a4bd2019-06-17 09:00:34 -0600234sudo dd if=${IMAGE_DEV}p4 of=${IMAGE_DEV}p2 bs=2M
Allen Webb6b80db62019-06-12 10:06:01 -0700235sync
Gabe Blackb9827592014-09-03 21:58:11 -0700236
Mike Frysinger9ebc8ce2015-04-06 13:15:01 -0400237trap 'die_err_trap' INT TERM EXIT
238switch_to_strict_mode
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700239loopback_detach "${IMAGE_DEV}"
Gabe Blackb9827592014-09-03 21:58:11 -0700240
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530241echo Creating final image
Gaurav Shah550edca2014-09-25 11:13:55 -0700242mv "${TEMP_IMG}" "${FLAGS_to}/${DEFAULT_QEMU_IMAGE}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530243
Gabe Black067b3542014-09-23 01:15:14 -0700244rm -rf "${TEMP_IMG}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530245
246echo "Created image at ${FLAGS_to}"
247
Nicolas Norvez82f51ec2018-01-26 10:10:37 -0800248echo "You can start the image with:"
Achuith Bhandarkar0faf2d12020-06-30 08:36:32 +0000249echo "cros_vm --start --board ${FLAGS_board} \
250--image-path ${FLAGS_to}/${DEFAULT_QEMU_IMAGE}"