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

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
#!/bin/sh
# vi: ts=4 noexpandtab
#
#    Copyright (C) 2010 Canonical Ltd.
#
#    Authors: Scott Moser <smoser@canonical.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, version 3 of the License.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

TEMP_D=""

error() { echo "$@" 1>&2; }
fail() { [ $# -eq 0 ] || error "$@"; exit 1; }
Usage() {
	cat <<EOF
Usage: ${0##*/} output.img
   Create a multiboot floppy image that can boot a cloudimg disk image

   The output.iso file can then be booted with
      kvm -fda output.img -boot a -drive cloudimg-image.img...
EOF
}
bad_Usage() { Usage 1>&2; fail "$@"; }
cleanup() {
	[ -z "${TEMP_D}" -o ! -d "${TEMP_D}" ] || rm -Rf "${TEMP_D}"
}

getsize() {
	local f="$1" size=""
	size=$(ls -l "${f}" 2>/dev/null | awk '{print $5}') &&
		[ -n "${size}" ] && _RET=${size} && return 0
}
checksize() {
	getsize "$1" || return 1
	[ "${_RET}" "$2" "$3" ]
}

mk_grub_rescue() {
	local output=${1}
	local bdir="${TEMP_D}/bdir"
	local err="${TEMP_D}/err"
	local modules="configfile"
    modules="biosdisk tar memdisk configfile part_msdos ext2 multiboot test serial echo terminal chain sleep minicmd search linux"
	mkdir "${bdir}" &&
		mkdir -p "${bdir}/boot/grub" &&
		cat > "${bdir}/boot/grub/grub.cfg" <<EOF
serial --unit=0 --speed=9600
terminal_input console serial
terminal_output console serial

set default=0
set timeout=5

set menu_color_normal=white/black
set menu_color_highlight=black/light-gray

menuentry 'cloud-image with random ubuntu password' --class gnu-linux --class gnu --class os {
	set gfxpayload=keep
	search --no-floppy --label cloudimg-rootf --set
	linux	/vmlinuz "ro init=/usr/lib/cloud-init/uncloud-init root=LABEL=cloudimg-rootfs ds=nocloud-net ubuntu-pass=random"
	if [ -f /initrd.img ]; then
		initrd	/initrd.img
	fi
}

menuentry 'cloud-image with ubuntu:ubuntu' --class gnu-linux --class gnu --class os {
	set gfxpayload=keep
	search --no-floppy --label cloudimg-rootf --set
	linux	/vmlinuz "ro init=/usr/lib/cloud-init/uncloud-init root=LABEL=cloudimg-rootfs ds=nocloud-net ubuntu-pass=ubuntu"
	if [ -f /initrd.img ]; then
		initrd	/initrd.img
	fi
}

menuentry 'cloud-image seeded from a url [must be edited]' --class gnu-linux --class gnu --class os {
	set gfxpayload=keep
	search --no-floppy --label cloudimg-rootf --set
	linux	/vmlinuz "ro root=LABEL=cloudimg-rootfs ds=nocloud-net;s=http://example.com/demo-"
	if [ -f /initrd.img ]; then
		initrd	/initrd.img
	fi
}

EOF
		[ $? -eq 0 ] || return
		grub-mkrescue --output="${output}" \
			${modules:+"--modules=${modules}"} "${bdir}" > "${err}" 2>&1 ||
			{ cat "${err}" 1>&2; return 1; }
}

short_opts="h"
long_opts="help"
getopt_out=$(getopt --name "${0##*/}" \
	--options "${short_opts}" --long "${long_opts}" -- "$@") &&
	eval set -- "${getopt_out}" ||
	bad_Usage

mode="grub-rescue-floppy"
while [ $# -ne 0 ]; do
	cur=${1}; next=${2};
	case "$cur" in
		-h|--help) Usage; exit 0;;
		--) shift; break;;
	esac
	shift;
done

[ $# -eq 1 ] || bad_Usage "must supply only output image"

TEMP_D=$(mktemp -d "${TMPDIR:-/tmp}/.${0##*/}.XXXXXX") ||
	fail "failed to make tempd"
trap cleanup EXIT

output=${1}

case "${mode}" in
	grub-rescue-floppy)
		mk_grub_rescue "${output}" ||
			fail "failed to make rescue floppy"
		checksize "${output}" -lt $((2880*1024)) ||
			fail "output of mk_rescue was to large for floppy"
		size=${_RET}
		truncate --size 2880K "${output}" ||
			fail "failed to pad ${output} to 2880K"
		echo "created ${mode} image in ${output} (payload $size)"
		;;
	grub-rescue-cdrom)
		mk_grub_rescue "${output}" ||
			fail "failed to make rescue cdrom"
		echo "created ${mode} image in ${output}"
		;;
esac

[ $? -eq 0 ] || fail "failed to create image"