blob: a8b190efe5727818bd5aa477af8a0d20093e5cc9 [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/disk_layout_util.sh" || exit 1
13. "${SCRIPT_ROOT}/build_library/build_common.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
David James359d3e12012-07-10 13:09:48 -070020. "${SCRIPT_ROOT}/lib/cros_vm_constants.sh" || exit 1
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053021
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053022# Flags
Liam McLoughlin12a9a842012-10-10 10:37:06 -040023DEFINE_string adjust_part "" \
24 "Adjustments to apply to the partition table"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053025DEFINE_string board "${DEFAULT_BOARD}" \
26 "Board for which the image was built"
27DEFINE_boolean factory $FLAGS_FALSE \
28 "Modify the image for manufacturing testing"
29DEFINE_boolean factory_install $FLAGS_FALSE \
30 "Modify the image for factory install shim"
Simon Glass142ca062011-02-09 13:39:43 -080031
Gabe Black1d6dc252014-08-18 17:02:09 -070032# We default to TRUE so the buildbot gets its image.
Chris Sosa8dad50d2011-02-14 15:29:32 -080033DEFINE_boolean force_copy ${FLAGS_FALSE} "Always rebuild test image"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053034DEFINE_string from "" \
35 "Directory containing rootfs.image and mbr.image"
Don Garrett7937ef82014-08-25 18:04:05 -070036DEFINE_string disk_layout "2gb-rootfs-updatable" \
Liam McLoughlinb78a7c32012-11-06 20:19:05 -050037 "The disk layout type to use for this image."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053038DEFINE_string state_image "" \
39 "Stateful partition image (defaults to creating new statful partition)"
40DEFINE_boolean test_image "${FLAGS_FALSE}" \
Simon Glass142ca062011-02-09 13:39:43 -080041 "Copies normal image to ${CHROMEOS_TEST_IMAGE_NAME}, modifies it for test."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053042DEFINE_string to "" \
43 "Destination folder for VM output file(s)"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053044
45# Parse command line
46FLAGS "$@" || exit 1
47eval set -- "${FLAGS_ARGV}"
48
49# Die on any errors.
Brian Harring7f175a52012-03-02 05:37:00 -080050switch_to_strict_mode
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053051
52if [ -z "${FLAGS_board}" ] ; then
Brian Harring7f175a52012-03-02 05:37:00 -080053 die_notrace "--board is required."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053054fi
55
Gabe Black067b3542014-09-23 01:15:14 -070056TEMP_DIR=$(mktemp -d)
57TEMP_MNT=""
58TEMP_ESP_MNT=""
59SRC_DEV=""
60DST_DEV=""
61cleanup() {
62 if [[ -n "${TEMP_MNT}" ]]; then
63 safe_umount "${TEMP_MNT}" || true
64 rmdir "${TEMP_MNT}" || true
65 fi
66 if [[ -n "${TEMP_ESP_MNT}" ]]; then
67 safe_umount "${TEMP_ESP_MNT}" || true
68 rmdir "${TEMP_ESP_MNT}" || true
69 fi
70
71 if [[ -n "${SRC_DEV}" ]]; then
72 loopback_detach "${SRC_DEV}" || true
73 fi
74 if [[ -n "${DST_DEV}" ]]; then
75 loopback_detach "${DST_DEV}" || true
76 fi
77 rm -rf "${TEMP_DIR}"
78}
Mike Frysinger9ebc8ce2015-04-06 13:15:01 -040079trap 'ret=$?; cleanup; die_err_trap ${ret}' INT TERM EXIT
Gabe Black067b3542014-09-23 01:15:14 -070080
Liam McLoughlin5b37c542012-08-16 11:09:37 -070081BOARD="$FLAGS_board"
Chris Sosacc09f842010-09-21 17:09:51 -070082
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053083IMAGES_DIR="${DEFAULT_BUILD_ROOT}/images/${FLAGS_board}"
84# Default to the most recent image
85if [ -z "${FLAGS_from}" ] ; then
Kees Cook84a4c7a2011-10-18 13:21:41 -070086 FLAGS_from="$(${SCRIPT_ROOT}/get_latest_image.sh --board=${FLAGS_board})"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053087fi
88if [ -z "${FLAGS_to}" ] ; then
89 FLAGS_to="${FLAGS_from}"
90fi
91
Mike Frysingerb86854a2014-11-25 18:43:58 -050092# Convert args to full paths. Use echo here on the unquoted value to process all
93# shell level expansions like ~ and *.
94if ! resolved=$(readlink -f "$(echo ${FLAGS_from})"); then
95 die_notrace "image_to_vm: processing --from failed." \
96 "Verify the path exists: ${FLAGS_from}" \
97 " cwd: ${PWD}"
98fi
99FLAGS_from=${resolved}
100if ! resolved=$(readlink -f "$(echo ${FLAGS_to})"); then
101 die_notrace "image_to_vm: Processing --to failed." \
102 "Verify the path exists: ${FLAGS_to}" \
103 " cwd: ${PWD}"
104fi
105FLAGS_to=${resolved}
106
David James7efb76c2013-01-09 15:25:58 -0800107if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ]; then
108 SRC_IMAGE="${FLAGS_from}/${CHROMEOS_FACTORY_TEST_IMAGE_NAME}"
109elif [ ${FLAGS_test_image} -eq ${FLAGS_TRUE} ]; then
110 SRC_IMAGE="${FLAGS_from}/${CHROMEOS_TEST_IMAGE_NAME}"
Simon Glass142ca062011-02-09 13:39:43 -0800111else
112 # Use the standard image
113 SRC_IMAGE="${FLAGS_from}/${CHROMEOS_IMAGE_NAME}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530114fi
Mike Frysingerb86854a2014-11-25 18:43:58 -0500115if [[ ! -e ${SRC_IMAGE} ]]; then
116 die_notrace "image_to_vm: src image does not exist: ${SRC_IMAGE}" \
117 "Please verify you have selected the right input." \
118 "Note: only dev/test/factory images can be used as inputs."
119fi
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530120
121# Memory units are in MBs
Greg Spencer798d75f2011-02-01 22:04:49 -0800122TEMP_IMG="$(dirname "${SRC_IMAGE}")/vm_temp_image.bin"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530123
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530124# Split apart the partitions and make some new ones
Gabe Black067b3542014-09-23 01:15:14 -0700125SRC_DEV=$(loopback_partscan "${SRC_IMAGE}")
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530126
127# Fix the kernel command line
Gabe Black067b3542014-09-23 01:15:14 -0700128SRC_STATE="${SRC_DEV}"p1
129SRC_ROOTFS="${SRC_DEV}"p3
130SRC_KERN="${SRC_DEV}"p4
131SRC_OEM="${SRC_DEV}"p8
132SRC_ESP="${SRC_DEV}"p12
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530133if [ -n "${FLAGS_state_image}" ]; then
134 TEMP_STATE="${FLAGS_state_image}"
Rahul Chaturvedie770e162010-07-15 00:35:11 +0530135else
Liam McLoughlinb78a7c32012-11-06 20:19:05 -0500136 STATEFUL_SIZE_BYTES=$(get_filesystem_size "${FLAGS_disk_layout}" 1)
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700137 STATEFUL_SIZE_MEGABYTES=$(( STATEFUL_SIZE_BYTES / 1024 / 1024 ))
Gabe Black067b3542014-09-23 01:15:14 -0700138 original_image_size=$(bd_safe_size "${SRC_STATE}")
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700139 if [ "${original_image_size}" -gt "${STATEFUL_SIZE_BYTES}" ]; then
140 die "Cannot resize stateful image to smaller than original. Exiting."
Rahul Chaturvedie770e162010-07-15 00:35:11 +0530141 fi
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700142
143 echo "Resizing stateful partition to ${STATEFUL_SIZE_MEGABYTES}MB"
144 # Extend the original file size to the new size.
Gabe Black067b3542014-09-23 01:15:14 -0700145 TEMP_STATE="${TEMP_DIR}"/stateful
146 # Create TEMP_STATE as a regular user so a regular user can delete it.
Gabe Black8a1141a2014-10-04 02:36:49 -0700147 sudo chmod a+r "${SRC_STATE}"
Gabe Black1dd95222014-10-04 02:59:14 -0700148 cp "${SRC_STATE}" "${TEMP_STATE}"
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700149 sudo e2fsck -pf "${TEMP_STATE}"
150 sudo resize2fs "${TEMP_STATE}" ${STATEFUL_SIZE_MEGABYTES}M
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530151fi
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530152TEMP_PMBR="${TEMP_DIR}"/pmbr
153dd if="${SRC_IMAGE}" of="${TEMP_PMBR}" bs=512 count=1
154
Gabe Black067b3542014-09-23 01:15:14 -0700155# Set up a new partition table.
156PARTITION_SCRIPT_PATH=$(mktemp)
157write_partition_script "${FLAGS_disk_layout}" "${PARTITION_SCRIPT_PATH}"
158. "${PARTITION_SCRIPT_PATH}"
159write_partition_table "${TEMP_IMG}" "${TEMP_PMBR}"
160rm "${PARTITION_SCRIPT_PATH}"
161
162DST_DEV=$(loopback_partscan "${TEMP_IMG}")
163DST_STATE="${DST_DEV}"p1
164DST_ROOTFS="${DST_DEV}"p3
165DST_KERN="${DST_DEV}"p4
166DST_OEM="${DST_DEV}"p8
167DST_ESP="${DST_DEV}"p12
168
169# Copy into the partition parts of the file.
Gabe Black1dd95222014-10-04 02:59:14 -0700170sudo cp "${SRC_ROOTFS}" "${DST_ROOTFS}"
171sudo cp "${TEMP_STATE}" "${DST_STATE}"
172sudo cp "${SRC_ESP}" "${DST_ESP}"
173sudo cp "${SRC_OEM}" "${DST_OEM}"
Gabe Black067b3542014-09-23 01:15:14 -0700174
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530175TEMP_MNT=$(mktemp -d)
Will Drewryefce6682010-07-23 19:43:27 -0500176TEMP_ESP_MNT=$(mktemp -d)
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530177mkdir -p "${TEMP_MNT}"
Gabe Black067b3542014-09-23 01:15:14 -0700178enable_rw_mount "${DST_ROOTFS}"
179sudo mount "${DST_ROOTFS}" "${TEMP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500180mkdir -p "${TEMP_ESP_MNT}"
Gabe Black067b3542014-09-23 01:15:14 -0700181sudo mount "${DST_ESP}" "${TEMP_ESP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500182
Will Drewry537caa92010-08-20 20:30:56 -0500183# Unmount everything prior to building a final image
Mike Frysinger9ebc8ce2015-04-06 13:15:01 -0400184trap 'die_err_trap' INT TERM EXIT
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530185cleanup
186
Gabe Blackb9827592014-09-03 21:58:11 -0700187# Make the built-image bootable.
Will Drewry537caa92010-08-20 20:30:56 -0500188# NOTE: The TEMP_IMG must live in the same image dir as the original image
189# to operate automatically below.
190${SCRIPTS_DIR}/bin/cros_make_image_bootable $(dirname "${TEMP_IMG}") \
191 $(basename "${TEMP_IMG}") \
Arkaitz Ruiz Alvarez2bf88592011-07-07 15:47:31 -0700192 --force_developer_mode
Will Drewry537caa92010-08-20 20:30:56 -0500193
Gabe Black34bbc102014-09-10 16:35:56 -0700194IMAGE_DEV=""
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700195detach_loopback() {
Gabe Black34bbc102014-09-10 16:35:56 -0700196 if [ -n "${IMAGE_DEV}" ]; then
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700197 loopback_detach "${IMAGE_DEV}"
Gabe Black34bbc102014-09-10 16:35:56 -0700198 fi
199}
Mike Frysinger9ebc8ce2015-04-06 13:15:01 -0400200trap 'ret=$?; detach_loopback; die_err_trap ${ret}' INT TERM EXIT
Gabe Black34bbc102014-09-10 16:35:56 -0700201
Gabe Blackb9827592014-09-03 21:58:11 -0700202# cros_make_image_bootable made the kernel in slot A recovery signed. We want
203# it to be normally signed like the one in slot B, so copy B into A.
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700204IMAGE_DEV=$(loopback_partscan "${TEMP_IMG}")
Gabe Blackb9827592014-09-03 21:58:11 -0700205sudo cp ${IMAGE_DEV}p4 ${IMAGE_DEV}p2
206
Mike Frysinger9ebc8ce2015-04-06 13:15:01 -0400207trap 'die_err_trap' INT TERM EXIT
208switch_to_strict_mode
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700209loopback_detach "${IMAGE_DEV}"
Gabe Blackb9827592014-09-03 21:58:11 -0700210
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530211echo Creating final image
Gaurav Shah550edca2014-09-25 11:13:55 -0700212mv "${TEMP_IMG}" "${FLAGS_to}/${DEFAULT_QEMU_IMAGE}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530213
Gabe Black067b3542014-09-23 01:15:14 -0700214rm -rf "${TEMP_IMG}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530215
216echo "Created image at ${FLAGS_to}"
217
Gaurav Shah550edca2014-09-25 11:13:55 -0700218echo "If you have qemu-kvm installed, you can start the image by:"
219echo "sudo kvm -m ${FLAGS_mem} -vga cirrus -pidfile /tmp/kvm.pid" \
220 "-net nic,model=virtio -net user,hostfwd=tcp::9222-:22 \\"
221echo "-hda ${FLAGS_to}/${DEFAULT_QEMU_IMAGE}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530222