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" |
| 14 | |
| 15 | get_default_board |
| 16 | |
| 17 | DEFAULT_VMDK="ide.vmdk" |
| 18 | DEFAULT_VMX="chromiumos.vmx" |
| 19 | DEFAULT_VBOX_DISK="os.vdi" |
| 20 | DEFAULT_QEMU_IMAGE="chromiumos_qemu.image" |
| 21 | |
| 22 | MOD_SCRIPTS_ROOT="${GCLIENT_ROOT}/src/scripts/mod_for_test_scripts" |
| 23 | |
| 24 | # Flags |
| 25 | DEFINE_string board "${DEFAULT_BOARD}" \ |
| 26 | "Board for which the image was built" |
| 27 | DEFINE_boolean factory $FLAGS_FALSE \ |
| 28 | "Modify the image for manufacturing testing" |
| 29 | DEFINE_boolean factory_install $FLAGS_FALSE \ |
| 30 | "Modify the image for factory install shim" |
| 31 | DEFINE_boolean force_copy ${FLAGS_FALSE} "Always rebuild test image" |
| 32 | DEFINE_string format "qemu" \ |
| 33 | "Output format, either qemu, vmware or virtualbox" |
| 34 | DEFINE_string from "" \ |
| 35 | "Directory containing rootfs.image and mbr.image" |
| 36 | DEFINE_boolean make_vmx ${FLAGS_TRUE} \ |
| 37 | "Create a vmx file for use with vmplayer (vmware only)." |
| 38 | DEFINE_integer mem "${DEFAULT_MEM}" \ |
| 39 | "Memory size for the vm config in MBs (vmware only)." |
| 40 | DEFINE_integer rootfs_partition_size 1024 \ |
| 41 | "rootfs parition size in MBs." |
| 42 | DEFINE_string state_image "" \ |
| 43 | "Stateful partition image (defaults to creating new statful partition)" |
| 44 | DEFINE_boolean test_image "${FLAGS_FALSE}" \ |
| 45 | "Copies normal image to chromiumos_test_image.bin, modifies it for test." |
| 46 | DEFINE_string to "" \ |
| 47 | "Destination folder for VM output file(s)" |
| 48 | DEFINE_string vbox_disk "${DEFAULT_VBOX_DISK}" \ |
| 49 | "Filename for the output disk (virtualbox only)." |
| 50 | DEFINE_integer vdisk_size 3072 \ |
| 51 | "virtual disk size in MBs." |
| 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 | |
| 57 | # Parse command line |
| 58 | FLAGS "$@" || exit 1 |
| 59 | eval set -- "${FLAGS_ARGV}" |
| 60 | |
| 61 | # Die on any errors. |
| 62 | set -e |
| 63 | |
| 64 | if [ -z "${FLAGS_board}" ] ; then |
| 65 | die "--board is required." |
| 66 | fi |
| 67 | |
| 68 | IMAGES_DIR="${DEFAULT_BUILD_ROOT}/images/${FLAGS_board}" |
| 69 | # Default to the most recent image |
| 70 | if [ -z "${FLAGS_from}" ] ; then |
| 71 | FLAGS_from="${IMAGES_DIR}/$(ls -t $IMAGES_DIR | head -1)" |
| 72 | fi |
| 73 | if [ -z "${FLAGS_to}" ] ; then |
| 74 | FLAGS_to="${FLAGS_from}" |
| 75 | fi |
| 76 | |
| 77 | # Use this image as the source image to copy |
| 78 | SRC_IMAGE="${FLAGS_from}/chromiumos_image.bin" |
| 79 | |
| 80 | # If we're asked to modify the image for test, then let's make a copy and |
| 81 | # modify that instead. |
| 82 | if [ ${FLAGS_test_image} -eq ${FLAGS_TRUE} ] ; then |
| 83 | if [ ! -f "${FLAGS_from}/chromiumos_test_image.bin" ] || \ |
| 84 | [ ${FLAGS_force_copy} -eq ${FLAGS_TRUE} ] ; then |
| 85 | # Copy it. |
| 86 | echo "Creating test image from original..." |
| 87 | cp -f "${SRC_IMAGE}" "${FLAGS_from}/chromiumos_test_image.bin" |
| 88 | |
| 89 | # Check for manufacturing image. |
| 90 | if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ] ; then |
| 91 | EXTRA_ARGS="--factory" |
| 92 | fi |
| 93 | |
| 94 | # Check for install shim. |
| 95 | if [ ${FLAGS_factory_install} -eq ${FLAGS_TRUE} ] ; then |
| 96 | EXTRA_ARGS="--factory_install" |
| 97 | fi |
| 98 | |
| 99 | # Modify it. Pass --yes so that mod_image_for_test.sh won't ask us if we |
| 100 | # really want to modify the image; the user gave their assent already with |
| 101 | # --test-image and the original image is going to be preserved. |
| 102 | "${SCRIPTS_DIR}/mod_image_for_test.sh" --image \ |
| 103 | "${FLAGS_from}/chromiumos_test_image.bin" ${EXTRA_ARGS} --yes |
| 104 | echo "Done with mod_image_for_test." |
| 105 | else |
| 106 | echo "Using cached test image." |
| 107 | fi |
| 108 | SRC_IMAGE="${FLAGS_from}/chromiumos_test_image.bin" |
| 109 | echo "Source test image is: ${SRC_IMAGE}" |
| 110 | fi |
| 111 | |
| 112 | # Memory units are in MBs |
| 113 | DEFAULT_MEM="1024" |
| 114 | TEMP_IMAGE="${IMAGES_DIR}/temp_image.img" |
| 115 | |
| 116 | |
| 117 | # If we're not building for VMWare, don't build the vmx |
| 118 | if [ "${FLAGS_format}" != "vmware" ]; then |
| 119 | FLAGS_make_vmx="${FLAGS_FALSE}" |
| 120 | fi |
| 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. |
| 124 | FLAGS_from=`eval readlink -f $FLAGS_from` |
| 125 | FLAGS_to=`eval readlink -f $FLAGS_to` |
| 126 | |
| 127 | # Split apart the partitions and make some new ones |
| 128 | TEMP_DIR=$(mktemp -d) |
| 129 | (cd "${TEMP_DIR}" && |
| 130 | "${FLAGS_from}/unpack_partitions.sh" "${SRC_IMAGE}") |
| 131 | |
| 132 | # Fix the kernel command line |
| 133 | TEMP_ESP="${TEMP_DIR}"/part_12 |
| 134 | TEMP_ROOTFS="${TEMP_DIR}"/part_3 |
| 135 | TEMP_STATE="${TEMP_DIR}"/part_1 |
| 136 | if [ -n "${FLAGS_state_image}" ]; then |
| 137 | TEMP_STATE="${FLAGS_state_image}" |
| 138 | fi |
| 139 | TEMP_KERN="${TEMP_DIR}"/part_2 |
| 140 | TEMP_PMBR="${TEMP_DIR}"/pmbr |
| 141 | dd if="${SRC_IMAGE}" of="${TEMP_PMBR}" bs=512 count=1 |
| 142 | |
| 143 | TEMP_MNT=$(mktemp -d) |
| 144 | cleanup() { |
| 145 | sudo umount -d "${TEMP_MNT}" |
| 146 | rmdir "${TEMP_MNT}" |
| 147 | } |
| 148 | trap cleanup INT TERM EXIT |
| 149 | mkdir -p "${TEMP_MNT}" |
| 150 | sudo mount -o loop "${TEMP_ROOTFS}" "${TEMP_MNT}" |
| 151 | if [ "${FLAGS_format}" = "qemu" ]; then |
| 152 | sudo python ./fixup_image_for_qemu.py --mounted_dir="${TEMP_MNT}" \ |
| 153 | --for_qemu=true |
| 154 | else |
| 155 | sudo python ./fixup_image_for_qemu.py --mounted_dir="${TEMP_MNT}" \ |
| 156 | --for_qemu=false |
| 157 | fi |
| 158 | |
| 159 | # Change this value if the rootfs partition changes |
| 160 | ROOTFS_PARTITION=/dev/sda3 |
| 161 | sudo "${TEMP_MNT}"/postinst_vm "${ROOTFS_PARTITION}" |
| 162 | trap - INT TERM EXIT |
| 163 | cleanup |
| 164 | |
| 165 | # Make 3 GiB output image |
| 166 | TEMP_IMG=$(mktemp) |
| 167 | # TOOD(adlr): pick a size that will for sure accomodate the partitions |
| 168 | sudo dd if=/dev/zero of="${TEMP_IMG}" bs=1 count=1 \ |
| 169 | seek=$((${FLAGS_vdisk_size} * 1024 * 1024 - 1)) |
| 170 | |
| 171 | # Set up the partition table |
| 172 | install_gpt "${TEMP_IMG}" "${TEMP_ROOTFS}" "${TEMP_KERN}" "${TEMP_STATE}" \ |
| 173 | "${TEMP_PMBR}" "${TEMP_ESP}" true false ${FLAGS_rootfs_partition_size} |
| 174 | # Copy into the partition parts of the file |
| 175 | dd if="${TEMP_ROOTFS}" of="${TEMP_IMG}" conv=notrunc bs=512 \ |
| 176 | seek="${START_ROOTFS_A}" |
| 177 | dd if="${TEMP_STATE}" of="${TEMP_IMG}" conv=notrunc bs=512 \ |
| 178 | seek="${START_STATEFUL}" |
| 179 | dd if="${TEMP_KERN}" of="${TEMP_IMG}" conv=notrunc bs=512 \ |
| 180 | seek="${START_KERN_A}" |
| 181 | dd if="${TEMP_ESP}" of="${TEMP_IMG}" conv=notrunc bs=512 \ |
| 182 | seek="${START_ESP}" |
| 183 | |
| 184 | echo Creating final image |
| 185 | # Convert image to output format |
| 186 | if [ "${FLAGS_format}" = "virtualbox" -o "${FLAGS_format}" = "qemu" ]; then |
| 187 | if [ "${FLAGS_format}" = "virtualbox" ]; then |
| 188 | VBoxManage convertdd "${TEMP_IMG}" "${FLAGS_to}/${FLAGS_vbox_disk}" |
| 189 | else |
| 190 | mv ${TEMP_IMG} ${FLAGS_to}/${DEFAULT_QEMU_IMAGE} |
| 191 | fi |
| 192 | elif [ "${FLAGS_format}" = "vmware" ]; then |
| 193 | qemu-img convert -f raw "${TEMP_IMG}" \ |
| 194 | -O vmdk "${FLAGS_to}/${FLAGS_vmdk}" |
| 195 | else |
| 196 | die "Invalid format: ${FLAGS_format}" |
| 197 | fi |
| 198 | |
| 199 | rm -rf "${TEMP_DIR}" "${TEMP_IMG}" |
| 200 | if [ -z "${FLAGS_state_image}" ]; then |
| 201 | rm -f "${STATE_IMAGE}" |
| 202 | fi |
| 203 | |
| 204 | echo "Created image at ${FLAGS_to}" |
| 205 | |
| 206 | # Generate the vmware config file |
| 207 | # A good reference doc: http://www.sanbarrow.com/vmx.html |
| 208 | VMX_CONFIG="#!/usr/bin/vmware |
| 209 | .encoding = \"UTF-8\" |
| 210 | config.version = \"8\" |
| 211 | virtualHW.version = \"4\" |
| 212 | memsize = \"${FLAGS_mem}\" |
| 213 | ide0:0.present = \"TRUE\" |
| 214 | ide0:0.fileName = \"${FLAGS_vmdk}\" |
| 215 | ethernet0.present = \"TRUE\" |
| 216 | usb.present = \"TRUE\" |
| 217 | sound.present = \"TRUE\" |
| 218 | sound.virtualDev = \"es1371\" |
| 219 | displayName = \"Chromium OS\" |
| 220 | guestOS = \"otherlinux\" |
| 221 | ethernet0.addressType = \"generated\" |
| 222 | floppy0.present = \"FALSE\"" |
| 223 | |
| 224 | if [[ "${FLAGS_make_vmx}" = "${FLAGS_TRUE}" ]]; then |
| 225 | echo "${VMX_CONFIG}" > "${FLAGS_to}/${FLAGS_vmx}" |
| 226 | echo "Wrote the following config to: ${FLAGS_to}/${FLAGS_vmx}" |
| 227 | echo "${VMX_CONFIG}" |
| 228 | fi |
| 229 | |