blob: 8cd2d4a30ab1172f492bbd712bf2911eea868670 [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
David James359d3e12012-07-10 13:09:48 -070020. /usr/lib/installer/chromeos-common.sh || exit 1
21. "${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
33# We default to TRUE so the buildbot gets its image. Note this is different
34# behavior from image_to_usb.sh
Chris Sosa8dad50d2011-02-14 15:29:32 -080035DEFINE_boolean force_copy ${FLAGS_FALSE} "Always rebuild test image"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053036DEFINE_string format "qemu" \
37 "Output format, either qemu, vmware or virtualbox"
38DEFINE_string from "" \
39 "Directory containing rootfs.image and mbr.image"
40DEFINE_boolean make_vmx ${FLAGS_TRUE} \
41 "Create a vmx file for use with vmplayer (vmware only)."
42DEFINE_integer mem "${DEFAULT_MEM}" \
43 "Memory size for the vm config in MBs (vmware only)."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053044DEFINE_string state_image "" \
45 "Stateful partition image (defaults to creating new statful partition)"
46DEFINE_boolean test_image "${FLAGS_FALSE}" \
Simon Glass142ca062011-02-09 13:39:43 -080047 "Copies normal image to ${CHROMEOS_TEST_IMAGE_NAME}, modifies it for test."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053048DEFINE_string to "" \
49 "Destination folder for VM output file(s)"
50DEFINE_string vbox_disk "${DEFAULT_VBOX_DISK}" \
51 "Filename for the output disk (virtualbox only)."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053052DEFINE_string vmdk "${DEFAULT_VMDK}" \
53 "Filename for the vmware disk image (vmware only)."
54DEFINE_string vmx "${DEFAULT_VMX}" \
55 "Filename for the vmware config (vmware only)."
56
Liam McLoughlin5b37c542012-08-16 11:09:37 -070057# The following arguments are ignored.
58# They are here as part of a transition for CL #29931 beacuse the buildbots
59# specify these arguments.
60DEFINE_integer vdisk_size 3072 \
61 "virtual disk size in MBs."
62DEFINE_boolean full "${FLAGS_FALSE}" "Build full image with all partitions."
63DEFINE_integer rootfs_partition_size 1024 \
64 "rootfs parition size in MBs."
65DEFINE_integer statefulfs_size 2048 \
66 "Stateful partition size in MBs."
67
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053068# Parse command line
69FLAGS "$@" || exit 1
70eval set -- "${FLAGS_ARGV}"
71
72# Die on any errors.
Brian Harring7f175a52012-03-02 05:37:00 -080073switch_to_strict_mode
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053074
75if [ -z "${FLAGS_board}" ] ; then
Brian Harring7f175a52012-03-02 05:37:00 -080076 die_notrace "--board is required."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053077fi
78
Liam McLoughlin5b37c542012-08-16 11:09:37 -070079BOARD="$FLAGS_board"
Chris Sosacc09f842010-09-21 17:09:51 -070080
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053081IMAGES_DIR="${DEFAULT_BUILD_ROOT}/images/${FLAGS_board}"
82# Default to the most recent image
83if [ -z "${FLAGS_from}" ] ; then
Kees Cook84a4c7a2011-10-18 13:21:41 -070084 FLAGS_from="$(${SCRIPT_ROOT}/get_latest_image.sh --board=${FLAGS_board})"
Zelidrag Hornung42ca8182010-07-12 18:08:44 -070085else
86 pushd "${FLAGS_from}" && FLAGS_from=`pwd` && popd
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053087fi
88if [ -z "${FLAGS_to}" ] ; then
89 FLAGS_to="${FLAGS_from}"
90fi
91
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053092if [ ${FLAGS_test_image} -eq ${FLAGS_TRUE} ] ; then
Simon Glass142ca062011-02-09 13:39:43 -080093 # Make a test image - this returns the test filename in CHROMEOS_RETURN_VAL
94 prepare_test_image "${FLAGS_from}" "${CHROMEOS_IMAGE_NAME}"
95 SRC_IMAGE="${CHROMEOS_RETURN_VAL}"
96else
97 # Use the standard image
98 SRC_IMAGE="${FLAGS_from}/${CHROMEOS_IMAGE_NAME}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053099fi
100
101# Memory units are in MBs
Greg Spencer798d75f2011-02-01 22:04:49 -0800102TEMP_IMG="$(dirname "${SRC_IMAGE}")/vm_temp_image.bin"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530103
104# If we're not building for VMWare, don't build the vmx
105if [ "${FLAGS_format}" != "vmware" ]; then
106 FLAGS_make_vmx="${FLAGS_FALSE}"
107fi
108
109# Convert args to paths. Need eval to un-quote the string so that shell
110# chars like ~ are processed; just doing FOO=`readlink -f $FOO` won't work.
111FLAGS_from=`eval readlink -f $FLAGS_from`
112FLAGS_to=`eval readlink -f $FLAGS_to`
113
114# Split apart the partitions and make some new ones
115TEMP_DIR=$(mktemp -d)
Mike Frysinger919bf0c2012-08-22 15:32:39 -0400116pushd "${TEMP_DIR}" >/dev/null
117"${FLAGS_from}/unpack_partitions.sh" "${SRC_IMAGE}"
118popd >/dev/null
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530119
120# Fix the kernel command line
121TEMP_ESP="${TEMP_DIR}"/part_12
Liam McLoughlinaca5e1e2012-09-23 19:31:29 +0000122TEMP_OEM="${TEMP_DIR}"/part_8
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530123TEMP_ROOTFS="${TEMP_DIR}"/part_3
124TEMP_STATE="${TEMP_DIR}"/part_1
Will Drewryefce6682010-07-23 19:43:27 -0500125TEMP_KERN="${TEMP_DIR}"/part_2
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530126if [ -n "${FLAGS_state_image}" ]; then
127 TEMP_STATE="${FLAGS_state_image}"
Rahul Chaturvedie770e162010-07-15 00:35:11 +0530128else
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700129 STATEFUL_SIZE_BYTES=$(get_filesystem_size vm 1)
130 STATEFUL_SIZE_MEGABYTES=$(( STATEFUL_SIZE_BYTES / 1024 / 1024 ))
131 original_image_size=$(stat -c%s "${TEMP_STATE}")
132 if [ "${original_image_size}" -gt "${STATEFUL_SIZE_BYTES}" ]; then
133 die "Cannot resize stateful image to smaller than original. Exiting."
Rahul Chaturvedie770e162010-07-15 00:35:11 +0530134 fi
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700135
136 echo "Resizing stateful partition to ${STATEFUL_SIZE_MEGABYTES}MB"
137 # Extend the original file size to the new size.
138 sudo e2fsck -pf "${TEMP_STATE}"
139 sudo resize2fs "${TEMP_STATE}" ${STATEFUL_SIZE_MEGABYTES}M
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530140fi
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530141TEMP_PMBR="${TEMP_DIR}"/pmbr
142dd if="${SRC_IMAGE}" of="${TEMP_PMBR}" bs=512 count=1
143
144TEMP_MNT=$(mktemp -d)
Will Drewryefce6682010-07-23 19:43:27 -0500145TEMP_ESP_MNT=$(mktemp -d)
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530146cleanup() {
Brian Harringece65e02012-08-30 13:42:21 -0700147 safe_umount "${TEMP_MNT}"
148 safe_umount "${TEMP_ESP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500149 rmdir "${TEMP_MNT}" "${TEMP_ESP_MNT}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530150}
151trap cleanup INT TERM EXIT
152mkdir -p "${TEMP_MNT}"
Will Drewryf929c302010-10-20 18:48:20 -0500153enable_rw_mount "${TEMP_ROOTFS}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530154sudo mount -o loop "${TEMP_ROOTFS}" "${TEMP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500155mkdir -p "${TEMP_ESP_MNT}"
156sudo mount -o loop "${TEMP_ESP}" "${TEMP_ESP_MNT}"
157
Will Drewry537caa92010-08-20 20:30:56 -0500158# Modify the unverified usb template which uses a default usb_disk of sdb3
159sudo sed -i -e 's/sdb3/sda3/g' "${TEMP_MNT}/boot/syslinux/usb.A.cfg"
160
161# Unmount everything prior to building a final image
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530162trap - INT TERM EXIT
163cleanup
164
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700165# Set up a new partition table
166PARTITION_SCRIPT_PATH=$( tempfile )
167write_partition_script "vm" "${PARTITION_SCRIPT_PATH}"
168. "${PARTITION_SCRIPT_PATH}"
169write_partition_table "${TEMP_IMG}" "${TEMP_PMBR}"
170rm "${PARTITION_SCRIPT_PATH}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530171
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530172# Copy into the partition parts of the file
173dd if="${TEMP_ROOTFS}" of="${TEMP_IMG}" conv=notrunc bs=512 \
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700174 seek=$(partoffset ${TEMP_IMG} 3)
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530175dd if="${TEMP_STATE}" of="${TEMP_IMG}" conv=notrunc bs=512 \
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700176 seek=$(partoffset ${TEMP_IMG} 1)
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530177dd if="${TEMP_KERN}" of="${TEMP_IMG}" conv=notrunc bs=512 \
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700178 seek=$(partoffset ${TEMP_IMG} 2)
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530179dd if="${TEMP_ESP}" of="${TEMP_IMG}" conv=notrunc bs=512 \
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700180 seek=$(partoffset ${TEMP_IMG} 12)
Liam McLoughlinaca5e1e2012-09-23 19:31:29 +0000181dd if="${TEMP_OEM}" of="${TEMP_IMG}" conv=notrunc bs=512 \
182 seek=$(partoffset ${TEMP_IMG} 8)
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530183
Will Drewry537caa92010-08-20 20:30:56 -0500184# Make the built-image bootable and ensure that the legacy default usb boot
185# uses /dev/sda instead of /dev/sdb3.
186# NOTE: The TEMP_IMG must live in the same image dir as the original image
187# to operate automatically below.
188${SCRIPTS_DIR}/bin/cros_make_image_bootable $(dirname "${TEMP_IMG}") \
189 $(basename "${TEMP_IMG}") \
Arkaitz Ruiz Alvarez2bf88592011-07-07 15:47:31 -0700190 --usb_disk /dev/sda3 \
191 --force_developer_mode
Will Drewry537caa92010-08-20 20:30:56 -0500192
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530193echo Creating final image
194# Convert image to output format
195if [ "${FLAGS_format}" = "virtualbox" -o "${FLAGS_format}" = "qemu" ]; then
196 if [ "${FLAGS_format}" = "virtualbox" ]; then
Liam McLoughlinc9f1dab2011-11-29 18:17:26 +0000197 sudo VBoxManage convertdd "${TEMP_IMG}" "${FLAGS_to}/${FLAGS_vbox_disk}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530198 else
199 mv ${TEMP_IMG} ${FLAGS_to}/${DEFAULT_QEMU_IMAGE}
200 fi
201elif [ "${FLAGS_format}" = "vmware" ]; then
202 qemu-img convert -f raw "${TEMP_IMG}" \
203 -O vmdk "${FLAGS_to}/${FLAGS_vmdk}"
204else
Brian Harring7f175a52012-03-02 05:37:00 -0800205 die_notrace "Invalid format: ${FLAGS_format}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530206fi
207
208rm -rf "${TEMP_DIR}" "${TEMP_IMG}"
209if [ -z "${FLAGS_state_image}" ]; then
210 rm -f "${STATE_IMAGE}"
211fi
212
213echo "Created image at ${FLAGS_to}"
214
215# Generate the vmware config file
216# A good reference doc: http://www.sanbarrow.com/vmx.html
217VMX_CONFIG="#!/usr/bin/vmware
218.encoding = \"UTF-8\"
219config.version = \"8\"
220virtualHW.version = \"4\"
221memsize = \"${FLAGS_mem}\"
222ide0:0.present = \"TRUE\"
223ide0:0.fileName = \"${FLAGS_vmdk}\"
224ethernet0.present = \"TRUE\"
225usb.present = \"TRUE\"
226sound.present = \"TRUE\"
227sound.virtualDev = \"es1371\"
228displayName = \"Chromium OS\"
229guestOS = \"otherlinux\"
230ethernet0.addressType = \"generated\"
231floppy0.present = \"FALSE\""
232
233if [[ "${FLAGS_make_vmx}" = "${FLAGS_TRUE}" ]]; then
234 echo "${VMX_CONFIG}" > "${FLAGS_to}/${FLAGS_vmx}"
235 echo "Wrote the following config to: ${FLAGS_to}/${FLAGS_vmx}"
236 echo "${VMX_CONFIG}"
237fi
238
Olof Johansson3ed8d122010-09-27 13:29:40 -0500239
240if [ "${FLAGS_format}" == "qemu" ]; then
241 echo "If you have qemu-kvm installed, you can start the image by:"
Anush Elangovan0de6de62010-12-07 17:31:48 -0800242 echo "sudo kvm -m ${FLAGS_mem} -vga std -pidfile /tmp/kvm.pid -net nic,model=virtio " \
Chris Masone7d04c7a2010-11-09 08:24:13 -0800243 "-net user,hostfwd=tcp::9222-:22 \\"
Olof Johansson3ed8d122010-09-27 13:29:40 -0500244 echo " -hda ${FLAGS_to}/${DEFAULT_QEMU_IMAGE}"
245fi