blob: fb786cbf8941e1be66c657fb30511286c29cd8d6 [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
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}"
170 sudo losetup -d "${STATEFUL_LOOP_DEV}"
171 fi
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530172fi
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530173TEMP_PMBR="${TEMP_DIR}"/pmbr
174dd if="${SRC_IMAGE}" of="${TEMP_PMBR}" bs=512 count=1
175
176TEMP_MNT=$(mktemp -d)
Will Drewryefce6682010-07-23 19:43:27 -0500177TEMP_ESP_MNT=$(mktemp -d)
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530178cleanup() {
179 sudo umount -d "${TEMP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500180 sudo umount -d "${TEMP_ESP_MNT}"
181 rmdir "${TEMP_MNT}" "${TEMP_ESP_MNT}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530182}
183trap cleanup INT TERM EXIT
184mkdir -p "${TEMP_MNT}"
Will Drewryf929c302010-10-20 18:48:20 -0500185enable_rw_mount "${TEMP_ROOTFS}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530186sudo mount -o loop "${TEMP_ROOTFS}" "${TEMP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500187mkdir -p "${TEMP_ESP_MNT}"
188sudo mount -o loop "${TEMP_ESP}" "${TEMP_ESP_MNT}"
189
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530190if [ "${FLAGS_format}" = "qemu" ]; then
Chris Sosacc09f842010-09-21 17:09:51 -0700191 sudo python "$(dirname $0)/fixup_image_for_qemu.py" \
192 --mounted_dir="${TEMP_MNT}" \
193 --enable_tablet=true
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530194else
Chris Sosacc09f842010-09-21 17:09:51 -0700195 sudo python "$(dirname $0)/fixup_image_for_qemu.py" \
196 --mounted_dir="${TEMP_MNT}" \
197 --enable_tablet=false
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530198fi
Will Drewry537caa92010-08-20 20:30:56 -0500199
200# Modify the unverified usb template which uses a default usb_disk of sdb3
201sudo sed -i -e 's/sdb3/sda3/g' "${TEMP_MNT}/boot/syslinux/usb.A.cfg"
202
203# Unmount everything prior to building a final image
Will Drewryefce6682010-07-23 19:43:27 -0500204sync
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530205trap - INT TERM EXIT
206cleanup
207
Chris Sosacc09f842010-09-21 17:09:51 -0700208# TOOD(adlr): pick a size that will for sure accomodate the partitions.
Will Drewry537caa92010-08-20 20:30:56 -0500209dd if=/dev/zero of="${TEMP_IMG}" bs=1 count=1 \
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530210 seek=$((${FLAGS_vdisk_size} * 1024 * 1024 - 1))
211
Chris Sosacc09f842010-09-21 17:09:51 -0700212GPT_FULL="false"
213[ "${FLAGS_full}" -eq "${FLAGS_TRUE}" ] && GPT_FULL="true"
214
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530215# Set up the partition table
Tan Gao0d5f9482010-08-06 08:28:20 -0700216install_gpt "${TEMP_IMG}" "$(numsectors $TEMP_ROOTFS)" \
217 "$(numsectors $TEMP_STATE)" "${TEMP_PMBR}" "$(numsectors $TEMP_ESP)" \
Chris Sosacc09f842010-09-21 17:09:51 -0700218 "${GPT_FULL}" ${FLAGS_rootfs_partition_size}
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530219# Copy into the partition parts of the file
220dd if="${TEMP_ROOTFS}" of="${TEMP_IMG}" conv=notrunc bs=512 \
221 seek="${START_ROOTFS_A}"
222dd if="${TEMP_STATE}" of="${TEMP_IMG}" conv=notrunc bs=512 \
223 seek="${START_STATEFUL}"
224dd if="${TEMP_KERN}" of="${TEMP_IMG}" conv=notrunc bs=512 \
225 seek="${START_KERN_A}"
226dd if="${TEMP_ESP}" of="${TEMP_IMG}" conv=notrunc bs=512 \
227 seek="${START_ESP}"
228
Will Drewry537caa92010-08-20 20:30:56 -0500229# Make the built-image bootable and ensure that the legacy default usb boot
230# uses /dev/sda instead of /dev/sdb3.
231# NOTE: The TEMP_IMG must live in the same image dir as the original image
232# to operate automatically below.
233${SCRIPTS_DIR}/bin/cros_make_image_bootable $(dirname "${TEMP_IMG}") \
234 $(basename "${TEMP_IMG}") \
235 --usb_disk /dev/sda3
236
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530237echo Creating final image
238# Convert image to output format
239if [ "${FLAGS_format}" = "virtualbox" -o "${FLAGS_format}" = "qemu" ]; then
240 if [ "${FLAGS_format}" = "virtualbox" ]; then
241 VBoxManage convertdd "${TEMP_IMG}" "${FLAGS_to}/${FLAGS_vbox_disk}"
242 else
243 mv ${TEMP_IMG} ${FLAGS_to}/${DEFAULT_QEMU_IMAGE}
244 fi
245elif [ "${FLAGS_format}" = "vmware" ]; then
246 qemu-img convert -f raw "${TEMP_IMG}" \
247 -O vmdk "${FLAGS_to}/${FLAGS_vmdk}"
248else
249 die "Invalid format: ${FLAGS_format}"
250fi
251
252rm -rf "${TEMP_DIR}" "${TEMP_IMG}"
253if [ -z "${FLAGS_state_image}" ]; then
254 rm -f "${STATE_IMAGE}"
255fi
256
257echo "Created image at ${FLAGS_to}"
258
259# Generate the vmware config file
260# A good reference doc: http://www.sanbarrow.com/vmx.html
261VMX_CONFIG="#!/usr/bin/vmware
262.encoding = \"UTF-8\"
263config.version = \"8\"
264virtualHW.version = \"4\"
265memsize = \"${FLAGS_mem}\"
266ide0:0.present = \"TRUE\"
267ide0:0.fileName = \"${FLAGS_vmdk}\"
268ethernet0.present = \"TRUE\"
269usb.present = \"TRUE\"
270sound.present = \"TRUE\"
271sound.virtualDev = \"es1371\"
272displayName = \"Chromium OS\"
273guestOS = \"otherlinux\"
274ethernet0.addressType = \"generated\"
275floppy0.present = \"FALSE\""
276
277if [[ "${FLAGS_make_vmx}" = "${FLAGS_TRUE}" ]]; then
278 echo "${VMX_CONFIG}" > "${FLAGS_to}/${FLAGS_vmx}"
279 echo "Wrote the following config to: ${FLAGS_to}/${FLAGS_vmx}"
280 echo "${VMX_CONFIG}"
281fi
282
Olof Johansson3ed8d122010-09-27 13:29:40 -0500283
284if [ "${FLAGS_format}" == "qemu" ]; then
285 echo "If you have qemu-kvm installed, you can start the image by:"
Olof Johansson1688a6d2010-10-14 19:08:44 -0500286 echo "sudo kvm -m ${FLAGS_mem} -vga std -pidfile /tmp/kvm.pid -net nic,model=e1000 " \
Olof Johansson3ed8d122010-09-27 13:29:40 -0500287 "-net user,hostfwd=tcp::922-:22 \\"
288 echo " -hda ${FLAGS_to}/${DEFAULT_QEMU_IMAGE}"
289fi