+
+# Locate initrd.
+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 $@"
+ echo "The default path is '@libdir@/initrd'."
+ exit 2
+fi
+
+# Locate kernel if not specified.
+if [ -z "$kernel" ]; then
+ kernel="$(ls -vr /boot/vmlinuz-*.$arch | 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
+ if "$q" --help >/dev/null 2>&1; then
+ qemu="$q"
+ break
+ fi
+ done
+ if [ -z "$qemu" ]; then
+ echo "$0: cannot find a qemu binary on the \$PATH"
+ echo "Choose a qemu binary to test using --qemu=/path/to/qemu"
+ exit 2
+ fi
+fi
+
+# Choose a temporary file for the output.
+test_output="$(mktemp --suff=.out)"
+
+# Generate the parameters for the qemu command.
+declare -a argv
+i=0
+argv[$((i++))]="$qemu"
+argv[$((i++))]="-nographic"
+argv[$((i++))]="-nodefconfig"
+argv[$((i++))]="-nodefaults"
+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 panic=1"
+
+#echo "${argv[@]}"
+
+# Run the command.
+timeout "$timeout" "${argv[@]}"
+r="${PIPESTATUS[0]}"
+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"
+ exit 1
+fi
+
+# Verify that userspace was reached.
+if ! grep -sq "initrd started up OK" "$test_output"; then
+ cat "$test_output"
+ echo "$0: error: test $kernel on $qemu: init process did not start up"
+ rm "$test_output"
+ exit 1
+fi
+
+# Successful.
+rm "$test_output"
+exit 0