1*e940bc13SJeff Cody#!/bin/bash 2*e940bc13SJeff Cody# 3*e940bc13SJeff Cody# This allows for launching of multiple QEMU instances, with independent 4*e940bc13SJeff Cody# communication possible to each instance. 5*e940bc13SJeff Cody# 6*e940bc13SJeff Cody# Each instance can choose, at launch, to use either the QMP or the 7*e940bc13SJeff Cody# HMP (monitor) interface. 8*e940bc13SJeff Cody# 9*e940bc13SJeff Cody# All instances are cleaned up via _cleanup_qemu, including killing the 10*e940bc13SJeff Cody# running qemu instance. 11*e940bc13SJeff Cody# 12*e940bc13SJeff Cody# Copyright (C) 2014 Red Hat, Inc. 13*e940bc13SJeff Cody# 14*e940bc13SJeff Cody# This program is free software; you can redistribute it and/or modify 15*e940bc13SJeff Cody# it under the terms of the GNU General Public License as published by 16*e940bc13SJeff Cody# the Free Software Foundation; either version 2 of the License, or 17*e940bc13SJeff Cody# (at your option) any later version. 18*e940bc13SJeff Cody# 19*e940bc13SJeff Cody# This program is distributed in the hope that it will be useful, 20*e940bc13SJeff Cody# but WITHOUT ANY WARRANTY; without even the implied warranty of 21*e940bc13SJeff Cody# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22*e940bc13SJeff Cody# GNU General Public License for more details. 23*e940bc13SJeff Cody# 24*e940bc13SJeff Cody# You should have received a copy of the GNU General Public License 25*e940bc13SJeff Cody# along with this program. If not, see <http://www.gnu.org/licenses/>. 26*e940bc13SJeff Cody# 27*e940bc13SJeff Cody 28*e940bc13SJeff CodyQEMU_COMM_TIMEOUT=10 29*e940bc13SJeff Cody 30*e940bc13SJeff CodyQEMU_FIFO_IN="${TEST_DIR}/qmp-in-$$" 31*e940bc13SJeff CodyQEMU_FIFO_OUT="${TEST_DIR}/qmp-out-$$" 32*e940bc13SJeff Cody 33*e940bc13SJeff CodyQEMU_PID= 34*e940bc13SJeff Cody_QEMU_HANDLE=0 35*e940bc13SJeff CodyQEMU_HANDLE=0 36*e940bc13SJeff Cody 37*e940bc13SJeff Cody# If bash version is >= 4.1, these will be overwritten and dynamic 38*e940bc13SJeff Cody# file descriptor values assigned. 39*e940bc13SJeff Cody_out_fd=3 40*e940bc13SJeff Cody_in_fd=4 41*e940bc13SJeff Cody 42*e940bc13SJeff Cody# Wait for expected QMP response from QEMU. Will time out 43*e940bc13SJeff Cody# after 10 seconds, which counts as failure. 44*e940bc13SJeff Cody# 45*e940bc13SJeff Cody# Override QEMU_COMM_TIMEOUT for a timeout different than the 46*e940bc13SJeff Cody# default 10 seconds 47*e940bc13SJeff Cody# 48*e940bc13SJeff Cody# $1: The handle to use 49*e940bc13SJeff Cody# $2+ All remaining arguments comprise the string to search for 50*e940bc13SJeff Cody# in the response. 51*e940bc13SJeff Cody# 52*e940bc13SJeff Cody# If $silent is set to anything but an empty string, then 53*e940bc13SJeff Cody# response is not echoed out. 54*e940bc13SJeff Codyfunction _timed_wait_for() 55*e940bc13SJeff Cody{ 56*e940bc13SJeff Cody local h=${1} 57*e940bc13SJeff Cody shift 58*e940bc13SJeff Cody 59*e940bc13SJeff Cody QEMU_STATUS[$h]=0 60*e940bc13SJeff Cody while read -t ${QEMU_COMM_TIMEOUT} resp <&${QEMU_OUT[$h]} 61*e940bc13SJeff Cody do 62*e940bc13SJeff Cody if [ -z "${silent}" ]; then 63*e940bc13SJeff Cody echo "${resp}" | _filter_testdir | _filter_qemu \ 64*e940bc13SJeff Cody | _filter_qemu_io | _filter_qmp 65*e940bc13SJeff Cody fi 66*e940bc13SJeff Cody grep -q "${*}" < <(echo ${resp}) 67*e940bc13SJeff Cody if [ $? -eq 0 ]; then 68*e940bc13SJeff Cody return 69*e940bc13SJeff Cody fi 70*e940bc13SJeff Cody done 71*e940bc13SJeff Cody QEMU_STATUS[$h]=-1 72*e940bc13SJeff Cody if [ -z "${qemu_error_no_exit}" ]; then 73*e940bc13SJeff Cody echo "Timeout waiting for ${*} on handle ${h}" 74*e940bc13SJeff Cody exit 1 # Timeout means the test failed 75*e940bc13SJeff Cody fi 76*e940bc13SJeff Cody} 77*e940bc13SJeff Cody 78*e940bc13SJeff Cody 79*e940bc13SJeff Cody# Sends QMP or HMP command to QEMU, and waits for the expected response 80*e940bc13SJeff Cody# 81*e940bc13SJeff Cody# $1: QEMU handle to use 82*e940bc13SJeff Cody# $2: String of the QMP command to send 83*e940bc13SJeff Cody# ${@: -1} (Last string passed) 84*e940bc13SJeff Cody# String that the QEMU response should contain. If it is a null 85*e940bc13SJeff Cody# string, do not wait for a response 86*e940bc13SJeff Cody# 87*e940bc13SJeff Cody# Set qemu_cmd_repeat to the number of times to repeat the cmd 88*e940bc13SJeff Cody# until either timeout, or a response. If it is not set, or <=0, 89*e940bc13SJeff Cody# then the command is only sent once. 90*e940bc13SJeff Cody# 91*e940bc13SJeff Cody# If $qemu_error_no_exit is set, then even if the expected response 92*e940bc13SJeff Cody# is not seen, we will not exit. $QEMU_STATUS[$1] will be set it -1 in 93*e940bc13SJeff Cody# that case. 94*e940bc13SJeff Codyfunction _send_qemu_cmd() 95*e940bc13SJeff Cody{ 96*e940bc13SJeff Cody local h=${1} 97*e940bc13SJeff Cody local count=1 98*e940bc13SJeff Cody local cmd= 99*e940bc13SJeff Cody local use_error=${qemu_error_no_exit} 100*e940bc13SJeff Cody shift 101*e940bc13SJeff Cody 102*e940bc13SJeff Cody if [ ${qemu_cmd_repeat} -gt 0 ] 2>/dev/null; then 103*e940bc13SJeff Cody count=${qemu_cmd_repeat} 104*e940bc13SJeff Cody use_error="no" 105*e940bc13SJeff Cody fi 106*e940bc13SJeff Cody # This array element extraction is done to accomodate pathnames with spaces 107*e940bc13SJeff Cody cmd=${@: 1:${#@}-1} 108*e940bc13SJeff Cody shift $(($# - 1)) 109*e940bc13SJeff Cody 110*e940bc13SJeff Cody while [ ${count} -gt 0 ] 111*e940bc13SJeff Cody do 112*e940bc13SJeff Cody echo "${cmd}" >&${QEMU_IN[${h}]} 113*e940bc13SJeff Cody if [ -n "${1}" ]; then 114*e940bc13SJeff Cody qemu_error_no_exit=${use_error} _timed_wait_for ${h} "${1}" 115*e940bc13SJeff Cody if [ ${QEMU_STATUS[$h]} -eq 0 ]; then 116*e940bc13SJeff Cody return 117*e940bc13SJeff Cody fi 118*e940bc13SJeff Cody fi 119*e940bc13SJeff Cody let count--; 120*e940bc13SJeff Cody done 121*e940bc13SJeff Cody if [ ${QEMU_STATUS[$h]} -ne 0 ] && [ -z "${qemu_error_no_exit}" ]; then 122*e940bc13SJeff Cody echo "Timeout waiting for ${1} on handle ${h}" 123*e940bc13SJeff Cody exit 1 #Timeout means the test failed 124*e940bc13SJeff Cody fi 125*e940bc13SJeff Cody} 126*e940bc13SJeff Cody 127*e940bc13SJeff Cody 128*e940bc13SJeff Cody# Launch a QEMU process. 129*e940bc13SJeff Cody# 130*e940bc13SJeff Cody# Input parameters: 131*e940bc13SJeff Cody# $qemu_comm_method: set this variable to 'monitor' (case insensitive) 132*e940bc13SJeff Cody# to use the QEMU HMP monitor for communication. 133*e940bc13SJeff Cody# Otherwise, the default of QMP is used. 134*e940bc13SJeff Cody# Returns: 135*e940bc13SJeff Cody# $QEMU_HANDLE: set to a handle value to communicate with this QEMU instance. 136*e940bc13SJeff Cody# 137*e940bc13SJeff Codyfunction _launch_qemu() 138*e940bc13SJeff Cody{ 139*e940bc13SJeff Cody local comm= 140*e940bc13SJeff Cody local fifo_out= 141*e940bc13SJeff Cody local fifo_in= 142*e940bc13SJeff Cody 143*e940bc13SJeff Cody if (shopt -s nocasematch; [[ "${qemu_comm_method}" == "monitor" ]]) 144*e940bc13SJeff Cody then 145*e940bc13SJeff Cody comm="-monitor stdio" 146*e940bc13SJeff Cody else 147*e940bc13SJeff Cody local qemu_comm_method="qmp" 148*e940bc13SJeff Cody comm="-monitor none -qmp stdio" 149*e940bc13SJeff Cody fi 150*e940bc13SJeff Cody 151*e940bc13SJeff Cody fifo_out=${QEMU_FIFO_OUT}_${_QEMU_HANDLE} 152*e940bc13SJeff Cody fifo_in=${QEMU_FIFO_IN}_${_QEMU_HANDLE} 153*e940bc13SJeff Cody mkfifo "${fifo_out}" 154*e940bc13SJeff Cody mkfifo "${fifo_in}" 155*e940bc13SJeff Cody 156*e940bc13SJeff Cody "${QEMU}" -nographic -serial none ${comm} -machine accel=qtest "${@}" 2>&1 \ 157*e940bc13SJeff Cody >"${fifo_out}" \ 158*e940bc13SJeff Cody <"${fifo_in}" & 159*e940bc13SJeff Cody QEMU_PID[${_QEMU_HANDLE}]=$! 160*e940bc13SJeff Cody 161*e940bc13SJeff Cody if [[ "${BASH_VERSINFO[0]}" -ge "5" || 162*e940bc13SJeff Cody ("${BASH_VERSINFO[0]}" -ge "4" && "${BASH_VERSINFO[1]}" -ge "1") ]] 163*e940bc13SJeff Cody then 164*e940bc13SJeff Cody # bash >= 4.1 required for automatic fd 165*e940bc13SJeff Cody exec {_out_fd}<"${fifo_out}" 166*e940bc13SJeff Cody exec {_in_fd}>"${fifo_in}" 167*e940bc13SJeff Cody else 168*e940bc13SJeff Cody let _out_fd++ 169*e940bc13SJeff Cody let _in_fd++ 170*e940bc13SJeff Cody eval "exec ${_out_fd}<'${fifo_out}'" 171*e940bc13SJeff Cody eval "exec ${_in_fd}>'${fifo_in}'" 172*e940bc13SJeff Cody fi 173*e940bc13SJeff Cody 174*e940bc13SJeff Cody QEMU_OUT[${_QEMU_HANDLE}]=${_out_fd} 175*e940bc13SJeff Cody QEMU_IN[${_QEMU_HANDLE}]=${_in_fd} 176*e940bc13SJeff Cody QEMU_STATUS[${_QEMU_HANDLE}]=0 177*e940bc13SJeff Cody 178*e940bc13SJeff Cody if [ "${qemu_comm_method}" == "qmp" ] 179*e940bc13SJeff Cody then 180*e940bc13SJeff Cody # Don't print response, since it has version information in it 181*e940bc13SJeff Cody silent=yes _timed_wait_for ${_QEMU_HANDLE} "capabilities" 182*e940bc13SJeff Cody fi 183*e940bc13SJeff Cody QEMU_HANDLE=${_QEMU_HANDLE} 184*e940bc13SJeff Cody let _QEMU_HANDLE++ 185*e940bc13SJeff Cody} 186*e940bc13SJeff Cody 187*e940bc13SJeff Cody 188*e940bc13SJeff Cody# Silenty kills the QEMU process 189*e940bc13SJeff Codyfunction _cleanup_qemu() 190*e940bc13SJeff Cody{ 191*e940bc13SJeff Cody # QEMU_PID[], QEMU_IN[], QEMU_OUT[] all use same indices 192*e940bc13SJeff Cody for i in "${!QEMU_OUT[@]}" 193*e940bc13SJeff Cody do 194*e940bc13SJeff Cody kill -KILL ${QEMU_PID[$i]} 2>/dev/null 195*e940bc13SJeff Cody wait ${QEMU_PID[$i]} 2>/dev/null # silent kill 196*e940bc13SJeff Cody rm -f "${QEMU_FIFO_IN}_${i}" "${QEMU_FIFO_OUT}_${i}" 197*e940bc13SJeff Cody eval "exec ${QEMU_IN[$i]}<&-" # close file descriptors 198*e940bc13SJeff Cody eval "exec ${QEMU_OUT[$i]}<&-" 199*e940bc13SJeff Cody done 200*e940bc13SJeff Cody} 201