1e940bc13SJeff Cody#!/bin/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 30e940bc13SJeff CodyQEMU_FIFO_IN="${TEST_DIR}/qmp-in-$$" 31e940bc13SJeff CodyQEMU_FIFO_OUT="${TEST_DIR}/qmp-out-$$" 32e940bc13SJeff Cody 33e940bc13SJeff CodyQEMU_HANDLE=0 34e940bc13SJeff Cody 35e940bc13SJeff Cody# If bash version is >= 4.1, these will be overwritten and dynamic 36e940bc13SJeff Cody# file descriptor values assigned. 37e940bc13SJeff Cody_out_fd=3 38e940bc13SJeff Cody_in_fd=4 39e940bc13SJeff Cody 40e940bc13SJeff Cody# Wait for expected QMP response from QEMU. Will time out 41e940bc13SJeff Cody# after 10 seconds, which counts as failure. 42e940bc13SJeff Cody# 43e940bc13SJeff Cody# Override QEMU_COMM_TIMEOUT for a timeout different than the 44e940bc13SJeff Cody# default 10 seconds 45e940bc13SJeff Cody# 46e940bc13SJeff Cody# $1: The handle to use 47e940bc13SJeff Cody# $2+ All remaining arguments comprise the string to search for 48e940bc13SJeff Cody# in the response. 49e940bc13SJeff Cody# 50e940bc13SJeff Cody# If $silent is set to anything but an empty string, then 51e940bc13SJeff Cody# response is not echoed out. 52e940bc13SJeff Codyfunction _timed_wait_for() 53e940bc13SJeff Cody{ 54e940bc13SJeff Cody local h=${1} 55e940bc13SJeff Cody shift 56e940bc13SJeff Cody 57e940bc13SJeff Cody QEMU_STATUS[$h]=0 58e940bc13SJeff Cody while read -t ${QEMU_COMM_TIMEOUT} resp <&${QEMU_OUT[$h]} 59e940bc13SJeff Cody do 60e940bc13SJeff Cody if [ -z "${silent}" ]; then 61e940bc13SJeff Cody echo "${resp}" | _filter_testdir | _filter_qemu \ 62e940bc13SJeff Cody | _filter_qemu_io | _filter_qmp 63e940bc13SJeff Cody fi 64e940bc13SJeff Cody grep -q "${*}" < <(echo ${resp}) 65e940bc13SJeff Cody if [ $? -eq 0 ]; then 66e940bc13SJeff Cody return 67e940bc13SJeff Cody fi 68e940bc13SJeff Cody done 69e940bc13SJeff Cody QEMU_STATUS[$h]=-1 70e940bc13SJeff Cody if [ -z "${qemu_error_no_exit}" ]; then 71e940bc13SJeff Cody echo "Timeout waiting for ${*} on handle ${h}" 72e940bc13SJeff Cody exit 1 # Timeout means the test failed 73e940bc13SJeff Cody fi 74e940bc13SJeff Cody} 75e940bc13SJeff Cody 76e940bc13SJeff Cody 77e940bc13SJeff Cody# Sends QMP or HMP command to QEMU, and waits for the expected response 78e940bc13SJeff Cody# 79e940bc13SJeff Cody# $1: QEMU handle to use 80e940bc13SJeff Cody# $2: String of the QMP command to send 81e940bc13SJeff Cody# ${@: -1} (Last string passed) 82e940bc13SJeff Cody# String that the QEMU response should contain. If it is a null 83e940bc13SJeff Cody# string, do not wait for a response 84e940bc13SJeff Cody# 85e940bc13SJeff Cody# Set qemu_cmd_repeat to the number of times to repeat the cmd 86e940bc13SJeff Cody# until either timeout, or a response. If it is not set, or <=0, 87e940bc13SJeff Cody# then the command is only sent once. 88e940bc13SJeff Cody# 89e940bc13SJeff Cody# If $qemu_error_no_exit is set, then even if the expected response 90e940bc13SJeff Cody# is not seen, we will not exit. $QEMU_STATUS[$1] will be set it -1 in 91e940bc13SJeff Cody# that case. 92e940bc13SJeff Codyfunction _send_qemu_cmd() 93e940bc13SJeff Cody{ 94e940bc13SJeff Cody local h=${1} 95e940bc13SJeff Cody local count=1 96e940bc13SJeff Cody local cmd= 97e940bc13SJeff Cody local use_error=${qemu_error_no_exit} 98e940bc13SJeff Cody shift 99e940bc13SJeff Cody 100e940bc13SJeff Cody if [ ${qemu_cmd_repeat} -gt 0 ] 2>/dev/null; then 101e940bc13SJeff Cody count=${qemu_cmd_repeat} 102e940bc13SJeff Cody use_error="no" 103e940bc13SJeff Cody fi 1045d831be2SStefan Weil # This array element extraction is done to accommodate pathnames with spaces 105e940bc13SJeff Cody cmd=${@: 1:${#@}-1} 106e940bc13SJeff Cody shift $(($# - 1)) 107e940bc13SJeff Cody 108e940bc13SJeff Cody while [ ${count} -gt 0 ] 109e940bc13SJeff Cody do 110e940bc13SJeff Cody echo "${cmd}" >&${QEMU_IN[${h}]} 111e940bc13SJeff Cody if [ -n "${1}" ]; then 112e940bc13SJeff Cody qemu_error_no_exit=${use_error} _timed_wait_for ${h} "${1}" 113e940bc13SJeff Cody if [ ${QEMU_STATUS[$h]} -eq 0 ]; then 114e940bc13SJeff Cody return 115e940bc13SJeff Cody fi 116e940bc13SJeff Cody fi 117e940bc13SJeff Cody let count--; 118e940bc13SJeff Cody done 119e940bc13SJeff Cody if [ ${QEMU_STATUS[$h]} -ne 0 ] && [ -z "${qemu_error_no_exit}" ]; then 120e940bc13SJeff Cody echo "Timeout waiting for ${1} on handle ${h}" 121e940bc13SJeff Cody exit 1 #Timeout means the test failed 122e940bc13SJeff Cody fi 123e940bc13SJeff Cody} 124e940bc13SJeff Cody 125e940bc13SJeff Cody 126e940bc13SJeff Cody# Launch a QEMU process. 127e940bc13SJeff Cody# 128e940bc13SJeff Cody# Input parameters: 129e940bc13SJeff Cody# $qemu_comm_method: set this variable to 'monitor' (case insensitive) 130e940bc13SJeff Cody# to use the QEMU HMP monitor for communication. 131e940bc13SJeff Cody# Otherwise, the default of QMP is used. 13215cfba69SMax Reitz# $keep_stderr: Set this variable to 'y' to keep QEMU's stderr output on stderr. 13315cfba69SMax Reitz# If this variable is empty, stderr will be redirected to stdout. 134e940bc13SJeff Cody# Returns: 135e940bc13SJeff Cody# $QEMU_HANDLE: set to a handle value to communicate with this QEMU instance. 136e940bc13SJeff Cody# 137e940bc13SJeff Codyfunction _launch_qemu() 138e940bc13SJeff Cody{ 139e940bc13SJeff Cody local comm= 140e940bc13SJeff Cody local fifo_out= 141e940bc13SJeff Cody local fifo_in= 142e940bc13SJeff Cody 143e940bc13SJeff Cody if (shopt -s nocasematch; [[ "${qemu_comm_method}" == "monitor" ]]) 144e940bc13SJeff Cody then 145e940bc13SJeff Cody comm="-monitor stdio" 146e940bc13SJeff Cody else 147e940bc13SJeff Cody local qemu_comm_method="qmp" 148e940bc13SJeff Cody comm="-monitor none -qmp stdio" 149e940bc13SJeff Cody fi 150e940bc13SJeff Cody 151e940bc13SJeff Cody fifo_out=${QEMU_FIFO_OUT}_${_QEMU_HANDLE} 152e940bc13SJeff Cody fifo_in=${QEMU_FIFO_IN}_${_QEMU_HANDLE} 153e940bc13SJeff Cody mkfifo "${fifo_out}" 154e940bc13SJeff Cody mkfifo "${fifo_in}" 155e940bc13SJeff Cody 15615cfba69SMax Reitz if [ -z "$keep_stderr" ]; then 157f6c8c2e0SJeff Cody QEMU_NEED_PID='y'\ 158*3bb8ef4bSMax Reitz ${QEMU} -nographic -serial none ${comm} "${@}" >"${fifo_out}" \ 159d71a8b06SKevin Wolf 2>&1 \ 160e940bc13SJeff Cody <"${fifo_in}" & 16115cfba69SMax Reitz elif [ "$keep_stderr" = "y" ]; then 16215cfba69SMax Reitz QEMU_NEED_PID='y'\ 163*3bb8ef4bSMax Reitz ${QEMU} -nographic -serial none ${comm} "${@}" >"${fifo_out}" \ 16415cfba69SMax Reitz <"${fifo_in}" & 16515cfba69SMax Reitz else 16615cfba69SMax Reitz exit 1 16715cfba69SMax Reitz fi 168e940bc13SJeff Cody 169e940bc13SJeff Cody if [[ "${BASH_VERSINFO[0]}" -ge "5" || 170e940bc13SJeff Cody ("${BASH_VERSINFO[0]}" -ge "4" && "${BASH_VERSINFO[1]}" -ge "1") ]] 171e940bc13SJeff Cody then 172e940bc13SJeff Cody # bash >= 4.1 required for automatic fd 173e940bc13SJeff Cody exec {_out_fd}<"${fifo_out}" 174e940bc13SJeff Cody exec {_in_fd}>"${fifo_in}" 175e940bc13SJeff Cody else 176e940bc13SJeff Cody let _out_fd++ 177e940bc13SJeff Cody let _in_fd++ 178e940bc13SJeff Cody eval "exec ${_out_fd}<'${fifo_out}'" 179e940bc13SJeff Cody eval "exec ${_in_fd}>'${fifo_in}'" 180e940bc13SJeff Cody fi 181e940bc13SJeff Cody 182e940bc13SJeff Cody QEMU_OUT[${_QEMU_HANDLE}]=${_out_fd} 183e940bc13SJeff Cody QEMU_IN[${_QEMU_HANDLE}]=${_in_fd} 184e940bc13SJeff Cody QEMU_STATUS[${_QEMU_HANDLE}]=0 185e940bc13SJeff Cody 186e940bc13SJeff Cody if [ "${qemu_comm_method}" == "qmp" ] 187e940bc13SJeff Cody then 188e940bc13SJeff Cody # Don't print response, since it has version information in it 189e940bc13SJeff Cody silent=yes _timed_wait_for ${_QEMU_HANDLE} "capabilities" 190e940bc13SJeff Cody fi 191e940bc13SJeff Cody QEMU_HANDLE=${_QEMU_HANDLE} 192e940bc13SJeff Cody let _QEMU_HANDLE++ 193e940bc13SJeff Cody} 194e940bc13SJeff Cody 195e940bc13SJeff Cody 196e940bc13SJeff Cody# Silenty kills the QEMU process 197ea82aa42SMax Reitz# 198ea82aa42SMax Reitz# If $wait is set to anything other than the empty string, the process will not 199ea82aa42SMax Reitz# be killed but only waited for, and any output will be forwarded to stdout. If 200ea82aa42SMax Reitz# $wait is empty, the process will be killed and all output will be suppressed. 201e940bc13SJeff Codyfunction _cleanup_qemu() 202e940bc13SJeff Cody{ 203e940bc13SJeff Cody # QEMU_PID[], QEMU_IN[], QEMU_OUT[] all use same indices 204e940bc13SJeff Cody for i in "${!QEMU_OUT[@]}" 205e940bc13SJeff Cody do 206f6c8c2e0SJeff Cody local QEMU_PID 207f6c8c2e0SJeff Cody if [ -f "${TEST_DIR}/qemu-${i}.pid" ]; then 208f6c8c2e0SJeff Cody read QEMU_PID < "${TEST_DIR}/qemu-${i}.pid" 209f6c8c2e0SJeff Cody rm -f "${TEST_DIR}/qemu-${i}.pid" 210f6c8c2e0SJeff Cody if [ -z "${wait}" ] && [ -n "${QEMU_PID}" ]; then 211f6c8c2e0SJeff Cody kill -KILL ${QEMU_PID} 2>/dev/null 212ea82aa42SMax Reitz fi 213f6c8c2e0SJeff Cody if [ -n "${QEMU_PID}" ]; then 214f6c8c2e0SJeff Cody wait ${QEMU_PID} 2>/dev/null # silent kill 215f6c8c2e0SJeff Cody fi 216f6c8c2e0SJeff Cody fi 217f6c8c2e0SJeff Cody 218ea82aa42SMax Reitz if [ -n "${wait}" ]; then 219ea82aa42SMax Reitz cat <&${QEMU_OUT[$i]} | _filter_testdir | _filter_qemu \ 220ea82aa42SMax Reitz | _filter_qemu_io | _filter_qmp 221ea82aa42SMax Reitz fi 222e940bc13SJeff Cody rm -f "${QEMU_FIFO_IN}_${i}" "${QEMU_FIFO_OUT}_${i}" 223e940bc13SJeff Cody eval "exec ${QEMU_IN[$i]}<&-" # close file descriptors 224e940bc13SJeff Cody eval "exec ${QEMU_OUT[$i]}<&-" 225e940bc13SJeff Cody done 226e940bc13SJeff Cody} 227