blob: dff302303f5caef887837550b14883efca73b17f [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
Greg Spencer798d75f2011-02-01 22:04:49 -080010# --- BEGIN COMMON.SH BOILERPLATE ---
11# Load common CrOS utilities. Inside the chroot this file is installed in
12# /usr/lib/crosutils. Outside the chroot we find it relative to the script's
13# location.
14find_common_sh() {
15 local common_paths=(/usr/lib/crosutils $(dirname "$(readlink -f "$0")"))
16 local path
17
18 SCRIPT_ROOT=
19 for path in "${common_paths[@]}"; do
20 if [ -r "${path}/common.sh" ]; then
21 SCRIPT_ROOT=${path}
22 break
23 fi
24 done
25}
26
27find_common_sh
28. "${SCRIPT_ROOT}/common.sh" || (echo "Unable to load common.sh" && exit 1)
29# --- END COMMON.SH BOILERPLATE ---
30
31# Need to be inside the chroot to load chromeos-common.sh
32assert_inside_chroot
33
34# Load functions and constants for chromeos-install
35. "/usr/lib/installer/chromeos-common.sh" || \
36 die "Unable to load /usr/lib/installer/chromeos-common.sh"
37
38. "${SCRIPT_ROOT}/lib/cros_vm_constants.sh" || \
39 die "Unable to load ${SCRIPT_ROOT}/lib/cros_vm_constants.sh"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053040
41get_default_board
42
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053043# Flags
44DEFINE_string board "${DEFAULT_BOARD}" \
45 "Board for which the image was built"
46DEFINE_boolean factory $FLAGS_FALSE \
47 "Modify the image for manufacturing testing"
48DEFINE_boolean factory_install $FLAGS_FALSE \
49 "Modify the image for factory install shim"
50DEFINE_boolean force_copy ${FLAGS_FALSE} "Always rebuild test image"
51DEFINE_string format "qemu" \
52 "Output format, either qemu, vmware or virtualbox"
53DEFINE_string from "" \
54 "Directory containing rootfs.image and mbr.image"
Chris Sosacc09f842010-09-21 17:09:51 -070055DEFINE_boolean full "${FLAGS_FALSE}" "Build full image with all partitions."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053056DEFINE_boolean make_vmx ${FLAGS_TRUE} \
57 "Create a vmx file for use with vmplayer (vmware only)."
58DEFINE_integer mem "${DEFAULT_MEM}" \
59 "Memory size for the vm config in MBs (vmware only)."
60DEFINE_integer rootfs_partition_size 1024 \
61 "rootfs parition size in MBs."
62DEFINE_string state_image "" \
63 "Stateful partition image (defaults to creating new statful partition)"
Chris Masone7d04c7a2010-11-09 08:24:13 -080064DEFINE_integer statefulfs_size 2048 \
Rahul Chaturvedie770e162010-07-15 00:35:11 +053065 "Stateful partition size in MBs."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053066DEFINE_boolean test_image "${FLAGS_FALSE}" \
67 "Copies normal image to chromiumos_test_image.bin, modifies it for test."
68DEFINE_string to "" \
69 "Destination folder for VM output file(s)"
70DEFINE_string vbox_disk "${DEFAULT_VBOX_DISK}" \
71 "Filename for the output disk (virtualbox only)."
72DEFINE_integer vdisk_size 3072 \
73 "virtual disk size in MBs."
74DEFINE_string vmdk "${DEFAULT_VMDK}" \
75 "Filename for the vmware disk image (vmware only)."
76DEFINE_string vmx "${DEFAULT_VMX}" \
77 "Filename for the vmware config (vmware only)."
78
79# Parse command line
80FLAGS "$@" || exit 1
81eval set -- "${FLAGS_ARGV}"
82
83# Die on any errors.
84set -e
85
86if [ -z "${FLAGS_board}" ] ; then
87 die "--board is required."
88fi
89
Chris Sosacc09f842010-09-21 17:09:51 -070090if [ "${FLAGS_full}" -eq "${FLAGS_TRUE}" ] && \
91 ( [[ ${FLAGS_vdisk_size} < ${MIN_VDISK_SIZE_FULL} ]] || \
92 [[ ${FLAGS_statefulfs_size} < ${MIN_STATEFUL_FS_SIZE_FULL} ]]); then
Chris Sosa9fe00b92010-10-22 12:34:16 -070093 warn "Disk is too small for full, using minimum: vdisk size equal to \
94${MIN_VDISK_SIZE_FULL} and statefulfs size equal to \
Chris Sosacc09f842010-09-21 17:09:51 -070095${MIN_STATEFUL_FS_SIZE_FULL}."
Chris Sosa9fe00b92010-10-22 12:34:16 -070096 FLAGS_vdisk_size=${MIN_VDISK_SIZE_FULL}
97 FLAGS_statefulfs_size=${MIN_STATEFUL_FS_SIZE_FULL}
Chris Sosacc09f842010-09-21 17:09:51 -070098fi
99
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530100IMAGES_DIR="${DEFAULT_BUILD_ROOT}/images/${FLAGS_board}"
101# Default to the most recent image
102if [ -z "${FLAGS_from}" ] ; then
Chris Sosabd613042010-10-04 13:29:23 -0700103 FLAGS_from="$(./get_latest_image.sh --board=${FLAGS_board})"
Zelidrag Hornung42ca8182010-07-12 18:08:44 -0700104else
105 pushd "${FLAGS_from}" && FLAGS_from=`pwd` && popd
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530106fi
107if [ -z "${FLAGS_to}" ] ; then
108 FLAGS_to="${FLAGS_from}"
109fi
110
111# Use this image as the source image to copy
112SRC_IMAGE="${FLAGS_from}/chromiumos_image.bin"
113
114# If we're asked to modify the image for test, then let's make a copy and
115# modify that instead.
116if [ ${FLAGS_test_image} -eq ${FLAGS_TRUE} ] ; then
117 if [ ! -f "${FLAGS_from}/chromiumos_test_image.bin" ] || \
118 [ ${FLAGS_force_copy} -eq ${FLAGS_TRUE} ] ; then
119 # Copy it.
120 echo "Creating test image from original..."
121 cp -f "${SRC_IMAGE}" "${FLAGS_from}/chromiumos_test_image.bin"
122
123 # Check for manufacturing image.
124 if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ] ; then
125 EXTRA_ARGS="--factory"
126 fi
127
128 # Check for install shim.
129 if [ ${FLAGS_factory_install} -eq ${FLAGS_TRUE} ] ; then
130 EXTRA_ARGS="--factory_install"
131 fi
132
133 # Modify it. Pass --yes so that mod_image_for_test.sh won't ask us if we
134 # really want to modify the image; the user gave their assent already with
135 # --test-image and the original image is going to be preserved.
Ryan Cairnsa7be5ff2010-08-23 20:47:07 -0700136 "${SCRIPTS_DIR}/mod_image_for_test.sh" --board=${FLAGS_board} --image \
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530137 "${FLAGS_from}/chromiumos_test_image.bin" ${EXTRA_ARGS} --yes
138 echo "Done with mod_image_for_test."
139 else
140 echo "Using cached test image."
141 fi
142 SRC_IMAGE="${FLAGS_from}/chromiumos_test_image.bin"
143 echo "Source test image is: ${SRC_IMAGE}"
144fi
145
146# Memory units are in MBs
Greg Spencer798d75f2011-02-01 22:04:49 -0800147TEMP_IMG="$(dirname "${SRC_IMAGE}")/vm_temp_image.bin"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530148
149# If we're not building for VMWare, don't build the vmx
150if [ "${FLAGS_format}" != "vmware" ]; then
151 FLAGS_make_vmx="${FLAGS_FALSE}"
152fi
153
154# Convert args to paths. Need eval to un-quote the string so that shell
155# chars like ~ are processed; just doing FOO=`readlink -f $FOO` won't work.
156FLAGS_from=`eval readlink -f $FLAGS_from`
157FLAGS_to=`eval readlink -f $FLAGS_to`
158
159# Split apart the partitions and make some new ones
160TEMP_DIR=$(mktemp -d)
161(cd "${TEMP_DIR}" &&
162 "${FLAGS_from}/unpack_partitions.sh" "${SRC_IMAGE}")
163
164# Fix the kernel command line
165TEMP_ESP="${TEMP_DIR}"/part_12
166TEMP_ROOTFS="${TEMP_DIR}"/part_3
167TEMP_STATE="${TEMP_DIR}"/part_1
Will Drewryefce6682010-07-23 19:43:27 -0500168TEMP_KERN="${TEMP_DIR}"/part_2
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530169if [ -n "${FLAGS_state_image}" ]; then
170 TEMP_STATE="${FLAGS_state_image}"
Rahul Chaturvedie770e162010-07-15 00:35:11 +0530171else
172 # If we have a stateful fs size specified create a new state partition
173 # of the specified size.
174 if [ "${FLAGS_statefulfs_size}" -ne -1 ]; then
175 STATEFUL_SIZE_BYTES=$((1024 * 1024 * ${FLAGS_statefulfs_size}))
176 original_image_size=$(stat -c%s "${TEMP_STATE}")
177 if [ "${original_image_size}" -gt "${STATEFUL_SIZE_BYTES}" ]; then
178 die "Cannot resize stateful image to smaller than original. Exiting."
179 fi
180
181 echo "Resizing stateful partition to ${FLAGS_statefulfs_size}MB"
182 STATEFUL_LOOP_DEV=$(sudo losetup -f)
183 if [ -z "${STATEFUL_LOOP_DEV}" ]; then
184 die "No free loop device. Free up a loop device or reboot. Exiting."
185 fi
186
187 # Extend the original file size to the new size.
188 dd if=/dev/zero of="${TEMP_STATE}" bs=1 count=1 \
189 seek=$((STATEFUL_SIZE_BYTES - 1))
190 # Resize the partition.
191 sudo losetup "${STATEFUL_LOOP_DEV}" "${TEMP_STATE}"
Chris Sosa020aa692010-10-08 16:36:20 -0700192 sudo e2fsck -pf "${STATEFUL_LOOP_DEV}"
Rahul Chaturvedie770e162010-07-15 00:35:11 +0530193 sudo resize2fs "${STATEFUL_LOOP_DEV}"
Chris Sosa1b11f382010-10-25 15:10:26 -0700194 sync
Rahul Chaturvedie770e162010-07-15 00:35:11 +0530195 sudo losetup -d "${STATEFUL_LOOP_DEV}"
196 fi
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530197fi
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530198TEMP_PMBR="${TEMP_DIR}"/pmbr
199dd if="${SRC_IMAGE}" of="${TEMP_PMBR}" bs=512 count=1
200
201TEMP_MNT=$(mktemp -d)
Will Drewryefce6682010-07-23 19:43:27 -0500202TEMP_ESP_MNT=$(mktemp -d)
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530203cleanup() {
204 sudo umount -d "${TEMP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500205 sudo umount -d "${TEMP_ESP_MNT}"
206 rmdir "${TEMP_MNT}" "${TEMP_ESP_MNT}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530207}
208trap cleanup INT TERM EXIT
209mkdir -p "${TEMP_MNT}"
Will Drewryf929c302010-10-20 18:48:20 -0500210enable_rw_mount "${TEMP_ROOTFS}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530211sudo mount -o loop "${TEMP_ROOTFS}" "${TEMP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500212mkdir -p "${TEMP_ESP_MNT}"
213sudo mount -o loop "${TEMP_ESP}" "${TEMP_ESP_MNT}"
214
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530215if [ "${FLAGS_format}" = "qemu" ]; then
Greg Spencer798d75f2011-02-01 22:04:49 -0800216 sudo python "${SCRIPTS_DIR}/fixup_image_for_qemu.py" \
Chris Sosacc09f842010-09-21 17:09:51 -0700217 --mounted_dir="${TEMP_MNT}" \
218 --enable_tablet=true
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530219else
Greg Spencer798d75f2011-02-01 22:04:49 -0800220 sudo python "${SCRIPTS_DIR}/fixup_image_for_qemu.py" \
Chris Sosacc09f842010-09-21 17:09:51 -0700221 --mounted_dir="${TEMP_MNT}" \
222 --enable_tablet=false
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530223fi
Will Drewry537caa92010-08-20 20:30:56 -0500224
225# Modify the unverified usb template which uses a default usb_disk of sdb3
226sudo sed -i -e 's/sdb3/sda3/g' "${TEMP_MNT}/boot/syslinux/usb.A.cfg"
227
228# Unmount everything prior to building a final image
Will Drewryefce6682010-07-23 19:43:27 -0500229sync
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530230trap - INT TERM EXIT
231cleanup
232
Chris Sosacc09f842010-09-21 17:09:51 -0700233# TOOD(adlr): pick a size that will for sure accomodate the partitions.
Will Drewry537caa92010-08-20 20:30:56 -0500234dd if=/dev/zero of="${TEMP_IMG}" bs=1 count=1 \
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530235 seek=$((${FLAGS_vdisk_size} * 1024 * 1024 - 1))
236
Chris Sosacc09f842010-09-21 17:09:51 -0700237GPT_FULL="false"
238[ "${FLAGS_full}" -eq "${FLAGS_TRUE}" ] && GPT_FULL="true"
239
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530240# Set up the partition table
Tan Gao0d5f9482010-08-06 08:28:20 -0700241install_gpt "${TEMP_IMG}" "$(numsectors $TEMP_ROOTFS)" \
242 "$(numsectors $TEMP_STATE)" "${TEMP_PMBR}" "$(numsectors $TEMP_ESP)" \
Chris Sosacc09f842010-09-21 17:09:51 -0700243 "${GPT_FULL}" ${FLAGS_rootfs_partition_size}
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530244# Copy into the partition parts of the file
245dd if="${TEMP_ROOTFS}" of="${TEMP_IMG}" conv=notrunc bs=512 \
246 seek="${START_ROOTFS_A}"
247dd if="${TEMP_STATE}" of="${TEMP_IMG}" conv=notrunc bs=512 \
248 seek="${START_STATEFUL}"
249dd if="${TEMP_KERN}" of="${TEMP_IMG}" conv=notrunc bs=512 \
250 seek="${START_KERN_A}"
251dd if="${TEMP_ESP}" of="${TEMP_IMG}" conv=notrunc bs=512 \
252 seek="${START_ESP}"
253
Will Drewry537caa92010-08-20 20:30:56 -0500254# Make the built-image bootable and ensure that the legacy default usb boot
255# uses /dev/sda instead of /dev/sdb3.
256# NOTE: The TEMP_IMG must live in the same image dir as the original image
257# to operate automatically below.
258${SCRIPTS_DIR}/bin/cros_make_image_bootable $(dirname "${TEMP_IMG}") \
259 $(basename "${TEMP_IMG}") \
260 --usb_disk /dev/sda3
261
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530262echo Creating final image
263# Convert image to output format
264if [ "${FLAGS_format}" = "virtualbox" -o "${FLAGS_format}" = "qemu" ]; then
265 if [ "${FLAGS_format}" = "virtualbox" ]; then
266 VBoxManage convertdd "${TEMP_IMG}" "${FLAGS_to}/${FLAGS_vbox_disk}"
267 else
268 mv ${TEMP_IMG} ${FLAGS_to}/${DEFAULT_QEMU_IMAGE}
269 fi
270elif [ "${FLAGS_format}" = "vmware" ]; then
271 qemu-img convert -f raw "${TEMP_IMG}" \
272 -O vmdk "${FLAGS_to}/${FLAGS_vmdk}"
273else
274 die "Invalid format: ${FLAGS_format}"
275fi
276
277rm -rf "${TEMP_DIR}" "${TEMP_IMG}"
278if [ -z "${FLAGS_state_image}" ]; then
279 rm -f "${STATE_IMAGE}"
280fi
281
282echo "Created image at ${FLAGS_to}"
283
284# Generate the vmware config file
285# A good reference doc: http://www.sanbarrow.com/vmx.html
286VMX_CONFIG="#!/usr/bin/vmware
287.encoding = \"UTF-8\"
288config.version = \"8\"
289virtualHW.version = \"4\"
290memsize = \"${FLAGS_mem}\"
291ide0:0.present = \"TRUE\"
292ide0:0.fileName = \"${FLAGS_vmdk}\"
293ethernet0.present = \"TRUE\"
294usb.present = \"TRUE\"
295sound.present = \"TRUE\"
296sound.virtualDev = \"es1371\"
297displayName = \"Chromium OS\"
298guestOS = \"otherlinux\"
299ethernet0.addressType = \"generated\"
300floppy0.present = \"FALSE\""
301
302if [[ "${FLAGS_make_vmx}" = "${FLAGS_TRUE}" ]]; then
303 echo "${VMX_CONFIG}" > "${FLAGS_to}/${FLAGS_vmx}"
304 echo "Wrote the following config to: ${FLAGS_to}/${FLAGS_vmx}"
305 echo "${VMX_CONFIG}"
306fi
307
Olof Johansson3ed8d122010-09-27 13:29:40 -0500308
309if [ "${FLAGS_format}" == "qemu" ]; then
310 echo "If you have qemu-kvm installed, you can start the image by:"
Anush Elangovan0de6de62010-12-07 17:31:48 -0800311 echo "sudo kvm -m ${FLAGS_mem} -vga std -pidfile /tmp/kvm.pid -net nic,model=virtio " \
Chris Masone7d04c7a2010-11-09 08:24:13 -0800312 "-net user,hostfwd=tcp::9222-:22 \\"
Olof Johansson3ed8d122010-09-27 13:29:40 -0500313 echo " -hda ${FLAGS_to}/${DEFAULT_QEMU_IMAGE}"
314fi