blob: 8450639276bf734ce20868f35523a97de3d381c0 [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"
20DEFAULT_QEMU_IMAGE="chromiumos_qemu.image"
21
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)"
44DEFINE_boolean test_image "${FLAGS_FALSE}" \
45 "Copies normal image to chromiumos_test_image.bin, modifies it for test."
46DEFINE_string to "" \
47 "Destination folder for VM output file(s)"
48DEFINE_string vbox_disk "${DEFAULT_VBOX_DISK}" \
49 "Filename for the output disk (virtualbox only)."
50DEFINE_integer vdisk_size 3072 \
51 "virtual disk size in MBs."
52DEFINE_string vmdk "${DEFAULT_VMDK}" \
53 "Filename for the vmware disk image (vmware only)."
54DEFINE_string vmx "${DEFAULT_VMX}" \
55 "Filename for the vmware config (vmware only)."
56
57# Parse command line
58FLAGS "$@" || exit 1
59eval set -- "${FLAGS_ARGV}"
60
61# Die on any errors.
62set -e
63
64if [ -z "${FLAGS_board}" ] ; then
65 die "--board is required."
66fi
67
68IMAGES_DIR="${DEFAULT_BUILD_ROOT}/images/${FLAGS_board}"
69# Default to the most recent image
70if [ -z "${FLAGS_from}" ] ; then
71 FLAGS_from="${IMAGES_DIR}/$(ls -t $IMAGES_DIR | head -1)"
72fi
73if [ -z "${FLAGS_to}" ] ; then
74 FLAGS_to="${FLAGS_from}"
75fi
76
77# Use this image as the source image to copy
78SRC_IMAGE="${FLAGS_from}/chromiumos_image.bin"
79
80# If we're asked to modify the image for test, then let's make a copy and
81# modify that instead.
82if [ ${FLAGS_test_image} -eq ${FLAGS_TRUE} ] ; then
83 if [ ! -f "${FLAGS_from}/chromiumos_test_image.bin" ] || \
84 [ ${FLAGS_force_copy} -eq ${FLAGS_TRUE} ] ; then
85 # Copy it.
86 echo "Creating test image from original..."
87 cp -f "${SRC_IMAGE}" "${FLAGS_from}/chromiumos_test_image.bin"
88
89 # Check for manufacturing image.
90 if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ] ; then
91 EXTRA_ARGS="--factory"
92 fi
93
94 # Check for install shim.
95 if [ ${FLAGS_factory_install} -eq ${FLAGS_TRUE} ] ; then
96 EXTRA_ARGS="--factory_install"
97 fi
98
99 # Modify it. Pass --yes so that mod_image_for_test.sh won't ask us if we
100 # really want to modify the image; the user gave their assent already with
101 # --test-image and the original image is going to be preserved.
102 "${SCRIPTS_DIR}/mod_image_for_test.sh" --image \
103 "${FLAGS_from}/chromiumos_test_image.bin" ${EXTRA_ARGS} --yes
104 echo "Done with mod_image_for_test."
105 else
106 echo "Using cached test image."
107 fi
108 SRC_IMAGE="${FLAGS_from}/chromiumos_test_image.bin"
109 echo "Source test image is: ${SRC_IMAGE}"
110fi
111
112# Memory units are in MBs
113DEFAULT_MEM="1024"
114TEMP_IMAGE="${IMAGES_DIR}/temp_image.img"
115
116
117# If we're not building for VMWare, don't build the vmx
118if [ "${FLAGS_format}" != "vmware" ]; then
119 FLAGS_make_vmx="${FLAGS_FALSE}"
120fi
121
122# Convert args to paths. Need eval to un-quote the string so that shell
123# chars like ~ are processed; just doing FOO=`readlink -f $FOO` won't work.
124FLAGS_from=`eval readlink -f $FLAGS_from`
125FLAGS_to=`eval readlink -f $FLAGS_to`
126
127# Split apart the partitions and make some new ones
128TEMP_DIR=$(mktemp -d)
129(cd "${TEMP_DIR}" &&
130 "${FLAGS_from}/unpack_partitions.sh" "${SRC_IMAGE}")
131
132# Fix the kernel command line
133TEMP_ESP="${TEMP_DIR}"/part_12
134TEMP_ROOTFS="${TEMP_DIR}"/part_3
135TEMP_STATE="${TEMP_DIR}"/part_1
136if [ -n "${FLAGS_state_image}" ]; then
137 TEMP_STATE="${FLAGS_state_image}"
138fi
139TEMP_KERN="${TEMP_DIR}"/part_2
140TEMP_PMBR="${TEMP_DIR}"/pmbr
141dd if="${SRC_IMAGE}" of="${TEMP_PMBR}" bs=512 count=1
142
143TEMP_MNT=$(mktemp -d)
144cleanup() {
145 sudo umount -d "${TEMP_MNT}"
146 rmdir "${TEMP_MNT}"
147}
148trap cleanup INT TERM EXIT
149mkdir -p "${TEMP_MNT}"
150sudo mount -o loop "${TEMP_ROOTFS}" "${TEMP_MNT}"
151if [ "${FLAGS_format}" = "qemu" ]; then
152 sudo python ./fixup_image_for_qemu.py --mounted_dir="${TEMP_MNT}" \
153 --for_qemu=true
154else
155 sudo python ./fixup_image_for_qemu.py --mounted_dir="${TEMP_MNT}" \
156 --for_qemu=false
157fi
158
159# Change this value if the rootfs partition changes
160ROOTFS_PARTITION=/dev/sda3
161sudo "${TEMP_MNT}"/postinst_vm "${ROOTFS_PARTITION}"
162trap - INT TERM EXIT
163cleanup
164
165# Make 3 GiB output image
166TEMP_IMG=$(mktemp)
167# TOOD(adlr): pick a size that will for sure accomodate the partitions
168sudo dd if=/dev/zero of="${TEMP_IMG}" bs=1 count=1 \
169 seek=$((${FLAGS_vdisk_size} * 1024 * 1024 - 1))
170
171# Set up the partition table
172install_gpt "${TEMP_IMG}" "${TEMP_ROOTFS}" "${TEMP_KERN}" "${TEMP_STATE}" \
173 "${TEMP_PMBR}" "${TEMP_ESP}" true false ${FLAGS_rootfs_partition_size}
174# Copy into the partition parts of the file
175dd if="${TEMP_ROOTFS}" of="${TEMP_IMG}" conv=notrunc bs=512 \
176 seek="${START_ROOTFS_A}"
177dd if="${TEMP_STATE}" of="${TEMP_IMG}" conv=notrunc bs=512 \
178 seek="${START_STATEFUL}"
179dd if="${TEMP_KERN}" of="${TEMP_IMG}" conv=notrunc bs=512 \
180 seek="${START_KERN_A}"
181dd if="${TEMP_ESP}" of="${TEMP_IMG}" conv=notrunc bs=512 \
182 seek="${START_ESP}"
183
184echo Creating final image
185# Convert image to output format
186if [ "${FLAGS_format}" = "virtualbox" -o "${FLAGS_format}" = "qemu" ]; then
187 if [ "${FLAGS_format}" = "virtualbox" ]; then
188 VBoxManage convertdd "${TEMP_IMG}" "${FLAGS_to}/${FLAGS_vbox_disk}"
189 else
190 mv ${TEMP_IMG} ${FLAGS_to}/${DEFAULT_QEMU_IMAGE}
191 fi
192elif [ "${FLAGS_format}" = "vmware" ]; then
193 qemu-img convert -f raw "${TEMP_IMG}" \
194 -O vmdk "${FLAGS_to}/${FLAGS_vmdk}"
195else
196 die "Invalid format: ${FLAGS_format}"
197fi
198
199rm -rf "${TEMP_DIR}" "${TEMP_IMG}"
200if [ -z "${FLAGS_state_image}" ]; then
201 rm -f "${STATE_IMAGE}"
202fi
203
204echo "Created image at ${FLAGS_to}"
205
206# Generate the vmware config file
207# A good reference doc: http://www.sanbarrow.com/vmx.html
208VMX_CONFIG="#!/usr/bin/vmware
209.encoding = \"UTF-8\"
210config.version = \"8\"
211virtualHW.version = \"4\"
212memsize = \"${FLAGS_mem}\"
213ide0:0.present = \"TRUE\"
214ide0:0.fileName = \"${FLAGS_vmdk}\"
215ethernet0.present = \"TRUE\"
216usb.present = \"TRUE\"
217sound.present = \"TRUE\"
218sound.virtualDev = \"es1371\"
219displayName = \"Chromium OS\"
220guestOS = \"otherlinux\"
221ethernet0.addressType = \"generated\"
222floppy0.present = \"FALSE\""
223
224if [[ "${FLAGS_make_vmx}" = "${FLAGS_TRUE}" ]]; then
225 echo "${VMX_CONFIG}" > "${FLAGS_to}/${FLAGS_vmx}"
226 echo "Wrote the following config to: ${FLAGS_to}/${FLAGS_vmx}"
227 echo "${VMX_CONFIG}"
228fi
229