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 | |
| 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" |
Chris Sosa | 45bec93 | 2010-09-29 15:38:53 -0700 | [diff] [blame] | 14 | . "$(dirname "$0")/lib/cros_vm_constants.sh" |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 15 | |
| 16 | get_default_board |
Chris Sosa | cc09f84 | 2010-09-21 17:09:51 -0700 | [diff] [blame] | 17 | assert_inside_chroot |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 18 | |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 19 | # Flags |
| 20 | DEFINE_string board "${DEFAULT_BOARD}" \ |
| 21 | "Board for which the image was built" |
| 22 | DEFINE_boolean factory $FLAGS_FALSE \ |
| 23 | "Modify the image for manufacturing testing" |
| 24 | DEFINE_boolean factory_install $FLAGS_FALSE \ |
| 25 | "Modify the image for factory install shim" |
| 26 | DEFINE_boolean force_copy ${FLAGS_FALSE} "Always rebuild test image" |
| 27 | DEFINE_string format "qemu" \ |
| 28 | "Output format, either qemu, vmware or virtualbox" |
| 29 | DEFINE_string from "" \ |
| 30 | "Directory containing rootfs.image and mbr.image" |
Chris Sosa | cc09f84 | 2010-09-21 17:09:51 -0700 | [diff] [blame] | 31 | DEFINE_boolean full "${FLAGS_FALSE}" "Build full image with all partitions." |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 32 | DEFINE_boolean make_vmx ${FLAGS_TRUE} \ |
| 33 | "Create a vmx file for use with vmplayer (vmware only)." |
| 34 | DEFINE_integer mem "${DEFAULT_MEM}" \ |
| 35 | "Memory size for the vm config in MBs (vmware only)." |
| 36 | DEFINE_integer rootfs_partition_size 1024 \ |
| 37 | "rootfs parition size in MBs." |
| 38 | DEFINE_string state_image "" \ |
| 39 | "Stateful partition image (defaults to creating new statful partition)" |
Rahul Chaturvedi | e770e16 | 2010-07-15 00:35:11 +0530 | [diff] [blame] | 40 | DEFINE_integer statefulfs_size -1 \ |
| 41 | "Stateful partition size in MBs." |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 42 | DEFINE_boolean test_image "${FLAGS_FALSE}" \ |
| 43 | "Copies normal image to chromiumos_test_image.bin, modifies it for test." |
| 44 | DEFINE_string to "" \ |
| 45 | "Destination folder for VM output file(s)" |
| 46 | DEFINE_string vbox_disk "${DEFAULT_VBOX_DISK}" \ |
| 47 | "Filename for the output disk (virtualbox only)." |
| 48 | DEFINE_integer vdisk_size 3072 \ |
| 49 | "virtual disk size in MBs." |
| 50 | DEFINE_string vmdk "${DEFAULT_VMDK}" \ |
| 51 | "Filename for the vmware disk image (vmware only)." |
| 52 | DEFINE_string vmx "${DEFAULT_VMX}" \ |
| 53 | "Filename for the vmware config (vmware only)." |
| 54 | |
| 55 | # Parse command line |
| 56 | FLAGS "$@" || exit 1 |
| 57 | eval set -- "${FLAGS_ARGV}" |
| 58 | |
| 59 | # Die on any errors. |
| 60 | set -e |
| 61 | |
| 62 | if [ -z "${FLAGS_board}" ] ; then |
| 63 | die "--board is required." |
| 64 | fi |
| 65 | |
Chris Sosa | cc09f84 | 2010-09-21 17:09:51 -0700 | [diff] [blame] | 66 | if [ "${FLAGS_full}" -eq "${FLAGS_TRUE}" ] && \ |
| 67 | ( [[ ${FLAGS_vdisk_size} < ${MIN_VDISK_SIZE_FULL} ]] || \ |
| 68 | [[ ${FLAGS_statefulfs_size} < ${MIN_STATEFUL_FS_SIZE_FULL} ]]); then |
| 69 | die "Disk is too small for full, please select a vdisk size greater than \ |
| 70 | ${MIN_VDISK_SIZE_FULL} and statefulfs size greater than \ |
| 71 | ${MIN_STATEFUL_FS_SIZE_FULL}." |
| 72 | fi |
| 73 | |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 74 | IMAGES_DIR="${DEFAULT_BUILD_ROOT}/images/${FLAGS_board}" |
| 75 | # Default to the most recent image |
| 76 | if [ -z "${FLAGS_from}" ] ; then |
Chris Sosa | bd61304 | 2010-10-04 13:29:23 -0700 | [diff] [blame] | 77 | FLAGS_from="$(./get_latest_image.sh --board=${FLAGS_board})" |
Zelidrag Hornung | 42ca818 | 2010-07-12 18:08:44 -0700 | [diff] [blame] | 78 | else |
| 79 | pushd "${FLAGS_from}" && FLAGS_from=`pwd` && popd |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 80 | fi |
| 81 | if [ -z "${FLAGS_to}" ] ; then |
| 82 | FLAGS_to="${FLAGS_from}" |
| 83 | fi |
| 84 | |
| 85 | # Use this image as the source image to copy |
| 86 | SRC_IMAGE="${FLAGS_from}/chromiumos_image.bin" |
| 87 | |
| 88 | # If we're asked to modify the image for test, then let's make a copy and |
| 89 | # modify that instead. |
| 90 | if [ ${FLAGS_test_image} -eq ${FLAGS_TRUE} ] ; then |
| 91 | if [ ! -f "${FLAGS_from}/chromiumos_test_image.bin" ] || \ |
| 92 | [ ${FLAGS_force_copy} -eq ${FLAGS_TRUE} ] ; then |
| 93 | # Copy it. |
| 94 | echo "Creating test image from original..." |
| 95 | cp -f "${SRC_IMAGE}" "${FLAGS_from}/chromiumos_test_image.bin" |
| 96 | |
| 97 | # Check for manufacturing image. |
| 98 | if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ] ; then |
| 99 | EXTRA_ARGS="--factory" |
| 100 | fi |
| 101 | |
| 102 | # Check for install shim. |
| 103 | if [ ${FLAGS_factory_install} -eq ${FLAGS_TRUE} ] ; then |
| 104 | EXTRA_ARGS="--factory_install" |
| 105 | fi |
| 106 | |
| 107 | # Modify it. Pass --yes so that mod_image_for_test.sh won't ask us if we |
| 108 | # really want to modify the image; the user gave their assent already with |
| 109 | # --test-image and the original image is going to be preserved. |
Ryan Cairns | a7be5ff | 2010-08-23 20:47:07 -0700 | [diff] [blame] | 110 | "${SCRIPTS_DIR}/mod_image_for_test.sh" --board=${FLAGS_board} --image \ |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 111 | "${FLAGS_from}/chromiumos_test_image.bin" ${EXTRA_ARGS} --yes |
| 112 | echo "Done with mod_image_for_test." |
| 113 | else |
| 114 | echo "Using cached test image." |
| 115 | fi |
| 116 | SRC_IMAGE="${FLAGS_from}/chromiumos_test_image.bin" |
| 117 | echo "Source test image is: ${SRC_IMAGE}" |
| 118 | fi |
| 119 | |
| 120 | # Memory units are in MBs |
Will Drewry | 537caa9 | 2010-08-20 20:30:56 -0500 | [diff] [blame] | 121 | TEMP_IMG="$(dirname ${SRC_IMAGE})/vm_temp_image.bin" |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 122 | |
| 123 | # If we're not building for VMWare, don't build the vmx |
| 124 | if [ "${FLAGS_format}" != "vmware" ]; then |
| 125 | FLAGS_make_vmx="${FLAGS_FALSE}" |
| 126 | fi |
| 127 | |
| 128 | # Convert args to paths. Need eval to un-quote the string so that shell |
| 129 | # chars like ~ are processed; just doing FOO=`readlink -f $FOO` won't work. |
| 130 | FLAGS_from=`eval readlink -f $FLAGS_from` |
| 131 | FLAGS_to=`eval readlink -f $FLAGS_to` |
| 132 | |
| 133 | # Split apart the partitions and make some new ones |
| 134 | TEMP_DIR=$(mktemp -d) |
| 135 | (cd "${TEMP_DIR}" && |
| 136 | "${FLAGS_from}/unpack_partitions.sh" "${SRC_IMAGE}") |
| 137 | |
| 138 | # Fix the kernel command line |
| 139 | TEMP_ESP="${TEMP_DIR}"/part_12 |
| 140 | TEMP_ROOTFS="${TEMP_DIR}"/part_3 |
| 141 | TEMP_STATE="${TEMP_DIR}"/part_1 |
Will Drewry | efce668 | 2010-07-23 19:43:27 -0500 | [diff] [blame] | 142 | TEMP_KERN="${TEMP_DIR}"/part_2 |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 143 | if [ -n "${FLAGS_state_image}" ]; then |
| 144 | TEMP_STATE="${FLAGS_state_image}" |
Rahul Chaturvedi | e770e16 | 2010-07-15 00:35:11 +0530 | [diff] [blame] | 145 | else |
| 146 | # If we have a stateful fs size specified create a new state partition |
| 147 | # of the specified size. |
| 148 | if [ "${FLAGS_statefulfs_size}" -ne -1 ]; then |
| 149 | STATEFUL_SIZE_BYTES=$((1024 * 1024 * ${FLAGS_statefulfs_size})) |
| 150 | original_image_size=$(stat -c%s "${TEMP_STATE}") |
| 151 | if [ "${original_image_size}" -gt "${STATEFUL_SIZE_BYTES}" ]; then |
| 152 | die "Cannot resize stateful image to smaller than original. Exiting." |
| 153 | fi |
| 154 | |
| 155 | echo "Resizing stateful partition to ${FLAGS_statefulfs_size}MB" |
| 156 | STATEFUL_LOOP_DEV=$(sudo losetup -f) |
| 157 | if [ -z "${STATEFUL_LOOP_DEV}" ]; then |
| 158 | die "No free loop device. Free up a loop device or reboot. Exiting." |
| 159 | fi |
| 160 | |
| 161 | # Extend the original file size to the new size. |
| 162 | dd if=/dev/zero of="${TEMP_STATE}" bs=1 count=1 \ |
| 163 | seek=$((STATEFUL_SIZE_BYTES - 1)) |
| 164 | # Resize the partition. |
| 165 | sudo losetup "${STATEFUL_LOOP_DEV}" "${TEMP_STATE}" |
| 166 | sudo e2fsck -f "${STATEFUL_LOOP_DEV}" |
| 167 | sudo resize2fs "${STATEFUL_LOOP_DEV}" |
| 168 | sudo losetup -d "${STATEFUL_LOOP_DEV}" |
| 169 | fi |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 170 | fi |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 171 | TEMP_PMBR="${TEMP_DIR}"/pmbr |
| 172 | dd if="${SRC_IMAGE}" of="${TEMP_PMBR}" bs=512 count=1 |
| 173 | |
| 174 | TEMP_MNT=$(mktemp -d) |
Will Drewry | efce668 | 2010-07-23 19:43:27 -0500 | [diff] [blame] | 175 | TEMP_ESP_MNT=$(mktemp -d) |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 176 | cleanup() { |
| 177 | sudo umount -d "${TEMP_MNT}" |
Will Drewry | efce668 | 2010-07-23 19:43:27 -0500 | [diff] [blame] | 178 | sudo umount -d "${TEMP_ESP_MNT}" |
| 179 | rmdir "${TEMP_MNT}" "${TEMP_ESP_MNT}" |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 180 | } |
| 181 | trap cleanup INT TERM EXIT |
| 182 | mkdir -p "${TEMP_MNT}" |
| 183 | sudo mount -o loop "${TEMP_ROOTFS}" "${TEMP_MNT}" |
Will Drewry | efce668 | 2010-07-23 19:43:27 -0500 | [diff] [blame] | 184 | mkdir -p "${TEMP_ESP_MNT}" |
| 185 | sudo mount -o loop "${TEMP_ESP}" "${TEMP_ESP_MNT}" |
| 186 | |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 187 | if [ "${FLAGS_format}" = "qemu" ]; then |
Chris Sosa | cc09f84 | 2010-09-21 17:09:51 -0700 | [diff] [blame] | 188 | sudo python "$(dirname $0)/fixup_image_for_qemu.py" \ |
| 189 | --mounted_dir="${TEMP_MNT}" \ |
| 190 | --enable_tablet=true |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 191 | else |
Chris Sosa | cc09f84 | 2010-09-21 17:09:51 -0700 | [diff] [blame] | 192 | sudo python "$(dirname $0)/fixup_image_for_qemu.py" \ |
| 193 | --mounted_dir="${TEMP_MNT}" \ |
| 194 | --enable_tablet=false |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 195 | fi |
Will Drewry | 537caa9 | 2010-08-20 20:30:56 -0500 | [diff] [blame] | 196 | |
| 197 | # Modify the unverified usb template which uses a default usb_disk of sdb3 |
| 198 | sudo sed -i -e 's/sdb3/sda3/g' "${TEMP_MNT}/boot/syslinux/usb.A.cfg" |
| 199 | |
| 200 | # Unmount everything prior to building a final image |
Will Drewry | efce668 | 2010-07-23 19:43:27 -0500 | [diff] [blame] | 201 | sync |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 202 | trap - INT TERM EXIT |
| 203 | cleanup |
| 204 | |
Chris Sosa | cc09f84 | 2010-09-21 17:09:51 -0700 | [diff] [blame] | 205 | # TOOD(adlr): pick a size that will for sure accomodate the partitions. |
Will Drewry | 537caa9 | 2010-08-20 20:30:56 -0500 | [diff] [blame] | 206 | dd if=/dev/zero of="${TEMP_IMG}" bs=1 count=1 \ |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 207 | seek=$((${FLAGS_vdisk_size} * 1024 * 1024 - 1)) |
| 208 | |
Chris Sosa | cc09f84 | 2010-09-21 17:09:51 -0700 | [diff] [blame] | 209 | GPT_FULL="false" |
| 210 | [ "${FLAGS_full}" -eq "${FLAGS_TRUE}" ] && GPT_FULL="true" |
| 211 | |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 212 | # Set up the partition table |
Tan Gao | 0d5f948 | 2010-08-06 08:28:20 -0700 | [diff] [blame] | 213 | install_gpt "${TEMP_IMG}" "$(numsectors $TEMP_ROOTFS)" \ |
| 214 | "$(numsectors $TEMP_STATE)" "${TEMP_PMBR}" "$(numsectors $TEMP_ESP)" \ |
Chris Sosa | cc09f84 | 2010-09-21 17:09:51 -0700 | [diff] [blame] | 215 | "${GPT_FULL}" ${FLAGS_rootfs_partition_size} |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 216 | # Copy into the partition parts of the file |
| 217 | dd if="${TEMP_ROOTFS}" of="${TEMP_IMG}" conv=notrunc bs=512 \ |
| 218 | seek="${START_ROOTFS_A}" |
| 219 | dd if="${TEMP_STATE}" of="${TEMP_IMG}" conv=notrunc bs=512 \ |
| 220 | seek="${START_STATEFUL}" |
| 221 | dd if="${TEMP_KERN}" of="${TEMP_IMG}" conv=notrunc bs=512 \ |
| 222 | seek="${START_KERN_A}" |
| 223 | dd if="${TEMP_ESP}" of="${TEMP_IMG}" conv=notrunc bs=512 \ |
| 224 | seek="${START_ESP}" |
| 225 | |
Will Drewry | 537caa9 | 2010-08-20 20:30:56 -0500 | [diff] [blame] | 226 | # Make the built-image bootable and ensure that the legacy default usb boot |
| 227 | # uses /dev/sda instead of /dev/sdb3. |
| 228 | # NOTE: The TEMP_IMG must live in the same image dir as the original image |
| 229 | # to operate automatically below. |
| 230 | ${SCRIPTS_DIR}/bin/cros_make_image_bootable $(dirname "${TEMP_IMG}") \ |
| 231 | $(basename "${TEMP_IMG}") \ |
| 232 | --usb_disk /dev/sda3 |
| 233 | |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 234 | echo Creating final image |
| 235 | # Convert image to output format |
| 236 | if [ "${FLAGS_format}" = "virtualbox" -o "${FLAGS_format}" = "qemu" ]; then |
| 237 | if [ "${FLAGS_format}" = "virtualbox" ]; then |
| 238 | VBoxManage convertdd "${TEMP_IMG}" "${FLAGS_to}/${FLAGS_vbox_disk}" |
| 239 | else |
| 240 | mv ${TEMP_IMG} ${FLAGS_to}/${DEFAULT_QEMU_IMAGE} |
| 241 | fi |
| 242 | elif [ "${FLAGS_format}" = "vmware" ]; then |
| 243 | qemu-img convert -f raw "${TEMP_IMG}" \ |
| 244 | -O vmdk "${FLAGS_to}/${FLAGS_vmdk}" |
| 245 | else |
| 246 | die "Invalid format: ${FLAGS_format}" |
| 247 | fi |
| 248 | |
| 249 | rm -rf "${TEMP_DIR}" "${TEMP_IMG}" |
| 250 | if [ -z "${FLAGS_state_image}" ]; then |
| 251 | rm -f "${STATE_IMAGE}" |
| 252 | fi |
| 253 | |
| 254 | echo "Created image at ${FLAGS_to}" |
| 255 | |
| 256 | # Generate the vmware config file |
| 257 | # A good reference doc: http://www.sanbarrow.com/vmx.html |
| 258 | VMX_CONFIG="#!/usr/bin/vmware |
| 259 | .encoding = \"UTF-8\" |
| 260 | config.version = \"8\" |
| 261 | virtualHW.version = \"4\" |
| 262 | memsize = \"${FLAGS_mem}\" |
| 263 | ide0:0.present = \"TRUE\" |
| 264 | ide0:0.fileName = \"${FLAGS_vmdk}\" |
| 265 | ethernet0.present = \"TRUE\" |
| 266 | usb.present = \"TRUE\" |
| 267 | sound.present = \"TRUE\" |
| 268 | sound.virtualDev = \"es1371\" |
| 269 | displayName = \"Chromium OS\" |
| 270 | guestOS = \"otherlinux\" |
| 271 | ethernet0.addressType = \"generated\" |
| 272 | floppy0.present = \"FALSE\"" |
| 273 | |
| 274 | if [[ "${FLAGS_make_vmx}" = "${FLAGS_TRUE}" ]]; then |
| 275 | echo "${VMX_CONFIG}" > "${FLAGS_to}/${FLAGS_vmx}" |
| 276 | echo "Wrote the following config to: ${FLAGS_to}/${FLAGS_vmx}" |
| 277 | echo "${VMX_CONFIG}" |
| 278 | fi |
| 279 | |
Olof Johansson | 3ed8d12 | 2010-09-27 13:29:40 -0500 | [diff] [blame] | 280 | |
| 281 | if [ "${FLAGS_format}" == "qemu" ]; then |
| 282 | echo "If you have qemu-kvm installed, you can start the image by:" |
| 283 | echo "sudo kvm -m ${FLAGS_mem} -vga std -pidfile /tmp/kvm.pid -net nic " \ |
| 284 | "-net user,hostfwd=tcp::922-:22 \\" |
| 285 | echo " -hda ${FLAGS_to}/${DEFAULT_QEMU_IMAGE}" |
| 286 | fi |