blob: ae3aaab72f6d8c1dd4b194a3f35a71f60a367f0a [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")")
Brian Harringd5d5dbf2011-07-11 16:36:21 -070012. "${SCRIPT_ROOT}/common.sh" || { echo "Unable to load common.sh"; exit 1; }
Greg Spencer798d75f2011-02-01 22:04:49 -080013
14# Need to be inside the chroot to load chromeos-common.sh
15assert_inside_chroot
16
17# Load functions and constants for chromeos-install
18. "/usr/lib/installer/chromeos-common.sh" || \
19 die "Unable to load /usr/lib/installer/chromeos-common.sh"
20
21. "${SCRIPT_ROOT}/lib/cros_vm_constants.sh" || \
22 die "Unable to load ${SCRIPT_ROOT}/lib/cros_vm_constants.sh"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053023
24get_default_board
25
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053026# Flags
27DEFINE_string board "${DEFAULT_BOARD}" \
28 "Board for which the image was built"
29DEFINE_boolean factory $FLAGS_FALSE \
30 "Modify the image for manufacturing testing"
31DEFINE_boolean factory_install $FLAGS_FALSE \
32 "Modify the image for factory install shim"
Simon Glass142ca062011-02-09 13:39:43 -080033
34# We default to TRUE so the buildbot gets its image. Note this is different
35# behavior from image_to_usb.sh
Chris Sosa8dad50d2011-02-14 15:29:32 -080036DEFINE_boolean force_copy ${FLAGS_FALSE} "Always rebuild test image"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053037DEFINE_string format "qemu" \
38 "Output format, either qemu, vmware or virtualbox"
39DEFINE_string from "" \
40 "Directory containing rootfs.image and mbr.image"
Chris Sosacc09f842010-09-21 17:09:51 -070041DEFINE_boolean full "${FLAGS_FALSE}" "Build full image with all partitions."
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)."
46DEFINE_integer rootfs_partition_size 1024 \
47 "rootfs parition size in MBs."
48DEFINE_string state_image "" \
49 "Stateful partition image (defaults to creating new statful partition)"
Chris Masone7d04c7a2010-11-09 08:24:13 -080050DEFINE_integer statefulfs_size 2048 \
Rahul Chaturvedie770e162010-07-15 00:35:11 +053051 "Stateful partition size in MBs."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053052DEFINE_boolean test_image "${FLAGS_FALSE}" \
Simon Glass142ca062011-02-09 13:39:43 -080053 "Copies normal image to ${CHROMEOS_TEST_IMAGE_NAME}, modifies it for test."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053054DEFINE_string to "" \
55 "Destination folder for VM output file(s)"
56DEFINE_string vbox_disk "${DEFAULT_VBOX_DISK}" \
57 "Filename for the output disk (virtualbox only)."
58DEFINE_integer vdisk_size 3072 \
59 "virtual disk size in MBs."
60DEFINE_string vmdk "${DEFAULT_VMDK}" \
61 "Filename for the vmware disk image (vmware only)."
62DEFINE_string vmx "${DEFAULT_VMX}" \
63 "Filename for the vmware config (vmware only)."
64
65# Parse command line
66FLAGS "$@" || exit 1
67eval set -- "${FLAGS_ARGV}"
68
69# Die on any errors.
70set -e
71
72if [ -z "${FLAGS_board}" ] ; then
73 die "--board is required."
74fi
75
Chris Sosacc09f842010-09-21 17:09:51 -070076if [ "${FLAGS_full}" -eq "${FLAGS_TRUE}" ] && \
77 ( [[ ${FLAGS_vdisk_size} < ${MIN_VDISK_SIZE_FULL} ]] || \
78 [[ ${FLAGS_statefulfs_size} < ${MIN_STATEFUL_FS_SIZE_FULL} ]]); then
Chris Sosa9fe00b92010-10-22 12:34:16 -070079 warn "Disk is too small for full, using minimum: vdisk size equal to \
80${MIN_VDISK_SIZE_FULL} and statefulfs size equal to \
Chris Sosacc09f842010-09-21 17:09:51 -070081${MIN_STATEFUL_FS_SIZE_FULL}."
Chris Sosa9fe00b92010-10-22 12:34:16 -070082 FLAGS_vdisk_size=${MIN_VDISK_SIZE_FULL}
83 FLAGS_statefulfs_size=${MIN_STATEFUL_FS_SIZE_FULL}
Chris Sosacc09f842010-09-21 17:09:51 -070084fi
85
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053086IMAGES_DIR="${DEFAULT_BUILD_ROOT}/images/${FLAGS_board}"
87# Default to the most recent image
88if [ -z "${FLAGS_from}" ] ; then
Kees Cook84a4c7a2011-10-18 13:21:41 -070089 FLAGS_from="$(${SCRIPT_ROOT}/get_latest_image.sh --board=${FLAGS_board})"
Zelidrag Hornung42ca8182010-07-12 18:08:44 -070090else
91 pushd "${FLAGS_from}" && FLAGS_from=`pwd` && popd
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053092fi
93if [ -z "${FLAGS_to}" ] ; then
94 FLAGS_to="${FLAGS_from}"
95fi
96
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053097if [ ${FLAGS_test_image} -eq ${FLAGS_TRUE} ] ; then
Simon Glass142ca062011-02-09 13:39:43 -080098 # Make a test image - this returns the test filename in CHROMEOS_RETURN_VAL
99 prepare_test_image "${FLAGS_from}" "${CHROMEOS_IMAGE_NAME}"
100 SRC_IMAGE="${CHROMEOS_RETURN_VAL}"
101else
102 # Use the standard image
103 SRC_IMAGE="${FLAGS_from}/${CHROMEOS_IMAGE_NAME}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530104fi
105
106# Memory units are in MBs
Greg Spencer798d75f2011-02-01 22:04:49 -0800107TEMP_IMG="$(dirname "${SRC_IMAGE}")/vm_temp_image.bin"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530108
109# If we're not building for VMWare, don't build the vmx
110if [ "${FLAGS_format}" != "vmware" ]; then
111 FLAGS_make_vmx="${FLAGS_FALSE}"
112fi
113
114# Convert args to paths. Need eval to un-quote the string so that shell
115# chars like ~ are processed; just doing FOO=`readlink -f $FOO` won't work.
116FLAGS_from=`eval readlink -f $FLAGS_from`
117FLAGS_to=`eval readlink -f $FLAGS_to`
118
119# Split apart the partitions and make some new ones
120TEMP_DIR=$(mktemp -d)
121(cd "${TEMP_DIR}" &&
122 "${FLAGS_from}/unpack_partitions.sh" "${SRC_IMAGE}")
123
124# Fix the kernel command line
125TEMP_ESP="${TEMP_DIR}"/part_12
126TEMP_ROOTFS="${TEMP_DIR}"/part_3
127TEMP_STATE="${TEMP_DIR}"/part_1
Will Drewryefce6682010-07-23 19:43:27 -0500128TEMP_KERN="${TEMP_DIR}"/part_2
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530129if [ -n "${FLAGS_state_image}" ]; then
130 TEMP_STATE="${FLAGS_state_image}"
Rahul Chaturvedie770e162010-07-15 00:35:11 +0530131else
132 # If we have a stateful fs size specified create a new state partition
133 # of the specified size.
134 if [ "${FLAGS_statefulfs_size}" -ne -1 ]; then
135 STATEFUL_SIZE_BYTES=$((1024 * 1024 * ${FLAGS_statefulfs_size}))
136 original_image_size=$(stat -c%s "${TEMP_STATE}")
137 if [ "${original_image_size}" -gt "${STATEFUL_SIZE_BYTES}" ]; then
138 die "Cannot resize stateful image to smaller than original. Exiting."
139 fi
140
141 echo "Resizing stateful partition to ${FLAGS_statefulfs_size}MB"
Rahul Chaturvedie770e162010-07-15 00:35:11 +0530142 # Extend the original file size to the new size.
David James596520a2011-08-15 17:11:00 -0700143 sudo e2fsck -pf "${TEMP_STATE}"
144 sudo resize2fs "${TEMP_STATE}" ${FLAGS_statefulfs_size}M
Rahul Chaturvedie770e162010-07-15 00:35:11 +0530145 fi
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530146fi
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530147TEMP_PMBR="${TEMP_DIR}"/pmbr
148dd if="${SRC_IMAGE}" of="${TEMP_PMBR}" bs=512 count=1
149
150TEMP_MNT=$(mktemp -d)
Will Drewryefce6682010-07-23 19:43:27 -0500151TEMP_ESP_MNT=$(mktemp -d)
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530152cleanup() {
153 sudo umount -d "${TEMP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500154 sudo umount -d "${TEMP_ESP_MNT}"
155 rmdir "${TEMP_MNT}" "${TEMP_ESP_MNT}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530156}
157trap cleanup INT TERM EXIT
158mkdir -p "${TEMP_MNT}"
Will Drewryf929c302010-10-20 18:48:20 -0500159enable_rw_mount "${TEMP_ROOTFS}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530160sudo mount -o loop "${TEMP_ROOTFS}" "${TEMP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500161mkdir -p "${TEMP_ESP_MNT}"
162sudo mount -o loop "${TEMP_ESP}" "${TEMP_ESP_MNT}"
163
Stéphane Marchesin7e0b3d82011-03-29 06:58:17 -0700164sudo python "${SCRIPTS_DIR}/fixup_image_for_qemu.py" \
165 --mounted_dir="${TEMP_MNT}"
166
Will Drewry537caa92010-08-20 20:30:56 -0500167# Modify the unverified usb template which uses a default usb_disk of sdb3
168sudo sed -i -e 's/sdb3/sda3/g' "${TEMP_MNT}/boot/syslinux/usb.A.cfg"
169
170# Unmount everything prior to building a final image
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530171trap - INT TERM EXIT
172cleanup
173
Chris Sosacc09f842010-09-21 17:09:51 -0700174# TOOD(adlr): pick a size that will for sure accomodate the partitions.
Will Drewry537caa92010-08-20 20:30:56 -0500175dd if=/dev/zero of="${TEMP_IMG}" bs=1 count=1 \
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530176 seek=$((${FLAGS_vdisk_size} * 1024 * 1024 - 1))
177
Chris Sosacc09f842010-09-21 17:09:51 -0700178GPT_FULL="false"
179[ "${FLAGS_full}" -eq "${FLAGS_TRUE}" ] && GPT_FULL="true"
180
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530181# Set up the partition table
Tan Gao0d5f9482010-08-06 08:28:20 -0700182install_gpt "${TEMP_IMG}" "$(numsectors $TEMP_ROOTFS)" \
183 "$(numsectors $TEMP_STATE)" "${TEMP_PMBR}" "$(numsectors $TEMP_ESP)" \
Chris Sosacc09f842010-09-21 17:09:51 -0700184 "${GPT_FULL}" ${FLAGS_rootfs_partition_size}
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530185# Copy into the partition parts of the file
186dd if="${TEMP_ROOTFS}" of="${TEMP_IMG}" conv=notrunc bs=512 \
187 seek="${START_ROOTFS_A}"
188dd if="${TEMP_STATE}" of="${TEMP_IMG}" conv=notrunc bs=512 \
189 seek="${START_STATEFUL}"
190dd if="${TEMP_KERN}" of="${TEMP_IMG}" conv=notrunc bs=512 \
191 seek="${START_KERN_A}"
192dd if="${TEMP_ESP}" of="${TEMP_IMG}" conv=notrunc bs=512 \
193 seek="${START_ESP}"
194
Will Drewry537caa92010-08-20 20:30:56 -0500195# Make the built-image bootable and ensure that the legacy default usb boot
196# uses /dev/sda instead of /dev/sdb3.
197# NOTE: The TEMP_IMG must live in the same image dir as the original image
198# to operate automatically below.
199${SCRIPTS_DIR}/bin/cros_make_image_bootable $(dirname "${TEMP_IMG}") \
200 $(basename "${TEMP_IMG}") \
Arkaitz Ruiz Alvarez2bf88592011-07-07 15:47:31 -0700201 --usb_disk /dev/sda3 \
202 --force_developer_mode
Will Drewry537caa92010-08-20 20:30:56 -0500203
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530204echo Creating final image
205# Convert image to output format
206if [ "${FLAGS_format}" = "virtualbox" -o "${FLAGS_format}" = "qemu" ]; then
207 if [ "${FLAGS_format}" = "virtualbox" ]; then
Liam McLoughlinc9f1dab2011-11-29 18:17:26 +0000208 sudo VBoxManage convertdd "${TEMP_IMG}" "${FLAGS_to}/${FLAGS_vbox_disk}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530209 else
210 mv ${TEMP_IMG} ${FLAGS_to}/${DEFAULT_QEMU_IMAGE}
211 fi
212elif [ "${FLAGS_format}" = "vmware" ]; then
213 qemu-img convert -f raw "${TEMP_IMG}" \
214 -O vmdk "${FLAGS_to}/${FLAGS_vmdk}"
215else
216 die "Invalid format: ${FLAGS_format}"
217fi
218
219rm -rf "${TEMP_DIR}" "${TEMP_IMG}"
220if [ -z "${FLAGS_state_image}" ]; then
221 rm -f "${STATE_IMAGE}"
222fi
223
224echo "Created image at ${FLAGS_to}"
225
226# Generate the vmware config file
227# A good reference doc: http://www.sanbarrow.com/vmx.html
228VMX_CONFIG="#!/usr/bin/vmware
229.encoding = \"UTF-8\"
230config.version = \"8\"
231virtualHW.version = \"4\"
232memsize = \"${FLAGS_mem}\"
233ide0:0.present = \"TRUE\"
234ide0:0.fileName = \"${FLAGS_vmdk}\"
235ethernet0.present = \"TRUE\"
236usb.present = \"TRUE\"
237sound.present = \"TRUE\"
238sound.virtualDev = \"es1371\"
239displayName = \"Chromium OS\"
240guestOS = \"otherlinux\"
241ethernet0.addressType = \"generated\"
242floppy0.present = \"FALSE\""
243
244if [[ "${FLAGS_make_vmx}" = "${FLAGS_TRUE}" ]]; then
245 echo "${VMX_CONFIG}" > "${FLAGS_to}/${FLAGS_vmx}"
246 echo "Wrote the following config to: ${FLAGS_to}/${FLAGS_vmx}"
247 echo "${VMX_CONFIG}"
248fi
249
Olof Johansson3ed8d122010-09-27 13:29:40 -0500250
251if [ "${FLAGS_format}" == "qemu" ]; then
252 echo "If you have qemu-kvm installed, you can start the image by:"
Anush Elangovan0de6de62010-12-07 17:31:48 -0800253 echo "sudo kvm -m ${FLAGS_mem} -vga std -pidfile /tmp/kvm.pid -net nic,model=virtio " \
Chris Masone7d04c7a2010-11-09 08:24:13 -0800254 "-net user,hostfwd=tcp::9222-:22 \\"
Olof Johansson3ed8d122010-09-27 13:29:40 -0500255 echo " -hda ${FLAGS_to}/${DEFAULT_QEMU_IMAGE}"
256fi