blob: a70e76a5636d95bc95e74a30a5e9b33391ad42ae [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
Greg Spencer798d75f2011-02-01 22:04:49 -080013
14# Need to be inside the chroot to load chromeos-common.sh
15assert_inside_chroot
16
17# Load functions and constants for chromeos-install
Gwendal Grignou0f618492014-04-07 20:05:58 +000018. /usr/share/misc/chromeos-common.sh || exit 1
Mike Frysinger566c9432019-03-02 14:18:29 -050019
20# Default values for creating VM's.
21DEFAULT_QEMU_IMAGE="chromiumos_qemu_image.bin"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053022
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053023# Flags
Liam McLoughlin12a9a842012-10-10 10:37:06 -040024DEFINE_string adjust_part "" \
25 "Adjustments to apply to the partition table"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053026DEFINE_string board "${DEFAULT_BOARD}" \
27 "Board for which the image was built"
28DEFINE_boolean factory $FLAGS_FALSE \
29 "Modify the image for manufacturing testing"
30DEFINE_boolean factory_install $FLAGS_FALSE \
31 "Modify the image for factory install shim"
Simon Glass142ca062011-02-09 13:39:43 -080032
Gabe Black1d6dc252014-08-18 17:02:09 -070033# We default to TRUE so the buildbot gets its image.
Chris Sosa8dad50d2011-02-14 15:29:32 -080034DEFINE_boolean force_copy ${FLAGS_FALSE} "Always rebuild test image"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053035DEFINE_string from "" \
36 "Directory containing rootfs.image and mbr.image"
Don Garrett7937ef82014-08-25 18:04:05 -070037DEFINE_string disk_layout "2gb-rootfs-updatable" \
Liam McLoughlinb78a7c32012-11-06 20:19:05 -050038 "The disk layout type to use for this image."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053039DEFINE_string state_image "" \
40 "Stateful partition image (defaults to creating new statful partition)"
41DEFINE_boolean test_image "${FLAGS_FALSE}" \
Jacob Kopczynski75d465c2018-07-19 12:54:17 -070042 "Acquires image from ${CHROMEOS_TEST_IMAGE_NAME} instead of " \
43 "${CHROMEOS_IMAGE_NAME}."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053044DEFINE_string to "" \
45 "Destination folder for VM output file(s)"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053046
47# Parse command line
48FLAGS "$@" || exit 1
49eval set -- "${FLAGS_ARGV}"
50
51# Die on any errors.
Brian Harring7f175a52012-03-02 05:37:00 -080052switch_to_strict_mode
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053053
Mike Frysinger79e9dcd2019-07-29 00:48:36 -040054# Get the size of a regular file or a block device.
55#
56# $1 - The regular file or block device to get the size of.
57bd_safe_size() {
58 local file="$1"
59 if [[ -b "${file}" ]]; then
60 sudo blockdev --getsize64 "${file}"
61 else
62 stat -c%s "${file}"
63 fi
64}
65
Gabe Black067b3542014-09-23 01:15:14 -070066TEMP_DIR=$(mktemp -d)
67TEMP_MNT=""
68TEMP_ESP_MNT=""
69SRC_DEV=""
70DST_DEV=""
71cleanup() {
72 if [[ -n "${TEMP_MNT}" ]]; then
73 safe_umount "${TEMP_MNT}" || true
74 rmdir "${TEMP_MNT}" || true
75 fi
76 if [[ -n "${TEMP_ESP_MNT}" ]]; then
77 safe_umount "${TEMP_ESP_MNT}" || true
78 rmdir "${TEMP_ESP_MNT}" || true
79 fi
80
81 if [[ -n "${SRC_DEV}" ]]; then
82 loopback_detach "${SRC_DEV}" || true
83 fi
84 if [[ -n "${DST_DEV}" ]]; then
85 loopback_detach "${DST_DEV}" || true
86 fi
87 rm -rf "${TEMP_DIR}"
88}
Mike Frysinger9ebc8ce2015-04-06 13:15:01 -040089trap 'ret=$?; cleanup; die_err_trap ${ret}' INT TERM EXIT
Gabe Black067b3542014-09-23 01:15:14 -070090
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053091# Default to the most recent image
92if [ -z "${FLAGS_from}" ] ; then
Alex Klein999443c2018-10-17 12:02:05 -060093 FLAGS_from="${IMAGES_DIR}/${FLAGS_board}/latest"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053094fi
95if [ -z "${FLAGS_to}" ] ; then
96 FLAGS_to="${FLAGS_from}"
97fi
98
Mike Frysingerb86854a2014-11-25 18:43:58 -050099# Convert args to full paths. Use echo here on the unquoted value to process all
100# shell level expansions like ~ and *.
101if ! resolved=$(readlink -f "$(echo ${FLAGS_from})"); then
102 die_notrace "image_to_vm: processing --from failed." \
103 "Verify the path exists: ${FLAGS_from}" \
104 " cwd: ${PWD}"
105fi
106FLAGS_from=${resolved}
107if ! resolved=$(readlink -f "$(echo ${FLAGS_to})"); then
108 die_notrace "image_to_vm: Processing --to failed." \
109 "Verify the path exists: ${FLAGS_to}" \
110 " cwd: ${PWD}"
111fi
112FLAGS_to=${resolved}
113
Hung-Te Lin269680c2016-05-30 14:47:37 +0800114if [ ${FLAGS_test_image} -eq ${FLAGS_TRUE} ]; then
David James7efb76c2013-01-09 15:25:58 -0800115 SRC_IMAGE="${FLAGS_from}/${CHROMEOS_TEST_IMAGE_NAME}"
Simon Glass142ca062011-02-09 13:39:43 -0800116else
117 # Use the standard image
118 SRC_IMAGE="${FLAGS_from}/${CHROMEOS_IMAGE_NAME}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530119fi
Mike Frysingerb86854a2014-11-25 18:43:58 -0500120if [[ ! -e ${SRC_IMAGE} ]]; then
121 die_notrace "image_to_vm: src image does not exist: ${SRC_IMAGE}" \
122 "Please verify you have selected the right input." \
123 "Note: only dev/test/factory images can be used as inputs."
124fi
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530125
Prathmesh Prabhuaa96e572017-08-31 22:42:37 -0700126if [[ -z "${FLAGS_board}" ]] && [[ -n "${FLAGS_from}" ]]; then
127 # The user may not know the board of in the input image, so infer it for them.
128 # TODO(pprabhu): This will fail if the user's chroot has a default_board set
129 # which is different from the image provided, because FLAGS_board will use
130 # that. Fix this project-wide by respecting the --board flag when provided,
131 # but preferring the board inferred from FLAGS_from over the default,
132 # everywhere.
133 FLAGS_board="$(
134 . "${BUILD_LIBRARY_DIR}/mount_gpt_util.sh"
135 get_board_from_image "${SRC_IMAGE}"
136 )"
137fi
138
139if [[ ! -d "/build/${FLAGS_board}" ]]; then
140 # Using board options and overrides requires that the board sysroot be setup
141 # (in order to read kernel and disk-image options).
142 # OTOH, we don't actually need to build any packages / update the host
143 # sysroot.
Alex Kleind65766f2019-01-11 14:57:34 -0700144 setup_board --quiet --board="${FLAGS_board}" \
145 --skip-toolchain-update --skip-chroot-upgrade --skip-board-pkg-init
Prathmesh Prabhuaa96e572017-08-31 22:42:37 -0700146fi
Bertrand SIMONNET1e77c192015-06-03 15:22:01 -0700147. "${BUILD_LIBRARY_DIR}/board_options.sh" || exit 1
148. "${SCRIPT_ROOT}/build_library/disk_layout_util.sh" || exit 1
149
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530150# Memory units are in MBs
Greg Spencer798d75f2011-02-01 22:04:49 -0800151TEMP_IMG="$(dirname "${SRC_IMAGE}")/vm_temp_image.bin"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530152
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530153# Split apart the partitions and make some new ones
Gabe Black067b3542014-09-23 01:15:14 -0700154SRC_DEV=$(loopback_partscan "${SRC_IMAGE}")
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530155
156# Fix the kernel command line
Gabe Black067b3542014-09-23 01:15:14 -0700157SRC_STATE="${SRC_DEV}"p1
158SRC_ROOTFS="${SRC_DEV}"p3
159SRC_KERN="${SRC_DEV}"p4
160SRC_OEM="${SRC_DEV}"p8
161SRC_ESP="${SRC_DEV}"p12
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530162if [ -n "${FLAGS_state_image}" ]; then
163 TEMP_STATE="${FLAGS_state_image}"
Rahul Chaturvedie770e162010-07-15 00:35:11 +0530164else
Liam McLoughlinb78a7c32012-11-06 20:19:05 -0500165 STATEFUL_SIZE_BYTES=$(get_filesystem_size "${FLAGS_disk_layout}" 1)
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700166 STATEFUL_SIZE_MEGABYTES=$(( STATEFUL_SIZE_BYTES / 1024 / 1024 ))
Gabe Black067b3542014-09-23 01:15:14 -0700167 original_image_size=$(bd_safe_size "${SRC_STATE}")
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700168 if [ "${original_image_size}" -gt "${STATEFUL_SIZE_BYTES}" ]; then
Sam Hurstc94b7f92018-06-07 07:14:54 -0700169 if [ $(( original_image_size - STATEFUL_SIZE_BYTES )) -lt \
170 $(( 1024 * 1024 )) ]; then
171 # cgpt.py sometimes makes the stateful a tiny bit larger to
172 # counteract alignment losses.
173 # This is fine -- just keep using the slightly larger partition as it is.
174 TEMP_STATE="${SRC_STATE}"
175 else
176 die "Cannot resize stateful image to smaller than original. Exiting."
177 fi
178 else
179 echo "Resizing stateful partition to ${STATEFUL_SIZE_MEGABYTES}MB"
180 # Extend the original file size to the new size.
181 TEMP_STATE="${TEMP_DIR}"/stateful
182 # Create TEMP_STATE as a regular user so a regular user can delete it.
183 sudo dd if="${SRC_STATE}" bs=16M status=none > "${TEMP_STATE}"
184 sudo e2fsck -pf "${TEMP_STATE}"
185 sudo resize2fs "${TEMP_STATE}" ${STATEFUL_SIZE_MEGABYTES}M
Rahul Chaturvedie770e162010-07-15 00:35:11 +0530186 fi
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530187fi
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530188TEMP_PMBR="${TEMP_DIR}"/pmbr
189dd if="${SRC_IMAGE}" of="${TEMP_PMBR}" bs=512 count=1
190
Gabe Black067b3542014-09-23 01:15:14 -0700191# Set up a new partition table.
192PARTITION_SCRIPT_PATH=$(mktemp)
193write_partition_script "${FLAGS_disk_layout}" "${PARTITION_SCRIPT_PATH}"
194. "${PARTITION_SCRIPT_PATH}"
195write_partition_table "${TEMP_IMG}" "${TEMP_PMBR}"
196rm "${PARTITION_SCRIPT_PATH}"
197
198DST_DEV=$(loopback_partscan "${TEMP_IMG}")
199DST_STATE="${DST_DEV}"p1
200DST_ROOTFS="${DST_DEV}"p3
201DST_KERN="${DST_DEV}"p4
202DST_OEM="${DST_DEV}"p8
203DST_ESP="${DST_DEV}"p12
204
205# Copy into the partition parts of the file.
LaMont Jonesb24a4bd2019-06-17 09:00:34 -0600206# When copying to (or from) a non-regular file, cp ignores --sparse. Since we
207# have created a collection of empty partitions for the new image above, we can
208# use 'dd conv=sparse' to both speed up the copy, and (apparently) avoid
209# b/135292499. See also crbug.com/957712. This only works because we know that
210# the destination partition is all zeros.
211sudo dd if="${SRC_ROOTFS}" of="${DST_ROOTFS}" conv=sparse bs=2M
212sudo dd if="${TEMP_STATE}" of="${DST_STATE}" conv=sparse bs=2M
213sudo dd if="${SRC_ESP}" of="${DST_ESP}" conv=sparse bs=2M
214sudo dd if="${SRC_OEM}" of="${DST_OEM}" conv=sparse bs=2M
Allen Webb6b80db62019-06-12 10:06:01 -0700215sync
Gabe Black067b3542014-09-23 01:15:14 -0700216
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530217TEMP_MNT=$(mktemp -d)
Will Drewryefce6682010-07-23 19:43:27 -0500218TEMP_ESP_MNT=$(mktemp -d)
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530219mkdir -p "${TEMP_MNT}"
Gabe Black067b3542014-09-23 01:15:14 -0700220enable_rw_mount "${DST_ROOTFS}"
221sudo mount "${DST_ROOTFS}" "${TEMP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500222mkdir -p "${TEMP_ESP_MNT}"
Gabe Black067b3542014-09-23 01:15:14 -0700223sudo mount "${DST_ESP}" "${TEMP_ESP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500224
Will Drewry537caa92010-08-20 20:30:56 -0500225# Unmount everything prior to building a final image
Mike Frysinger9ebc8ce2015-04-06 13:15:01 -0400226trap 'die_err_trap' INT TERM EXIT
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530227cleanup
228
Gabe Blackb9827592014-09-03 21:58:11 -0700229# Make the built-image bootable.
Will Drewry537caa92010-08-20 20:30:56 -0500230# NOTE: The TEMP_IMG must live in the same image dir as the original image
231# to operate automatically below.
232${SCRIPTS_DIR}/bin/cros_make_image_bootable $(dirname "${TEMP_IMG}") \
233 $(basename "${TEMP_IMG}") \
Arkaitz Ruiz Alvarez2bf88592011-07-07 15:47:31 -0700234 --force_developer_mode
Will Drewry537caa92010-08-20 20:30:56 -0500235
Gabe Black34bbc102014-09-10 16:35:56 -0700236IMAGE_DEV=""
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700237detach_loopback() {
Gabe Black34bbc102014-09-10 16:35:56 -0700238 if [ -n "${IMAGE_DEV}" ]; then
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700239 loopback_detach "${IMAGE_DEV}"
Gabe Black34bbc102014-09-10 16:35:56 -0700240 fi
241}
Mike Frysinger9ebc8ce2015-04-06 13:15:01 -0400242trap 'ret=$?; detach_loopback; die_err_trap ${ret}' INT TERM EXIT
Gabe Black34bbc102014-09-10 16:35:56 -0700243
Gabe Blackb9827592014-09-03 21:58:11 -0700244# cros_make_image_bootable made the kernel in slot A recovery signed. We want
245# it to be normally signed like the one in slot B, so copy B into A.
LaMont Jonesb24a4bd2019-06-17 09:00:34 -0600246# Because cros_make_image_bootable overwrote p2 above, we cannot do a sparse
247# copy.
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700248IMAGE_DEV=$(loopback_partscan "${TEMP_IMG}")
LaMont Jonesb24a4bd2019-06-17 09:00:34 -0600249sudo dd if=${IMAGE_DEV}p4 of=${IMAGE_DEV}p2 bs=2M
Allen Webb6b80db62019-06-12 10:06:01 -0700250sync
Gabe Blackb9827592014-09-03 21:58:11 -0700251
Mike Frysinger9ebc8ce2015-04-06 13:15:01 -0400252trap 'die_err_trap' INT TERM EXIT
253switch_to_strict_mode
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700254loopback_detach "${IMAGE_DEV}"
Gabe Blackb9827592014-09-03 21:58:11 -0700255
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530256echo Creating final image
Gaurav Shah550edca2014-09-25 11:13:55 -0700257mv "${TEMP_IMG}" "${FLAGS_to}/${DEFAULT_QEMU_IMAGE}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530258
Gabe Black067b3542014-09-23 01:15:14 -0700259rm -rf "${TEMP_IMG}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530260
261echo "Created image at ${FLAGS_to}"
262
Nicolas Norvez82f51ec2018-01-26 10:10:37 -0800263echo "You can start the image with:"
264echo "cros_vm --start --image-path ${FLAGS_to}/${DEFAULT_QEMU_IMAGE}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530265