2
0

common.qemu 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. #!/usr/bin/env 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. QEMU_COMM_TIMEOUT=10
  28. QEMU_FIFO_IN="${QEMU_TEST_DIR}/qmp-in-$$"
  29. QEMU_FIFO_OUT="${QEMU_TEST_DIR}/qmp-out-$$"
  30. QEMU_HANDLE=0
  31. export _QEMU_HANDLE=0
  32. # If bash version is >= 4.1, these will be overwritten and dynamic
  33. # file descriptor values assigned.
  34. _out_fd=3
  35. _in_fd=4
  36. # Wait for expected QMP response from QEMU. Will time out
  37. # after 10 seconds, which counts as failure.
  38. #
  39. # Override QEMU_COMM_TIMEOUT for a timeout different than the
  40. # default 10 seconds
  41. #
  42. # $1: The handle to use
  43. # $2+ All remaining arguments comprise the string to search for
  44. # in the response.
  45. #
  46. # If $silent is set to anything but an empty string, then
  47. # response is not echoed out.
  48. # If $mismatch_only is set, only non-matching responses will
  49. # be echoed.
  50. #
  51. # If $capture_events is non-empty, then any QMP event names it lists
  52. # will not be echoed out, but instead collected in the $QEMU_EVENTS
  53. # variable. The _wait_event function can later be used to receive
  54. # the cached events.
  55. #
  56. # If $only_capture_events is set to anything but an empty string,
  57. # then an error will be raised if a QMP message is seen which is
  58. # not an event listed in $capture_events.
  59. #
  60. # If $success_or_failure is set, the meaning of the arguments is
  61. # changed as follows:
  62. # $2: A string to search for in the response; if found, this indicates
  63. # success and ${QEMU_STATUS[$1]} is set to 0.
  64. # $3: A string to search for in the response; if found, this indicates
  65. # failure and the test is either aborted (if $qemu_error_no_exit
  66. # is not set) or ${QEMU_STATUS[$1]} is set to -1 (otherwise).
  67. _timed_wait_for()
  68. {
  69. local h=${1}
  70. shift
  71. if [ -z "${success_or_failure}" ]; then
  72. success_match=${*}
  73. failure_match=
  74. else
  75. success_match=${1}
  76. failure_match=${2}
  77. fi
  78. timeout=yes
  79. QEMU_STATUS[$h]=0
  80. read_timeout="-t ${QEMU_COMM_TIMEOUT}"
  81. if [ -n "${GDB_OPTIONS}" ]; then
  82. read_timeout=
  83. fi
  84. while IFS= read ${read_timeout} resp <&${QEMU_OUT[$h]}
  85. do
  86. if [ -n "$capture_events" ]; then
  87. capture=0
  88. local evname
  89. for evname in $capture_events
  90. do
  91. case ${resp} in
  92. *\"event\":\ \"${evname}\"* ) capture=1 ;;
  93. esac
  94. done
  95. if [ $capture = 1 ];
  96. then
  97. ev=$(echo "${resp}" | tr -d '\r' | tr % .)
  98. QEMU_EVENTS="${QEMU_EVENTS:+${QEMU_EVENTS}%}${ev}"
  99. if [ -n "$only_capture_events" ]; then
  100. return
  101. else
  102. continue
  103. fi
  104. fi
  105. fi
  106. if [ -n "$only_capture_events" ]; then
  107. echo "Only expected $capture_events but got ${resp}"
  108. exit 1
  109. fi
  110. if [ -z "${silent}" ] && [ -z "${mismatch_only}" ]; then
  111. echo "${resp}" | _filter_testdir | _filter_qemu \
  112. | _filter_qemu_io | _filter_qmp | _filter_hmp
  113. fi
  114. if [ -n "${failure_match}" ]; then
  115. grep -q "${failure_match}" < <(echo "${resp}")
  116. if [ $? -eq 0 ]; then
  117. timeout=
  118. break
  119. fi
  120. fi
  121. grep -q "${success_match}" < <(echo "${resp}")
  122. if [ $? -eq 0 ]; then
  123. return
  124. fi
  125. if [ -z "${silent}" ] && [ -n "${mismatch_only}" ]; then
  126. echo "${resp}" | _filter_testdir | _filter_qemu \
  127. | _filter_qemu_io | _filter_qmp | _filter_hmp
  128. fi
  129. done
  130. QEMU_STATUS[$h]=-1
  131. if [ -z "${qemu_error_no_exit}" ]; then
  132. if [ -n "${timeout}" ]; then
  133. echo "Timeout waiting for ${success_match} on handle ${h}"
  134. else
  135. echo "Wrong response matching ${failure_match} on handle ${h}"
  136. fi
  137. exit 1 # Timeout or wrong match mean the test failed
  138. fi
  139. }
  140. # Sends QMP or HMP command to QEMU, and waits for the expected response
  141. #
  142. # $1: QEMU handle to use
  143. # $2: String of the QMP command to send
  144. # ${@: -1} (Last string passed)
  145. # String that the QEMU response should contain. If it is a null
  146. # string, do not wait for a response
  147. #
  148. # Set qemu_cmd_repeat to the number of times to repeat the cmd
  149. # until either timeout, or a response. If it is not set, or <=0,
  150. # then the command is only sent once.
  151. #
  152. # If neither $silent nor $mismatch_only is set, and $cmd begins with '{',
  153. # echo the command before sending it the first time.
  154. #
  155. # If $qemu_error_no_exit is set, then even if the expected response
  156. # is not seen, we will not exit. $QEMU_STATUS[$1] will be set it -1 in
  157. # that case.
  158. #
  159. # If $success_or_failure is set, then the last two strings are the
  160. # strings the response will be scanned for. The first of the two
  161. # indicates success, the latter indicates failure. Failure is handled
  162. # like a timeout.
  163. _send_qemu_cmd()
  164. {
  165. local h=${1}
  166. local count=1
  167. local cmd=
  168. local use_error=${qemu_error_no_exit}
  169. shift
  170. if [ ${qemu_cmd_repeat} -gt 0 ] 2>/dev/null; then
  171. count=${qemu_cmd_repeat}
  172. use_error="no"
  173. fi
  174. cmd=$1
  175. shift
  176. # Display QMP being sent, but not HMP (since HMP already echoes its
  177. # input back to output); decide based on leading '{'
  178. if [ -z "$silent" ] && [ -z "$mismatch_only" ] &&
  179. [ "$cmd" != "${cmd#\{}" ]; then
  180. echo "${cmd}" | _filter_testdir | _filter_imgfmt
  181. fi
  182. while [ ${count} -gt 0 ]
  183. do
  184. echo "${cmd}" >&${QEMU_IN[${h}]}
  185. if [ -n "${1}" ]; then
  186. if [ -z "${success_or_failure}" ]; then
  187. qemu_error_no_exit=${use_error} _timed_wait_for ${h} "${1}"
  188. else
  189. qemu_error_no_exit=${use_error} _timed_wait_for ${h} "${1}" "${2}"
  190. fi
  191. if [ ${QEMU_STATUS[$h]} -eq 0 ]; then
  192. return
  193. fi
  194. fi
  195. let count--;
  196. done
  197. if [ ${QEMU_STATUS[$h]} -ne 0 ] && [ -z "${qemu_error_no_exit}" ]; then
  198. echo "Timeout waiting for command ${1} response on handle ${h}"
  199. exit 1 #Timeout means the test failed
  200. fi
  201. }
  202. # Check event cache for a named QMP event
  203. #
  204. # Input parameters:
  205. # $1: Name of the QMP event to check for
  206. #
  207. # Checks if the named QMP event that was previously captured
  208. # into $QEMU_EVENTS. When matched, the QMP event will be echoed
  209. # and the $matched variable set to 1.
  210. #
  211. # _wait_event is more suitable for test usage in most cases
  212. _check_cached_events()
  213. {
  214. local evname=${1}
  215. local match="\"event\": \"$evname\""
  216. matched=0
  217. if [ -n "$QEMU_EVENTS" ]; then
  218. CURRENT_QEMU_EVENTS=$QEMU_EVENTS
  219. QEMU_EVENTS=
  220. old_IFS=$IFS
  221. IFS="%"
  222. for ev in $CURRENT_QEMU_EVENTS
  223. do
  224. grep -q "$match" < <(echo "${ev}")
  225. if [ $? -eq 0 ] && [ $matched = 0 ]; then
  226. echo "${ev}" | _filter_testdir | _filter_qemu \
  227. | _filter_qemu_io | _filter_qmp | _filter_hmp
  228. matched=1
  229. else
  230. QEMU_EVENTS="${QEMU_EVENTS:+${QEMU_EVENTS}%}${ev}"
  231. fi
  232. done
  233. IFS=$old_IFS
  234. fi
  235. }
  236. # Wait for a named QMP event
  237. #
  238. # Input parameters:
  239. # $1: QEMU handle to use
  240. # $2: Name of the QMP event to wait for
  241. #
  242. # Checks if the named QMP even was previously captured
  243. # into $QEMU_EVENTS. If none are present, then waits for the
  244. # event to arrive on the QMP channel. When matched, the QMP
  245. # event will be echoed
  246. _wait_event()
  247. {
  248. local h=${1}
  249. local evname=${2}
  250. while true
  251. do
  252. _check_cached_events $evname
  253. if [ $matched = 1 ];
  254. then
  255. return
  256. fi
  257. only_capture_events=1 qemu_error_no_exit=1 _timed_wait_for ${h}
  258. if [ ${QEMU_STATUS[$h]} -ne 0 ] ; then
  259. echo "Timeout waiting for event ${evname} on handle ${h}"
  260. exit 1 #Timeout means the test failed
  261. fi
  262. done
  263. }
  264. # Launch a QEMU process.
  265. #
  266. # Input parameters:
  267. # $qemu_comm_method: set this variable to 'monitor' (case insensitive)
  268. # to use the QEMU HMP monitor for communication.
  269. # Otherwise, the default of QMP is used.
  270. # $qmp_pretty: Set this variable to 'y' to enable QMP pretty printing.
  271. # $keep_stderr: Set this variable to 'y' to keep QEMU's stderr output on stderr.
  272. # If this variable is empty, stderr will be redirected to stdout.
  273. # Returns:
  274. # $QEMU_HANDLE: set to a handle value to communicate with this QEMU instance.
  275. #
  276. _launch_qemu()
  277. {
  278. local comm=
  279. local fifo_out=
  280. local fifo_in=
  281. if (shopt -s nocasematch; [[ "${qemu_comm_method}" == "monitor" ]])
  282. then
  283. comm="-monitor stdio"
  284. else
  285. local qemu_comm_method="qmp"
  286. if [ "$qmp_pretty" = "y" ]; then
  287. comm="-monitor none -qmp-pretty stdio"
  288. else
  289. comm="-monitor none -qmp stdio"
  290. fi
  291. fi
  292. fifo_out=${QEMU_FIFO_OUT}_${_QEMU_HANDLE}
  293. fifo_in=${QEMU_FIFO_IN}_${_QEMU_HANDLE}
  294. mkfifo "${fifo_out}"
  295. mkfifo "${fifo_in}"
  296. object_options=
  297. if [ -n "$IMGKEYSECRET" ]; then
  298. object_options="--object secret,id=keysec0,data=$IMGKEYSECRET"
  299. fi
  300. if [ -z "$keep_stderr" ]; then
  301. QEMU_NEED_PID='y'\
  302. ${QEMU} ${object_options} -nographic -serial none ${comm} "${@}" >"${fifo_out}" \
  303. 2>&1 \
  304. <"${fifo_in}" &
  305. elif [ "$keep_stderr" = "y" ]; then
  306. QEMU_NEED_PID='y'\
  307. ${QEMU} ${object_options} -nographic -serial none ${comm} "${@}" >"${fifo_out}" \
  308. <"${fifo_in}" &
  309. else
  310. exit 1
  311. fi
  312. if [[ "${BASH_VERSINFO[0]}" -ge "5" ||
  313. ("${BASH_VERSINFO[0]}" -ge "4" && "${BASH_VERSINFO[1]}" -ge "1") ]]
  314. then
  315. # bash >= 4.1 required for automatic fd
  316. exec {_out_fd}<"${fifo_out}"
  317. exec {_in_fd}>"${fifo_in}"
  318. else
  319. let _out_fd++
  320. let _in_fd++
  321. eval "exec ${_out_fd}<'${fifo_out}'"
  322. eval "exec ${_in_fd}>'${fifo_in}'"
  323. fi
  324. QEMU_OUT[${_QEMU_HANDLE}]=${_out_fd}
  325. QEMU_IN[${_QEMU_HANDLE}]=${_in_fd}
  326. QEMU_STATUS[${_QEMU_HANDLE}]=0
  327. if [ "${qemu_comm_method}" == "qmp" ]
  328. then
  329. # Don't print response, since it has version information in it
  330. silent=yes _timed_wait_for ${_QEMU_HANDLE} "capabilities"
  331. if [ "$qmp_pretty" = "y" ]; then
  332. silent=yes _timed_wait_for ${_QEMU_HANDLE} "^}"
  333. fi
  334. fi
  335. QEMU_HANDLE=${_QEMU_HANDLE}
  336. let _QEMU_HANDLE++
  337. }
  338. # Silently kills the QEMU process
  339. #
  340. # If $wait is set to anything other than the empty string, the process will not
  341. # be killed but only waited for, and any output will be forwarded to stdout. If
  342. # $wait is empty, the process will be killed and all output will be suppressed.
  343. _cleanup_qemu()
  344. {
  345. # QEMU_PID[], QEMU_IN[], QEMU_OUT[] all use same indices
  346. for i in "${!QEMU_OUT[@]}"
  347. do
  348. local QEMU_PID
  349. if [ -f "${QEMU_TEST_DIR}/qemu-${i}.pid" ]; then
  350. read QEMU_PID < "${QEMU_TEST_DIR}/qemu-${i}.pid"
  351. rm -f "${QEMU_TEST_DIR}/qemu-${i}.pid"
  352. if [ -z "${wait}" ] && [ -n "${QEMU_PID}" ]; then
  353. kill -KILL ${QEMU_PID} 2>/dev/null
  354. fi
  355. if [ -n "${QEMU_PID}" ]; then
  356. wait ${QEMU_PID} 2>/dev/null # silent kill
  357. fi
  358. fi
  359. if [ -n "${wait}" ]; then
  360. cat <&${QEMU_OUT[$i]} | _filter_testdir | _filter_qemu \
  361. | _filter_qemu_io | _filter_qmp | _filter_hmp
  362. fi
  363. rm -f "${QEMU_FIFO_IN}_${i}" "${QEMU_FIFO_OUT}_${i}"
  364. eval "exec ${QEMU_IN[$i]}<&-" # close file descriptors
  365. eval "exec ${QEMU_OUT[$i]}<&-"
  366. unset QEMU_IN[$i]
  367. unset QEMU_OUT[$i]
  368. done
  369. }