blob: 3b9a2c920854bf7727d2a50a17338d4f0b883ab6 [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
18# Load functions and constants for chromeos-install
Gwendal Grignou0f618492014-04-07 20:05:58 +000019. /usr/share/misc/chromeos-common.sh || exit 1
Mike Frysinger566c9432019-03-02 14:18:29 -050020
21# Default values for creating VM's.
22DEFAULT_QEMU_IMAGE="chromiumos_qemu_image.bin"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053023
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053024# Flags
Liam McLoughlin12a9a842012-10-10 10:37:06 -040025DEFINE_string adjust_part "" \
26 "Adjustments to apply to the partition table"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053027DEFINE_string board "${DEFAULT_BOARD}" \
28 "Board for which the image was built"
29DEFINE_boolean factory $FLAGS_FALSE \
30 "Modify the image for manufacturing testing"
31DEFINE_boolean factory_install $FLAGS_FALSE \
32 "Modify the image for factory install shim"
Simon Glass142ca062011-02-09 13:39:43 -080033
Gabe Black1d6dc252014-08-18 17:02:09 -070034# We default to TRUE so the buildbot gets its image.
Chris Sosa8dad50d2011-02-14 15:29:32 -080035DEFINE_boolean force_copy ${FLAGS_FALSE} "Always rebuild test image"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053036DEFINE_string from "" \
37 "Directory containing rootfs.image and mbr.image"
Don Garrett7937ef82014-08-25 18:04:05 -070038DEFINE_string disk_layout "2gb-rootfs-updatable" \
Liam McLoughlinb78a7c32012-11-06 20:19:05 -050039 "The disk layout type to use for this image."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053040DEFINE_string state_image "" \
41 "Stateful partition image (defaults to creating new statful partition)"
42DEFINE_boolean test_image "${FLAGS_FALSE}" \
Jacob Kopczynski75d465c2018-07-19 12:54:17 -070043 "Acquires image from ${CHROMEOS_TEST_IMAGE_NAME} instead of " \
44 "${CHROMEOS_IMAGE_NAME}."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053045DEFINE_string to "" \
46 "Destination folder for VM output file(s)"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053047
48# Parse command line
49FLAGS "$@" || exit 1
50eval set -- "${FLAGS_ARGV}"
51
52# Die on any errors.
Brian Harring7f175a52012-03-02 05:37:00 -080053switch_to_strict_mode
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053054
Mike Frysinger79e9dcd2019-07-29 00:48:36 -040055# Get the size of a regular file or a block device.
56#
57# $1 - The regular file or block device to get the size of.
58bd_safe_size() {
59 local file="$1"
60 if [[ -b "${file}" ]]; then
61 sudo blockdev --getsize64 "${file}"
62 else
63 stat -c%s "${file}"
64 fi
65}
66
Gabe Black067b3542014-09-23 01:15:14 -070067TEMP_DIR=$(mktemp -d)
68TEMP_MNT=""
69TEMP_ESP_MNT=""
70SRC_DEV=""
71DST_DEV=""
72cleanup() {
73 if [[ -n "${TEMP_MNT}" ]]; then
74 safe_umount "${TEMP_MNT}" || true
75 rmdir "${TEMP_MNT}" || true
76 fi
77 if [[ -n "${TEMP_ESP_MNT}" ]]; then
78 safe_umount "${TEMP_ESP_MNT}" || true
79 rmdir "${TEMP_ESP_MNT}" || true
80 fi
81
82 if [[ -n "${SRC_DEV}" ]]; then
83 loopback_detach "${SRC_DEV}" || true
84 fi
85 if [[ -n "${DST_DEV}" ]]; then
86 loopback_detach "${DST_DEV}" || true
87 fi
88 rm -rf "${TEMP_DIR}"
89}
Mike Frysinger9ebc8ce2015-04-06 13:15:01 -040090trap 'ret=$?; cleanup; die_err_trap ${ret}' INT TERM EXIT
Gabe Black067b3542014-09-23 01:15:14 -070091
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053092# Default to the most recent image
93if [ -z "${FLAGS_from}" ] ; then
Alex Klein999443c2018-10-17 12:02:05 -060094 FLAGS_from="${IMAGES_DIR}/${FLAGS_board}/latest"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053095fi
96if [ -z "${FLAGS_to}" ] ; then
97 FLAGS_to="${FLAGS_from}"
98fi
99
Mike Frysingerb86854a2014-11-25 18:43:58 -0500100# Convert args to full paths. Use echo here on the unquoted value to process all
101# shell level expansions like ~ and *.
102if ! resolved=$(readlink -f "$(echo ${FLAGS_from})"); then
103 die_notrace "image_to_vm: processing --from failed." \
104 "Verify the path exists: ${FLAGS_from}" \
105 " cwd: ${PWD}"
106fi
107FLAGS_from=${resolved}
108if ! resolved=$(readlink -f "$(echo ${FLAGS_to})"); then
109 die_notrace "image_to_vm: Processing --to failed." \
110 "Verify the path exists: ${FLAGS_to}" \
111 " cwd: ${PWD}"
112fi
113FLAGS_to=${resolved}
114
Hung-Te Lin269680c2016-05-30 14:47:37 +0800115if [ ${FLAGS_test_image} -eq ${FLAGS_TRUE} ]; then
David James7efb76c2013-01-09 15:25:58 -0800116 SRC_IMAGE="${FLAGS_from}/${CHROMEOS_TEST_IMAGE_NAME}"
Simon Glass142ca062011-02-09 13:39:43 -0800117else
118 # Use the standard image
119 SRC_IMAGE="${FLAGS_from}/${CHROMEOS_IMAGE_NAME}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530120fi
Mike Frysingerb86854a2014-11-25 18:43:58 -0500121if [[ ! -e ${SRC_IMAGE} ]]; then
122 die_notrace "image_to_vm: src image does not exist: ${SRC_IMAGE}" \
123 "Please verify you have selected the right input." \
124 "Note: only dev/test/factory images can be used as inputs."
125fi
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530126
Prathmesh Prabhuaa96e572017-08-31 22:42:37 -0700127if [[ -z "${FLAGS_board}" ]] && [[ -n "${FLAGS_from}" ]]; then
128 # The user may not know the board of in the input image, so infer it for them.
129 # TODO(pprabhu): This will fail if the user's chroot has a default_board set
130 # which is different from the image provided, because FLAGS_board will use
131 # that. Fix this project-wide by respecting the --board flag when provided,
132 # but preferring the board inferred from FLAGS_from over the default,
133 # everywhere.
134 FLAGS_board="$(
135 . "${BUILD_LIBRARY_DIR}/mount_gpt_util.sh"
136 get_board_from_image "${SRC_IMAGE}"
137 )"
138fi
139
140if [[ ! -d "/build/${FLAGS_board}" ]]; then
141 # Using board options and overrides requires that the board sysroot be setup
142 # (in order to read kernel and disk-image options).
143 # OTOH, we don't actually need to build any packages / update the host
144 # sysroot.
Alex Kleind65766f2019-01-11 14:57:34 -0700145 setup_board --quiet --board="${FLAGS_board}" \
146 --skip-toolchain-update --skip-chroot-upgrade --skip-board-pkg-init
Prathmesh Prabhuaa96e572017-08-31 22:42:37 -0700147fi
Bertrand SIMONNET1e77c192015-06-03 15:22:01 -0700148. "${BUILD_LIBRARY_DIR}/board_options.sh" || exit 1
149. "${SCRIPT_ROOT}/build_library/disk_layout_util.sh" || exit 1
150
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530151# Memory units are in MBs
Greg Spencer798d75f2011-02-01 22:04:49 -0800152TEMP_IMG="$(dirname "${SRC_IMAGE}")/vm_temp_image.bin"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530153
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530154# Split apart the partitions and make some new ones
Gabe Black067b3542014-09-23 01:15:14 -0700155SRC_DEV=$(loopback_partscan "${SRC_IMAGE}")
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530156
157# Fix the kernel command line
Gabe Black067b3542014-09-23 01:15:14 -0700158SRC_STATE="${SRC_DEV}"p1
159SRC_ROOTFS="${SRC_DEV}"p3
160SRC_KERN="${SRC_DEV}"p4
161SRC_OEM="${SRC_DEV}"p8
162SRC_ESP="${SRC_DEV}"p12
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530163if [ -n "${FLAGS_state_image}" ]; then
164 TEMP_STATE="${FLAGS_state_image}"
Rahul Chaturvedie770e162010-07-15 00:35:11 +0530165else
Liam McLoughlinb78a7c32012-11-06 20:19:05 -0500166 STATEFUL_SIZE_BYTES=$(get_filesystem_size "${FLAGS_disk_layout}" 1)
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700167 STATEFUL_SIZE_MEGABYTES=$(( STATEFUL_SIZE_BYTES / 1024 / 1024 ))
Gabe Black067b3542014-09-23 01:15:14 -0700168 original_image_size=$(bd_safe_size "${SRC_STATE}")
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700169 if [ "${original_image_size}" -gt "${STATEFUL_SIZE_BYTES}" ]; then
Sam Hurstc94b7f92018-06-07 07:14:54 -0700170 if [ $(( original_image_size - STATEFUL_SIZE_BYTES )) -lt \
171 $(( 1024 * 1024 )) ]; then
172 # cgpt.py sometimes makes the stateful a tiny bit larger to
173 # counteract alignment losses.
174 # This is fine -- just keep using the slightly larger partition as it is.
175 TEMP_STATE="${SRC_STATE}"
176 else
177 die "Cannot resize stateful image to smaller than original. Exiting."
178 fi
179 else
180 echo "Resizing stateful partition to ${STATEFUL_SIZE_MEGABYTES}MB"
181 # Extend the original file size to the new size.
182 TEMP_STATE="${TEMP_DIR}"/stateful
183 # Create TEMP_STATE as a regular user so a regular user can delete it.
184 sudo dd if="${SRC_STATE}" bs=16M status=none > "${TEMP_STATE}"
185 sudo e2fsck -pf "${TEMP_STATE}"
186 sudo resize2fs "${TEMP_STATE}" ${STATEFUL_SIZE_MEGABYTES}M
Rahul Chaturvedie770e162010-07-15 00:35:11 +0530187 fi
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530188fi
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530189TEMP_PMBR="${TEMP_DIR}"/pmbr
190dd if="${SRC_IMAGE}" of="${TEMP_PMBR}" bs=512 count=1
191
Gabe Black067b3542014-09-23 01:15:14 -0700192# Set up a new partition table.
193PARTITION_SCRIPT_PATH=$(mktemp)
194write_partition_script "${FLAGS_disk_layout}" "${PARTITION_SCRIPT_PATH}"
195. "${PARTITION_SCRIPT_PATH}"
196write_partition_table "${TEMP_IMG}" "${TEMP_PMBR}"
197rm "${PARTITION_SCRIPT_PATH}"
198
199DST_DEV=$(loopback_partscan "${TEMP_IMG}")
200DST_STATE="${DST_DEV}"p1
201DST_ROOTFS="${DST_DEV}"p3
202DST_KERN="${DST_DEV}"p4
203DST_OEM="${DST_DEV}"p8
204DST_ESP="${DST_DEV}"p12
205
206# Copy into the partition parts of the file.
LaMont Jonesb24a4bd2019-06-17 09:00:34 -0600207# When copying to (or from) a non-regular file, cp ignores --sparse. Since we
208# have created a collection of empty partitions for the new image above, we can
209# use 'dd conv=sparse' to both speed up the copy, and (apparently) avoid
210# b/135292499. See also crbug.com/957712. This only works because we know that
211# the destination partition is all zeros.
212sudo dd if="${SRC_ROOTFS}" of="${DST_ROOTFS}" conv=sparse bs=2M
213sudo dd if="${TEMP_STATE}" of="${DST_STATE}" conv=sparse bs=2M
214sudo dd if="${SRC_ESP}" of="${DST_ESP}" conv=sparse bs=2M
215sudo dd if="${SRC_OEM}" of="${DST_OEM}" conv=sparse bs=2M
Allen Webb6b80db62019-06-12 10:06:01 -0700216sync
Gabe Black067b3542014-09-23 01:15:14 -0700217
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530218TEMP_MNT=$(mktemp -d)
Will Drewryefce6682010-07-23 19:43:27 -0500219TEMP_ESP_MNT=$(mktemp -d)
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530220mkdir -p "${TEMP_MNT}"
Gabe Black067b3542014-09-23 01:15:14 -0700221enable_rw_mount "${DST_ROOTFS}"
222sudo mount "${DST_ROOTFS}" "${TEMP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500223mkdir -p "${TEMP_ESP_MNT}"
Gabe Black067b3542014-09-23 01:15:14 -0700224sudo mount "${DST_ESP}" "${TEMP_ESP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500225
Will Drewry537caa92010-08-20 20:30:56 -0500226# Unmount everything prior to building a final image
Mike Frysinger9ebc8ce2015-04-06 13:15:01 -0400227trap 'die_err_trap' INT TERM EXIT
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530228cleanup
229
Gabe Blackb9827592014-09-03 21:58:11 -0700230# Make the built-image bootable.
Will Drewry537caa92010-08-20 20:30:56 -0500231# NOTE: The TEMP_IMG must live in the same image dir as the original image
232# to operate automatically below.
233${SCRIPTS_DIR}/bin/cros_make_image_bootable $(dirname "${TEMP_IMG}") \
234 $(basename "${TEMP_IMG}") \
Arkaitz Ruiz Alvarez2bf88592011-07-07 15:47:31 -0700235 --force_developer_mode
Will Drewry537caa92010-08-20 20:30:56 -0500236
Gabe Black34bbc102014-09-10 16:35:56 -0700237IMAGE_DEV=""
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700238detach_loopback() {
Gabe Black34bbc102014-09-10 16:35:56 -0700239 if [ -n "${IMAGE_DEV}" ]; then
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700240 loopback_detach "${IMAGE_DEV}"
Gabe Black34bbc102014-09-10 16:35:56 -0700241 fi
242}
Mike Frysinger9ebc8ce2015-04-06 13:15:01 -0400243trap 'ret=$?; detach_loopback; die_err_trap ${ret}' INT TERM EXIT
Gabe Black34bbc102014-09-10 16:35:56 -0700244
Gabe Blackb9827592014-09-03 21:58:11 -0700245# cros_make_image_bootable made the kernel in slot A recovery signed. We want
246# it to be normally signed like the one in slot B, so copy B into A.
LaMont Jonesb24a4bd2019-06-17 09:00:34 -0600247# Because cros_make_image_bootable overwrote p2 above, we cannot do a sparse
248# copy.
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700249IMAGE_DEV=$(loopback_partscan "${TEMP_IMG}")
LaMont Jonesb24a4bd2019-06-17 09:00:34 -0600250sudo dd if=${IMAGE_DEV}p4 of=${IMAGE_DEV}p2 bs=2M
Allen Webb6b80db62019-06-12 10:06:01 -0700251sync
Gabe Blackb9827592014-09-03 21:58:11 -0700252
Mike Frysinger9ebc8ce2015-04-06 13:15:01 -0400253trap 'die_err_trap' INT TERM EXIT
254switch_to_strict_mode
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700255loopback_detach "${IMAGE_DEV}"
Gabe Blackb9827592014-09-03 21:58:11 -0700256
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530257echo Creating final image
Gaurav Shah550edca2014-09-25 11:13:55 -0700258mv "${TEMP_IMG}" "${FLAGS_to}/${DEFAULT_QEMU_IMAGE}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530259
Gabe Black067b3542014-09-23 01:15:14 -0700260rm -rf "${TEMP_IMG}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530261
262echo "Created image at ${FLAGS_to}"
263
Nicolas Norvez82f51ec2018-01-26 10:10:37 -0800264echo "You can start the image with:"
Achuith Bhandarkar0faf2d12020-06-30 08:36:32 +0000265echo "cros_vm --start --board ${FLAGS_board} \
266--image-path ${FLAGS_to}/${DEFAULT_QEMU_IMAGE}"