Add --cpu 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 verbose=no
28 timeout=10m
29 accel=kvm:tcg
30 cpu=
31 machine=
32
33 # Both libguestfs and virt-manager choose cpu=host when we think that
34 # KVM is available, and default otherwise.  Although testing for KVM
35 # is hairy, I found that on aarch64 it can fail unless we choose
36 # cpu=host for KVM.
37 if test -r /dev/kvm; then
38     cpu=host
39 fi
40
41 # Default machine and CPU type depends on arch.  You can override this
42 # using -m|--machine and --cpu options.
43 case "$canonical_arch" in
44     arm*)
45         machine=virt ;;
46     aarch*)
47         if [ "$cpu" = "" ]; then cpu=cortex-a57; fi
48         machine=virt ;;
49 esac
50
51 # Handle command line parsing.
52
53 function usage {
54     echo "qemu-sanity-check [options]"
55     echo "Options:"
56     echo "  --help               Display this help"
57     echo "  --accel=[kvm|tcg]    Force KVM or software emulation"
58     echo "  --cpu=cpu            Set CPU"
59     echo "  -i|--initrd=initrd   Set location of initramfs"
60     echo "  -k|--kernel=vmlinuz  Set location of kernel"
61     echo "  -m|--machine=machine Set machine type"
62     echo "  -q|--qemu=qemu       Set location of qemu/KVM binary"
63     echo "  -t|--timeout=timeout Set the timeout"
64     echo "  -v|--verbose         Verbose output"
65     echo "  -V|--version         Display version and exit"
66     exit 0
67 }
68
69 TEMP=$(getopt \
70     -o i:k:m:q:t:vV \
71     --long help \
72     --long accel: \
73     --long cpu: \
74     --long initrd: \
75     --long kernel: \
76     --long machine: \
77     --long qemu: \
78     --long timeout: \
79     --long verbose \
80     --long version \
81     -n 'qemu-sanity-check' -- "$@")
82 if [ $? != 0 ]; then exit 2; fi
83 eval set -- "$TEMP"
84
85 while true; do
86     case "$1" in
87         --help)
88             usage
89             ;;
90         --accel)
91             accel="$2"
92             shift 2
93             ;;
94         --cpu)
95             cpu="$2"
96             shift 2
97             ;;
98         -i|--initrd)
99             initrd="$2"
100             shift 2
101             ;;
102         -k|--kernel)
103             kernel="$2"
104             shift 2
105             ;;
106         -m|--machine)
107             machine="$2"
108             shift 2
109             ;;
110         -q|--qemu)
111             qemu="$2"
112             shift 2
113             ;;
114         -t|--timeout)
115             timeout="$2"
116             shift 2
117             ;;
118         -v|--verbose)
119             verbose=yes
120             shift
121             ;;
122         -V|--version)
123             echo "@PACKAGE_NAME@ @PACKAGE_VERSION@"
124             exit 0
125             ;;
126         --)
127             shift
128             break
129             ;;
130         *)
131             echo "$0: internal error parsing options: $1"
132             exit 2
133             ;;
134     esac
135 done
136
137 # Locate initrd.
138 if [ ! -r "$initrd" ]; then
139     echo "$0: cannot find 'initrd', try using --initrd=/path/to/initrd"
140     echo "If you are running qemu-sanity-check without installing, then do:"
141     echo "    $0 --initrd=./initrd $@"
142     echo "The default path is '@libdir@/initrd'."
143     exit 2
144 fi
145
146 # Locate kernel if not specified.
147 if [ -z "$kernel" ]; then
148     kernel="$(ls -1dvr /boot/vmlinuz-*.$arch* 2>/dev/null | grep -v xen | head -1)"
149     if [ -z "$kernel" ]; then
150         echo "$0: cannot find a Linux kernel in /boot"
151         echo "Choose a kernel to test using --kernel=/path/to/vmlinuz"
152         exit 2
153     fi
154 fi
155 if [ ! -r "$kernel" ]; then
156     echo "$0: kernel $kernel is not readable"
157     exit 2
158 fi
159
160 # Locate qemu if not specified.
161 if [ -z "$qemu" ]; then
162     for q in @QEMU_LIST@; do
163         if "$q" --help >/dev/null 2>&1; then
164             qemu="$q"
165             break
166         fi
167     done
168     if [ -z "$qemu" ]; then
169         echo "$0: cannot find a qemu binary on the \$PATH"
170         echo "Choose a qemu binary to test using --qemu=/path/to/qemu"
171         exit 2
172     fi
173 fi
174
175 # Choose a temporary file for the output.
176 test_output="$(mktemp --suff=.out)"
177
178 # Generate the parameters for the qemu command.
179 declare -a argv
180 i=0
181 argv[$((i++))]="$qemu"
182 argv[$((i++))]="-display"
183 argv[$((i++))]="none"
184 argv[$((i++))]="-no-user-config"
185 argv[$((i++))]="-nodefaults"
186 argv[$((i++))]="-machine"
187 argv[$((i++))]="$machine${machine:+,}accel=$accel"
188 if [ "$cpu" != "" ]; then
189     argv[$((i++))]="-cpu"
190     argv[$((i++))]="$cpu"
191 fi
192 argv[$((i++))]="-no-reboot"
193 argv[$((i++))]="-serial"
194 argv[$((i++))]="file:$test_output"
195 argv[$((i++))]="-kernel"
196 argv[$((i++))]="$kernel"
197 argv[$((i++))]="-initrd"
198 argv[$((i++))]="$initrd"
199 argv[$((i++))]="-append"
200 argv[$((i++))]="console=ttyS0 oops=panic panic=-1"
201
202 if [ "$verbose" = "yes" ]; then
203     echo "${argv[@]}"
204 fi
205
206 # Run the command.
207 timeout "$timeout" "${argv[@]}"
208 r="${PIPESTATUS[0]}"
209 if [ $r -eq 124 ]; then
210     cat "$test_output"
211     echo "$0: error: test $kernel on $qemu: timed out"
212     rm "$test_output"
213     exit 1
214 elif [ $r -ne 0 ]; then
215     cat "$test_output"
216     echo "$0: error: test $kernel on $qemu: failed"
217     rm "$test_output"
218     exit 1
219 fi
220
221 # Verify that userspace was reached.
222 if ! grep -sq "initrd started up OK" "$test_output"; then
223     cat "$test_output"
224     echo "$0: error: test $kernel on $qemu: init process did not start up"
225     rm "$test_output"
226     exit 1
227 fi
228
229 # Successful.
230 rm "$test_output"
231 exit 0