| OLD | NEW |
| 1 #!/bin/bash | 1 #!/bin/bash |
| 2 | 2 |
| 3 # Copyright (c) 2009 The Chromium OS Authors. All rights reserved. | 3 # Copyright (c) 2009 The Chromium OS Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
| 6 | 6 |
| 7 # Script to convert the output of build_image.sh to a VMware image and write a | 7 # Script to convert the output of build_image.sh to a VMware image and write a |
| 8 # corresponding VMware config file. | 8 # corresponding VMware config file. |
| 9 | 9 |
| 10 # Load common constants. This should be the first executable line. | 10 # Load common constants. This should be the first executable line. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 # Memory units are in MBs | 22 # Memory units are in MBs |
| 23 DEFAULT_MEM="1024" | 23 DEFAULT_MEM="1024" |
| 24 VBOX_TEMP_IMAGE="${IMAGES_DIR}/vbox_temp.img" | 24 VBOX_TEMP_IMAGE="${IMAGES_DIR}/vbox_temp.img" |
| 25 | 25 |
| 26 | 26 |
| 27 # Flags | 27 # Flags |
| 28 DEFINE_string from "$DEFAULT_FROM" \ | 28 DEFINE_string from "$DEFAULT_FROM" \ |
| 29 "Directory containing rootfs.image and mbr.image" | 29 "Directory containing rootfs.image and mbr.image" |
| 30 DEFINE_string to "$DEFAULT_TO" \ | 30 DEFINE_string to "$DEFAULT_TO" \ |
| 31 "Destination folder for VM output file(s)" | 31 "Destination folder for VM output file(s)" |
| 32 DEFINE_string state_image "" \ |
| 33 "Stateful partition image (defaults to creating new statful partition)" |
| 32 DEFINE_string format "vmware" \ | 34 DEFINE_string format "vmware" \ |
| 33 "Output format, either vmware or virtualbox" | 35 "Output format, either vmware or virtualbox" |
| 34 | 36 |
| 35 DEFINE_boolean make_vmx ${FLAGS_TRUE} \ | 37 DEFINE_boolean make_vmx ${FLAGS_TRUE} \ |
| 36 "Create a vmx file for use with vmplayer (vmware only)." | 38 "Create a vmx file for use with vmplayer (vmware only)." |
| 37 DEFINE_string vmdk "$DEFAULT_VMDK" \ | 39 DEFINE_string vmdk "$DEFAULT_VMDK" \ |
| 38 "Filename for the vmware disk image (vmware only)." | 40 "Filename for the vmware disk image (vmware only)." |
| 39 DEFINE_string vmx "$DEFAULT_VMX" \ | 41 DEFINE_string vmx "$DEFAULT_VMX" \ |
| 40 "Filename for the vmware config (vmware only)." | 42 "Filename for the vmware config (vmware only)." |
| 41 DEFINE_integer mem "$DEFAULT_MEM" \ | 43 DEFINE_integer mem "$DEFAULT_MEM" \ |
| (...skipping 11 matching lines...) Expand all Loading... |
| 53 | 55 |
| 54 if [ "$FLAGS_format" != "vmware" ]; then | 56 if [ "$FLAGS_format" != "vmware" ]; then |
| 55 FLAGS_make_vmx=${FLAGS_FALSE} | 57 FLAGS_make_vmx=${FLAGS_FALSE} |
| 56 fi | 58 fi |
| 57 | 59 |
| 58 # Convert args to paths. Need eval to un-quote the string so that shell | 60 # Convert args to paths. Need eval to un-quote the string so that shell |
| 59 # chars like ~ are processed; just doing FOO=`readlink -f $FOO` won't work. | 61 # chars like ~ are processed; just doing FOO=`readlink -f $FOO` won't work. |
| 60 FLAGS_from=`eval readlink -f $FLAGS_from` | 62 FLAGS_from=`eval readlink -f $FLAGS_from` |
| 61 FLAGS_to=`eval readlink -f $FLAGS_to` | 63 FLAGS_to=`eval readlink -f $FLAGS_to` |
| 62 | 64 |
| 63 # Make sure we have the gpt tool | 65 # Split apart the partitions and make some new ones |
| 64 if [ -z "$GPT" ]; then | 66 TEMP_DIR=$(mktemp -d) |
| 65 echo Unable to find gpt | 67 (cd "$TEMP_DIR" && |
| 66 exit 1 | 68 "${FLAGS_from}/unpack_partitions.sh" "${FLAGS_from}/chromiumos_image.bin") |
| 69 |
| 70 # Fix the kernel command line |
| 71 TEMP_ROOTFS="$TEMP_DIR"/part_3 |
| 72 TEMP_STATE="$TEMP_DIR"/part_1 |
| 73 if [ -n "${FLAGS_state_image}" ]; then |
| 74 TEMP_STATE="${FLAGS_state_image}" |
| 67 fi | 75 fi |
| 76 TEMP_KERN="$TEMP_DIR"/part_2 |
| 77 TEMP_PMBR="$TEMP_DIR"/pmbr |
| 78 dd if="${FLAGS_from}/chromiumos_image.bin" of="$TEMP_PMBR" bs=512 count=1 |
| 68 | 79 |
| 69 # Fix bootloader config. | |
| 70 TEMP_IMG=$(mktemp) | |
| 71 TEMP_MNT=$(mktemp -d) | 80 TEMP_MNT=$(mktemp -d) |
| 72 | |
| 73 LOOP_DEV=$(sudo losetup -f) | |
| 74 if [ -z "$LOOP_DEV" ]; then | |
| 75 echo "No free loop device" | |
| 76 exit 1 | |
| 77 fi | |
| 78 | |
| 79 # Get rootfs offset | |
| 80 OFFSET=$(( $(partoffset "${FLAGS_from}/chromiumos_image.bin" 3) * 512 )) # bytes | |
| 81 | |
| 82 echo Copying to temp file | |
| 83 cp "${FLAGS_from}/chromiumos_image.bin" "$TEMP_IMG" | |
| 84 | |
| 85 cleanup() { | 81 cleanup() { |
| 86 sudo umount "$TEMP_MNT" || true | 82 sudo umount -d "$TEMP_MNT" |
| 87 sudo losetup -d "$LOOP_DEV" | 83 rmdir "$TEMP_MNT" |
| 88 } | 84 } |
| 89 trap cleanup INT TERM EXIT | 85 trap cleanup INT TERM EXIT |
| 90 sudo losetup -o $OFFSET "$LOOP_DEV" "$TEMP_IMG" | |
| 91 mkdir -p "$TEMP_MNT" | 86 mkdir -p "$TEMP_MNT" |
| 92 sudo mount "$LOOP_DEV" "$TEMP_MNT" | 87 sudo mount -o loop "$TEMP_ROOTFS" "$TEMP_MNT" |
| 93 sudo "$TEMP_MNT"/postinst /dev/sda3 | 88 sudo "$TEMP_MNT"/postinst /dev/sda3 |
| 94 trap - INT TERM EXIT | 89 trap - INT TERM EXIT |
| 95 cleanup | 90 cleanup |
| 96 rmdir "$TEMP_MNT" | 91 |
| 92 # Make 3 GiB output image |
| 93 TEMP_IMG=$(mktemp) |
| 94 # TOOD(adlr): pick a size that will for sure accomodate the partitions |
| 95 sudo dd if=/dev/zero of="${TEMP_IMG}" bs=1 count=1 \ |
| 96 seek=$((3 * 1024 * 1024 * 1024 - 1)) |
| 97 |
| 98 # Set up the partition table |
| 99 install_gpt "$TEMP_IMG" "$TEMP_ROOTFS" "$TEMP_KERN" "$TEMP_STATE" \ |
| 100 "$TEMP_PMBR" true |
| 101 # Copy into the partition parts of the file |
| 102 dd if="$TEMP_ROOTFS" of="$TEMP_IMG" conv=notrunc bs=512 seek="$START_ROOTFS_A" |
| 103 dd if="$TEMP_STATE" of="$TEMP_IMG" conv=notrunc bs=512 seek="$START_STATEFUL" |
| 104 dd if="$TEMP_KERN" of="$TEMP_IMG" conv=notrunc bs=512 seek="$START_KERN_A" |
| 97 | 105 |
| 98 echo Creating final image | 106 echo Creating final image |
| 99 # Convert image to output format | 107 # Convert image to output format |
| 100 if [ "$FLAGS_format" = "virtualbox" ]; then | 108 if [ "$FLAGS_format" = "virtualbox" ]; then |
| 101 qemu-img convert -f raw $TEMP_IMG \ | 109 qemu-img convert -f raw $TEMP_IMG \ |
| 102 -O raw "${VBOX_TEMP_IMAGE}" | 110 -O raw "${VBOX_TEMP_IMAGE}" |
| 103 VBoxManage convertdd "${VBOX_TEMP_IMAGE}" "${FLAGS_to}/${FLAGS_vbox_disk}" | 111 VBoxManage convertdd "${VBOX_TEMP_IMAGE}" "${FLAGS_to}/${FLAGS_vbox_disk}" |
| 104 elif [ "$FLAGS_format" = "vmware" ]; then | 112 elif [ "$FLAGS_format" = "vmware" ]; then |
| 105 qemu-img convert -f raw $TEMP_IMG \ | 113 qemu-img convert -f raw $TEMP_IMG \ |
| 106 -O vmdk "${FLAGS_to}/${FLAGS_vmdk}" | 114 -O vmdk "${FLAGS_to}/${FLAGS_vmdk}" |
| 107 else | 115 else |
| 108 echo invalid format: "$FLAGS_format" | 116 echo invalid format: "$FLAGS_format" |
| 109 exit 1 | 117 exit 1 |
| 110 fi | 118 fi |
| 111 | 119 |
| 112 rm -f "$TEMP_IMG" "${VBOX_TEMP_IMAGE}" | 120 rm -rf "$TEMP_DIR" "${VBOX_TEMP_IMAGE}" "$TEMP_IMG" |
| 121 if [ -z "$FLAGS_state_image" ]; then |
| 122 rm -f "$STATE_IMAGE" |
| 123 fi |
| 113 | 124 |
| 114 echo "Created image ${FLAGS_to}" | 125 echo "Created image ${FLAGS_to}" |
| 115 | 126 |
| 116 # Generate the vmware config file | 127 # Generate the vmware config file |
| 117 # A good reference doc: http://www.sanbarrow.com/vmx.html | 128 # A good reference doc: http://www.sanbarrow.com/vmx.html |
| 118 VMX_CONFIG="#!/usr/bin/vmware | 129 VMX_CONFIG="#!/usr/bin/vmware |
| 119 .encoding = \"UTF-8\" | 130 .encoding = \"UTF-8\" |
| 120 config.version = \"8\" | 131 config.version = \"8\" |
| 121 virtualHW.version = \"4\" | 132 virtualHW.version = \"4\" |
| 122 memsize = \"${FLAGS_mem}\" | 133 memsize = \"${FLAGS_mem}\" |
| 123 ide0:0.present = \"TRUE\" | 134 ide0:0.present = \"TRUE\" |
| 124 ide0:0.fileName = \"${FLAGS_vmdk}\" | 135 ide0:0.fileName = \"${FLAGS_vmdk}\" |
| 125 ethernet0.present = \"TRUE\" | 136 ethernet0.present = \"TRUE\" |
| 126 usb.present = \"TRUE\" | 137 usb.present = \"TRUE\" |
| 127 sound.present = \"TRUE\" | 138 sound.present = \"TRUE\" |
| 128 sound.virtualDev = \"es1371\" | 139 sound.virtualDev = \"es1371\" |
| 129 displayName = \"Chromium OS\" | 140 displayName = \"Chromium OS\" |
| 130 guestOS = \"otherlinux\" | 141 guestOS = \"otherlinux\" |
| 131 ethernet0.addressType = \"generated\" | 142 ethernet0.addressType = \"generated\" |
| 132 floppy0.present = \"FALSE\"" | 143 floppy0.present = \"FALSE\"" |
| 133 | 144 |
| 134 if [[ ${FLAGS_make_vmx} = ${FLAGS_TRUE} ]]; then | 145 if [[ ${FLAGS_make_vmx} = ${FLAGS_TRUE} ]]; then |
| 135 echo "${VMX_CONFIG}" > "${FLAGS_to}/${FLAGS_vmx}" | 146 echo "${VMX_CONFIG}" > "${FLAGS_to}/${FLAGS_vmx}" |
| 136 echo "Wrote the following config to: ${FLAGS_to}/${FLAGS_vmx}" | 147 echo "Wrote the following config to: ${FLAGS_to}/${FLAGS_vmx}" |
| 137 echo "${VMX_CONFIG}" | 148 echo "${VMX_CONFIG}" |
| 138 fi | 149 fi |
| 139 | 150 |
| OLD | NEW |