blob: 0e531715a06deaf1de2a2011a5489a094929cf41 [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})"
Zelidrag Hornung42ca8182010-07-12 18:08:44 -070098else
99 pushd "${FLAGS_from}" && FLAGS_from=`pwd` && popd
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530100fi
101if [ -z "${FLAGS_to}" ] ; then
102 FLAGS_to="${FLAGS_from}"
103fi
104
David James7efb76c2013-01-09 15:25:58 -0800105if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ]; then
106 SRC_IMAGE="${FLAGS_from}/${CHROMEOS_FACTORY_TEST_IMAGE_NAME}"
107elif [ ${FLAGS_test_image} -eq ${FLAGS_TRUE} ]; then
108 SRC_IMAGE="${FLAGS_from}/${CHROMEOS_TEST_IMAGE_NAME}"
Simon Glass142ca062011-02-09 13:39:43 -0800109else
110 # Use the standard image
111 SRC_IMAGE="${FLAGS_from}/${CHROMEOS_IMAGE_NAME}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530112fi
113
114# Memory units are in MBs
Greg Spencer798d75f2011-02-01 22:04:49 -0800115TEMP_IMG="$(dirname "${SRC_IMAGE}")/vm_temp_image.bin"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530116
117# If we're not building for VMWare, don't build the vmx
118if [ "${FLAGS_format}" != "vmware" ]; then
119 FLAGS_make_vmx="${FLAGS_FALSE}"
120fi
121
122# Convert args to paths. Need eval to un-quote the string so that shell
123# chars like ~ are processed; just doing FOO=`readlink -f $FOO` won't work.
124FLAGS_from=`eval readlink -f $FLAGS_from`
125FLAGS_to=`eval readlink -f $FLAGS_to`
126
127# Split apart the partitions and make some new ones
Gabe Black067b3542014-09-23 01:15:14 -0700128SRC_DEV=$(loopback_partscan "${SRC_IMAGE}")
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530129
130# Fix the kernel command line
Gabe Black067b3542014-09-23 01:15:14 -0700131SRC_STATE="${SRC_DEV}"p1
132SRC_ROOTFS="${SRC_DEV}"p3
133SRC_KERN="${SRC_DEV}"p4
134SRC_OEM="${SRC_DEV}"p8
135SRC_ESP="${SRC_DEV}"p12
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530136if [ -n "${FLAGS_state_image}" ]; then
137 TEMP_STATE="${FLAGS_state_image}"
Rahul Chaturvedie770e162010-07-15 00:35:11 +0530138else
Liam McLoughlinb78a7c32012-11-06 20:19:05 -0500139 STATEFUL_SIZE_BYTES=$(get_filesystem_size "${FLAGS_disk_layout}" 1)
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700140 STATEFUL_SIZE_MEGABYTES=$(( STATEFUL_SIZE_BYTES / 1024 / 1024 ))
Gabe Black067b3542014-09-23 01:15:14 -0700141 original_image_size=$(bd_safe_size "${SRC_STATE}")
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700142 if [ "${original_image_size}" -gt "${STATEFUL_SIZE_BYTES}" ]; then
143 die "Cannot resize stateful image to smaller than original. Exiting."
Rahul Chaturvedie770e162010-07-15 00:35:11 +0530144 fi
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700145
146 echo "Resizing stateful partition to ${STATEFUL_SIZE_MEGABYTES}MB"
147 # Extend the original file size to the new size.
Gabe Black067b3542014-09-23 01:15:14 -0700148 TEMP_STATE="${TEMP_DIR}"/stateful
149 # Create TEMP_STATE as a regular user so a regular user can delete it.
Gabe Black8a1141a2014-10-04 02:36:49 -0700150 sudo chmod a+r "${SRC_STATE}"
Gabe Black1dd95222014-10-04 02:59:14 -0700151 cp "${SRC_STATE}" "${TEMP_STATE}"
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700152 sudo e2fsck -pf "${TEMP_STATE}"
153 sudo resize2fs "${TEMP_STATE}" ${STATEFUL_SIZE_MEGABYTES}M
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530154fi
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530155TEMP_PMBR="${TEMP_DIR}"/pmbr
156dd if="${SRC_IMAGE}" of="${TEMP_PMBR}" bs=512 count=1
157
Gabe Black067b3542014-09-23 01:15:14 -0700158# Set up a new partition table.
159PARTITION_SCRIPT_PATH=$(mktemp)
160write_partition_script "${FLAGS_disk_layout}" "${PARTITION_SCRIPT_PATH}"
161. "${PARTITION_SCRIPT_PATH}"
162write_partition_table "${TEMP_IMG}" "${TEMP_PMBR}"
163rm "${PARTITION_SCRIPT_PATH}"
164
165DST_DEV=$(loopback_partscan "${TEMP_IMG}")
166DST_STATE="${DST_DEV}"p1
167DST_ROOTFS="${DST_DEV}"p3
168DST_KERN="${DST_DEV}"p4
169DST_OEM="${DST_DEV}"p8
170DST_ESP="${DST_DEV}"p12
171
172# Copy into the partition parts of the file.
Gabe Black1dd95222014-10-04 02:59:14 -0700173sudo cp "${SRC_ROOTFS}" "${DST_ROOTFS}"
174sudo cp "${TEMP_STATE}" "${DST_STATE}"
175sudo cp "${SRC_ESP}" "${DST_ESP}"
176sudo cp "${SRC_OEM}" "${DST_OEM}"
Gabe Black067b3542014-09-23 01:15:14 -0700177
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530178TEMP_MNT=$(mktemp -d)
Will Drewryefce6682010-07-23 19:43:27 -0500179TEMP_ESP_MNT=$(mktemp -d)
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530180mkdir -p "${TEMP_MNT}"
Gabe Black067b3542014-09-23 01:15:14 -0700181enable_rw_mount "${DST_ROOTFS}"
182sudo mount "${DST_ROOTFS}" "${TEMP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500183mkdir -p "${TEMP_ESP_MNT}"
Gabe Black067b3542014-09-23 01:15:14 -0700184sudo mount "${DST_ESP}" "${TEMP_ESP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500185
Will Drewry537caa92010-08-20 20:30:56 -0500186# Unmount everything prior to building a final image
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530187trap - INT TERM EXIT
188cleanup
189
Gabe Blackb9827592014-09-03 21:58:11 -0700190# Make the built-image bootable.
Will Drewry537caa92010-08-20 20:30:56 -0500191# NOTE: The TEMP_IMG must live in the same image dir as the original image
192# to operate automatically below.
193${SCRIPTS_DIR}/bin/cros_make_image_bootable $(dirname "${TEMP_IMG}") \
194 $(basename "${TEMP_IMG}") \
Arkaitz Ruiz Alvarez2bf88592011-07-07 15:47:31 -0700195 --force_developer_mode
Will Drewry537caa92010-08-20 20:30:56 -0500196
Gabe Black34bbc102014-09-10 16:35:56 -0700197IMAGE_DEV=""
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700198detach_loopback() {
Gabe Black34bbc102014-09-10 16:35:56 -0700199 if [ -n "${IMAGE_DEV}" ]; then
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700200 loopback_detach "${IMAGE_DEV}"
Gabe Black34bbc102014-09-10 16:35:56 -0700201 fi
202}
203trap detach_loopback INT TERM EXIT
204
Gabe Blackb9827592014-09-03 21:58:11 -0700205# cros_make_image_bootable made the kernel in slot A recovery signed. We want
206# it to be normally signed like the one in slot B, so copy B into A.
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700207IMAGE_DEV=$(loopback_partscan "${TEMP_IMG}")
Gabe Blackb9827592014-09-03 21:58:11 -0700208sudo cp ${IMAGE_DEV}p4 ${IMAGE_DEV}p2
209
Gabe Black34bbc102014-09-10 16:35:56 -0700210trap - INT TERM EXIT
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700211loopback_detach "${IMAGE_DEV}"
Gabe Blackb9827592014-09-03 21:58:11 -0700212
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530213echo Creating final image
214# Convert image to output format
Gaurav Shahd6618fb2014-09-12 16:58:32 -0700215if [ "${FLAGS_format}" = "qemu" ]; then
216 mv "${TEMP_IMG}" "${FLAGS_to}/${DEFAULT_QEMU_IMAGE}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530217elif [ "${FLAGS_format}" = "vmware" ]; then
218 qemu-img convert -f raw "${TEMP_IMG}" \
219 -O vmdk "${FLAGS_to}/${FLAGS_vmdk}"
220else
Brian Harring7f175a52012-03-02 05:37:00 -0800221 die_notrace "Invalid format: ${FLAGS_format}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530222fi
223
Gabe Black067b3542014-09-23 01:15:14 -0700224rm -rf "${TEMP_IMG}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530225
226echo "Created image at ${FLAGS_to}"
227
228# Generate the vmware config file
229# A good reference doc: http://www.sanbarrow.com/vmx.html
230VMX_CONFIG="#!/usr/bin/vmware
231.encoding = \"UTF-8\"
232config.version = \"8\"
233virtualHW.version = \"4\"
234memsize = \"${FLAGS_mem}\"
235ide0:0.present = \"TRUE\"
236ide0:0.fileName = \"${FLAGS_vmdk}\"
237ethernet0.present = \"TRUE\"
238usb.present = \"TRUE\"
239sound.present = \"TRUE\"
240sound.virtualDev = \"es1371\"
241displayName = \"Chromium OS\"
242guestOS = \"otherlinux\"
243ethernet0.addressType = \"generated\"
244floppy0.present = \"FALSE\""
245
246if [[ "${FLAGS_make_vmx}" = "${FLAGS_TRUE}" ]]; then
247 echo "${VMX_CONFIG}" > "${FLAGS_to}/${FLAGS_vmx}"
248 echo "Wrote the following config to: ${FLAGS_to}/${FLAGS_vmx}"
249 echo "${VMX_CONFIG}"
250fi
251
Olof Johansson3ed8d122010-09-27 13:29:40 -0500252
253if [ "${FLAGS_format}" == "qemu" ]; then
254 echo "If you have qemu-kvm installed, you can start the image by:"
John Sheub7b095b2012-12-18 13:30:26 -0800255 echo "sudo kvm -m ${FLAGS_mem} -vga cirrus -pidfile /tmp/kvm.pid" \
256 "-net nic,model=virtio -net user,hostfwd=tcp::9222-:22 \\"
257 echo "-hda ${FLAGS_to}/${DEFAULT_QEMU_IMAGE}"
Olof Johansson3ed8d122010-09-27 13:29:40 -0500258fi