Set RAM to something larger than qemu default.
[qemu-sanity-check.git] / qemu-sanity-check.in
index da4f59a..5066ebc 100644 (file)
@@ -1,7 +1,7 @@
 #!/bin/bash
 # -*- shell-script -*-
 # qemu-sanity-check
-# Copyright (C) 2013 Red Hat Inc.
+# Copyright (C) 2013-2020 Red Hat Inc.
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
 prefix="@prefix@"
 exec_prefix="@exec_prefix@"
-initrd="@libdir@/initrd"
+initrd="@libdir@/qemu-sanity-check/initrd"
 
 arch="$(uname -m)"
 canonical_arch="$(uname -m | sed 's/i[456]86/i386/')"
 
-#timeout=10m
+verbose=no
+timeout=10m
+accel=kvm:tcg
+cpu=
+memory=768
+machine=
+
+# Both libguestfs and virt-manager choose cpu=host when we think that
+# KVM is available, and default otherwise.  Although testing for KVM
+# is hairy, I found that on aarch64 it can fail unless we choose
+# cpu=host for KVM.
+if test -r /dev/kvm; then
+    cpu=host
+fi
+
+# Default machine and CPU type depends on arch.  You can override this
+# using -m|--machine and --cpu options.
+case "$canonical_arch" in
+    arm*)
+       machine=virt ;;
+    aarch*)
+       if [ "$cpu" = "" ]; then cpu=cortex-a57; fi
+       machine=virt ;;
+esac
 
 # Handle command line parsing.
 
@@ -32,20 +55,29 @@ function usage {
     echo "qemu-sanity-check [options]"
     echo "Options:"
     echo "  --help               Display this help"
+    echo "  --accel=[kvm|tcg]    Force KVM or software emulation"
+    echo "  --cpu=cpu            Set CPU"
     echo "  -i|--initrd=initrd   Set location of initramfs"
     echo "  -k|--kernel=vmlinuz  Set location of kernel"
+    echo "  -m|--machine=machine Set machine type"
     echo "  -q|--qemu=qemu       Set location of qemu/KVM binary"
-#    echo "  -t|--timeout=timeout Set the timeout"
+    echo "  -t|--timeout=timeout Set the timeout"
+    echo "  -v|--verbose         Verbose output"
     echo "  -V|--version         Display version and exit"
     exit 0
 }
 
 TEMP=$(getopt \
-    -o i:k:q:V \
+    -o i:k:m:q:t:vV \
     --long help \
+    --long accel: \
+    --long cpu: \
     --long initrd: \
     --long kernel: \
+    --long machine: \
     --long qemu: \
+    --long timeout: \
+    --long verbose \
     --long version \
     -n 'qemu-sanity-check' -- "$@")
 if [ $? != 0 ]; then exit 2; fi
@@ -56,6 +88,14 @@ while true; do
         --help)
             usage
             ;;
+        --accel)
+            accel="$2"
+            shift 2
+            ;;
+       --cpu)
+           cpu="$2"
+           shift 2
+           ;;
         -i|--initrd)
             initrd="$2"
             shift 2
@@ -64,14 +104,22 @@ while true; do
             kernel="$2"
             shift 2
             ;;
+        -m|--machine)
+            machine="$2"
+            shift 2
+            ;;
         -q|--qemu)
             qemu="$2"
             shift 2
             ;;
-#        -t|--timeout)
-#            timeout="$2"
-#            shift 2
-#            ;;
+        -t|--timeout)
+            timeout="$2"
+            shift 2
+            ;;
+        -v|--verbose)
+            verbose=yes
+            shift
+            ;;
         -V|--version)
             echo "@PACKAGE_NAME@ @PACKAGE_VERSION@"
             exit 0
@@ -88,7 +136,7 @@ while true; do
 done
 
 # Locate initrd.
-if [ ! -f "$initrd" ]; then
+if [ ! -r "$initrd" ]; then
     echo "$0: cannot find 'initrd', try using --initrd=/path/to/initrd"
     echo "If you are running qemu-sanity-check without installing, then do:"
     echo "    $0 --initrd=./initrd $@"
@@ -98,17 +146,21 @@ fi
 
 # Locate kernel if not specified.
 if [ -z "$kernel" ]; then
-    kernel="$(ls -vr /boot/vmlinuz-*.$arch | head -1)"
+    kernel="$(ls -1dvr /boot/vmlinuz-*.$arch* 2>/dev/null | grep -v xen | head -1)"
     if [ -z "$kernel" ]; then
         echo "$0: cannot find a Linux kernel in /boot"
         echo "Choose a kernel to test using --kernel=/path/to/vmlinuz"
         exit 2
     fi
 fi
+if [ ! -r "$kernel" ]; then
+    echo "$0: kernel $kernel is not readable"
+    exit 2
+fi
 
 # Locate qemu if not specified.
 if [ -z "$qemu" ]; then
-    for q in qemu-kvm qemu-system-$canonical_arch qemu kvm; do
+    for q in @QEMU_LIST@; do
         if "$q" --help >/dev/null 2>&1; then
             qemu="$q"
             break
@@ -124,24 +176,45 @@ fi
 # Choose a temporary file for the output.
 test_output="$(mktemp --suff=.out)"
 
-# Run the command
-set -x
-#timeout "$timeout"
-"$qemu" \
-    -nographic -nodefconfig -nodefaults \
-    -no-reboot \
-    -serial stdio \
-    -kernel "$kernel" \
-    -initrd "$initrd" \
-    -append "console=ttyS0 panic=1" 2>&1 | tee "$test_output"
+# Generate the parameters for the qemu command.
+declare -a argv
+i=0
+argv[$((i++))]="$qemu"
+argv[$((i++))]="-display"
+argv[$((i++))]="none"
+argv[$((i++))]="-no-user-config"
+argv[$((i++))]="-nodefaults"
+argv[$((i++))]="-machine"
+argv[$((i++))]="$machine${machine:+,}accel=$accel"
+if [ "$cpu" != "" ]; then
+    argv[$((i++))]="-cpu"
+    argv[$((i++))]="$cpu"
+fi
+argv[$((i++))]="-m"
+argv[$((i++))]="$memory"
+argv[$((i++))]="-no-reboot"
+argv[$((i++))]="-serial"
+argv[$((i++))]="file:$test_output"
+argv[$((i++))]="-kernel"
+argv[$((i++))]="$kernel"
+argv[$((i++))]="-initrd"
+argv[$((i++))]="$initrd"
+argv[$((i++))]="-append"
+argv[$((i++))]="console=ttyS0 oops=panic panic=-1"
+
+if [ "$verbose" = "yes" ]; then
+    echo "${argv[@]}"
+fi
+
+# Run the command.
+timeout "$timeout" "${argv[@]}"
 r="${PIPESTATUS[0]}"
-set +x
-#if [ $r -eq 124 ]; then
-#    cat "$test_output"
-#    echo "$0: error: test $kernel on $qemu: timed out"
-#    rm "$test_output"
-#    exit 1
-if [ $r -ne 0 ]; then
+if [ $r -eq 124 ]; then
+    cat "$test_output"
+    echo "$0: error: test $kernel on $qemu: timed out"
+    rm "$test_output"
+    exit 1
+elif [ $r -ne 0 ]; then
     cat "$test_output"
     echo "$0: error: test $kernel on $qemu: failed"
     rm "$test_output"