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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
|
#!/bin/bash
rootd="${1:-/}"
set -e
CLOUD_IMG_STR="# CLOUD_IMG: This file was created/modified by the Cloud Image build process"
LANG=C
# dist_ge(dist1,dist2)
# return true if dist1 is newer or the same as dist2
dist_ge() { [[ "$1" > "$2" || "$1" == "$2" ]]; }
dist_le() { [[ "$1" < "$2" || "$1" == "$2" ]]; }
_xchroot() {
# Prevent multiple chroots
if [ "$1" = "/" ]; then
shift;
"$@"
else
chroot "$@"
fi
}
codename=$(_xchroot "${rootd}" sh -c 'lsb_release --short --codename')
#### COMMON architecture independent functions
arch=$(_xchroot "${rootd}" dpkg --print-architecture)
add_serial_console() {
condev=$1
idir="$rootd/etc/init"
cat << EOF > "${idir}/${condev}.conf"
# CONDEV - getty
#
# This service maintains a getty on CONDEV from the point the system is
# started until it is shut down again.
start on stopped rc RUNLEVEL=[2345] and (
not-container or
container CONTAINER=lxc or
container CONTAINER=lxc-libvirt)
stop on runlevel [!2345]
pre-start script
# getty will not be started if the serial console is not present
stty -F /dev/CONDEV -a 2> /dev/null > /dev/null || { stop ; exit 0; }
end script
respawn
script
exec /sbin/getty -L CONDEV 115200 vt102
end script
${CLOUD_IMG_STR}
EOF
sed -i "s/CONDEV/${condev}/g" "$idir/${condev}.conf"
}
## --------------
# remove 127.0.1.1 entry (LP: #440757)
_xchroot "${rootd}" sh -c 'sed -i "/^127.0.1.1/d" /etc/hosts'
## --------------
# remove ssh pregenerated keys (LP: #512377)
if dist_ge "${codename}" lucid; then
_xchroot "${rootd}" sh -c 'rm -f /etc/ssh/ssh_host_[rd]sa_key*'
fi
## --------------
if dist_ge "${codename}" trusty; then
_xchroot "${rootd}" locale-gen en_US.utf8
fi
## --------------
# set cloud-init to be on
if dist_ge "${codename}" natty; then
case "$codename" in
natty|oneiric) values="NoCloud, OVF, Ec2";;
precise) values="NoCloud, ConfigDrive, OVF, MAAS, Ec2";;
*) # this matches quantal and later
values="NoCloud, ConfigDrive, AltCloud, OVF, MAAS, Ec2, None";;
esac
printf "%s\t%s\t%s\t%s\n" \
cloud-init cloud-init/datasources multiselect "$values" |
_xchroot "${rootd}" debconf-set-selections
_xchroot "${rootd}" dpkg-reconfigure --frontend=noninteractive cloud-init
fi
## --------------
# write some build information to the guest
# the idea is that given runtime info and this info, the instance
# can at least determine if there is a newer build available
# these variables are passed in in environment from cloudimg-build-launcher
if dist_ge "${codename}" lucid &&
[ -n "${build_name}" -o -n "${serial}" ]; then
d="${rootd}/etc/cloud"
[ -d "$d" ] || mkdir -p "${d}"
{
[ -n "${build_name}" ] && echo "build_name: ${build_name}"
[ -n "${serial}" ] && echo "serial: ${serial}"
} > "$d/build.info"
fi
## --------------
# for maverick and newer, use LABEL= for the '/' entry in fstab
if dist_ge "${codename}" lucid && [ -n "${root_fs_label}" ]; then
bl="[:blank:]"
lstr="LABEL=${root_fs_label}"
sed -i "s,^[^#${bl}]*\([${bl}]*/[${bl}].*\),${lstr}\1," "${rootd}/etc/fstab"
fi
## Make sure that the update-motd.d directory exists
[ ! -e "${rootd}/etc/update-motd.d" ] &&
mkdir -p "${rootd}/etc/update-motd.d"
## write a MOTD file advertising support for images
cat > "${rootd}/etc/update-motd.d/51-cloudguest" << EOF
#!/bin/sh
#
${CLOUD_IMG_STR}
# This file is not managed by a package. If you no longer want to
# see this message you can safely remove the file.
echo ""
echo " Get cloud support with Ubuntu Advantage Cloud Guest:"
echo " http://www.ubuntu.com/business/services/cloud"
EOF
chmod +x "${rootd}/etc/update-motd.d/51-cloudguest"
# for quantal and newer, add /etc/overlayroot.local.conf
# but do not overwrite anything that somehow got there
if dist_ge "${codename}" "quantal" &&
[ -f "${rootd}/etc/overlayroot.conf" ] &&
[ ! -f "${rootd}/etc/overlayroot.local.conf" ]; then
{
echo "${CLOUD_IMG_STR}"
echo "overlayroot_cfgdisk=LABEL=OROOTCFG"
} > "${rootd}/etc/overlayroot.local.conf"
fi
#### END COMMON ARCH FUNCTIONS
if [ "$arch" = "ppc64el" ]; then
add_serial_console hvc0
exit 0
fi
# ARM images are special
if [[ "$arch" =~ (arm|arm64|aarch64) ]]; then
echo "Configuring ARM Serial Port"
add_serial_console ttyAMA0
if dist_le "${code_name}" oneiric; then
echo "Removing resolv.conf as unneeded"
fi
echo "Image architecture is ARM. Existing vmbuilder-fixups"
exit 0
fi
## Add ttyS0 for i386/amd64 for Trusty and newer
if dist_ge "$codename" trusty && [ "$arch" = "i386" -o "$arch" = "amd64" ]; then
add_serial_console ttyS0
fi
psuedo_grub_probe() {
cat <<"PSUEDO_GRUB_PROBE"
#!/bin/sh
Usage() {
cat <<EOF
Usage: euca-psuedo-grub-probe
this is a wrapper around grub-probe to provide the answers for an ec2 guest
EOF
}
bad_Usage() { Usage 1>&2; fail "$@"; }
short_opts=""
long_opts="device-map:,target:,device"
getopt_out=$(getopt --name "${0##*/}" \
--options "${short_opts}" --long "${long_opts}" -- "$@") &&
eval set -- "${getopt_out}" ||
bad_Usage
device_map=""
target=""
device=0
arg=""
while [ $# -ne 0 ]; do
cur=${1}; next=${2};
case "$cur" in
--device-map) device_map=${next}; shift;;
--device) device=1;;
--target) target=${next}; shift;;
--) shift; break;;
esac
shift;
done
arg=${1}
case "${target}:${device}:${arg}" in
device:*:/*) echo "/dev/sda1"; exit 0;;
fs:*:*) echo "ext2"; exit 0;;
partmap:*:*)
# older versions of grub (lucid) want 'part_msdos' written
# rather than 'msdos'
legacy_pre=""
grubver=$(dpkg-query --show --showformat '${Version}\n' grub-pc 2>/dev/null) &&
dpkg --compare-versions "${grubver}" lt 1.98+20100804-5ubuntu3 &&
legacy_pre="part_"
echo "${legacy_pre}msdos";
exit 0;;
abstraction:*:*) echo ""; exit 0;;
drive:*:/dev/sda) echo "(hd0)";;
drive:*:/dev/sda*) echo "(hd0,1)";;
fs_uuid:*:*) exit 1;;
esac
PSUEDO_GRUB_PROBE
}
## --------------
# add 'acpiphp' to modules so it is loaded on boot (LP: #450463)
# but only do it on precise (LP: #1430323)
if dist_le "${codename}" precise; then
_xchroot "${rootd}" sh -c 'f=/etc/modules;
grep "^acpiphp$" "${f}" ||
printf "# %s\n%s\n" "LP: #450463" "acpiphp" >> "${f}"'
fi
## --------------
# below installs neatx from the freenx-team ppa , but only
# if the ubuntu-desktop is installed and suite is lucid.
# ideally we could use '--ppa' and '--add-pkgs' of vmbuilder, but
# because initially there was no 'lucid' repo there, we used
# karmic like this. Then, quick change to lucid.
#
# to avoid key server timeouts we have the key in a HERE document
if dist_ge "${codename}" lucid && ! dist_ge "${codename}" natty; then
_xchroot "${rootd}" sh -c '
desktop=$(dpkg-query --show ubuntu-desktop) ||
{ echo "not installing nx (not -desktop)"; exit 0; }
ppaurl="http://ppa.launchpad.net/freenx-team/ppa/ubuntu";
key=D018A4CE;
export DEBIAN_FRONTEND=noninteractive
printf "deb ${ppaurl} lucid main" > \
/etc/apt/sources.list.d/freenx-team-ppa.list &&
apt-key add - &&
apt-get update && apt-get install --assume-yes neatx-server ||
{ echo "failed"; exit 1; }
' <<EOF
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1.4.9 (GNU/Linux)
mI0ESXcA1AEEAK/FkL6U3huCjVb3NtewLY+6dNgmH+zvFDQi7Ltz1VmYEq8nIZfj
N4AeC4Y7PELf/+4aSCiTI17+DAnrnfQkFLoXXTpqWL9YvsFpc3loX7EQJejyUPjk
7KrmRJOZSID+JuBmf14IzCL8ShScta5ifu3rrjY5MpSdDARCuiIBK/TnABEBAAG0
HUxhdW5jaHBhZCBQUEEgZm9yIEZyZWVOWCBUZWFtiLYEEwECACAFAkl3ANQCGwMG
CwkIBwMCBBUCCAMEFgIDAQIeAQIXgAAKCRAqjjA00BikzvLPA/9iNkk2dSIqrHsw
9AAna96O7f/oZ5CCL+3tu/OQf8oh42hcT8du7kqR69GZDDZnAs92yXDh+/5Qs87f
pYBO7Buj41iVj9NQgY/o7RbJCoWhZGVvv48pPNKGtEnQnwI7ulELPKbD9WspZrRL
O3VSl6Po7xQVVRw5N8xt4n8j/Z84Tw==
=/zdI
-----END PGP PUBLIC KEY BLOCK-----
EOF
fi
## install / setup grub2
if dist_ge "${codename}" lucid; then
gprobe="${rootd}/usr/sbin/grub-probe"
moved=0
if [ -f "${gprobe}" ]; then
mv "${gprobe}" "${gprobe}.dist"
moved=1
fi
psuedo_grub_probe > "${gprobe}"
chmod 755 "${gprobe}"
if dist_ge "${codename}" quantal; then
# for Quantal and later, use /etc/default/grub.d functionality
# rather than modifying the grub configuration itself.
# This avoids the mess of having to do dpkg stuff
# LP: 1179940
mkdir -p "${rootd}/etc/default/grub.d"
cat << EOF > "${rootd}/etc/default/grub.d/50-cloudimg-settings.cfg"
# Cloud Image specific Grub settings for Generic Cloud Images
${CLOUD_IMG_STR}
# Set the recordfail timeout
GRUB_RECORDFAIL_TIMEOUT=0
# Do not wait on grub prompt
GRUB_TIMEOUT=0
# Set the default commandline
GRUB_CMDLINE_LINUX_DEFAULT="console=tty1 console=ttyS0"
# Set the grub console type
GRUB_TERMINAL=console
EOF
_xchroot "${rootd}" update-grub2
else
if dist_ge "${codename}" oneiric; then
# on precise or later, do not hang at grub prompt after incomplete boot.
# This requires a grub feature also. (LP: #1035279, LP: #669481)
printf "\n%s\n%s\n" "#Disable recordfail timeout" \
"GRUB_RECORDFAIL_TIMEOUT=0" >> "${rootd}/etc/default/grub"
fi
# Set the grub time out
new_timeout=5
sed -i -e "s,^GRUB_TIMEOUT=.*,GRUB_TIMEOUT=${new_timeout}," \
"${rootd}/etc/default/grub"
# modify default linux command line for serial console output
# change GRUB_CMDLINE_LINUX_DEFAULT="quite splash"
# to GRUB_CMDLINE_LINUX_DEFAULT="console=ttyS0"
opt="GRUB_CMDLINE_LINUX_DEFAULT"
defstr="console=ttyS0"
sed -i "s/\(${opt}=\".*\"\)$/${opt}=\"${defstr}\"/" \
"${rootd}/etc/default/grub"
# make the debconf values match those stored in /etc/default/grub
# above. This means that the user isn't prompted for a merge on
# a dpkg-reconfigure grub-pc
printf "%s\t%s\t%s\t%s\n%s\t%s\t%s\t%s\n" \
grub-pc grub2/linux_cmdline_default string "${defstr}" \
grub-pc grub2/linux_cmdline string " " |
_xchroot "${rootd}" debconf-set-selections
sed -i -e 's,^\(GRUB_HIDDEN_TIMEOUT=.*\),#\1,' \
-e 's/^\(GRUB_HIDDEN_TIMEOUT_QUIET=.*\)/#\1/' \
-e 's,^#GRUB_TERMINAL=console,GRUB_TERMINAL=console,' \
"${rootd}/etc/default/grub"
fi
if dist_ge "${codename}" "trusty"; then
# since this is a disk image, we technically don't need to install all the
# grub modules, as the image itself is not bootable. This makes for a small
# disk image
_xchroot "${rootd}" update-grub
else
_xchroot "${rootd}" /bin/sh -c \
'grub-install --grub-setup=/bin/true /dev/sda && /usr/sbin/update-grub'
fi
# reconfigure grub so that upgrades to grub-pc do not force a debconf config
# changed prompt (LP: #1009294). This re-runs update-grub
if dist_ge "$codename" "precise"; then
_xchroot "${rootd}" env DEBIAN_FRONTEND=noninteractive \
dpkg-reconfigure grub-pc
fi
grub2cfg="${rootd}/boot/grub/grub.cfg"
[ ! -f "${grub2cfg}" ] ||
sed -i -e "s,root=/dev/sda1,root=LABEL=${root_fs_label}," "${grub2cfg}"
if [ "${codename}" = "lucid" ]; then
# for lucid builds, remove /boot/grub/video.lst that was created
# during 'update-grub' above. Its very hacky, but this file with 'vbe'
# in it takes update-grub down a sour path, causing failure on ec2.
# this workaround could be removed if LP: #623609 is fixed in lucid
rm -f "${rootd}/boot/grub/video.lst"
fi
[ ${moved} -eq 0 ] || mv "${gprobe}.dist" "${gprobe}"
fi
if [ "${codename}" = "lucid" ]; then
# for lucid builds, write hooks to kernel-img.conf. This is not
# necessary for maverick and later grub2 packages as it puts hooks
# into etc/kernel/postinst.d/ (#554175), and handles removing
# these entries on release upgrade also
kimg_conf="${rootd}/etc/kernel-img.conf"
if [ -f "${kimg_conf}" ]; then
for hook in postinst_hook postrm_hook; do
grep -q "${hook}" "${kimg_conf}" ||
echo "${hook} = update-grub" >> "${kimg_conf}"
done
fi
fi
## modify /boot/grub/menu.lst if it exists
## this is generated at install time by grub-legacy-ec2, but will have
## devices as found from the _xchroot. Here we write what it will be on ec2
if [ -f "${rootd}/boot/grub/menu.lst" ]; then
grub_root="(hd0)"
linux_root=/dev/sda1
[ -n "${root_fs_label}" ] && linux_root="LABEL=${root_fs_label}"
# the sed code below basically fixes/sets the following lines in a
# /boot/grub/menu.lst file:
# # kopt=root=xxxxxxx ro
# kernel /boot/vmlinuz-... root=xxxxxx ....
# # groot=xxxxx
# root xxxxx
# uuuid xxxxx
sed -i "${rootd}/boot/grub/menu.lst" \
-e "s|^\(# kopt=root=\)[^ ]*|\1${linux_root}|" \
-e "s|^\(kernel.*root=\)[^ ]*|\1${linux_root}|" \
-e "s|^\(# groot=\)[^ ]*|\1${grub_root}|" \
-e "s|^\(root\|uuid\)\([[:space:]]*\).*|root\2${grub_root}|"
# LP: #682831
# in lucid with -ec2 kernel, we need to pass xencons=hvc0 in addition
# to console=hvc0 to work around an issue in plymouth
if [ "${codename}" = "lucid" ]; then
sed -i '/xencons=hvc0/!s,console=hvc0,xencons=hvc0 console=hvc0,' \
"${rootd}/boot/grub/menu.lst"
fi
# grub-legacy-ec2 writes this ucf entry. since we've modified
# /boot/grub/menu.lst, we have to remove it, or the user will
# get prompted for a 3 way merge of the changes the first time this runs
echo "ucf prior to grub removal contains:"
_xchroot "${rootd}" /bin/cat /var/lib/ucf/registry
_xchroot "${rootd}" /usr/bin/ucfr --purge grub /var/run/grub/menu.lst
_xchroot "${rootd}" /bin/sed -i -r \
'/grub[[:space:]]+\/var\/run\/grub\/menu.lst/d' \
/var/lib/ucf/registry
echo "ucf registry now contains:"
_xchroot "${rootd}" /bin/cat /var/lib/ucf/registry
fi
if [ "${codename}" = "hardy" -o "${codename}" = "karmic" ] &&
[ ! -f "${rootd}/boot/grub/menu.lst" ]; then
[ -d "${rootd}/boot/grub" ] || mkdir -p "${rootd}/boot/grub"
cat > "${rootd}/boot/grub/menu.lst" <<EOF
# This file is not managed. It is only present in order to provide
# a way to boot a default AMI with a pv-grub image.
default 0
timeout 0
hiddenmenu
title vmlinuz
root (hd0)
kernel /vmlinuz root=/dev/sda1 ro
initrd /initrd.img
EOF
fi
if [ "${codename}" = "hardy" ]; then
# LP: #859814 : there is no cloud-init package in hardy, so we've
# chosen to apply that here directly
cat > "${rootd}/etc/apt/apt.conf.d/90cloud-init-pipelining" <<EOF
//Written by cloud-init per 'apt_pipelining'
Acquire::http::Pipeline-Depth "0";
EOF
fi
## remove /etc/resolv.conf on oneiric and earlier
if dist_le "${code_name}" oneiric; then
echo "Removing resolv.conf as unneeded"
_xchroot "${rootd}" rm /etc/resolv.conf
fi
# vi: ts=3 expandtab
|