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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
|
#!/bin/bash
VERBOSITY=0
TEMP_D=""
COPY_TYPES="auto overlay qcow2 cp none"
error() { echo "$@" 1>&2; }
fail() { [ $# -eq 0 ] || error "$@"; exit 1; }
Usage() {
cat <<EOF
Usage: ${0##*/} [ options ] image kernel-package out-dir
Install kernel-package into image and copy the generated kernel
and initrd out into out-dir
out-dir/
kernel
initrd
dtb-<filename>.dtb # for each file in lib/firmare/<ver>/device-tree
Requires sudo to invoke mount-image-callback.
options:
--kihelper PROG invoke PROG with kernel and initrd
This allows re-packing (ie, mkimage the files)
PROG is invoked with 2 arguments:
kernel-path initrd-path
it will have in its environment:
ROOT: path to the mounted 'image'
VERSION_FLAVOR: a string like 3.13.0-29-generic
--copy mode use 'mode' to copy. default: auto
one of: ${COPY_TYPES}
overlay can be used only with mount-image-callback support
EOF
}
bad_Usage() { Usage 1>&2; [ $# -eq 0 ] || error "$@"; return 1; }
cleanup() {
[ -z "${TEMP_D}" -o ! -d "${TEMP_D}" ] || rm -Rf "${TEMP_D}"
}
debug() {
local level=${1}; shift;
[ "${level}" -gt "${VERBOSITY}" ] && return
error "${@}"
}
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"
}
_chroot() {
local dir="$1"
shift
if [ "$dir" = "/" ]; then
"$@"
else
chroot "$dir" "$@"
fi
}
replace_newaliases() {
local dir="$1"
debug 1 "hacking around newaliases in root (LP: #1531299)"
_chroot "$dir" dpkg-divert --local --rename --add /usr/bin/newaliases &&
ln -s /bin/true "$dir/usr/bin/newaliases"
}
unreplace_newaliases() {
local dir="$1" chroot_dir=""
debug 1 "un-diverting newaliases in root"
[ "$dir/bin/true" -ef "$dir/usr/bin/newaliases" ] || return 0
rm -f "$dir/usr/bin/newaliases"
_chroot "$dir" dpkg-divert --local --rename --remove /usr/bin/newaliases
}
kernel_from_mp() {
# this is the unwrapped function that actually does everything
# it essentially gets called by mount-image-callback.
local short_opts="hv"
local long_opts="chown-to:,help,kihelper:,proposed,verbose"
local getopt_out=""
getopt_out=$(getopt --name "${0##*/}" \
--options "${short_opts}" --long "${long_opts}" -- "$@") &&
eval set -- "${getopt_out}" ||
{ bad_Usage; return; }
local cur="" next=""
local output="" copy=true chown_to="" gid="" uid="" kihelper=""
local proposed="false"
local skip_update=${KPACK_SKIP_APT_UPDATE:-false}
while [ $# -ne 0 ]; do
cur="$1"; next="$2";
case "$cur" in
--chown-to) chown_to=$next; shift;;
-h|--help) Usage ; exit 0;;
-v|--verbose) VERBOSITY=$((${VERBOSITY}+1));;
--kihelper) kihelper="$next"; shift;;
--proposed) proposed=true;;
--skip-update) skip_update=true;;
--) shift; break;;
esac
shift;
done
[ $# -eq 3 ] ||
{ bad_Usage "expected 3 args, got $# [$*]"; return; }
local mp="$1" kpkg="$2" outd="$3"
local me="" myd=""
me=$(readlink -f "$0") && myd="${me%/*}" ||
{ error "couldn't figure out full path to $0"; return 1; }
if [ -n "$kihelper" ]; then
if ! command -v "$kihelper" >/dev/null 2>&1 ; then
[ -x "$myd/$kihelper" ] ||
{ error "failed to find kihelper"; return 1; }
kihelper="$myd/$kihelper"
fi
fi
if [ ! -d "$outd" ]; then
mkdir -p "$outd" ||
{ error "failed mkdir $outd"; return 1; }
if [ -n "$chown_to" ]; then
chown "$chown_to" "$outd" || {
error "failed chown output dir '$outd' to '$chown_to'";
return 1;
}
fi
fi
local ch_outd="/tmp/output"
KPACK_SKIP_APT_UPDATE=$skip_update CHROOTED_VERBOSITY=$VERBOSITY \
PROPOSED=$proposed \
chroot "$mp" /bin/bash -s chrooted "$kpkg" "$ch_outd" < "$me" ||
{ error "failed chroot and kernel install"; return 1; }
if [ -n "$kihelper" ]; then
local verflav=""
read verflav < "$mp/$ch_outd/verflav"
debug 1 "using $kihelper verflav='$verflav'"
ROOT="$mp" VERSION_FLAVOR="$verflav" "$kihelper" \
"$mp/${ch_outd}/kernel" "$mp/${ch_outd}/initrd" ||
{ error "$kihelper failed"; return 1; }
fi
if [ -n "$chown_to" ]; then
( cd "$mp/$ch_outd" && chown "$chown_to" * ) || {
error "failed chown $mp/$ch_outd/*";
return 1;
}
fi
mv "$mp/$ch_outd/"* "$outd" ||
{ error "failed move files from chrooted outdi '$ch_outd'"; return 1; }
}
config_addl_modules() {
local root="${1:-/}" fname=""
fname="${root%/}/etc/initramfs-tools/hooks/kpack_extra"
cat > "$fname" <<EOF
#!/bin/sh
. /usr/share/initramfs-tools/hook-functions
copy_modules_dir kernel/drivers/char/ipmi # LP: #1333271
copy_modules_dir kernel/drivers/net/usb # LP: #1552378
manual_add_modules bcache # (LP: #1513176)
EOF
[ $? -eq 0 ] || { error "failed writing $fname"; return 1; }
chmod 755 "$fname" ||
{ error "failed setting execute on $fname"; return 1; }
return 0
}
ensure_iscsi_initramfs_support() {
local root="${1:-/}"
local mfile="${root%/}/etc/iscsi/iscsi.initramfs"
if [ ! -f "$mfile" ]; then
mkdir -p "${mfile%/*}" && touch "$mfile" ||
{ error "failed to create iscsi.initramfs"; return 1; }
fi
}
clean_local_udev_rules() {
# in wily and later, /etc/udev/rules.d/*.rules will be copied
# into the initramfs. If there existed a '70-persistent-net.rules'
# then those would be clearly wrong (mac addresses and naming for
# the specific system that generated this). Clean those out.
local root="${1:-/}"
rm -f "${root%/}/etc/udev/rules.d/70-persistent-net.rules"
}
insist_running_in_container() {
local root="${1:-/}"
ln -sf /bin/true "${root%/}/bin/running-in-container"
local sdv="/usr/bin/systemd-detect-virt"
local fpsdv="${root%/}${sdv}"
if [ -x "${fpsdv}" ]; then
# make 'systemd-detect-virt X' exit true for any value of
# X in -c/--container -r/--chroot
mv "${fpsdv}" "$fpsdv.real" && cat > "$fpsdv" <<"EOF"
#!/bin/sh
args=" $* "
hasarg() { [ "${args#* $1 }" != "$args" -o "${args#* $2 }" != "$args" ]; }
hasarg -q --quiet && msg=: || msg=echo
hasarg -c --container && { $msg "lxc"; exit 0; }
hasarg -r --chroot && { $msg "chroot"; exit 0; }
exec "$0.real" "$@"
EOF
[ $? -eq 0 ] || { error "failed writing $sdv"; return 1; }
chmod 755 "$fpsdv" || { error "failed chmod $fpsdv"; return 1; }
fi
}
vrun() {
local vlvl="$1" msg="$2"
shift 2
debug $vlvl "$msg: $*"
"$@"
}
chrooted() {
# this is expected to run chrooted inside the root
# installs all packages (arguments), writes kernel
# and initramfs and 'verflav' to /tmp/
VERBOSITY=${CHROOTED_VERBOSITY:-${VERBOSITY}}
local pkgs="" kpkg="" copymods zzgrub emd="" out pkg rel
local skip_update=${KPACK_SKIP_APT_UPDATE:-false}
local proposed=${PROPOSED:-false} proposed_lfile=""
local verflav initrd kernel
export LANG=C
kpkg="$1"
outd="$2"
shift 2;
pkgs=( "$kpkg" "$@" )
mkdir -p "$outd" || { error "failed mkdir $outd"; return 1; }
local stamp="$outd/stamp"
touch "$stamp"
# add proposed if necessary
rel=$(lsb_release -sc) ||
{ error "failed to read release"; return 1; }
if $proposed; then
local mirror=${UBUNTU_MIRROR:-http://archive.ubuntu.com/ubuntu/}
proposed_lfile="/etc/apt/sources.list.d/proposed-$rel.list"
debug 1 "adding proposed ($mirror) to $proposed_lfile"
echo "deb $mirror ${rel}-proposed main" > "$proposed_lfile"
$skip_update &&
{ skip_update=false; debug 1 "enabling update due to proposed"; }
fi
# trusty and wily do not have mdadm or lvm2 installed in the image
# but we want those modules included (LP: #1519470)
# the packages are expected to be in xenial already
case "$rel" in
precise|trusty|wily) pkgs=( lvm2 mdadm "${pkgs[@]}" );;
esac
$skip_update || apt-get update -q ||
{ error "apt-get update failed"; return 1; }
copymods="cloud-initramfs-copymods"
if [ -f /usr/share/initramfs-tools/hooks/copymods ]; then
error "$copymods already installed"
copymods=""
else
apt-cache show $copymods >/dev/null 2>&1 ||
{ error "copymods not available"; return 1; }
fi
zzgrub=/etc/kernel/postinst.d/zz-update-grub
[ ! -x "$zzgrub" ] || chmod ugo-x $zzgrub
which eatmydata >/dev/null 2>&1 && emd=eatmydata
config_addl_modules / ||
{ error "failed configuring additional modules!"; return 1; }
ensure_iscsi_initramfs_support / ||
{ error "failed to enable iscsi initramfs support"; return 1; }
clean_local_udev_rules / ||
{ error "failed configuring additional modules!"; return 1; }
insist_running_in_container / ||
{ error "failed to set running-in-container to true"; return 1; }
disable_services / ||
{ error "failed to disable services before apt installs"; return 1; }
replace_newaliases / ||
{ error "failed to replace newaliases"; return 1; }
vrun 1 "installing packages" \
env DEBIAN_FRONTEND=noninteractive FLASH_KERNEL_SKIP=1 \
$emd apt-get install --assume-yes --quiet \
$copymods "${pkgs[@]}" </dev/null
[ $? -eq 0 ] ||
{ error "failed install of $copymods ${pkgs[*]}"; return 1; }
undisable_services / ||
{ error "failed to re-enable services after apt installs"; return 1; }
unreplace_newaliases / ||
{ error "failed to un-replace newaliases"; return 1; }
[ -z "$proposed_lfile" ] || rm -f "$proposed_lfile"
# here we look at all kernel files in /boot and get the package that
# owns it. if the package is the kernel that was supplied, then we
# found it. It supports 'kpkg' being a meta package by seeing
# if kpkg would be removed if we uninstalled the kernel package.
kernel=""
for f in /boot/vmlin*; do
[ -f "$f" ] || continue
out=$(dpkg -S "$f")
pkg=${out%%:*}
[ "$pkg" = "$kpkg" ] && kernel="$f" && break
if apt-get --dry-run remove $pkg | grep -q "Remv $kpkg "; then
kernel="$f"
break
fi
done
[ -z "$kernel" ] && { error "no kernel found!" 1>&2; return 1; }
verflav=${kernel#/boot/*-}
initrd="/boot/initrd.img-$verflav"
debug 2 "verflav is $verflav"
if [ "$initrd" -ot "$stamp" ]; then
debug 1 "initramfs did not update by package install. regenerating."
update-initramfs -c -k "$verflav"
fi
[ -f "$initrd" ] || { error "no initrd for $verflav" 1>&2; return 1; }
local name="" t="" outf=""
for f in /boot/*-${verflav}; do
t=$(basename "$f")
case "$t" in
vmlinu?*) name="kernel";;
initrd.img*) name="initrd";;
*)
# name is the basename with -${verfla} removed
name="${t%-${verflav}}"
esac
outf="$outd/$name"
debug 2 "$f -> $outf"
cp "$f" "$outf" && chmod ugo+r "$outf" ||
{ error "failed to copy '$name' from '$f'"; return 1; }
done
local path="/lib/firmware/$verflav/device-tree"
if [ -d "$path" ]; then
for f in $(find "$path" -type f -name "*.dtb"); do
outf="$outd/dtb-${f##*/}"
debug 2 "$f -> $outf"
[ ! -f "$outf" ] || {
error "basename collision over '${outf##*/}'"
find "$path" -type f -name "${f##*/}" 1>&2
return 1
}
cp "$f" "$outf" && chmod ugo+r "$outf" ||
{ error "failed to copy '$outf' from '$f'"; return 1; }
done
else
debug 1 "dtb path '$path' did not exist. assume no dtb files"
fi
echo "$verflav" > "$outd/verflav" ||
{ error "failed to write verflavor"; return 1; }
rm -f "$stamp"
}
main() {
local short_opts="hv"
local long_opts="help,kihelper:,copy:,proposed,verbose"
local getopt_out=""
getopt_out=$(getopt --name "${0##*/}" \
--options "${short_opts}" --long "${long_opts}" -- "$@") &&
eval set -- "${getopt_out}" ||
{ bad_Usage; return; }
local cur="" next="" vflags=""
local output="" copy=auto chown_to="" gid="" uid="" proposed_flag=""
while [ $# -ne 0 ]; do
cur="$1"; next="$2";
case "$cur" in
-h|--help) Usage ; exit 0;;
-v|--verbose) VERBOSITY=$((${VERBOSITY}+1)); vflags="${vflags}v";;
--kihelper) kihelper="$next"; shift;;
--copy) copy=$next; shift;;
--proposed) proposed_flag="--proposed";;
--) shift; break;;
esac
shift;
done
[ $# -eq 3 ] ||
{ bad_Usage "expected 3 args, got $#"; return; }
local img="$1" kpkg="$2" outd=${3}
[ -z "$vflags" ] || vflags="-${vflags}"
TEMP_D=$(mktemp -d "${TMPDIR:-/tmp}/${0##*/}.XXXXXX") ||
fail "failed to make tempdir"
trap cleanup EXIT
if [ "$copy" = "auto" ]; then
if mount-image-callback --help | grep -q -- --overlay; then
copy="overlay"
elif command -v qemu-img >/dev/null 2>&1; then
copy="qcow"
else
copy="cp"
fi
fi
local imgfp myimg="${TEMP_D}/my.img" overlay_flags=""
imgfp=$(readlink -f "$img") ||
{ error "failed to get full path to $img"; return 1; }
debug 1 "copy mode = $copy"
case "$copy" in
none) ln -s "$imgfp" "$myimg" ||
{ error "failed link $imgfp -> $myimg"; return 1; };;
cp) cp --sparse=always "$img" "$myimg" ||
{ error "failed cp '$img' to tempdir"; return 1; };;
qcow)
qemu-img create -f qcow2 -b "$imgfp" "$myimg" ||
{ error "failed create qcow2 backed by $img"; return 1; };;
overlay)
overlay_flags="--overlay --read-only"
ln -s "$imgfp" "$myimg" ||
{ error "failed link $imgfp -> $myimg"; return 1; };;
*) error "bad copy type. one of: $COPY_TYPES"; return 1;;
esac
gid=$(id -g)
uid=$(id -u)
chown_to="$uid:$gid"
local mic=""
mic=$(which mount-image-callback) ||
{ error "mount-image-callback not in PATH"; return 1; }
sudo \
"KPACK_SKIP_APT_UPDATE=${KPACK_SKIP_APT_UPDATE:-false}" \
"http_proxy=$http_proxy" PACKAGES="$kpkg" \
"$mic" --sys --proc $overlay_flags \
--system-resolvconf "$myimg" -- "$0" "kernel_from_mp" $vflags \
_MOUNTPOINT_ "--chown-to=$chown_to" \
${kihelper:+"--kihelper=$kihelper"} ${proposed_flag} \
"$kpkg" "$outd" ||
{ error "failed"; return 1; }
error "wrote to $outd"
error " " $(cd "$outd" && echo *)
return
}
[ "$1" = "chrooted" ] && { shift; chrooted "$@"; exit; }
[ "$1" = "kernel_from_mp" ] && { shift; kernel_from_mp "$@"; exit; }
main "$@"
# vi: ts=4 expandtab
|