blob: 1bb60190e9748dbfafa435bb3036ddf2bc4aec48 [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" \
36 "Output format, either qemu, vmware or virtualbox"
37DEFINE_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)"
51DEFINE_string vbox_disk "${DEFAULT_VBOX_DISK}" \
52 "Filename for the output disk (virtualbox only)."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053053DEFINE_string vmdk "${DEFAULT_VMDK}" \
54 "Filename for the vmware disk image (vmware only)."
55DEFINE_string vmx "${DEFAULT_VMX}" \
56 "Filename for the vmware config (vmware only)."
57
58# Parse command line
59FLAGS "$@" || exit 1
60eval set -- "${FLAGS_ARGV}"
61
62# Die on any errors.
Brian Harring7f175a52012-03-02 05:37:00 -080063switch_to_strict_mode
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053064
65if [ -z "${FLAGS_board}" ] ; then
Brian Harring7f175a52012-03-02 05:37:00 -080066 die_notrace "--board is required."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053067fi
68
Gabe Black067b3542014-09-23 01:15:14 -070069TEMP_DIR=$(mktemp -d)
70TEMP_MNT=""
71TEMP_ESP_MNT=""
72SRC_DEV=""
73DST_DEV=""
74cleanup() {
75 if [[ -n "${TEMP_MNT}" ]]; then
76 safe_umount "${TEMP_MNT}" || true
77 rmdir "${TEMP_MNT}" || true
78 fi
79 if [[ -n "${TEMP_ESP_MNT}" ]]; then
80 safe_umount "${TEMP_ESP_MNT}" || true
81 rmdir "${TEMP_ESP_MNT}" || true
82 fi
83
84 if [[ -n "${SRC_DEV}" ]]; then
85 loopback_detach "${SRC_DEV}" || true
86 fi
87 if [[ -n "${DST_DEV}" ]]; then
88 loopback_detach "${DST_DEV}" || true
89 fi
90 rm -rf "${TEMP_DIR}"
91}
92trap cleanup INT TERM EXIT
93
Liam McLoughlin5b37c542012-08-16 11:09:37 -070094BOARD="$FLAGS_board"
Chris Sosacc09f842010-09-21 17:09:51 -070095
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053096IMAGES_DIR="${DEFAULT_BUILD_ROOT}/images/${FLAGS_board}"
97# Default to the most recent image
98if [ -z "${FLAGS_from}" ] ; then
Kees Cook84a4c7a2011-10-18 13:21:41 -070099 FLAGS_from="$(${SCRIPT_ROOT}/get_latest_image.sh --board=${FLAGS_board})"
Zelidrag Hornung42ca8182010-07-12 18:08:44 -0700100else
101 pushd "${FLAGS_from}" && FLAGS_from=`pwd` && popd
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530102fi
103if [ -z "${FLAGS_to}" ] ; then
104 FLAGS_to="${FLAGS_from}"
105fi
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
115
116# Memory units are in MBs
Greg Spencer798d75f2011-02-01 22:04:49 -0800117TEMP_IMG="$(dirname "${SRC_IMAGE}")/vm_temp_image.bin"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530118
119# If we're not building for VMWare, don't build the vmx
120if [ "${FLAGS_format}" != "vmware" ]; then
121 FLAGS_make_vmx="${FLAGS_FALSE}"
122fi
123
124# Convert args to paths. Need eval to un-quote the string so that shell
125# chars like ~ are processed; just doing FOO=`readlink -f $FOO` won't work.
126FLAGS_from=`eval readlink -f $FLAGS_from`
127FLAGS_to=`eval readlink -f $FLAGS_to`
128
129# Split apart the partitions and make some new ones
Gabe Black067b3542014-09-23 01:15:14 -0700130SRC_DEV=$(loopback_partscan "${SRC_IMAGE}")
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530131
132# Fix the kernel command line
Gabe Black067b3542014-09-23 01:15:14 -0700133SRC_STATE="${SRC_DEV}"p1
134SRC_ROOTFS="${SRC_DEV}"p3
135SRC_KERN="${SRC_DEV}"p4
136SRC_OEM="${SRC_DEV}"p8
137SRC_ESP="${SRC_DEV}"p12
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530138if [ -n "${FLAGS_state_image}" ]; then
139 TEMP_STATE="${FLAGS_state_image}"
Rahul Chaturvedie770e162010-07-15 00:35:11 +0530140else
Liam McLoughlinb78a7c32012-11-06 20:19:05 -0500141 STATEFUL_SIZE_BYTES=$(get_filesystem_size "${FLAGS_disk_layout}" 1)
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700142 STATEFUL_SIZE_MEGABYTES=$(( STATEFUL_SIZE_BYTES / 1024 / 1024 ))
Gabe Black067b3542014-09-23 01:15:14 -0700143 original_image_size=$(bd_safe_size "${SRC_STATE}")
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700144 if [ "${original_image_size}" -gt "${STATEFUL_SIZE_BYTES}" ]; then
145 die "Cannot resize stateful image to smaller than original. Exiting."
Rahul Chaturvedie770e162010-07-15 00:35:11 +0530146 fi
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700147
148 echo "Resizing stateful partition to ${STATEFUL_SIZE_MEGABYTES}MB"
149 # Extend the original file size to the new size.
Gabe Black067b3542014-09-23 01:15:14 -0700150 TEMP_STATE="${TEMP_DIR}"/stateful
151 # Create TEMP_STATE as a regular user so a regular user can delete it.
152 sudo chmod a+r "${SRC_STATE}"
153 dd if="${SRC_STATE}" of="${TEMP_STATE}"
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700154 sudo e2fsck -pf "${TEMP_STATE}"
155 sudo resize2fs "${TEMP_STATE}" ${STATEFUL_SIZE_MEGABYTES}M
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530156fi
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530157TEMP_PMBR="${TEMP_DIR}"/pmbr
158dd if="${SRC_IMAGE}" of="${TEMP_PMBR}" bs=512 count=1
159
Gabe Black067b3542014-09-23 01:15:14 -0700160# Set up a new partition table.
161PARTITION_SCRIPT_PATH=$(mktemp)
162write_partition_script "${FLAGS_disk_layout}" "${PARTITION_SCRIPT_PATH}"
163. "${PARTITION_SCRIPT_PATH}"
164write_partition_table "${TEMP_IMG}" "${TEMP_PMBR}"
165rm "${PARTITION_SCRIPT_PATH}"
166
167DST_DEV=$(loopback_partscan "${TEMP_IMG}")
168DST_STATE="${DST_DEV}"p1
169DST_ROOTFS="${DST_DEV}"p3
170DST_KERN="${DST_DEV}"p4
171DST_OEM="${DST_DEV}"p8
172DST_ESP="${DST_DEV}"p12
173
174# Copy into the partition parts of the file.
175sudo dd if="${SRC_ROOTFS}" of="${DST_ROOTFS}"
176sudo dd if="${TEMP_STATE}" of="${DST_STATE}"
177sudo dd if="${SRC_ESP}" of="${DST_ESP}"
178sudo dd if="${SRC_OEM}" of="${DST_OEM}"
179
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530180TEMP_MNT=$(mktemp -d)
Will Drewryefce6682010-07-23 19:43:27 -0500181TEMP_ESP_MNT=$(mktemp -d)
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530182mkdir -p "${TEMP_MNT}"
Gabe Black067b3542014-09-23 01:15:14 -0700183enable_rw_mount "${DST_ROOTFS}"
184sudo mount "${DST_ROOTFS}" "${TEMP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500185mkdir -p "${TEMP_ESP_MNT}"
Gabe Black067b3542014-09-23 01:15:14 -0700186sudo mount "${DST_ESP}" "${TEMP_ESP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500187
Ben Chand4ec2052014-07-17 18:18:31 -0700188# Modify the unverified usb template, which uses a default usb_disk of sdb3,
189# for targets (e.g. x86 and amd64) that have syslinux installed.
190SYSLINUX_USB_A_CONFIG="${TEMP_MNT}/boot/syslinux/usb.A.cfg"
191if [ -e "${SYSLINUX_USB_A_CONFIG}" ]; then
192 sudo sed -i -e 's/sdb3/sda3/g' "${SYSLINUX_USB_A_CONFIG}"
193fi
Will Drewry537caa92010-08-20 20:30:56 -0500194
Jorge Lucangeli Obes29b972b2013-12-18 15:56:26 -0800195# Add loading of cirrus fb module
Dominik Behrd1a71ca2013-10-01 15:18:38 -0700196if [ "${FLAGS_format}" = "qemu" ]; then
197 sudo_clobber "${TEMP_MNT}/etc/init/cirrusfb.conf" <<END
198start on starting boot-splash
199task
200exec modprobe cirrus
201END
202fi
Jorge Lucangeli Obes29b972b2013-12-18 15:56:26 -0800203
Will Drewry537caa92010-08-20 20:30:56 -0500204# Unmount everything prior to building a final image
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530205trap - INT TERM EXIT
206cleanup
207
Gabe Blackb9827592014-09-03 21:58:11 -0700208# Make the built-image bootable.
Will Drewry537caa92010-08-20 20:30:56 -0500209# NOTE: The TEMP_IMG must live in the same image dir as the original image
210# to operate automatically below.
211${SCRIPTS_DIR}/bin/cros_make_image_bootable $(dirname "${TEMP_IMG}") \
212 $(basename "${TEMP_IMG}") \
Arkaitz Ruiz Alvarez2bf88592011-07-07 15:47:31 -0700213 --force_developer_mode
Will Drewry537caa92010-08-20 20:30:56 -0500214
Gabe Black34bbc102014-09-10 16:35:56 -0700215IMAGE_DEV=""
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700216detach_loopback() {
Gabe Black34bbc102014-09-10 16:35:56 -0700217 if [ -n "${IMAGE_DEV}" ]; then
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700218 loopback_detach "${IMAGE_DEV}"
Gabe Black34bbc102014-09-10 16:35:56 -0700219 fi
220}
221trap detach_loopback INT TERM EXIT
222
Gabe Blackb9827592014-09-03 21:58:11 -0700223# cros_make_image_bootable made the kernel in slot A recovery signed. We want
224# it to be normally signed like the one in slot B, so copy B into A.
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700225IMAGE_DEV=$(loopback_partscan "${TEMP_IMG}")
Gabe Blackb9827592014-09-03 21:58:11 -0700226sudo cp ${IMAGE_DEV}p4 ${IMAGE_DEV}p2
227
Gabe Black34bbc102014-09-10 16:35:56 -0700228trap - INT TERM EXIT
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700229loopback_detach "${IMAGE_DEV}"
Gabe Blackb9827592014-09-03 21:58:11 -0700230
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530231echo Creating final image
232# Convert image to output format
233if [ "${FLAGS_format}" = "virtualbox" -o "${FLAGS_format}" = "qemu" ]; then
234 if [ "${FLAGS_format}" = "virtualbox" ]; then
Liam McLoughlinc9f1dab2011-11-29 18:17:26 +0000235 sudo VBoxManage convertdd "${TEMP_IMG}" "${FLAGS_to}/${FLAGS_vbox_disk}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530236 else
237 mv ${TEMP_IMG} ${FLAGS_to}/${DEFAULT_QEMU_IMAGE}
238 fi
239elif [ "${FLAGS_format}" = "vmware" ]; then
240 qemu-img convert -f raw "${TEMP_IMG}" \
241 -O vmdk "${FLAGS_to}/${FLAGS_vmdk}"
242else
Brian Harring7f175a52012-03-02 05:37:00 -0800243 die_notrace "Invalid format: ${FLAGS_format}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530244fi
245
Gabe Black067b3542014-09-23 01:15:14 -0700246rm -rf "${TEMP_IMG}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530247
248echo "Created image at ${FLAGS_to}"
249
250# Generate the vmware config file
251# A good reference doc: http://www.sanbarrow.com/vmx.html
252VMX_CONFIG="#!/usr/bin/vmware
253.encoding = \"UTF-8\"
254config.version = \"8\"
255virtualHW.version = \"4\"
256memsize = \"${FLAGS_mem}\"
257ide0:0.present = \"TRUE\"
258ide0:0.fileName = \"${FLAGS_vmdk}\"
259ethernet0.present = \"TRUE\"
260usb.present = \"TRUE\"
261sound.present = \"TRUE\"
262sound.virtualDev = \"es1371\"
263displayName = \"Chromium OS\"
264guestOS = \"otherlinux\"
265ethernet0.addressType = \"generated\"
266floppy0.present = \"FALSE\""
267
268if [[ "${FLAGS_make_vmx}" = "${FLAGS_TRUE}" ]]; then
269 echo "${VMX_CONFIG}" > "${FLAGS_to}/${FLAGS_vmx}"
270 echo "Wrote the following config to: ${FLAGS_to}/${FLAGS_vmx}"
271 echo "${VMX_CONFIG}"
272fi
273
Olof Johansson3ed8d122010-09-27 13:29:40 -0500274
275if [ "${FLAGS_format}" == "qemu" ]; then
276 echo "If you have qemu-kvm installed, you can start the image by:"
John Sheub7b095b2012-12-18 13:30:26 -0800277 echo "sudo kvm -m ${FLAGS_mem} -vga cirrus -pidfile /tmp/kvm.pid" \
278 "-net nic,model=virtio -net user,hostfwd=tcp::9222-:22 \\"
279 echo "-hda ${FLAGS_to}/${DEFAULT_QEMU_IMAGE}"
Olof Johansson3ed8d122010-09-27 13:29:40 -0500280fi