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
|
#!/bin/bash
#
# maas-cloudimg2ephemeral - update a cloud image to make it sufficient
# for use as a maas ephemeral image
#
# Copyright (C) 2011-2012 Canonical
#
# Authors:
# Scott Moser <scott.moser@canonical.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
VERBOSITY=0
TOP_D="${0%/*}"
error() { echo "$@" 1>&2; }
errorp() { printf "$@" 1>&2; }
fail() { [ $# -eq 0 ] || error "$@"; exit 1; }
failp() { [ $# -eq 0 ] || errorp "$@"; exit 1; }
Usage() {
cat <<EOF
Usage: ${0##*/} [ options ] disk kernel initrd [manifest]
Update the image 'disk', kernel 'kernel' and initrd 'initrd'
If manifest is given, a (dpkg --show) manifest will be written.
Expects to receive in a cloudimg disk image with no partition
table, and will update it for maas ephemeral use.
options:
-v | --verbose increase verbosity
EOF
}
bad_Usage() { Usage 1>&2; [ $# -eq 0 ] || error "$@"; exit 1; }
cleanup() {
[ -z "${TEMP_D}" -o ! -d "${TEMP_D}" ] || {
unmount_under "${TEMP_D}" &&
rm -Rf "${TEMP_D}"
}
}
debug() {
local level=${1}; shift;
[ "${level}" -gt "${VERBOSITY}" ] && return
error "$(date -R):" "${@}"
}
unmount_under() {
# unmount_under(dir)
# unmount all mounts under 'dir'
[ -f /proc/mounts ] ||
{ error "/proc/mounts not a file"; return 1; }
tac /proc/mounts | sh -c '
under=$1
while read s mp t opt a b ; do
[ "${mp#${under}}" != "${mp}" ] || continue;
umount $mp ||
{ echo "failed umount $mp, waiting, trying again" 1>&2;
sleep 10;
umount $mp || exit 1; }
done' -- "$1"
}
loop_mount() {
# Create more loop nodes, if necessary
local mounts=$(grep -c /dev/loop /proc/mounts) || mounts=0
local loops=$(ls /dev/loop* | wc -l) || loops=0
if [ $mounts -ge $loops ]; then
mknod -m 660 /dev/loop$loops b 7 $loops &&
chown root:disk /dev/loop$loops ||
return 1
fi
# Do the loop mount
mount -o loop "$1" "$2"
}
mount_callback_umount() {
# mount_callback_umount(img_or_device, func, args)
# mount the image given, call function with args,
# umount the image, return function's exit value
local device="$1" cb="$2" mp="" opts="" ret=0 m=""
shift 2;
mp=$(mktemp -d "$TEMP_D/mp.XXXXXX")
if [ -b "$device" ]; then
mount $opts "$device" "$mp" || return 1
else
loop_mount "$device" "$mp" || return 1
fi
for m in "/proc" "/sys"; do
[ -d "$mp/$m" ] || continue
mount --bind "$m" "$mp/$m" || {
error "failed to mount $mp/$m";
unmount_under "$mp";
return 1;
}
done
"$cb" "$mp" "$@"
ret=$?
unmount_under "$mp" && rmdir "$mp" ||
{ error "WARN! failed to umount $device from $mp"; return 2; }
return $ret
}
disable_services() {
local dir="$1"
cat > "$dir/usr/sbin/policy-rc.d" <<"EOF"
#!/bin/sh
while true; do
case "$1" in
-*) shift ;;
makedev) exit 0 ;;
x11-common) exit 0 ;;
*) exit 101 ;;
esac
done
EOF
[ $? -eq 0 ] && chmod 755 "$dir/usr/sbin/policy-rc.d" ||
{ error "failed to write policy-rc.d"; return 1; }
}
undisable_services() {
local dir="$1"
rm -f "$dir/usr/sbin/policy-rc.d"
}
apply_updates() {
# apply_updates(dir, kernel_out, initramfs_out)
# update directory given, and pull out kernel and initramfs
# to given locations
local dir=$1 kernel_out=$2 initrd_out=$3 manifest=$4
local tab=" "
if [ -f "$dir/etc/resolv.conf" -o -L "$dir/etc/resolv.conf" ]; then
mv "$dir/etc/resolv.conf" "$dir/etc/resolv.conf.dist" || return 1
fi
cp "/etc/resolv.conf" "$dir/etc/resolv.conf" ||
return 1
disable_services "$dir" || return
# we expect /etc/lsb-release to be shell syntax with DISTRIB_CODENAME
local rel="" fout arch host_arch
rel=$(. "$dir/etc/lsb-release" && echo "${DISTRIB_CODENAME}") &&
[ -n "$rel" ] ||
fail "failed to read DISTRIB_CODENAME from $dir/etc/lsb-release"
# not pretty, use 'file' to determine arch
fout=$(file -L "$dir/bin/sh") ||
{ error "'file' on $dir/bin/sh failed"; return 1; }
case "$fout" in
*\ x86-64,*) arch="x86_64";;
*\ 80386,*) arch="i386";;
*\ ARM,*) arch="arm";;
*\ 64-bit\ PowerPC*) arch="ppc64el";;
*) fail "unknown arch for fileoutput: $fout"; return 1;;
esac
case "$(uname -m)" in
i?86) host_arch="i386";;
x86_64) host_arch="x86_64";;
arm) host_arch="arm";;
ppc64le) host_arch="ppc64el";;
esac
debug 1 "arch: $arch. rel: $rel host_arch: $host_arch"
if [ "$arch" = "$host_arch" ] ||
[ "${host_arch}:${arch}" = "x86_64:i386" ]; then
debug 1 "no qemu-static needed to execute $arch on ${host_arch}"
else
local fmt="${QEMU_STATIC_FMT:-/usr/bin/qemu-%a-static}" exe=""
exe=${fmt//%a/$arch}
cp "$exe" "$dir/usr/bin/qemu-$arch-static" || {
error "failed to copy qemu-static [$exe] for $arch";
error "install qemu-user-static or set QEMU_STATIC_FMT";
return 1;
}
debug 1 "copied $exe into filesystem /usr/bin/qemu-$arch-static"
fi
local prox="" apt_opts=""
out=$(apt-config shell prox Acquire::HTTP::Proxy) &&
eval $out && [ -n "$prox" ] &&
apt_opts="--option=Acquire::HTTP::Proxy=${prox}"
apt_opts="${apt_opts} --option=Dpkg::Options::=--force-confold"
[ -n "${apt_opts}" ] &&
debug 1 "using apt options ${apt_opts} for install"
# FIXME: remove this after no longer necessary
local ppa_file="" ppa_url=""
if [ "$rel" = "precise" ]; then
# chroot and apt-add-repository failed when trying quantal amd64 with
# qemu-static. Instead, do it manually.
ppa_file="/etc/apt/sources.list.d/maas-ephemeral-images.list"
ppa_url="http://ppa.launchpad.net/maas-maintainers/maas-ephemeral-images/ubuntu"
echo "deb $ppa_url $rel main" | sudo tee "$dir/${ppa_file}"
chroot $dir apt-key add - <<"EOF"
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: SKS 1.0.10
mI0ETxf8lwEEAMAqtJVUnlCVzjTOsohaE/M4mGHFl4Py1cuE9ryOgmTWje+6BrNjtWLSfTQJ
Kp1V6hViUoxPBck1qkZoAz7VU5nDuBWDybCsolliUX4zzTYNiDnPS74fs4CDUWx9qpl5Sdb6
7aygIid/mFXubhJnTPR6Bq9ptGmc0Ks6ttNs3WJ/ABEBAAG0IkxhdW5jaHBhZCBQUEEgZm9y
IE1hYVMgTWFpbnRhaW5lcnOIuAQTAQIAIgUCTxf8lwIbAwYLCQgHAwIGFQgCCQoLBBYCAwEC
HgECF4AACgkQXP8eqZPujMU2zQP/W9OCzaU7HvFrqEt6nHGej2PEanIunxo7J8D5OR+Yl578
FpRkHRgvcdQnGuZUpdBnOFatDDFME7ClN9qUrD1wDN1r9ip2luaKiO2cZOW4Uu5Z0n/3Qc6J
eh9TNspyDMuHVVZ5GiAk+GXgF1m7ps5lCnOCZK/pXUEEUOS8AWnt3sM=
=1RpZ
-----END PGP PUBLIC KEY BLOCK-----
EOF
fi
mount --bind /dev "$dir/dev" ||
{ error "failed to bind-mount /dev to $dir"; return 1; }
LC_ALL=C FLASH_KERNEL_SKIP=1 DEBIAN_FRONTEND=noninteractive \
apt_opts="${apt_opts}" \
PPA_CLEAR="$ppa_file" \
chroot "$dir" sh -c '
_apt() {
echo apt-get ${apt_opts} -q --assume-yes "$@" 1>&2;
apt-get ${apt_opts} -q --assume-yes "$@";
}
aptin() { _apt install "$@"; }
aptrm() { _apt remove --purge "$@"; }
aptupgrade() { _apt dist-upgrade "$@"; }
aptautoremove() { _apt autoremove; }
rel=$(lsb_release -sc)
# ensure mellanox mlx4_en gets loaded (LP: #1115710)
if [ ! -f "/etc/modprobe.d/mlx4.conf" ]; then
# packaged version would be mlx4.conf, to avoid conflict
# we install a different file here.
printf "%s\n%s; %s\n" \
"# mlx4_core should load mlx4_en (LP: #1115710)." \
"install mlx4_core /sbin/modprobe --ignore-install mlx4_core" \
"/sbin/modprobe mlx4_en" \
> "/etc/modprobe.d/mlx4.ephemeral.conf"
fi
arch="$(dpkg --print-architecture)" || exit
mkdir -p /etc/iscsi && touch /etc/iscsi/iscsi.initramfs &&
apt-get -q ${apt_opts} update || exit
if [ "$arch" = "armhf" ]; then
pkgs="u-boot-tools"
removes="linux.*lpae"
case "$rel" in
saucy|trusty|u*)
pkgs="$pkgs linux-generic";;
precise|quantal|raring)
pkgs="$pkgs linux-image-highbank";;
esac
removes="$removes linux.*omap"
elif [ "$arch" = "ppc64el" ]; then
pkgs="linux-generic"
else
removes="linux.*virtual"
pkgs="linux-server"
fi
set -f;
toremove=""
for r in $removes; do
dpkg-query --show | grep -q "$r" &&
toremove="$toremove $r"
done
aptrm $toremove
set +f
aptin $pkgs || exit
aptautoremove || exit
aptupgrade || exit
pkgs="cloud-initramfs-dyn-netconf maas-enlist open-iscsi overlayroot"
aptin $pkgs || exit
if [ -n "$PPA_CLEAR" -a -f "$PPA_CLEAR" ]; then
sed -i "s,^,#," "$PPA_CLEAR" || exit
fi
if dpkg --print-foreign-architectures | grep -q i386; then
# precise used multiarch file
dpkg --remove-architecture i386 >/dev/null 2>&1 ||
rm /etc/dpkg/dpkg.cfg.d/multiarch
dpkg --print-foreign-architectures | grep i386 &&
{ echo "failed to remove i386 foreign arch"; exit 1; }
echo "removed foreign arch i386"
fi
apt-get clean && apt-get update || exit
f=/etc/network/interfaces
mv "$f" "$f.dist" &&
ln -sf ../../run/network/dynamic-interfaces "$f" || exit
k=""
for i in /boot/vmlinu?-*; do
[ "${i%-virtual}" = "${i}" ] && k=${i}; done
ver=${k##*/vmlinu?-}
update-initramfs -u -k $ver &&
cp /boot/initrd.img-$ver /tmp/initrd.img &&
cp /boot/vmlinu?-$ver /tmp/kernel.img &&
chmod ugo+r /tmp/kernel.img &&
dpkg-query --show > /tmp/manifest' </dev/null
[ $? -eq 0 ] || {
error "failed to install packages or mkinitramfs in chroot";
return 1;
}
mv "$dir/tmp/kernel.img" "$kernel_out" &&
mv "$dir/tmp/initrd.img" "$initrd_out" ||
{ error "failed to copy kernels out"; return 1; }
# LP: #1034116: disable rescuevol and overlayroot's diskcfg
if grep -q "^cloud-initramfs-rescuevol${tab}" "$dir/tmp/manifest"; then
# if cloud-initramfs-resuevol is installed, disable it
echo "# cloud-initramfs-rescuevol is disabled by ${0##*/}" \
> "$dir/etc/rescuevol-ignore" ||
{ error "failed to disable rescuevol"; return 1; }
fi
if grep -q "^overlayroot${tab}" "$dir/tmp/manifest"; then
printf "%s\n%s\n%s\n" "# overlayroot_cfgdisk disabled by ${0##*/}" \
"overlayroot_cfgdisk=disabled" "overlayroot=tmpfs" \
> "$dir/etc/overlayroot.local.conf"
fi
if [ -n "$manifest" ]; then
mv "$dir/tmp/manifest" "$manifest" || fail "failed to move manifest"
else
rm "$dir/tmp/manifest" ||
fail "failed to rm manifest"
fi
rm -f "$dir/etc/resolv.conf"
[ ! -e "$dir/etc/resolv.conf.dist" -a ! -L "$dir/etc/resolv.conf.dist" ] ||
mv "$dir/etc/resolv.conf.dist" "$dir/etc/resolv.conf" ||
{ error "failed to replace resolv.conf"; return 1; }
undisable_services "$dir"
dd bs=1M if=/dev/zero of="$dir/tmp/zero"
rm -f "$dir/tmp/zero"
return 0
}
short_opts="hv"
long_opts="help,verbose"
getopt_out=$(getopt --name "${0##*/}" \
--options "${short_opts}" --long "${long_opts}" -- "$@") &&
eval set -- "${getopt_out}" ||
bad_Usage
while [ $# -ne 0 ]; do
cur=${1}; next=${2};
case "$cur" in
-h|--help) Usage ; exit 0;;
-v|--verbose) VERBOSITY=$((${VERBOSITY}+1));;
--) shift; break;;
esac
shift;
done
[ $# -eq 3 -o $# -eq 4 ] ||
bad_Usage "expected image, kernel, ramdisk [,manifest]"
img="$1"
kernel="$2"
initrd="$3"
manifest="$4"
TEMP_D=$(mktemp -d "${TMPDIR:-/tmp}/${0##*/}.XXXXXX") ||
fail "failed to make tempdir"
trap cleanup EXIT
mount_callback_umount "$img" apply_updates \
"$kernel" "$initrd" ${manifest:+"${manifest}"} ||
fail "failed to apply updates to $img"
chown "--reference=$img" "$kernel" "$initrd" ${manifest:+"${manifest}"}
exit 0
# vi: ts=4 noexpandtab
|