blob: 087f2c9f97be832951f97a34554502450e7978d1 [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}"
Chris Sosa020aa692010-10-08 16:36:20 -0700166 sudo e2fsck -pf "${STATEFUL_LOOP_DEV}"
Rahul Chaturvedie770e162010-07-15 00:35:11 +0530167 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}"
Will Drewryf929c302010-10-20 18:48:20 -0500183enable_rw_mount "${TEMP_ROOTFS}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530184sudo mount -o loop "${TEMP_ROOTFS}" "${TEMP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500185mkdir -p "${TEMP_ESP_MNT}"
186sudo mount -o loop "${TEMP_ESP}" "${TEMP_ESP_MNT}"
187
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530188if [ "${FLAGS_format}" = "qemu" ]; then
Chris Sosacc09f842010-09-21 17:09:51 -0700189 sudo python "$(dirname $0)/fixup_image_for_qemu.py" \
190 --mounted_dir="${TEMP_MNT}" \
191 --enable_tablet=true
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530192else
Chris Sosacc09f842010-09-21 17:09:51 -0700193 sudo python "$(dirname $0)/fixup_image_for_qemu.py" \
194 --mounted_dir="${TEMP_MNT}" \
195 --enable_tablet=false
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530196fi
Will Drewry537caa92010-08-20 20:30:56 -0500197
198# Modify the unverified usb template which uses a default usb_disk of sdb3
199sudo sed -i -e 's/sdb3/sda3/g' "${TEMP_MNT}/boot/syslinux/usb.A.cfg"
200
201# Unmount everything prior to building a final image
Will Drewryefce6682010-07-23 19:43:27 -0500202sync
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530203trap - INT TERM EXIT
204cleanup
205
Chris Sosacc09f842010-09-21 17:09:51 -0700206# TOOD(adlr): pick a size that will for sure accomodate the partitions.
Will Drewry537caa92010-08-20 20:30:56 -0500207dd if=/dev/zero of="${TEMP_IMG}" bs=1 count=1 \
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530208 seek=$((${FLAGS_vdisk_size} * 1024 * 1024 - 1))
209
Chris Sosacc09f842010-09-21 17:09:51 -0700210GPT_FULL="false"
211[ "${FLAGS_full}" -eq "${FLAGS_TRUE}" ] && GPT_FULL="true"
212
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530213# Set up the partition table
Tan Gao0d5f9482010-08-06 08:28:20 -0700214install_gpt "${TEMP_IMG}" "$(numsectors $TEMP_ROOTFS)" \
215 "$(numsectors $TEMP_STATE)" "${TEMP_PMBR}" "$(numsectors $TEMP_ESP)" \
Chris Sosacc09f842010-09-21 17:09:51 -0700216 "${GPT_FULL}" ${FLAGS_rootfs_partition_size}
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530217# Copy into the partition parts of the file
218dd if="${TEMP_ROOTFS}" of="${TEMP_IMG}" conv=notrunc bs=512 \
219 seek="${START_ROOTFS_A}"
220dd if="${TEMP_STATE}" of="${TEMP_IMG}" conv=notrunc bs=512 \
221 seek="${START_STATEFUL}"
222dd if="${TEMP_KERN}" of="${TEMP_IMG}" conv=notrunc bs=512 \
223 seek="${START_KERN_A}"
224dd if="${TEMP_ESP}" of="${TEMP_IMG}" conv=notrunc bs=512 \
225 seek="${START_ESP}"
226
Will Drewry537caa92010-08-20 20:30:56 -0500227# Make the built-image bootable and ensure that the legacy default usb boot
228# uses /dev/sda instead of /dev/sdb3.
229# NOTE: The TEMP_IMG must live in the same image dir as the original image
230# to operate automatically below.
231${SCRIPTS_DIR}/bin/cros_make_image_bootable $(dirname "${TEMP_IMG}") \
232 $(basename "${TEMP_IMG}") \
233 --usb_disk /dev/sda3
234
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530235echo Creating final image
236# Convert image to output format
237if [ "${FLAGS_format}" = "virtualbox" -o "${FLAGS_format}" = "qemu" ]; then
238 if [ "${FLAGS_format}" = "virtualbox" ]; then
239 VBoxManage convertdd "${TEMP_IMG}" "${FLAGS_to}/${FLAGS_vbox_disk}"
240 else
241 mv ${TEMP_IMG} ${FLAGS_to}/${DEFAULT_QEMU_IMAGE}
242 fi
243elif [ "${FLAGS_format}" = "vmware" ]; then
244 qemu-img convert -f raw "${TEMP_IMG}" \
245 -O vmdk "${FLAGS_to}/${FLAGS_vmdk}"
246else
247 die "Invalid format: ${FLAGS_format}"
248fi
249
250rm -rf "${TEMP_DIR}" "${TEMP_IMG}"
251if [ -z "${FLAGS_state_image}" ]; then
252 rm -f "${STATE_IMAGE}"
253fi
254
255echo "Created image at ${FLAGS_to}"
256
257# Generate the vmware config file
258# A good reference doc: http://www.sanbarrow.com/vmx.html
259VMX_CONFIG="#!/usr/bin/vmware
260.encoding = \"UTF-8\"
261config.version = \"8\"
262virtualHW.version = \"4\"
263memsize = \"${FLAGS_mem}\"
264ide0:0.present = \"TRUE\"
265ide0:0.fileName = \"${FLAGS_vmdk}\"
266ethernet0.present = \"TRUE\"
267usb.present = \"TRUE\"
268sound.present = \"TRUE\"
269sound.virtualDev = \"es1371\"
270displayName = \"Chromium OS\"
271guestOS = \"otherlinux\"
272ethernet0.addressType = \"generated\"
273floppy0.present = \"FALSE\""
274
275if [[ "${FLAGS_make_vmx}" = "${FLAGS_TRUE}" ]]; then
276 echo "${VMX_CONFIG}" > "${FLAGS_to}/${FLAGS_vmx}"
277 echo "Wrote the following config to: ${FLAGS_to}/${FLAGS_vmx}"
278 echo "${VMX_CONFIG}"
279fi
280
Olof Johansson3ed8d122010-09-27 13:29:40 -0500281
282if [ "${FLAGS_format}" == "qemu" ]; then
283 echo "If you have qemu-kvm installed, you can start the image by:"
Olof Johansson1688a6d2010-10-14 19:08:44 -0500284 echo "sudo kvm -m ${FLAGS_mem} -vga std -pidfile /tmp/kvm.pid -net nic,model=e1000 " \
Olof Johansson3ed8d122010-09-27 13:29:40 -0500285 "-net user,hostfwd=tcp::922-:22 \\"
286 echo " -hda ${FLAGS_to}/${DEFAULT_QEMU_IMAGE}"
287fi