blob: 65a68df641885c31a96b1faba5ea8d90c1532087 [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
Kees Cook84a4c7a2011-10-18 13:21:41 -070010# Helper scripts should be run from the same location as this script.
11SCRIPT_ROOT=$(dirname "$(readlink -f "$0")")
David James359d3e12012-07-10 13:09:48 -070012. "${SCRIPT_ROOT}/common.sh" || exit 1
Liam McLoughlin5b37c542012-08-16 11:09:37 -070013. "${SCRIPT_ROOT}/build_library/disk_layout_util.sh" || exit 1
14. "${SCRIPT_ROOT}/build_library/build_common.sh" || exit 1
Greg Spencer798d75f2011-02-01 22:04:49 -080015
16# Need to be inside the chroot to load chromeos-common.sh
17assert_inside_chroot
18
19# Load functions and constants for chromeos-install
Gwendal Grignou0f618492014-04-07 20:05:58 +000020. /usr/share/misc/chromeos-common.sh || exit 1
David James359d3e12012-07-10 13:09:48 -070021. "${SCRIPT_ROOT}/lib/cros_vm_constants.sh" || exit 1
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053022
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053023# Flags
Liam McLoughlin12a9a842012-10-10 10:37:06 -040024DEFINE_string adjust_part "" \
25 "Adjustments to apply to the partition table"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053026DEFINE_string board "${DEFAULT_BOARD}" \
27 "Board for which the image was built"
28DEFINE_boolean factory $FLAGS_FALSE \
29 "Modify the image for manufacturing testing"
30DEFINE_boolean factory_install $FLAGS_FALSE \
31 "Modify the image for factory install shim"
Simon Glass142ca062011-02-09 13:39:43 -080032
Gabe Black1d6dc252014-08-18 17:02:09 -070033# We default to TRUE so the buildbot gets its image.
Chris Sosa8dad50d2011-02-14 15:29:32 -080034DEFINE_boolean force_copy ${FLAGS_FALSE} "Always rebuild test image"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053035DEFINE_string format "qemu" \
36 "Output format, either qemu, vmware or virtualbox"
37DEFINE_string from "" \
38 "Directory containing rootfs.image and mbr.image"
Don Garrett7937ef82014-08-25 18:04:05 -070039DEFINE_string disk_layout "2gb-rootfs-updatable" \
Liam McLoughlinb78a7c32012-11-06 20:19:05 -050040 "The disk layout type to use for this image."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053041DEFINE_boolean make_vmx ${FLAGS_TRUE} \
42 "Create a vmx file for use with vmplayer (vmware only)."
43DEFINE_integer mem "${DEFAULT_MEM}" \
44 "Memory size for the vm config in MBs (vmware only)."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053045DEFINE_string state_image "" \
46 "Stateful partition image (defaults to creating new statful partition)"
47DEFINE_boolean test_image "${FLAGS_FALSE}" \
Simon Glass142ca062011-02-09 13:39:43 -080048 "Copies normal image to ${CHROMEOS_TEST_IMAGE_NAME}, modifies it for test."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053049DEFINE_string to "" \
50 "Destination folder for VM output file(s)"
51DEFINE_string vbox_disk "${DEFAULT_VBOX_DISK}" \
52 "Filename for the output disk (virtualbox only)."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053053DEFINE_string vmdk "${DEFAULT_VMDK}" \
54 "Filename for the vmware disk image (vmware only)."
55DEFINE_string vmx "${DEFAULT_VMX}" \
56 "Filename for the vmware config (vmware only)."
57
58# Parse command line
59FLAGS "$@" || exit 1
60eval set -- "${FLAGS_ARGV}"
61
62# Die on any errors.
Brian Harring7f175a52012-03-02 05:37:00 -080063switch_to_strict_mode
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053064
65if [ -z "${FLAGS_board}" ] ; then
Brian Harring7f175a52012-03-02 05:37:00 -080066 die_notrace "--board is required."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053067fi
68
Liam McLoughlin5b37c542012-08-16 11:09:37 -070069BOARD="$FLAGS_board"
Chris Sosacc09f842010-09-21 17:09:51 -070070
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053071IMAGES_DIR="${DEFAULT_BUILD_ROOT}/images/${FLAGS_board}"
72# Default to the most recent image
73if [ -z "${FLAGS_from}" ] ; then
Kees Cook84a4c7a2011-10-18 13:21:41 -070074 FLAGS_from="$(${SCRIPT_ROOT}/get_latest_image.sh --board=${FLAGS_board})"
Zelidrag Hornung42ca8182010-07-12 18:08:44 -070075else
76 pushd "${FLAGS_from}" && FLAGS_from=`pwd` && popd
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053077fi
78if [ -z "${FLAGS_to}" ] ; then
79 FLAGS_to="${FLAGS_from}"
80fi
81
David James7efb76c2013-01-09 15:25:58 -080082if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ]; then
83 SRC_IMAGE="${FLAGS_from}/${CHROMEOS_FACTORY_TEST_IMAGE_NAME}"
84elif [ ${FLAGS_test_image} -eq ${FLAGS_TRUE} ]; then
85 SRC_IMAGE="${FLAGS_from}/${CHROMEOS_TEST_IMAGE_NAME}"
Simon Glass142ca062011-02-09 13:39:43 -080086else
87 # Use the standard image
88 SRC_IMAGE="${FLAGS_from}/${CHROMEOS_IMAGE_NAME}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053089fi
90
91# Memory units are in MBs
Greg Spencer798d75f2011-02-01 22:04:49 -080092TEMP_IMG="$(dirname "${SRC_IMAGE}")/vm_temp_image.bin"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053093
94# If we're not building for VMWare, don't build the vmx
95if [ "${FLAGS_format}" != "vmware" ]; then
96 FLAGS_make_vmx="${FLAGS_FALSE}"
97fi
98
99# Convert args to paths. Need eval to un-quote the string so that shell
100# chars like ~ are processed; just doing FOO=`readlink -f $FOO` won't work.
101FLAGS_from=`eval readlink -f $FLAGS_from`
102FLAGS_to=`eval readlink -f $FLAGS_to`
103
104# Split apart the partitions and make some new ones
105TEMP_DIR=$(mktemp -d)
Mike Frysinger919bf0c2012-08-22 15:32:39 -0400106pushd "${TEMP_DIR}" >/dev/null
107"${FLAGS_from}/unpack_partitions.sh" "${SRC_IMAGE}"
108popd >/dev/null
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530109
110# Fix the kernel command line
111TEMP_ESP="${TEMP_DIR}"/part_12
Liam McLoughlinaca5e1e2012-09-23 19:31:29 +0000112TEMP_OEM="${TEMP_DIR}"/part_8
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530113TEMP_ROOTFS="${TEMP_DIR}"/part_3
114TEMP_STATE="${TEMP_DIR}"/part_1
Gabe Black2ca93ae2014-08-25 14:33:46 -0700115TEMP_KERN="${TEMP_DIR}"/part_4
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530116if [ -n "${FLAGS_state_image}" ]; then
117 TEMP_STATE="${FLAGS_state_image}"
Rahul Chaturvedie770e162010-07-15 00:35:11 +0530118else
Liam McLoughlinb78a7c32012-11-06 20:19:05 -0500119 STATEFUL_SIZE_BYTES=$(get_filesystem_size "${FLAGS_disk_layout}" 1)
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700120 STATEFUL_SIZE_MEGABYTES=$(( STATEFUL_SIZE_BYTES / 1024 / 1024 ))
121 original_image_size=$(stat -c%s "${TEMP_STATE}")
122 if [ "${original_image_size}" -gt "${STATEFUL_SIZE_BYTES}" ]; then
123 die "Cannot resize stateful image to smaller than original. Exiting."
Rahul Chaturvedie770e162010-07-15 00:35:11 +0530124 fi
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700125
126 echo "Resizing stateful partition to ${STATEFUL_SIZE_MEGABYTES}MB"
127 # Extend the original file size to the new size.
128 sudo e2fsck -pf "${TEMP_STATE}"
129 sudo resize2fs "${TEMP_STATE}" ${STATEFUL_SIZE_MEGABYTES}M
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530130fi
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530131TEMP_PMBR="${TEMP_DIR}"/pmbr
132dd if="${SRC_IMAGE}" of="${TEMP_PMBR}" bs=512 count=1
133
134TEMP_MNT=$(mktemp -d)
Will Drewryefce6682010-07-23 19:43:27 -0500135TEMP_ESP_MNT=$(mktemp -d)
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530136cleanup() {
Brian Harringece65e02012-08-30 13:42:21 -0700137 safe_umount "${TEMP_MNT}"
138 safe_umount "${TEMP_ESP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500139 rmdir "${TEMP_MNT}" "${TEMP_ESP_MNT}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530140}
141trap cleanup INT TERM EXIT
142mkdir -p "${TEMP_MNT}"
Will Drewryf929c302010-10-20 18:48:20 -0500143enable_rw_mount "${TEMP_ROOTFS}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530144sudo mount -o loop "${TEMP_ROOTFS}" "${TEMP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500145mkdir -p "${TEMP_ESP_MNT}"
146sudo mount -o loop "${TEMP_ESP}" "${TEMP_ESP_MNT}"
147
Ben Chand4ec2052014-07-17 18:18:31 -0700148# Modify the unverified usb template, which uses a default usb_disk of sdb3,
149# for targets (e.g. x86 and amd64) that have syslinux installed.
150SYSLINUX_USB_A_CONFIG="${TEMP_MNT}/boot/syslinux/usb.A.cfg"
151if [ -e "${SYSLINUX_USB_A_CONFIG}" ]; then
152 sudo sed -i -e 's/sdb3/sda3/g' "${SYSLINUX_USB_A_CONFIG}"
153fi
Will Drewry537caa92010-08-20 20:30:56 -0500154
Jorge Lucangeli Obes29b972b2013-12-18 15:56:26 -0800155# Add loading of cirrus fb module
Dominik Behrd1a71ca2013-10-01 15:18:38 -0700156if [ "${FLAGS_format}" = "qemu" ]; then
157 sudo_clobber "${TEMP_MNT}/etc/init/cirrusfb.conf" <<END
158start on starting boot-splash
159task
160exec modprobe cirrus
161END
162fi
Jorge Lucangeli Obes29b972b2013-12-18 15:56:26 -0800163
Jorge Lucangeli Obes29b972b2013-12-18 15:56:26 -0800164# TODO as these image-modifying hacks accumulate, we should consider
Dominik Behrd1a71ca2013-10-01 15:18:38 -0700165# creating a better solution
166
Will Drewry537caa92010-08-20 20:30:56 -0500167# Unmount everything prior to building a final image
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530168trap - INT TERM EXIT
169cleanup
170
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700171# Set up a new partition table
172PARTITION_SCRIPT_PATH=$( tempfile )
Liam McLoughlinb78a7c32012-11-06 20:19:05 -0500173write_partition_script "${FLAGS_disk_layout}" "${PARTITION_SCRIPT_PATH}"
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700174. "${PARTITION_SCRIPT_PATH}"
175write_partition_table "${TEMP_IMG}" "${TEMP_PMBR}"
176rm "${PARTITION_SCRIPT_PATH}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530177
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530178# Copy into the partition parts of the file
179dd if="${TEMP_ROOTFS}" of="${TEMP_IMG}" conv=notrunc bs=512 \
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700180 seek=$(partoffset ${TEMP_IMG} 3)
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530181dd if="${TEMP_STATE}" of="${TEMP_IMG}" conv=notrunc bs=512 \
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700182 seek=$(partoffset ${TEMP_IMG} 1)
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530183dd if="${TEMP_ESP}" of="${TEMP_IMG}" conv=notrunc bs=512 \
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700184 seek=$(partoffset ${TEMP_IMG} 12)
Liam McLoughlinaca5e1e2012-09-23 19:31:29 +0000185dd if="${TEMP_OEM}" of="${TEMP_IMG}" conv=notrunc bs=512 \
186 seek=$(partoffset ${TEMP_IMG} 8)
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530187
Gabe Blackb9827592014-09-03 21:58:11 -0700188# Make the built-image bootable.
Will Drewry537caa92010-08-20 20:30:56 -0500189# NOTE: The TEMP_IMG must live in the same image dir as the original image
190# to operate automatically below.
191${SCRIPTS_DIR}/bin/cros_make_image_bootable $(dirname "${TEMP_IMG}") \
192 $(basename "${TEMP_IMG}") \
Arkaitz Ruiz Alvarez2bf88592011-07-07 15:47:31 -0700193 --force_developer_mode
Will Drewry537caa92010-08-20 20:30:56 -0500194
Gabe Black34bbc102014-09-10 16:35:56 -0700195IMAGE_DEV=""
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700196detach_loopback() {
Gabe Black34bbc102014-09-10 16:35:56 -0700197 if [ -n "${IMAGE_DEV}" ]; then
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700198 loopback_detach "${IMAGE_DEV}"
Gabe Black34bbc102014-09-10 16:35:56 -0700199 fi
200}
201trap detach_loopback INT TERM EXIT
202
Gabe Blackb9827592014-09-03 21:58:11 -0700203# cros_make_image_bootable made the kernel in slot A recovery signed. We want
204# it to be normally signed like the one in slot B, so copy B into A.
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700205IMAGE_DEV=$(loopback_partscan "${TEMP_IMG}")
Gabe Blackb9827592014-09-03 21:58:11 -0700206sudo cp ${IMAGE_DEV}p4 ${IMAGE_DEV}p2
207
Gabe Black34bbc102014-09-10 16:35:56 -0700208trap - INT TERM EXIT
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700209loopback_detach "${IMAGE_DEV}"
Gabe Blackb9827592014-09-03 21:58:11 -0700210
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530211echo Creating final image
212# Convert image to output format
213if [ "${FLAGS_format}" = "virtualbox" -o "${FLAGS_format}" = "qemu" ]; then
214 if [ "${FLAGS_format}" = "virtualbox" ]; then
Liam McLoughlinc9f1dab2011-11-29 18:17:26 +0000215 sudo VBoxManage convertdd "${TEMP_IMG}" "${FLAGS_to}/${FLAGS_vbox_disk}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530216 else
217 mv ${TEMP_IMG} ${FLAGS_to}/${DEFAULT_QEMU_IMAGE}
218 fi
219elif [ "${FLAGS_format}" = "vmware" ]; then
220 qemu-img convert -f raw "${TEMP_IMG}" \
221 -O vmdk "${FLAGS_to}/${FLAGS_vmdk}"
222else
Brian Harring7f175a52012-03-02 05:37:00 -0800223 die_notrace "Invalid format: ${FLAGS_format}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530224fi
225
226rm -rf "${TEMP_DIR}" "${TEMP_IMG}"
227if [ -z "${FLAGS_state_image}" ]; then
228 rm -f "${STATE_IMAGE}"
229fi
230
231echo "Created image at ${FLAGS_to}"
232
233# Generate the vmware config file
234# A good reference doc: http://www.sanbarrow.com/vmx.html
235VMX_CONFIG="#!/usr/bin/vmware
236.encoding = \"UTF-8\"
237config.version = \"8\"
238virtualHW.version = \"4\"
239memsize = \"${FLAGS_mem}\"
240ide0:0.present = \"TRUE\"
241ide0:0.fileName = \"${FLAGS_vmdk}\"
242ethernet0.present = \"TRUE\"
243usb.present = \"TRUE\"
244sound.present = \"TRUE\"
245sound.virtualDev = \"es1371\"
246displayName = \"Chromium OS\"
247guestOS = \"otherlinux\"
248ethernet0.addressType = \"generated\"
249floppy0.present = \"FALSE\""
250
251if [[ "${FLAGS_make_vmx}" = "${FLAGS_TRUE}" ]]; then
252 echo "${VMX_CONFIG}" > "${FLAGS_to}/${FLAGS_vmx}"
253 echo "Wrote the following config to: ${FLAGS_to}/${FLAGS_vmx}"
254 echo "${VMX_CONFIG}"
255fi
256
Olof Johansson3ed8d122010-09-27 13:29:40 -0500257
258if [ "${FLAGS_format}" == "qemu" ]; then
259 echo "If you have qemu-kvm installed, you can start the image by:"
John Sheub7b095b2012-12-18 13:30:26 -0800260 echo "sudo kvm -m ${FLAGS_mem} -vga cirrus -pidfile /tmp/kvm.pid" \
261 "-net nic,model=virtio -net user,hostfwd=tcp::9222-:22 \\"
262 echo "-hda ${FLAGS_to}/${DEFAULT_QEMU_IMAGE}"
Olof Johansson3ed8d122010-09-27 13:29:40 -0500263fi