~ubuntu-on-ec2/vmbuilder/automated-ec2-builds

60 by smoser
refactor how build-ec2-image is run
1
#!/bin/bash
2
# vi: ts=4 noexpandtab
667 by Ben Howard
Set umask to 022
3
umask 022
278 by Ben Howard
Code-review for new build-lb-ec2-image changes
4
cmd_args=$*
15 by Scott Moser
several changes
5
base_d=$(dirname $(readlink -f "${0}"))
60 by smoser
refactor how build-ec2-image is run
6
7
SKIP_FINISH=0
96 by Scott Moser
get build_name, serial into /etc/cloud/build.info in image
8
serial=""
60 by smoser
refactor how build-ec2-image is run
9
TEMP_D=""
1 by Soren Hansen
Import image building script and crontab
10
14 by Scott Moser
add function to copy out and publish kernels/initramdisks
11
error() { echo "$@" 1>&2; }
60 by smoser
refactor how build-ec2-image is run
12
debug() { error "$(date -R):" "$@"; }
13
cleanup() {
280 by Scott Moser
fix whitespace. indentation is via tab.
14
		[ "${preserve_image}" = "1" -o -z "${TEMP_D}" -o ! -d "${TEMP_D}" ] || rm -Rf "${TEMP_D}"
60 by smoser
refactor how build-ec2-image is run
15
}
81 by Scott Moser
add config file control of partfile and rootfs label support
16
cat_partfile() {
17
	# Note: this function may be overwritten in a config file
18
	local arch=${1} size=${2}
19
	if [ "${arch}" = "i386" ]; then
20
		printf "%s\n%s\n%s\n" "root ${size} a1" \
21
			"/mnt 1 a2" "swap 1024 a3"
22
	else
23
		printf "%s\n%s\n" "root ${size} a1" "/mnt 1 b"
24
	fi
25
}
60 by smoser
refactor how build-ec2-image is run
26
14 by Scott Moser
add function to copy out and publish kernels/initramdisks
27
fail() { [ $# -eq 0 ] || error "$@"; exit 1; }
60 by smoser
refactor how build-ec2-image is run
28
Usage() {
29
	cat <<EOF
30
Usage: ${0##*/} config output_d
31
   build according to config, putting output in output_d
32
EOF
33
}
82 by Scott Moser
add bad_Usage func (was undefined, but used)
34
bad_Usage() { Usage 1>&2; fail "$@"; }
60 by smoser
refactor how build-ec2-image is run
35
175 by Scott Moser
add a floppy image to the tar archive
36
# optional_file(filename,directory_src,target)
37
#  if filename is non-empty, copy it from directory_src to target
38
#  return in _RET the target
39
#  if filename is empty, return true, but _RET as empty.
40
optional_file() {
41
	local fname="$1" src_d=${2} targ=${3}
42
	[ -n "${fname}" ] || { _RET=""; return 0; }
43
	cp "${src_d}/${fname}" "${targ}" && _RET=${targ} && return 0
44
	return 1
45
}
46
60 by smoser
refactor how build-ec2-image is run
47
short_opts="h"
676 by Ben Howard
Don't use the PPA for live-builds of Vivid or later
48
long_opts="help,skip-finish,serial:,proposed,hwe"
60 by smoser
refactor how build-ec2-image is run
49
getopt_out=$(getopt --name "${0##*/}" \
50
	--options "${short_opts}" --long "${long_opts}" -- "$@") &&
51
	eval set -- "${getopt_out}" ||
52
	bad_Usage
53
278 by Ben Howard
Code-review for new build-lb-ec2-image changes
54
skip_gen_image=""
321 by Scott Moser
turn 'preserve-image' off by default
55
preserve_image=0
644 by Ben Howard
Whitespace changes. Added the ability to build images from -proposed.
56
proposed=0
676 by Ben Howard
Don't use the PPA for live-builds of Vivid or later
57
hwe=0
278 by Ben Howard
Code-review for new build-lb-ec2-image changes
58
60 by smoser
refactor how build-ec2-image is run
59
while [ $# -ne 0 ]; do
60
	cur=${1}; next=${2};
61
	case "$cur" in
62
		-h|--help) Usage; exit 0;;
96 by Scott Moser
get build_name, serial into /etc/cloud/build.info in image
63
		   --serial) serial=$2; shift;;
60 by smoser
refactor how build-ec2-image is run
64
		   --skip-finish) SKIP_FINISH=1;;
280 by Scott Moser
fix whitespace. indentation is via tab.
65
		   --image) skip_gen_image=$1;;
66
		   --preserve) preserve_image=1;;
644 by Ben Howard
Whitespace changes. Added the ability to build images from -proposed.
67
		   --proposed) proposed=1;;
676 by Ben Howard
Don't use the PPA for live-builds of Vivid or later
68
		   --hwe) hwe=1;;
60 by smoser
refactor how build-ec2-image is run
69
		--) shift; break;;
70
	esac
71
	shift;
