summaryrefslogtreecommitdiff
path: root/lib/libktest.sh
blob: 86073e0e3f83038f8edb7375e530bc96d285b7c1 (plain)
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

. "$ktest_dir/lib/util.sh"
. "$ktest_dir/lib/parse-test.sh"

if [[ $(id -u) = 0 ]] ; then
    echo $0 should not be run as root
    exit 1
fi

ktest_root_image=""	# virtual machine root filesystem
                        #       set with: -i <path>
                        #       defaults: /var/lib/ktest/root
                        #       auto-override: $HOME/.ktest/root
ktest_out="./ktest-out"	# dir for test output (logs, code coverage, etc.)

ktest_priority=0	# hint for how long test should run
ktest_interactive=0     # if set to 1, timeout is ignored completely
                        #       sets with: -I
ktest_exit_on_success=0	# if true, exit on success, not failure or timeout
ktest_failfast=false
ktest_loop=0
ktest_verbose=0		# if false, append quiet to kernel commad line
ktest_crashdump=0
ktest_kgdb=0
ktest_ssh_port=0
ktest_networking=user
ktest_dio=off
ktest_nice=0

checkdep socat
checkdep qemu-system-x86_64	qemu-system-x86
checkdep brotli

# config files:
[[ -f $ktest_dir/ktestrc ]]	&& . "$ktest_dir/ktestrc"
[[ -f /etc/ktestrc ]]		&& . /etc/ktestrc
[[ -f $HOME/.ktestrc ]]		&& . "$HOME/.ktestrc"

# defaults:
[[ -f $HOME/.ktestrc ]]		&& . "$HOME/.ktestrc"

# args:

ktest_args="a:o:p:ISFLvxn:N:"
parse_ktest_arg()
{
    local arg=$1

    case $arg in
	a)
	    ktest_arch=$OPTARG
	    ;;
	o)
	    ktest_out=$OPTARG
	    ;;
	p)
	    ktest_priority=$OPTARG
	    ;;
	I)
	    ktest_interactive=1
	    ;;
	S)
	    ktest_exit_on_success=1
	    ;;
	F)
	    ktest_failfast=true
	    ;;
	L)
	    ktest_loop=1
	    ;;
	v)
	    ktest_verbose=1
	    ;;
	x)
	    set -x
	    ;;
	n)
	    ktest_networking=$OPTARG
	    ;;
	N)
	    ktest_nice=$OPTARG
	    ;;
    esac
}

parse_args_post()
{
    parse_arch "$ktest_arch"

    ktest_out=$(readlink -f "$ktest_out")
    ktest_kernel_binary="$ktest_out/kernel.$ktest_arch"

    if [[ $ktest_interactive = 1 ]]; then
	ktest_kgdb=1
    else
	ktest_crashdump=1
    fi

    if [[ $ktest_nice != 0 ]]; then
	renice  --priority $ktest_nice $$
    fi
}

ktest_usage_opts()
{
    echo "      -a <arch>       architecture"
    echo "      -o <dir>        output directory; defaults to ./ktest-out"
    echo "      -n (user|vde)   Networking type to use"
    echo "      -x              bash debug statements"
    echo "      -h              display this help and exit"
}

ktest_usage_run_opts()
{
    echo "      -p <num>        hint for test duration (higher is longer, default is 0)"
    echo "      -I              interactive mode - don't shut down VM automatically"
    echo "      -S              exit on test success"
    echo "      -F              failfast - stop after first test failure"
    echo "      -L              run all tests in infinite loop until failure"
    echo "      -v              verbose mode"
    echo "      -N <val>        Nice value for kernel build and VM"
}

ktest_usage_cmds()
{
    echo "  boot                Boot a VM without running anything"
    echo "  run <test>          Run a kernel test"
    echo "  ssh                 Login as root"
    echo "  gdb                 Connect to qemu's gdb interface"
    echo "  kgdb                Connect to kgdb"
    echo "  mon                 Connect to qemu monitor"
    echo "  sysrq <key>         Send magic sysrq key via monitor"
}

ktest_usage_post()
{
    echo "For kgdb to be enabled, either -I or -S must be specified"
}

# subcommands:

