-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquiz-prepare-kernel
executable file
·349 lines (303 loc) · 10.1 KB
/
quiz-prepare-kernel
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
#!/usr/bin/env bash
# SPDX-License-Identifier: MPL-2.0
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright (c) 2023-2025, Rob Norris <[email protected]>
set -uo pipefail
usage() {
cat <<EOF
compile a kernel optimised for the quiz microvm
usage: quiz-prepare-kernel [opts]
options:
-k <kernel> kernel to build
(can be used multiple times)
-a <arch> architecture to build for
-e/-d/-m <key> set kernel config key to enable/disable/module (Y/N/M)
(can be used multiple times)
-u update standard config for this kernel minor version
-L build with LLVM/Clang
-C don't install kernel config before build
-B don't build kernel
-K rebuild all installed kernels
(equivalent to -k <v1> -k <v2> ...)
-U upgrade kernels (build latest for minor series)
-X with -U, remove old kernels in same minor series
-h this help
EOF
exit 1
}
trace() {
local STAMP=$(date +%Y%m%d-%H:%M:%S)
echo "[quiz-prepare-kernel] $STAMP $@" >&2
}
fail() {
local TEXT=${@:-}
if [[ -z $TEXT ]] ; then
TEXT="[${BASH_SOURCE[0]}:${BASH_LINENO[0]}: $BASH_COMMAND]"
fi
trace "FATAL $TEXT"
exit 1
}
trap fail ERR
RUNDIR=$(realpath $(dirname $0))
source $RUNDIR/quiz-config
source $RUNDIR/quiz-lib
opt_arch=""
opt_kernel=""
opt_no_config=""
opt_no_build=""
opt_llvm=""
opt_update_config=""
opt_modify_config=""
opt_all_kernels=""
opt_upgrade=""
opt_upgrade_cleanup=""
OPTIND=1
while getopts "a:k:CBe:d:m:uLKUXh" opt
do
case "$opt" in
'a') opt_arch=$OPTARG ;;
'k') opt_kernel="$opt_kernel $OPTARG" ;;
'C') opt_no_config=1 ;;
'B') opt_no_build=1 ;;
'e') opt_modify_config="$opt_modify_config -e $OPTARG" ;;
'd') opt_modify_config="$opt_modify_config -d $OPTARG" ;;
'm') opt_modify_config="$opt_modify_config -m $OPTARG" ;;
'u') opt_update_config=1 ;;
'L') opt_llvm=1 ;;
'K') opt_all_kernels=1 ;;
'U') opt_upgrade=1 ;;
'X') opt_upgrade_cleanup=1 ;;
'h') usage ;;
*) exit 1 ;;
esac
done
shift $(expr $OPTIND - 1)
quiz_arch_vars ${opt_arch:-$QUIZ_ARCH}
cd $RUNDIR
if [[ -n $opt_kernel && -n $opt_all_kernels ]] ; then
fail "invalid kernel version options; can't use -k and -K together"
fi
if [[ -z $opt_kernel && -z $opt_all_kernels ]] ; then
fail "no kernel version supplied; one of -k or -K required"
fi
if [[ -z $opt_upgrade && -n $opt_upgrade_cleanup ]] ; then
fail "invalid upgrade options; can't use -X without -U"
fi
if [[ -n $opt_all_kernels ]] ; then
opt_kernel=$(ls system/kernel/$_quiz_arch_kernel/kernel-* | cut -d- -f2 | sort -V)
if [[ -z $opt_kernel ]] ; then
fail "no kernels found in system/kernel/$_quiz_arch_kernel"
fi
fi
makeopts=""
if [[ -n $opt_llvm ]] ; then
IFS=:
best_clang=$(find $PATH -maxdepth 1 -executable -name "clang-??" -print | \
sed -Ee 's/^.+-//' | sort -r | head -1)
unset IFS
if [[ -z "$best_clang" ]] ; then
trace "no specific clang found, setting LLVM=1"
best_clang=1
else
trace "found clang-$best_clang, setting LLVM=-$best_clang"
best_clang="-$best_clang"
fi
makeopts="$makeopts LLVM=$best_clang"
fi
if [[ -z $_quiz_arch_native ]] ; then
trace "setting up for cross-compile: $_quiz_arch_kernel"
makeopts="$makeopts CROSS_COMPILE=${_quiz_arch_prefix}- ARCH=$_quiz_arch_kernel"
fi
declare -A kernel_sumfile
kernel_versions=""
for kver in $opt_kernel ; do
_quiz_kernel_vars $kver
if [[ $_quiz_kmajor && $_quiz_kminor && ( -z $_quiz_kpatch || -n $opt_upgrade ) ]] ; then
if [[ -z ${kernel_sumfile[$_quiz_kmajor]:-} ]] ; then
sumfile=build/kernel/sha256sums.$_quiz_kmajor
trace "fetching list of available kernels for v$_quiz_kmajor.x"
if [[ ! -e $sumfile ]] ; then
touch --date=@0 $sumfile
fi
curl -# -f -o $sumfile -z $sumfile -R \
https://cdn.kernel.org/pub/linux/kernel/v$_quiz_kmajor.x/sha256sums.asc
kernel_sumfile[$_quiz_kmajor]=$sumfile
fi
latest=$(cat ${kernel_sumfile[$_quiz_kmajor]} | \
sed -Ene "/linux-.+\.tar.xz/ { s/.*linux-($_quiz_kmajor\.$_quiz_kminor\.[0-9]+)\.tar\.xz/\1/ p }" | tail -1)
if [[ -z $latest ]] ; then
trace "no latest kernel matching $_quiz_kmajor.$_quiz_kminor"
kver=""
elif [[ -n $opt_upgrade ]] ; then
if [[ $latest == $kver ]] ; then
trace "$kver already latest, not rebuilding"
kver=""
else
trace "will upgrade $kver to $latest"
kver=$latest
fi
else
trace "will build $latest for $kver"
kver=$latest
fi
fi
if [[ $kver ]] ; then
kernel_versions="$kernel_versions $kver"
fi
done
kernel_versions=$(echo $kernel_versions | xargs -n1 | sort -V | uniq | xargs)
trace "will build kernel versions: $kernel_versions"
kdest=$RUNDIR/system/kernel/$_quiz_arch_kernel
for kver in $kernel_versions ; do
quiz_kernel_vars $kver
case $_quiz_ktype in
release)
ksrctype=tar
ktar=linux-$_quiz_ksrcver.tar.xz
kurl=https://cdn.kernel.org/pub/linux/kernel/v$_quiz_kmajor.x/$ktar
kzopt=J
;;
rc)
ksrctype=tar
ktar=linux-$_quiz_ksrcver.tar.gz
kurl=https://git.kernel.org/torvalds/t/$ktar
kzopt=z
;;
next)
ksrctype=git
kgiturl=git://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git
kgittag=next-$_quiz_ktype_patch # XXX is this kinda $_quiz_ksrcver?
;;
*)
fail "unknown kernel version type: $_quiz_ktype"
esac
trace "preparing build: $_quiz_kver ($_quiz_ktype)"
mkdir -p build/kernel
ksrc=$RUNDIR/build/kernel/linux-$_quiz_ksrcver
if [[ -d $ksrc ]] ; then
trace "using existing source dir: $ksrc"
if [[ $ksrctype == 'git' ]] ; then
trace "cleaning up git dir: $ksrc"
git -C $ksrc clean -fdx
fi
else
trace "setting up for build in: $ksrc"
pushd build/kernel > /dev/null
case $ksrctype in
tar)
if [[ -f $ktar ]] ; then
trace "using existing tarball: $ktar"
else
trace "downloading: $kurl"
curl -# -fOL $kurl
fi
tar xf$kzopt $ktar
;;
git)
trace "cloning: $kgittag @ $kgiturl"
git clone \
--config advice.detachedHead=false \
--depth 1 \
--branch $kgittag \
$kgiturl linux-$_quiz_ksrcver
;;
*)
fail "unknown kernel source type: $ksrctype"
;;
esac
popd > /dev/null
fi
if [[ -f $ksrc/.config ]] ; then
if ! grep -q -m1 "^# Linux/$_quiz_arch_kernel" $ksrc/.config ; then
trace "existing source dir was for a different architecture, cleaning it"
make -s -C $ksrc distclean $makeopts
fi
fi
kcfg=config-$_quiz_kmajor.$_quiz_kminor
if [[ -z $opt_no_config ]] ; then
working_kcfg=$kcfg
if [[ ! -f kernel/$_quiz_arch_kernel/$working_kcfg ]] ; then
trace "config not found: $working_kcfg"
working_kcfg=$(ls kernel/$_quiz_arch_kernel/config-$_quiz_kmajor.* | \
cut -f3 -d/ | sort -V | tail -1)
fi
if [[ ! -f kernel/$_quiz_arch_kernel/$working_kcfg ]] ; then
trace "config not found: $working_kcfg"
working_kcfg=$(ls kernel/$_quiz_arch_kernel/config-* | \
cut -f3 -d/ | sort -V | tail -1)
fi
if [[ ! -f kernel/$_quiz_arch_kernel/$working_kcfg ]] ; then
trace "config not found: $working_kcfg"
fail "no usable config found!"
fi
if [[ $kcfg != $working_kcfg ]] ; then
trace "specific config not found, using fallback: $working_kcfg"
fi
trace "installing config: $working_kcfg"
cp kernel/$_quiz_arch_kernel/$working_kcfg \
build/kernel/linux-$_quiz_ksrcver/.config
else
trace "NOT installing config as requested"
fi
pushd $ksrc > /dev/null
if [[ -n $opt_modify_config ]] ; then
trace "updating config from commandline switches"
scripts/config $opt_modify_config
fi
trace "compiling config: $_quiz_ksrcver"
make -s -j6 olddefconfig $makeopts
if [[ -z $opt_no_build ]] ; then
kimagepath=$(make -s image_name $makeopts)
kimage=$(basename $kimagepath)
trace "building kernel: $_quiz_ksrcver ($kimage)"
make -j6 $makeopts $kimage modules
trace "installing completed kernel: $_quiz_kver"
mkdir -p $kdest
# merged /usr setup
mkdir -p $kdest/usr/lib
ln -sf usr/lib $kdest/lib
# install everything we need. the kernel's own install stuff doesn't quite
# do everything the way we want, so we do a few things ourselves
# XXX delete the old stuff first? -- robn, 2025-01-13
# kernel and system map
cp -vf $ksrc/$kimagepath $kdest/kernel-$_quiz_kver
cp -vf $ksrc/System.map $kdest/System.map-$_quiz_kver
# modules tree
make -C $ksrc $makeopts modules_install INSTALL_MOD_PATH=$kdest
/sbin/depmod -b $kdest $_quiz_kver
# extern module build, so we're not dependent on kernel source to rebuild
# this is roughly what you find symlinked from /lib/modules/XXX/build
# on stock Linux systems. other architectures may require more/different
# things, refer to builddeb/buildrpm/install-extmod-build in the kernel
# source tree for details
mkdir -p $kdest/kbuild/$_quiz_kver
(
cd $ksrc
archdir=arch/$_quiz_arch_kernel
find Makefile include scripts tools/objtool/objtool -type f -o -type l
find $archdir -maxdepth 1 -name 'Makefile*'
find $archdir -name include -o -name scripts -type d
find $archdir/include Module.symvers include scripts -type f
) | tar -cf - -C $ksrc -T - | tar -xf - -C $kdest/kbuild/$_quiz_kver
else
trace "NOT building kernel as requested"
fi
popd > /dev/null
if [[ -n $opt_update_config ]] ; then
trace "updating standard config: $kcfg"
cp $ksrc/.config kernel/$_quiz_arch_kernel/$kcfg
fi
trace "build finished: $_quiz_kver"
if [[ -n $opt_upgrade_cleanup ]] ; then
trace "removing old kernels in minor series: $_quiz_kmajor.$_quiz_kminor"
find $kdest \
-name "*$_quiz_kmajor.$_quiz_kminor.*" -a \
! -name "*$_quiz_kver" \
| xargs -r rm -r
fi
done
# vim: ft=bash