blob: 50e00e5612b33ba87277d2fcdb34f67bcd0bd8c8 [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)"
Chris Masone7d04c7a2010-11-09 08:24:13 -080040DEFINE_integer statefulfs_size 2048 \
Rahul Chaturvedie770e162010-07-15 00:35:11 +053041 "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
Chris Sosa9fe00b92010-10-22 12:34:16 -070069 warn "Disk is too small for full, using minimum: vdisk size equal to \
70${MIN_VDISK_SIZE_FULL} and statefulfs size equal to \
Chris Sosacc09f842010-09-21 17:09:51 -070071${MIN_STATEFUL_FS_SIZE_FULL}."
Chris Sosa9fe00b92010-10-22 12:34:16 -070072 FLAGS_vdisk_size=${MIN_VDISK_SIZE_FULL}
73 FLAGS_statefulfs_size=${MIN_STATEFUL_FS_SIZE_FULL}
Chris Sosacc09f842010-09-21 17:09:51 -070074fi
75
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053076IMAGES_DIR="${DEFAULT_BUILD_ROOT}/images/${FLAGS_board}"
77# Default to the most recent image
78if [ -z "${FLAGS_from}" ] ; then
Chris Sosabd613042010-10-04 13:29:23 -070079 FLAGS_from="$(./get_latest_image.sh --board=${FLAGS_board})"
Zelidrag Hornung42ca8182010-07-12 18:08:44 -070080else
81 pushd "${FLAGS_from}" && FLAGS_from=`pwd` && popd
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053082fi
83if [ -z "${FLAGS_to}" ] ; then
84 FLAGS_to="${FLAGS_from}"
85fi
86
87# Use this image as the source image to copy
88SRC_IMAGE="${FLAGS_from}/chromiumos_image.bin"
89
90# If we're asked to modify the image for test, then let's make a copy and
91# modify that instead.
92if [ ${FLAGS_test_image} -eq ${FLAGS_TRUE} ] ; then
93 if [ ! -f "${FLAGS_from}/chromiumos_test_image.bin" ] || \
94 [ ${FLAGS_force_copy} -eq ${FLAGS_TRUE} ] ; then
95 # Copy it.
96 echo "Creating test image from original..."
97 cp -f "${SRC_IMAGE}" "${FLAGS_from}/chromiumos_test_image.bin"
98
99 # Check for manufacturing image.
100 if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ] ; then
101 EXTRA_ARGS="--factory"
102 fi
103
104 # Check for install shim.
105 if [ ${FLAGS_factory_install} -eq ${FLAGS_TRUE} ] ; then
106 EXTRA_ARGS="--factory_install"
107 fi
108
109 # Modify it. Pass --yes so that mod_image_for_test.sh won't ask us if we
110 # really want to modify the image; the user gave their assent already with
111 # --test-image and the original image is going to be preserved.
Ryan Cairnsa7be5ff2010-08-23 20:47:07 -0700112 "${SCRIPTS_DIR}/mod_image_for_test.sh" --board=${FLAGS_board} --image \
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530113 "${FLAGS_from}/chromiumos_test_image.bin" ${EXTRA_ARGS} --yes
114 echo "Done with mod_image_for_test."
115 else
116 echo "Using cached test image."
117 fi
118 SRC_IMAGE="${FLAGS_from}/chromiumos_test_image.bin"
119 echo "Source test image is: ${SRC_IMAGE}"
120fi
121
122# Memory units are in MBs
Will Drewry537caa92010-08-20 20:30:56 -0500123TEMP_IMG="$(dirname ${SRC_IMAGE})/vm_temp_image.bin"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530124
125# If we're not building for VMWare, don't build the vmx
126if [ "${FLAGS_format}" != "vmware" ]; then
127 FLAGS_make_vmx="${FLAGS_FALSE}"
128fi
129
130# Convert args to paths. Need eval to un-quote the string so that shell
131# chars like ~ are processed; just doing FOO=`readlink -f $FOO` won't work.
132FLAGS_from=`eval readlink -f $FLAGS_from`
133FLAGS_to=`eval readlink -f $FLAGS_to`
134
135# Split apart the partitions and make some new ones
136TEMP_DIR=$(mktemp -d)
137(cd "${TEMP_DIR}" &&
138 "${FLAGS_from}/unpack_partitions.sh" "${SRC_IMAGE}")
139
140# Fix the kernel command line
141TEMP_ESP="${TEMP_DIR}"/part_12
142TEMP_ROOTFS="${TEMP_DIR}"/part_3
143TEMP_STATE="${TEMP_DIR}"/part_1
Will Drewryefce6682010-07-23 19:43:27 -0500144TEMP_KERN="${TEMP_DIR}"/part_2
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530145if [ -n "${FLAGS_state_image}" ]; then
146 TEMP_STATE="${FLAGS_state_image}"
Rahul Chaturvedie770e162010-07-15 00:35:11 +0530147else
148 # If we have a stateful fs size specified create a new state partition
149 # of the specified size.
150 if [ "${FLAGS_statefulfs_size}" -ne -1 ]; then
151 STATEFUL_SIZE_BYTES=$((1024 * 1024 * ${FLAGS_statefulfs_size}))
152 original_image_size=$(stat -c%s "${TEMP_STATE}")
153 if [ "${original_image_size}" -gt "${STATEFUL_SIZE_BYTES}" ]; then
154 die "Cannot resize stateful image to smaller than original. Exiting."
155 fi
156
157 echo "Resizing stateful partition to ${FLAGS_statefulfs_size}MB"
158 STATEFUL_LOOP_DEV=$(sudo losetup -f)
159 if [ -z "${STATEFUL_LOOP_DEV}" ]; then
160 die "No free loop device. Free up a loop device or reboot. Exiting."
161 fi
162
163 # Extend the original file size to the new size.
164 dd if=/dev/zero of="${TEMP_STATE}" bs=1 count=1 \
165 seek=$((STATEFUL_SIZE_BYTES - 1))
166 # Resize the partition.
167 sudo losetup "${STATEFUL_LOOP_DEV}" "${TEMP_STATE}"
Chris Sosa020aa692010-10-08 16:36:20 -0700168 sudo e2fsck -pf "${STATEFUL_LOOP_DEV}"
Rahul Chaturvedie770e162010-07-15 00:35:11 +0530169 sudo resize2fs "${STATEFUL_LOOP_DEV}"
Chris Sosa1b11f382010-10-25 15:10:26 -0700170 sync
Rahul Chaturvedie770e162010-07-15 00:35:11 +0530171 sudo losetup -d "${STATEFUL_LOOP_DEV}"
172 fi
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530173fi
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530174TEMP_PMBR="${TEMP_DIR}"/pmbr
175dd if="${SRC_IMAGE}" of="${TEMP_PMBR}" bs=512 count=1
176
177TEMP_MNT=$(mktemp -d)
Will Drewryefce6682010-07-23 19:43:27 -0500178TEMP_ESP_MNT=$(mktemp -d)
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530179cleanup() {
180 sudo umount -d "${TEMP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500181 sudo umount -d "${TEMP_ESP_MNT}"
182 rmdir "${TEMP_MNT}" "${TEMP_ESP_MNT}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530183}
184trap cleanup INT TERM EXIT
185mkdir -p "${TEMP_MNT}"
Will Drewryf929c302010-10-20 18:48:20 -0500186enable_rw_mount "${TEMP_ROOTFS}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530187sudo mount -o loop "${TEMP_ROOTFS}" "${TEMP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500188mkdir -p "${TEMP_ESP_MNT}"
189sudo mount -o loop "${TEMP_ESP}" "${TEMP_ESP_MNT}"
190
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530191if [ "${FLAGS_format}" = "qemu" ]; then
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=true
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530195else
Chris Sosacc09f842010-09-21 17:09:51 -0700196 sudo python "$(dirname $0)/fixup_image_for_qemu.py" \
197 --mounted_dir="${TEMP_MNT}" \
198 --enable_tablet=false
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530199fi
Will Drewry537caa92010-08-20 20:30:56 -0500200
201# Modify the unverified usb template which uses a default usb_disk of sdb3
202sudo sed -i -e 's/sdb3/sda3/g' "${TEMP_MNT}/boot/syslinux/usb.A.cfg"
203
204# Unmount everything prior to building a final image
Will Drewryefce6682010-07-23 19:43:27 -0500205sync
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530206trap - INT TERM EXIT
207cleanup
208
Chris Sosacc09f842010-09-21 17:09:51 -0700209# TOOD(adlr): pick a size that will for sure accomodate the partitions.
Will Drewry537caa92010-08-20 20:30:56 -0500210dd if=/dev/zero of="${TEMP_IMG}" bs=1 count=1 \
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530211 seek=$((${FLAGS_vdisk_size} * 1024 * 1024 - 1))
212
Chris Sosacc09f842010-09-21 17:09:51 -0700213GPT_FULL="false"
214[ "${FLAGS_full}" -eq "${FLAGS_TRUE}" ] && GPT_FULL="true"
215
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530216# Set up the partition table
Tan Gao0d5f9482010-08-06 08:28:20 -0700217install_gpt "${TEMP_IMG}" "$(numsectors $TEMP_ROOTFS)" \
218 "$(numsectors $TEMP_STATE)" "${TEMP_PMBR}" "$(numsectors $TEMP_ESP)" \
Chris Sosacc09f842010-09-21 17:09:51 -0700219 "${GPT_FULL}" ${FLAGS_rootfs_partition_size}
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530220# Copy into the partition parts of the file
221dd if="${TEMP_ROOTFS}" of="${TEMP_IMG}" conv=notrunc bs=512 \
222 seek="${START_ROOTFS_A}"
223dd if="${TEMP_STATE}" of="${TEMP_IMG}" conv=notrunc bs=512 \
224 seek="${START_STATEFUL}"
225dd if="${TEMP_KERN}" of="${TEMP_IMG}" conv=notrunc bs=512 \
226 seek="${START_KERN_A}"
227dd if="${TEMP_ESP}" of="${TEMP_IMG}" conv=notrunc bs=512 \
228 seek="${START_ESP}"
229
Will Drewry537caa92010-08-20 20:30:56 -0500230# Make the built-image bootable and ensure that the legacy default usb boot
231# uses /dev/sda instead of /dev/sdb3.
232# NOTE: The TEMP_IMG must live in the same image dir as the original image
233# to operate automatically below.
234${SCRIPTS_DIR}/bin/cros_make_image_bootable $(dirname "${TEMP_IMG}") \
235 $(basename "${TEMP_IMG}") \
236 --usb_disk /dev/sda3
237
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530238echo Creating final image
239# Convert image to output format
240if [ "${FLAGS_format}" = "virtualbox" -o "${FLAGS_format}" = "qemu" ]; then
241 if [ "${FLAGS_format}" = "virtualbox" ]; then
242 VBoxManage convertdd "${TEMP_IMG}" "${FLAGS_to}/${FLAGS_vbox_disk}"
243 else
244 mv ${TEMP_IMG} ${FLAGS_to}/${DEFAULT_QEMU_IMAGE}
245 fi
246elif [ "${FLAGS_format}" = "vmware" ]; then
247 qemu-img convert -f raw "${TEMP_IMG}" \
248 -O vmdk "${FLAGS_to}/${FLAGS_vmdk}"
249else
250 die "Invalid format: ${FLAGS_format}"
251fi
252
253rm -rf "${TEMP_DIR}" "${TEMP_IMG}"
254if [ -z "${FLAGS_state_image}" ]; then
255 rm -f "${STATE_IMAGE}"
256fi
257
258echo "Created image at ${FLAGS_to}"
259
260# Generate the vmware config file
261# A good reference doc: http://www.sanbarrow.com/vmx.html
262VMX_CONFIG="#!/usr/bin/vmware
263.encoding = \"UTF-8\"
264config.version = \"8\"
265virtualHW.version = \"4\"
266memsize = \"${FLAGS_mem}\"
267ide0:0.present = \"TRUE\"
268ide0:0.fileName = \"${FLAGS_vmdk}\"
269ethernet0.present = \"TRUE\"
270usb.present = \"TRUE\"
271sound.present = \"TRUE\"
272sound.virtualDev = \"es1371\"
273displayName = \"Chromium OS\"
274guestOS = \"otherlinux\"
275ethernet0.addressType = \"generated\"
276floppy0.present = \"FALSE\""
277
278if [[ "${FLAGS_make_vmx}" = "${FLAGS_TRUE}" ]]; then
279 echo "${VMX_CONFIG}" > "${FLAGS_to}/${FLAGS_vmx}"
280 echo "Wrote the following config to: ${FLAGS_to}/${FLAGS_vmx}"
281 echo "${VMX_CONFIG}"
282fi
283
Olof Johansson3ed8d122010-09-27 13:29:40 -0500284
285if [ "${FLAGS_format}" == "qemu" ]; then
286 echo "If you have qemu-kvm installed, you can start the image by:"
Olof Johansson1688a6d2010-10-14 19:08:44 -0500287 echo "sudo kvm -m ${FLAGS_mem} -vga std -pidfile /tmp/kvm.pid -net nic,model=e1000 " \
Chris Masone7d04c7a2010-11-09 08:24:13 -0800288 "-net user,hostfwd=tcp::9222-:22 \\"
Olof Johansson3ed8d122010-09-27 13:29:40 -0500289 echo " -hda ${FLAGS_to}/${DEFAULT_QEMU_IMAGE}"
290fi