blob: b8210398910f7d43943439deeaa21944b054f764 [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
Bryan Freed4cc30e52014-04-07 17:21:29 +000020. /usr/lib/installer/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
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"
Chris Sosac46094a2013-04-23 14:29:04 -070040DEFINE_string disk_layout "usb" \
Liam McLoughlinb78a7c32012-11-06 20:19:05 -050041 "The disk layout type to use for this image."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053042DEFINE_boolean make_vmx ${FLAGS_TRUE} \
43 "Create a vmx file for use with vmplayer (vmware only)."
44DEFINE_integer mem "${DEFAULT_MEM}" \
45 "Memory size for the vm config in MBs (vmware only)."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053046DEFINE_string state_image "" \
47 "Stateful partition image (defaults to creating new statful partition)"
48DEFINE_boolean test_image "${FLAGS_FALSE}" \
Simon Glass142ca062011-02-09 13:39:43 -080049 "Copies normal image to ${CHROMEOS_TEST_IMAGE_NAME}, modifies it for test."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053050DEFINE_string to "" \
51 "Destination folder for VM output file(s)"
52DEFINE_string vbox_disk "${DEFAULT_VBOX_DISK}" \
53 "Filename for the output disk (virtualbox only)."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053054DEFINE_string vmdk "${DEFAULT_VMDK}" \
55 "Filename for the vmware disk image (vmware only)."
56DEFINE_string vmx "${DEFAULT_VMX}" \
57 "Filename for the vmware config (vmware only)."
58
59# Parse command line
60FLAGS "$@" || exit 1
61eval set -- "${FLAGS_ARGV}"
62
63# Die on any errors.
Brian Harring7f175a52012-03-02 05:37:00 -080064switch_to_strict_mode
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053065
66if [ -z "${FLAGS_board}" ] ; then
Brian Harring7f175a52012-03-02 05:37:00 -080067 die_notrace "--board is required."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053068fi
69
Liam McLoughlin5b37c542012-08-16 11:09:37 -070070BOARD="$FLAGS_board"
Chris Sosacc09f842010-09-21 17:09:51 -070071
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053072IMAGES_DIR="${DEFAULT_BUILD_ROOT}/images/${FLAGS_board}"
73# Default to the most recent image
74if [ -z "${FLAGS_from}" ] ; then
Kees Cook84a4c7a2011-10-18 13:21:41 -070075 FLAGS_from="$(${SCRIPT_ROOT}/get_latest_image.sh --board=${FLAGS_board})"
Zelidrag Hornung42ca8182010-07-12 18:08:44 -070076else
77 pushd "${FLAGS_from}" && FLAGS_from=`pwd` && popd
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053078fi
79if [ -z "${FLAGS_to}" ] ; then
80 FLAGS_to="${FLAGS_from}"
81fi
82
David James7efb76c2013-01-09 15:25:58 -080083if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ]; then
84 SRC_IMAGE="${FLAGS_from}/${CHROMEOS_FACTORY_TEST_IMAGE_NAME}"
85elif [ ${FLAGS_test_image} -eq ${FLAGS_TRUE} ]; then
86 SRC_IMAGE="${FLAGS_from}/${CHROMEOS_TEST_IMAGE_NAME}"
Simon Glass142ca062011-02-09 13:39:43 -080087else
88 # Use the standard image
89 SRC_IMAGE="${FLAGS_from}/${CHROMEOS_IMAGE_NAME}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053090fi
91
92# Memory units are in MBs
Greg Spencer798d75f2011-02-01 22:04:49 -080093TEMP_IMG="$(dirname "${SRC_IMAGE}")/vm_temp_image.bin"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053094
95# If we're not building for VMWare, don't build the vmx
96if [ "${FLAGS_format}" != "vmware" ]; then
97 FLAGS_make_vmx="${FLAGS_FALSE}"
98fi
99
100# Convert args to paths. Need eval to un-quote the string so that shell
101# chars like ~ are processed; just doing FOO=`readlink -f $FOO` won't work.
102FLAGS_from=`eval readlink -f $FLAGS_from`
103FLAGS_to=`eval readlink -f $FLAGS_to`
104
105# Split apart the partitions and make some new ones
106TEMP_DIR=$(mktemp -d)
Mike Frysinger919bf0c2012-08-22 15:32:39 -0400107pushd "${TEMP_DIR}" >/dev/null
108"${FLAGS_from}/unpack_partitions.sh" "${SRC_IMAGE}"
109popd >/dev/null
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530110
111# Fix the kernel command line
112TEMP_ESP="${TEMP_DIR}"/part_12
Liam McLoughlinaca5e1e2012-09-23 19:31:29 +0000113TEMP_OEM="${TEMP_DIR}"/part_8
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530114TEMP_ROOTFS="${TEMP_DIR}"/part_3
115TEMP_STATE="${TEMP_DIR}"/part_1
Will Drewryefce6682010-07-23 19:43:27 -0500116TEMP_KERN="${TEMP_DIR}"/part_2
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530117if [ -n "${FLAGS_state_image}" ]; then
118 TEMP_STATE="${FLAGS_state_image}"
Rahul Chaturvedie770e162010-07-15 00:35:11 +0530119else
Liam McLoughlinb78a7c32012-11-06 20:19:05 -0500120 STATEFUL_SIZE_BYTES=$(get_filesystem_size "${FLAGS_disk_layout}" 1)
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700121 STATEFUL_SIZE_MEGABYTES=$(( STATEFUL_SIZE_BYTES / 1024 / 1024 ))
122 original_image_size=$(stat -c%s "${TEMP_STATE}")
123 if [ "${original_image_size}" -gt "${STATEFUL_SIZE_BYTES}" ]; then
124 die "Cannot resize stateful image to smaller than original. Exiting."
Rahul Chaturvedie770e162010-07-15 00:35:11 +0530125 fi
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700126
127 echo "Resizing stateful partition to ${STATEFUL_SIZE_MEGABYTES}MB"
128 # Extend the original file size to the new size.
129 sudo e2fsck -pf "${TEMP_STATE}"
130 sudo resize2fs "${TEMP_STATE}" ${STATEFUL_SIZE_MEGABYTES}M
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530131fi
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530132TEMP_PMBR="${TEMP_DIR}"/pmbr
133dd if="${SRC_IMAGE}" of="${TEMP_PMBR}" bs=512 count=1
134
135TEMP_MNT=$(mktemp -d)
Will Drewryefce6682010-07-23 19:43:27 -0500136TEMP_ESP_MNT=$(mktemp -d)
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530137cleanup() {
Brian Harringece65e02012-08-30 13:42:21 -0700138 safe_umount "${TEMP_MNT}"
139 safe_umount "${TEMP_ESP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500140 rmdir "${TEMP_MNT}" "${TEMP_ESP_MNT}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530141}
142trap cleanup INT TERM EXIT
143mkdir -p "${TEMP_MNT}"
Will Drewryf929c302010-10-20 18:48:20 -0500144enable_rw_mount "${TEMP_ROOTFS}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530145sudo mount -o loop "${TEMP_ROOTFS}" "${TEMP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500146mkdir -p "${TEMP_ESP_MNT}"
147sudo mount -o loop "${TEMP_ESP}" "${TEMP_ESP_MNT}"
148
Will Drewry537caa92010-08-20 20:30:56 -0500149# Modify the unverified usb template which uses a default usb_disk of sdb3
150sudo sed -i -e 's/sdb3/sda3/g' "${TEMP_MNT}/boot/syslinux/usb.A.cfg"
151
Jorge Lucangeli Obes29b972b2013-12-18 15:56:26 -0800152# Add loading of cirrus fb module
Dominik Behrd1a71ca2013-10-01 15:18:38 -0700153if [ "${FLAGS_format}" = "qemu" ]; then
154 sudo_clobber "${TEMP_MNT}/etc/init/cirrusfb.conf" <<END
155start on starting boot-splash
156task
157exec modprobe cirrus
158END
159fi
Jorge Lucangeli Obes29b972b2013-12-18 15:56:26 -0800160
Jorge Lucangeli Obes29b972b2013-12-18 15:56:26 -0800161# TODO as these image-modifying hacks accumulate, we should consider
Dominik Behrd1a71ca2013-10-01 15:18:38 -0700162# creating a better solution
163
Will Drewry537caa92010-08-20 20:30:56 -0500164# Unmount everything prior to building a final image
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530165trap - INT TERM EXIT
166cleanup
167
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700168# Set up a new partition table
169PARTITION_SCRIPT_PATH=$( tempfile )
Liam McLoughlinb78a7c32012-11-06 20:19:05 -0500170write_partition_script "${FLAGS_disk_layout}" "${PARTITION_SCRIPT_PATH}"
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700171. "${PARTITION_SCRIPT_PATH}"
172write_partition_table "${TEMP_IMG}" "${TEMP_PMBR}"
173rm "${PARTITION_SCRIPT_PATH}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530174
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530175# Copy into the partition parts of the file
176dd if="${TEMP_ROOTFS}" of="${TEMP_IMG}" conv=notrunc bs=512 \
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700177 seek=$(partoffset ${TEMP_IMG} 3)
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530178dd if="${TEMP_STATE}" of="${TEMP_IMG}" conv=notrunc bs=512 \
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700179 seek=$(partoffset ${TEMP_IMG} 1)
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530180dd if="${TEMP_KERN}" of="${TEMP_IMG}" conv=notrunc bs=512 \
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700181 seek=$(partoffset ${TEMP_IMG} 2)
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530182dd if="${TEMP_ESP}" of="${TEMP_IMG}" conv=notrunc bs=512 \
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700183 seek=$(partoffset ${TEMP_IMG} 12)
Liam McLoughlinaca5e1e2012-09-23 19:31:29 +0000184dd if="${TEMP_OEM}" of="${TEMP_IMG}" conv=notrunc bs=512 \
185 seek=$(partoffset ${TEMP_IMG} 8)
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530186
Will Drewry537caa92010-08-20 20:30:56 -0500187# Make the built-image bootable and ensure that the legacy default usb boot
188# uses /dev/sda instead of /dev/sdb3.
189# NOTE: The TEMP_IMG must live in the same image dir as the original image
190# to operate automatically below.
191${SCRIPTS_DIR}/bin/cros_make_image_bootable $(dirname "${TEMP_IMG}") \
192 $(basename "${TEMP_IMG}") \
Arkaitz Ruiz Alvarez2bf88592011-07-07 15:47:31 -0700193 --usb_disk /dev/sda3 \
194 --force_developer_mode
Will Drewry537caa92010-08-20 20:30:56 -0500195
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530196echo Creating final image
197# Convert image to output format
198if [ "${FLAGS_format}" = "virtualbox" -o "${FLAGS_format}" = "qemu" ]; then
199 if [ "${FLAGS_format}" = "virtualbox" ]; then
Liam McLoughlinc9f1dab2011-11-29 18:17:26 +0000200 sudo VBoxManage convertdd "${TEMP_IMG}" "${FLAGS_to}/${FLAGS_vbox_disk}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530201 else
202 mv ${TEMP_IMG} ${FLAGS_to}/${DEFAULT_QEMU_IMAGE}
203 fi
204elif [ "${FLAGS_format}" = "vmware" ]; then
205 qemu-img convert -f raw "${TEMP_IMG}" \
206 -O vmdk "${FLAGS_to}/${FLAGS_vmdk}"
207else
Brian Harring7f175a52012-03-02 05:37:00 -0800208 die_notrace "Invalid format: ${FLAGS_format}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530209fi
210
211rm -rf "${TEMP_DIR}" "${TEMP_IMG}"
212if [ -z "${FLAGS_state_image}" ]; then
213 rm -f "${STATE_IMAGE}"
214fi
215
216echo "Created image at ${FLAGS_to}"
217
218# Generate the vmware config file
219# A good reference doc: http://www.sanbarrow.com/vmx.html
220VMX_CONFIG="#!/usr/bin/vmware
221.encoding = \"UTF-8\"
222config.version = \"8\"
223virtualHW.version = \"4\"
224memsize = \"${FLAGS_mem}\"
225ide0:0.present = \"TRUE\"
226ide0:0.fileName = \"${FLAGS_vmdk}\"
227ethernet0.present = \"TRUE\"
228usb.present = \"TRUE\"
229sound.present = \"TRUE\"
230sound.virtualDev = \"es1371\"
231displayName = \"Chromium OS\"
232guestOS = \"otherlinux\"
233ethernet0.addressType = \"generated\"
234floppy0.present = \"FALSE\""
235
236if [[ "${FLAGS_make_vmx}" = "${FLAGS_TRUE}" ]]; then
237 echo "${VMX_CONFIG}" > "${FLAGS_to}/${FLAGS_vmx}"
238 echo "Wrote the following config to: ${FLAGS_to}/${FLAGS_vmx}"
239 echo "${VMX_CONFIG}"
240fi
241
Olof Johansson3ed8d122010-09-27 13:29:40 -0500242
243if [ "${FLAGS_format}" == "qemu" ]; then
244 echo "If you have qemu-kvm installed, you can start the image by:"
John Sheub7b095b2012-12-18 13:30:26 -0800245 echo "sudo kvm -m ${FLAGS_mem} -vga cirrus -pidfile /tmp/kvm.pid" \
246 "-net nic,model=virtio -net user,hostfwd=tcp::9222-:22 \\"
247 echo "-hda ${FLAGS_to}/${DEFAULT_QEMU_IMAGE}"
Olof Johansson3ed8d122010-09-27 13:29:40 -0500248fi