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