-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
quiz-prepare-kernel
executable file
·263 lines (229 loc) · 7.11 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
#!/usr/bin/env bash
# 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, 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)
-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 build latest of all kernel series with available config
-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_kernel=""
opt_no_config=""
opt_no_build=""
opt_llvm=""
opt_update_config=""
opt_modify_config=""
opt_all_kernels=""
OPTIND=1
while getopts "k:CBe:d:m:uLKh" opt
do
case "$opt" in
'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 ;;
'h') usage ;;
*) exit 1 ;;
esac
done
shift $(expr $OPTIND - 1)
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 [[ -n $opt_all_kernels ]] ; then
opt_kernel=$(ls kernel/config-* | cut -d- -f2 | sort -V)
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
declare -A kernel_sumfile
kernel_versions=""
for kver in $opt_kernel ; do
_quiz_kernel_vars $kver
if [[ $_quiz_kmajor && $_quiz_kminor && -z $_quiz_kpatch ]] ; 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-($kver\.[0-9]+)\.tar\.xz/\1/ p }" | tail -1)
if [[ -z $latest ]] ; then
trace "no available kernel for $kver"
kver=""
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"
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
if [[ -d build/kernel/linux-$_quiz_ksrcver ]] ; then
trace "using existing source dir: build/kernel/linux-$_quiz_ksrcver"
if [[ $ksrctype == 'git' ]] ; then
trace "cleaning up git dir: build/kernel/linux-$_quiz_ksrcver"
git -C build/kernel/linux-$_quiz_ksrcver -fdx
fi
else
trace "setting up for build in: build/kernel/linux-$_quiz_ksrcver"
mkdir -p build/kernel
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
kcfg=config-$_quiz_kmajor.$_quiz_kminor
if [[ -z $opt_no_config ]] ; then
working_kcfg=$kcfg
if [[ ! -f kernel/$working_kcfg ]] ; then
trace "config not found: $working_kcfg"
working_kcfg=$(ls kernel/config-$_quiz_kmajor.* | cut -f2 -d/ | tail -1)
fi
if [[ ! -f kernel/$working_kcfg ]] ; then
trace "config not found: $working_kcfg"
working_kcfg=$(ls kernel/config-* | cut -f2 -d/ | tail -1)
fi
if [[ ! -f kernel/$working_kcfg ]] ; then
trace "config not found: $working_kcfg"
fatal "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/$working_kcfg build/kernel/linux-$_quiz_ksrcver/.config
else
trace "NOT installing config as requested"
fi
pushd build/kernel/linux-$_quiz_ksrcver > /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
trace "building kernel: $_quiz_ksrcver"
make -j6 $makeopts
else
trace "NOT building kernel as requested"
fi
popd > /dev/null
if [[ -n $opt_update_config ]] ; then
trace "updating standard config: $kcfg"
cp build/kernel/linux-$_quiz_ksrcver/.config kernel/$kcfg
fi
if [[ -z $opt_no_build ]] ; then
trace "moving completed kernel into quiz system dir: $_quiz_kver"
mkdir -p system
cp build/kernel/linux-$_quiz_ksrcver/arch/x86/boot/bzImage system/bzImage-$_quiz_kver
fi
trace "build finished: $_quiz_kver"
done
# vim: ft=bash