Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 1 | #!/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 Cook | 84a4c7a | 2011-10-18 13:21:41 -0700 | [diff] [blame] | 10 | # Helper scripts should be run from the same location as this script. |
| 11 | SCRIPT_ROOT=$(dirname "$(readlink -f "$0")") |
David James | 359d3e1 | 2012-07-10 13:09:48 -0700 | [diff] [blame] | 12 | . "${SCRIPT_ROOT}/common.sh" || exit 1 |
Liam McLoughlin | 5b37c54 | 2012-08-16 11:09:37 -0700 | [diff] [blame] | 13 | . "${SCRIPT_ROOT}/build_library/disk_layout_util.sh" || exit 1 |
| 14 | . "${SCRIPT_ROOT}/build_library/build_common.sh" || exit 1 |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 15 | |
| 16 | # Need to be inside the chroot to load chromeos-common.sh |
| 17 | assert_inside_chroot |
| 18 | |
| 19 | # Load functions and constants for chromeos-install |
David James | 359d3e1 | 2012-07-10 13:09:48 -0700 | [diff] [blame] | 20 | . /usr/lib/installer/chromeos-common.sh || exit 1 |
| 21 | . "${SCRIPT_ROOT}/lib/cros_vm_constants.sh" || exit 1 |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 22 | |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 23 | # Flags |
Liam McLoughlin | 12a9a84 | 2012-10-10 10:37:06 -0400 | [diff] [blame] | 24 | DEFINE_string adjust_part "" \ |
| 25 | "Adjustments to apply to the partition table" |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 26 | DEFINE_string board "${DEFAULT_BOARD}" \ |
| 27 | "Board for which the image was built" |
| 28 | DEFINE_boolean factory $FLAGS_FALSE \ |
| 29 | "Modify the image for manufacturing testing" |
| 30 | DEFINE_boolean factory_install $FLAGS_FALSE \ |
| 31 | "Modify the image for factory install shim" |
Simon Glass | 142ca06 | 2011-02-09 13:39:43 -0800 | [diff] [blame] | 32 | |
| 33 | # We default to TRUE so the buildbot gets its image. Note this is different |
| 34 | # behavior from image_to_usb.sh |
Chris Sosa | 8dad50d | 2011-02-14 15:29:32 -0800 | [diff] [blame] | 35 | DEFINE_boolean force_copy ${FLAGS_FALSE} "Always rebuild test image" |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 36 | DEFINE_string format "qemu" \ |
| 37 | "Output format, either qemu, vmware or virtualbox" |
| 38 | DEFINE_string from "" \ |
| 39 | "Directory containing rootfs.image and mbr.image" |
| 40 | DEFINE_boolean make_vmx ${FLAGS_TRUE} \ |
| 41 | "Create a vmx file for use with vmplayer (vmware only)." |
| 42 | DEFINE_integer mem "${DEFAULT_MEM}" \ |
| 43 | "Memory size for the vm config in MBs (vmware only)." |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 44 | DEFINE_string state_image "" \ |
| 45 | "Stateful partition image (defaults to creating new statful partition)" |
| 46 | DEFINE_boolean test_image "${FLAGS_FALSE}" \ |
Simon Glass | 142ca06 | 2011-02-09 13:39:43 -0800 | [diff] [blame] | 47 | "Copies normal image to ${CHROMEOS_TEST_IMAGE_NAME}, modifies it for test." |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 48 | DEFINE_string to "" \ |
| 49 | "Destination folder for VM output file(s)" |
| 50 | DEFINE_string vbox_disk "${DEFAULT_VBOX_DISK}" \ |
| 51 | "Filename for the output disk (virtualbox only)." |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 52 | DEFINE_string vmdk "${DEFAULT_VMDK}" \ |
| 53 | "Filename for the vmware disk image (vmware only)." |
| 54 | DEFINE_string vmx "${DEFAULT_VMX}" \ |
| 55 | "Filename for the vmware config (vmware only)." |
| 56 | |
Liam McLoughlin | 5b37c54 | 2012-08-16 11:09:37 -0700 | [diff] [blame] | 57 | # The following arguments are ignored. |
| 58 | # They are here as part of a transition for CL #29931 beacuse the buildbots |
| 59 | # specify these arguments. |
| 60 | DEFINE_integer vdisk_size 3072 \ |
| 61 | "virtual disk size in MBs." |
| 62 | DEFINE_boolean full "${FLAGS_FALSE}" "Build full image with all partitions." |
| 63 | DEFINE_integer rootfs_partition_size 1024 \ |
| 64 | "rootfs parition size in MBs." |
| 65 | DEFINE_integer statefulfs_size 2048 \ |
| 66 | "Stateful partition size in MBs." |
| 67 | |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 68 | # Parse command line |
| 69 | FLAGS "$@" || exit 1 |
| 70 | eval set -- "${FLAGS_ARGV}" |
| 71 | |
| 72 | # Die on any errors. |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 73 | switch_to_strict_mode |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 74 | |
| 75 | if [ -z "${FLAGS_board}" ] ; then |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 76 | die_notrace "--board is required." |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 77 | fi |
| 78 | |
Liam McLoughlin | 5b37c54 | 2012-08-16 11:09:37 -0700 | [diff] [blame] | 79 | BOARD="$FLAGS_board" |
Chris Sosa | cc09f84 | 2010-09-21 17:09:51 -0700 | [diff] [blame] | 80 | |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 81 | IMAGES_DIR="${DEFAULT_BUILD_ROOT}/images/${FLAGS_board}" |
| 82 | # Default to the most recent image |
| 83 | if [ -z "${FLAGS_from}" ] ; then |
Kees Cook | 84a4c7a | 2011-10-18 13:21:41 -0700 | [diff] [blame] | 84 | FLAGS_from="$(${SCRIPT_ROOT}/get_latest_image.sh --board=${FLAGS_board})" |
Zelidrag Hornung | 42ca818 | 2010-07-12 18:08:44 -0700 | [diff] [blame] | 85 | else |
| 86 | pushd "${FLAGS_from}" && FLAGS_from=`pwd` && popd |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 87 | fi |
| 88 | if [ -z "${FLAGS_to}" ] ; then |
| 89 | FLAGS_to="${FLAGS_from}" |
| 90 | fi |
| 91 | |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 92 | if [ ${FLAGS_test_image} -eq ${FLAGS_TRUE} ] ; then |
Simon Glass | 142ca06 | 2011-02-09 13:39:43 -0800 | [diff] [blame] | 93 | # Make a test image - this returns the test filename in CHROMEOS_RETURN_VAL |
| 94 | prepare_test_image "${FLAGS_from}" "${CHROMEOS_IMAGE_NAME}" |
| 95 | SRC_IMAGE="${CHROMEOS_RETURN_VAL}" |
| 96 | else |
| 97 | # Use the standard image |
| 98 | SRC_IMAGE="${FLAGS_from}/${CHROMEOS_IMAGE_NAME}" |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 99 | fi |
| 100 | |
| 101 | # Memory units are in MBs |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 102 | TEMP_IMG="$(dirname "${SRC_IMAGE}")/vm_temp_image.bin" |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 103 | |
| 104 | # If we're not building for VMWare, don't build the vmx |
| 105 | if [ "${FLAGS_format}" != "vmware" ]; then |
| 106 | FLAGS_make_vmx="${FLAGS_FALSE}" |
| 107 | fi |
| 108 | |
| 109 | # Convert args to paths. Need eval to un-quote the string so that shell |
| 110 | # chars like ~ are processed; just doing FOO=`readlink -f $FOO` won't work. |
| 111 | FLAGS_from=`eval readlink -f $FLAGS_from` |
| 112 | FLAGS_to=`eval readlink -f $FLAGS_to` |
| 113 | |
| 114 | # Split apart the partitions and make some new ones |
| 115 | TEMP_DIR=$(mktemp -d) |
Mike Frysinger | 919bf0c | 2012-08-22 15:32:39 -0400 | [diff] [blame] | 116 | pushd "${TEMP_DIR}" >/dev/null |
| 117 | "${FLAGS_from}/unpack_partitions.sh" "${SRC_IMAGE}" |
| 118 | popd >/dev/null |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 119 | |
| 120 | # Fix the kernel command line |
| 121 | TEMP_ESP="${TEMP_DIR}"/part_12 |
Liam McLoughlin | aca5e1e | 2012-09-23 19:31:29 +0000 | [diff] [blame] | 122 | TEMP_OEM="${TEMP_DIR}"/part_8 |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 123 | TEMP_ROOTFS="${TEMP_DIR}"/part_3 |
| 124 | TEMP_STATE="${TEMP_DIR}"/part_1 |
Will Drewry | efce668 | 2010-07-23 19:43:27 -0500 | [diff] [blame] | 125 | TEMP_KERN="${TEMP_DIR}"/part_2 |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 126 | if [ -n "${FLAGS_state_image}" ]; then |
| 127 | TEMP_STATE="${FLAGS_state_image}" |
Rahul Chaturvedi | e770e16 | 2010-07-15 00:35:11 +0530 | [diff] [blame] | 128 | else |
Liam McLoughlin | 5b37c54 | 2012-08-16 11:09:37 -0700 | [diff] [blame] | 129 | STATEFUL_SIZE_BYTES=$(get_filesystem_size vm 1) |
| 130 | STATEFUL_SIZE_MEGABYTES=$(( STATEFUL_SIZE_BYTES / 1024 / 1024 )) |
| 131 | original_image_size=$(stat -c%s "${TEMP_STATE}") |
| 132 | if [ "${original_image_size}" -gt "${STATEFUL_SIZE_BYTES}" ]; then |
| 133 | die "Cannot resize stateful image to smaller than original. Exiting." |
Rahul Chaturvedi | e770e16 | 2010-07-15 00:35:11 +0530 | [diff] [blame] | 134 | fi |
Liam McLoughlin | 5b37c54 | 2012-08-16 11:09:37 -0700 | [diff] [blame] | 135 | |
| 136 | echo "Resizing stateful partition to ${STATEFUL_SIZE_MEGABYTES}MB" |
| 137 | # Extend the original file size to the new size. |
| 138 | sudo e2fsck -pf "${TEMP_STATE}" |
| 139 | sudo resize2fs "${TEMP_STATE}" ${STATEFUL_SIZE_MEGABYTES}M |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 140 | fi |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 141 | TEMP_PMBR="${TEMP_DIR}"/pmbr |
| 142 | dd if="${SRC_IMAGE}" of="${TEMP_PMBR}" bs=512 count=1 |
| 143 | |
| 144 | TEMP_MNT=$(mktemp -d) |
Will Drewry | efce668 | 2010-07-23 19:43:27 -0500 | [diff] [blame] | 145 | TEMP_ESP_MNT=$(mktemp -d) |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 146 | cleanup() { |
Brian Harring | ece65e0 | 2012-08-30 13:42:21 -0700 | [diff] [blame] | 147 | safe_umount "${TEMP_MNT}" |
| 148 | safe_umount "${TEMP_ESP_MNT}" |
Will Drewry | efce668 | 2010-07-23 19:43:27 -0500 | [diff] [blame] | 149 | rmdir "${TEMP_MNT}" "${TEMP_ESP_MNT}" |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 150 | } |
| 151 | trap cleanup INT TERM EXIT |
| 152 | mkdir -p "${TEMP_MNT}" |
Will Drewry | f929c30 | 2010-10-20 18:48:20 -0500 | [diff] [blame] | 153 | enable_rw_mount "${TEMP_ROOTFS}" |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 154 | sudo mount -o loop "${TEMP_ROOTFS}" "${TEMP_MNT}" |
Will Drewry | efce668 | 2010-07-23 19:43:27 -0500 | [diff] [blame] | 155 | mkdir -p "${TEMP_ESP_MNT}" |
| 156 | sudo mount -o loop "${TEMP_ESP}" "${TEMP_ESP_MNT}" |
| 157 | |
Will Drewry | 537caa9 | 2010-08-20 20:30:56 -0500 | [diff] [blame] | 158 | # Modify the unverified usb template which uses a default usb_disk of sdb3 |
| 159 | sudo sed -i -e 's/sdb3/sda3/g' "${TEMP_MNT}/boot/syslinux/usb.A.cfg" |
| 160 | |
| 161 | # Unmount everything prior to building a final image |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 162 | trap - INT TERM EXIT |
| 163 | cleanup |
| 164 | |
Liam McLoughlin | 5b37c54 | 2012-08-16 11:09:37 -0700 | [diff] [blame] | 165 | # Set up a new partition table |
| 166 | PARTITION_SCRIPT_PATH=$( tempfile ) |
| 167 | write_partition_script "vm" "${PARTITION_SCRIPT_PATH}" |
| 168 | . "${PARTITION_SCRIPT_PATH}" |
| 169 | write_partition_table "${TEMP_IMG}" "${TEMP_PMBR}" |
| 170 | rm "${PARTITION_SCRIPT_PATH}" |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 171 | |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 172 | # Copy into the partition parts of the file |
| 173 | dd if="${TEMP_ROOTFS}" of="${TEMP_IMG}" conv=notrunc bs=512 \ |
Liam McLoughlin | 5b37c54 | 2012-08-16 11:09:37 -0700 | [diff] [blame] | 174 | seek=$(partoffset ${TEMP_IMG} 3) |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 175 | dd if="${TEMP_STATE}" of="${TEMP_IMG}" conv=notrunc bs=512 \ |
Liam McLoughlin | 5b37c54 | 2012-08-16 11:09:37 -0700 | [diff] [blame] | 176 | seek=$(partoffset ${TEMP_IMG} 1) |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 177 | dd if="${TEMP_KERN}" of="${TEMP_IMG}" conv=notrunc bs=512 \ |
Liam McLoughlin | 5b37c54 | 2012-08-16 11:09:37 -0700 | [diff] [blame] | 178 | seek=$(partoffset ${TEMP_IMG} 2) |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 179 | dd if="${TEMP_ESP}" of="${TEMP_IMG}" conv=notrunc bs=512 \ |
Liam McLoughlin | 5b37c54 | 2012-08-16 11:09:37 -0700 | [diff] [blame] | 180 | seek=$(partoffset ${TEMP_IMG} 12) |
Liam McLoughlin | aca5e1e | 2012-09-23 19:31:29 +0000 | [diff] [blame] | 181 | dd if="${TEMP_OEM}" of="${TEMP_IMG}" conv=notrunc bs=512 \ |
| 182 | seek=$(partoffset ${TEMP_IMG} 8) |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 183 | |
Will Drewry | 537caa9 | 2010-08-20 20:30:56 -0500 | [diff] [blame] | 184 | # Make the built-image bootable and ensure that the legacy default usb boot |
| 185 | # uses /dev/sda instead of /dev/sdb3. |
| 186 | # NOTE: The TEMP_IMG must live in the same image dir as the original image |
| 187 | # to operate automatically below. |
| 188 | ${SCRIPTS_DIR}/bin/cros_make_image_bootable $(dirname "${TEMP_IMG}") \ |
| 189 | $(basename "${TEMP_IMG}") \ |
Arkaitz Ruiz Alvarez | 2bf8859 | 2011-07-07 15:47:31 -0700 | [diff] [blame] | 190 | --usb_disk /dev/sda3 \ |
| 191 | --force_developer_mode |
Will Drewry | 537caa9 | 2010-08-20 20:30:56 -0500 | [diff] [blame] | 192 | |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 193 | echo Creating final image |
| 194 | # Convert image to output format |
| 195 | if [ "${FLAGS_format}" = "virtualbox" -o "${FLAGS_format}" = "qemu" ]; then |
| 196 | if [ "${FLAGS_format}" = "virtualbox" ]; then |
Liam McLoughlin | c9f1dab | 2011-11-29 18:17:26 +0000 | [diff] [blame] | 197 | sudo VBoxManage convertdd "${TEMP_IMG}" "${FLAGS_to}/${FLAGS_vbox_disk}" |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 198 | else |
| 199 | mv ${TEMP_IMG} ${FLAGS_to}/${DEFAULT_QEMU_IMAGE} |
| 200 | fi |
| 201 | elif [ "${FLAGS_format}" = "vmware" ]; then |
| 202 | qemu-img convert -f raw "${TEMP_IMG}" \ |
| 203 | -O vmdk "${FLAGS_to}/${FLAGS_vmdk}" |
| 204 | else |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 205 | die_notrace "Invalid format: ${FLAGS_format}" |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 206 | fi |
| 207 | |
| 208 | rm -rf "${TEMP_DIR}" "${TEMP_IMG}" |
| 209 | if [ -z "${FLAGS_state_image}" ]; then |
| 210 | rm -f "${STATE_IMAGE}" |
| 211 | fi |
| 212 | |
| 213 | echo "Created image at ${FLAGS_to}" |
| 214 | |
| 215 | # Generate the vmware config file |
| 216 | # A good reference doc: http://www.sanbarrow.com/vmx.html |
| 217 | VMX_CONFIG="#!/usr/bin/vmware |
| 218 | .encoding = \"UTF-8\" |
| 219 | config.version = \"8\" |
| 220 | virtualHW.version = \"4\" |
| 221 | memsize = \"${FLAGS_mem}\" |
| 222 | ide0:0.present = \"TRUE\" |
| 223 | ide0:0.fileName = \"${FLAGS_vmdk}\" |
| 224 | ethernet0.present = \"TRUE\" |
| 225 | usb.present = \"TRUE\" |
| 226 | sound.present = \"TRUE\" |
| 227 | sound.virtualDev = \"es1371\" |
| 228 | displayName = \"Chromium OS\" |
| 229 | guestOS = \"otherlinux\" |
| 230 | ethernet0.addressType = \"generated\" |
| 231 | floppy0.present = \"FALSE\"" |
| 232 | |
| 233 | if [[ "${FLAGS_make_vmx}" = "${FLAGS_TRUE}" ]]; then |
| 234 | echo "${VMX_CONFIG}" > "${FLAGS_to}/${FLAGS_vmx}" |
| 235 | echo "Wrote the following config to: ${FLAGS_to}/${FLAGS_vmx}" |
| 236 | echo "${VMX_CONFIG}" |
| 237 | fi |
| 238 | |
Olof Johansson | 3ed8d12 | 2010-09-27 13:29:40 -0500 | [diff] [blame] | 239 | |
| 240 | if [ "${FLAGS_format}" == "qemu" ]; then |
| 241 | echo "If you have qemu-kvm installed, you can start the image by:" |
Anush Elangovan | 0de6de6 | 2010-12-07 17:31:48 -0800 | [diff] [blame] | 242 | echo "sudo kvm -m ${FLAGS_mem} -vga std -pidfile /tmp/kvm.pid -net nic,model=virtio " \ |
Chris Masone | 7d04c7a | 2010-11-09 08:24:13 -0800 | [diff] [blame] | 243 | "-net user,hostfwd=tcp::9222-:22 \\" |
Olof Johansson | 3ed8d12 | 2010-09-27 13:29:40 -0500 | [diff] [blame] | 244 | echo " -hda ${FLAGS_to}/${DEFAULT_QEMU_IMAGE}" |
| 245 | fi |