1 by Soren Hansen
Import image building script and crontab
72
done
73
60 by smoser
refactor how build-ec2-image is run
74
[ $# -eq 2 ] || bad_Usage "must supply config and output dir"
75
76
conf=${1}
77
pub_d=${2}
78
79
[ -f "${conf}" ] || fail "${conf}: not a file"
80
278 by Ben Howard
Code-review for new build-lb-ec2-image changes
81
owner=${USER}
60 by smoser
refactor how build-ec2-image is run
82
suite=""
83
build_name=""
84
description=""
55 by Scott Moser
flip logic on copy_out_kernels so lucid kernels are copied out
85
copy_out_kernels=1
93 by Scott Moser
add support to build-ec2-image for "unpublished_ramdisks"
86
unpublished_ramdisks=""
60 by smoser
refactor how build-ec2-image is run
87
publish_ec2=1
88
fail_on_missing_kernels=1
89
root_fs_size="2048"
81 by Scott Moser
add config file control of partfile and rootfs label support
90
root_fs_label=""
175 by Scott Moser
add a floppy image to the tar archive
91
loader=""
92
floppy=""
227.1.1 by Scott Moser
initial commit of ovf support
93
ovf_create=0
94
ovf_cmd=( )
662 by Ben Howard
Build OVA images suitable for import into VMware, Virtualbox, Citrix, etc
95
ova_create=0
661.1.1 by Ben Howard
Build OVA images suitable for import into VMware, Virtualbox, Citrix, etc
96
ova_cmd=( )
227.1.1 by Scott Moser
initial commit of ovf support
97
package_img_tgz=1
452 by Scott Moser
enable building of -root.tar.gz tarballs
98
create_root_tgz=0
553.1.15 by Scott Moser
use img2roottgz
99
purge_roottgz_pkgs=( )
60 by smoser
refactor how build-ec2-image is run
100
101
. "${conf}"
102
334.2.2 by Ben Howard
Major revsion to add ARM (armel) support to cloud images
103
[ -n "${arches}" ] || arches="${img_arches}"
104
60 by smoser
refactor how build-ec2-image is run
105
for x in suite build_name description; do
106
	[ -n "${!x}" ] || fail "${x} is not defined in ${conf}"
107
done
108
67 by Scott Moser
PATH does not go through 'sudo', invoke vmbuider with full path
109
VMBUILDER=$(which vmbuilder 2>/dev/null) || fail "cannot find vmbuilder"
279 by Ben Howard
Updated cronrun to include live-build in the pathing declarations. Changed build-ec2-image to search for build-lb-ec2-image. Also updated build-lb-ec2-build to use the local locations for files
110
LBBUILD=$(which build-lb-ec2-image 2> /dev/null ) || fail "cannont find build-lb-ec2-image"
656 by Daniel Watkins
Pull buildd building in as a separate codepath.
111
BUILDD_BUILD=$(which build-buildd-image 2> /dev/null) || fail "cannot find build-buildd-image"
60 by smoser
refactor how build-ec2-image is run
112
113
{ [ -d "${pub_d}" ] || mkdir -p "${pub_d}"; } ||
114
	fail "failed to make ${pub_d}"
115
116
TEMP_D=$(mktemp -d ${pub_d}/.${0##*/}.tmp.XXXXXX) ||
117
	fail "failed to make tempd"
118
119
trap cleanup EXIT
120
258 by Scott Moser
vmlinuz files in the image my not be readable by non-root (LP: #752352)
121
chownperms=$(printf "%s:%s" "$(id -u)" "$(id -g)")
122
32 by Scott Moser
write version info to unpacked/tool-version-info.txt
123
unpacked_d="${pub_d}/unpacked"
227.1.10 by Scott Moser
build-ec2-image: fix syntax error
124
mkdir -p "${unpacked_d}" || fail "failed to make ${unpacked_d}"
248 by Scott Moser
publish ovf to top level directory
125
ovf_d="${pub_d}"
661.1.1 by Ben Howard
Build OVA images suitable for import into VMware, Virtualbox, Citrix, etc
126
if [ ${ovf_create:-0} -ne 0 -o ${ova_create:-0} -ne 1 ]; then
127
	mkdir -p "${ovf_d}" || fail "failed to make ovf/ova dir ${ovf_d}"
227.1.10 by Scott Moser
build-ec2-image: fix syntax error
128
fi
32 by Scott Moser
write version info to unpacked/tool-version-info.txt
129
334.2.2 by Ben Howard
Major revsion to add ARM (armel) support to cloud images
130
for arch in ${arches}
1 by Soren Hansen
Import image building script and crontab
131
do
334.2.2 by Ben Howard
Major revsion to add ARM (armel) support to cloud images
132
320 by Scott Moser
rename 'uec' to a combination of 'cloudimg' or 'cloud'
133
	name_prefix="${suite}-${build_name}-cloudimg"
60 by smoser
refactor how build-ec2-image is run
134
	img_base="${name_prefix}-${arch}"
75 by Scott Moser
just a cleanlyness thing, keep the arches output directories separate
135
	tmpdest="${pub_d}/tmp-${img_base}"
60 by smoser
refactor how build-ec2-image is run
136
137
	# set variables read by copy-out-kernels
138
	klist=""
139
140
	partfile=${TEMP_D}/partfile.${arch}
231 by Scott Moser
build-ec2-image: white space fix
141
	cat_partfile "${arch}" "${root_fs_size}" > "${partfile}" ||
81 by Scott Moser
add config file control of partfile and rootfs label support
142
		fail "failed to write partfile: cat_partfile $arch $root_fs_size"
143
96 by Scott Moser
get build_name, serial into /etc/cloud/build.info in image
144
	# expose certain variables to the execscript
145
	# this could also be done through sudo -E, but this just
146
	# seems more reliable
147
	execscript="${TEMP_D}/execscript"
148
	{
280 by Scott Moser
fix whitespace. indentation is via tab.
149
		spin=
150
		printf "#!/bin/bash\n"
504 by Ben Howard
Add suite back into the build.info
151
		for v in suite serial build_name root_fs_label; do
280 by Scott Moser
fix whitespace. indentation is via tab.
152
			printf "%s='%s'\n" "$v" "${!v}"
153
		done
154
		printf "\n"
320 by Scott Moser
rename 'uec' to a combination of 'cloudimg' or 'cloud'
155
		cat "${base_d}/vmbuilder-cloudimg-fixes"
96 by Scott Moser
get build_name, serial into /etc/cloud/build.info in image
156
	} > "${execscript}"
157
	chmod 755 "${execscript}"
158
236 by Scott Moser
build-ec2-image: move some variable assignments
159
	kinfo="${img_base}-kernel-info.txt"
160
	image="${img_base}.img"
278 by Ben Howard
Code-review for new build-lb-ec2-image changes
161
162
280 by Scott Moser
fix whitespace. indentation is via tab.
163
	# Use old scripts for Natty and older, and new for Oneiric and later
379 by Ben Howard
Commit to shrink delta between vmbuilder and live-build trees
164
	if [ "${suite}" \< "oneiric" -o "${BUILDER}" = "vmbuilder" ]
280 by Scott Moser
fix whitespace. indentation is via tab.
165
	then
166
		cmdline=(
167
		   ${VMBUILDER} xen ubuntu
168
		   "--suite=${suite}"
169
		   "--arch=${arch}"
170
		   "--dest=${tmpdest}"
171
		   --ec2
172
		   "--ec2-version=${description}"
173
		   --components main,restricted,universe,multiverse
174
		   --part "${partfile}"
175
		   --ec2-landscape --lock-user
176
		   "--manifest=${tmpdest}/${img_base}.manifest"
177
		   "--tmp=${TMPDIR:-/tmp}"
178
		   --execscript "${execscript}"
179
		   --debug "${vmb_addargs[@]}" )
657 by Daniel Watkins
Fix boundaries.
180
	elif [ "${suite}" \< "v" -o "${BUILDER}" = "live-build" ]
379 by Ben Howard
Commit to shrink delta between vmbuilder and live-build trees
181
	then
280 by Scott Moser
fix whitespace. indentation is via tab.
182
		cmdline=(
183
			${LBBUILD}
184
			"--suite=${suite}"
185
			"--arch=${arch}"
186
			"--dest=${tmpdest}"
187
			"--image=root.img"
188
			"--part=${partfile}"
189
			"--rootlabel=${root_fs_label}"
190
			"--rootsize=${root_fs_size}"
191
			"--owner=${owner}"
192
			"--manifest=${tmpdest}/${img_base}.manifest"
288 by Ben Howard
Style and grammer changes to increase readability and error handling robustness
193
			"--livepath=${LIVE_BUILD_PATH}"
322 by Ben Howard
Added img-diff tool to compare two images; Added build-info.txt information; Added ability to remove cloud_local-hooks and replace with a single script via the --hooks flag
194
			"--serial=${serial}"
334.2.2 by Ben Howard
Major revsion to add ARM (armel) support to cloud images
195
			"--hookscript=${execscript}"
385 by Ben Howard
Added support for /etc/ec2_version file
196
			"--ec2-version=${description}"
401 by Ben Howard
Enabled desktop builds
197
			"--type=${build_type:-server}"
434 by Ben Howard
Updated version of live-build for generic generation
198
			"--config=${conf}"
280 by Scott Moser
fix whitespace. indentation is via tab.
199
		 )
657 by Daniel Watkins
Fix boundaries.
200
	elif [ "${suite}" \> "u" -o "${BUILDER}" = "buildd" ]
656 by Daniel Watkins
Pull buildd building in as a separate codepath.
201
	then
202
		cmdline=(
203
			${BUILDD_BUILD}
204
			"--suite=${suite}"
205
			"--arch=${arch}"
206
			"--dest=${tmpdest}"
207
			"--image=root.img"
208
			"--owner=${owner}"
209
			"--manifest=${tmpdest}/${img_base}.manifest"
210
		 )
280 by Scott Moser
fix whitespace. indentation is via tab.
211
	fi
60 by smoser
refactor how build-ec2-image is run
212
661 by Daniel Watkins
Fix proposed building for buildd builds.
213
	# Allow building from propopsed
214
	[ "${proposed:-0}" -eq 1 ] && cmdline+=('--proposed')
215
67 by Scott Moser
PATH does not go through 'sudo', invoke vmbuider with full path
216
	debug "invoking ${cmdline[*]}"
278 by Ben Howard
Code-review for new build-lb-ec2-image changes
217
280 by Scott Moser
fix whitespace. indentation is via tab.
218
	if [ -z ${skip_gen_image} ]; then
219
		sudo env "PATH=$PATH" "${cmdline[@]}" ||
220
			fail "image generation command failed: ${cmdline[*]}";
221
	else
222
		sudo cp ${skip_gen_image} ${tmpdest} ||
223
			fail "unable to copy ${skip_gen_image} to ${tmpdest}";
224
	fi
60 by smoser
refactor how build-ec2-image is run
225
656 by Daniel Watkins
Pull buildd building in as a separate codepath.
226
	if [ "${suite}" \> "v" -o "${BUILDER}" = "buildd" ]; then
227
		sudo env "PATH=$PATH" mount-image-callback "${tmpdest}/root.img" -- \
228
			write-versions _MOUNTPOINT_ "${build_type:-server}" "${serial}" \
229
			"${description}"
230
	fi
231
60 by smoser
refactor how build-ec2-image is run
232
	if [ ${copy_out_kernels} -ne 0 ]; then
233
		debug "${img_base}: copying out kernels"
553.1.18 by Scott Moser
use mount-image-callback for copy-out-kernels
234
		sudo env "PATH=$PATH" mount-image-callback \
235
			--read-only "${tmpdest}/root.img" -- copy-out-kernels \
93 by Scott Moser
add support to build-ec2-image for "unpublished_ramdisks"
236
			${unpublished_ramdisks:+"--skip-ramdisks=${unpublished_ramdisks}"} \
258 by Scott Moser
vmlinuz files in the image my not be readable by non-root (LP: #752352)
237
			--chown ${chownperms} \
553.1.18 by Scott Moser
use mount-image-callback for copy-out-kernels
238
			_MOUNTPOINT_ "${tmpdest}" \
60 by smoser
refactor how build-ec2-image is run
239
			"${tmpdest}/${kinfo}" "${img_base}-" ||
553.1.18 by Scott Moser
use mount-image-callback for copy-out-kernels
240
			fail "failed copy out kernels"
613 by Ben Howard
Made ovf/part2disk-gpt architecture dependent.
241
60 by smoser
refactor how build-ec2-image is run
242
		[ "$fail_on_missing_kernels" = "0" -o -s "${tmpdest}/${kinfo}" ] ||
243
			fail "no kernels found"
244
	fi
245
452 by Scott Moser
enable building of -root.tar.gz tarballs
246
	if [ ${create_root_tgz} -ne 0 ]; then
247
		debug "${img_base}: creating root tarball"
553.1.16 by Scott Moser
pass the purgeopts through
248
		rpurgeopts=()
249
		for purgeopt in "${purge_roottgz_pkgs[@]}"; do
250
			rpurgeopts[${#rpurgeopts[@]}]="--purge=${purgeopt}"
251
		done
252
		if [ "${#rpurgeopts[@]}" -eq 0 ]; then
553.1.15 by Scott Moser
use img2roottgz
253
			# img2roottgz will mount 'ro' if no packages given
254
			ln -sf "$tmpdest/root.img" "$tmpdest/root.img.copy" ||
255
				fail "oddly failed to create symlink to $root.img.copy"
256
		else
257
			cp --sparse=always "${tmpdest}/root.img" \
258
				"${tmpdest}/root.img.copy" ||
259
			fail "failed copy of root img before img2roottgz"
260
		fi
553.1.16 by Scott Moser
pass the purgeopts through
261
		sudo env "PATH=$PATH" img2roottgz "${rpurgeopts[@]}" \
262
			"${tmpdest}/root.img.copy" "${pub_d}/${img_base}-root.tar.gz" ||
553.1.15 by Scott Moser
use img2roottgz
263
			fail "failed to run img2roottgz"
264
		rm -f "$tmpdest/root.img.copy"
691 by Robert Jennings
Add LXD metadata and xz root tarball to build output
265
266
		gzip -dc "${pub_d}/${img_base}-root.tar.gz" | 
267
			xz > "${pub_d}/${img_base}-root.tar.xz" ||
268
			fail "failed to create xz-compressed root.tar"
269
693 by Robert Jennings
lxd metadata env variables
270
		sudo env PATH=$PATH serial=$serial arch=$arch suite=$suite \
271
			lxd_metadata "${pub_d}/${img_base}-lxd.tar.xz" ||
691 by Robert Jennings
Add LXD metadata and xz root tarball to build output
272
			fail "failed to create LXD metadata for root.tar"
452 by Scott Moser
enable building of -root.tar.gz tarballs
273
	fi
274
81 by Scott Moser
add config file control of partfile and rootfs label support
275
	rootimg="${tmpdest}/root.img"
586 by Ben Howard
Zero disks after writing. Make UEFI images smaller.
276
277
	# Zero out the free space, to make the image smaller
278
	zerofree ${rootimg}
279
	sync
280
81 by Scott Moser
add config file control of partfile and rootfs label support
281
	if [ -n "${root_fs_label}" ]; then
282
		e2label "${rootimg}" "${root_fs_label}" ||
283
			fail "failed to add label ${root_fs_label} to ${rootimg}"
284
	fi
285
60 by smoser
refactor how build-ec2-image is run
286
	debug "${img_base}: moving files produced by vmbuilder"
81 by Scott Moser
add config file control of partfile and rootfs label support
287
	mv "${rootimg}" "${tmpdest}/${img_base}.img" ||
60 by smoser
refactor how build-ec2-image is run
288
		fail "failed to move root.img to ${img_base}"
289
290
	[ "${SKIP_FINISH:-0}" = "0" ] || continue
291
292
	# move all files to 'unpacked' dir
293
	mv ${tmpdest}/${img_base}*  "${unpacked_d}/" ||
294
		fail "failed to move files to ${unpacked_d}"
295
175 by Scott Moser
add a floppy image to the tar archive
296
	addl_tarmembers=""
297
298
	opt_ldrs=""
299
	# 'loader' could have a '%a' in it which would be replaced by arch
300
	optional_file "${loader//%a/${arch}}" "${base_d}/loaders" \
301
		"${unpacked_d}/${img_base}-loader" &&
302
		{ [ -z "${_RET}" ] || opt_ldrs="${opt_ldrs} ${_RET##*/}"; } ||
303
		fail "failed to copy optional loader"
304
305
	optional_file "${floppy//%a/${arch}}" "${base_d}/loaders" \
306
		"${unpacked_d}/${img_base}-floppy" &&
307
		{ [ -z "${_RET}" ] || opt_ldrs="${opt_ldrs} ${_RET##*/}"; } ||
308
		fail "failed to copy optional floppy"
144 by Scott Moser
add code to consume and package loaders and set maverick builds to use it.
309
60 by smoser
refactor how build-ec2-image is run
310
	# get a list of non-ec2 kernels in unpacked_d
175 by Scott Moser
add a floppy image to the tar archive
311
	if [ -s "${unpacked_d}/${kinfo}" -o -n "${opt_ldrs}" ]; then
312
		addl_tarmembers=$( cd "${unpacked_d}" ; out="";
313
			for f in ${img_base}*initrd* ${img_base}*vmlinu* ${opt_ldrs}; do
60 by smoser
refactor how build-ec2-image is run
314
				[ -f "${f}" -a "${f##*-ec2}" = "${f}" ] || continue
315
				out="${out} ${f}"
316
			done
317
			[ -z "${out}" ] || echo "${out}"
318
		)
319
	fi
320
321
	debug "${img_base}: creating tarfiles"
322
323
	( cd "${unpacked_d}" && tar \
324
		-S --use-compress-program=gzip-rsyncable \
175 by Scott Moser
add a floppy image to the tar archive
325
		-cf "${pub_d}/${img_base}.tar.gz" "${image}" ${addl_tarmembers} \
173 by Scott Moser
add README.files to image archive.
326
		-C "${base_d}" README.files ) ||
60 by smoser
refactor how build-ec2-image is run
327
		fail "failed to create ${pub_d}/${img_base}.tar.gz"
328
227.1.1 by Scott Moser
initial commit of ovf support
329
	if [ "${package_img_tgz:-0}" != "0" ]; then
330
		debug "${img_base}: creating unpacked tarfile"
331
		( cd "${unpacked_d}" && tar \
332
			-S --use-compress-program=gzip-rsyncable \
333
			-cf "${img_base}.img.tar.gz" "${image}" ) ||
334
			fail "failed to create ${img_base}.img.tar.gz in ${unpacked_d}"
335
	fi
336
354.1.1 by Ben Howard
Added multiple target disk formats to support KVM, openStack and ARMEL images
337
	# split up the image creatiom. Before this was done with part2ovf. However with the introduction
354.2.1 by Ben Howard
Plumbing on OVF and image generation
338
    # of armel and qemu images, the creation of disk images seperate from an OVF container is needed
354.1.1 by Ben Howard
Added multiple target disk formats to support KVM, openStack and ARMEL images
339
340
	# create raw paritioned disk
556 by Ben Howard
Introduction of BIOS/GPT and UEFI bootable AMD64 images
341
	disk_img="${pub_d}/${img_base}-filesystem.img"
604 by Ben Howard
Create gpt1.img and disk1.img for ppc64el arches
342
	if [ "${qcow2_create:-0}" -ne 0 -o "${vmdk_create:-0}" -ne 0 -o \
343
		 "${ovf_create:-0}" -ne 0 -o "${raw_img:-0}" -ne 0 -o \
661.1.1 by Ben Howard
Build OVA images suitable for import into VMware, Virtualbox, Citrix, etc
344
		 "${qemu_img:-0}" -ne 0 -o "${ova_create:-0}" ]; then
604 by Ben Howard
Create gpt1.img and disk1.img for ppc64el arches
345
	    debug "creating raw partitioned disk image"
354.1.1 by Ben Howard
Added multiple target disk formats to support KVM, openStack and ARMEL images
346
		part2disk_opts="-v"
347
        [ "${nogrub:-0}" -ne 1 ] && part2disk_opts="${part2disk_opts} -G"
604 by Ben Howard
Create gpt1.img and disk1.img for ppc64el arches
348
		part2disk="${base_d}/ovf/part2disk"
349
		# Certain arches need their own handling
350
		[[ "${arch}" =~ ppc64el ]] && part2disk="${part2disk}.${arch}"
351
		part2cmd="${part2disk} --size=${img_size} ${part2disk_opts} ${unpacked_d}/${image} ${disk_img}"
354.2.1 by Ben Howard
Plumbing on OVF and image generation
352
	    debug "  cmd: ${part2cmd}"
354.1.1 by Ben Howard
Added multiple target disk formats to support KVM, openStack and ARMEL images
353
		$part2cmd || fail "failed to run part2cmd: ${part2cmd}"
354
	fi
355
390.1.3 by Ben Howard
QEMU/Metal disk image generation for ARMEL images
356
	# create qemu images
357
	if [ "${qemu_img:-0}" -ne 0 ]; then
358
	    cmd=( "${qemu_img_cmd[@]}" )
359
		# %d- > base_dir, %o -> pub dir, %i -> final image, %r -> root fs
494 by Ben Howard
Allow arm??2qemu to have --label defined.
360
		cmd=( "${cmd[@]//%d/${base_d}}" )
556 by Ben Howard
Introduction of BIOS/GPT and UEFI bootable AMD64 images
361
		cmd=( "${cmd[@]//%r/${disk_img}}" )
362
		cmd=( "${cmd[@]//%i/${img_base}}" )
494 by Ben Howard
Allow arm??2qemu to have --label defined.
363
		cmd=( "${cmd[@]//%o/${pub_d}}" )
364
		cmd=( "${cmd[@]//%l/${root_fs_label}}" )
390.1.3 by Ben Howard
QEMU/Metal disk image generation for ARMEL images
365
366
        debug "creating qemu/metal disk image with ${cmd[@]}"
367
        ${cmd[@]} ||
368
             fail "failed to run qemu_img_cmd: ${cmd[@]}"
369
	fi
370
354.2.1 by Ben Howard
Plumbing on OVF and image generation
371
	ovf_disks=""
358 by Ben Howard
Fixed issue where -disk1 was not being appended to disk names.
372
	disk_out="${pub_d}/${img_base}-disk1"
556 by Ben Howard
Introduction of BIOS/GPT and UEFI bootable AMD64 images
373
	# create img files
354.2.1 by Ben Howard
Plumbing on OVF and image generation
374
	if [ "${qcow2_create:-0}" -ne 0 ]; then
359 by Ben Howard
Inital cut at image renaming
375
		qcow_cmd="${base_d}/ovf/part2img --type qcow2 --source ${disk_img} --target ${disk_out} --extension img"
376
		debug "creating qcow2 from partitioned ${disk_img} to ${disk_out}.img"
354.2.1 by Ben Howard
Plumbing on OVF and image generation
377
		$qcow_cmd || fail "failed to run qcow_cmd: ${qcow_cmd}"
358.1.1 by Ben Howard
QCOW2 images are now named .img
378
		[ "${qcow2_ovf:-0}" -eq 1 ] && { ovf_disks="${ovf_disks} img"; debug "qcow OVF queued for creation"; }
359 by Ben Howard
Inital cut at image renaming
379
		[ "${ovf_default:-vmdk}" = "qcow2" ] && ovf_default="img"
354.2.1 by Ben Howard
Plumbing on OVF and image generation
380
	fi
381
382
	# create vmdk image
383
	if [ "${vmdk_create:-0}" -ne 0 ]; then
384
		vmdk_cmd="${base_d}/ovf/part2img --type vmdk --source ${disk_img} --target ${disk_out}"
359 by Ben Howard
Inital cut at image renaming
385
		debug "creating vmdk from partitioned ${disk_img} to ${disk_out}.vmdk"
354.2.1 by Ben Howard
Plumbing on OVF and image generation
386
		$vmdk_cmd || fail "failed to run vmdk_cmd: ${vmdk_cmd}"
387
		[ "${vmdk_ovf:-0}" -eq 1 ] && { ovf_disks="${ovf_disks} vmdk"; debug "vmdk OVF queue for creation"; }
388
	fi
389
613 by Ben Howard
Made ovf/part2disk-gpt architecture dependent.
390
	# UEFI boot paths only work for {arm,amd}64
391
	if [[ "${arch}" =~ (amd64|arm64) ]]; then
560 by Ben Howard
Fixed name of uefi1 image from uefi-1
392
		uefi_disk_out="${pub_d}/${img_base}-uefi1"
556 by Ben Howard
Introduction of BIOS/GPT and UEFI bootable AMD64 images
393
394
		# create raw UEFI/BIOS/GPT partitioned disk
395
		uefi_img="${pub_d}/${img_base}-uefi.img"
557 by Ben Howard
Merge of changes for UEFI/BIOS/GPT and a few fixes
396
		if [ "${qcow2_uefi_create:-0}" -ne 0 -o ${vmdk_uefi_create:-0} -ne 0 ]; then
613 by Ben Howard
Made ovf/part2disk-gpt architecture dependent.
397
			debug "creating raw UEFI disk image"
398
			part2cmd_base="${base_d}/ovf/part2disk-gpt.${arch}"
399
			part2cmd="${part2cmd_base} --size=${img_size} ${part2disk_opts} ${unpacked_d}/${image} ${uefi_img}"
556 by Ben Howard
Introduction of BIOS/GPT and UEFI bootable AMD64 images
400
			debug "   cmd: ${part2cmd}"
401
			$part2cmd || fail "failed to run part2cmd: ${part2cmd}"
402
		fi
403
613 by Ben Howard
Made ovf/part2disk-gpt architecture dependent.
404
		# create UEFI qcow's
556 by Ben Howard
Introduction of BIOS/GPT and UEFI bootable AMD64 images
405
		if [ "${qcow2_uefi_create:-0}" -ne 0 ]; then
406
			qcow_cmd="${base_d}/ovf/part2img --type qcow2 --source ${uefi_img} --target ${uefi_disk_out} --extension img"
613 by Ben Howard
Made ovf/part2disk-gpt architecture dependent.
407
			debug "creating qcow2 from UEFI partitioned ${uefi_img} to ${uefi_disk_out}.img"
556 by Ben Howard
Introduction of BIOS/GPT and UEFI bootable AMD64 images
408
			$qcow_cmd || fail "failed to run qcow_cmd: ${qcow_cmd}"
409
			[ "${qcow2_ovf:-0}" -eq 1 ] && { ovf_disks="${ovf_disks} img"; debug "qcow OVF queued for creation"; }
410
			[ "${ovf_default:-vmdk}" = "qcow2" ] && ovf_default="img"
411
		fi
412
613 by Ben Howard
Made ovf/part2disk-gpt architecture dependent.
413
		# create UEFI vmdk's
557 by Ben Howard
Merge of changes for UEFI/BIOS/GPT and a few fixes
414
		if [ "${vmdk_uefi_create:-0}" -ne 0 ]; then
556 by Ben Howard
Introduction of BIOS/GPT and UEFI bootable AMD64 images
415
			qcow_cmd="${base_d}/ovf/part2img --type vmdk --source ${uefi_img} --target ${uefi_disk_out}"
613 by Ben Howard
Made ovf/part2disk-gpt architecture dependent.
416
			debug "creating vmdk from UEFI partitioned ${uefi_img} to ${uefi_disk_out}.vmdk"
556 by Ben Howard
Introduction of BIOS/GPT and UEFI bootable AMD64 images
417
			$qcow_cmd || fail "failed to run qcow_cmd: ${qcow_cmd}"
418
			[ "${qcow2_ovf:-0}" -eq 1 ] && { ovf_disks="${ovf_disks} img"; debug "qcow OVF queued for creation"; }
419
			[ "${ovf_default:-vmdk}" = "qcow2" ] && ovf_default="img"
420
		fi
421
422
		[ "${raw_disk:-0}" -ne 0 ] || {	[ -e "${uefi_img}" ] && rm ${uefi_img}; }
423
	fi
424
586.1.3 by Robert Jennings
Fix comments regarding ppc64el and GPT
425
	# GPT/PReP images for ppc64el
586.1.1 by Robert Jennings
Build bootable GPT partitioned images for ppc64el
426
	if [ "${arch}" == "ppc64el" ]; then
606 by Ben Howard
Renamed ppc64el gpt disks to disk1 extensions
427
		gpt_disk_out="${pub_d}/${img_base}-disk1"
586.1.1 by Robert Jennings
Build bootable GPT partitioned images for ppc64el
428
429
		# create raw GPT partitioned disk
602.1.1 by Robert Jennings
ppc64el: GPT image name 'disk1' -> 'gpt1'
430
		gpt_img="${pub_d}/${img_base}-gpt.img"
586.1.1 by Robert Jennings
Build bootable GPT partitioned images for ppc64el
431
		if [ "${qcow2_gpt_create:-0}" -ne 0 -o ${vmdk_gpt_create:-0} -ne 0 ]; then
432
			debug "creating raw GPT disk image"
433
			part2cmd="${base_d}/ovf/part2disk-gpt.ppc64el --size=${img_size} ${part2disk_opts} ${unpacked_d}/${image} ${gpt_img}"
434
			debug "   cmd: ${part2cmd}"
435
			$part2cmd || fail "failed to run part2cmd: ${part2cmd}"
436
		fi
437
438
		# create GPT qcow's
439
		if [ "${qcow2_gpt_create:-0}" -ne 0 ]; then
440
			qcow_cmd="${base_d}/ovf/part2img --type qcow2 --source ${gpt_img} --target ${gpt_disk_out} --extension img"
586.1.3 by Robert Jennings
Fix comments regarding ppc64el and GPT
441
			debug "creating qcow2 from GPT/PReP partitioned ${gpt_img} to ${gpt_disk_out}.img"
586.1.1 by Robert Jennings
Build bootable GPT partitioned images for ppc64el
442
			$qcow_cmd || fail "failed to run qcow_cmd: ${qcow_cmd}"
443
			[ "${qcow2_ovf:-0}" -eq 1 ] && { ovf_disks="${ovf_disks} img"; debug "qcow OVF queued for creation"; }
444
			[ "${ovf_default:-vmdk}" = "qcow2" ] && ovf_default="img"
445
		fi
446
447
		# create GPT vmdk's
448
		# XXX UNTESTED
449
		if [ "${vmdk_gpt_create:-0}" -ne 0 ]; then
450
			qcow_cmd="${base_d}/ovf/part2img --type vmdk --source ${gpt_img} --target ${gpt_disk_out}"
451
			debug "creating vmdk from GPT partitioned ${gpt_img} to ${gpt_disk_out}.vmdk"
452
			$qcow_cmd || fail "failed to run qcow_cmd: ${qcow_cmd}"
453
			[ "${qcow2_ovf:-0}" -eq 1 ] && { ovf_disks="${ovf_disks} img"; debug "qcow OVF queued for creation"; }
454
			[ "${ovf_default:-vmdk}" = "qcow2" ] && ovf_default="img"
455
		fi
456
457
		[ "${raw_disk:-0}" -ne 0 ] || {	[ -e "${gpt_img}" ] && rm ${gpt_img}; }
458
	fi
459
661.1.1 by Ben Howard
Build OVA images suitable for import into VMware, Virtualbox, Citrix, etc
460
	# create the OVA container
461
	if [ "${ova_create:-0}" -eq 1 ]; then
462
		prefix="${img_base}"
463
		debug "Creating OVA for ${disk_name}"
464
		debug "    OVA Filename: ${ovf_d}/${prefix}.ova"
465
		cmd=( "${ova_cmd[@]}" )
466
		cmd=( "${cmd[@]//%d/${base_d}}" )
467
		cmd=( "${cmd[@]//%o/${ovf_d}}" )
468
		cmd=( "${cmd[@]//%p/${prefix}}" )
469
		cmd=( "${cmd[@]//%r/${disk_img}}" )
470
		cmd=( "${cmd[@]//%s/${img_size}}" )
471
472
		debug "creating ova with ${cmd[@]}"
473
		"${cmd[@]}" || 	fail "failed to run ova_cmd: ${cmd[@]}"
474
		sudo chown -R "$(id -u):$(id -g)" "${ovf_d}"
475
		chmod go+r "${ovf_d}/"*
667 by Ben Howard
Set umask to 022
476
		touch "${ovf_d}/"*
661.1.1 by Ben Howard
Build OVA images suitable for import into VMware, Virtualbox, Citrix, etc
477
	fi
478
354.2.1 by Ben Howard
Plumbing on OVF and image generation
479
	# create OVF containers
480
	for disk_type in ${ovf_disks}; do
667 by Ben Howard
Set umask to 022
481
		[ "${ovf_default:-vmdk}" = "${disk_type}" ] &&
661.1.1 by Ben Howard
Build OVA images suitable for import into VMware, Virtualbox, Citrix, etc
482
			prefix="${img_base}" ||
483
			prefix="${img_base}-${disk_type}"
362 by Ben Howard
Pathing for OVF disk files fixed
484
		disk_name="${img_base}-disk1.${disk_type}"
359 by Ben Howard
Inital cut at image renaming
485
354.2.1 by Ben Howard
Plumbing on OVF and image generation
486
		debug "Creating OVF for ${disk_type} disk: ${disk_name}"
487
		debug "    OVF Filename: ${ovf_d}/${prefix}.ovf"
488
227.1.1 by Scott Moser
initial commit of ovf support
489
		cmd=( "${ovf_cmd[@]}" )
233 by Scott Moser
build-ec2-image: fixes to ovf creation logic
490
		cmd=( "${cmd[@]//%o/${ovf_d}}" )
491
		cmd=( "${cmd[@]//%i/${unpacked_d}/${image}}" )
492
		cmd=( "${cmd[@]//%d/${base_d}}" )
354.2.1 by Ben Howard
Plumbing on OVF and image generation
493
		cmd=( "${cmd[@]//%p/${prefix}}" )
494
		cmd=( "${cmd[@]//%r/${disk_name}}" )
354.1.1 by Ben Howard
Added multiple target disk formats to support KVM, openStack and ARMEL images
495
		cmd=( "${cmd[@]//%s/${img_size}}" )
354.2.1 by Ben Howard
Plumbing on OVF and image generation
496
		cmd=( "${cmd[@]//%t/${disk_type}}" )
661.1.1 by Ben Howard
Build OVA images suitable for import into VMware, Virtualbox, Citrix, etc
497
233 by Scott Moser
build-ec2-image: fixes to ovf creation logic
498
		debug "creating ovf with ${cmd[@]}"
661.1.1 by Ben Howard
Build OVA images suitable for import into VMware, Virtualbox, Citrix, etc
499
		"${cmd[@]}" || 	fail "failed to run ovf_cmd: ${cmd[@]}"
227.1.1 by Scott Moser
initial commit of ovf support
500
		sudo chown -R "$(id -u):$(id -g)" "${ovf_d}"
249 by Scott Moser
grant read access to ovf files
501
		chmod go+r "${ovf_d}/"*
667 by Ben Howard
Set umask to 022
502
		touch "${ovf_d}/"*
354.2.1 by Ben Howard
Plumbing on OVF and image generation
503
	done
60 by smoser
refactor how build-ec2-image is run
504
401 by Ben Howard
Enabled desktop builds
505
	[ "${raw_disk:-0}" -ne 0 ] || {	[ -e "${disk_img}" ] && rm ${disk_img}; }
354.1.1 by Ben Howard
Added multiple target disk formats to support KVM, openStack and ARMEL images
506
60 by smoser
refactor how build-ec2-image is run
507
	{ [ "${copy_out_kernels}" -eq 0 ] ||
508
	  cp "${unpacked_d}/${kinfo}" "${pub_d}"; } &&
509
		cp "${unpacked_d}/${img_base}.manifest" "${pub_d}" ||
401 by Ben Howard
Enabled desktop builds
510
			fail "failed to copy kinfo and manifest"
60 by smoser
refactor how build-ec2-image is run
511
401 by Ben Howard
Enabled desktop builds
512
	[ -e "${unpacked_d}/${img_base}.img" ] && {
513
			rm "${unpacked_d}/${img_base}.img" ||
514
				fail "failed deletion of ${img_base}.img";
515
		}
278 by Ben Howard
Code-review for new build-lb-ec2-image changes
516
280 by Scott Moser
fix whitespace. indentation is via tab.
517
	if [ "${preserve_image}" = 0 ]; then
518
		rm -rf "${tmpdest}" || fail "failed to delete ${tmpdest}"
519
	fi
278 by Ben Howard
Code-review for new build-lb-ec2-image changes
520
60 by smoser
refactor how build-ec2-image is run
521
done
522
523
exit 0