blob: f0c6e654fa23a945800617ef0b95f19b38da343b [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"
Chris Sosa45bec932010-09-29 15:38:53 -070014. "$(dirname "$0")/lib/cros_vm_constants.sh"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053015
16get_default_board
Chris Sosacc09f842010-09-21 17:09:51 -070017assert_inside_chroot
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053018
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053019# Flags
20DEFINE_string board "${DEFAULT_BOARD}" \
21 "Board for which the image was built"
22DEFINE_boolean factory $FLAGS_FALSE \
23 "Modify the image for manufacturing testing"
24DEFINE_boolean factory_install $FLAGS_FALSE \
25 "Modify the image for factory install shim"
26DEFINE_boolean force_copy ${FLAGS_FALSE} "Always rebuild test image"
27DEFINE_string format "qemu" \
28 "Output format, either qemu, vmware or virtualbox"
29DEFINE_string from "" \
30 "Directory containing rootfs.image and mbr.image"
Chris Sosacc09f842010-09-21 17:09:51 -070031DEFINE_boolean full "${FLAGS_FALSE}" "Build full image with all partitions."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053032DEFINE_boolean make_vmx ${FLAGS_TRUE} \
33 "Create a vmx file for use with vmplayer (vmware only)."
34DEFINE_integer mem "${DEFAULT_MEM}" \
35 "Memory size for the vm config in MBs (vmware only)."
36DEFINE_integer rootfs_partition_size 1024 \
37 "rootfs parition size in MBs."
38DEFINE_string state_image "" \
39 "Stateful partition image (defaults to creating new statful partition)"
Rahul Chaturvedie770e162010-07-15 00:35:11 +053040DEFINE_integer statefulfs_size -1 \
41 "Stateful partition size in MBs."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053042DEFINE_boolean test_image "${FLAGS_FALSE}" \
43 "Copies normal image to chromiumos_test_image.bin, modifies it for test."
44DEFINE_string to "" \
45 "Destination folder for VM output file(s)"
46DEFINE_string vbox_disk "${DEFAULT_VBOX_DISK}" \
47 "Filename for the output disk (virtualbox only)."
48DEFINE_integer vdisk_size 3072 \
49 "virtual disk size in MBs."
50DEFINE_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
55# Parse command line
56FLAGS "$@" || exit 1
57eval set -- "${FLAGS_ARGV}"
58
59# Die on any errors.
60set -e
61
62if [ -z "${FLAGS_board}" ] ; then
63 die "--board is required."
64fi
65
Chris Sosacc09f842010-09-21 17:09:51 -070066if [ "${FLAGS_full}" -eq "${FLAGS_TRUE}" ] && \
67 ( [[ ${FLAGS_vdisk_size} < ${MIN_VDISK_SIZE_FULL} ]] || \
68 [[ ${FLAGS_statefulfs_size} < ${MIN_STATEFUL_FS_SIZE_FULL} ]]); then
69 die "Disk is too small for full, please select a vdisk size greater than \
70${MIN_VDISK_SIZE_FULL} and statefulfs size greater than \
71${MIN_STATEFUL_FS_SIZE_FULL}."
72fi
73
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053074IMAGES_DIR="${DEFAULT_BUILD_ROOT}/images/${FLAGS_board}"
75# Default to the most recent image
76if [ -z "${FLAGS_from}" ] ; then
Chris Sosabd613042010-10-04 13:29:23 -070077 FLAGS_from="$(./get_latest_image.sh --board=${FLAGS_board})"
Zelidrag Hornung42ca8182010-07-12 18:08:44 -070078else
79 pushd "${FLAGS_from}" && FLAGS_from=`pwd` && popd
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053080fi
81if [ -z "${FLAGS_to}" ] ; then
82 FLAGS_to="${FLAGS_from}"
83fi
84
85# Use this image as the source image to copy
86SRC_IMAGE="${FLAGS_from}/chromiumos_image.bin"
87
88# If we're asked to modify the image for test, then let's make a copy and
89# modify that instead.
90if [ ${FLAGS_test_image} -eq ${FLAGS_TRUE} ] ; then
91 if [ ! -f "${FLAGS_from}/chromiumos_test_image.bin" ] || \
92 [ ${FLAGS_force_copy} -eq ${FLAGS_TRUE} ] ; then
93 # Copy it.
94 echo "Creating test image from original..."
95 cp -f "${SRC_IMAGE}" "${FLAGS_from}/chromiumos_test_image.bin"
96
97 # Check for manufacturing image.
98 if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ] ; then
99 EXTRA_ARGS="--factory"
100 fi
101
102 # Check for install shim.
103 if [ ${FLAGS_factory_install} -eq ${FLAGS_TRUE} ] ; then
104 EXTRA_ARGS="--factory_install"
105 fi
106
107 # Modify it. Pass --yes so that mod_image_for_test.sh won't ask us if we
108 # really want to modify the image; the user gave their assent already with
109 # --test-image and the original image is going to be preserved.
Ryan Cairnsa7be5ff2010-08-23 20:47:07 -0700110 "${SCRIPTS_DIR}/mod_image_for_test.sh" --board=${FLAGS_board} --image \
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530111 "${FLAGS_from}/chromiumos_test_image.bin" ${EXTRA_ARGS} --yes
112 echo "Done with mod_image_for_test."
113 else
114 echo "Using cached test image."
115 fi
116 SRC_IMAGE="${FLAGS_from}/chromiumos_test_image.bin"
117 echo "Source test image is: ${SRC_IMAGE}"
118fi
119
120# Memory units are in MBs
Will Drewry537caa92010-08-20 20:30:56 -0500121TEMP_IMG="$(dirname ${SRC_IMAGE})/vm_temp_image.bin"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530122
123# If we're not building for VMWare, don't build the vmx
124if [ "${FLAGS_format}" != "vmware" ]; then
125 FLAGS_make_vmx="${FLAGS_FALSE}"
126fi
127
128# Convert args to paths. Need eval to un-quote the string so that shell
129# chars like ~ are processed; just doing FOO=`readlink -f $FOO` won't work.
130FLAGS_from=`eval readlink -f $FLAGS_from`
131FLAGS_to=`eval readlink -f $FLAGS_to`
132
133# Split apart the partitions and make some new ones
134TEMP_DIR=$(mktemp -d)
135(cd "${TEMP_DIR}" &&
136 "${FLAGS_from}/unpack_partitions.sh" "${SRC_IMAGE}")
137
138# Fix the kernel command line
139TEMP_ESP="${TEMP_DIR}"/part_12
140TEMP_ROOTFS="${TEMP_DIR}"/part_3
141TEMP_STATE="${TEMP_DIR}"/part_1
Will Drewryefce6682010-07-23 19:43:27 -0500142TEMP_KERN="${TEMP_DIR}"/part_2
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530143if [ -n "${FLAGS_state_image}" ]; then
144 TEMP_STATE="${FLAGS_state_image}"
Rahul Chaturvedie770e162010-07-15 00:35:11 +0530145else
146 # If we have a stateful fs size specified create a new state partition
147 # of the specified size.
148 if [ "${FLAGS_statefulfs_size}" -ne -1 ]; then
149 STATEFUL_SIZE_BYTES=$((1024 * 1024 * ${FLAGS_statefulfs_size}))
150 original_image_size=$(stat -c%s "${TEMP_STATE}")
151 if [ "${original_image_size}" -gt "${STATEFUL_SIZE_BYTES}" ]; then
152 die "Cannot resize stateful image to smaller than original. Exiting."
153 fi
154
155 echo "Resizing stateful partition to ${FLAGS_statefulfs_size}MB"
156 STATEFUL_LOOP_DEV=$(sudo losetup -f)
157 if [ -z "${STATEFUL_LOOP_DEV}" ]; then
158 die "No free loop device. Free up a loop device or reboot. Exiting."
159 fi
160
161 # Extend the original file size to the new size.
162 dd if=/dev/zero of="${TEMP_STATE}" bs=1 count=1 \
163 seek=$((STATEFUL_SIZE_BYTES - 1))
164 # Resize the partition.
165 sudo losetup "${STATEFUL_LOOP_DEV}" "${TEMP_STATE}"
166 sudo e2fsck -f "${STATEFUL_LOOP_DEV}"
167 sudo resize2fs "${STATEFUL_LOOP_DEV}"
168 sudo losetup -d "${STATEFUL_LOOP_DEV}"
169 fi
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530170fi
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530171TEMP_PMBR="${TEMP_DIR}"/pmbr
172dd if="${SRC_IMAGE}" of="${TEMP_PMBR}" bs=512 count=1
173
174TEMP_MNT=$(mktemp -d)
Will Drewryefce6682010-07-23 19:43:27 -0500175TEMP_ESP_MNT=$(mktemp -d)
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530176cleanup() {
177 sudo umount -d "${TEMP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500178 sudo umount -d "${TEMP_ESP_MNT}"
179 rmdir "${TEMP_MNT}" "${TEMP_ESP_MNT}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530180}
181trap cleanup INT TERM EXIT
182mkdir -p "${TEMP_MNT}"
183sudo mount -o loop "${TEMP_ROOTFS}" "${TEMP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500184mkdir -p "${TEMP_ESP_MNT}"
185sudo mount -o loop "${TEMP_ESP}" "${TEMP_ESP_MNT}"
186
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530187if [ "${FLAGS_format}" = "qemu" ]; then
Chris Sosacc09f842010-09-21 17:09:51 -0700188 sudo python "$(dirname $0)/fixup_image_for_qemu.py" \
189 --mounted_dir="${TEMP_MNT}" \
190 --enable_tablet=true
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530191else
Chris Sosacc09f842010-09-21 17:09:51 -0700192 sudo python "$(dirname $0)/fixup_image_for_qemu.py" \
193 --mounted_dir="${TEMP_MNT}" \
194 --enable_tablet=false
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530195fi
Will Drewry537caa92010-08-20 20:30:56 -0500196
197# Modify the unverified usb template which uses a default usb_disk of sdb3
198sudo sed -i -e 's/sdb3/sda3/g' "${TEMP_MNT}/boot/syslinux/usb.A.cfg"
199
200# Unmount everything prior to building a final image
Will Drewryefce6682010-07-23 19:43:27 -0500201sync
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530202trap - INT TERM EXIT
203cleanup
204
Chris Sosacc09f842010-09-21 17:09:51 -0700205# TOOD(adlr): pick a size that will for sure accomodate the partitions.
Will Drewry537caa92010-08-20 20:30:56 -0500206dd if=/dev/zero of="${TEMP_IMG}" bs=1 count=1 \
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530207 seek=$((${FLAGS_vdisk_size} * 1024 * 1024 - 1))
208
Chris Sosacc09f842010-09-21 17:09:51 -0700209GPT_FULL="false"
210[ "${FLAGS_full}" -eq "${FLAGS_TRUE}" ] && GPT_FULL="true"
211
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530212# Set up the partition table
Tan Gao0d5f9482010-08-06 08:28:20 -0700213install_gpt "${TEMP_IMG}" "$(numsectors $TEMP_ROOTFS)" \
214 "$(numsectors $TEMP_STATE)" "${TEMP_PMBR}" "$(numsectors $TEMP_ESP)" \
Chris Sosacc09f842010-09-21 17:09:51 -0700215 "${GPT_FULL}" ${FLAGS_rootfs_partition_size}
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530216# Copy into the partition parts of the file
217dd if="${TEMP_ROOTFS}" of="${TEMP_IMG}" conv=notrunc bs=512 \
218 seek="${START_ROOTFS_A}"
219dd if="${TEMP_STATE}" of="${TEMP_IMG}" conv=notrunc bs=512 \
220 seek="${START_STATEFUL}"
221dd if="${TEMP_KERN}" of="${TEMP_IMG}" conv=notrunc bs=512 \
222 seek="${START_KERN_A}"
223dd if="${TEMP_ESP}" of="${TEMP_IMG}" conv=notrunc bs=512 \
224 seek="${START_ESP}"
225
Will Drewry537caa92010-08-20 20:30:56 -0500226# Make the built-image bootable and ensure that the legacy default usb boot
227# uses /dev/sda instead of /dev/sdb3.
228# NOTE: The TEMP_IMG must live in the same image dir as the original image
229# to operate automatically below.
230${SCRIPTS_DIR}/bin/cros_make_image_bootable $(dirname "${TEMP_IMG}") \
231 $(basename "${TEMP_IMG}") \
232 --usb_disk /dev/sda3
233
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530234echo Creating final image
235# Convert image to output format
236if [ "${FLAGS_format}" = "virtualbox" -o "${FLAGS_format}" = "qemu" ]; then
237 if [ "${FLAGS_format}" = "virtualbox" ]; then
238 VBoxManage convertdd "${TEMP_IMG}" "${FLAGS_to}/${FLAGS_vbox_disk}"
239 else
240 mv ${TEMP_IMG} ${FLAGS_to}/${DEFAULT_QEMU_IMAGE}
241 fi
242elif [ "${FLAGS_format}" = "vmware" ]; then
243 qemu-img convert -f raw "${TEMP_IMG}" \
244 -O vmdk "${FLAGS_to}/${FLAGS_vmdk}"
245else
246 die "Invalid format: ${FLAGS_format}"
247fi
248
249rm -rf "${TEMP_DIR}" "${TEMP_IMG}"
250if [ -z "${FLAGS_state_image}" ]; then
251 rm -f "${STATE_IMAGE}"
252fi
253
254echo "Created image at ${FLAGS_to}"
255
256# Generate the vmware config file
257# A good reference doc: http://www.sanbarrow.com/vmx.html
258VMX_CONFIG="#!/usr/bin/vmware
259.encoding = \"UTF-8\"
260config.version = \"8\"
261virtualHW.version = \"4\"
262memsize = \"${FLAGS_mem}\"
263ide0:0.present = \"TRUE\"
264ide0:0.fileName = \"${FLAGS_vmdk}\"
265ethernet0.present = \"TRUE\"
266usb.present = \"TRUE\"
267sound.present = \"TRUE\"
268sound.virtualDev = \"es1371\"
269displayName = \"Chromium OS\"
270guestOS = \"otherlinux\"
271ethernet0.addressType = \"generated\"
272floppy0.present = \"FALSE\""
273
274if [[ "${FLAGS_make_vmx}" = "${FLAGS_TRUE}" ]]; then
275 echo "${VMX_CONFIG}" > "${FLAGS_to}/${FLAGS_vmx}"
276 echo "Wrote the following config to: ${FLAGS_to}/${FLAGS_vmx}"
277 echo "${VMX_CONFIG}"
278fi
279
Olof Johansson3ed8d122010-09-27 13:29:40 -0500280
281if [ "${FLAGS_format}" == "qemu" ]; then
282 echo "If you have qemu-kvm installed, you can start the image by:"
283 echo "sudo kvm -m ${FLAGS_mem} -vga std -pidfile /tmp/kvm.pid -net nic " \
284 "-net user,hostfwd=tcp::922-:22 \\"
285 echo " -hda ${FLAGS_to}/${DEFAULT_QEMU_IMAGE}"
286fi