ktest_run()
{
    if [[ $# = 0 ]]; then
	echo "$0: missing test"
	usage
	exit 1
    fi

    ktest_test=$1
    shift
    ktest_testargs="$@"

    parse_test_deps "$ktest_test"

    start_vm
}

ktest_boot()
{
    ktest_interactive=1
    ktest_kgdb=1

    ktest_run "$ktest_dir/boot.ktest" "$@"
}

ktest_ssh()
{
    local ssh_cmd=(ssh -t -F /dev/null					\
	    -o CheckHostIP=no						\
	    -o StrictHostKeyChecking=no					\
	    -o UserKnownHostsFile=/dev/null				\
	    -o NoHostAuthenticationForLocalhost=yes			\
	    -o ServerAliveInterval=2					\
	    -o ControlMaster=no					\
	)

    if [[ -f $ktest_out/vm/ssh_port ]]; then
	ktest_ssh_port=$(<$ktest_out/vm/ssh_port)
	ssh_cmd+=(-p $ktest_ssh_port)
    elif [[ -d $ktest_out/vm/net ]]; then
	sock=$ktest_out/vm/net
	ip="10.0.2.2"

	checkdep /usr/include/lwipv6.h liblwipv6-dev
	make -C "$ktest_dir/lib" lwip-connect

	ssh_cmd+=(-o ProxyCommand="$ktest_dir/lib/lwip-connect $sock $ip 22")
    else
	echo "No networking found"
	exit 1
    fi

    exec "${ssh_cmd[@]}" root@localhost "$@"
}

ktest_gdb()
{
    if [[ -z $ktest_kernel_binary ]]; then
	echo "Required parameter -k missing: kernel"
	exit 1
    fi

    exec gdb -ex "set remote interrupt-on-connect"			\
	     -ex "target remote | socat UNIX-CONNECT:$ktest_out/vm/gdb -"\
	     "$ktest_kernel_binary/vmlinux"
}

ktest_kgdb()
{
    if [[ -z $ktest_kernel_binary ]]; then
	echo "Required parameter -k missing: kernel"
	exit 1
    fi

    ktest_sysrq g

    exec gdb -ex "set remote interrupt-on-connect"			\
	     -ex "target remote | socat UNIX-CONNECT:$ktest_out/vm/kgdb -"\
	     "$ktest_kernel_binary/vmlinux"
}

ktest_mon()
{
    exec socat UNIX-CONNECT:"$ktest_out/vm/on" STDIO
    exec nc "$ktest_out/vm/mon"
}

ktest_sysrq()
{
    local key=$1

    echo sendkey alt-sysrq-$key | socat - "UNIX-CONNECT:$ktest_out/vm/mon"
}

save_env()
{
    set |grep -v "^PATH=" > "$ktest_out/vm/env_tmp"
    readonly_variables="$(readonly | cut -d= -f1 | cut -d' ' -f3)"
    for variable in ${readonly_variables}
    do
	grep -v "${variable}" "$ktest_out/vm/env_tmp" > "$ktest_out/vm/env"
	cp "$ktest_out/vm/env" "$ktest_out/vm/env_tmp"
    done
    sed -i "s/^ ;$//g" "$ktest_out/vm/env"
    rm -rf "$ktest_out/vm/env_tmp"
}

get_unused_port()
{
    # This probably shouldn't be needed, but I was unable to determine which
    # part of the pipeline was returning an error:
    set +o pipefail
    comm -23 --nocheck-order \
	<(seq 10000 65535) \
	<(ss -tan | awk '{print $4}' | cut -d':' -f2 | grep '[0-9]\{1,5\}' | sort -n | uniq) \
	| shuf | head -n1
}

start_vm()
{
    checkdep_arch

    log_verbose "ktest_arch=$ktest_arch"

    if [[ -z $ktest_kernel_binary ]]; then
	echo "Required parameter -k missing: kernel"
	exit 1
    fi

    if [[ ! -f $ktest_root_image ]]; then
	echo "VM root filesystem not found, use vm_create_image to create one"
	exit 1
    fi

    # upper case vars aren't exported to vm:
    local home=$HOME

    get_tmpdir

    rm -f "$ktest_out/core.*"
    rm -f "$ktest_out/vmcore"
    rm -f "$ktest_out/vm"
    ln -s "$ktest_tmp" "$ktest_out/vm"

    local kernelargs=()
    kernelargs+=(mitigations=off)
    kernelargs+=(console=hvc0)
    kernelargs+=(root=/dev/sda rw log_buf_len=8M)
    kernelargs+=("ktest.dir=$ktest_dir")
    kernelargs+=(ktest.env=$(readlink -f "$ktest_out/vm/env"))
    [[ $ktest_kgdb = 1 ]]	&& kernelargs+=(kgdboc=ttyS0,115200 nokaslr)
    [[ $ktest_verbose = 0 ]]	&& kernelargs+=(quiet systemd.show_status=0 systemd.log-target=journal)
    [[ $ktest_crashdump = 1 ]]	&& kernelargs+=(crashkernel=128M)

    kernelargs+=("${ktest_kernel_append[@]}")

    local qemu_cmd=("$QEMU_BIN" -nodefaults -nographic)
    case $ktest_arch in
	x86|x86_64)
	    qemu_cmd+=(-cpu host -machine type=q35,accel=kvm,nvdimm=on)
	    ;;
	mips)
	    qemu_cmd+=(-cpu 24Kf -machine malta)
	    ktest_cpus=1
	    ;;
	mips64)
	    qemu_cmd+=(-cpu MIPS64R2-generic -machine malta)
	    ;;
    esac

    qemu_cmd+=(								\
	-m		"$ktest_mem,slots=8,maxmem=1T"			\
	-smp		"$ktest_cpus"					\
	-kernel		"$ktest_kernel_binary/vmlinuz"			\
	-append		"$(join_by " " ${kernelargs[@]})"		\
	-device		virtio-serial					\
	-chardev	stdio,id=console				\
	-device		virtconsole,chardev=console			\
	-serial		"unix:$ktest_out/vm/kgdb,server,nowait"		\
	-monitor	"unix:$ktest_out/vm/mon,server,nowait"		\
	-gdb		"unix:$ktest_out/vm/gdb,server,nowait"		\
	-device		virtio-rng-pci					\
	-virtfs		local,path=/,mount_tag=host,security_model=none	\
	-device		$ktest_storage_bus,id=hba			\
    )

    if [[ -f $ktest_kernel_binary/initramfs ]]; then
	qemu_cmd+=(-initrd 	"$ktest_kernel_binary/initramfs")
    fi

    case $ktest_networking in
	user)
	    ktest_ssh_port=$(get_unused_port)
	    echo $ktest_ssh_port > "$ktest_out/vm/ssh_port"

	    qemu_cmd+=( \
		-nic    user,model=virtio,hostfwd=tcp:127.0.0.1:$ktest_ssh_port-:22	\
	    )
	    ;;
	vde)
	    local net="$ktest_out/vm/net"

	    checkdep vde_switch	vde2

	    [[ ! -p "$ktest_out/vm/vde_input" ]] && mkfifo "$ktest_out/vm/vde_input"
	    tail -f "$ktest_out/vm/vde_input" |vde_switch -sock "$net" >& /dev/null &

	    while [[ ! -e "$net" ]]; do
		sleep 0.1
	    done
	    slirpvde --sock "$net" --dhcp=10.0.2.2 --host 10.0.2.1/24 >& /dev/null &
	    qemu_cmd+=( \
		-net		nic,model=virtio,macaddr=de:ad:be:ef:00:00	\
		-net		vde,sock="$ktest_out/vm/net"			\
	    )
	    ;;
	*)
	    echo "Invalid networking type $ktest_networking"
	    exit 1
    esac

    local disknr=0

    qemu_disk()
    {
	qemu_cmd+=(-drive if=none,format=raw,id=disk$disknr,"$1")
	case $ktest_storage_bus in
	    ahci|piix4-ide)
		qemu_cmd+=(-device ide-hd,bus=hba.$disknr,drive=disk$disknr)
		;;
	    *)
		qemu_cmd+=(-device scsi-hd,bus=hba.0,drive=disk$disknr)
		;;
	esac
	disknr=$((disknr + 1))
    }

    qemu_pmem()
    {
	qemu_cmd+=(-object memory-backend-file,id=mem$disknr,share,"$1",align=128M)
	qemu_cmd+=(-device nvdimm,memdev=mem$disknr,id=nv$disknr,label-size=2M)
	disknr=$((disknr + 1))
    }

    qemu_disk file="$ktest_root_image",snapshot=on

    for file in "${ktest_images[@]}"; do
	qemu_disk file="$file",snapshot=on,cache.no-flush=on,cache.direct=$ktest_dio
    done

    for size in "${ktest_scratch_devs[@]}"; do
	local file="$ktest_out/vm/dev-$disknr"

	truncate -s "$size" "$file"

	qemu_disk file="$file",cache=unsafe
    done

    for size in "${ktest_pmem_devs[@]}"; do
	local file="$ktest_out/vm/dev-$disknr"

	fallocate -l "$size" "$file"
	qemu_pmem mem-path="$file",size=$size
    done

    set +o errexit
    save_env
    "${qemu_cmd[@]}"
}