-
Notifications
You must be signed in to change notification settings - Fork 19
/
build-test-kernel
executable file
·348 lines (282 loc) · 6.83 KB
/
build-test-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
#!/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=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
echo " options:"
ktest_usage_opts
echo
echo " options for build-test-kernel run:"
ktest_usage_run_opts
echo " -k <dir> kernel source dir"
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
ktest_usage_post
}
if [[ $# = 0 ]]; then
usage
exit 1
fi
#parse command and shift for rest of arg parsing
CMD="$1"
shift
while getopts "k:Pc:M:Kh${ktest_args}" arg; do
case $arg in
k)
ktest_kernel_source="$OPTARG"
;;
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
;;
h)
usage
exit 0
;;
esac
parse_ktest_arg $arg
done
shift $(( OPTIND - 1 ))
parse_args_post
# default parameters
[[ -z $ktest_kernel_source ]] && ktest_kernel_source="."
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")
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 "$@"
}
do_make()
{
if [[ -n $CROSS_COMPILE ]]; then
export ARCH="$KERNEL_ARCH"
export CROSS_COMPILE="$ARCH_TRIPLE-"
fi
make --jobs="$ktest_njobs" \
--directory="$ktest_kernel_source" \
CC="$ktest_compiler" \
O="$ktest_kernel_build" \
INSTALL_MOD_PATH="$ktest_kernel_binary" \
"${ktest_kernel_make_append[@]}" \
"${MAKEARGS[@]}" \
"$@"
}
new_config()
{
local kconfig="$ktest_kernel_build/.config"
local config_tool="$ktest_kernel_source/scripts/config"
if [[ ! -f $kconfig ]]; then
do_make allnoconfig
# Really undefine everything:
sed -i -e 's/\(CONFIG_.*\)=.*/# \1 is not set/' "$kconfig"
fi
}
kernel_opt()
{
local cmd=$1
local opt=$2
local kconfig="$ktest_kernel_build/.config"
local config_tool="$ktest_kernel_source/scripts/config"
local val=y
local ret=0
if [[ $opt =~ = ]]; then
local val=${opt#*=}
opt="${opt%=*}"
fi
case $cmd in
set)
"$config_tool" --file "$kconfig" --set-val "$opt" "$val"
;;
check)
local c=$("$config_tool" --file "$kconfig" -s "$opt")
[[ $c = undef ]] && c=n
if [[ $c != $val ]]; then
echo "Kernel config option $opt is $c; should be $val"
# If the current kernel doesn't have the option available,
# don't fail
if [[ $(cd $ktest_kernel_source; git grep "CONFIG_$opt") ]]; then
ret=1
fi
fi
;;
esac
if [[ $ret != 0 ]]; then
return $ret
fi
}
configure_kernel()
{
local kconfig="$ktest_kernel_build/.config"
if [[ -f "$kconfig" ]]; then
cp "$kconfig" "$kconfig".bak
fi
if [[ -z $ktest_kconfig_base ]]; then
if $ktest_precise; then
rm -f "$kconfig"
fi
new_config
else
cp "$ktest_kconfig_base" "$kconfig"
fi
log_verbose "kernel_config_require: ${ktest_kernel_config_require[@]} ${ktest_kernel_config_require_soft[@]}"
MAKEARGS+=("LOCALVERSION=-ktest")
for opt in "${ktest_kernel_config_require[@]}"; do
[[ -n $opt ]] && kernel_opt set "$opt"
done
for opt in "${ktest_kernel_config_require_soft[@]}"; do
[[ -n $opt ]] && kernel_opt set "$opt"
done
do_make olddefconfig
for opt in "${ktest_kernel_config_require[@]}"; do
[[ -n $opt ]] && kernel_opt check "$opt"
done
# Preserve timestamp if config didn't change:
if [[ -f "$kconfig".bak ]] && diff -q "$kconfig" "$kconfig".bak; then
mv "$kconfig".bak "$kconfig"
fi
}
build_kernel()
{
rm -rf "$ktest_kernel_binary"
mkdir -p "$ktest_kernel_binary"
if ! $ktest_skip_kernel_config; then
configure_kernel
fi
case $KERNEL_ARCH in
mips)
do_make -k vmlinuz
;;
*)
do_make -k
;;
esac
local BOOT=$ktest_kernel_build/arch/$KERNEL_ARCH/boot
case $ktest_arch in
x86*)
install -m0644 "$BOOT/bzImage" "$ktest_kernel_binary/vmlinuz"
;;
aarch64)
install -m0644 "$BOOT/Image" "$ktest_kernel_binary/vmlinuz"
;;
mips)
install -m0644 "$BOOT/vmlinux.strip" "$ktest_kernel_binary/vmlinuz"
#install -m0644 "$ktest_kernel_build/vmlinux" "$ktest_kernel_binary/vmlinuz"
;;
default)
echo "Don't know how to install kernel"
exit 1
;;
esac
install -m0644 "$ktest_kernel_build/vmlinux" "$ktest_kernel_binary/vmlinux"
install -m0644 "$ktest_kernel_build/.config" "$ktest_kernel_binary/config"
# if there weren't actually any modules selected, make modules_install gets
# confused:
touch "$ktest_kernel_build/modules.order"
touch "$ktest_kernel_build/modules.builtin"
do_make modules_install
local kernel_version=$(cat "$ktest_kernel_build/include/config/kernel.release")
$DEPMOD -b "$ktest_kernel_binary/" -v $kernel_version
}
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"
if [[ -n $COVERAGE ]]; then
ktest_kernel_config_require+=(GCOV_KERNEL)
fi
run_quiet "building kernel" build_kernel
start_vm
}
cmd_boot()
{
cmd_run "$ktest_dir/boot.ktest"
}
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
$CMD "$@"