common.rc 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. #!/usr/bin/env bash
  2. #
  3. # Copyright Red Hat
  4. # Copyright (c) 2000-2006 Silicon Graphics, Inc. All Rights Reserved.
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. export LANG=C
  20. PATH=".:$PATH"
  21. HOSTOS=$(uname -s)
  22. arch=$(uname -m)
  23. [[ "$arch" =~ "ppc64" ]] && qemu_arch=ppc64 || qemu_arch="$arch"
  24. # make sure we have a standard umask
  25. umask 022
  26. # bail out, setting up .notrun file
  27. _notrun()
  28. {
  29. echo "$*" >"$TEST_DIR/$seq.notrun"
  30. echo "$seq not run: $*"
  31. status=0
  32. exit
  33. }
  34. if ! command -v gsed >/dev/null 2>&1; then
  35. if sed --version 2>&1 | grep -v 'not GNU sed' | grep 'GNU sed' > /dev/null;
  36. then
  37. gsed()
  38. {
  39. sed "$@"
  40. }
  41. else
  42. gsed()
  43. {
  44. _notrun "GNU sed not available"
  45. }
  46. fi
  47. fi
  48. dd()
  49. {
  50. if [ "$HOSTOS" == "Linux" ]
  51. then
  52. command dd --help | grep noxfer > /dev/null 2>&1
  53. if [ "$?" -eq 0 ]
  54. then
  55. command dd status=noxfer $@
  56. else
  57. command dd $@
  58. fi
  59. else
  60. command dd $@
  61. fi
  62. }
  63. # poke_file 'test.img' 512 '\xff\xfe'
  64. poke_file()
  65. {
  66. printf "$3" | dd "of=$1" bs=1 "seek=$2" conv=notrunc &>/dev/null
  67. }
  68. # poke_file_le $img_filename $offset $byte_width $value
  69. # Example: poke_file_le "$TEST_IMG" 512 2 65534
  70. poke_file_le()
  71. {
  72. local img=$1 ofs=$2 len=$3 val=$4 str=''
  73. while ((len--)); do
  74. str+=$(printf '\\x%02x' $((val & 0xff)))
  75. val=$((val >> 8))
  76. done
  77. poke_file "$img" "$ofs" "$str"
  78. }
  79. # poke_file_be $img_filename $offset $byte_width $value
  80. # Example: poke_file_be "$TEST_IMG" 512 2 65279
  81. poke_file_be()
  82. {
  83. local img=$1 ofs=$2 len=$3 val=$4
  84. local str=$(printf "%0$((len * 2))x\n" $val | sed 's/\(..\)/\\x\1/g')
  85. poke_file "$img" "$ofs" "$str"
  86. }
  87. # peek_file_le 'test.img' 512 2 => 65534
  88. peek_file_le()
  89. {
  90. local val=0 shift=0 byte
  91. # coreutils' od --endian is not portable, so manually assemble bytes.
  92. for byte in $(od -j"$2" -N"$3" -An -v -tu1 "$1"); do
  93. val=$(( val | (byte << shift) ))
  94. shift=$((shift + 8))
  95. done
  96. printf %llu $val
  97. }
  98. # peek_file_be 'test.img' 512 2 => 65279
  99. peek_file_be()
  100. {
  101. local val=0 byte
  102. # coreutils' od --endian is not portable, so manually assemble bytes.
  103. for byte in $(od -j"$2" -N"$3" -An -v -tu1 "$1"); do
  104. val=$(( (val << 8) | byte ))
  105. done
  106. printf %llu $val
  107. }
  108. # peek_file_raw 'test.img' 512 2 => '\xff\xfe'. Do not use if the raw data
  109. # is likely to contain \0 or trailing \n.
  110. peek_file_raw()
  111. {
  112. dd if="$1" bs=1 skip="$2" count="$3" status=none
  113. }
  114. _optstr_add()
  115. {
  116. if [ -n "$1" ]; then
  117. echo "$1,$2"
  118. else
  119. echo "$2"
  120. fi
  121. }
  122. # Set the variables to the empty string to turn Valgrind off
  123. # for specific processes, e.g.
  124. # $ VALGRIND_QEMU_IO= ./check -qcow2 -valgrind 015
  125. : ${VALGRIND_QEMU_VM=$VALGRIND_QEMU}
  126. : ${VALGRIND_QEMU_IMG=$VALGRIND_QEMU}
  127. : ${VALGRIND_QEMU_IO=$VALGRIND_QEMU}
  128. : ${VALGRIND_QEMU_NBD=$VALGRIND_QEMU}
  129. : ${VALGRIND_QSD=$VALGRIND_QEMU}
  130. # The Valgrind own parameters may be set with
  131. # its environment variable VALGRIND_OPTS, e.g.
  132. # $ VALGRIND_OPTS="--leak-check=yes" ./check -qcow2 -valgrind 015
  133. _qemu_proc_exec()
  134. {
  135. local VALGRIND_LOGFILE="$1"
  136. shift
  137. if [[ "${VALGRIND_QEMU}" == "y" && "${NO_VALGRIND}" != "y" ]]; then
  138. exec valgrind --log-file="${VALGRIND_LOGFILE}" --error-exitcode=99 "$@"
  139. else
  140. exec "$@"
  141. fi
  142. }
  143. _qemu_proc_valgrind_log()
  144. {
  145. local VALGRIND_LOGFILE="$1"
  146. local RETVAL="$2"
  147. if [[ "${VALGRIND_QEMU}" == "y" && "${NO_VALGRIND}" != "y" ]]; then
  148. if [ $RETVAL == 99 ]; then
  149. cat "${VALGRIND_LOGFILE}"
  150. fi
  151. rm -f "${VALGRIND_LOGFILE}"
  152. fi
  153. }
  154. _qemu_wrapper()
  155. {
  156. local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind
  157. (
  158. if [ -n "${QEMU_NEED_PID}" ]; then
  159. echo $BASHPID > "${QEMU_TEST_DIR}/qemu-${_QEMU_HANDLE}.pid"
  160. fi
  161. GDB=""
  162. if [ -n "${GDB_OPTIONS}" ]; then
  163. GDB="gdbserver ${GDB_OPTIONS}"
  164. fi
  165. VALGRIND_QEMU="${VALGRIND_QEMU_VM}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \
  166. $GDB "$QEMU_PROG" $QEMU_OPTIONS "$@"
  167. )
  168. RETVAL=$?
  169. _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
  170. return $RETVAL
  171. }
  172. _qemu_img_wrapper()
  173. {
  174. local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind
  175. (
  176. VALGRIND_QEMU="${VALGRIND_QEMU_IMG}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \
  177. "$QEMU_IMG_PROG" $QEMU_IMG_OPTIONS "$@"
  178. )
  179. RETVAL=$?
  180. _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
  181. return $RETVAL
  182. }
  183. _qemu_io_wrapper()
  184. {
  185. local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind
  186. local QEMU_IO_ARGS="$QEMU_IO_OPTIONS"
  187. if [ "$IMGOPTSSYNTAX" = "true" ]; then
  188. QEMU_IO_ARGS="--image-opts $QEMU_IO_ARGS"
  189. if [ -n "$IMGKEYSECRET" ]; then
  190. QEMU_IO_ARGS="--object secret,id=keysec0,data=$IMGKEYSECRET $QEMU_IO_ARGS"
  191. fi
  192. fi
  193. (
  194. VALGRIND_QEMU="${VALGRIND_QEMU_IO}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \
  195. "$QEMU_IO_PROG" $QEMU_IO_ARGS "$@"
  196. )
  197. RETVAL=$?
  198. _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
  199. return $RETVAL
  200. }
  201. _qemu_nbd_wrapper()
  202. {
  203. local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind
  204. (
  205. VALGRIND_QEMU="${VALGRIND_QEMU_NBD}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \
  206. "$QEMU_NBD_PROG" --pid-file="${QEMU_TEST_DIR}/qemu-nbd.pid" \
  207. $QEMU_NBD_OPTIONS "$@"
  208. )
  209. RETVAL=$?
  210. _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
  211. return $RETVAL
  212. }
  213. _qemu_storage_daemon_wrapper()
  214. {
  215. local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind
  216. (
  217. if [ -n "${QSD_NEED_PID}" ]; then
  218. echo $BASHPID > "${QEMU_TEST_DIR}/qemu-storage-daemon.pid"
  219. fi
  220. VALGRIND_QEMU="${VALGRIND_QSD}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \
  221. "$QSD_PROG" $QSD_OPTIONS "$@"
  222. )
  223. RETVAL=$?
  224. _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
  225. return $RETVAL
  226. }
  227. # Valgrind bug #409141 https://bugs.kde.org/show_bug.cgi?id=409141
  228. # Until valgrind 3.16+ is ubiquitous, we must work around a hang in
  229. # valgrind when issuing sigkill. Disable valgrind for this invocation.
  230. _NO_VALGRIND()
  231. {
  232. NO_VALGRIND="y" "$@"
  233. }
  234. export QEMU=_qemu_wrapper
  235. export QEMU_IMG=_qemu_img_wrapper
  236. export QEMU_IO=_qemu_io_wrapper
  237. export QEMU_NBD=_qemu_nbd_wrapper
  238. export QSD=_qemu_storage_daemon_wrapper
  239. if [ "$IMGOPTSSYNTAX" = "true" ]; then
  240. DRIVER="driver=$IMGFMT"
  241. QEMU_IMG_EXTRA_ARGS="--image-opts $QEMU_IMG_EXTRA_ARGS"
  242. if [ -n "$IMGKEYSECRET" ]; then
  243. QEMU_IMG_EXTRA_ARGS="--object secret,id=keysec0,data=$IMGKEYSECRET $QEMU_IMG_EXTRA_ARGS"
  244. fi
  245. if [ "$IMGFMT" = "luks" ]; then
  246. DRIVER="$DRIVER,key-secret=keysec0"
  247. fi
  248. if [ "$IMGPROTO" = "file" ]; then
  249. TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
  250. TEST_IMG="$DRIVER,file.filename=$TEST_DIR/t.$IMGFMT"
  251. elif [ "$IMGPROTO" = "nbd" ]; then
  252. TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
  253. TEST_IMG="$DRIVER,file.driver=nbd,file.type=unix"
  254. TEST_IMG="$TEST_IMG,file.path=$SOCK_DIR/nbd"
  255. elif [ "$IMGPROTO" = "fuse" ]; then
  256. TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
  257. TEST_IMG="$DRIVER,file.filename=$SOCK_DIR/fuse-t.$IMGFMT"
  258. elif [ "$IMGPROTO" = "ssh" ]; then
  259. TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
  260. TEST_IMG="$DRIVER,file.driver=ssh,file.host=127.0.0.1,file.path=$TEST_IMG_FILE"
  261. elif [ "$IMGPROTO" = "nfs" ]; then
  262. TEST_DIR="$DRIVER,file.driver=nfs,file.filename=nfs://127.0.0.1/$TEST_DIR"
  263. TEST_IMG=$TEST_DIR/t.$IMGFMT
  264. else
  265. TEST_IMG="$DRIVER,file.driver=$IMGPROTO,file.filename=$TEST_DIR/t.$IMGFMT"
  266. fi
  267. else
  268. QEMU_IMG_EXTRA_ARGS=
  269. if [ "$IMGPROTO" = "file" ]; then
  270. TEST_IMG=$TEST_DIR/t.$IMGFMT
  271. elif [ "$IMGPROTO" = "nbd" ]; then
  272. TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
  273. TEST_IMG="nbd+unix:///?socket=$SOCK_DIR/nbd"
  274. elif [ "$IMGPROTO" = "fuse" ]; then
  275. TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
  276. TEST_IMG="$SOCK_DIR/fuse-t.$IMGFMT"
  277. elif [ "$IMGPROTO" = "ssh" ]; then
  278. TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
  279. REMOTE_TEST_DIR="ssh://\\($USER@\\)\\?127.0.0.1\\(:[0-9]\\+\\)\\?$TEST_DIR"
  280. TEST_IMG="ssh://127.0.0.1$TEST_IMG_FILE"
  281. elif [ "$IMGPROTO" = "nfs" ]; then
  282. TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
  283. REMOTE_TEST_DIR="nfs://127.0.0.1$TEST_DIR"
  284. TEST_IMG="nfs://127.0.0.1$TEST_IMG_FILE"
  285. else
  286. TEST_IMG=$IMGPROTO:$TEST_DIR/t.$IMGFMT
  287. fi
  288. fi
  289. ORIG_TEST_IMG_FILE=$TEST_IMG_FILE
  290. ORIG_TEST_IMG="$TEST_IMG"
  291. FUSE_PIDS=()
  292. FUSE_EXPORTS=()
  293. if [ -z "$TEST_DIR" ]; then
  294. TEST_DIR=$PWD/scratch
  295. fi
  296. QEMU_TEST_DIR="${TEST_DIR}"
  297. if [ ! -e "$TEST_DIR" ]; then
  298. mkdir "$TEST_DIR"
  299. fi
  300. if [ ! -d "$TEST_DIR" ]; then
  301. echo "common.rc: Error: \$TEST_DIR ($TEST_DIR) is not a directory"
  302. exit 1
  303. fi
  304. if [ -z "$REMOTE_TEST_DIR" ]; then
  305. REMOTE_TEST_DIR="$TEST_DIR"
  306. fi
  307. if [ ! -d "$SAMPLE_IMG_DIR" ]; then
  308. echo "common.rc: Error: \$SAMPLE_IMG_DIR ($SAMPLE_IMG_DIR) is not a directory"
  309. exit 1
  310. fi
  311. _use_sample_img()
  312. {
  313. SAMPLE_IMG_FILE="${1%\.bz2}"
  314. TEST_IMG="$TEST_DIR/$SAMPLE_IMG_FILE"
  315. bzcat "$SAMPLE_IMG_DIR/$1" > "$TEST_IMG"
  316. if [ $? -ne 0 ]
  317. then
  318. echo "_use_sample_img error, cannot extract '$SAMPLE_IMG_DIR/$1'"
  319. exit 1
  320. fi
  321. }
  322. _stop_nbd_server()
  323. {
  324. if [ -f "${QEMU_TEST_DIR}/qemu-nbd.pid" ]; then
  325. local QEMU_NBD_PID
  326. read QEMU_NBD_PID < "${QEMU_TEST_DIR}/qemu-nbd.pid"
  327. kill ${QEMU_NBD_PID}
  328. rm -f "${QEMU_TEST_DIR}/qemu-nbd.pid" "$SOCK_DIR/nbd"
  329. fi
  330. }
  331. # Gets the data_file value from IMGOPTS and replaces the '$TEST_IMG'
  332. # pattern by '$1'
  333. # Caution: The replacement is done with sed, so $1 must be escaped
  334. # properly. (The delimiter is '#'.)
  335. _get_data_file()
  336. {
  337. if ! echo "$IMGOPTS" | grep -q 'data_file='; then
  338. return 1
  339. fi
  340. echo "$IMGOPTS" | sed -e 's/.*data_file=\([^,]*\).*/\1/' \
  341. | sed -e "s#\\\$TEST_IMG#$1#"
  342. }
  343. # Translate a $TEST_IMG to its corresponding $TEST_IMG_FILE for
  344. # different protocols
  345. _test_img_to_test_img_file()
  346. {
  347. case "$IMGPROTO" in
  348. file)
  349. echo "$1"
  350. ;;
  351. fuse)
  352. echo "$1" | sed -e "s#$SOCK_DIR/fuse-#$TEST_DIR/#"
  353. ;;
  354. nfs)
  355. echo "$1" | sed -e "s#nfs://127.0.0.1##"
  356. ;;
  357. ssh)
  358. echo "$1" | \
  359. sed -e "s#ssh://\\($USER@\\)\\?127.0.0.1\\(:[0-9]\\+\\)\\?##"
  360. ;;
  361. *)
  362. return 1
  363. ;;
  364. esac
  365. }
  366. _make_test_img()
  367. {
  368. # extra qemu-img options can be added by tests
  369. # at least one argument (the image size) needs to be added
  370. local extra_img_options=""
  371. local optstr=""
  372. local img_name=""
  373. local use_backing=0
  374. local backing_file=""
  375. local object_options=""
  376. local opts_param=false
  377. local misc_params=()
  378. if [[ $IMGPROTO == fuse && $TEST_IMG == $SOCK_DIR/fuse-* ]]; then
  379. # The caller may be trying to overwrite an existing image
  380. _rm_test_img "$TEST_IMG"
  381. fi
  382. if [ -z "$TEST_IMG_FILE" ]; then
  383. img_name=$TEST_IMG
  384. elif [ "$IMGOPTSSYNTAX" != "true" -a \
  385. "$TEST_IMG_FILE" = "$ORIG_TEST_IMG_FILE" ]; then
  386. # Handle cases of tests only updating TEST_IMG, but not TEST_IMG_FILE
  387. img_name=$(_test_img_to_test_img_file "$TEST_IMG")
  388. if [ "$?" != 0 ]; then
  389. img_name=$TEST_IMG_FILE
  390. fi
  391. else
  392. # $TEST_IMG_FILE is not the default value, so it definitely has been
  393. # modified by the test
  394. img_name=$TEST_IMG_FILE
  395. fi
  396. if [ -n "$IMGOPTS" ]; then
  397. imgopts_expanded=$(echo "$IMGOPTS" | sed -e "s#\\\$TEST_IMG#$img_name#")
  398. optstr=$(_optstr_add "$optstr" "$imgopts_expanded")
  399. fi
  400. if [ -n "$IMGKEYSECRET" ]; then
  401. object_options="--object secret,id=keysec0,data=$IMGKEYSECRET"
  402. optstr=$(_optstr_add "$optstr" "key-secret=keysec0")
  403. fi
  404. for param; do
  405. if [ "$use_backing" = "1" -a -z "$backing_file" ]; then
  406. backing_file=$param
  407. continue
  408. elif $opts_param; then
  409. optstr=$(_optstr_add "$optstr" "$param")
  410. opts_param=false
  411. continue
  412. fi
  413. case "$param" in
  414. -b)
  415. use_backing=1
  416. ;;
  417. -o)
  418. opts_param=true
  419. ;;
  420. --no-opts)
  421. optstr=""
  422. ;;
  423. *)
  424. misc_params=("${misc_params[@]}" "$param")
  425. ;;
  426. esac
  427. done
  428. if [ \( "$IMGFMT" = "qcow2" -o "$IMGFMT" = "qed" \) -a -n "$CLUSTER_SIZE" ]; then
  429. optstr=$(_optstr_add "$optstr" "cluster_size=$CLUSTER_SIZE")
  430. fi
  431. if [ -n "$optstr" ]; then
  432. extra_img_options="-o $optstr $extra_img_options"
  433. fi
  434. if [ $IMGPROTO = "nbd" ]; then
  435. _stop_nbd_server
  436. fi
  437. # XXX(hch): have global image options?
  438. (
  439. if [ $use_backing = 1 ]; then
  440. $QEMU_IMG create $object_options -f $IMGFMT $extra_img_options -b "$backing_file" "$img_name" "${misc_params[@]}" 2>&1
  441. else
  442. $QEMU_IMG create $object_options -f $IMGFMT $extra_img_options "$img_name" "${misc_params[@]}" 2>&1
  443. fi
  444. ) | _filter_img_create
  445. # Start an NBD server on the image file, which is what we'll be talking to.
  446. # Once NBD gains resize support, we may also want to use -f raw at the
  447. # server and interpret format over NBD, but for now, the format is
  448. # interpreted at the server and raw data sent over NBD.
  449. if [ $IMGPROTO = "nbd" ]; then
  450. # Pass a sufficiently high number to -e that should be enough for all
  451. # tests
  452. eval "$QEMU_NBD -v -t -k '$SOCK_DIR/nbd' -f $IMGFMT -e 42 -x '' $TEST_IMG_FILE >/dev/null &"
  453. sleep 1 # FIXME: qemu-nbd needs to be listening before we continue
  454. fi
  455. if [ $IMGPROTO = "fuse" -a -f "$img_name" ]; then
  456. local export_mp
  457. local pid
  458. local pidfile
  459. local timeout
  460. export_mp=$(echo "$img_name" | sed -e "s#$TEST_DIR/#$SOCK_DIR/fuse-#")
  461. if ! echo "$export_mp" | grep -q "^$SOCK_DIR"; then
  462. echo 'Cannot use FUSE exports with images outside of TEST_DIR' >&2
  463. return 1
  464. fi
  465. touch "$export_mp"
  466. rm -f "$SOCK_DIR/fuse-output"
  467. # Usually, users would export formatted nodes. But we present fuse as a
  468. # protocol-level driver here, so we have to leave the format to the
  469. # client.
  470. # Switch off allow-other, because in general we do not need it for
  471. # iotests. The default allow-other=auto has the downside of printing a
  472. # fusermount error on its first attempt if allow_other is not
  473. # permissible, which we would need to filter.
  474. QSD_NEED_PID=y $QSD \
  475. --blockdev file,node-name=export-node,filename=$img_name,discard=unmap \
  476. --export fuse,id=fuse-export,node-name=export-node,mountpoint="$export_mp",writable=on,growable=on,allow-other=off \
  477. &
  478. pidfile="$QEMU_TEST_DIR/qemu-storage-daemon.pid"
  479. # Wait for the PID file
  480. while [ ! -f "$pidfile" ]; do
  481. sleep 0.5
  482. done
  483. pid=$(cat "$pidfile")
  484. rm -f "$pidfile"
  485. FUSE_PIDS+=($pid)
  486. FUSE_EXPORTS+=("$export_mp")
  487. fi
  488. }
  489. _rm_test_img()
  490. {
  491. local img=$1
  492. if [[ $IMGPROTO == fuse && $img == $SOCK_DIR/fuse-* ]]; then
  493. # Drop a FUSE export
  494. local df_output
  495. local i
  496. local image_file
  497. local index=''
  498. local timeout
  499. for i in "${!FUSE_EXPORTS[@]}"; do
  500. if [ "${FUSE_EXPORTS[i]}" = "$img" ]; then
  501. index=$i
  502. break
  503. fi
  504. done
  505. if [ -z "$index" ]; then
  506. # Probably gone already
  507. return 0
  508. fi
  509. kill "${FUSE_PIDS[index]}"
  510. # Wait until the mount is gone
  511. timeout=10 # *0.5 s
  512. while true; do
  513. # Will show the mount point; if the mount is still there,
  514. # it will be $img.
  515. df_output=$(df "$img" 2>/dev/null)
  516. # But df may also show an error ("Transpoint endpoint not
  517. # connected"), so retry in such cases
  518. if [ -n "$df_output" ]; then
  519. if ! echo "$df_output" | grep -q "$img"; then
  520. break
  521. fi
  522. fi
  523. sleep 0.5
  524. timeout=$((timeout - 1))
  525. if [ "$timeout" = 0 ]; then
  526. echo 'Failed to take down FUSE export' >&2
  527. return 1
  528. fi
  529. done
  530. rm -f "$img"
  531. unset "FUSE_PIDS[$index]"
  532. unset "FUSE_EXPORTS[$index]"
  533. image_file=$(echo "$img" | sed -e "s#$SOCK_DIR/fuse-#$TEST_DIR/#")
  534. _rm_test_img "$image_file"
  535. return
  536. fi
  537. if [ "$IMGFMT" = "vmdk" ]; then
  538. # Remove all the extents for vmdk
  539. "$QEMU_IMG" info "$img" 2>/dev/null | grep 'filename:' | cut -f 2 -d: \
  540. | xargs -I {} rm -f "{}"
  541. elif [ "$IMGFMT" = "qcow2" ]; then
  542. # Remove external data file
  543. if data_file=$(_get_data_file "$img"); then
  544. rm -f "$data_file"
  545. fi
  546. fi
  547. rm -f "$img"
  548. }
  549. _cleanup_test_img()
  550. {
  551. case "$IMGPROTO" in
  552. nbd)
  553. _stop_nbd_server
  554. rm -f "$TEST_IMG_FILE"
  555. ;;
  556. fuse)
  557. local mp
  558. for mp in "${FUSE_EXPORTS[@]}"; do
  559. _rm_test_img "$mp"
  560. done
  561. FUSE_PIDS=()
  562. FUSE_EXPORTS=()
  563. ;;
  564. file)
  565. _rm_test_img "$TEST_DIR/t.$IMGFMT"
  566. _rm_test_img "$TEST_DIR/t.$IMGFMT.orig"
  567. _rm_test_img "$TEST_DIR/t.$IMGFMT.base"
  568. if [ -n "$SAMPLE_IMG_FILE" ]
  569. then
  570. rm -f "$TEST_DIR/$SAMPLE_IMG_FILE"
  571. SAMPLE_IMG_FILE=
  572. TEST_IMG="$ORIG_TEST_IMG"
  573. fi
  574. ;;
  575. rbd)
  576. rbd --no-progress rm "$TEST_DIR/t.$IMGFMT" > /dev/null
  577. ;;
  578. esac
  579. }
  580. _check_test_img()
  581. {
  582. (
  583. if [ "$IMGOPTSSYNTAX" = "true" ]; then
  584. $QEMU_IMG check $QEMU_IMG_EXTRA_ARGS "$@" "$TEST_IMG" 2>&1
  585. else
  586. $QEMU_IMG check "$@" -f $IMGFMT "$TEST_IMG" 2>&1
  587. fi
  588. ) | _filter_testdir | _filter_qemu_img_check
  589. # return real qemu_img check status, to analyze in
  590. # _check_test_img_ignore_leaks
  591. return ${PIPESTATUS[0]}
  592. }
  593. _check_test_img_ignore_leaks()
  594. {
  595. out=$(_check_test_img "$@")
  596. status=$?
  597. if [ $status = 3 ]; then
  598. # This must correspond to success output in dump_human_image_check()
  599. echo "No errors were found on the image."
  600. return 0
  601. fi
  602. echo "$out"
  603. return $status
  604. }
  605. _img_info()
  606. {
  607. if [[ "$1" == "--format-specific" ]]; then
  608. local format_specific=1
  609. shift
  610. else
  611. local format_specific=0
  612. fi
  613. discard=0
  614. regex_json_spec_start='^ *"format-specific": \{'
  615. regex_json_child_start='^ *"children": \['
  616. $QEMU_IMG info $QEMU_IMG_EXTRA_ARGS "$@" "$TEST_IMG" 2>&1 | \
  617. sed -e "s#$REMOTE_TEST_DIR#TEST_DIR#g" \
  618. -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \
  619. -e "s#$TEST_DIR#TEST_DIR#g" \
  620. -e "s#$SOCK_DIR/fuse-#TEST_DIR/#g" \
  621. -e "s#$SOCK_DIR/#SOCK_DIR/#g" \
  622. -e "s#$IMGFMT#IMGFMT#g" \
  623. -e 's/\(compression type: \)\(zlib\|zstd\)/\1COMPRESSION_TYPE/' \
  624. -e "/^disk size:/ D" \
  625. -e "/actual-size/ D" | \
  626. while IFS='' read -r line; do
  627. if [[ $discard == 0 ]]; then
  628. if [[ $format_specific == 0 && $line == "Format specific information:" ]]; then
  629. discard=1
  630. elif [[ $line =~ "Child node '/" ]]; then
  631. discard=1
  632. elif [[ $format_specific == 0 && $line =~ $regex_json_spec_start ]]; then
  633. discard=2
  634. regex_json_end="^${line%%[^ ]*}\\},? *$"
  635. elif [[ $line =~ $regex_json_child_start ]]; then
  636. discard=2
  637. regex_json_end="^${line%%[^ ]*}\\],? *$"
  638. fi
  639. fi
  640. if [[ $discard == 0 ]]; then
  641. echo "$line"
  642. elif [[ $discard == 1 && ! $line ]]; then
  643. echo
  644. discard=0
  645. elif [[ $discard == 2 && $line =~ $regex_json_end ]]; then
  646. discard=0
  647. fi
  648. done
  649. }
  650. # bail out, setting up .casenotrun file
  651. # The function _casenotrun() is used as a notifier. It is the
  652. # caller's responsibility to make skipped a particular test.
  653. #
  654. _casenotrun()
  655. {
  656. echo " [case not run] $*" >>"$TEST_DIR/$seq.casenotrun"
  657. }
  658. # just plain bail out
  659. #
  660. _fail()
  661. {
  662. echo "$*" | tee -a "$TEST_DIR/$seq.full"
  663. echo "(see $seq.full for details)"
  664. status=1
  665. exit 1
  666. }
  667. # tests whether $IMGFMT is one of the supported image formats for a test
  668. #
  669. _supported_fmt()
  670. {
  671. # "generic" is suitable for most image formats. For some formats it doesn't
  672. # work, however (most notably read-only formats), so they can opt out by
  673. # setting IMGFMT_GENERIC to false.
  674. for f; do
  675. if [ "$f" = "$IMGFMT" -o "$f" = "generic" -a "$IMGFMT_GENERIC" = "true" ]; then
  676. if [ "$IMGFMT" = "luks" ]; then
  677. _require_working_luks
  678. fi
  679. return
  680. fi
  681. done
  682. _notrun "not suitable for this image format: $IMGFMT"
  683. }
  684. # tests whether $IMGFMT is one of the unsupported image format for a test
  685. #
  686. _unsupported_fmt()
  687. {
  688. for f; do
  689. if [ "$f" = "$IMGFMT" ]; then
  690. _notrun "not suitable for this image format: $IMGFMT"
  691. fi
  692. done
  693. }
  694. # tests whether $IMGPROTO is one of the supported image protocols for a test
  695. #
  696. _supported_proto()
  697. {
  698. for f; do
  699. if [ "$f" = "$IMGPROTO" -o "$f" = "generic" ]; then
  700. return
  701. fi
  702. done
  703. _notrun "not suitable for this image protocol: $IMGPROTO"
  704. }
  705. # tests whether $IMGPROTO is specified as an unsupported image protocol for a test
  706. #
  707. _unsupported_proto()
  708. {
  709. for f; do
  710. if [ "$f" = "$IMGPROTO" ]; then
  711. _notrun "not suitable for this image protocol: $IMGPROTO"
  712. return
  713. fi
  714. done
  715. }
  716. # tests whether the host OS is one of the supported OSes for a test
  717. #
  718. _supported_os()
  719. {
  720. for h
  721. do
  722. if [ "$h" = "$HOSTOS" ]
  723. then
  724. return
  725. fi
  726. done
  727. _notrun "not suitable for this OS: $HOSTOS"
  728. }
  729. _supported_cache_modes()
  730. {
  731. for mode; do
  732. if [ "$mode" = "$CACHEMODE" ]; then
  733. return
  734. fi
  735. done
  736. _notrun "not suitable for cache mode: $CACHEMODE"
  737. }
  738. # Check whether the filesystem supports O_DIRECT
  739. _check_o_direct()
  740. {
  741. testfile="$TEST_DIR"/_check_o_direct
  742. $QEMU_IMG create -f raw "$testfile" 1M > /dev/null
  743. out=$($QEMU_IO -f raw -t none -c quit "$testfile" 2>&1)
  744. rm -f "$testfile"
  745. [[ "$out" != *"O_DIRECT"* ]]
  746. }
  747. _require_o_direct()
  748. {
  749. if ! _check_o_direct; then
  750. _notrun "file system on $TEST_DIR does not support O_DIRECT"
  751. fi
  752. }
  753. _check_cache_mode()
  754. {
  755. if [ $CACHEMODE == "none" ] || [ $CACHEMODE == "directsync" ]; then
  756. _require_o_direct
  757. fi
  758. }
  759. _check_cache_mode
  760. # $1 - cache mode to use by default
  761. # $2 - (optional) cache mode to use by default if O_DIRECT is not supported
  762. _default_cache_mode()
  763. {
  764. if $CACHEMODE_IS_DEFAULT; then
  765. if [ -z "$2" ] || _check_o_direct; then
  766. CACHEMODE="$1"
  767. else
  768. CACHEMODE="$2"
  769. fi
  770. QEMU_IO="$QEMU_IO --cache $CACHEMODE"
  771. _check_cache_mode
  772. return
  773. fi
  774. }
  775. _supported_aio_modes()
  776. {
  777. for mode; do
  778. if [ "$mode" = "$AIOMODE" ]; then
  779. return
  780. fi
  781. done
  782. _notrun "not suitable for aio mode: $AIOMODE"
  783. }
  784. _default_aio_mode()
  785. {
  786. AIOMODE="$1"
  787. QEMU_IO="$QEMU_IO --aio $1"
  788. }
  789. _unsupported_imgopts()
  790. {
  791. for bad_opt
  792. do
  793. # Add a space so tests can match for whitespace that marks the
  794. # end of an option (\b or \> are not portable)
  795. if echo "$IMGOPTS " | grep -q 2>/dev/null "$bad_opt"
  796. then
  797. _notrun "not suitable for image option: $bad_opt"
  798. fi
  799. done
  800. }
  801. # Caution: Overwrites $TEST_DIR/t.luks
  802. _require_working_luks()
  803. {
  804. file="$TEST_DIR/t.luks"
  805. output=$(
  806. $QEMU_IMG create -f luks \
  807. --object secret,id=sec0,data=hunter0 \
  808. -o key-secret=sec0 \
  809. -o iter-time=10 \
  810. "$file" \
  811. 1M \
  812. 2>&1
  813. )
  814. status=$?
  815. IMGFMT='luks' _rm_test_img "$file"
  816. if [ $status != 0 ]; then
  817. reason=$(echo "$output" | grep "$file:" | sed -e "s#.*$file: *##")
  818. if [ -z "$reason" ]; then
  819. reason="Failed to create a LUKS image"
  820. fi
  821. _notrun "$reason"
  822. fi
  823. }
  824. # this test requires that a specified command (executable) exists
  825. #
  826. _require_command()
  827. {
  828. if [ "$1" = "QEMU" ]; then
  829. c=$QEMU_PROG
  830. elif [ "$1" = "QEMU_IMG" ]; then
  831. c=$QEMU_IMG_PROG
  832. elif [ "$1" = "QEMU_IO" ]; then
  833. c=$QEMU_IO_PROG
  834. elif [ "$1" = "QEMU_NBD" ]; then
  835. c=$QEMU_NBD_PROG
  836. else
  837. eval c=\$$1
  838. fi
  839. [ -x "$c" ] || _notrun "$1 utility required, skipped this test"
  840. }
  841. # Check that a set of drivers has been whitelisted in the QEMU binary
  842. #
  843. _require_drivers()
  844. {
  845. available=$($QEMU -drive format=help | \
  846. sed -e '/Supported formats:/!d' -e 's/Supported formats://')
  847. for driver
  848. do
  849. if ! echo "$available" | grep -q " $driver\( \|$\)"; then
  850. _notrun "$driver not available"
  851. fi
  852. done
  853. }
  854. # Check that we have a file system that allows huge (but very sparse) files
  855. #
  856. _require_large_file()
  857. {
  858. if [ -z "$TEST_IMG_FILE" ]; then
  859. FILENAME="$TEST_IMG"
  860. else
  861. FILENAME="$TEST_IMG_FILE"
  862. fi
  863. if ! truncate --size="$1" "$FILENAME"; then
  864. _notrun "file system on $TEST_DIR does not support large enough files"
  865. fi
  866. rm "$FILENAME"
  867. }
  868. # Check that a set of devices is available in the QEMU binary
  869. #
  870. _require_devices()
  871. {
  872. available=$($QEMU -M none -device help 2> /dev/null | \
  873. grep ^name | sed -e 's/^name "//' -e 's/".*$//')
  874. for device
  875. do
  876. if ! echo "$available" | grep -q "$device" ; then
  877. _notrun "$device not available"
  878. fi
  879. done
  880. }
  881. _require_one_device_of()
  882. {
  883. available=$($QEMU -M none -device help 2> /dev/null | \
  884. grep ^name | sed -e 's/^name "//' -e 's/".*$//')
  885. for device
  886. do
  887. if echo "$available" | grep -q "$device" ; then
  888. return
  889. fi
  890. done
  891. _notrun "$* not available"
  892. }
  893. _qcow2_dump_header()
  894. {
  895. if [[ "$1" == "--no-filter-compression" ]]; then
  896. local filter_compression=0
  897. shift
  898. else
  899. local filter_compression=1
  900. fi
  901. img="$1"
  902. if [ -z "$img" ]; then
  903. img="$TEST_IMG"
  904. fi
  905. if [[ $filter_compression == 0 ]]; then
  906. $PYTHON qcow2.py "$img" dump-header
  907. else
  908. $PYTHON qcow2.py "$img" dump-header | _filter_qcow2_compression_type_bit
  909. fi
  910. }
  911. # make sure this script returns success
  912. true