blob: 5c27c8770832158323b47c47d65960a17f5008fd [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
24DEFINE_string board "${DEFAULT_BOARD}" \
25 "Board for which the image was built"
26DEFINE_boolean factory $FLAGS_FALSE \
27 "Modify the image for manufacturing testing"
28DEFINE_boolean factory_install $FLAGS_FALSE \
29 "Modify the image for factory install shim"
Simon Glass142ca062011-02-09 13:39:43 -080030
31# We default to TRUE so the buildbot gets its image. Note this is different
32# behavior from image_to_usb.sh
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 format "qemu" \
35 "Output format, either qemu, vmware or virtualbox"
36DEFINE_string from "" \
37 "Directory containing rootfs.image and mbr.image"
38DEFINE_boolean make_vmx ${FLAGS_TRUE} \
39 "Create a vmx file for use with vmplayer (vmware only)."
40DEFINE_integer mem "${DEFAULT_MEM}" \
41 "Memory size for the vm config in MBs (vmware only)."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053042DEFINE_string state_image "" \
43 "Stateful partition image (defaults to creating new statful partition)"
44DEFINE_boolean test_image "${FLAGS_FALSE}" \
Simon Glass142ca062011-02-09 13:39:43 -080045 "Copies normal image to ${CHROMEOS_TEST_IMAGE_NAME}, modifies it for test."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053046DEFINE_string to "" \
47 "Destination folder for VM output file(s)"
48DEFINE_string vbox_disk "${DEFAULT_VBOX_DISK}" \
49 "Filename for the output disk (virtualbox only)."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053050DEFINE_string vmdk "${DEFAULT_VMDK}" \
51 "Filename for the vmware disk image (vmware only)."
52DEFINE_string vmx "${DEFAULT_VMX}" \
53 "Filename for the vmware config (vmware only)."
54
Liam McLoughlin5b37c542012-08-16 11:09:37 -070055# The following arguments are ignored.
56# They are here as part of a transition for CL #29931 beacuse the buildbots
57# specify these arguments.
58DEFINE_integer vdisk_size 3072 \
59 "virtual disk size in MBs."
60DEFINE_boolean full "${FLAGS_FALSE}" "Build full image with all partitions."
61DEFINE_integer rootfs_partition_size 1024 \
62 "rootfs parition size in MBs."
63DEFINE_integer statefulfs_size 2048 \
64 "Stateful partition size in MBs."
65
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053066# Parse command line
67FLAGS "$@" || exit 1
68eval set -- "${FLAGS_ARGV}"
69
70# Die on any errors.
Brian Harring7f175a52012-03-02 05:37:00 -080071switch_to_strict_mode
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053072
73if [ -z "${FLAGS_board}" ] ; then
Brian Harring7f175a52012-03-02 05:37:00 -080074 die_notrace "--board is required."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053075fi
76
Liam McLoughlin5b37c542012-08-16 11:09:37 -070077BOARD="$FLAGS_board"
Chris Sosacc09f842010-09-21 17:09:51 -070078
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053079IMAGES_DIR="${DEFAULT_BUILD_ROOT}/images/${FLAGS_board}"
80# Default to the most recent image
81if [ -z "${FLAGS_from}" ] ; then
Kees Cook84a4c7a2011-10-18 13:21:41 -070082 FLAGS_from="$(${SCRIPT_ROOT}/get_latest_image.sh --board=${FLAGS_board})"
Zelidrag Hornung42ca8182010-07-12 18:08:44 -070083else
84 pushd "${FLAGS_from}" && FLAGS_from=`pwd` && popd
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053085fi
86if [ -z "${FLAGS_to}" ] ; then
87 FLAGS_to="${FLAGS_from}"
88fi
89
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053090if [ ${FLAGS_test_image} -eq ${FLAGS_TRUE} ] ; then
Simon Glass142ca062011-02-09 13:39:43 -080091 # Make a test image - this returns the test filename in CHROMEOS_RETURN_VAL
92 prepare_test_image "${FLAGS_from}" "${CHROMEOS_IMAGE_NAME}"
93 SRC_IMAGE="${CHROMEOS_RETURN_VAL}"
94else
95 # Use the standard image
96 SRC_IMAGE="${FLAGS_from}/${CHROMEOS_IMAGE_NAME}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053097fi
98
99# Memory units are in MBs
Greg Spencer798d75f2011-02-01 22:04:49 -0800100TEMP_IMG="$(dirname "${SRC_IMAGE}")/vm_temp_image.bin"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530101
102# If we're not building for VMWare, don't build the vmx
103if [ "${FLAGS_format}" != "vmware" ]; then
104 FLAGS_make_vmx="${FLAGS_FALSE}"
105fi
106
107# Convert args to paths. Need eval to un-quote the string so that shell
108# chars like ~ are processed; just doing FOO=`readlink -f $FOO` won't work.
109FLAGS_from=`eval readlink -f $FLAGS_from`
110FLAGS_to=`eval readlink -f $FLAGS_to`
111
112# Split apart the partitions and make some new ones
113TEMP_DIR=$(mktemp -d)
Mike Frysinger919bf0c2012-08-22 15:32:39 -0400114pushd "${TEMP_DIR}" >/dev/null
115"${FLAGS_from}/unpack_partitions.sh" "${SRC_IMAGE}"
116popd >/dev/null
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530117
118# Fix the kernel command line
119TEMP_ESP="${TEMP_DIR}"/part_12
120TEMP_ROOTFS="${TEMP_DIR}"/part_3
121TEMP_STATE="${TEMP_DIR}"/part_1
Will Drewryefce6682010-07-23 19:43:27 -0500122TEMP_KERN="${TEMP_DIR}"/part_2
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530123if [ -n "${FLAGS_state_image}" ]; then
124 TEMP_STATE="${FLAGS_state_image}"
Rahul Chaturvedie770e162010-07-15 00:35:11 +0530125else
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700126 STATEFUL_SIZE_BYTES=$(get_filesystem_size vm 1)
127 STATEFUL_SIZE_MEGABYTES=$(( STATEFUL_SIZE_BYTES / 1024 / 1024 ))
128 original_image_size=$(stat -c%s "${TEMP_STATE}")
129 if [ "${original_image_size}" -gt "${STATEFUL_SIZE_BYTES}" ]; then
130 die "Cannot resize stateful image to smaller than original. Exiting."
Rahul Chaturvedie770e162010-07-15 00:35:11 +0530131 fi
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700132
133 echo "Resizing stateful partition to ${STATEFUL_SIZE_MEGABYTES}MB"
134 # Extend the original file size to the new size.
135 sudo e2fsck -pf "${TEMP_STATE}"
136 sudo resize2fs "${TEMP_STATE}" ${STATEFUL_SIZE_MEGABYTES}M
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530137fi
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530138TEMP_PMBR="${TEMP_DIR}"/pmbr
139dd if="${SRC_IMAGE}" of="${TEMP_PMBR}" bs=512 count=1
140
141TEMP_MNT=$(mktemp -d)
Will Drewryefce6682010-07-23 19:43:27 -0500142TEMP_ESP_MNT=$(mktemp -d)
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530143cleanup() {
Brian Harringece65e02012-08-30 13:42:21 -0700144 safe_umount "${TEMP_MNT}"
145 safe_umount "${TEMP_ESP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500146 rmdir "${TEMP_MNT}" "${TEMP_ESP_MNT}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530147}
148trap cleanup INT TERM EXIT
149mkdir -p "${TEMP_MNT}"
Will Drewryf929c302010-10-20 18:48:20 -0500150enable_rw_mount "${TEMP_ROOTFS}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530151sudo mount -o loop "${TEMP_ROOTFS}" "${TEMP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500152mkdir -p "${TEMP_ESP_MNT}"
153sudo mount -o loop "${TEMP_ESP}" "${TEMP_ESP_MNT}"
154
Will Drewry537caa92010-08-20 20:30:56 -0500155# Modify the unverified usb template which uses a default usb_disk of sdb3
156sudo sed -i -e 's/sdb3/sda3/g' "${TEMP_MNT}/boot/syslinux/usb.A.cfg"
157
158# Unmount everything prior to building a final image
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530159trap - INT TERM EXIT
160cleanup
161
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700162# Set up a new partition table
163PARTITION_SCRIPT_PATH=$( tempfile )
164write_partition_script "vm" "${PARTITION_SCRIPT_PATH}"
165. "${PARTITION_SCRIPT_PATH}"
166write_partition_table "${TEMP_IMG}" "${TEMP_PMBR}"
167rm "${PARTITION_SCRIPT_PATH}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530168
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530169# Copy into the partition parts of the file
170dd if="${TEMP_ROOTFS}" of="${TEMP_IMG}" conv=notrunc bs=512 \
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700171 seek=$(partoffset ${TEMP_IMG} 3)
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530172dd if="${TEMP_STATE}" of="${TEMP_IMG}" conv=notrunc bs=512 \
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700173 seek=$(partoffset ${TEMP_IMG} 1)
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530174dd if="${TEMP_KERN}" of="${TEMP_IMG}" conv=notrunc bs=512 \
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700175 seek=$(partoffset ${TEMP_IMG} 2)
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530176dd if="${TEMP_ESP}" of="${TEMP_IMG}" conv=notrunc bs=512 \
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700177 seek=$(partoffset ${TEMP_IMG} 12)
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530178
Will Drewry537caa92010-08-20 20:30:56 -0500179# Make the built-image bootable and ensure that the legacy default usb boot
180# uses /dev/sda instead of /dev/sdb3.
181# NOTE: The TEMP_IMG must live in the same image dir as the original image
182# to operate automatically below.
183${SCRIPTS_DIR}/bin/cros_make_image_bootable $(dirname "${TEMP_IMG}") \
184 $(basename "${TEMP_IMG}") \
Arkaitz Ruiz Alvarez2bf88592011-07-07 15:47:31 -0700185 --usb_disk /dev/sda3 \
186 --force_developer_mode
Will Drewry537caa92010-08-20 20:30:56 -0500187
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530188echo Creating final image
189# Convert image to output format
190if [ "${FLAGS_format}" = "virtualbox" -o "${FLAGS_format}" = "qemu" ]; then
191 if [ "${FLAGS_format}" = "virtualbox" ]; then
Liam McLoughlinc9f1dab2011-11-29 18:17:26 +0000192 sudo VBoxManage convertdd "${TEMP_IMG}" "${FLAGS_to}/${FLAGS_vbox_disk}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530193 else
194 mv ${TEMP_IMG} ${FLAGS_to}/${DEFAULT_QEMU_IMAGE}
195 fi
196elif [ "${FLAGS_format}" = "vmware" ]; then
197 qemu-img convert -f raw "${TEMP_IMG}" \
198 -O vmdk "${FLAGS_to}/${FLAGS_vmdk}"
199else
Brian Harring7f175a52012-03-02 05:37:00 -0800200 die_notrace "Invalid format: ${FLAGS_format}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530201fi
202
203rm -rf "${TEMP_DIR}" "${TEMP_IMG}"
204if [ -z "${FLAGS_state_image}" ]; then
205 rm -f "${STATE_IMAGE}"
206fi
207
208echo "Created image at ${FLAGS_to}"
209
210# Generate the vmware config file
211# A good reference doc: http://www.sanbarrow.com/vmx.html
212VMX_CONFIG="#!/usr/bin/vmware
213.encoding = \"UTF-8\"
214config.version = \"8\"
215virtualHW.version = \"4\"
216memsize = \"${FLAGS_mem}\"
217ide0:0.present = \"TRUE\"
218ide0:0.fileName = \"${FLAGS_vmdk}\"
219ethernet0.present = \"TRUE\"
220usb.present = \"TRUE\"
221sound.present = \"TRUE\"
222sound.virtualDev = \"es1371\"
223displayName = \"Chromium OS\"
224guestOS = \"otherlinux\"
225ethernet0.addressType = \"generated\"
226floppy0.present = \"FALSE\""
227
228if [[ "${FLAGS_make_vmx}" = "${FLAGS_TRUE}" ]]; then
229 echo "${VMX_CONFIG}" > "${FLAGS_to}/${FLAGS_vmx}"
230 echo "Wrote the following config to: ${FLAGS_to}/${FLAGS_vmx}"
231 echo "${VMX_CONFIG}"
232fi
233
Olof Johansson3ed8d122010-09-27 13:29:40 -0500234
235if [ "${FLAGS_format}" == "qemu" ]; then
236 echo "If you have qemu-kvm installed, you can start the image by:"
Anush Elangovan0de6de62010-12-07 17:31:48 -0800237 echo "sudo kvm -m ${FLAGS_mem} -vga std -pidfile /tmp/kvm.pid -net nic,model=virtio " \
Chris Masone7d04c7a2010-11-09 08:24:13 -0800238 "-net user,hostfwd=tcp::9222-:22 \\"
Olof Johansson3ed8d122010-09-27 13:29:40 -0500239 echo " -hda ${FLAGS_to}/${DEFAULT_QEMU_IMAGE}"
240fi