blob: e05c3fa0609a9d955ceba3b6fee266b612f4a939 [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
David James359d3e12012-07-10 13:09:48 -070019. "${SCRIPT_ROOT}/lib/cros_vm_constants.sh" || exit 1
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"
26DEFINE_boolean factory $FLAGS_FALSE \
27 "Modify the image for manufacturing testing"
28DEFINE_boolean factory_install $FLAGS_FALSE \
29 "Modify the image for factory install shim"
Simon Glass142ca062011-02-09 13:39:43 -080030
Gabe Black1d6dc252014-08-18 17:02:09 -070031# We default to TRUE so the buildbot gets its image.
Chris Sosa8dad50d2011-02-14 15:29:32 -080032DEFINE_boolean force_copy ${FLAGS_FALSE} "Always rebuild test image"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053033DEFINE_string from "" \
34 "Directory containing rootfs.image and mbr.image"
Don Garrett7937ef82014-08-25 18:04:05 -070035DEFINE_string disk_layout "2gb-rootfs-updatable" \
Liam McLoughlinb78a7c32012-11-06 20:19:05 -050036 "The disk layout type to use for this image."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053037DEFINE_string state_image "" \
38 "Stateful partition image (defaults to creating new statful partition)"
39DEFINE_boolean test_image "${FLAGS_FALSE}" \
Simon Glass142ca062011-02-09 13:39:43 -080040 "Copies normal image to ${CHROMEOS_TEST_IMAGE_NAME}, modifies it for test."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053041DEFINE_string to "" \
42 "Destination folder for VM output file(s)"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053043
44# Parse command line
45FLAGS "$@" || exit 1
46eval set -- "${FLAGS_ARGV}"
47
48# Die on any errors.
Brian Harring7f175a52012-03-02 05:37:00 -080049switch_to_strict_mode
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053050
Gabe Black067b3542014-09-23 01:15:14 -070051TEMP_DIR=$(mktemp -d)
52TEMP_MNT=""
53TEMP_ESP_MNT=""
54SRC_DEV=""
55DST_DEV=""
56cleanup() {
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 Frysinger9ebc8ce2015-04-06 13:15:01 -040074trap 'ret=$?; cleanup; die_err_trap ${ret}' INT TERM EXIT
Gabe Black067b3542014-09-23 01:15:14 -070075
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053076# Default to the most recent image
77if [ -z "${FLAGS_from}" ] ; then
Kees Cook84a4c7a2011-10-18 13:21:41 -070078 FLAGS_from="$(${SCRIPT_ROOT}/get_latest_image.sh --board=${FLAGS_board})"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053079fi
80if [ -z "${FLAGS_to}" ] ; then
81 FLAGS_to="${FLAGS_from}"
82fi
83
Mike Frysingerb86854a2014-11-25 18:43:58 -050084# Convert args to full paths. Use echo here on the unquoted value to process all
85# shell level expansions like ~ and *.
86if ! 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}"
90fi
91FLAGS_from=${resolved}
92if ! 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}"
96fi
97FLAGS_to=${resolved}
98
Hung-Te Lin269680c2016-05-30 14:47:37 +080099if [ ${FLAGS_test_image} -eq ${FLAGS_TRUE} ]; then
David James7efb76c2013-01-09 15:25:58 -0800100 SRC_IMAGE="${FLAGS_from}/${CHROMEOS_TEST_IMAGE_NAME}"
Simon Glass142ca062011-02-09 13:39:43 -0800101else
102 # Use the standard image
103 SRC_IMAGE="${FLAGS_from}/${CHROMEOS_IMAGE_NAME}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530104fi
Mike Frysingerb86854a2014-11-25 18:43:58 -0500105if [[ ! -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."
109fi
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530110
Prathmesh Prabhuaa96e572017-08-31 22:42:37 -0700111if [[ -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 )"
122fi
123
124if [[ ! -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
131fi
Bertrand SIMONNET1e77c192015-06-03 15:22:01 -0700132. "${BUILD_LIBRARY_DIR}/board_options.sh" || exit 1
133. "${SCRIPT_ROOT}/build_library/disk_layout_util.sh" || exit 1
134
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530135# Memory units are in MBs
Greg Spencer798d75f2011-02-01 22:04:49 -0800136TEMP_IMG="$(dirname "${SRC_IMAGE}")/vm_temp_image.bin"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530137
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530138# Split apart the partitions and make some new ones
Gabe Black067b3542014-09-23 01:15:14 -0700139SRC_DEV=$(loopback_partscan "${SRC_IMAGE}")
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530140
141# Fix the kernel command line
Gabe Black067b3542014-09-23 01:15:14 -0700142SRC_STATE="${SRC_DEV}"p1
143SRC_ROOTFS="${SRC_DEV}"p3
144SRC_KERN="${SRC_DEV}"p4
145SRC_OEM="${SRC_DEV}"p8
146SRC_ESP="${SRC_DEV}"p12
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530147if [ -n "${FLAGS_state_image}" ]; then
148 TEMP_STATE="${FLAGS_state_image}"
Rahul Chaturvedie770e162010-07-15 00:35:11 +0530149else
Liam McLoughlinb78a7c32012-11-06 20:19:05 -0500150 STATEFUL_SIZE_BYTES=$(get_filesystem_size "${FLAGS_disk_layout}" 1)
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700151 STATEFUL_SIZE_MEGABYTES=$(( STATEFUL_SIZE_BYTES / 1024 / 1024 ))
Gabe Black067b3542014-09-23 01:15:14 -0700152 original_image_size=$(bd_safe_size "${SRC_STATE}")
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700153 if [ "${original_image_size}" -gt "${STATEFUL_SIZE_BYTES}" ]; then
154 die "Cannot resize stateful image to smaller than original. Exiting."
Rahul Chaturvedie770e162010-07-15 00:35:11 +0530155 fi
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700156
157 echo "Resizing stateful partition to ${STATEFUL_SIZE_MEGABYTES}MB"
158 # Extend the original file size to the new size.
Gabe Black067b3542014-09-23 01:15:14 -0700159 TEMP_STATE="${TEMP_DIR}"/stateful
160 # Create TEMP_STATE as a regular user so a regular user can delete it.
Gabe Black99841f72015-06-24 17:00:12 -0700161 sudo dd if="${SRC_STATE}" bs=16M status=none > "${TEMP_STATE}"
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700162 sudo e2fsck -pf "${TEMP_STATE}"
163 sudo resize2fs "${TEMP_STATE}" ${STATEFUL_SIZE_MEGABYTES}M
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530164fi
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530165TEMP_PMBR="${TEMP_DIR}"/pmbr
166dd if="${SRC_IMAGE}" of="${TEMP_PMBR}" bs=512 count=1
167
Gabe Black067b3542014-09-23 01:15:14 -0700168# Set up a new partition table.
169PARTITION_SCRIPT_PATH=$(mktemp)
170write_partition_script "${FLAGS_disk_layout}" "${PARTITION_SCRIPT_PATH}"
171. "${PARTITION_SCRIPT_PATH}"
172write_partition_table "${TEMP_IMG}" "${TEMP_PMBR}"
173rm "${PARTITION_SCRIPT_PATH}"
174
175DST_DEV=$(loopback_partscan "${TEMP_IMG}")
176DST_STATE="${DST_DEV}"p1
177DST_ROOTFS="${DST_DEV}"p3
178DST_KERN="${DST_DEV}"p4
179DST_OEM="${DST_DEV}"p8
180DST_ESP="${DST_DEV}"p12
181
182# Copy into the partition parts of the file.
Gabe Black1dd95222014-10-04 02:59:14 -0700183sudo cp "${SRC_ROOTFS}" "${DST_ROOTFS}"
184sudo cp "${TEMP_STATE}" "${DST_STATE}"
185sudo cp "${SRC_ESP}" "${DST_ESP}"
186sudo cp "${SRC_OEM}" "${DST_OEM}"
Gabe Black067b3542014-09-23 01:15:14 -0700187
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530188TEMP_MNT=$(mktemp -d)
Will Drewryefce6682010-07-23 19:43:27 -0500189TEMP_ESP_MNT=$(mktemp -d)
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530190mkdir -p "${TEMP_MNT}"
Gabe Black067b3542014-09-23 01:15:14 -0700191enable_rw_mount "${DST_ROOTFS}"
192sudo mount "${DST_ROOTFS}" "${TEMP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500193mkdir -p "${TEMP_ESP_MNT}"
Gabe Black067b3542014-09-23 01:15:14 -0700194sudo mount "${DST_ESP}" "${TEMP_ESP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500195
Will Drewry537caa92010-08-20 20:30:56 -0500196# Unmount everything prior to building a final image
Mike Frysinger9ebc8ce2015-04-06 13:15:01 -0400197trap 'die_err_trap' INT TERM EXIT
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530198cleanup
199
Gabe Blackb9827592014-09-03 21:58:11 -0700200# Make the built-image bootable.
Will Drewry537caa92010-08-20 20:30:56 -0500201# 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 Alvarez2bf88592011-07-07 15:47:31 -0700205 --force_developer_mode
Will Drewry537caa92010-08-20 20:30:56 -0500206
Gabe Black34bbc102014-09-10 16:35:56 -0700207IMAGE_DEV=""
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700208detach_loopback() {
Gabe Black34bbc102014-09-10 16:35:56 -0700209 if [ -n "${IMAGE_DEV}" ]; then
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700210 loopback_detach "${IMAGE_DEV}"
Gabe Black34bbc102014-09-10 16:35:56 -0700211 fi
212}
Mike Frysinger9ebc8ce2015-04-06 13:15:01 -0400213trap 'ret=$?; detach_loopback; die_err_trap ${ret}' INT TERM EXIT
Gabe Black34bbc102014-09-10 16:35:56 -0700214
Gabe Blackb9827592014-09-03 21:58:11 -0700215# 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 Blackfbdb1d52014-09-22 23:43:35 -0700217IMAGE_DEV=$(loopback_partscan "${TEMP_IMG}")
Gabe Blackb9827592014-09-03 21:58:11 -0700218sudo cp ${IMAGE_DEV}p4 ${IMAGE_DEV}p2
219
Mike Frysinger9ebc8ce2015-04-06 13:15:01 -0400220trap 'die_err_trap' INT TERM EXIT
221switch_to_strict_mode
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700222loopback_detach "${IMAGE_DEV}"
Gabe Blackb9827592014-09-03 21:58:11 -0700223
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530224echo Creating final image
Gaurav Shah550edca2014-09-25 11:13:55 -0700225mv "${TEMP_IMG}" "${FLAGS_to}/${DEFAULT_QEMU_IMAGE}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530226
Gabe Black067b3542014-09-23 01:15:14 -0700227rm -rf "${TEMP_IMG}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530228
229echo "Created image at ${FLAGS_to}"
230
Gaurav Shah550edca2014-09-25 11:13:55 -0700231echo "If you have qemu-kvm installed, you can start the image by:"
Jorge Lucangeli Obes161d47c2016-01-06 10:38:52 -0800232echo "sudo kvm -m ${DEFAULT_MEM} -vga cirrus -pidfile /tmp/kvm.pid" \
Nicolas Norvezd3b04f92016-07-12 17:13:35 +0100233 "-net nic,model=virtio -net user,hostfwd=tcp:127.0.0.1:9222-:22 \\"
Gaurav Shah550edca2014-09-25 11:13:55 -0700234echo "-hda ${FLAGS_to}/${DEFAULT_QEMU_IMAGE}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530235