xref: /kvm-unit-tests/scripts/vmm.bash (revision 6eb072c22598410818ddf9d38afc98adc9a97397)
1# The following parameters are enabled by default when running a test with
2# kvmtool:
3# --nodefaults: suppress VM configuration that cannot be disabled (like
4#               modifying the supplied kernel command line). Otherwise tests
5#               that use the command line will fail without this parameter.
6# --network mode=none: do not create a network device. kvmtool tries to help the
7#               user by automatically create one, and then prints a warning
8#               when the VM terminates if the device hasn't been initialized.
9# --loglevel=warning: reduce verbosity
10: "${KVMTOOL_DEFAULT_OPTS:="--nodefaults --network mode=none --loglevel=warning"}"
11
12##############################################################################
13# qemu_fixup_return_code translates the ambiguous exit status in Table1 to that
14# in Table2.  Table3 simply documents the complete status table.
15#
16# Table1: Before fixup
17# --------------------
18# 0      - Unexpected exit from QEMU (possible signal), or the unittest did
19#          not use debug-exit
20# 1      - most likely unittest succeeded, or QEMU failed
21#
22# Table2: After fixup
23# -------------------
24# 0      - Everything succeeded
25# 1      - most likely QEMU failed
26#
27# Table3: Complete table
28# ----------------------
29# 0      - SUCCESS
30# 1      - most likely QEMU failed
31# 2      - most likely a run script failed
32# 3      - most likely the unittest failed
33# 124    - most likely the unittest timed out
34# 127    - most likely the unittest called abort()
35# 1..127 - FAILURE (could be QEMU, a run script, or the unittest)
36# >= 128 - Signal (signum = status - 128)
37##############################################################################
38function qemu_fixup_return_code()
39{
40	local ret=$1
41	# Remove $ret from the list of arguments
42	shift 1
43	local errors=$*
44	local sig
45
46	[ $ret -eq 134 ] && echo "QEMU Aborted" >&2
47
48	if [ "$errors" ]; then
49		sig=$(grep 'terminating on signal' <<<"$errors")
50		if [ "$sig" ]; then
51			# This is too complex for ${var/search/replace}
52			# shellcheck disable=SC2001
53			sig=$(sed 's/.*terminating on signal \([0-9][0-9]*\).*/\1/' <<<"$sig")
54		fi
55	fi
56
57	if [ $ret -eq 0 ]; then
58		# Some signals result in a zero return status, but the
59		# error log tells the truth.
60		if [ "$sig" ]; then
61			((ret=sig+128))
62		else
63			# Exiting with zero (non-debugexit) is an error
64			ret=1
65		fi
66	elif [ $ret -eq 1 ]; then
67		# Even when ret==1 (unittest success) if we also got stderr
68		# logs, then we assume a QEMU failure. Otherwise we translate
69		# status of 1 to 0 (SUCCESS)
70	        if [ "$errors" ]; then
71			if ! grep -qvi warning <<<"$errors" ; then
72				ret=0
73			fi
74		else
75			ret=0
76		fi
77	fi
78
79	echo $ret
80}
81
82function kvmtool_fixup_return_code()
83{
84	local ret=$1
85
86	# Force run_test_status() to interpret the STATUS line.
87	if [ $ret -eq 0 ]; then
88		ret=1
89	fi
90
91	echo $ret
92}
93
94declare -A vmm_optname=(
95	[qemu,args]='-append'
96	[qemu,default_opts]=''
97	[qemu,fixup_return_code]=qemu_fixup_return_code
98	[qemu,initrd]='-initrd'
99	[qemu,nr_cpus]='-smp'
100
101	[kvmtool,args]='--params'
102	[kvmtool,default_opts]="$KVMTOOL_DEFAULT_OPTS"
103	[kvmtool,fixup_return_code]=kvmtool_fixup_return_code
104	[kvmtool,initrd]='--initrd'
105	[kvmtool,nr_cpus]='--cpus'
106)
107
108function vmm_optname_args()
109{
110	echo ${vmm_optname[$(vmm_get_target),args]}
111}
112
113function vmm_default_opts()
114{
115	echo ${vmm_optname[$(vmm_get_target),default_opts]}
116}
117
118function vmm_fixup_return_code()
119{
120	${vmm_optname[$(vmm_get_target),fixup_return_code]} "$@"
121}
122
123function vmm_optname_initrd()
124{
125	echo ${vmm_optname[$(vmm_get_target),initrd]}
126}
127
128function vmm_optname_nr_cpus()
129{
130	echo ${vmm_optname[$(vmm_get_target),nr_cpus]}
131}
132
133function vmm_get_target()
134{
135	if [[ -z "$TARGET" ]]; then
136		echo "qemu"
137	else
138		echo "$TARGET"
139	fi
140}
141
142function vmm_check_supported()
143{
144	# We're not interested in the return code for vmm_get_target().
145	# shellcheck disable=SC2155
146	local target=$(vmm_get_target)
147
148	case "$target" in
149	qemu)
150		return 0
151		;;
152	*)
153		echo "$0 does not support target '$target'"
154		exit 2
155		;;
156	esac
157}
158
159function vmm_unittest_params_name()
160{
161	# shellcheck disable=SC2155
162	local target=$(vmm_get_target)
163
164	case "$target" in
165	qemu)
166		echo "extra_params|qemu_params"
167		;;
168	kvmtool)
169		echo "kvmtool_params"
170		;;
171	*)
172		echo "$0 does not support '$target'"
173		exit 2
174		;;
175	esac
176}
177