Add -m/--machine option.
[qemu-sanity-check.git] / qemu-sanity-check.in
1 #!/bin/bash
2 # -*- shell-script -*-
3 # qemu-sanity-check
4 # Copyright (C) 2013-2020 Red Hat Inc.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with this program; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 prefix="@prefix@"
21 exec_prefix="@exec_prefix@"
22 initrd="@libdir@/qemu-sanity-check/initrd"
23
24 arch="$(uname -m)"
25 canonical_arch="$(uname -m | sed 's/i[456]86/i386/')"
26
27 timeout=10m
28 accel=kvm:tcg
29
30 # Default machine type depends on arch.  You can override this using
31 # -m|--machine option.
32 case "$canonical_arch" in
33     arm*|aarch*) machine=virt ;;
34     # On non-ARM let qemu pick the default.
35     *) machine= ;;
36 esac
37
38 # Handle command line parsing.
39
40 function usage {
41     echo "qemu-sanity-check [options]"
42     echo "Options:"
43     echo "  --help               Display this help"
44     echo "  --accel=[kvm|tcg]    Force KVM or software emulation"
45     echo "  -i|--initrd=initrd   Set location of initramfs"
46     echo "  -k|--kernel=vmlinuz  Set location of kernel"
47     echo "  -m|--machine=machine Set machine type"
48     echo "  -q|--qemu=qemu       Set location of qemu/KVM binary"
49     echo "  -t|--timeout=timeout Set the timeout"
50     echo "  -V|--version         Display version and exit"
51     exit 0
52 }
53
54 TEMP=$(getopt \
55     -o i:k:m:q:t:V \
56     --long help \
57     --long accel: \
58     --long initrd: \
59     --long kernel: \
60     --long machine: \
61     --long qemu: \
62     --long timeout: \
63     --long version \
64     -n 'qemu-sanity-check' -- "$@")
65 if [ $? != 0 ]; then exit 2; fi
66 eval set -- "$TEMP"
67
68 while true; do
69     case "$1" in
70         --help)
71             usage
72             ;;
73         --accel)
74             accel="$2"
75             shift 2
76             ;;
77         -i|--initrd)
78             initrd="$2"
79             shift 2
80             ;;
81         -k|--kernel)
82             kernel="$2"
83             shift 2
84             ;;
85         -m|--machine)
86             machine="$2"
87             shift 2
88             ;;
89         -q|--qemu)
90             qemu="$2"
91             shift 2
92             ;;
93         -t|--timeout)
94             timeout="$2"
95             shift 2
96             ;;
97         -V|--version)
98             echo "@PACKAGE_NAME@ @PACKAGE_VERSION@"
99             exit 0
100             ;;
101         --)
102             shift
103             break
104             ;;
105         *)
106             echo "$0: internal error parsing options: $1"
107             exit 2
108             ;;
109     esac
110 done
111
112 # Locate initrd.
113 if [ ! -r "$initrd" ]; then
114     echo "$0: cannot find 'initrd', try using --initrd=/path/to/initrd"
115     echo "If you are running qemu-sanity-check without installing, then do:"
116     echo "    $0 --initrd=./initrd $@"
117     echo "The default path is '@libdir@/initrd'."
118     exit 2
119 fi
120
121 # Locate kernel if not specified.
122 if [ -z "$kernel" ]; then
123     kernel="$(ls -1dvr /boot/vmlinuz-*.$arch* 2>/dev/null | grep -v xen | head -1)"
124     if [ -z "$kernel" ]; then
125         echo "$0: cannot find a Linux kernel in /boot"
126         echo "Choose a kernel to test using --kernel=/path/to/vmlinuz"
127         exit 2
128     fi
129 fi
130 if [ ! -r "$kernel" ]; then
131     echo "$0: kernel $kernel is not readable"
132     exit 2
133 fi
134
135 # Locate qemu if not specified.
136 if [ -z "$qemu" ]; then
137     for q in @QEMU_LIST@; do
138         if "$q" --help >/dev/null 2>&1; then
139             qemu="$q"
140             break
141         fi
142     done
143     if [ -z "$qemu" ]; then
144         echo "$0: cannot find a qemu binary on the \$PATH"
145         echo "Choose a qemu binary to test using --qemu=/path/to/qemu"
146         exit 2
147     fi
148 fi
149
150 # Choose a temporary file for the output.
151 test_output="$(mktemp --suff=.out)"
152
153 # Generate the parameters for the qemu command.
154 declare -a argv
155 i=0
156 argv[$((i++))]="$qemu"
157 argv[$((i++))]="-display"
158 argv[$((i++))]="none"
159 argv[$((i++))]="-no-user-config"
160 argv[$((i++))]="-nodefaults"
161 argv[$((i++))]="-machine"
162 argv[$((i++))]="$machine${machine:+,}accel=$accel"
163 argv[$((i++))]="-no-reboot"
164 argv[$((i++))]="-serial"
165 argv[$((i++))]="file:$test_output"
166 argv[$((i++))]="-kernel"
167 argv[$((i++))]="$kernel"
168 argv[$((i++))]="-initrd"
169 argv[$((i++))]="$initrd"
170 argv[$((i++))]="-append"
171 argv[$((i++))]="console=ttyS0 oops=panic panic=-1"
172
173 #echo "${argv[@]}"
174
175 # Run the command.
176 timeout "$timeout" "${argv[@]}"
177 r="${PIPESTATUS[0]}"
178 if [ $r -eq 124 ]; then
179     cat "$test_output"
180     echo "$0: error: test $kernel on $qemu: timed out"
181     rm "$test_output"
182     exit 1
183 elif [ $r -ne 0 ]; then
184     cat "$test_output"
185     echo "$0: error: test $kernel on $qemu: failed"
186     rm "$test_output"
187     exit 1
188 fi
189
190 # Verify that userspace was reached.
191 if ! grep -sq "initrd started up OK" "$test_output"; then
192     cat "$test_output"
193     echo "$0: error: test $kernel on $qemu: init process did not start up"
194     rm "$test_output"
195     exit 1
196 fi
197
198 # Successful.
199 rm "$test_output"
200 exit 0