blob: 477d98ea6a58a66dab0664f2eafd8c6e19945607 [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
Gabe Black067b3542014-09-23 01:15:14 -070054TEMP_DIR=$(mktemp -d)
55TEMP_MNT=""
56TEMP_ESP_MNT=""
57SRC_DEV=""
58DST_DEV=""
59cleanup() {
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 Frysinger9ebc8ce2015-04-06 13:15:01 -040077trap 'ret=$?; cleanup; die_err_trap ${ret}' INT TERM EXIT
Gabe Black067b3542014-09-23 01:15:14 -070078
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053079# Default to the most recent image
80if [ -z "${FLAGS_from}" ] ; then
Alex Klein999443c2018-10-17 12:02:05 -060081 FLAGS_from="${IMAGES_DIR}/${FLAGS_board}/latest"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053082fi
83if [ -z "${FLAGS_to}" ] ; then
84 FLAGS_to="${FLAGS_from}"
85fi
86
Mike Frysingerb86854a2014-11-25 18:43:58 -050087# Convert args to full paths. Use echo here on the unquoted value to process all
88# shell level expansions like ~ and *.
89if ! 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}"
93fi
94FLAGS_from=${resolved}
95if ! 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}"
99fi
100FLAGS_to=${resolved}
101
Hung-Te Lin269680c2016-05-30 14:47:37 +0800102if [ ${FLAGS_test_image} -eq ${FLAGS_TRUE} ]; then
David James7efb76c2013-01-09 15:25:58 -0800103 SRC_IMAGE="${FLAGS_from}/${CHROMEOS_TEST_IMAGE_NAME}"
Simon Glass142ca062011-02-09 13:39:43 -0800104else
105 # Use the standard image
106 SRC_IMAGE="${FLAGS_from}/${CHROMEOS_IMAGE_NAME}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530107fi
Mike Frysingerb86854a2014-11-25 18:43:58 -0500108if [[ ! -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."
112fi
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530113
Prathmesh Prabhuaa96e572017-08-31 22:42:37 -0700114if [[ -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 )"
125fi
126
127if [[ ! -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.
132 "${SCRIPT_ROOT}/setup_board" --quiet --board="${FLAGS_board}" \
133 --skip_toolchain_update --skip_chroot_upgrade --skip_board_pkg_init
134fi
Bertrand SIMONNET1e77c192015-06-03 15:22:01 -0700135. "${BUILD_LIBRARY_DIR}/board_options.sh" || exit 1
136. "${SCRIPT_ROOT}/build_library/disk_layout_util.sh" || exit 1
137
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530138# Memory units are in MBs
Greg Spencer798d75f2011-02-01 22:04:49 -0800139TEMP_IMG="$(dirname "${SRC_IMAGE}")/vm_temp_image.bin"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530140
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530141# Split apart the partitions and make some new ones
Gabe Black067b3542014-09-23 01:15:14 -0700142SRC_DEV=$(loopback_partscan "${SRC_IMAGE}")
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530143
144# Fix the kernel command line
Gabe Black067b3542014-09-23 01:15:14 -0700145SRC_STATE="${SRC_DEV}"p1
146SRC_ROOTFS="${SRC_DEV}"p3
147SRC_KERN="${SRC_DEV}"p4
148SRC_OEM="${SRC_DEV}"p8
149SRC_ESP="${SRC_DEV}"p12
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530150if [ -n "${FLAGS_state_image}" ]; then
151 TEMP_STATE="${FLAGS_state_image}"
Rahul Chaturvedie770e162010-07-15 00:35:11 +0530152else
Liam McLoughlinb78a7c32012-11-06 20:19:05 -0500153 STATEFUL_SIZE_BYTES=$(get_filesystem_size "${FLAGS_disk_layout}" 1)
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700154 STATEFUL_SIZE_MEGABYTES=$(( STATEFUL_SIZE_BYTES / 1024 / 1024 ))
Gabe Black067b3542014-09-23 01:15:14 -0700155 original_image_size=$(bd_safe_size "${SRC_STATE}")
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700156 if [ "${original_image_size}" -gt "${STATEFUL_SIZE_BYTES}" ]; then
Sam Hurstc94b7f92018-06-07 07:14:54 -0700157 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 Chaturvedie770e162010-07-15 00:35:11 +0530174 fi
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530175fi
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530176TEMP_PMBR="${TEMP_DIR}"/pmbr
177dd if="${SRC_IMAGE}" of="${TEMP_PMBR}" bs=512 count=1
178
Gabe Black067b3542014-09-23 01:15:14 -0700179# Set up a new partition table.
180PARTITION_SCRIPT_PATH=$(mktemp)
181write_partition_script "${FLAGS_disk_layout}" "${PARTITION_SCRIPT_PATH}"
182. "${PARTITION_SCRIPT_PATH}"
183write_partition_table "${TEMP_IMG}" "${TEMP_PMBR}"
184rm "${PARTITION_SCRIPT_PATH}"
185
186DST_DEV=$(loopback_partscan "${TEMP_IMG}")
187DST_STATE="${DST_DEV}"p1
188DST_ROOTFS="${DST_DEV}"p3
189DST_KERN="${DST_DEV}"p4
190DST_OEM="${DST_DEV}"p8
191DST_ESP="${DST_DEV}"p12
192
193# Copy into the partition parts of the file.
Gabe Black1dd95222014-10-04 02:59:14 -0700194sudo cp "${SRC_ROOTFS}" "${DST_ROOTFS}"
195sudo cp "${TEMP_STATE}" "${DST_STATE}"
196sudo cp "${SRC_ESP}" "${DST_ESP}"
197sudo cp "${SRC_OEM}" "${DST_OEM}"
Gabe Black067b3542014-09-23 01:15:14 -0700198
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530199TEMP_MNT=$(mktemp -d)
Will Drewryefce6682010-07-23 19:43:27 -0500200TEMP_ESP_MNT=$(mktemp -d)
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530201mkdir -p "${TEMP_MNT}"
Gabe Black067b3542014-09-23 01:15:14 -0700202enable_rw_mount "${DST_ROOTFS}"
203sudo mount "${DST_ROOTFS}" "${TEMP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500204mkdir -p "${TEMP_ESP_MNT}"
Gabe Black067b3542014-09-23 01:15:14 -0700205sudo mount "${DST_ESP}" "${TEMP_ESP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500206
Will Drewry537caa92010-08-20 20:30:56 -0500207# Unmount everything prior to building a final image
Mike Frysinger9ebc8ce2015-04-06 13:15:01 -0400208trap 'die_err_trap' INT TERM EXIT
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530209cleanup
210
Gabe Blackb9827592014-09-03 21:58:11 -0700211# Make the built-image bootable.
Will Drewry537caa92010-08-20 20:30:56 -0500212# 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 Alvarez2bf88592011-07-07 15:47:31 -0700216 --force_developer_mode
Will Drewry537caa92010-08-20 20:30:56 -0500217
Gabe Black34bbc102014-09-10 16:35:56 -0700218IMAGE_DEV=""
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700219detach_loopback() {
Gabe Black34bbc102014-09-10 16:35:56 -0700220 if [ -n "${IMAGE_DEV}" ]; then
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700221 loopback_detach "${IMAGE_DEV}"
Gabe Black34bbc102014-09-10 16:35:56 -0700222 fi
223}
Mike Frysinger9ebc8ce2015-04-06 13:15:01 -0400224trap 'ret=$?; detach_loopback; die_err_trap ${ret}' INT TERM EXIT
Gabe Black34bbc102014-09-10 16:35:56 -0700225
Gabe Blackb9827592014-09-03 21:58:11 -0700226# 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 Blackfbdb1d52014-09-22 23:43:35 -0700228IMAGE_DEV=$(loopback_partscan "${TEMP_IMG}")
Gabe Blackb9827592014-09-03 21:58:11 -0700229sudo cp ${IMAGE_DEV}p4 ${IMAGE_DEV}p2
230
Mike Frysinger9ebc8ce2015-04-06 13:15:01 -0400231trap 'die_err_trap' INT TERM EXIT
232switch_to_strict_mode
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700233loopback_detach "${IMAGE_DEV}"
Gabe Blackb9827592014-09-03 21:58:11 -0700234
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530235echo Creating final image
Gaurav Shah550edca2014-09-25 11:13:55 -0700236mv "${TEMP_IMG}" "${FLAGS_to}/${DEFAULT_QEMU_IMAGE}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530237
Gabe Black067b3542014-09-23 01:15:14 -0700238rm -rf "${TEMP_IMG}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530239
240echo "Created image at ${FLAGS_to}"
241
Nicolas Norvez82f51ec2018-01-26 10:10:37 -0800242echo "You can start the image with:"
243echo "cros_vm --start --image-path ${FLAGS_to}/${DEFAULT_QEMU_IMAGE}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530244