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