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" |
Zelidrag Hornung | 478a0d4 | 2010-07-14 11:47:39 -0700 | [diff] [blame] | 20 | DEFAULT_QEMU_IMAGE="chromiumos_qemu_image.bin" |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 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)" |
Rahul Chaturvedi | e770e16 | 2010-07-15 00:35:11 +0530 | [diff] [blame] | 44 | DEFINE_integer statefulfs_size -1 \ |
| 45 | "Stateful partition size in MBs." |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 46 | DEFINE_boolean test_image "${FLAGS_FALSE}" \ |
| 47 | "Copies normal image to chromiumos_test_image.bin, modifies it for test." |
| 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)." |
| 52 | DEFINE_integer vdisk_size 3072 \ |
| 53 | "virtual disk size in MBs." |
| 54 | DEFINE_string vmdk "${DEFAULT_VMDK}" \ |
| 55 | "Filename for the vmware disk image (vmware only)." |
| 56 | DEFINE_string vmx "${DEFAULT_VMX}" \ |
| 57 | "Filename for the vmware config (vmware only)." |
| 58 | |
| 59 | # Parse command line |
| 60 | FLAGS "$@" || exit 1 |
| 61 | eval set -- "${FLAGS_ARGV}" |
| 62 | |
| 63 | # Die on any errors. |
| 64 | set -e |
| 65 | |
| 66 | if [ -z "${FLAGS_board}" ] ; then |
| 67 | die "--board is required." |
| 68 | fi |
| 69 | |
| 70 | IMAGES_DIR="${DEFAULT_BUILD_ROOT}/images/${FLAGS_board}" |
| 71 | # Default to the most recent image |
| 72 | if [ -z "${FLAGS_from}" ] ; then |
| 73 | FLAGS_from="${IMAGES_DIR}/$(ls -t $IMAGES_DIR | head -1)" |
Zelidrag Hornung | 42ca818 | 2010-07-12 18:08:44 -0700 | [diff] [blame] | 74 | else |
| 75 | pushd "${FLAGS_from}" && FLAGS_from=`pwd` && popd |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 76 | fi |
| 77 | if [ -z "${FLAGS_to}" ] ; then |
| 78 | FLAGS_to="${FLAGS_from}" |
| 79 | fi |
| 80 | |
| 81 | # Use this image as the source image to copy |
| 82 | SRC_IMAGE="${FLAGS_from}/chromiumos_image.bin" |
| 83 | |
| 84 | # If we're asked to modify the image for test, then let's make a copy and |
| 85 | # modify that instead. |
| 86 | if [ ${FLAGS_test_image} -eq ${FLAGS_TRUE} ] ; then |
| 87 | if [ ! -f "${FLAGS_from}/chromiumos_test_image.bin" ] || \ |
| 88 | [ ${FLAGS_force_copy} -eq ${FLAGS_TRUE} ] ; then |
| 89 | # Copy it. |
| 90 | echo "Creating test image from original..." |
| 91 | cp -f "${SRC_IMAGE}" "${FLAGS_from}/chromiumos_test_image.bin" |
| 92 | |
| 93 | # Check for manufacturing image. |
| 94 | if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ] ; then |
| 95 | EXTRA_ARGS="--factory" |
| 96 | fi |
| 97 | |
| 98 | # Check for install shim. |
| 99 | if [ ${FLAGS_factory_install} -eq ${FLAGS_TRUE} ] ; then |
| 100 | EXTRA_ARGS="--factory_install" |
| 101 | fi |
| 102 | |
| 103 | # Modify it. Pass --yes so that mod_image_for_test.sh won't ask us if we |
| 104 | # really want to modify the image; the user gave their assent already with |
| 105 | # --test-image and the original image is going to be preserved. |
| 106 | "${SCRIPTS_DIR}/mod_image_for_test.sh" --image \ |
| 107 | "${FLAGS_from}/chromiumos_test_image.bin" ${EXTRA_ARGS} --yes |
| 108 | echo "Done with mod_image_for_test." |
| 109 | else |
| 110 | echo "Using cached test image." |
| 111 | fi |
| 112 | SRC_IMAGE="${FLAGS_from}/chromiumos_test_image.bin" |
| 113 | echo "Source test image is: ${SRC_IMAGE}" |
| 114 | fi |
| 115 | |
| 116 | # Memory units are in MBs |
| 117 | DEFAULT_MEM="1024" |
| 118 | TEMP_IMAGE="${IMAGES_DIR}/temp_image.img" |
| 119 | |
| 120 | |
| 121 | # If we're not building for VMWare, don't build the vmx |
| 122 | if [ "${FLAGS_format}" != "vmware" ]; then |
| 123 | FLAGS_make_vmx="${FLAGS_FALSE}" |
| 124 | fi |
| 125 | |
| 126 | # Convert args to paths. Need eval to un-quote the string so that shell |
| 127 | # chars like ~ are processed; just doing FOO=`readlink -f $FOO` won't work. |
| 128 | FLAGS_from=`eval readlink -f $FLAGS_from` |
| 129 | FLAGS_to=`eval readlink -f $FLAGS_to` |
| 130 | |
| 131 | # Split apart the partitions and make some new ones |
| 132 | TEMP_DIR=$(mktemp -d) |
| 133 | (cd "${TEMP_DIR}" && |
| 134 | "${FLAGS_from}/unpack_partitions.sh" "${SRC_IMAGE}") |
| 135 | |
| 136 | # Fix the kernel command line |
| 137 | TEMP_ESP="${TEMP_DIR}"/part_12 |
| 138 | TEMP_ROOTFS="${TEMP_DIR}"/part_3 |
| 139 | TEMP_STATE="${TEMP_DIR}"/part_1 |
Will Drewry | efce668 | 2010-07-23 19:43:27 -0500 | [diff] [blame^] | 140 | TEMP_KERN="${TEMP_DIR}"/part_2 |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 141 | if [ -n "${FLAGS_state_image}" ]; then |
| 142 | TEMP_STATE="${FLAGS_state_image}" |
Rahul Chaturvedi | e770e16 | 2010-07-15 00:35:11 +0530 | [diff] [blame] | 143 | else |
| 144 | # If we have a stateful fs size specified create a new state partition |
| 145 | # of the specified size. |
| 146 | if [ "${FLAGS_statefulfs_size}" -ne -1 ]; then |
| 147 | STATEFUL_SIZE_BYTES=$((1024 * 1024 * ${FLAGS_statefulfs_size})) |
| 148 | original_image_size=$(stat -c%s "${TEMP_STATE}") |
| 149 | if [ "${original_image_size}" -gt "${STATEFUL_SIZE_BYTES}" ]; then |
| 150 | die "Cannot resize stateful image to smaller than original. Exiting." |
| 151 | fi |
| 152 | |
| 153 | echo "Resizing stateful partition to ${FLAGS_statefulfs_size}MB" |
| 154 | STATEFUL_LOOP_DEV=$(sudo losetup -f) |
| 155 | if [ -z "${STATEFUL_LOOP_DEV}" ]; then |
| 156 | die "No free loop device. Free up a loop device or reboot. Exiting." |
| 157 | fi |
| 158 | |
| 159 | # Extend the original file size to the new size. |
| 160 | dd if=/dev/zero of="${TEMP_STATE}" bs=1 count=1 \ |
| 161 | seek=$((STATEFUL_SIZE_BYTES - 1)) |
| 162 | # Resize the partition. |
| 163 | sudo losetup "${STATEFUL_LOOP_DEV}" "${TEMP_STATE}" |
| 164 | sudo e2fsck -f "${STATEFUL_LOOP_DEV}" |
| 165 | sudo resize2fs "${STATEFUL_LOOP_DEV}" |
| 166 | sudo losetup -d "${STATEFUL_LOOP_DEV}" |
| 167 | fi |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 168 | fi |
| 169 | TEMP_KERN="${TEMP_DIR}"/part_2 |
| 170 | TEMP_PMBR="${TEMP_DIR}"/pmbr |
| 171 | dd if="${SRC_IMAGE}" of="${TEMP_PMBR}" bs=512 count=1 |
| 172 | |
| 173 | TEMP_MNT=$(mktemp -d) |
Will Drewry | efce668 | 2010-07-23 19:43:27 -0500 | [diff] [blame^] | 174 | TEMP_ESP_MNT=$(mktemp -d) |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 175 | cleanup() { |
| 176 | sudo umount -d "${TEMP_MNT}" |
Will Drewry | efce668 | 2010-07-23 19:43:27 -0500 | [diff] [blame^] | 177 | sudo umount -d "${TEMP_ESP_MNT}" |
| 178 | rmdir "${TEMP_MNT}" "${TEMP_ESP_MNT}" |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 179 | } |
| 180 | trap cleanup INT TERM EXIT |
| 181 | mkdir -p "${TEMP_MNT}" |
| 182 | sudo mount -o loop "${TEMP_ROOTFS}" "${TEMP_MNT}" |
Will Drewry | efce668 | 2010-07-23 19:43:27 -0500 | [diff] [blame^] | 183 | mkdir -p "${TEMP_ESP_MNT}" |
| 184 | sudo mount -o loop "${TEMP_ESP}" "${TEMP_ESP_MNT}" |
| 185 | |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 186 | if [ "${FLAGS_format}" = "qemu" ]; then |
| 187 | sudo python ./fixup_image_for_qemu.py --mounted_dir="${TEMP_MNT}" \ |
Rahul Chaturvedi | e79e88e | 2010-07-21 02:04:59 +0530 | [diff] [blame] | 188 | --enable_tablet=true |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 189 | else |
| 190 | sudo python ./fixup_image_for_qemu.py --mounted_dir="${TEMP_MNT}" \ |
Rahul Chaturvedi | e79e88e | 2010-07-21 02:04:59 +0530 | [diff] [blame] | 191 | --enable_tablet=false |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 192 | fi |
Will Drewry | efce668 | 2010-07-23 19:43:27 -0500 | [diff] [blame^] | 193 | # Remount read-only so that when we call setimage, it will recreate correct |
| 194 | # boot hashes for verifying the rootfs integrity. This is a bit of a cheat |
| 195 | # but it will have to do. We don't assume legacy bootloaders are secure so |
| 196 | # we update the hash too, but the hash in part_2 doesn't change which would |
| 197 | # cause failures on a Chrome OS boot (without re-running build_kernel_image). |
| 198 | sudo mount -o remount,ro "${TEMP_MNT}" |
| 199 | sync |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 200 | |
Will Drewry | efce668 | 2010-07-23 19:43:27 -0500 | [diff] [blame^] | 201 | # Check if the current image was build with --enable_rootfs_verification |
| 202 | enable_rootfs_verification= |
| 203 | if grep -qE '^chromeos-v' "${TEMP_ESP_MNT}"/syslinux/default.cfg; then |
| 204 | enable_rootfs_verification=--enable_rootfs_verification |
| 205 | fi |
| 206 | |
| 207 | # Update the bootloader and verified hashes for the given rootfs in the |
| 208 | # vm and fixup changes. |
| 209 | DST_DEV=/dev/sda |
| 210 | BOOT_SLOT=A |
| 211 | syslinux_cfg="${TEMP_MNT}/boot/syslinux/root.${BOOT_SLOT}.cfg" |
| 212 | grub_cfg="${TEMP_MNT}/boot/efi/boot/grub.cfg" |
| 213 | sudo "${TEMP_MNT}"/usr/sbin/chromeos-setimage ${BOOT_SLOT} \ |
| 214 | --dst=${DST_DEV} --run_as_root \ |
| 215 | --update_syslinux_cfg="${syslinux_cfg}" \ |
| 216 | --update_grub_cfg="${grub_cfg}" \ |
| 217 | --rootfs_image="${TEMP_ROOTFS}" \ |
| 218 | --esp_mounted_at="${TEMP_ESP_MNT}" \ |
| 219 | --kernel_image="${TEMP_KERN}" \ |
| 220 | --update_vmlinuz=${TEMP_MNT}/boot/vmlinuz \ |
| 221 | ${enable_rootfs_verification} |
| 222 | |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 223 | trap - INT TERM EXIT |
| 224 | cleanup |
| 225 | |
| 226 | # Make 3 GiB output image |
| 227 | TEMP_IMG=$(mktemp) |
| 228 | # TOOD(adlr): pick a size that will for sure accomodate the partitions |
| 229 | sudo dd if=/dev/zero of="${TEMP_IMG}" bs=1 count=1 \ |
| 230 | seek=$((${FLAGS_vdisk_size} * 1024 * 1024 - 1)) |
| 231 | |
| 232 | # Set up the partition table |
| 233 | install_gpt "${TEMP_IMG}" "${TEMP_ROOTFS}" "${TEMP_KERN}" "${TEMP_STATE}" \ |
Zelidrag Hornung | 42ca818 | 2010-07-12 18:08:44 -0700 | [diff] [blame] | 234 | "${TEMP_PMBR}" "${TEMP_ESP}" false ${FLAGS_rootfs_partition_size} |
Rahul Chaturvedi | b5643e8 | 2010-07-09 10:46:05 +0530 | [diff] [blame] | 235 | # Copy into the partition parts of the file |
| 236 | dd if="${TEMP_ROOTFS}" of="${TEMP_IMG}" conv=notrunc bs=512 \ |
| 237 | seek="${START_ROOTFS_A}" |
| 238 | dd if="${TEMP_STATE}" of="${TEMP_IMG}" conv=notrunc bs=512 \ |
| 239 | seek="${START_STATEFUL}" |
| 240 | dd if="${TEMP_KERN}" of="${TEMP_IMG}" conv=notrunc bs=512 \ |
| 241 | seek="${START_KERN_A}" |
| 242 | dd if="${TEMP_ESP}" of="${TEMP_IMG}" conv=notrunc bs=512 \ |
| 243 | seek="${START_ESP}" |
| 244 | |
| 245 | echo Creating final image |
| 246 | # Convert image to output format |
| 247 | if [ "${FLAGS_format}" = "virtualbox" -o "${FLAGS_format}" = "qemu" ]; then |
| 248 | if [ "${FLAGS_format}" = "virtualbox" ]; then |
| 249 | VBoxManage convertdd "${TEMP_IMG}" "${FLAGS_to}/${FLAGS_vbox_disk}" |
| 250 | else |
| 251 | mv ${TEMP_IMG} ${FLAGS_to}/${DEFAULT_QEMU_IMAGE} |
| 252 | fi |
| 253 | elif [ "${FLAGS_format}" = "vmware" ]; then |
| 254 | qemu-img convert -f raw "${TEMP_IMG}" \ |
| 255 | -O vmdk "${FLAGS_to}/${FLAGS_vmdk}" |
| 256 | else |
| 257 | die "Invalid format: ${FLAGS_format}" |
| 258 | fi |
| 259 | |
| 260 | rm -rf "${TEMP_DIR}" "${TEMP_IMG}" |
| 261 | if [ -z "${FLAGS_state_image}" ]; then |
| 262 | rm -f "${STATE_IMAGE}" |
| 263 | fi |
| 264 | |
| 265 | echo "Created image at ${FLAGS_to}" |
| 266 | |
| 267 | # Generate the vmware config file |
| 268 | # A good reference doc: http://www.sanbarrow.com/vmx.html |
| 269 | VMX_CONFIG="#!/usr/bin/vmware |
| 270 | .encoding = \"UTF-8\" |
| 271 | config.version = \"8\" |
| 272 | virtualHW.version = \"4\" |
| 273 | memsize = \"${FLAGS_mem}\" |
| 274 | ide0:0.present = \"TRUE\" |
| 275 | ide0:0.fileName = \"${FLAGS_vmdk}\" |
| 276 | ethernet0.present = \"TRUE\" |
| 277 | usb.present = \"TRUE\" |
| 278 | sound.present = \"TRUE\" |
| 279 | sound.virtualDev = \"es1371\" |
| 280 | displayName = \"Chromium OS\" |
| 281 | guestOS = \"otherlinux\" |
| 282 | ethernet0.addressType = \"generated\" |
| 283 | floppy0.present = \"FALSE\"" |
| 284 | |
| 285 | if [[ "${FLAGS_make_vmx}" = "${FLAGS_TRUE}" ]]; then |
| 286 | echo "${VMX_CONFIG}" > "${FLAGS_to}/${FLAGS_vmx}" |
| 287 | echo "Wrote the following config to: ${FLAGS_to}/${FLAGS_vmx}" |
| 288 | echo "${VMX_CONFIG}" |
| 289 | fi |
| 290 | |