Initial version of qemu-sanity-check.
[qemu-sanity-check.git] / qemu-sanity-check.in
1 #!/bin/bash
2 # -*- shell-script -*-
3 # qemu-sanity-check
4 # Copyright (C) 2013 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
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20 prefix="@prefix@"
21 exec_prefix="@exec_prefix@"
22 initrd="@libdir@/initrd"
23
24 arch="$(uname -m)"
25 canonical_arch="$(uname -m | sed 's/i[456]86/i386/')"
26
27 #timeout=10m
28
29 # Handle command line parsing.
30
31 function usage {
32     echo "qemu-sanity-check [options]"
33     echo "Options:"
34     echo "  --help               Display this help"
35     echo "  -i|--initrd=initrd   Set location of initramfs"
36     echo "  -k|--kernel=vmlinuz  Set location of kernel"
37     echo "  -q|--qemu=qemu       Set location of qemu/KVM binary"
38 #    echo "  -t|--timeout=timeout Set the timeout"
39     echo "  -V|--version         Display version and exit"
40     exit 0
41 }
42
43 TEMP=$(getopt \
44     -o i:k:q:V \
45     --long help \
46     --long initrd: \
47     --long kernel: \
48     --long qemu: \
49     --long version \
50     -n 'qemu-sanity-check' -- "$@")
51 if [ $? != 0 ]; then exit 2; fi
52 eval set -- "$TEMP"
53
54 while true; do
55     case "$1" in
56         --help)
57             usage
58             ;;
59         -i|--initrd)
60             initrd="$2"
61             shift 2
62             ;;
63         -k|--kernel)
64             kernel="$2"
65             shift 2
66             ;;
67         -q|--qemu)
68             qemu="$2"
69             shift 2
70             ;;
71 #        -t|--timeout)
72 #            timeout="$2"
73 #            shift 2
74 #            ;;
75         -V|--version)
76             echo "@PACKAGE_NAME@ @PACKAGE_VERSION@"
77             exit 0
78             ;;
79         --)
80             shift
81             break
82             ;;
83         *)
84             echo "$0: internal error parsing options: $1"
85             exit 2
86             ;;
87     esac
88 done
89
90 # Locate initrd.
91 if [ ! -f "$initrd" ]; then
92     echo "$0: cannot find 'initrd', try using --initrd=/path/to/initrd"
93     echo "If you are running qemu-sanity-check without installing, then do:"
94     echo "    $0 --initrd=./initrd $@"
95     echo "The default path is '@libdir@/initrd'."
96     exit 2
97 fi
98
99 # Locate kernel if not specified.
100 if [ -z "$kernel" ]; then
101     kernel="$(ls -vr /boot/vmlinuz-*.$arch | head -1)"
102     if [ -z "$kernel" ]; then
103         echo "$0: cannot find a Linux kernel in /boot"
104         echo "Choose a kernel to test using --kernel=/path/to/vmlinuz"
105         exit 2
106     fi
107 fi
108
109 # Locate qemu if not specified.
110 if [ -z "$qemu" ]; then
111     for q in qemu-kvm qemu-system-$canonical_arch qemu kvm; do
112         if "$q" --help >/dev/null 2>&1; then
113             qemu="$q"
114             break
115         fi
116     done
117     if [ -z "$qemu" ]; then
118         echo "$0: cannot find a qemu binary on the \$PATH"
119         echo "Choose a qemu binary to test using --qemu=/path/to/qemu"
120         exit 2
121     fi
122 fi
123
124 # Choose a temporary file for the output.
125 test_output="$(mktemp --suff=.out)"
126
127 # Run the command
128 set -x
129 #timeout "$timeout"
130 "$qemu" \
131     -nographic -nodefconfig -nodefaults \
132     -no-reboot \
133     -serial stdio \
134     -kernel "$kernel" \
135     -initrd "$initrd" \
136     -append "console=ttyS0 panic=1" 2>&1 | tee "$test_output"
137 r="${PIPESTATUS[0]}"
138 set +x
139 #if [ $r -eq 124 ]; then
140 #    cat "$test_output"
141 #    echo "$0: error: test $kernel on $qemu: timed out"
142 #    rm "$test_output"
143 #    exit 1
144 if [ $r -ne 0 ]; then
145     cat "$test_output"
146     echo "$0: error: test $kernel on $qemu: failed"
147     rm "$test_output"
148     exit 1
149 fi
150
151 # Verify that userspace was reached.
152 if ! grep -sq "initrd started up OK" "$test_output"; then
153     cat "$test_output"
154     echo "$0: error: test $kernel on $qemu: init process did not start up"
155     rm "$test_output"
156     exit 1
157 fi
158
159 # Successful.
160 rm "$test_output"
161 exit 0