blob: f63ce0357f1f26b871bdea24fafad07997aac98d [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" \
Gaurav Shahd6618fb2014-09-12 16:58:32 -070036 "Output format, either qemu or vmware"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053037DEFINE_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)"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053051DEFINE_string vmdk "${DEFAULT_VMDK}" \
52 "Filename for the vmware disk image (vmware only)."
53DEFINE_string vmx "${DEFAULT_VMX}" \
54 "Filename for the vmware config (vmware only)."
55
56# Parse command line
57FLAGS "$@" || exit 1
58eval set -- "${FLAGS_ARGV}"
59
60# Die on any errors.
Brian Harring7f175a52012-03-02 05:37:00 -080061switch_to_strict_mode
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053062
63if [ -z "${FLAGS_board}" ] ; then
Brian Harring7f175a52012-03-02 05:37:00 -080064 die_notrace "--board is required."
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053065fi
66
Gabe Black067b3542014-09-23 01:15:14 -070067TEMP_DIR=$(mktemp -d)
68TEMP_MNT=""
69TEMP_ESP_MNT=""
70SRC_DEV=""
71DST_DEV=""
72cleanup() {
73 if [[ -n "${TEMP_MNT}" ]]; then
74 safe_umount "${TEMP_MNT}" || true
75 rmdir "${TEMP_MNT}" || true
76 fi
77 if [[ -n "${TEMP_ESP_MNT}" ]]; then
78 safe_umount "${TEMP_ESP_MNT}" || true
79 rmdir "${TEMP_ESP_MNT}" || true
80 fi
81
82 if [[ -n "${SRC_DEV}" ]]; then
83 loopback_detach "${SRC_DEV}" || true
84 fi
85 if [[ -n "${DST_DEV}" ]]; then
86 loopback_detach "${DST_DEV}" || true
87 fi
88 rm -rf "${TEMP_DIR}"
89}
90trap cleanup INT TERM EXIT
91
Liam McLoughlin5b37c542012-08-16 11:09:37 -070092BOARD="$FLAGS_board"
Chris Sosacc09f842010-09-21 17:09:51 -070093
Rahul Chaturvedib5643e82010-07-09 10:46:05 +053094IMAGES_DIR="${DEFAULT_BUILD_ROOT}/images/${FLAGS_board}"
95# Default to the most recent image
96if [ -z "${FLAGS_from}" ] ; then
Kees Cook84a4c7a2011-10-18 13:21:41 -070097 FLAGS_from="$(${SCRIPT_ROOT}/get_latest_image.sh --board=${FLAGS_board})"
Zelidrag Hornung42ca8182010-07-12 18:08:44 -070098else
99 pushd "${FLAGS_from}" && FLAGS_from=`pwd` && popd
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530100fi
101if [ -z "${FLAGS_to}" ] ; then
102 FLAGS_to="${FLAGS_from}"
103fi
104
David James7efb76c2013-01-09 15:25:58 -0800105if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ]; then
106 SRC_IMAGE="${FLAGS_from}/${CHROMEOS_FACTORY_TEST_IMAGE_NAME}"
107elif [ ${FLAGS_test_image} -eq ${FLAGS_TRUE} ]; then
108 SRC_IMAGE="${FLAGS_from}/${CHROMEOS_TEST_IMAGE_NAME}"
Simon Glass142ca062011-02-09 13:39:43 -0800109else
110 # Use the standard image
111 SRC_IMAGE="${FLAGS_from}/${CHROMEOS_IMAGE_NAME}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530112fi
113
114# Memory units are in MBs
Greg Spencer798d75f2011-02-01 22:04:49 -0800115TEMP_IMG="$(dirname "${SRC_IMAGE}")/vm_temp_image.bin"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530116
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
Gabe Black067b3542014-09-23 01:15:14 -0700128SRC_DEV=$(loopback_partscan "${SRC_IMAGE}")
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530129
130# Fix the kernel command line
Gabe Black067b3542014-09-23 01:15:14 -0700131SRC_STATE="${SRC_DEV}"p1
132SRC_ROOTFS="${SRC_DEV}"p3
133SRC_KERN="${SRC_DEV}"p4
134SRC_OEM="${SRC_DEV}"p8
135SRC_ESP="${SRC_DEV}"p12
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530136if [ -n "${FLAGS_state_image}" ]; then
137 TEMP_STATE="${FLAGS_state_image}"
Rahul Chaturvedie770e162010-07-15 00:35:11 +0530138else
Liam McLoughlinb78a7c32012-11-06 20:19:05 -0500139 STATEFUL_SIZE_BYTES=$(get_filesystem_size "${FLAGS_disk_layout}" 1)
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700140 STATEFUL_SIZE_MEGABYTES=$(( STATEFUL_SIZE_BYTES / 1024 / 1024 ))
Gabe Black067b3542014-09-23 01:15:14 -0700141 original_image_size=$(bd_safe_size "${SRC_STATE}")
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700142 if [ "${original_image_size}" -gt "${STATEFUL_SIZE_BYTES}" ]; then
143 die "Cannot resize stateful image to smaller than original. Exiting."
Rahul Chaturvedie770e162010-07-15 00:35:11 +0530144 fi
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700145
146 echo "Resizing stateful partition to ${STATEFUL_SIZE_MEGABYTES}MB"
147 # Extend the original file size to the new size.
Gabe Black067b3542014-09-23 01:15:14 -0700148 TEMP_STATE="${TEMP_DIR}"/stateful
149 # Create TEMP_STATE as a regular user so a regular user can delete it.
Gabe Black3fc1f1f2014-09-29 21:53:01 -0700150 max_tries=10
151 for (( i = 1; i <= max_tries; i++ )); do
Gabe Blackada84882014-10-02 03:50:39 -0700152 sudo chmod a+r "${SRC_STATE}"
Gabe Black3fc1f1f2014-09-29 21:53:01 -0700153 if dd if="${SRC_STATE}" of="${TEMP_STATE}"; then
154 break
155 fi
156 if [[ ${i} -eq ${max_tries} ]]; then
157 die "Failed to dd ${SRC_STATE} to ${TEMP_STATE} ${i} times, giving up."
158 fi
159 start_offset=$(cat /sys/block/${SRC_DEV##/dev/}/${SRC_STATE##/dev/}/start)
160 warn "${SRC_STATE} start is: ${start_offset}"
161 sudo cgpt show "${SRC_DEV}"
162 warn "Failed to dd ${SRC_STATE} to ${TEMP_STATE} on try ${i}, retrying"
Gabe Blackada84882014-10-02 03:50:39 -0700163 if [[ ${i} -gt $(( max_tries / 2 )) ]]; then
Gabe Black3fc1f1f2014-09-29 21:53:01 -0700164 warn "Trying to fix ${SRC_DEV} by rereading PT"
165 sudo blockdev --rereadpt "${SRC_DEV}"
166 fi
167 sleep 5
168 done
Liam McLoughlin5b37c542012-08-16 11:09:37 -0700169 sudo e2fsck -pf "${TEMP_STATE}"
170 sudo resize2fs "${TEMP_STATE}" ${STATEFUL_SIZE_MEGABYTES}M
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530171fi
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530172TEMP_PMBR="${TEMP_DIR}"/pmbr
173dd if="${SRC_IMAGE}" of="${TEMP_PMBR}" bs=512 count=1
174
Gabe Black067b3542014-09-23 01:15:14 -0700175# Set up a new partition table.
176PARTITION_SCRIPT_PATH=$(mktemp)
177write_partition_script "${FLAGS_disk_layout}" "${PARTITION_SCRIPT_PATH}"
178. "${PARTITION_SCRIPT_PATH}"
179write_partition_table "${TEMP_IMG}" "${TEMP_PMBR}"
180rm "${PARTITION_SCRIPT_PATH}"
181
182DST_DEV=$(loopback_partscan "${TEMP_IMG}")
183DST_STATE="${DST_DEV}"p1
184DST_ROOTFS="${DST_DEV}"p3
185DST_KERN="${DST_DEV}"p4
186DST_OEM="${DST_DEV}"p8
187DST_ESP="${DST_DEV}"p12
188
189# Copy into the partition parts of the file.
190sudo dd if="${SRC_ROOTFS}" of="${DST_ROOTFS}"
191sudo dd if="${TEMP_STATE}" of="${DST_STATE}"
192sudo dd if="${SRC_ESP}" of="${DST_ESP}"
193sudo dd if="${SRC_OEM}" of="${DST_OEM}"
194
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530195TEMP_MNT=$(mktemp -d)
Will Drewryefce6682010-07-23 19:43:27 -0500196TEMP_ESP_MNT=$(mktemp -d)
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530197mkdir -p "${TEMP_MNT}"
Gabe Black067b3542014-09-23 01:15:14 -0700198enable_rw_mount "${DST_ROOTFS}"
199sudo mount "${DST_ROOTFS}" "${TEMP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500200mkdir -p "${TEMP_ESP_MNT}"
Gabe Black067b3542014-09-23 01:15:14 -0700201sudo mount "${DST_ESP}" "${TEMP_ESP_MNT}"
Will Drewryefce6682010-07-23 19:43:27 -0500202
Ben Chand4ec2052014-07-17 18:18:31 -0700203# Modify the unverified usb template, which uses a default usb_disk of sdb3,
204# for targets (e.g. x86 and amd64) that have syslinux installed.
205SYSLINUX_USB_A_CONFIG="${TEMP_MNT}/boot/syslinux/usb.A.cfg"
206if [ -e "${SYSLINUX_USB_A_CONFIG}" ]; then
207 sudo sed -i -e 's/sdb3/sda3/g' "${SYSLINUX_USB_A_CONFIG}"
208fi
Will Drewry537caa92010-08-20 20:30:56 -0500209
Jorge Lucangeli Obes29b972b2013-12-18 15:56:26 -0800210# Add loading of cirrus fb module
Dominik Behrd1a71ca2013-10-01 15:18:38 -0700211if [ "${FLAGS_format}" = "qemu" ]; then
212 sudo_clobber "${TEMP_MNT}/etc/init/cirrusfb.conf" <<END
213start on starting boot-splash
214task
215exec modprobe cirrus
216END
217fi
Jorge Lucangeli Obes29b972b2013-12-18 15:56:26 -0800218
Will Drewry537caa92010-08-20 20:30:56 -0500219# Unmount everything prior to building a final image
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530220trap - INT TERM EXIT
221cleanup
222
Gabe Blackb9827592014-09-03 21:58:11 -0700223# Make the built-image bootable.
Will Drewry537caa92010-08-20 20:30:56 -0500224# NOTE: The TEMP_IMG must live in the same image dir as the original image
225# to operate automatically below.
226${SCRIPTS_DIR}/bin/cros_make_image_bootable $(dirname "${TEMP_IMG}") \
227 $(basename "${TEMP_IMG}") \
Arkaitz Ruiz Alvarez2bf88592011-07-07 15:47:31 -0700228 --force_developer_mode
Will Drewry537caa92010-08-20 20:30:56 -0500229
Gabe Black34bbc102014-09-10 16:35:56 -0700230IMAGE_DEV=""
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700231detach_loopback() {
Gabe Black34bbc102014-09-10 16:35:56 -0700232 if [ -n "${IMAGE_DEV}" ]; then
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700233 loopback_detach "${IMAGE_DEV}"
Gabe Black34bbc102014-09-10 16:35:56 -0700234 fi
235}
236trap detach_loopback INT TERM EXIT
237
Gabe Blackb9827592014-09-03 21:58:11 -0700238# cros_make_image_bootable made the kernel in slot A recovery signed. We want
239# it to be normally signed like the one in slot B, so copy B into A.
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700240IMAGE_DEV=$(loopback_partscan "${TEMP_IMG}")
Gabe Blackb9827592014-09-03 21:58:11 -0700241sudo cp ${IMAGE_DEV}p4 ${IMAGE_DEV}p2
242
Gabe Black34bbc102014-09-10 16:35:56 -0700243trap - INT TERM EXIT
Gabe Blackfbdb1d52014-09-22 23:43:35 -0700244loopback_detach "${IMAGE_DEV}"
Gabe Blackb9827592014-09-03 21:58:11 -0700245
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530246echo Creating final image
247# Convert image to output format
Gaurav Shahd6618fb2014-09-12 16:58:32 -0700248if [ "${FLAGS_format}" = "qemu" ]; then
249 mv "${TEMP_IMG}" "${FLAGS_to}/${DEFAULT_QEMU_IMAGE}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530250elif [ "${FLAGS_format}" = "vmware" ]; then
251 qemu-img convert -f raw "${TEMP_IMG}" \
252 -O vmdk "${FLAGS_to}/${FLAGS_vmdk}"
253else
Brian Harring7f175a52012-03-02 05:37:00 -0800254 die_notrace "Invalid format: ${FLAGS_format}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530255fi
256
Gabe Black067b3542014-09-23 01:15:14 -0700257rm -rf "${TEMP_IMG}"
Rahul Chaturvedib5643e82010-07-09 10:46:05 +0530258
259echo "Created image at ${FLAGS_to}"
260
261# Generate the vmware config file
262# A good reference doc: http://www.sanbarrow.com/vmx.html
263VMX_CONFIG="#!/usr/bin/vmware
264.encoding = \"UTF-8\"
265config.version = \"8\"
266virtualHW.version = \"4\"
267memsize = \"${FLAGS_mem}\"
268ide0:0.present = \"TRUE\"
269ide0:0.fileName = \"${FLAGS_vmdk}\"
270ethernet0.present = \"TRUE\"
271usb.present = \"TRUE\"
272sound.present = \"TRUE\"
273sound.virtualDev = \"es1371\"
274displayName = \"Chromium OS\"
275guestOS = \"otherlinux\"
276ethernet0.addressType = \"generated\"
277floppy0.present = \"FALSE\""
278
279if [[ "${FLAGS_make_vmx}" = "${FLAGS_TRUE}" ]]; then
280 echo "${VMX_CONFIG}" > "${FLAGS_to}/${FLAGS_vmx}"
281 echo "Wrote the following config to: ${FLAGS_to}/${FLAGS_vmx}"
282 echo "${VMX_CONFIG}"
283fi
284
Olof Johansson3ed8d122010-09-27 13:29:40 -0500285
286if [ "${FLAGS_format}" == "qemu" ]; then
287 echo "If you have qemu-kvm installed, you can start the image by:"
John Sheub7b095b2012-12-18 13:30:26 -0800288 echo "sudo kvm -m ${FLAGS_mem} -vga cirrus -pidfile /tmp/kvm.pid" \
289 "-net nic,model=virtio -net user,hostfwd=tcp::9222-:22 \\"
290 echo "-hda ${FLAGS_to}/${DEFAULT_QEMU_IMAGE}"
Olof Johansson3ed8d122010-09-27 13:29:40 -0500291fi