blob: 6ebc0aefa487e8816f410f5cb2c57789c1604ea7 [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
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
David James359d3e12012-07-10 13:09:48 -070018. /usr/lib/installer/chromeos-common.sh || exit 1
19. "${SCRIPT_ROOT}/lib/cros_vm_constants.sh" || exit 1
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053020
21get_default_board
22
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"
Chris Sosacc09f842010-09-21 17:09:51 -070038DEFINE_boolean full "${FLAGS_FALSE}" "Build full image with all partitions."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053039DEFINE_boolean make_vmx ${FLAGS_TRUE} \
40 "Create a vmx file for use with vmplayer (vmware only)."
41DEFINE_integer mem "${DEFAULT_MEM}" \
42 "Memory size for the vm config in MBs (vmware only)."
43DEFINE_integer rootfs_partition_size 1024 \
44 "rootfs parition size in MBs."
45DEFINE_string state_image "" \
46 "Stateful partition image (defaults to creating new statful partition)"
Chris Masone7d04c7a2010-11-09 08:24:13 -080047DEFINE_integer statefulfs_size 2048 \
Rahul Chaturvedie770e162010-07-15 00:35:11 +053048 "Stateful partition size in MBs."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053049DEFINE_boolean test_image "${FLAGS_FALSE}" \
Simon Glass142ca062011-02-09 13:39:43 -080050 "Copies normal image to ${CHROMEOS_TEST_IMAGE_NAME}, modifies it for test."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053051DEFINE_string to "" \
52 "Destination folder for VM output file(s)"
53DEFINE_string vbox_disk "${DEFAULT_VBOX_DISK}" \
54 "Filename for the output disk (virtualbox only)."
55DEFINE_integer vdisk_size 3072 \
56 "virtual disk size in MBs."
57DEFINE_string vmdk "${DEFAULT_VMDK}" \
58 "Filename for the vmware disk image (vmware only)."
59DEFINE_string vmx "${DEFAULT_VMX}" \
60 "Filename for the vmware config (vmware only)."
61
62# Parse command line
63FLAGS "$@" || exit 1
64eval set -- "${FLAGS_ARGV}"
65
66# Die on any errors.
Brian Harring7f175a52012-03-02 05:37:00 -080067switch_to_strict_mode
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053068
69if [ -z "${FLAGS_board}" ] ; then
Brian Harring7f175a52012-03-02 05:37:00 -080070 die_notrace "--board is required."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053071fi
72
Chris Sosacc09f842010-09-21 17:09:51 -070073if [ "${FLAGS_full}" -eq "${FLAGS_TRUE}" ] && \
74 ( [[ ${FLAGS_vdisk_size} < ${MIN_VDISK_SIZE_FULL} ]] || \
75 [[ ${FLAGS_statefulfs_size} < ${MIN_STATEFUL_FS_SIZE_FULL} ]]); then
Chris Sosa9fe00b92010-10-22 12:34:16 -070076 warn "Disk is too small for full, using minimum: vdisk size equal to \
77${MIN_VDISK_SIZE_FULL} and statefulfs size equal to \
Chris Sosacc09f842010-09-21 17:09:51 -070078${MIN_STATEFUL_FS_SIZE_FULL}."
Chris Sosa9fe00b92010-10-22 12:34:16 -070079 FLAGS_vdisk_size=${MIN_VDISK_SIZE_FULL}
80 FLAGS_statefulfs_size=${MIN_STATEFUL_FS_SIZE_FULL}
Chris Sosacc09f842010-09-21 17:09:51 -070081fi
82
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053083IMAGES_DIR="${DEFAULT_BUILD_ROOT}/images/${FLAGS_board}"
84# Default to the most recent image
85if [ -z "${FLAGS_from}" ] ; then
Kees Cook84a4c7a2011-10-18 13:21:41 -070086 FLAGS_from="$(${SCRIPT_ROOT}/get_latest_image.sh --board=${FLAGS_board})"
Zelidrag Hornung42ca8182010-07-12 18:08:44 -070087else
88 pushd "${FLAGS_from}" && FLAGS_from=`pwd` && popd
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053089fi
90if [ -z "${FLAGS_to}" ] ; then
91 FLAGS_to="${FLAGS_from}"
92fi
93
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053094if [ ${FLAGS_test_image} -eq ${FLAGS_TRUE} ] ; then
Simon Glass142ca062011-02-09 13:39:43 -080095 # Make a test image - this returns the test filename in CHROMEOS_RETURN_VAL
96 prepare_test_image "${FLAGS_from}" "${CHROMEOS_IMAGE_NAME}"
97 SRC_IMAGE="${CHROMEOS_RETURN_VAL}"
98else
99 # Use the standard image
100 SRC_IMAGE="${FLAGS_from}/${CHROMEOS_IMAGE_NAME}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530101fi
102
103# Memory units are in MBs
Greg Spencer798d75f2011-02-01 22:04:49 -0800104TEMP_IMG="$(dirname "${SRC_IMAGE}")/vm_temp_image.bin"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530105
106# If we're not building for VMWare, don't build the vmx
107if [ "${FLAGS_format}" != "vmware" ]; then
108 FLAGS_make_vmx="${FLAGS_FALSE}"
109fi
110
111# Convert args to paths. Need eval to un-quote the string so that shell
112# chars like ~ are processed; just doing FOO=`readlink -f $FOO` won't work.
113FLAGS_from=`eval readlink -f $FLAGS_from`
114FLAGS_to=`eval readlink -f $FLAGS_to`
115
116# Split apart the partitions and make some new ones
117TEMP_DIR=$(mktemp -d)
118(cd "${TEMP_DIR}" &&
119 "${FLAGS_from}/unpack_partitions.sh" "${SRC_IMAGE}")
120
121# Fix the kernel command line
122TEMP_ESP="${TEMP_DIR}"/part_12
123TEMP_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
129 # If we have a stateful fs size specified create a new state partition
130 # of the specified size.
131 if [ "${FLAGS_statefulfs_size}" -ne -1 ]; then
132 STATEFUL_SIZE_BYTES=$((1024 * 1024 * ${FLAGS_statefulfs_size}))
133 original_image_size=$(stat -c%s "${TEMP_STATE}")
134 if [ "${original_image_size}" -gt "${STATEFUL_SIZE_BYTES}" ]; then
135 die "Cannot resize stateful image to smaller than original. Exiting."
136 fi
137
138 echo "Resizing stateful partition to ${FLAGS_statefulfs_size}MB"
Rahul Chaturvedie770e162010-07-15 00:35:11 +0530139 # Extend the original file size to the new size.
David James596520a2011-08-15 17:11:00 -0700140 sudo e2fsck -pf "${TEMP_STATE}"
141 sudo resize2fs "${TEMP_STATE}" ${FLAGS_statefulfs_size}M
Rahul Chaturvedie770e162010-07-15 00:35:11 +0530142 fi
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530143fi
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530144TEMP_PMBR="${TEMP_DIR}"/pmbr
145dd if="${SRC_IMAGE}" of="${TEMP_PMBR}" bs=512 count=1
146
147TEMP_MNT=$(mktemp -d)
Will Drewryefce6682010-07-23 19:43:27 -0500148TEMP_ESP_MNT=$(mktemp -d)
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530149cleanup() {
J. Richard Barnette8a7aa3e2012-02-15 17:34:04 -0800150 sudo umount "${TEMP_MNT}"
151 sudo umount "${TEMP_ESP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500152 rmdir "${TEMP_MNT}" "${TEMP_ESP_MNT}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530153}
154trap cleanup INT TERM EXIT
155mkdir -p "${TEMP_MNT}"
Will Drewryf929c302010-10-20 18:48:20 -0500156enable_rw_mount "${TEMP_ROOTFS}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530157sudo mount -o loop "${TEMP_ROOTFS}" "${TEMP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500158mkdir -p "${TEMP_ESP_MNT}"
159sudo mount -o loop "${TEMP_ESP}" "${TEMP_ESP_MNT}"
160
Stéphane Marchesin7e0b3d82011-03-29 06:58:17 -0700161sudo python "${SCRIPTS_DIR}/fixup_image_for_qemu.py" \
162 --mounted_dir="${TEMP_MNT}"
163
Will Drewry537caa92010-08-20 20:30:56 -0500164# Modify the unverified usb template which uses a default usb_disk of sdb3
165sudo sed -i -e 's/sdb3/sda3/g' "${TEMP_MNT}/boot/syslinux/usb.A.cfg"
166
167# Unmount everything prior to building a final image
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530168trap - INT TERM EXIT
169cleanup
170
Chris Sosacc09f842010-09-21 17:09:51 -0700171# TOOD(adlr): pick a size that will for sure accomodate the partitions.
Will Drewry537caa92010-08-20 20:30:56 -0500172dd if=/dev/zero of="${TEMP_IMG}" bs=1 count=1 \
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530173 seek=$((${FLAGS_vdisk_size} * 1024 * 1024 - 1))
174
Chris Sosacc09f842010-09-21 17:09:51 -0700175GPT_FULL="false"
176[ "${FLAGS_full}" -eq "${FLAGS_TRUE}" ] && GPT_FULL="true"
177
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530178# Set up the partition table
Tan Gao0d5f9482010-08-06 08:28:20 -0700179install_gpt "${TEMP_IMG}" "$(numsectors $TEMP_ROOTFS)" \
180 "$(numsectors $TEMP_STATE)" "${TEMP_PMBR}" "$(numsectors $TEMP_ESP)" \
Chris Sosacc09f842010-09-21 17:09:51 -0700181 "${GPT_FULL}" ${FLAGS_rootfs_partition_size}
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530182# Copy into the partition parts of the file
183dd if="${TEMP_ROOTFS}" of="${TEMP_IMG}" conv=notrunc bs=512 \
184 seek="${START_ROOTFS_A}"
185dd if="${TEMP_STATE}" of="${TEMP_IMG}" conv=notrunc bs=512 \
186 seek="${START_STATEFUL}"
187dd if="${TEMP_KERN}" of="${TEMP_IMG}" conv=notrunc bs=512 \
188 seek="${START_KERN_A}"
189dd if="${TEMP_ESP}" of="${TEMP_IMG}" conv=notrunc bs=512 \
190 seek="${START_ESP}"
191
Will Drewry537caa92010-08-20 20:30:56 -0500192# Make the built-image bootable and ensure that the legacy default usb boot
193# uses /dev/sda instead of /dev/sdb3.
194# NOTE: The TEMP_IMG must live in the same image dir as the original image
195# to operate automatically below.
196${SCRIPTS_DIR}/bin/cros_make_image_bootable $(dirname "${TEMP_IMG}") \
197 $(basename "${TEMP_IMG}") \
Arkaitz Ruiz Alvarez2bf88592011-07-07 15:47:31 -0700198 --usb_disk /dev/sda3 \
199 --force_developer_mode
Will Drewry537caa92010-08-20 20:30:56 -0500200
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530201echo Creating final image
202# Convert image to output format
203if [ "${FLAGS_format}" = "virtualbox" -o "${FLAGS_format}" = "qemu" ]; then
204 if [ "${FLAGS_format}" = "virtualbox" ]; then
Liam McLoughlinc9f1dab2011-11-29 18:17:26 +0000205 sudo VBoxManage convertdd "${TEMP_IMG}" "${FLAGS_to}/${FLAGS_vbox_disk}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530206 else
207 mv ${TEMP_IMG} ${FLAGS_to}/${DEFAULT_QEMU_IMAGE}
208 fi
209elif [ "${FLAGS_format}" = "vmware" ]; then
210 qemu-img convert -f raw "${TEMP_IMG}" \
211 -O vmdk "${FLAGS_to}/${FLAGS_vmdk}"
212else
Brian Harring7f175a52012-03-02 05:37:00 -0800213 die_notrace "Invalid format: ${FLAGS_format}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530214fi
215
216rm -rf "${TEMP_DIR}" "${TEMP_IMG}"
217if [ -z "${FLAGS_state_image}" ]; then
218 rm -f "${STATE_IMAGE}"
219fi
220
221echo "Created image at ${FLAGS_to}"
222
223# Generate the vmware config file
224# A good reference doc: http://www.sanbarrow.com/vmx.html
225VMX_CONFIG="#!/usr/bin/vmware
226.encoding = \"UTF-8\"
227config.version = \"8\"
228virtualHW.version = \"4\"
229memsize = \"${FLAGS_mem}\"
230ide0:0.present = \"TRUE\"
231ide0:0.fileName = \"${FLAGS_vmdk}\"
232ethernet0.present = \"TRUE\"
233usb.present = \"TRUE\"
234sound.present = \"TRUE\"
235sound.virtualDev = \"es1371\"
236displayName = \"Chromium OS\"
237guestOS = \"otherlinux\"
238ethernet0.addressType = \"generated\"
239floppy0.present = \"FALSE\""
240
241if [[ "${FLAGS_make_vmx}" = "${FLAGS_TRUE}" ]]; then
242 echo "${VMX_CONFIG}" > "${FLAGS_to}/${FLAGS_vmx}"
243 echo "Wrote the following config to: ${FLAGS_to}/${FLAGS_vmx}"
244 echo "${VMX_CONFIG}"
245fi
246
Olof Johansson3ed8d122010-09-27 13:29:40 -0500247
248if [ "${FLAGS_format}" == "qemu" ]; then
249 echo "If you have qemu-kvm installed, you can start the image by:"
Anush Elangovan0de6de62010-12-07 17:31:48 -0800250 echo "sudo kvm -m ${FLAGS_mem} -vga std -pidfile /tmp/kvm.pid -net nic,model=virtio " \
Chris Masone7d04c7a2010-11-09 08:24:13 -0800251 "-net user,hostfwd=tcp::9222-:22 \\"
Olof Johansson3ed8d122010-09-27 13:29:40 -0500252 echo " -hda ${FLAGS_to}/${DEFAULT_QEMU_IMAGE}"
253fi