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