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