111a82d14SPhilippe Mathieu-Daudé#!/usr/bin/env bash 2e940bc13SJeff Cody# 3e940bc13SJeff Cody# This allows for launching of multiple QEMU instances, with independent 4e940bc13SJeff Cody# communication possible to each instance. 5e940bc13SJeff Cody# 6e940bc13SJeff Cody# Each instance can choose, at launch, to use either the QMP or the 7e940bc13SJeff Cody# HMP (monitor) interface. 8e940bc13SJeff Cody# 9e940bc13SJeff Cody# All instances are cleaned up via _cleanup_qemu, including killing the 10e940bc13SJeff Cody# running qemu instance. 11e940bc13SJeff Cody# 12e940bc13SJeff Cody# Copyright (C) 2014 Red Hat, Inc. 13e940bc13SJeff Cody# 14e940bc13SJeff Cody# This program is free software; you can redistribute it and/or modify 15e940bc13SJeff Cody# it under the terms of the GNU General Public License as published by 16e940bc13SJeff Cody# the Free Software Foundation; either version 2 of the License, or 17e940bc13SJeff Cody# (at your option) any later version. 18e940bc13SJeff Cody# 19e940bc13SJeff Cody# This program is distributed in the hope that it will be useful, 20e940bc13SJeff Cody# but WITHOUT ANY WARRANTY; without even the implied warranty of 21e940bc13SJeff Cody# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22e940bc13SJeff Cody# GNU General Public License for more details. 23e940bc13SJeff Cody# 24e940bc13SJeff Cody# You should have received a copy of the GNU General Public License 25e940bc13SJeff Cody# along with this program. If not, see <http://www.gnu.org/licenses/>. 26e940bc13SJeff Cody# 27e940bc13SJeff Cody 28e940bc13SJeff CodyQEMU_COMM_TIMEOUT=10 29e940bc13SJeff Cody 30846a1d11SJeff CodyQEMU_FIFO_IN="${QEMU_TEST_DIR}/qmp-in-$$" 31846a1d11SJeff CodyQEMU_FIFO_OUT="${QEMU_TEST_DIR}/qmp-out-$$" 32e940bc13SJeff Cody 33e940bc13SJeff CodyQEMU_HANDLE=0 34cce293a2SPaolo Bonziniexport _QEMU_HANDLE=0 35e940bc13SJeff Cody 36e940bc13SJeff Cody# If bash version is >= 4.1, these will be overwritten and dynamic 37e940bc13SJeff Cody# file descriptor values assigned. 38e940bc13SJeff Cody_out_fd=3 39e940bc13SJeff Cody_in_fd=4 40e940bc13SJeff Cody 41e940bc13SJeff Cody# Wait for expected QMP response from QEMU. Will time out 42e940bc13SJeff Cody# after 10 seconds, which counts as failure. 43e940bc13SJeff Cody# 44e940bc13SJeff Cody# Override QEMU_COMM_TIMEOUT for a timeout different than the 45e940bc13SJeff Cody# default 10 seconds 46e940bc13SJeff Cody# 47e940bc13SJeff Cody# $1: The handle to use 48e940bc13SJeff Cody# $2+ All remaining arguments comprise the string to search for 49e940bc13SJeff Cody# in the response. 50e940bc13SJeff Cody# 51e940bc13SJeff Cody# If $silent is set to anything but an empty string, then 52e940bc13SJeff Cody# response is not echoed out. 53a2339699SJeff Cody# If $mismatch_only is set, only non-matching responses will 54a2339699SJeff Cody# be echoed. 5581c6ddf4SMax Reitz# 5681c6ddf4SMax Reitz# If $success_or_failure is set, the meaning of the arguments is 5781c6ddf4SMax Reitz# changed as follows: 5881c6ddf4SMax Reitz# $2: A string to search for in the response; if found, this indicates 5981c6ddf4SMax Reitz# success and ${QEMU_STATUS[$1]} is set to 0. 6081c6ddf4SMax Reitz# $3: A string to search for in the response; if found, this indicates 6181c6ddf4SMax Reitz# failure and the test is either aborted (if $qemu_error_no_exit 6281c6ddf4SMax Reitz# is not set) or ${QEMU_STATUS[$1]} is set to -1 (otherwise). 638cedcffdSEric Blake_timed_wait_for() 64e940bc13SJeff Cody{ 65e940bc13SJeff Cody local h=${1} 66e940bc13SJeff Cody shift 67e940bc13SJeff Cody 6881c6ddf4SMax Reitz if [ -z "${success_or_failure}" ]; then 6981c6ddf4SMax Reitz success_match=${*} 7081c6ddf4SMax Reitz failure_match= 7181c6ddf4SMax Reitz else 7281c6ddf4SMax Reitz success_match=${1} 7381c6ddf4SMax Reitz failure_match=${2} 7481c6ddf4SMax Reitz fi 7581c6ddf4SMax Reitz 7681c6ddf4SMax Reitz timeout=yes 7781c6ddf4SMax Reitz 78e940bc13SJeff Cody QEMU_STATUS[$h]=0 7972538537SKevin Wolf while IFS= read -t ${QEMU_COMM_TIMEOUT} resp <&${QEMU_OUT[$h]} 80e940bc13SJeff Cody do 81a2339699SJeff Cody if [ -z "${silent}" ] && [ -z "${mismatch_only}" ]; then 82e940bc13SJeff Cody echo "${resp}" | _filter_testdir | _filter_qemu \ 8369404d9eSKevin Wolf | _filter_qemu_io | _filter_qmp | _filter_hmp 84e940bc13SJeff Cody fi 8581c6ddf4SMax Reitz if [ -n "${failure_match}" ]; then 8681c6ddf4SMax Reitz grep -q "${failure_match}" < <(echo "${resp}") 8781c6ddf4SMax Reitz if [ $? -eq 0 ]; then 8881c6ddf4SMax Reitz timeout= 8981c6ddf4SMax Reitz break 9081c6ddf4SMax Reitz fi 9181c6ddf4SMax Reitz fi 9281c6ddf4SMax Reitz grep -q "${success_match}" < <(echo "${resp}") 93e940bc13SJeff Cody if [ $? -eq 0 ]; then 94e940bc13SJeff Cody return 9581c6ddf4SMax Reitz fi 9681c6ddf4SMax Reitz if [ -z "${silent}" ] && [ -n "${mismatch_only}" ]; then 97a2339699SJeff Cody echo "${resp}" | _filter_testdir | _filter_qemu \ 98a2339699SJeff Cody | _filter_qemu_io | _filter_qmp | _filter_hmp 99e940bc13SJeff Cody fi 100a2339699SJeff Cody 101e940bc13SJeff Cody done 102e940bc13SJeff Cody QEMU_STATUS[$h]=-1 103e940bc13SJeff Cody if [ -z "${qemu_error_no_exit}" ]; then 10481c6ddf4SMax Reitz if [ -n "${timeout}" ]; then 10581c6ddf4SMax Reitz echo "Timeout waiting for ${success_match} on handle ${h}" 10681c6ddf4SMax Reitz else 10781c6ddf4SMax Reitz echo "Wrong response matching ${failure_match} on handle ${h}" 10881c6ddf4SMax Reitz fi 10981c6ddf4SMax Reitz exit 1 # Timeout or wrong match mean the test failed 110e940bc13SJeff Cody fi 111e940bc13SJeff Cody} 112e940bc13SJeff Cody 113e940bc13SJeff Cody 114e940bc13SJeff Cody# Sends QMP or HMP command to QEMU, and waits for the expected response 115e940bc13SJeff Cody# 116e940bc13SJeff Cody# $1: QEMU handle to use 117e940bc13SJeff Cody# $2: String of the QMP command to send 118e940bc13SJeff Cody# ${@: -1} (Last string passed) 119e940bc13SJeff Cody# String that the QEMU response should contain. If it is a null 120e940bc13SJeff Cody# string, do not wait for a response 121e940bc13SJeff Cody# 122e940bc13SJeff Cody# Set qemu_cmd_repeat to the number of times to repeat the cmd 123e940bc13SJeff Cody# until either timeout, or a response. If it is not set, or <=0, 124e940bc13SJeff Cody# then the command is only sent once. 125e940bc13SJeff Cody# 126*a98b1a1fSEric Blake# If neither $silent nor $mismatch_only is set, and $cmd begins with '{', 127*a98b1a1fSEric Blake# echo the command before sending it the first time. 128*a98b1a1fSEric Blake# 129e940bc13SJeff Cody# If $qemu_error_no_exit is set, then even if the expected response 130e940bc13SJeff Cody# is not seen, we will not exit. $QEMU_STATUS[$1] will be set it -1 in 131e940bc13SJeff Cody# that case. 13281c6ddf4SMax Reitz# 13381c6ddf4SMax Reitz# If $success_or_failure is set, then the last two strings are the 13481c6ddf4SMax Reitz# strings the response will be scanned for. The first of the two 13581c6ddf4SMax Reitz# indicates success, the latter indicates failure. Failure is handled 13681c6ddf4SMax Reitz# like a timeout. 1378cedcffdSEric Blake_send_qemu_cmd() 138e940bc13SJeff Cody{ 139e940bc13SJeff Cody local h=${1} 140e940bc13SJeff Cody local count=1 141e940bc13SJeff Cody local cmd= 142e940bc13SJeff Cody local use_error=${qemu_error_no_exit} 143e940bc13SJeff Cody shift 144e940bc13SJeff Cody 145e940bc13SJeff Cody if [ ${qemu_cmd_repeat} -gt 0 ] 2>/dev/null; then 146e940bc13SJeff Cody count=${qemu_cmd_repeat} 147e940bc13SJeff Cody use_error="no" 148e940bc13SJeff Cody fi 1495d831be2SStefan Weil # This array element extraction is done to accommodate pathnames with spaces 15081c6ddf4SMax Reitz if [ -z "${success_or_failure}" ]; then 151e940bc13SJeff Cody cmd=${@: 1:${#@}-1} 152e940bc13SJeff Cody shift $(($# - 1)) 15381c6ddf4SMax Reitz else 15481c6ddf4SMax Reitz cmd=${@: 1:${#@}-2} 15581c6ddf4SMax Reitz shift $(($# - 2)) 15681c6ddf4SMax Reitz fi 157e940bc13SJeff Cody 158*a98b1a1fSEric Blake # Display QMP being sent, but not HMP (since HMP already echoes its 159*a98b1a1fSEric Blake # input back to output); decide based on leading '{' 160*a98b1a1fSEric Blake if [ -z "$silent" ] && [ -z "$mismatch_only" ] && 161*a98b1a1fSEric Blake [ "$cmd" != "${cmd#\{}" ]; then 162*a98b1a1fSEric Blake echo "${cmd}" | _filter_testdir | _filter_imgfmt 163*a98b1a1fSEric Blake fi 164e940bc13SJeff Cody while [ ${count} -gt 0 ] 165e940bc13SJeff Cody do 166e940bc13SJeff Cody echo "${cmd}" >&${QEMU_IN[${h}]} 167e940bc13SJeff Cody if [ -n "${1}" ]; then 16881c6ddf4SMax Reitz if [ -z "${success_or_failure}" ]; then 169e940bc13SJeff Cody qemu_error_no_exit=${use_error} _timed_wait_for ${h} "${1}" 17081c6ddf4SMax Reitz else 17181c6ddf4SMax Reitz qemu_error_no_exit=${use_error} _timed_wait_for ${h} "${1}" "${2}" 17281c6ddf4SMax Reitz fi 173e940bc13SJeff Cody if [ ${QEMU_STATUS[$h]} -eq 0 ]; then 174e940bc13SJeff Cody return 175e940bc13SJeff Cody fi 176e940bc13SJeff Cody fi 177e940bc13SJeff Cody let count--; 178e940bc13SJeff Cody done 179e940bc13SJeff Cody if [ ${QEMU_STATUS[$h]} -ne 0 ] && [ -z "${qemu_error_no_exit}" ]; then 180e940bc13SJeff Cody echo "Timeout waiting for ${1} on handle ${h}" 181e940bc13SJeff Cody exit 1 #Timeout means the test failed 182e940bc13SJeff Cody fi 183e940bc13SJeff Cody} 184e940bc13SJeff Cody 185e940bc13SJeff Cody 186e940bc13SJeff Cody# Launch a QEMU process. 187e940bc13SJeff Cody# 188e940bc13SJeff Cody# Input parameters: 189e940bc13SJeff Cody# $qemu_comm_method: set this variable to 'monitor' (case insensitive) 190e940bc13SJeff Cody# to use the QEMU HMP monitor for communication. 191e940bc13SJeff Cody# Otherwise, the default of QMP is used. 19272538537SKevin Wolf# $qmp_pretty: Set this variable to 'y' to enable QMP pretty printing. 19315cfba69SMax Reitz# $keep_stderr: Set this variable to 'y' to keep QEMU's stderr output on stderr. 19415cfba69SMax Reitz# If this variable is empty, stderr will be redirected to stdout. 195e940bc13SJeff Cody# Returns: 196e940bc13SJeff Cody# $QEMU_HANDLE: set to a handle value to communicate with this QEMU instance. 197e940bc13SJeff Cody# 1988cedcffdSEric Blake_launch_qemu() 199e940bc13SJeff Cody{ 200e940bc13SJeff Cody local comm= 201e940bc13SJeff Cody local fifo_out= 202e940bc13SJeff Cody local fifo_in= 203e940bc13SJeff Cody 204e940bc13SJeff Cody if (shopt -s nocasematch; [[ "${qemu_comm_method}" == "monitor" ]]) 205e940bc13SJeff Cody then 206e940bc13SJeff Cody comm="-monitor stdio" 207e940bc13SJeff Cody else 208e940bc13SJeff Cody local qemu_comm_method="qmp" 20972538537SKevin Wolf if [ "$qmp_pretty" = "y" ]; then 21072538537SKevin Wolf comm="-monitor none -qmp-pretty stdio" 21172538537SKevin Wolf else 212e940bc13SJeff Cody comm="-monitor none -qmp stdio" 213e940bc13SJeff Cody fi 21472538537SKevin Wolf fi 215e940bc13SJeff Cody 216e940bc13SJeff Cody fifo_out=${QEMU_FIFO_OUT}_${_QEMU_HANDLE} 217e940bc13SJeff Cody fifo_in=${QEMU_FIFO_IN}_${_QEMU_HANDLE} 218e940bc13SJeff Cody mkfifo "${fifo_out}" 219e940bc13SJeff Cody mkfifo "${fifo_in}" 220e940bc13SJeff Cody 22113a1d4a7SDaniel P. Berrange object_options= 22213a1d4a7SDaniel P. Berrange if [ -n "$IMGKEYSECRET" ]; then 22313a1d4a7SDaniel P. Berrange object_options="--object secret,id=keysec0,data=$IMGKEYSECRET" 22413a1d4a7SDaniel P. Berrange fi 22513a1d4a7SDaniel P. Berrange 22615cfba69SMax Reitz if [ -z "$keep_stderr" ]; then 227f6c8c2e0SJeff Cody QEMU_NEED_PID='y'\ 22813a1d4a7SDaniel P. Berrange ${QEMU} ${object_options} -nographic -serial none ${comm} "${@}" >"${fifo_out}" \ 229d71a8b06SKevin Wolf 2>&1 \ 230e940bc13SJeff Cody <"${fifo_in}" & 23115cfba69SMax Reitz elif [ "$keep_stderr" = "y" ]; then 23215cfba69SMax Reitz QEMU_NEED_PID='y'\ 23313a1d4a7SDaniel P. Berrange ${QEMU} ${object_options} -nographic -serial none ${comm} "${@}" >"${fifo_out}" \ 23415cfba69SMax Reitz <"${fifo_in}" & 23515cfba69SMax Reitz else 23615cfba69SMax Reitz exit 1 23715cfba69SMax Reitz fi 238e940bc13SJeff Cody 239e940bc13SJeff Cody if [[ "${BASH_VERSINFO[0]}" -ge "5" || 240e940bc13SJeff Cody ("${BASH_VERSINFO[0]}" -ge "4" && "${BASH_VERSINFO[1]}" -ge "1") ]] 241e940bc13SJeff Cody then 242e940bc13SJeff Cody # bash >= 4.1 required for automatic fd 243e940bc13SJeff Cody exec {_out_fd}<"${fifo_out}" 244e940bc13SJeff Cody exec {_in_fd}>"${fifo_in}" 245e940bc13SJeff Cody else 246e940bc13SJeff Cody let _out_fd++ 247e940bc13SJeff Cody let _in_fd++ 248e940bc13SJeff Cody eval "exec ${_out_fd}<'${fifo_out}'" 249e940bc13SJeff Cody eval "exec ${_in_fd}>'${fifo_in}'" 250e940bc13SJeff Cody fi 251e940bc13SJeff Cody 252e940bc13SJeff Cody QEMU_OUT[${_QEMU_HANDLE}]=${_out_fd} 253e940bc13SJeff Cody QEMU_IN[${_QEMU_HANDLE}]=${_in_fd} 254e940bc13SJeff Cody QEMU_STATUS[${_QEMU_HANDLE}]=0 255e940bc13SJeff Cody 256e940bc13SJeff Cody if [ "${qemu_comm_method}" == "qmp" ] 257e940bc13SJeff Cody then 258e940bc13SJeff Cody # Don't print response, since it has version information in it 259e940bc13SJeff Cody silent=yes _timed_wait_for ${_QEMU_HANDLE} "capabilities" 26072538537SKevin Wolf if [ "$qmp_pretty" = "y" ]; then 26172538537SKevin Wolf silent=yes _timed_wait_for ${_QEMU_HANDLE} "^}" 26272538537SKevin Wolf fi 263e940bc13SJeff Cody fi 264e940bc13SJeff Cody QEMU_HANDLE=${_QEMU_HANDLE} 265e940bc13SJeff Cody let _QEMU_HANDLE++ 266e940bc13SJeff Cody} 267e940bc13SJeff Cody 268e940bc13SJeff Cody 269e50a6121SStefan Weil# Silently kills the QEMU process 270ea82aa42SMax Reitz# 271ea82aa42SMax Reitz# If $wait is set to anything other than the empty string, the process will not 272ea82aa42SMax Reitz# be killed but only waited for, and any output will be forwarded to stdout. If 273ea82aa42SMax Reitz# $wait is empty, the process will be killed and all output will be suppressed. 2748cedcffdSEric Blake_cleanup_qemu() 275e940bc13SJeff Cody{ 276e940bc13SJeff Cody # QEMU_PID[], QEMU_IN[], QEMU_OUT[] all use same indices 277e940bc13SJeff Cody for i in "${!QEMU_OUT[@]}" 278e940bc13SJeff Cody do 279f6c8c2e0SJeff Cody local QEMU_PID 280846a1d11SJeff Cody if [ -f "${QEMU_TEST_DIR}/qemu-${i}.pid" ]; then 281846a1d11SJeff Cody read QEMU_PID < "${QEMU_TEST_DIR}/qemu-${i}.pid" 282846a1d11SJeff Cody rm -f "${QEMU_TEST_DIR}/qemu-${i}.pid" 283f6c8c2e0SJeff Cody if [ -z "${wait}" ] && [ -n "${QEMU_PID}" ]; then 284f6c8c2e0SJeff Cody kill -KILL ${QEMU_PID} 2>/dev/null 285ea82aa42SMax Reitz fi 286f6c8c2e0SJeff Cody if [ -n "${QEMU_PID}" ]; then 287f6c8c2e0SJeff Cody wait ${QEMU_PID} 2>/dev/null # silent kill 288f6c8c2e0SJeff Cody fi 289f6c8c2e0SJeff Cody fi 290f6c8c2e0SJeff Cody 291ea82aa42SMax Reitz if [ -n "${wait}" ]; then 292ea82aa42SMax Reitz cat <&${QEMU_OUT[$i]} | _filter_testdir | _filter_qemu \ 29369404d9eSKevin Wolf | _filter_qemu_io | _filter_qmp | _filter_hmp 294ea82aa42SMax Reitz fi 295e940bc13SJeff Cody rm -f "${QEMU_FIFO_IN}_${i}" "${QEMU_FIFO_OUT}_${i}" 296e940bc13SJeff Cody eval "exec ${QEMU_IN[$i]}<&-" # close file descriptors 297e940bc13SJeff Cody eval "exec ${QEMU_OUT[$i]}<&-" 298ecaf8c8aSKevin Wolf 299ecaf8c8aSKevin Wolf unset QEMU_IN[$i] 300ecaf8c8aSKevin Wolf unset QEMU_OUT[$i] 301e940bc13SJeff Cody done 302e940bc13SJeff Cody} 303