Repository navigation
Expand file tree
/
Copy pathbuild-test-kernel
More file actions
executable file
·388 lines (332 loc) · 11.3 KB
/
Copy pathbuild-test-kernel
File metadata and controls
executable file
·388 lines (332 loc) · 11.3 KB
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
#!/usr/bin/env bash
set -o nounset
set -o errexit
set -o errtrace
ktest_dir=$(dirname "$(readlink -f "$0")")
KTEST=$ktest_dir/ktest
. "$ktest_dir/lib/libktest.sh"
checkdep gcc
checkdep clang
checkdep make
checkdep bison
checkdep flex
checkdep bc
ktest_njobs=$(nproc)
ktest_precise=false
ktest_compiler="${CC:-gcc}"
ktest_skip_kernel_config=false
COVERAGE="" # doing code coverage?
MAKEARGS=()
DEPMOD=depmod
if ! which depmod > /dev/null; then
DEPMOD=/sbin/depmod
fi
usage()
{
echo "build-test-kernel: Run generic virtual machine tests"
echo "Usage: build-test-kernel cmd [options]"
ktest_usage_cmds
echo " oldconfig Run make oldconfig"
echo " config Run make nconfig"
echo " build Build a kernel + publish to the kernel store"
echo
echo " options:"
ktest_usage_opts
echo
echo " options for build-test-kernel run:"
ktest_usage_run_opts
echo " -k <dir> kernel source dir to build from (default: cwd)"
echo " -c <dir> enable coverage for this dir (only valid without -K)"
echo " -M <arg> extra arguments to be passed to make when building the kernel"
echo " -K keep existing kernel .config"
echo
echo " options for build-test-kernel build:"
echo " -V <variant> variant name; sources tests/<variant>-base.sh for"
echo " require-kernel-config entries (default = none)"
echo " -D <dir> destination (kernel store entry); atomic publish"
echo
ktest_usage_post
}
if [[ $# = 0 ]]; then
usage
exit 1
fi
# Saved before the command and options are consumed, so ktest_supervise_self
# can re-exec us with exactly what we were invoked with.
ktest_argv=("$@")
#parse command and shift for rest of arg parsing
CMD="$1"
shift
ktest_build_variant=""
ktest_build_destdir=""
while getopts "k:Pc:M:KV:D:h${ktest_args}" arg; do
case $arg in
k)
# build-test-kernel's -k is the kernel SOURCE dir to build from
# (default: cwd). continue past parse_ktest_arg, which would
# otherwise also bind -k to ktest_kernel_name — the pre-built-kernel
# selector that the run path resolves to ktest_kernel_binary and
# rm -rf's as its build output dir.
ktest_kernel_source="$OPTARG"
continue
;;
P)
ktest_precise=true
;;
c)
if [[ ! -d $OPTARG ]]; then
echo "$OPTARG must be a directory"
exit 1
fi
checkdep lcov
# Strip trailing / from directory name, substitute _ for /
OPTARG=$(echo "${OPTARG%/}"|tr / _)
MAKEARGS+=("GCOV_PROFILE_$OPTARG=y")
COVERAGE=1
;;
M)
MAKEARGS+=("$OPTARG")
;;
K)
ktest_skip_kernel_config=true
;;
V)
ktest_build_variant="$OPTARG"
;;
D)
ktest_build_destdir="$OPTARG"
;;
h)
usage
exit 0
;;
esac
parse_ktest_arg $arg
done
shift $(( OPTIND - 1 ))
parse_args_post
if [[ ! -d $ktest_kernel_source ]]; then
echo "kernel source directory $ktest_kernel_source does not exist"
exit 1
fi
ktest_kernel_source=$(readlink -e "$ktest_kernel_source")
# Version we're building against, exposed for version-conditional kernel config
# in kconfig.sh (options that only exist or only apply on newer kernels). `make
# kernelversion` just reads VERSION/PATCHLEVEL/SUBLEVEL from the top Makefile so
# it works on an unconfigured tree — but not reliably inside the build nspawn,
# so honor a value the caller passed in (upstream-kernel-build sets it from the
# host). Log it: a wrong/empty version silently misfires kconfig.sh's guards.
ktest_kernel_version=${ktest_kernel_version:-$(make -s -C "$ktest_kernel_source" kernelversion 2>/dev/null)}
export ktest_kernel_version
echo "Kernel version: ${ktest_kernel_version:-UNKNOWN}"
ktest_kernel_build="$ktest_out/kernel_build.$ktest_arch"
mkdir -p "$ktest_kernel_build"
if [[ -n $CROSS_COMPILE ]]; then
checkdep "$ARCH_TRIPLE-gcc" "gcc-$ARCH_TRIPLE"
fi
run_ktest()
{
arg=$1
shift
"$KTEST" "$arg" $KTESTARGS "$@"
}
cmd_run()
{
if [[ $# = 0 ]]; then
echo "build-test-kernel: missing test"
usage
exit 1
fi
ktest_test=$(realpath "$1")
shift
ktest_testargs="$@"
echo Running test $(basename "$ktest_test") on $(uname -n) at $(pwd)
parse_test_deps "$ktest_test"
ktest_supervise_self
if [[ -n $COVERAGE ]]; then
ktest_kernel_config_require+=(GCOV_KERNEL)
fi
if ! $ktest_no_kbuild; then
run_quiet "building kernel" build_kernel
fi
if $ktest_no_vm; then
run_tests_host
else
start_vm
fi
}
run_tests_host()
{
local ret=0
mkdir -p "$ktest_out/out"
export ktest_kernel_source
export ktest_kernel_build
export ktest_out
echo "Running tests on host: $ktest_tests"
"$ktest_test" run-tests $ktest_tests || ret=$?
if [[ $ret = 0 ]]; then
echo "TEST PASSED"
else
echo "TEST FAILED"
fi
return $ret
}
cmd_boot()
{
cmd_run "$ktest_dir/boot.ktest"
}
# Build a kernel from $ktest_kernel_source and publish it as a kernel-store
# entry at the path given by -S. The variant given by -V picks a config
# overlay from tests/<variant>-base.sh (existing convention — same files
# the test machinery sources for require-kernel-config); -V is optional
# and a no-variant build just uses defconfig + whatever require-kernel-config
# entries live in the test-libs the variants share.
cmd_build()
{
if [[ -z $ktest_build_destdir ]]; then
echo "build-test-kernel build: -D <destdir> required"
exit 1
fi
# test-libs.sh is the proper test-side entry point: sources prelude.sh
# (which initializes ktest_* variables configure_kernel / build_kernel
# reference) AND kconfig.sh (which provides per-arch base requirements
# including MODULES, SMP, ACPI, etc. — without it MODULES=n and OOT
# builds fail). cmd_run gets all this via parse_test_deps; cmd_build
# sources it directly. Idempotent via prelude's `[[ ! -v ... ]]` guards.
. "$ktest_dir/tests/test-libs.sh"
if [[ -n $ktest_build_variant && $ktest_build_variant != default ]]; then
local base_sh="$ktest_dir/tests/${ktest_build_variant}-base.sh"
if [[ ! -f $base_sh ]]; then
echo "build-test-kernel build: no such variant — $base_sh not found"
exit 1
fi
# Harvest config requirements only; suppress the *-base.sh's
# tail-call to call_base_test (which would re-source a sibling
# file based on BASH_SOURCE[2] — points at us, not a test).
ktest_no_dispatch=true . "$base_sh"
ktest_no_dispatch=false
fi
# Stage into a sibling of the final path so the publish step is one
# rename. Survives concurrent builders for the same target (last one
# wins; rename(2) is atomic).
local stage="${ktest_build_destdir}.work.$$"
rm -rf "$stage"
mkdir -p "$stage"
ktest_kernel_binary="$stage"
build_kernel
finalize_kernel_build "$stage"
mkdir -p "$(dirname "$ktest_build_destdir")"
rm -rf "$ktest_build_destdir"
mv "$stage" "$ktest_build_destdir"
echo " [PUBLISH] $ktest_build_destdir"
}
# Round out a freshly-built kernel-store entry: write manifest.json, add
# the uniform `build` top-level symlink, copy headers tree.
finalize_kernel_build()
{
local stage=$1
local version
version=$(cat "$ktest_kernel_build/include/config/kernel.release")
# Headers tree: copy the build-output dir (objects + .config +
# Module.symvers + generated includes) plus the source-side scripts/
# and include/ trees so OOT module builds have everything.
# For an in-tree build (source == build dir), this is a single copy;
# with O= they may be separate.
#
# Exclusions trim ~200MB off a default kernel build:
# - non-target arch/ trees (we only need arch/$KERNEL_ARCH/ for
# this kernel's OOT module builds; ~100MB across arm, arm64,
# powerpc, mips, riscv, s390, etc.)
# - tools/{perf,testing,selftests,bpf}/ — internal/standalone
# userspace; not used by external module kbuild (~80MB)
# - object files, .cmd dep files, archives — recreated on use
local hdrs="$stage/lib/modules/$version/build"
rm -rf "$hdrs"
mkdir -p "$hdrs"
# Exclude patterns: NO leading './' — we tar from both the build dir
# (with `.` so paths in the archive start with `./`) and the source
# dir (with explicit `arch tools include …` so paths start without
# leading dot). Tar's exclude matches both forms when the pattern is
# plain `arch/<a>`.
local non_arch_excludes=()
local a
for a in alpha arc arm arm64 csky hexagon loongarch m68k microblaze mips \
nios2 openrisc parisc powerpc riscv s390 sh sparc um xtensa; do
[[ $a = $KERNEL_ARCH ]] && continue
non_arch_excludes+=("--exclude=arch/$a")
done
local tar_excludes=(
--exclude='*.o' --exclude='*.o.cmd' --exclude='*.ko' --exclude='*.ko.cmd'
--exclude='vmlinux*' --exclude='*.tmp_*' --exclude='built-in.a'
--exclude='tools/perf' --exclude='tools/testing'
--exclude='tools/selftests' --exclude='tools/bpf'
"${non_arch_excludes[@]}"
)
# Build-dir contents (configs, Module.symvers, scripts/, arch/<a>/include/generated, ...).
# tar's --exclude only applies to positional args that appear AFTER it on the
# command line — keep that order.
(cd "$ktest_kernel_build" && tar -cf - "${tar_excludes[@]}" .) \
| (cd "$hdrs" && tar -xf -)
# Source-dir headers + script sources (only when O= split from source).
if [[ $(readlink -f "$ktest_kernel_source") != $(readlink -f "$ktest_kernel_build") ]]; then
(cd "$ktest_kernel_source" && tar -cf - \
"${tar_excludes[@]}" \
Makefile Kbuild Kconfig \
include scripts arch tools 2>/dev/null) \
| (cd "$hdrs" && tar -xf - 2>/dev/null) || true
fi
ln -sfn "lib/modules/$version/build" "$stage/build"
# vmlinux-gdb.py (at the store root, from build_kernel) is a stub that
# imports the `linux` gdb package from ./scripts/gdb relative to itself.
# It's release+config-specific (constants.py is generated from this
# kernel's .config), so copy this build's into the store root — the stub
# then resolves and ktest_kgdb gets lx-symbols. The headers tree has it.
if [[ -f "$stage/vmlinux-gdb.py" && -d "$hdrs/scripts/gdb" ]]; then
mkdir -p "$stage/scripts"
cp -r "$hdrs/scripts/gdb" "$stage/scripts/gdb"
fi
cat > "$stage/manifest.json" <<EOF
{
"manifest_version": 1,
"kind": "upstream",
"variant": "${ktest_build_variant:-default}",
"version": "$version",
"arch": "$ktest_arch",
"kernel_source": "$(readlink -f "$ktest_kernel_source")",
"kernel_source_rev": "$(cd "$ktest_kernel_source" 2>/dev/null && git rev-parse HEAD 2>/dev/null || echo unknown)",
"built_at": "$(date -u +%Y-%m-%dT%H:%M:%SZ)"
}
EOF
}
cmd_oldconfig()
{
new_config
do_make oldconfig
}
cmd_config()
{
new_config
do_make nconfig "$@"
}
cmd_faddr2line()
{
./scripts/faddr2line "$ktest_kernel_build/vmlinux" $@
}
cmd_help()
{
usage
}
if [[ $(type -t "cmd_$CMD") == function ]]; then
CMD="cmd_$CMD"
elif [[ $(type -t "ktest_$CMD") == function ]]; then
CMD="ktest_$CMD"
else
usage
exit 1
fi
# build-test-kernel's local commands (run, config, oldconfig, boot) build into
# ktest-out/kernel.$arch; libktest no longer defaults ktest_kernel_binary, so set
# it here for all of them. cmd_build overrides it — it stages into a kernel-store
# path instead.
ktest_kernel_binary="$ktest_out/kernel.$ktest_arch"
$CMD "$@"