2 # SPDX-License-Identifier: GPL-2.0-or-later
4 # Run previously built VM image in qemu and check if it boots.
5 # Requires virtiofs support in VM.
9 if [ "$#" -ne 2 ]; then
10 echo "$0: Invalid arguments" >&2
11 echo "Expect: $0 VM_IMAGE VM_HOSTNAME" >&2
20 TEST_TMPDIR=$(mktemp -d)
21 TESTS_RESULTSDIR="$PWD/tests/results"
22 echo "Working in $TEST_TMPDIR, writing results to $TESTS_RESULTSDIR"
23 mkdir -p "$TESTS_RESULTSDIR"
26 if [ -n "${QEMU_PID:-}" ] ; then
27 # shellcheck disable=SC2009
28 ps --pid="${QEMU_PID}" -o pid= | grep -q '.' && kill "${QEMU_PID:-}"
31 rm -rf "${TEST_TMPDIR}"
33 [ -n "${1:-}" ] && EXIT_CODE="$1" || EXIT_CODE=1
36 trap bailout 1 2 3 6 14 15
39 cp ./tests/goss "$TEST_TMPDIR"/
40 cp ./tests/goss.yaml "$TEST_TMPDIR"/
41 cat <<EOT > "$TEST_TMPDIR"/testrunner
43 # Do not set -eu, we want to continue even if individual commands fail.
45 echo "INSIDE_VM $0 running"
48 # Collect information from VM first
49 lsb_release -a > results/lsb_release.txt
50 uname -a > results/uname-a.txt
51 systemctl list-units > results/systemctl_list-units.txt
52 systemctl status > results/systemctl_status.txt
53 fdisk -l > results/fdisk-l.txt
54 hostname -f > results/hostname-f.txt 2>&1
55 journalctl -b > results/journalctl-b.txt
56 dpkg -l > results/dpkg-l.txt
59 ./goss --gossfile goss.yaml validate --format tap > results/goss.tap
60 # Detection of testrunner success hinges on goss.exitcode file.
61 echo \$? > results/goss.exitcode
63 echo "INSIDE_VM $0 finished"
65 chmod a+rx "$TEST_TMPDIR"/testrunner
70 qemu-system-x86_64 -hda "${VM_IMAGE}" -m 2048 \
71 -display none -vnc :0 \
72 -virtfs local,path="$TEST_TMPDIR",mount_tag="$MOUNT_TAG",security_model=none,id=host0 \
73 -serial pty &>qemu.log &
78 while [ "$timeout" -gt 0 ] ; do
80 if grep -q 'char device redirected to ' qemu.log ; then
85 echo "No serial console from Qemu found yet [$timeout retries left]"
90 if [ "$success" = "1" ] ; then
91 serial_port=$(awk '/char device redirected/ {print $5}' qemu.log)
93 echo "Error: Failed to identify serial console port." >&2
100 while [ "$timeout" -gt 0 ] ; do
102 if [ -c "$serial_port" ] ; then
107 echo "No block device for serial console found yet [$timeout retries left]"
112 if [ "$success" = "0" ] ; then
113 echo "Error: can't access serial console block device." >&2
117 "$TEST_PWD"/tests/serial-console-connection \
119 --port "$serial_port" \
120 --hostname "$VM_HOSTNAME" \
122 "mount -t 9p -o trans=virtio,version=9p2000.L,rw $MOUNT_TAG /mnt && cd /mnt && ./testrunner"
124 if [ ! -d results ] || [ ! -f ./results/goss.tap ] || [ ! -f ./results/goss.exitcode ]; then
125 echo "Running tests inside VM failed for unknown reason" >&2
128 RC=$(cat results/goss.exitcode)
129 echo "goss exitcode: $RC"
134 echo "Finished serial console connection [timeout=${timeout}]."
136 mv results/* "$TESTS_RESULTSDIR/"