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