1#!/usr/bin/env bash 2# 3# Copyright (C) 2009 Red Hat, Inc. 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 20SED= 21for sed in sed gsed; do 22 ($sed --version | grep 'GNU sed') > /dev/null 2>&1 23 if [ "$?" -eq 0 ]; then 24 SED=$sed 25 break 26 fi 27done 28if [ -z "$SED" ]; then 29 echo "$0: GNU sed not found" 30 exit 1 31fi 32 33dd() 34{ 35 if [ "$HOSTOS" == "Linux" ] 36 then 37 command dd --help | grep noxfer > /dev/null 2>&1 38 39 if [ "$?" -eq 0 ] 40 then 41 command dd status=noxfer $@ 42 else 43 command dd $@ 44 fi 45 else 46 command dd $@ 47 fi 48} 49 50# poke_file 'test.img' 512 '\xff\xfe' 51poke_file() 52{ 53 printf "$3" | dd "of=$1" bs=1 "seek=$2" conv=notrunc &>/dev/null 54} 55 56# peek_file_le 'test.img' 512 2 => 65534 57peek_file_le() 58{ 59 # Wrap in echo $() to strip spaces 60 echo $(od -j"$2" -N"$3" --endian=little -An -vtu"$3" "$1") 61} 62 63# peek_file_be 'test.img' 512 2 => 65279 64peek_file_be() 65{ 66 # Wrap in echo $() to strip spaces 67 echo $(od -j"$2" -N"$3" --endian=big -An -vtu"$3" "$1") 68} 69 70# peek_file_raw 'test.img' 512 2 => '\xff\xfe' 71peek_file_raw() 72{ 73 dd if="$1" bs=1 skip="$2" count="$3" status=none 74} 75 76 77if ! . ./common.config 78 then 79 echo "$0: failed to source common.config" 80 exit 1 81fi 82 83# Set the variables to the empty string to turn Valgrind off 84# for specific processes, e.g. 85# $ VALGRIND_QEMU_IO= ./check -qcow2 -valgrind 015 86 87: ${VALGRIND_QEMU_VM=$VALGRIND_QEMU} 88: ${VALGRIND_QEMU_IMG=$VALGRIND_QEMU} 89: ${VALGRIND_QEMU_IO=$VALGRIND_QEMU} 90: ${VALGRIND_QEMU_NBD=$VALGRIND_QEMU} 91: ${VALGRIND_QEMU_VXHS=$VALGRIND_QEMU} 92 93# The Valgrind own parameters may be set with 94# its environment variable VALGRIND_OPTS, e.g. 95# $ VALGRIND_OPTS="--leak-check=yes" ./check -qcow2 -valgrind 015 96 97_qemu_proc_exec() 98{ 99 local VALGRIND_LOGFILE="$1" 100 shift 101 if [[ "${VALGRIND_QEMU}" == "y" && "${NO_VALGRIND}" != "y" ]]; then 102 exec valgrind --log-file="${VALGRIND_LOGFILE}" --error-exitcode=99 "$@" 103 else 104 exec "$@" 105 fi 106} 107 108_qemu_proc_valgrind_log() 109{ 110 local VALGRIND_LOGFILE="$1" 111 local RETVAL="$2" 112 if [[ "${VALGRIND_QEMU}" == "y" && "${NO_VALGRIND}" != "y" ]]; then 113 if [ $RETVAL == 99 ]; then 114 cat "${VALGRIND_LOGFILE}" 115 fi 116 rm -f "${VALGRIND_LOGFILE}" 117 fi 118} 119 120_qemu_wrapper() 121{ 122 local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind 123 ( 124 if [ -n "${QEMU_NEED_PID}" ]; then 125 echo $BASHPID > "${QEMU_TEST_DIR}/qemu-${_QEMU_HANDLE}.pid" 126 fi 127 VALGRIND_QEMU="${VALGRIND_QEMU_VM}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \ 128 "$QEMU_PROG" $QEMU_OPTIONS "$@" 129 ) 130 RETVAL=$? 131 _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL 132 return $RETVAL 133} 134 135_qemu_img_wrapper() 136{ 137 local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind 138 ( 139 VALGRIND_QEMU="${VALGRIND_QEMU_IMG}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \ 140 "$QEMU_IMG_PROG" $QEMU_IMG_OPTIONS "$@" 141 ) 142 RETVAL=$? 143 _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL 144 return $RETVAL 145} 146 147_qemu_io_wrapper() 148{ 149 local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind 150 local QEMU_IO_ARGS="$QEMU_IO_OPTIONS" 151 if [ "$IMGOPTSSYNTAX" = "true" ]; then 152 QEMU_IO_ARGS="--image-opts $QEMU_IO_ARGS" 153 if [ -n "$IMGKEYSECRET" ]; then 154 QEMU_IO_ARGS="--object secret,id=keysec0,data=$IMGKEYSECRET $QEMU_IO_ARGS" 155 fi 156 fi 157 ( 158 VALGRIND_QEMU="${VALGRIND_QEMU_IO}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \ 159 "$QEMU_IO_PROG" $QEMU_IO_ARGS "$@" 160 ) 161 RETVAL=$? 162 _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL 163 return $RETVAL 164} 165 166_qemu_nbd_wrapper() 167{ 168 local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind 169 ( 170 VALGRIND_QEMU="${VALGRIND_QEMU_NBD}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \ 171 "$QEMU_NBD_PROG" --pid-file="${QEMU_TEST_DIR}/qemu-nbd.pid" \ 172 $QEMU_NBD_OPTIONS "$@" 173 ) 174 RETVAL=$? 175 _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL 176 return $RETVAL 177} 178 179_qemu_vxhs_wrapper() 180{ 181 local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind 182 ( 183 echo $BASHPID > "${TEST_DIR}/qemu-vxhs.pid" 184 VALGRIND_QEMU="${VALGRIND_QEMU_VXHS}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \ 185 "$QEMU_VXHS_PROG" $QEMU_VXHS_OPTIONS "$@" 186 ) 187 RETVAL=$? 188 _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL 189 return $RETVAL 190} 191 192# Valgrind bug #409141 https://bugs.kde.org/show_bug.cgi?id=409141 193# Until valgrind 3.16+ is ubiquitous, we must work around a hang in 194# valgrind when issuing sigkill. Disable valgrind for this invocation. 195_NO_VALGRIND() 196{ 197 NO_VALGRIND="y" "$@" 198} 199 200export QEMU=_qemu_wrapper 201export QEMU_IMG=_qemu_img_wrapper 202export QEMU_IO=_qemu_io_wrapper 203export QEMU_NBD=_qemu_nbd_wrapper 204export QEMU_VXHS=_qemu_vxhs_wrapper 205 206if [ "$IMGOPTSSYNTAX" = "true" ]; then 207 DRIVER="driver=$IMGFMT" 208 QEMU_IMG_EXTRA_ARGS="--image-opts $QEMU_IMG_EXTRA_ARGS" 209 if [ -n "$IMGKEYSECRET" ]; then 210 QEMU_IMG_EXTRA_ARGS="--object secret,id=keysec0,data=$IMGKEYSECRET $QEMU_IMG_EXTRA_ARGS" 211 fi 212 if [ "$IMGFMT" = "luks" ]; then 213 DRIVER="$DRIVER,key-secret=keysec0" 214 fi 215 if [ "$IMGPROTO" = "file" ]; then 216 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT 217 TEST_IMG="$DRIVER,file.filename=$TEST_DIR/t.$IMGFMT" 218 elif [ "$IMGPROTO" = "nbd" ]; then 219 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT 220 TEST_IMG="$DRIVER,file.driver=nbd,file.type=unix,file.path=$SOCKDIR/nbd" 221 elif [ "$IMGPROTO" = "ssh" ]; then 222 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT 223 TEST_IMG="$DRIVER,file.driver=ssh,file.host=127.0.0.1,file.path=$TEST_IMG_FILE" 224 elif [ "$IMGPROTO" = "nfs" ]; then 225 TEST_DIR="$DRIVER,file.driver=nfs,file.filename=nfs://127.0.0.1/$TEST_DIR" 226 TEST_IMG=$TEST_DIR/t.$IMGFMT 227 else 228 TEST_IMG="$DRIVER,file.driver=$IMGPROTO,file.filename=$TEST_DIR/t.$IMGFMT" 229 fi 230else 231 QEMU_IMG_EXTRA_ARGS= 232 if [ "$IMGPROTO" = "file" ]; then 233 TEST_IMG=$TEST_DIR/t.$IMGFMT 234 elif [ "$IMGPROTO" = "nbd" ]; then 235 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT 236 TEST_IMG="nbd+unix:///?socket=$SOCK_DIR/nbd" 237 elif [ "$IMGPROTO" = "ssh" ]; then 238 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT 239 REMOTE_TEST_DIR="ssh://\\($USER@\\)\\?127.0.0.1\\(:[0-9]\\+\\)\\?$TEST_DIR" 240 TEST_IMG="ssh://127.0.0.1$TEST_IMG_FILE" 241 elif [ "$IMGPROTO" = "nfs" ]; then 242 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT 243 REMOTE_TEST_DIR="nfs://127.0.0.1$TEST_DIR" 244 TEST_IMG="nfs://127.0.0.1$TEST_IMG_FILE" 245 elif [ "$IMGPROTO" = "vxhs" ]; then 246 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT 247 TEST_IMG="vxhs://127.0.0.1:9999/t.$IMGFMT" 248 else 249 TEST_IMG=$IMGPROTO:$TEST_DIR/t.$IMGFMT 250 fi 251fi 252ORIG_TEST_IMG="$TEST_IMG" 253 254if [ -z "$TEST_DIR" ]; then 255 TEST_DIR=$PWD/scratch 256fi 257 258QEMU_TEST_DIR="${TEST_DIR}" 259 260if [ ! -e "$TEST_DIR" ]; then 261 mkdir "$TEST_DIR" 262fi 263 264if [ ! -d "$TEST_DIR" ]; then 265 echo "common.rc: Error: \$TEST_DIR ($TEST_DIR) is not a directory" 266 exit 1 267fi 268 269if [ -z "$REMOTE_TEST_DIR" ]; then 270 REMOTE_TEST_DIR="$TEST_DIR" 271fi 272 273if [ ! -d "$SAMPLE_IMG_DIR" ]; then 274 echo "common.rc: Error: \$SAMPLE_IMG_DIR ($SAMPLE_IMG_DIR) is not a directory" 275 exit 1 276fi 277 278_use_sample_img() 279{ 280 SAMPLE_IMG_FILE="${1%\.bz2}" 281 TEST_IMG="$TEST_DIR/$SAMPLE_IMG_FILE" 282 bzcat "$SAMPLE_IMG_DIR/$1" > "$TEST_IMG" 283 if [ $? -ne 0 ] 284 then 285 echo "_use_sample_img error, cannot extract '$SAMPLE_IMG_DIR/$1'" 286 exit 1 287 fi 288} 289 290_stop_nbd_server() 291{ 292 if [ -f "${QEMU_TEST_DIR}/qemu-nbd.pid" ]; then 293 local QEMU_NBD_PID 294 read QEMU_NBD_PID < "${QEMU_TEST_DIR}/qemu-nbd.pid" 295 kill ${QEMU_NBD_PID} 296 rm -f "${QEMU_TEST_DIR}/qemu-nbd.pid" "$SOCK_DIR/nbd" 297 fi 298} 299 300_make_test_img() 301{ 302 # extra qemu-img options can be added by tests 303 # at least one argument (the image size) needs to be added 304 local extra_img_options="" 305 local image_size=$* 306 local optstr="" 307 local img_name="" 308 local use_backing=0 309 local backing_file="" 310 local object_options="" 311 312 if [ -n "$TEST_IMG_FILE" ]; then 313 img_name=$TEST_IMG_FILE 314 else 315 img_name=$TEST_IMG 316 fi 317 318 if [ -n "$IMGOPTS" ]; then 319 optstr=$(_optstr_add "$optstr" "$IMGOPTS") 320 fi 321 if [ -n "$IMGKEYSECRET" ]; then 322 object_options="--object secret,id=keysec0,data=$IMGKEYSECRET" 323 optstr=$(_optstr_add "$optstr" "key-secret=keysec0") 324 fi 325 326 if [ "$1" = "-b" ]; then 327 use_backing=1 328 backing_file=$2 329 image_size=$3 330 fi 331 if [ \( "$IMGFMT" = "qcow2" -o "$IMGFMT" = "qed" \) -a -n "$CLUSTER_SIZE" ]; then 332 optstr=$(_optstr_add "$optstr" "cluster_size=$CLUSTER_SIZE") 333 fi 334 335 if [ -n "$optstr" ]; then 336 extra_img_options="-o $optstr $extra_img_options" 337 fi 338 339 if [ $IMGPROTO = "nbd" ]; then 340 _stop_nbd_server 341 fi 342 343 # XXX(hch): have global image options? 344 ( 345 if [ $use_backing = 1 ]; then 346 $QEMU_IMG create $object_options -f $IMGFMT $extra_img_options -b "$backing_file" "$img_name" $image_size 2>&1 347 else 348 $QEMU_IMG create $object_options -f $IMGFMT $extra_img_options "$img_name" $image_size 2>&1 349 fi 350 ) | _filter_img_create 351 352 # Start an NBD server on the image file, which is what we'll be talking to. 353 # Once NBD gains resize support, we may also want to use -f raw at the 354 # server and interpret format over NBD, but for now, the format is 355 # interpreted at the server and raw data sent over NBD. 356 if [ $IMGPROTO = "nbd" ]; then 357 # Pass a sufficiently high number to -e that should be enough for all 358 # tests 359 eval "$QEMU_NBD -v -t -k '$SOCK_DIR/nbd' -f $IMGFMT -e 42 -x '' $TEST_IMG_FILE >/dev/null &" 360 sleep 1 # FIXME: qemu-nbd needs to be listening before we continue 361 fi 362 363 # Start QNIO server on image directory for vxhs protocol 364 if [ $IMGPROTO = "vxhs" ]; then 365 eval "$QEMU_VXHS -d $TEST_DIR > /dev/null &" 366 sleep 1 # Wait for server to come up. 367 fi 368} 369 370_rm_test_img() 371{ 372 local img=$1 373 if [ "$IMGFMT" = "vmdk" ]; then 374 # Remove all the extents for vmdk 375 "$QEMU_IMG" info "$img" 2>/dev/null | grep 'filename:' | cut -f 2 -d: \ 376 | xargs -I {} rm -f "{}" 377 fi 378 rm -f "$img" 379} 380 381_cleanup_test_img() 382{ 383 case "$IMGPROTO" in 384 385 nbd) 386 _stop_nbd_server 387 rm -f "$TEST_IMG_FILE" 388 ;; 389 vxhs) 390 if [ -f "${TEST_DIR}/qemu-vxhs.pid" ]; then 391 local QEMU_VXHS_PID 392 read QEMU_VXHS_PID < "${TEST_DIR}/qemu-vxhs.pid" 393 kill ${QEMU_VXHS_PID} >/dev/null 2>&1 394 rm -f "${TEST_DIR}/qemu-vxhs.pid" 395 fi 396 rm -f "$TEST_IMG_FILE" 397 ;; 398 399 file) 400 _rm_test_img "$TEST_DIR/t.$IMGFMT" 401 _rm_test_img "$TEST_DIR/t.$IMGFMT.orig" 402 _rm_test_img "$TEST_DIR/t.$IMGFMT.base" 403 if [ -n "$SAMPLE_IMG_FILE" ] 404 then 405 rm -f "$TEST_DIR/$SAMPLE_IMG_FILE" 406 SAMPLE_IMG_FILE= 407 TEST_IMG="$ORIG_TEST_IMG" 408 fi 409 ;; 410 411 rbd) 412 rbd --no-progress rm "$TEST_DIR/t.$IMGFMT" > /dev/null 413 ;; 414 415 sheepdog) 416 collie vdi delete "$TEST_DIR/t.$IMGFMT" 417 ;; 418 419 esac 420} 421 422_check_test_img() 423{ 424 ( 425 if [ "$IMGOPTSSYNTAX" = "true" ]; then 426 $QEMU_IMG check $QEMU_IMG_EXTRA_ARGS "$@" "$TEST_IMG" 2>&1 427 else 428 $QEMU_IMG check "$@" -f $IMGFMT "$TEST_IMG" 2>&1 429 fi 430 ) | _filter_testdir | _filter_qemu_img_check 431 432 # return real qemu_img check status, to analyze in 433 # _check_test_img_ignore_leaks 434 return ${PIPESTATUS[0]} 435} 436 437_check_test_img_ignore_leaks() 438{ 439 out=$(_check_test_img "$@") 440 status=$? 441 if [ $status = 3 ]; then 442 # This must correspond to success output in dump_human_image_check() 443 echo "No errors were found on the image." 444 return 0 445 fi 446 echo "$out" 447 return $status 448} 449 450_img_info() 451{ 452 if [[ "$1" == "--format-specific" ]]; then 453 local format_specific=1 454 shift 455 else 456 local format_specific=0 457 fi 458 459 discard=0 460 regex_json_spec_start='^ *"format-specific": \{' 461 $QEMU_IMG info $QEMU_IMG_EXTRA_ARGS "$@" "$TEST_IMG" 2>&1 | \ 462 sed -e "s#$REMOTE_TEST_DIR#TEST_DIR#g" \ 463 -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \ 464 -e "s#$TEST_DIR#TEST_DIR#g" \ 465 -e "s#$IMGFMT#IMGFMT#g" \ 466 -e "/^disk size:/ D" \ 467 -e "/actual-size/ D" | \ 468 while IFS='' read -r line; do 469 if [[ $format_specific == 1 ]]; then 470 discard=0 471 elif [[ $line == "Format specific information:" ]]; then 472 discard=1 473 elif [[ $line =~ $regex_json_spec_start ]]; then 474 discard=2 475 regex_json_spec_end="^${line%%[^ ]*}\\},? *$" 476 fi 477 if [[ $discard == 0 ]]; then 478 echo "$line" 479 elif [[ $discard == 1 && ! $line ]]; then 480 echo 481 discard=0 482 elif [[ $discard == 2 && $line =~ $regex_json_spec_end ]]; then 483 discard=0 484 fi 485 done 486} 487 488# bail out, setting up .notrun file 489# 490_notrun() 491{ 492 echo "$*" >"$OUTPUT_DIR/$seq.notrun" 493 echo "$seq not run: $*" 494 status=0 495 exit 496} 497 498# bail out, setting up .casenotrun file 499# The function _casenotrun() is used as a notifier. It is the 500# caller's responsibility to make skipped a particular test. 501# 502_casenotrun() 503{ 504 echo " [case not run] $*" >>"$OUTPUT_DIR/$seq.casenotrun" 505} 506 507# just plain bail out 508# 509_fail() 510{ 511 echo "$*" | tee -a "$OUTPUT_DIR/$seq.full" 512 echo "(see $seq.full for details)" 513 status=1 514 exit 1 515} 516 517# tests whether $IMGFMT is one of the supported image formats for a test 518# 519_supported_fmt() 520{ 521 # "generic" is suitable for most image formats. For some formats it doesn't 522 # work, however (most notably read-only formats), so they can opt out by 523 # setting IMGFMT_GENERIC to false. 524 for f; do 525 if [ "$f" = "$IMGFMT" -o "$f" = "generic" -a "$IMGFMT_GENERIC" = "true" ]; then 526 return 527 fi 528 done 529 530 _notrun "not suitable for this image format: $IMGFMT" 531} 532 533# tests whether $IMGFMT is one of the unsupported image format for a test 534# 535_unsupported_fmt() 536{ 537 for f; do 538 if [ "$f" = "$IMGFMT" ]; then 539 _notrun "not suitable for this image format: $IMGFMT" 540 fi 541 done 542} 543 544# tests whether $IMGPROTO is one of the supported image protocols for a test 545# 546_supported_proto() 547{ 548 for f; do 549 if [ "$f" = "$IMGPROTO" -o "$f" = "generic" ]; then 550 return 551 fi 552 done 553 554 _notrun "not suitable for this image protocol: $IMGPROTO" 555} 556 557# tests whether $IMGPROTO is specified as an unsupported image protocol for a test 558# 559_unsupported_proto() 560{ 561 for f; do 562 if [ "$f" = "$IMGPROTO" ]; then 563 _notrun "not suitable for this image protocol: $IMGPROTO" 564 return 565 fi 566 done 567} 568 569# tests whether the host OS is one of the supported OSes for a test 570# 571_supported_os() 572{ 573 for h 574 do 575 if [ "$h" = "$HOSTOS" ] 576 then 577 return 578 fi 579 done 580 581 _notrun "not suitable for this OS: $HOSTOS" 582} 583 584_supported_cache_modes() 585{ 586 for mode; do 587 if [ "$mode" = "$CACHEMODE" ]; then 588 return 589 fi 590 done 591 _notrun "not suitable for cache mode: $CACHEMODE" 592} 593 594_default_cache_mode() 595{ 596 if $CACHEMODE_IS_DEFAULT; then 597 CACHEMODE="$1" 598 QEMU_IO="$QEMU_IO --cache $1" 599 return 600 fi 601} 602 603_unsupported_imgopts() 604{ 605 for bad_opt 606 do 607 if echo "$IMGOPTS" | grep -q 2>/dev/null "$bad_opt" 608 then 609 _notrun "not suitable for image option: $bad_opt" 610 fi 611 done 612} 613 614# this test requires that a specified command (executable) exists 615# 616_require_command() 617{ 618 if [ "$1" = "QEMU" ]; then 619 c=$QEMU_PROG 620 elif [ "$1" = "QEMU_IMG" ]; then 621 c=$QEMU_IMG_PROG 622 elif [ "$1" = "QEMU_IO" ]; then 623 c=$QEMU_IO_PROG 624 elif [ "$1" = "QEMU_NBD" ]; then 625 c=$QEMU_NBD_PROG 626 else 627 eval c=\$$1 628 fi 629 [ -x "$c" ] || _notrun "$1 utility required, skipped this test" 630} 631 632# Check that a set of drivers has been whitelisted in the QEMU binary 633# 634_require_drivers() 635{ 636 available=$($QEMU -drive format=help | \ 637 sed -e '/Supported formats:/!d' -e 's/Supported formats://') 638 for driver 639 do 640 if ! echo "$available" | grep -q " $driver\( \|$\)"; then 641 _notrun "$driver not available" 642 fi 643 done 644} 645 646# make sure this script returns success 647true 648