blob: cf665959e82165cde0f2c6a86d9b22b75cec0fe1 [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
10# Load common constants. This should be the first executable line.
11# The path to common.sh should be relative to your script's location.
12. "$(dirname "$0")/common.sh"
13. "$(dirname "$0")/chromeos-common.sh"
14
15get_default_board
16
17DEFAULT_VMDK="ide.vmdk"
18DEFAULT_VMX="chromiumos.vmx"
19DEFAULT_VBOX_DISK="os.vdi"
Zelidrag Hornung478a0d42010-07-14 11:47:39 -070020DEFAULT_QEMU_IMAGE="chromiumos_qemu_image.bin"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053021
22MOD_SCRIPTS_ROOT="${GCLIENT_ROOT}/src/scripts/mod_for_test_scripts"
23
24# Flags
25DEFINE_string board "${DEFAULT_BOARD}" \
26 "Board for which the image was built"
27DEFINE_boolean factory $FLAGS_FALSE \
28 "Modify the image for manufacturing testing"
29DEFINE_boolean factory_install $FLAGS_FALSE \
30 "Modify the image for factory install shim"
31DEFINE_boolean force_copy ${FLAGS_FALSE} "Always rebuild test image"
32DEFINE_string format "qemu" \
33 "Output format, either qemu, vmware or virtualbox"
34DEFINE_string from "" \
35 "Directory containing rootfs.image and mbr.image"
36DEFINE_boolean make_vmx ${FLAGS_TRUE} \
37 "Create a vmx file for use with vmplayer (vmware only)."
38DEFINE_integer mem "${DEFAULT_MEM}" \
39 "Memory size for the vm config in MBs (vmware only)."
40DEFINE_integer rootfs_partition_size 1024 \
41 "rootfs parition size in MBs."
42DEFINE_string state_image "" \
43 "Stateful partition image (defaults to creating new statful partition)"
Rahul Chaturvedie770e162010-07-15 00:35:11 +053044DEFINE_integer statefulfs_size -1 \
45 "Stateful partition size in MBs."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053046DEFINE_boolean test_image "${FLAGS_FALSE}" \
47 "Copies normal image to chromiumos_test_image.bin, modifies it for test."
48DEFINE_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)."
52DEFINE_integer vdisk_size 3072 \
53 "virtual disk size in MBs."
54DEFINE_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.
64set -e
65
66if [ -z "${FLAGS_board}" ] ; then
67 die "--board is required."
68fi
69
70IMAGES_DIR="${DEFAULT_BUILD_ROOT}/images/${FLAGS_board}"
71# Default to the most recent image
72if [ -z "${FLAGS_from}" ] ; then
73 FLAGS_from="${IMAGES_DIR}/$(ls -t $IMAGES_DIR | head -1)"
Zelidrag Hornung42ca8182010-07-12 18:08:44 -070074else
75 pushd "${FLAGS_from}" && FLAGS_from=`pwd` && popd
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053076fi
77if [ -z "${FLAGS_to}" ] ; then
78 FLAGS_to="${FLAGS_from}"
79fi
80
81# Use this image as the source image to copy
82SRC_IMAGE="${FLAGS_from}/chromiumos_image.bin"
83
84# If we're asked to modify the image for test, then let's make a copy and
85# modify that instead.
86if [ ${FLAGS_test_image} -eq ${FLAGS_TRUE} ] ; then
87 if [ ! -f "${FLAGS_from}/chromiumos_test_image.bin" ] || \
88 [ ${FLAGS_force_copy} -eq ${FLAGS_TRUE} ] ; then
89 # Copy it.
90 echo "Creating test image from original..."
91 cp -f "${SRC_IMAGE}" "${FLAGS_from}/chromiumos_test_image.bin"
92
93 # Check for manufacturing image.
94 if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ] ; then
95 EXTRA_ARGS="--factory"
96 fi
97
98 # Check for install shim.
99 if [ ${FLAGS_factory_install} -eq ${FLAGS_TRUE} ] ; then
100 EXTRA_ARGS="--factory_install"
101 fi
102
103 # Modify it. Pass --yes so that mod_image_for_test.sh won't ask us if we
104 # really want to modify the image; the user gave their assent already with
105 # --test-image and the original image is going to be preserved.
106 "${SCRIPTS_DIR}/mod_image_for_test.sh" --image \
107 "${FLAGS_from}/chromiumos_test_image.bin" ${EXTRA_ARGS} --yes
108 echo "Done with mod_image_for_test."
109 else
110 echo "Using cached test image."
111 fi
112 SRC_IMAGE="${FLAGS_from}/chromiumos_test_image.bin"
113 echo "Source test image is: ${SRC_IMAGE}"
114fi
115
116# Memory units are in MBs
117DEFAULT_MEM="1024"
Will Drewry537caa92010-08-20 20:30:56 -0500118TEMP_IMG="$(dirname ${SRC_IMAGE})/vm_temp_image.bin"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530119
120# If we're not building for VMWare, don't build the vmx
121if [ "${FLAGS_format}" != "vmware" ]; then
122 FLAGS_make_vmx="${FLAGS_FALSE}"
123fi
124
125# Convert args to paths. Need eval to un-quote the string so that shell
126# chars like ~ are processed; just doing FOO=`readlink -f $FOO` won't work.
127FLAGS_from=`eval readlink -f $FLAGS_from`
128FLAGS_to=`eval readlink -f $FLAGS_to`
129
130# Split apart the partitions and make some new ones
131TEMP_DIR=$(mktemp -d)
132(cd "${TEMP_DIR}" &&
133 "${FLAGS_from}/unpack_partitions.sh" "${SRC_IMAGE}")
134
135# Fix the kernel command line
136TEMP_ESP="${TEMP_DIR}"/part_12
137TEMP_ROOTFS="${TEMP_DIR}"/part_3
138TEMP_STATE="${TEMP_DIR}"/part_1
Will Drewryefce6682010-07-23 19:43:27 -0500139TEMP_KERN="${TEMP_DIR}"/part_2
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530140if [ -n "${FLAGS_state_image}" ]; then
141 TEMP_STATE="${FLAGS_state_image}"
Rahul Chaturvedie770e162010-07-15 00:35:11 +0530142else
143 # If we have a stateful fs size specified create a new state partition
144 # of the specified size.
145 if [ "${FLAGS_statefulfs_size}" -ne -1 ]; then
146 STATEFUL_SIZE_BYTES=$((1024 * 1024 * ${FLAGS_statefulfs_size}))
147 original_image_size=$(stat -c%s "${TEMP_STATE}")
148 if [ "${original_image_size}" -gt "${STATEFUL_SIZE_BYTES}" ]; then
149 die "Cannot resize stateful image to smaller than original. Exiting."
150 fi
151
152 echo "Resizing stateful partition to ${FLAGS_statefulfs_size}MB"
153 STATEFUL_LOOP_DEV=$(sudo losetup -f)
154 if [ -z "${STATEFUL_LOOP_DEV}" ]; then
155 die "No free loop device. Free up a loop device or reboot. Exiting."
156 fi
157
158 # Extend the original file size to the new size.
159 dd if=/dev/zero of="${TEMP_STATE}" bs=1 count=1 \
160 seek=$((STATEFUL_SIZE_BYTES - 1))
161 # Resize the partition.
162 sudo losetup "${STATEFUL_LOOP_DEV}" "${TEMP_STATE}"
163 sudo e2fsck -f "${STATEFUL_LOOP_DEV}"
164 sudo resize2fs "${STATEFUL_LOOP_DEV}"
165 sudo losetup -d "${STATEFUL_LOOP_DEV}"
166 fi
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530167fi
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530168TEMP_PMBR="${TEMP_DIR}"/pmbr
169dd if="${SRC_IMAGE}" of="${TEMP_PMBR}" bs=512 count=1
170
171TEMP_MNT=$(mktemp -d)
Will Drewryefce6682010-07-23 19:43:27 -0500172TEMP_ESP_MNT=$(mktemp -d)
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530173cleanup() {
174 sudo umount -d "${TEMP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500175 sudo umount -d "${TEMP_ESP_MNT}"
176 rmdir "${TEMP_MNT}" "${TEMP_ESP_MNT}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530177}
178trap cleanup INT TERM EXIT
179mkdir -p "${TEMP_MNT}"
180sudo mount -o loop "${TEMP_ROOTFS}" "${TEMP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500181mkdir -p "${TEMP_ESP_MNT}"
182sudo mount -o loop "${TEMP_ESP}" "${TEMP_ESP_MNT}"
183
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530184if [ "${FLAGS_format}" = "qemu" ]; then
185 sudo python ./fixup_image_for_qemu.py --mounted_dir="${TEMP_MNT}" \
Rahul Chaturvedie79e88e2010-07-21 02:04:59 +0530186 --enable_tablet=true
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530187else
188 sudo python ./fixup_image_for_qemu.py --mounted_dir="${TEMP_MNT}" \
Rahul Chaturvedie79e88e2010-07-21 02:04:59 +0530189 --enable_tablet=false
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530190fi
Will Drewry537caa92010-08-20 20:30:56 -0500191
192# Modify the unverified usb template which uses a default usb_disk of sdb3
193sudo sed -i -e 's/sdb3/sda3/g' "${TEMP_MNT}/boot/syslinux/usb.A.cfg"
194
195# Unmount everything prior to building a final image
Will Drewryefce6682010-07-23 19:43:27 -0500196sync
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530197trap - INT TERM EXIT
198cleanup
199
200# Make 3 GiB output image
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530201# TOOD(adlr): pick a size that will for sure accomodate the partitions
Will Drewry537caa92010-08-20 20:30:56 -0500202dd if=/dev/zero of="${TEMP_IMG}" bs=1 count=1 \
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530203 seek=$((${FLAGS_vdisk_size} * 1024 * 1024 - 1))
204
205# Set up the partition table
Tan Gao0d5f9482010-08-06 08:28:20 -0700206install_gpt "${TEMP_IMG}" "$(numsectors $TEMP_ROOTFS)" \
207 "$(numsectors $TEMP_STATE)" "${TEMP_PMBR}" "$(numsectors $TEMP_ESP)" \
208 false ${FLAGS_rootfs_partition_size}
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530209# Copy into the partition parts of the file
210dd if="${TEMP_ROOTFS}" of="${TEMP_IMG}" conv=notrunc bs=512 \
211 seek="${START_ROOTFS_A}"
212dd if="${TEMP_STATE}" of="${TEMP_IMG}" conv=notrunc bs=512 \
213 seek="${START_STATEFUL}"
214dd if="${TEMP_KERN}" of="${TEMP_IMG}" conv=notrunc bs=512 \
215 seek="${START_KERN_A}"
216dd if="${TEMP_ESP}" of="${TEMP_IMG}" conv=notrunc bs=512 \
217 seek="${START_ESP}"
218
Will Drewry537caa92010-08-20 20:30:56 -0500219# Make the built-image bootable and ensure that the legacy default usb boot
220# uses /dev/sda instead of /dev/sdb3.
221# NOTE: The TEMP_IMG must live in the same image dir as the original image
222# to operate automatically below.
223${SCRIPTS_DIR}/bin/cros_make_image_bootable $(dirname "${TEMP_IMG}") \
224 $(basename "${TEMP_IMG}") \
225 --usb_disk /dev/sda3
226
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530227echo Creating final image
228# Convert image to output format
229if [ "${FLAGS_format}" = "virtualbox" -o "${FLAGS_format}" = "qemu" ]; then
230 if [ "${FLAGS_format}" = "virtualbox" ]; then
231 VBoxManage convertdd "${TEMP_IMG}" "${FLAGS_to}/${FLAGS_vbox_disk}"
232 else
233 mv ${TEMP_IMG} ${FLAGS_to}/${DEFAULT_QEMU_IMAGE}
234 fi
235elif [ "${FLAGS_format}" = "vmware" ]; then
236 qemu-img convert -f raw "${TEMP_IMG}" \
237 -O vmdk "${FLAGS_to}/${FLAGS_vmdk}"
238else
239 die "Invalid format: ${FLAGS_format}"
240fi
241
242rm -rf "${TEMP_DIR}" "${TEMP_IMG}"
243if [ -z "${FLAGS_state_image}" ]; then
244 rm -f "${STATE_IMAGE}"
245fi
246
247echo "Created image at ${FLAGS_to}"
248
249# Generate the vmware config file
250# A good reference doc: http://www.sanbarrow.com/vmx.html
251VMX_CONFIG="#!/usr/bin/vmware
252.encoding = \"UTF-8\"
253config.version = \"8\"
254virtualHW.version = \"4\"
255memsize = \"${FLAGS_mem}\"
256ide0:0.present = \"TRUE\"
257ide0:0.fileName = \"${FLAGS_vmdk}\"
258ethernet0.present = \"TRUE\"
259usb.present = \"TRUE\"
260sound.present = \"TRUE\"
261sound.virtualDev = \"es1371\"
262displayName = \"Chromium OS\"
263guestOS = \"otherlinux\"
264ethernet0.addressType = \"generated\"
265floppy0.present = \"FALSE\""
266
267if [[ "${FLAGS_make_vmx}" = "${FLAGS_TRUE}" ]]; then
268 echo "${VMX_CONFIG}" > "${FLAGS_to}/${FLAGS_vmx}"
269 echo "Wrote the following config to: ${FLAGS_to}/${FLAGS_vmx}"
270 echo "${VMX_CONFIG}"
271fi
272