| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 1 | #!/bin/bash | 
|  | 2 | # | 
|  | 3 | # Copyright (c) 2011 Citrix Systems, Inc. | 
| ZhiQiang Fan | 7d56215 | 2013-09-20 02:20:35 +0800 | [diff] [blame] | 4 | # Copyright 2011 OpenStack Foundation | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 5 | # All Rights Reserved. | 
|  | 6 | # | 
|  | 7 | #    Licensed under the Apache License, Version 2.0 (the "License"); you may | 
|  | 8 | #    not use this file except in compliance with the License. You may obtain | 
|  | 9 | #    a copy of the License at | 
|  | 10 | # | 
|  | 11 | #         http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 12 | # | 
|  | 13 | #    Unless required by applicable law or agreed to in writing, software | 
|  | 14 | #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | 
|  | 15 | #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | 
|  | 16 | #    License for the specific language governing permissions and limitations | 
|  | 17 | #    under the License. | 
|  | 18 | # | 
|  | 19 |  | 
|  | 20 | set -eu | 
|  | 21 |  | 
|  | 22 | set -o xtrace | 
|  | 23 |  | 
|  | 24 | VBOX_IMG=/output/packages/vbox-img | 
|  | 25 |  | 
|  | 26 | usage() { | 
|  | 27 | cat >&2 <<EOF | 
|  | 28 | $0 -o <output filenames> -t <types> -x <xml files> <fs-staging-dir> <fs-size-MiB> <tmpdir> | 
|  | 29 | -o: Colon-separated list of output filenames (one for each type). | 
|  | 30 | -p: Create a disk label and partition within the output image | 
|  | 31 | -t: Colon-separated list of types of output file.  xva and ovf supported. | 
|  | 32 | -x: XML filenames (one for each type) | 
|  | 33 |  | 
|  | 34 | EOF | 
|  | 35 | exit 1 | 
|  | 36 | } | 
|  | 37 |  | 
|  | 38 | # parse cmdline | 
|  | 39 |  | 
|  | 40 | OPT_USE_PARTITION= | 
|  | 41 | OPT_TYPES= | 
|  | 42 | OPT_OUTPUT_FILES= | 
|  | 43 | OPT_XML_FILES= | 
|  | 44 |  | 
|  | 45 | while getopts o:pt:x: o | 
|  | 46 | do case "$o" in | 
|  | 47 | o)    OPT_OUTPUT_FILES=$(echo "$OPTARG" | sed -e 's/\s*:\s*/ /g') | 
|  | 48 | ;; | 
|  | 49 | p)    OPT_USE_PARTITION=1 | 
|  | 50 | ;; | 
|  | 51 | t)    OPT_TYPES=$(echo "$OPTARG" | sed -e 's/\s*:\s*/ /g') | 
|  | 52 | ;; | 
|  | 53 | x)    OPT_XML_FILES=$(echo "$OPTARG" | sed -e 's/\s*:\s*/ /g') | 
|  | 54 | ;; | 
|  | 55 | [?])  usage | 
|  | 56 | ;; | 
|  | 57 | esac | 
|  | 58 | done | 
|  | 59 | shift $((OPTIND-1)) | 
|  | 60 |  | 
|  | 61 | [ $# -ne 3 ] && usage | 
|  | 62 | FS_STAGING="$1" | 
|  | 63 | FS_SIZE_MIB="$2" | 
|  | 64 | TMPDIR="$3" | 
|  | 65 |  | 
|  | 66 | if [ "$UID" = "0" ] | 
|  | 67 | then | 
|  | 68 | SUDO= | 
|  | 69 | else | 
|  | 70 | SUDO=sudo | 
|  | 71 | fi | 
|  | 72 |  | 
|  | 73 | if [ "$FS_SIZE_MIB" = "0" ] | 
|  | 74 | then | 
|  | 75 | # Just create a dummy file.  This allows developers to bypass bits of | 
|  | 76 | # the build by setting the size to 0. | 
|  | 77 | touch $OPT_OUTPUT_FILES | 
|  | 78 | exit 0 | 
|  | 79 | fi | 
|  | 80 |  | 
|  | 81 | # create temporary files and dirs | 
|  | 82 | FS_TMPFILE=$(mktemp "$TMPDIR/mkxva-fsimg-XXXXX") | 
|  | 83 | XVA_TARBALL_STAGING=$(mktemp -d "$TMPDIR/mkxva-tarball-staging-XXXXX") | 
|  | 84 | OVF_STAGING=$(mktemp -d "$TMPDIR/mkxva-ovf-staging-XXXXX") | 
|  | 85 |  | 
|  | 86 | # Find udevsettle and udevtrigger on this installation | 
|  | 87 | if [ -x "/sbin/udevsettle" ] ; then | 
|  | 88 | UDEVSETTLE="/sbin/udevsettle --timeout=30" | 
|  | 89 | elif [ -x "/sbin/udevadm" ] ; then | 
|  | 90 | UDEVSETTLE='/sbin/udevadm settle' | 
|  | 91 | else | 
|  | 92 | UDEVSETTLE='/bin/true' | 
|  | 93 | fi | 
|  | 94 |  | 
|  | 95 | if [ -x "/sbin/udevtrigger" ] ; then | 
|  | 96 | UDEVTRIGGER=/sbin/udevtrigger | 
|  | 97 | elif [ -x "/sbin/udevadm" ] ; then | 
|  | 98 | UDEVTRIGGER='/sbin/udevadm trigger' | 
|  | 99 | else | 
|  | 100 | UDEVTRIGGER= | 
|  | 101 | fi | 
|  | 102 |  | 
|  | 103 | # CLEAN_ variables track devices and mounts that must be taken down | 
|  | 104 | # no matter how the script exits.  Loop devices are vulnerable to | 
|  | 105 | # exhaustion so we make every effort to remove them | 
|  | 106 |  | 
|  | 107 | CLEAN_KPARTX= | 
|  | 108 | CLEAN_LOSETUP= | 
|  | 109 | CLEAN_MOUNTPOINT= | 
|  | 110 |  | 
|  | 111 | cleanup_devices () { | 
|  | 112 | if [ -n "$CLEAN_MOUNTPOINT" ] ; then | 
|  | 113 | echo "Mountpoint $CLEAN_MOUNTPOINT removed on abnormal exit" | 
|  | 114 | $SUDO umount "$CLEAN_MOUNTPOINT" || echo "umount failed" | 
|  | 115 | rmdir "$CLEAN_MOUNTPOINT" || echo "rmdir failed" | 
|  | 116 | fi | 
|  | 117 | if [ -n "$CLEAN_KPARTX" ] ; then | 
|  | 118 | echo "kpartx devices for $CLEAN_KPARTX removed on abnormal exit" | 
|  | 119 | $SUDO kpartx -d "$CLEAN_KPARTX" || echo "kpartx -d failed" | 
|  | 120 | fi | 
|  | 121 | if [ -n "$CLEAN_LOSETUP" ] ; then | 
|  | 122 | echo "Loop device $CLEAN_LOSETUP removed on abnormal exit" | 
|  | 123 | $SUDO losetup -d "$CLEAN_LOSETUP" # Allow losetup errors to propagate | 
|  | 124 | fi | 
|  | 125 | } | 
|  | 126 |  | 
|  | 127 | trap "cleanup_devices" EXIT | 
|  | 128 |  | 
|  | 129 | make_fs_inner () { | 
|  | 130 | local staging="$1" | 
|  | 131 | local output="$2" | 
|  | 132 | local options="$3" | 
|  | 133 | CLEAN_MOUNTPOINT=$(mktemp -d "$TMPDIR/mkfs-XXXXXX") | 
|  | 134 |  | 
|  | 135 | # copy staging dir contents to fs image | 
|  | 136 | $SUDO mount $options "$output" "$CLEAN_MOUNTPOINT" | 
|  | 137 | $SUDO tar -C "$staging" -c . | tar -C "$CLEAN_MOUNTPOINT" -x | 
|  | 138 | $SUDO umount "$CLEAN_MOUNTPOINT" | 
|  | 139 | rmdir "$CLEAN_MOUNTPOINT" | 
|  | 140 | CLEAN_MOUNTPOINT= | 
|  | 141 | } | 
|  | 142 |  | 
|  | 143 | # Turn a staging dir into an ext3 filesystem within a partition | 
|  | 144 | make_fs_in_partition () { | 
|  | 145 | local staging="$1" | 
|  | 146 | local output="$2" | 
|  | 147 |  | 
|  | 148 | # create new empty disk | 
|  | 149 | dd if=/dev/zero of="$output" bs=1M count=$FS_SIZE_MIB | 
|  | 150 | # Set up a loop device on the empty disk image | 
|  | 151 | local loopdevice=$($SUDO losetup -f) | 
|  | 152 | $SUDO losetup "$loopdevice" "$output" | 
|  | 153 | CLEAN_LOSETUP="$loopdevice" | 
|  | 154 | # Create a partition table and single partition. | 
|  | 155 | # Start partition at sector 63 to allow space for grub | 
|  | 156 | cat <<EOF | 
|  | 157 | Errors from sfdisk below are expected because the new disk is uninitialised | 
|  | 158 | Expecting: sfdisk: ERROR: sector 0 does not have an msdos signature | 
|  | 159 | Expecting: /dev/loop0: unrecognized partition table type | 
|  | 160 | EOF | 
|  | 161 | $SUDO sfdisk -uS "$CLEAN_LOSETUP" <<EOF | 
|  | 162 | 63 - - * | 
|  | 163 | EOF | 
|  | 164 |  | 
|  | 165 | # kpartx creates a device for the new partition | 
|  | 166 | # in the form /dev/mapper/loop1p1 | 
|  | 167 | $SUDO kpartx -av "$CLEAN_LOSETUP" | 
|  | 168 | CLEAN_KPARTX="$CLEAN_LOSETUP" | 
|  | 169 | # Wait for the device to appear | 
|  | 170 | $UDEVTRIGGER | 
|  | 171 | $UDEVSETTLE  || echo "udev settle command return code non-zero" | 
|  | 172 | # Infer the name of the partition device | 
|  | 173 | local partition="${CLEAN_LOSETUP/dev/dev/mapper}p1" | 
|  | 174 | # Set permissive privileges on the device | 
|  | 175 | $SUDO chmod 0777 "$partition" | 
|  | 176 | # Make an ext3 filesystem on the partition | 
|  | 177 | /sbin/mkfs.ext3 -I 128 -m0 -F "$partition" | 
|  | 178 | /sbin/e2label "$partition" vpxroot | 
|  | 179 | make_fs_inner "$staging" "$partition" "" | 
| Hengqing Hu | 3b719e5 | 2012-03-09 16:03:00 +0800 | [diff] [blame] | 180 |  | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 181 | # Now run grub on the image we've created | 
|  | 182 | CLEAN_MOUNTPOINT=$(mktemp -d "$TMPDIR/mkfs-XXXXXX") | 
|  | 183 |  | 
|  | 184 | # copy Set up[ grub files prior to installing grub within the image | 
|  | 185 | $SUDO mount "$partition" "$CLEAN_MOUNTPOINT" | 
|  | 186 | $SUDO cp $CLEAN_MOUNTPOINT/usr/share/grub/i386-redhat/* "$CLEAN_MOUNTPOINT/boot/grub" | 
|  | 187 | kernel_version=$($SUDO chroot "$CLEAN_MOUNTPOINT" rpm -qv kernel | sed -e 's/kernel-//') | 
|  | 188 | kernel_version_xen=$($SUDO chroot "$CLEAN_MOUNTPOINT" rpm -qv kernel-xen | sed -e 's/kernel-xen-//') | 
|  | 189 | $SUDO cat > "$CLEAN_MOUNTPOINT/boot/grub/grub.conf" <<EOF | 
|  | 190 | default 0 | 
|  | 191 | timeout 2 | 
|  | 192 |  | 
|  | 193 | title vmlinuz-$kernel_version (HVM) | 
|  | 194 | root (hd0,0) | 
|  | 195 | kernel /boot/vmlinuz-$kernel_version ro root=LABEL=vpxroot | 
|  | 196 | initrd /boot/initrd-$kernel_version.img | 
|  | 197 |  | 
|  | 198 | title vmlinuz-${kernel_version_xen}xen (PV) | 
|  | 199 | root (hd0,0) | 
|  | 200 | kernel /boot/vmlinuz-${kernel_version_xen}xen ro root=LABEL=vpxroot console=xvc0 | 
|  | 201 | initrd /boot/initrd-${kernel_version_xen}xen.img | 
|  | 202 | EOF | 
|  | 203 |  | 
|  | 204 | $SUDO umount "$CLEAN_MOUNTPOINT" | 
|  | 205 | CLEAN_MOUNTPOINT= | 
| Hengqing Hu | 3b719e5 | 2012-03-09 16:03:00 +0800 | [diff] [blame] | 206 |  | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 207 | # Grub expects a disk with name /dev/xxxx with a first partition | 
|  | 208 | # named /dev/xxxx1, so we give it what it wants using symlinks | 
|  | 209 | # Note: /dev is linked to the real /dev of the build machine, so | 
|  | 210 | # must be cleaned up | 
|  | 211 | local disk_name="/dev/osxva$$bld" | 
|  | 212 | local disk_part1_name="${disk_name}1" | 
|  | 213 | rm -f "$disk_name" | 
|  | 214 | rm -f "$disk_part1_name" | 
|  | 215 | ln -s "$CLEAN_LOSETUP" "$disk_name" | 
|  | 216 | ln -s "$partition" "$disk_part1_name" | 
| Hengqing Hu | 3b719e5 | 2012-03-09 16:03:00 +0800 | [diff] [blame] | 217 |  | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 218 | # Feed commands into the grub shell to setup the disk | 
|  | 219 | grub --no-curses --device-map=/dev/null <<EOF | 
|  | 220 | device (hd0) $disk_name | 
|  | 221 | setup (hd0) (hd0,0) | 
|  | 222 | quit | 
|  | 223 | EOF | 
| Hengqing Hu | 3b719e5 | 2012-03-09 16:03:00 +0800 | [diff] [blame] | 224 |  | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 225 | # Cleanup | 
|  | 226 | rm -f "$disk_name" | 
|  | 227 | rm -f "$disk_part1_name" | 
|  | 228 | $SUDO kpartx -dv "$CLEAN_KPARTX" | 
|  | 229 | CLEAN_KPARTX= | 
|  | 230 | $SUDO losetup -d "$CLEAN_LOSETUP" | 
|  | 231 | CLEAN_LOSETUP= | 
|  | 232 | } | 
|  | 233 |  | 
|  | 234 | # turn a staging dir into an ext3 filesystem image | 
|  | 235 | make_fs () { | 
|  | 236 | local staging="$1" | 
|  | 237 | local output="$2" | 
|  | 238 |  | 
|  | 239 | # create new empty fs | 
|  | 240 | dd if=/dev/zero of="$output" bs=1M count=0 seek=$FS_SIZE_MIB | 
|  | 241 | /sbin/mkfs.ext3 -m0 -F "$output" | 
|  | 242 | /sbin/e2label "$output" vpxroot | 
|  | 243 | make_fs_inner "$staging" "$output" "-oloop" | 
|  | 244 | } | 
|  | 245 |  | 
|  | 246 |  | 
|  | 247 | # split a virtual disk image into the format expected inside an xva file | 
|  | 248 | splitvdi () { | 
|  | 249 | local diskimg="$1" | 
|  | 250 | local outputdir="$2" | 
|  | 251 | local rio="$3" | 
|  | 252 |  | 
|  | 253 | local n_bytes=$(stat --printf=%s "$diskimg") | 
|  | 254 | local n_meg=$((($n_bytes+$((1024*1024 -1)))/$((1024*1024)))) | 
|  | 255 | local i=0 | 
| Hengqing Hu | 3b719e5 | 2012-03-09 16:03:00 +0800 | [diff] [blame] | 256 | while [ $i -lt $n_meg ] ; do | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 257 | if [ $rio -eq 0 ] ; then | 
|  | 258 | local file="$outputdir"/chunk-$(printf "%08d" $i) | 
|  | 259 | dd if="$diskimg" of="$file" skip=$i bs=1M count=1 2>/dev/null | 
|  | 260 | gzip "$file" | 
|  | 261 | else | 
|  | 262 | local file="$outputdir"/$(printf "%08d" $i) | 
|  | 263 | dd if="$diskimg" of="$file" skip=$i bs=1M count=1 2>/dev/null | 
|  | 264 | local chksum=$(sha1sum -b "$file") | 
|  | 265 | echo -n "${chksum/ */}" > "$file.checksum" | 
|  | 266 | fi | 
|  | 267 | i=$(($i + 1)) | 
|  | 268 | done | 
|  | 269 | } | 
|  | 270 |  | 
|  | 271 | if [ -n "$OPT_USE_PARTITION" ] ; then | 
|  | 272 | make_fs_in_partition "$FS_STAGING" "$FS_TMPFILE" | 
|  | 273 | else | 
|  | 274 | make_fs "$FS_STAGING" "$FS_TMPFILE" | 
|  | 275 | fi | 
|  | 276 |  | 
|  | 277 | VDI_SIZE=$(stat --format=%s "$FS_TMPFILE") | 
|  | 278 |  | 
|  | 279 | make_xva () { | 
|  | 280 | local output_file="$1" | 
|  | 281 | local xml_file="$2" | 
|  | 282 | local subdir | 
|  | 283 | local rio | 
|  | 284 |  | 
|  | 285 | if [[ `cat $xml_file` =~ "<member>\s*<name>class</name>\s*<value>VDI</value>\s*</member>\s*<member>\s*<name>id</name>\s*<value>(Ref:[0-9]+)</value>" ]] | 
|  | 286 | then | 
|  | 287 | # it's a rio style xva | 
|  | 288 | subdir="${BASH_REMATCH[1]}"; | 
|  | 289 | rio=1 | 
|  | 290 | else | 
|  | 291 | # it's a geneva style xva | 
|  | 292 | subdir="xvda" | 
|  | 293 | rio=0 | 
|  | 294 | fi | 
|  | 295 |  | 
|  | 296 | cp "$xml_file" "$XVA_TARBALL_STAGING"/ova.xml | 
|  | 297 | sed -i -e "s/@VDI_SIZE@/$VDI_SIZE/" "$XVA_TARBALL_STAGING"/ova.xml | 
|  | 298 | mkdir "$XVA_TARBALL_STAGING/$subdir" | 
|  | 299 | splitvdi "$FS_TMPFILE" "$XVA_TARBALL_STAGING/$subdir" "$rio" | 
|  | 300 | TARFILE_MEMBERS=$(cd "$XVA_TARBALL_STAGING" && echo ova.xml $subdir/*) | 
|  | 301 | tar -C "$XVA_TARBALL_STAGING" --format=v7 -c $TARFILE_MEMBERS -f "$output_file.tmp" | 
|  | 302 | mv "$output_file.tmp" "$output_file" | 
|  | 303 | } | 
|  | 304 |  | 
|  | 305 | make_ovf () { | 
|  | 306 | local output_dir="$1" | 
|  | 307 | local xml_file="$2" | 
|  | 308 | local output_base=$(basename "$output_dir") | 
|  | 309 | local disk="$output_dir/${output_base}.vmdk" | 
|  | 310 | local manifest="$output_dir/${output_base}.mf" | 
|  | 311 | local ovf="$output_dir/${output_base}.ovf" | 
|  | 312 |  | 
|  | 313 | mkdir -p "$output_dir" | 
|  | 314 | rm -f "$disk" | 
|  | 315 | $VBOX_IMG convert --srcfilename="$FS_TMPFILE" --dstfilename="$disk" \ | 
|  | 316 | --srcformat RAW --dstformat VMDK --variant Stream | 
|  | 317 | chmod 0644 "$disk" | 
|  | 318 |  | 
|  | 319 | local n_bytes=$(stat --printf=%s "$disk") | 
|  | 320 | cp "$xml_file" "$ovf" | 
|  | 321 | sed -i -e "s/@MKXVA_DISK_FULLSIZE@/$VDI_SIZE/" "$ovf" | 
|  | 322 | sed -i -e "s/@MKXVA_DISK_SIZE@/$n_bytes/" "$ovf" | 
|  | 323 | sed -i -e "s/@MKXVA_DISK_MIB_SIZE@/$FS_SIZE_MIB/" "$ovf" | 
|  | 324 | sed -i -e "s/@MKXVA_DISK_FILENAME@/${output_base}.vmdk/" "$ovf" | 
|  | 325 |  | 
|  | 326 | for to_sign in "$ovf" "$disk" | 
|  | 327 | do | 
|  | 328 | local sha1_sum=$(sha1sum "$to_sign" | cut -d' ' -f1) | 
|  | 329 | echo "SHA1($(basename "$to_sign"))= $sha1_sum" >> $manifest | 
|  | 330 | done | 
|  | 331 | } | 
|  | 332 |  | 
|  | 333 | output_files="$OPT_OUTPUT_FILES" | 
|  | 334 | xml_files="$OPT_XML_FILES" | 
|  | 335 | # Iterate through the type list creating the relevant VMs | 
|  | 336 | for create_type in $OPT_TYPES | 
|  | 337 | do | 
|  | 338 | # Shift one parameter from the front of the lists | 
|  | 339 | create_output_file="${output_files%% *}" | 
|  | 340 | output_files="${output_files#* }" | 
|  | 341 | create_xml_file="${xml_files%% *}" | 
|  | 342 | xml_files="${xml_files#* }" | 
|  | 343 | echo "Creating $create_type appliance $create_output_file using metadata file $create_xml_file" | 
|  | 344 |  | 
|  | 345 | case "$create_type" in | 
|  | 346 | xva) | 
|  | 347 | make_xva "$create_output_file" "$create_xml_file" | 
|  | 348 | ;; | 
|  | 349 | ovf) | 
|  | 350 | make_ovf "$create_output_file" "$create_xml_file" | 
|  | 351 | ;; | 
|  | 352 | *) | 
|  | 353 | echo "Unknown VM type '$create_type'" | 
|  | 354 | exit 1 | 
|  | 355 | ;; | 
|  | 356 | esac | 
|  | 357 |  | 
|  | 358 | done | 
|  | 359 |  | 
|  | 360 |  | 
|  | 361 | # cleanup | 
| Hengqing Hu | 3b719e5 | 2012-03-09 16:03:00 +0800 | [diff] [blame] | 362 | if [ -z "${DO_NOT_CLEANUP:-}" ] ; then | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 363 | rm -rf "$XVA_TARBALL_STAGING" | 
|  | 364 | rm -f "$FS_TMPFILE" | 
|  | 365 | fi |