blob: 536a688c0ec1845fea6e8c7ca25787efc061d963 [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
7# Script to convert the output of build_image.sh to a VMware image and write a
8# corresponding VMware config file.
9
Kees Cook84a4c7a2011-10-18 13:21:41 -070010# Helper scripts should be run from the same location as this script.
11SCRIPT_ROOT=$(dirname "$(readlink -f "$0")")
David James359d3e12012-07-10 13:09:48 -070012. "${SCRIPT_ROOT}/common.sh" || exit 1
Liam McLoughlin5b37c542012-08-16 11:09:37 -070013. "${SCRIPT_ROOT}/build_library/disk_layout_util.sh" || exit 1
14. "${SCRIPT_ROOT}/build_library/build_common.sh" || exit 1
Greg Spencer798d75f2011-02-01 22:04:49 -080015
16# Need to be inside the chroot to load chromeos-common.sh
17assert_inside_chroot
18
19# Load functions and constants for chromeos-install
Gwendal Grignou0f618492014-04-07 20:05:58 +000020. /usr/share/misc/chromeos-common.sh || exit 1
David James359d3e12012-07-10 13:09:48 -070021. "${SCRIPT_ROOT}/lib/cros_vm_constants.sh" || exit 1
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 format "qemu" \
Gaurav Shahd6618fb2014-09-12 16:58:32 -070036 "Output format, either qemu or vmware"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053037DEFINE_string from "" \
38 "Directory containing rootfs.image and mbr.image"
Don Garrett7937ef82014-08-25 18:04:05 -070039DEFINE_string disk_layout "2gb-rootfs-updatable" \
Liam McLoughlinb78a7c32012-11-06 20:19:05 -050040 "The disk layout type to use for this image."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053041DEFINE_boolean make_vmx ${FLAGS_TRUE} \
42 "Create a vmx file for use with vmplayer (vmware only)."
43DEFINE_integer mem "${DEFAULT_MEM}" \
44 "Memory size for the vm config in MBs (vmware only)."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053045DEFINE_string state_image "" \
46 "Stateful partition image (defaults to creating new statful partition)"
47DEFINE_boolean test_image "${FLAGS_FALSE}" \
Simon Glass142ca062011-02-09 13:39:43 -080048 "Copies normal image to ${CHROMEOS_TEST_IMAGE_NAME}, modifies it for test."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053049DEFINE_string to "" \
50 "Destination folder for VM output file(s)"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053051DEFINE_string vmdk "${DEFAULT_VMDK}" \
52 "Filename for the vmware disk image (vmware only)."
53DEFINE_string vmx "${DEFAULT_VMX}" \
54 "Filename for the vmware config (vmware only)."
55
56# Parse command line
57FLAGS "$@" || exit 1
58eval set -- "${FLAGS_ARGV}"
59
60# Die on any errors.
Brian Harring7f175a52012-03-02 05:37:00 -080061switch_to_strict_mode
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053062
63if [ -z "${FLAGS_board}" ] ; then
Brian Harring7f175a52012-03-02 05:37:00 -080064 die_notrace "--board is required."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053065fi
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}
90trap cleanup INT TERM EXIT
91
Liam McLoughlin5b37c542012-08-16 11:09:37 -070092BOARD="$FLAGS_board"
Chris Sosacc09f842010-09-21 17:09:51 -070093
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053094IMAGES_DIR="${DEFAULT_BUILD_ROOT}/images/${FLAGS_board}"
95# Default to the most recent image
96if [ -z "${FLAGS_from}" ] ; then
Kees Cook84a4c7a2011-10-18 13:21:41 -070097 FLAGS_from="$(${SCRIPT_ROOT}/get_latest_image.sh --board=${FLAGS_board})"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053098fi
99if [ -z "${FLAGS_to}" ] ; then
100 FLAGS_to="${FLAGS_from}"
101fi
102
Mike Frysingerb86854a2014-11-25 18:43:58 -0500103# Convert args to full paths. Use echo here on the unquoted value to process all
104# shell level expansions like ~ and *.
105if ! resolved=$(readlink -f "$(echo ${FLAGS_from})"); then
106 die_notrace "image_to_vm: processing --from failed." \
107 "Verify the path exists: ${FLAGS_from}" \
108 " cwd: ${PWD}"
109fi
110FLAGS_from=${resolved}
111if ! resolved=$(readlink -f "$(echo ${FLAGS_to})"); then
112 die_notrace "image_to_vm: Processing --to failed." \
113 "Verify the path exists: ${FLAGS_to}" \
114 " cwd: ${PWD}"
115fi
116FLAGS_to=${resolved}
117
David James7efb76c2013-01-09 15:25:58 -0800118if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ]; then
119 SRC_IMAGE="${FLAGS_from}/${CHROMEOS_FACTORY_TEST_IMAGE_NAME}"
120elif [ ${FLAGS_test_image} -eq ${FLAGS_TRUE} ]; then
121 SRC_IMAGE="${FLAGS_from}/${CHROMEOS_TEST_IMAGE_NAME}"
Simon Glass142ca062011-02-09 13:39:43 -0800122else
123 # Use the standard image
124 SRC_IMAGE="${FLAGS_from}/${CHROMEOS_IMAGE_NAME}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530125fi
Mike Frysingerb86854a2014-11-25 18:43:58 -0500126if [[ ! -e ${SRC_IMAGE} ]]; then
127 die_notrace "image_to_vm: src image does not exist: ${SRC_IMAGE}" \
128 "Please verify you have selected the right input." \
129 "Note: only dev/test/factory images can be used as inputs."
130fi
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530131
132# Memory units are in MBs
Greg Spencer798d75f2011-02-01 22:04:49 -0800133TEMP_IMG="$(dirname "${SRC_IMAGE}")/vm_temp_image.bin"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530134
135# If we're not building for VMWare, don't build the vmx
136if [ "${FLAGS_format}" != "vmware" ]; then
137 FLAGS_make_vmx="${FLAGS_FALSE}"
138fi
139
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530140# Split apart the partitions and make some new ones
Gabe Black067b3542014-09-23 01:15:14 -0700141SRC_DEV=$(loopback_partscan "${SRC_IMAGE}")
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530142
143# Fix the kernel command line
Gabe Black067b3542014-09-23 01:15:14 -0700144SRC_STATE="${SRC_DEV}"p1
145SRC_ROOTFS="${SRC_DEV}"p3
146SRC_KERN="${SRC_DEV}"p4
147SRC_OEM="${SRC_DEV}"p8
148SRC_ESP="${SRC_DEV}"p12
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530149if [ -n "${FLAGS_state_image}" ]; then
150 TEMP_STATE="${FLAGS_state_image}"
Rahul Chaturvedie770e162010-07-15 00:35:11 +0530151else
Liam McLoughlinb78a7c32012-11-06 20:19:05 -0500152 STATEFUL_SIZE_BYTES=$(get_filesystem_size "${FLAGS_disk_layout}" 1)
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700153 STATEFUL_SIZE_MEGABYTES=$(( STATEFUL_SIZE_BYTES / 1024 / 1024 ))
Gabe Black067b3542014-09-23 01:15:14 -0700154 original_image_size=$(bd_safe_size "${SRC_STATE}")
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700155 if [ "${original_image_size}" -gt "${STATEFUL_SIZE_BYTES}" ]; then
156 die "Cannot resize stateful image to smaller than original. Exiting."
Rahul Chaturvedie770e162010-07-15 00:35:11 +0530157 fi
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700158
159 echo "Resizing stateful partition to ${STATEFUL_SIZE_MEGABYTES}MB"
160 # Extend the original file size to the new size.
Gabe Black067b3542014-09-23 01:15:14 -0700161 TEMP_STATE="${TEMP_DIR}"/stateful
162 # Create TEMP_STATE as a regular user so a regular user can delete it.
Gabe Black8a1141a2014-10-04 02:36:49 -0700163 sudo chmod a+r "${SRC_STATE}"
Gabe Black1dd95222014-10-04 02:59:14 -0700164 cp "${SRC_STATE}" "${TEMP_STATE}"
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700165 sudo e2fsck -pf "${TEMP_STATE}"
166 sudo resize2fs "${TEMP_STATE}" ${STATEFUL_SIZE_MEGABYTES}M
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530167fi
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530168TEMP_PMBR="${TEMP_DIR}"/pmbr
169dd if="${SRC_IMAGE}" of="${TEMP_PMBR}" bs=512 count=1
170
Gabe Black067b3542014-09-23 01:15:14 -0700171# Set up a new partition table.
172PARTITION_SCRIPT_PATH=$(mktemp)
173write_partition_script "${FLAGS_disk_layout}" "${PARTITION_SCRIPT_PATH}"
174. "${PARTITION_SCRIPT_PATH}"
175write_partition_table "${TEMP_IMG}" "${TEMP_PMBR}"
176rm "${PARTITION_SCRIPT_PATH}"
177
178DST_DEV=$(loopback_partscan "${TEMP_IMG}")
179DST_STATE="${DST_DEV}"p1
180DST_ROOTFS="${DST_DEV}"p3
181DST_KERN="${DST_DEV}"p4
182DST_OEM="${DST_DEV}"p8
183DST_ESP="${DST_DEV}"p12
184
185# Copy into the partition parts of the file.
Gabe Black1dd95222014-10-04 02:59:14 -0700186sudo cp "${SRC_ROOTFS}" "${DST_ROOTFS}"
187sudo cp "${TEMP_STATE}" "${DST_STATE}"
188sudo cp "${SRC_ESP}" "${DST_ESP}"
189sudo cp "${SRC_OEM}" "${DST_OEM}"
Gabe Black067b3542014-09-23 01:15:14 -0700190
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530191TEMP_MNT=$(mktemp -d)
Will Drewryefce6682010-07-23 19:43:27 -0500192TEMP_ESP_MNT=$(mktemp -d)
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530193mkdir -p "${TEMP_MNT}"
Gabe Black067b3542014-09-23 01:15:14 -0700194enable_rw_mount "${DST_ROOTFS}"
195sudo mount "${DST_ROOTFS}" "${TEMP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500196mkdir -p "${TEMP_ESP_MNT}"
Gabe Black067b3542014-09-23 01:15:14 -0700197sudo mount "${DST_ESP}" "${TEMP_ESP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500198
Will Drewry537caa92010-08-20 20:30:56 -0500199# Unmount everything prior to building a final image
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530200trap - INT TERM EXIT
201cleanup
202
Gabe Blackb9827592014-09-03 21:58:11 -0700203# Make the built-image bootable.
Will Drewry537caa92010-08-20 20:30:56 -0500204# NOTE: The TEMP_IMG must live in the same image dir as the original image
205# to operate automatically below.
206${SCRIPTS_DIR}/bin/cros_make_image_bootable $(dirname "${TEMP_IMG}") \
207 $(basename "${TEMP_IMG}") \
Arkaitz Ruiz Alvarez2bf88592011-07-07 15:47:31 -0700208 --force_developer_mode
Will Drewry537caa92010-08-20 20:30:56 -0500209
Gabe Black34bbc102014-09-10 16:35:56 -0700210IMAGE_DEV=""
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700211detach_loopback() {
Gabe Black34bbc102014-09-10 16:35:56 -0700212 if [ -n "${IMAGE_DEV}" ]; then
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700213 loopback_detach "${IMAGE_DEV}"
Gabe Black34bbc102014-09-10 16:35:56 -0700214 fi
215}
216trap detach_loopback INT TERM EXIT
217
Gabe Blackb9827592014-09-03 21:58:11 -0700218# cros_make_image_bootable made the kernel in slot A recovery signed. We want
219# it to be normally signed like the one in slot B, so copy B into A.
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700220IMAGE_DEV=$(loopback_partscan "${TEMP_IMG}")
Gabe Blackb9827592014-09-03 21:58:11 -0700221sudo cp ${IMAGE_DEV}p4 ${IMAGE_DEV}p2
222
Gabe Black34bbc102014-09-10 16:35:56 -0700223trap - INT TERM EXIT
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700224loopback_detach "${IMAGE_DEV}"
Gabe Blackb9827592014-09-03 21:58:11 -0700225
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530226echo Creating final image
227# Convert image to output format
Gaurav Shahd6618fb2014-09-12 16:58:32 -0700228if [ "${FLAGS_format}" = "qemu" ]; then
229 mv "${TEMP_IMG}" "${FLAGS_to}/${DEFAULT_QEMU_IMAGE}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530230elif [ "${FLAGS_format}" = "vmware" ]; then
231 qemu-img convert -f raw "${TEMP_IMG}" \
232 -O vmdk "${FLAGS_to}/${FLAGS_vmdk}"
233else
Brian Harring7f175a52012-03-02 05:37:00 -0800234 die_notrace "Invalid format: ${FLAGS_format}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530235fi
236
Gabe Black067b3542014-09-23 01:15:14 -0700237rm -rf "${TEMP_IMG}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530238
239echo "Created image at ${FLAGS_to}"
240
241# Generate the vmware config file
242# A good reference doc: http://www.sanbarrow.com/vmx.html
243VMX_CONFIG="#!/usr/bin/vmware
244.encoding = \"UTF-8\"
245config.version = \"8\"
246virtualHW.version = \"4\"
247memsize = \"${FLAGS_mem}\"
248ide0:0.present = \"TRUE\"
249ide0:0.fileName = \"${FLAGS_vmdk}\"
250ethernet0.present = \"TRUE\"
251usb.present = \"TRUE\"
252sound.present = \"TRUE\"
253sound.virtualDev = \"es1371\"
254displayName = \"Chromium OS\"
255guestOS = \"otherlinux\"
256ethernet0.addressType = \"generated\"
257floppy0.present = \"FALSE\""
258
259if [[ "${FLAGS_make_vmx}" = "${FLAGS_TRUE}" ]]; then
260 echo "${VMX_CONFIG}" > "${FLAGS_to}/${FLAGS_vmx}"
261 echo "Wrote the following config to: ${FLAGS_to}/${FLAGS_vmx}"
262 echo "${VMX_CONFIG}"
263fi
264
Olof Johansson3ed8d122010-09-27 13:29:40 -0500265
266if [ "${FLAGS_format}" == "qemu" ]; then
267 echo "If you have qemu-kvm installed, you can start the image by:"
John Sheub7b095b2012-12-18 13:30:26 -0800268 echo "sudo kvm -m ${FLAGS_mem} -vga cirrus -pidfile /tmp/kvm.pid" \
269 "-net nic,model=virtio -net user,hostfwd=tcp::9222-:22 \\"
270 echo "-hda ${FLAGS_to}/${DEFAULT_QEMU_IMAGE}"
Olof Johansson3ed8d122010-09-27 13:29:40 -0500271fi