vmtest.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. #
  4. # Copyright (c) 2025 Red Hat
  5. # Copyright (c) 2025 Meta Platforms, Inc. and affiliates
  6. #
  7. # Dependencies:
  8. # * virtme-ng
  9. # * busybox-static (used by virtme-ng)
  10. # * qemu (used by virtme-ng)
  11. readonly SCRIPT_DIR="$(cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"
  12. readonly KERNEL_CHECKOUT=$(realpath "${SCRIPT_DIR}"/../../../../)
  13. source "${SCRIPT_DIR}"/../kselftest/ktap_helpers.sh
  14. readonly HID_BPF_TEST="${SCRIPT_DIR}"/hid_bpf
  15. readonly HIDRAW_TEST="${SCRIPT_DIR}"/hidraw
  16. readonly HID_BPF_PROGS="${KERNEL_CHECKOUT}/drivers/hid/bpf/progs"
  17. readonly SSH_GUEST_PORT=22
  18. readonly WAIT_PERIOD=3
  19. readonly WAIT_PERIOD_MAX=60
  20. readonly WAIT_TOTAL=$(( WAIT_PERIOD * WAIT_PERIOD_MAX ))
  21. readonly QEMU_PIDFILE=$(mktemp /tmp/qemu_hid_vmtest_XXXX.pid)
  22. readonly QEMU_OPTS="\
  23. --pidfile ${QEMU_PIDFILE} \
  24. "
  25. readonly KERNEL_CMDLINE=""
  26. readonly LOG=$(mktemp /tmp/hid_vmtest_XXXX.log)
  27. readonly TEST_NAMES=(vm_hid_bpf vm_hidraw vm_pytest)
  28. readonly TEST_DESCS=(
  29. "Run hid_bpf tests in the VM."
  30. "Run hidraw tests in the VM."
  31. "Run the hid-tools test-suite in the VM."
  32. )
  33. VERBOSE=0
  34. SHELL_MODE=0
  35. BUILD_HOST=""
  36. BUILD_HOST_PODMAN_CONTAINER_NAME=""
  37. usage() {
  38. local name
  39. local desc
  40. local i
  41. echo
  42. echo "$0 [OPTIONS] [TEST]... [-- tests-args]"
  43. echo "If no TEST argument is given, all tests will be run."
  44. echo
  45. echo "Options"
  46. echo " -b: build the kernel from the current source tree and use it for guest VMs"
  47. echo " -H: hostname for remote build host (used with -b)"
  48. echo " -p: podman container name for remote build host (used with -b)"
  49. echo " Example: -H beefyserver -p vng"
  50. echo " -q: set the path to or name of qemu binary"
  51. echo " -s: start a shell in the VM instead of running tests"
  52. echo " -v: more verbose output (can be repeated multiple times)"
  53. echo
  54. echo "Available tests"
  55. for ((i = 0; i < ${#TEST_NAMES[@]}; i++)); do
  56. name=${TEST_NAMES[${i}]}
  57. desc=${TEST_DESCS[${i}]}
  58. printf "\t%-35s%-35s\n" "${name}" "${desc}"
  59. done
  60. echo
  61. exit 1
  62. }
  63. die() {
  64. echo "$*" >&2
  65. exit "${KSFT_FAIL}"
  66. }
  67. vm_ssh() {
  68. # vng --ssh-client keeps shouting "Warning: Permanently added 'virtme-ng%22'
  69. # (ED25519) to the list of known hosts.",
  70. # So replace the command with what's actually called and add the "-q" option
  71. stdbuf -oL ssh -q \
  72. -F ${HOME}/.cache/virtme-ng/.ssh/virtme-ng-ssh.conf \
  73. -l root virtme-ng%${SSH_GUEST_PORT} \
  74. "$@"
  75. return $?
  76. }
  77. cleanup() {
  78. if [[ -s "${QEMU_PIDFILE}" ]]; then
  79. pkill -SIGTERM -F "${QEMU_PIDFILE}" > /dev/null 2>&1
  80. fi
  81. # If failure occurred during or before qemu start up, then we need
  82. # to clean this up ourselves.
  83. if [[ -e "${QEMU_PIDFILE}" ]]; then
  84. rm "${QEMU_PIDFILE}"
  85. fi
  86. }
  87. check_args() {
  88. local found
  89. for arg in "$@"; do
  90. found=0
  91. for name in "${TEST_NAMES[@]}"; do
  92. if [[ "${name}" = "${arg}" ]]; then
  93. found=1
  94. break
  95. fi
  96. done
  97. if [[ "${found}" -eq 0 ]]; then
  98. echo "${arg} is not an available test" >&2
  99. usage
  100. fi
  101. done
  102. for arg in "$@"; do
  103. if ! command -v > /dev/null "test_${arg}"; then
  104. echo "Test ${arg} not found" >&2
  105. usage
  106. fi
  107. done
  108. }
  109. check_deps() {
  110. for dep in vng ${QEMU} busybox pkill ssh pytest; do
  111. if [[ ! -x $(command -v "${dep}") ]]; then
  112. echo -e "skip: dependency ${dep} not found!\n"
  113. exit "${KSFT_SKIP}"
  114. fi
  115. done
  116. if [[ ! -x $(command -v "${HID_BPF_TEST}") ]]; then
  117. printf "skip: %s not found!" "${HID_BPF_TEST}"
  118. printf " Please build the kselftest hid_bpf target.\n"
  119. exit "${KSFT_SKIP}"
  120. fi
  121. if [[ ! -x $(command -v "${HIDRAW_TEST}") ]]; then
  122. printf "skip: %s not found!" "${HIDRAW_TEST}"
  123. printf " Please build the kselftest hidraw target.\n"
  124. exit "${KSFT_SKIP}"
  125. fi
  126. }
  127. check_vng() {
  128. local tested_versions
  129. local version
  130. local ok
  131. tested_versions=("1.36" "1.37")
  132. version="$(vng --version)"
  133. ok=0
  134. for tv in "${tested_versions[@]}"; do
  135. if [[ "${version}" == *"${tv}"* ]]; then
  136. ok=1
  137. break
  138. fi
  139. done
  140. if [[ ! "${ok}" -eq 1 ]]; then
  141. printf "warning: vng version '%s' has not been tested and may " "${version}" >&2
  142. printf "not function properly.\n\tThe following versions have been tested: " >&2
  143. echo "${tested_versions[@]}" >&2
  144. fi
  145. }
  146. handle_build() {
  147. if [[ ! "${BUILD}" -eq 1 ]]; then
  148. return
  149. fi
  150. if [[ ! -d "${KERNEL_CHECKOUT}" ]]; then
  151. echo "-b requires vmtest.sh called from the kernel source tree" >&2
  152. exit 1
  153. fi
  154. pushd "${KERNEL_CHECKOUT}" &>/dev/null
  155. if ! vng --kconfig --config "${SCRIPT_DIR}"/config; then
  156. die "failed to generate .config for kernel source tree (${KERNEL_CHECKOUT})"
  157. fi
  158. local vng_args=("-v" "--config" "${SCRIPT_DIR}/config" "--build")
  159. if [[ -n "${BUILD_HOST}" ]]; then
  160. vng_args+=("--build-host" "${BUILD_HOST}")
  161. fi
  162. if [[ -n "${BUILD_HOST_PODMAN_CONTAINER_NAME}" ]]; then
  163. vng_args+=("--build-host-exec-prefix" \
  164. "podman exec -ti ${BUILD_HOST_PODMAN_CONTAINER_NAME}")
  165. fi
  166. if ! vng "${vng_args[@]}"; then
  167. die "failed to build kernel from source tree (${KERNEL_CHECKOUT})"
  168. fi
  169. if ! make -j$(nproc) -C "${HID_BPF_PROGS}"; then
  170. die "failed to build HID bpf objects from source tree (${HID_BPF_PROGS})"
  171. fi
  172. if ! make -j$(nproc) -C "${SCRIPT_DIR}"; then
  173. die "failed to build HID selftests from source tree (${SCRIPT_DIR})"
  174. fi
  175. popd &>/dev/null
  176. }
  177. vm_start() {
  178. local logfile=/dev/null
  179. local verbose_opt=""
  180. local kernel_opt=""
  181. local qemu
  182. qemu=$(command -v "${QEMU}")
  183. if [[ "${VERBOSE}" -eq 2 ]]; then
  184. verbose_opt="--verbose"
  185. logfile=/dev/stdout
  186. fi
  187. # If we are running from within the kernel source tree, use the kernel source tree
  188. # as the kernel to boot, otherwise use the currently running kernel.
  189. if [[ "$(realpath "$(pwd)")" == "${KERNEL_CHECKOUT}"* ]]; then
  190. kernel_opt="${KERNEL_CHECKOUT}"
  191. fi
  192. vng \
  193. --run \
  194. ${kernel_opt} \
  195. ${verbose_opt} \
  196. --qemu-opts="${QEMU_OPTS}" \
  197. --qemu="${qemu}" \
  198. --user root \
  199. --append "${KERNEL_CMDLINE}" \
  200. --ssh "${SSH_GUEST_PORT}" \
  201. --rw &> ${logfile} &
  202. local vng_pid=$!
  203. local elapsed=0
  204. while [[ ! -s "${QEMU_PIDFILE}" ]]; do
  205. if ! kill -0 "${vng_pid}" 2>/dev/null; then
  206. echo "vng process (PID ${vng_pid}) exited early, check logs for details" >&2
  207. die "failed to boot VM"
  208. fi
  209. if [[ ${elapsed} -ge ${WAIT_TOTAL} ]]; then
  210. echo "Timed out after ${WAIT_TOTAL} seconds waiting for VM to boot" >&2
  211. die "failed to boot VM"
  212. fi
  213. sleep 1
  214. elapsed=$((elapsed + 1))
  215. done
  216. }
  217. vm_wait_for_ssh() {
  218. local i
  219. i=0
  220. while true; do
  221. if [[ ${i} -gt ${WAIT_PERIOD_MAX} ]]; then
  222. die "Timed out waiting for guest ssh"
  223. fi
  224. if vm_ssh -- true; then
  225. break
  226. fi
  227. i=$(( i + 1 ))
  228. sleep ${WAIT_PERIOD}
  229. done
  230. }
  231. vm_mount_bpffs() {
  232. vm_ssh -- mount bpffs -t bpf /sys/fs/bpf
  233. }
  234. __log_stdin() {
  235. stdbuf -oL awk '{ printf "%s:\t%s\n","'"${prefix}"'", $0; fflush() }'
  236. }
  237. __log_args() {
  238. echo "$*" | awk '{ printf "%s:\t%s\n","'"${prefix}"'", $0 }'
  239. }
  240. log() {
  241. local verbose="$1"
  242. shift
  243. local prefix="$1"
  244. shift
  245. local redirect=
  246. if [[ ${verbose} -le 0 ]]; then
  247. redirect=/dev/null
  248. else
  249. redirect=/dev/stdout
  250. fi
  251. if [[ "$#" -eq 0 ]]; then
  252. __log_stdin | tee -a "${LOG}" > ${redirect}
  253. else
  254. __log_args "$@" | tee -a "${LOG}" > ${redirect}
  255. fi
  256. }
  257. log_setup() {
  258. log $((VERBOSE-1)) "setup" "$@"
  259. }
  260. log_host() {
  261. local testname=$1
  262. shift
  263. log $((VERBOSE-1)) "test:${testname}:host" "$@"
  264. }
  265. log_guest() {
  266. local testname=$1
  267. shift
  268. log ${VERBOSE} "# test:${testname}" "$@"
  269. }
  270. test_vm_hid_bpf() {
  271. local testname="${FUNCNAME[0]#test_}"
  272. vm_ssh -- "${HID_BPF_TEST}" \
  273. 2>&1 | log_guest "${testname}"
  274. return ${PIPESTATUS[0]}
  275. }
  276. test_vm_hidraw() {
  277. local testname="${FUNCNAME[0]#test_}"
  278. vm_ssh -- "${HIDRAW_TEST}" \
  279. 2>&1 | log_guest "${testname}"
  280. return ${PIPESTATUS[0]}
  281. }
  282. test_vm_pytest() {
  283. local testname="${FUNCNAME[0]#test_}"
  284. shift
  285. vm_ssh -- pytest ${SCRIPT_DIR}/tests --color=yes "$@" \
  286. 2>&1 | log_guest "${testname}"
  287. return ${PIPESTATUS[0]}
  288. }
  289. run_test() {
  290. local vm_oops_cnt_before
  291. local vm_warn_cnt_before
  292. local vm_oops_cnt_after
  293. local vm_warn_cnt_after
  294. local name
  295. local rc
  296. vm_oops_cnt_before=$(vm_ssh -- dmesg | grep -c -i 'Oops')
  297. vm_error_cnt_before=$(vm_ssh -- dmesg --level=err | wc -l)
  298. name=$(echo "${1}" | awk '{ print $1 }')
  299. eval test_"${name}" "$@"
  300. rc=$?
  301. vm_oops_cnt_after=$(vm_ssh -- dmesg | grep -i 'Oops' | wc -l)
  302. if [[ ${vm_oops_cnt_after} -gt ${vm_oops_cnt_before} ]]; then
  303. echo "FAIL: kernel oops detected on vm" | log_host "${name}"
  304. rc=$KSFT_FAIL
  305. fi
  306. vm_error_cnt_after=$(vm_ssh -- dmesg --level=err | wc -l)
  307. if [[ ${vm_error_cnt_after} -gt ${vm_error_cnt_before} ]]; then
  308. echo "FAIL: kernel error detected on vm" | log_host "${name}"
  309. vm_ssh -- dmesg --level=err | log_host "${name}"
  310. rc=$KSFT_FAIL
  311. fi
  312. return "${rc}"
  313. }
  314. QEMU="qemu-system-$(uname -m)"
  315. while getopts :hvsbq:H:p: o
  316. do
  317. case $o in
  318. v) VERBOSE=$((VERBOSE+1));;
  319. s) SHELL_MODE=1;;
  320. b) BUILD=1;;
  321. q) QEMU=$OPTARG;;
  322. H) BUILD_HOST=$OPTARG;;
  323. p) BUILD_HOST_PODMAN_CONTAINER_NAME=$OPTARG;;
  324. h|*) usage;;
  325. esac
  326. done
  327. shift $((OPTIND-1))
  328. trap cleanup EXIT
  329. PARAMS=""
  330. if [[ ${#} -eq 0 ]]; then
  331. ARGS=("${TEST_NAMES[@]}")
  332. else
  333. ARGS=()
  334. COUNT=0
  335. for arg in $@; do
  336. COUNT=$((COUNT+1))
  337. if [[ x"$arg" == x"--" ]]; then
  338. break
  339. fi
  340. ARGS+=($arg)
  341. done
  342. shift $COUNT
  343. PARAMS="$@"
  344. fi
  345. if [[ "${SHELL_MODE}" -eq 0 ]]; then
  346. check_args "${ARGS[@]}"
  347. echo "1..${#ARGS[@]}"
  348. fi
  349. check_deps
  350. check_vng
  351. handle_build
  352. log_setup "Booting up VM"
  353. vm_start
  354. vm_wait_for_ssh
  355. vm_mount_bpffs
  356. log_setup "VM booted up"
  357. if [[ "${SHELL_MODE}" -eq 1 ]]; then
  358. log_setup "Starting interactive shell in VM"
  359. echo "Starting shell in VM. Use 'exit' to quit and shutdown the VM."
  360. CURRENT_DIR="$(pwd)"
  361. vm_ssh -t -- "cd '${CURRENT_DIR}' && exec bash -l"
  362. exit "$KSFT_PASS"
  363. fi
  364. cnt_pass=0
  365. cnt_fail=0
  366. cnt_skip=0
  367. cnt_total=0
  368. for arg in "${ARGS[@]}"; do
  369. run_test "${arg}" "${PARAMS}"
  370. rc=$?
  371. if [[ ${rc} -eq $KSFT_PASS ]]; then
  372. cnt_pass=$(( cnt_pass + 1 ))
  373. echo "ok ${cnt_total} ${arg}"
  374. elif [[ ${rc} -eq $KSFT_SKIP ]]; then
  375. cnt_skip=$(( cnt_skip + 1 ))
  376. echo "ok ${cnt_total} ${arg} # SKIP"
  377. elif [[ ${rc} -eq $KSFT_FAIL ]]; then
  378. cnt_fail=$(( cnt_fail + 1 ))
  379. echo "not ok ${cnt_total} ${arg} # exit=$rc"
  380. fi
  381. cnt_total=$(( cnt_total + 1 ))
  382. done
  383. echo "SUMMARY: PASS=${cnt_pass} SKIP=${cnt_skip} FAIL=${cnt_fail}"
  384. echo "Log: ${LOG}"
  385. if [ $((cnt_pass + cnt_skip)) -eq ${cnt_total} ]; then
  386. exit "$KSFT_PASS"
  387. else
  388. exit "$KSFT_FAIL"
  389. fi