1#!/bin/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 20dd() 21{ 22 if [ "$HOSTOS" == "Linux" ] 23 then 24 command dd --help | grep noxfer > /dev/null 2>&1 25 26 if [ "$?" -eq 0 ] 27 then 28 command dd status=noxfer $@ 29 else 30 command dd $@ 31 fi 32 else 33 command dd $@ 34 fi 35} 36 37# poke_file 'test.img' 512 '\xff\xfe' 38poke_file() 39{ 40 printf "$3" | dd "of=$1" bs=1 "seek=$2" conv=notrunc &>/dev/null 41} 42 43 44if ! . ./common.config 45 then 46 echo "$0: failed to source common.config" 47 exit 1 48fi 49 50_qemu_wrapper() 51{ 52 ( 53 if [ -n "${QEMU_NEED_PID}" ]; then 54 echo $BASHPID > "${QEMU_TEST_DIR}/qemu-${_QEMU_HANDLE}.pid" 55 fi 56 exec "$QEMU_PROG" $QEMU_OPTIONS "$@" 57 ) 58} 59 60_qemu_img_wrapper() 61{ 62 (exec "$QEMU_IMG_PROG" $QEMU_IMG_OPTIONS "$@") 63} 64 65_qemu_io_wrapper() 66{ 67 local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind 68 local QEMU_IO_ARGS="$QEMU_IO_OPTIONS" 69 if [ "$IMGOPTSSYNTAX" = "true" ]; then 70 QEMU_IO_ARGS="--image-opts $QEMU_IO_ARGS" 71 if [ -n "$IMGKEYSECRET" ]; then 72 QEMU_IO_ARGS="--object secret,id=keysec0,data=$IMGKEYSECRET $QEMU_IO_ARGS" 73 fi 74 fi 75 local RETVAL 76 ( 77 if [ "${VALGRIND_QEMU}" == "y" ]; then 78 exec valgrind --log-file="${VALGRIND_LOGFILE}" --error-exitcode=99 "$QEMU_IO_PROG" $QEMU_IO_ARGS "$@" 79 else 80 exec "$QEMU_IO_PROG" $QEMU_IO_ARGS "$@" 81 fi 82 ) 83 RETVAL=$? 84 if [ "${VALGRIND_QEMU}" == "y" ]; then 85 if [ $RETVAL == 99 ]; then 86 cat "${VALGRIND_LOGFILE}" 87 fi 88 rm -f "${VALGRIND_LOGFILE}" 89 fi 90 (exit $RETVAL) 91} 92 93_qemu_nbd_wrapper() 94{ 95 ( 96 echo $BASHPID > "${QEMU_TEST_DIR}/qemu-nbd.pid" 97 exec "$QEMU_NBD_PROG" $QEMU_NBD_OPTIONS "$@" 98 ) 99} 100 101_qemu_vxhs_wrapper() 102{ 103 ( 104 echo $BASHPID > "${TEST_DIR}/qemu-vxhs.pid" 105 exec "$QEMU_VXHS_PROG" $QEMU_VXHS_OPTIONS "$@" 106 ) 107} 108 109export QEMU=_qemu_wrapper 110export QEMU_IMG=_qemu_img_wrapper 111export QEMU_IO=_qemu_io_wrapper 112export QEMU_NBD=_qemu_nbd_wrapper 113export QEMU_VXHS=_qemu_vxhs_wrapper 114 115if [ "$IMGOPTSSYNTAX" = "true" ]; then 116 DRIVER="driver=$IMGFMT" 117 QEMU_IMG_EXTRA_ARGS="--image-opts $QEMU_IMG_EXTRA_ARGS" 118 if [ -n "$IMGKEYSECRET" ]; then 119 QEMU_IMG_EXTRA_ARGS="--object secret,id=keysec0,data=$IMGKEYSECRET $QEMU_IMG_EXTRA_ARGS" 120 fi 121 if [ "$IMGFMT" = "luks" ]; then 122 DRIVER="$DRIVER,key-secret=keysec0" 123 fi 124 if [ "$IMGPROTO" = "file" ]; then 125 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT 126 TEST_IMG="$DRIVER,file.filename=$TEST_DIR/t.$IMGFMT" 127 elif [ "$IMGPROTO" = "nbd" ]; then 128 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT 129 TEST_IMG="$DRIVER,file.driver=nbd,file.host=127.0.0.1,file.port=10810" 130 elif [ "$IMGPROTO" = "ssh" ]; then 131 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT 132 TEST_IMG="$DRIVER,file.driver=ssh,file.host=127.0.0.1,file.path=$TEST_IMG_FILE" 133 elif [ "$IMGPROTO" = "nfs" ]; then 134 TEST_DIR="$DRIVER,file.driver=nfs,file.filename=nfs://127.0.0.1/$TEST_DIR" 135 TEST_IMG=$TEST_DIR/t.$IMGFMT 136 else 137 TEST_IMG="$DRIVER,file.driver=$IMGPROTO,file.filename=$TEST_DIR/t.$IMGFMT" 138 fi 139else 140 QEMU_IMG_EXTRA_ARGS= 141 if [ "$IMGPROTO" = "file" ]; then 142 TEST_IMG=$TEST_DIR/t.$IMGFMT 143 elif [ "$IMGPROTO" = "nbd" ]; then 144 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT 145 TEST_IMG="nbd:127.0.0.1:10810" 146 elif [ "$IMGPROTO" = "ssh" ]; then 147 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT 148 REMOTE_TEST_DIR="ssh://127.0.0.1$TEST_DIR" 149 TEST_IMG="ssh://127.0.0.1$TEST_IMG_FILE" 150 elif [ "$IMGPROTO" = "nfs" ]; then 151 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT 152 REMOTE_TEST_DIR="nfs://127.0.0.1$TEST_DIR" 153 TEST_IMG="nfs://127.0.0.1$TEST_IMG_FILE" 154 elif [ "$IMGPROTO" = "vxhs" ]; then 155 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT 156 TEST_IMG="vxhs://127.0.0.1:9999/t.$IMGFMT" 157 else 158 TEST_IMG=$IMGPROTO:$TEST_DIR/t.$IMGFMT 159 fi 160fi 161ORIG_TEST_IMG="$TEST_IMG" 162 163if [ -z "$TEST_DIR" ]; then 164 TEST_DIR=$PWD/scratch 165fi 166 167QEMU_TEST_DIR="${TEST_DIR}" 168 169if [ ! -e "$TEST_DIR" ]; then 170 mkdir "$TEST_DIR" 171fi 172 173if [ ! -d "$TEST_DIR" ]; then 174 echo "common.rc: Error: \$TEST_DIR ($TEST_DIR) is not a directory" 175 exit 1 176fi 177 178if [ -z "$REMOTE_TEST_DIR" ]; then 179 REMOTE_TEST_DIR="$TEST_DIR" 180fi 181 182if [ ! -d "$SAMPLE_IMG_DIR" ]; then 183 echo "common.rc: Error: \$SAMPLE_IMG_DIR ($SAMPLE_IMG_DIR) is not a directory" 184 exit 1 185fi 186 187_use_sample_img() 188{ 189 SAMPLE_IMG_FILE="${1%\.bz2}" 190 TEST_IMG="$TEST_DIR/$SAMPLE_IMG_FILE" 191 bzcat "$SAMPLE_IMG_DIR/$1" > "$TEST_IMG" 192 if [ $? -ne 0 ] 193 then 194 echo "_use_sample_img error, cannot extract '$SAMPLE_IMG_DIR/$1'" 195 exit 1 196 fi 197} 198 199_stop_nbd_server() 200{ 201 if [ -f "${QEMU_TEST_DIR}/qemu-nbd.pid" ]; then 202 local QEMU_NBD_PID 203 read QEMU_NBD_PID < "${QEMU_TEST_DIR}/qemu-nbd.pid" 204 kill ${QEMU_NBD_PID} 205 rm -f "${QEMU_TEST_DIR}/qemu-nbd.pid" 206 fi 207} 208 209_make_test_img() 210{ 211 # extra qemu-img options can be added by tests 212 # at least one argument (the image size) needs to be added 213 local extra_img_options="" 214 local image_size=$* 215 local optstr="" 216 local img_name="" 217 local use_backing=0 218 local backing_file="" 219 local object_options="" 220 221 if [ -n "$TEST_IMG_FILE" ]; then 222 img_name=$TEST_IMG_FILE 223 else 224 img_name=$TEST_IMG 225 fi 226 227 if [ -n "$IMGOPTS" ]; then 228 optstr=$(_optstr_add "$optstr" "$IMGOPTS") 229 fi 230 if [ -n "$IMGKEYSECRET" ]; then 231 object_options="--object secret,id=keysec0,data=$IMGKEYSECRET" 232 optstr=$(_optstr_add "$optstr" "key-secret=keysec0") 233 fi 234 235 if [ "$1" = "-b" ]; then 236 use_backing=1 237 backing_file=$2 238 image_size=$3 239 fi 240 if [ \( "$IMGFMT" = "qcow2" -o "$IMGFMT" = "qed" \) -a -n "$CLUSTER_SIZE" ]; then 241 optstr=$(_optstr_add "$optstr" "cluster_size=$CLUSTER_SIZE") 242 fi 243 244 if [ -n "$optstr" ]; then 245 extra_img_options="-o $optstr $extra_img_options" 246 fi 247 248 if [ $IMGPROTO = "nbd" ]; then 249 _stop_nbd_server 250 fi 251 252 # XXX(hch): have global image options? 253 ( 254 if [ $use_backing = 1 ]; then 255 $QEMU_IMG create $object_options -f $IMGFMT $extra_img_options -b "$backing_file" "$img_name" $image_size 2>&1 256 else 257 $QEMU_IMG create $object_options -f $IMGFMT $extra_img_options "$img_name" $image_size 2>&1 258 fi 259 ) | _filter_img_create 260 261 # Start an NBD server on the image file, which is what we'll be talking to 262 if [ $IMGPROTO = "nbd" ]; then 263 # Pass a sufficiently high number to -e that should be enough for all 264 # tests 265 eval "$QEMU_NBD -v -t -b 127.0.0.1 -p 10810 -f $IMGFMT -e 42 -x '' $TEST_IMG_FILE >/dev/null &" 266 sleep 1 # FIXME: qemu-nbd needs to be listening before we continue 267 fi 268 269 # Start QNIO server on image directory for vxhs protocol 270 if [ $IMGPROTO = "vxhs" ]; then 271 eval "$QEMU_VXHS -d $TEST_DIR > /dev/null &" 272 sleep 1 # Wait for server to come up. 273 fi 274} 275 276_rm_test_img() 277{ 278 local img=$1 279 if [ "$IMGFMT" = "vmdk" ]; then 280 # Remove all the extents for vmdk 281 "$QEMU_IMG" info "$img" 2>/dev/null | grep 'filename:' | cut -f 2 -d: \ 282 | xargs -I {} rm -f "{}" 283 fi 284 rm -f "$img" 285} 286 287_cleanup_test_img() 288{ 289 case "$IMGPROTO" in 290 291 nbd) 292 _stop_nbd_server 293 rm -f "$TEST_IMG_FILE" 294 ;; 295 vxhs) 296 if [ -f "${TEST_DIR}/qemu-vxhs.pid" ]; then 297 local QEMU_VXHS_PID 298 read QEMU_VXHS_PID < "${TEST_DIR}/qemu-vxhs.pid" 299 kill ${QEMU_VXHS_PID} >/dev/null 2>&1 300 rm -f "${TEST_DIR}/qemu-vxhs.pid" 301 fi 302 rm -f "$TEST_IMG_FILE" 303 ;; 304 305 file) 306 _rm_test_img "$TEST_DIR/t.$IMGFMT" 307 _rm_test_img "$TEST_DIR/t.$IMGFMT.orig" 308 _rm_test_img "$TEST_DIR/t.$IMGFMT.base" 309 if [ -n "$SAMPLE_IMG_FILE" ] 310 then 311 rm -f "$TEST_DIR/$SAMPLE_IMG_FILE" 312 SAMPLE_IMG_FILE= 313 TEST_IMG="$ORIG_TEST_IMG" 314 fi 315 ;; 316 317 rbd) 318 rbd --no-progress rm "$TEST_DIR/t.$IMGFMT" > /dev/null 319 ;; 320 321 sheepdog) 322 collie vdi delete "$TEST_DIR/t.$IMGFMT" 323 ;; 324 325 esac 326} 327 328_check_test_img() 329{ 330 ( 331 if [ "$IMGOPTSSYNTAX" = "true" ]; then 332 $QEMU_IMG check $QEMU_IMG_EXTRA_ARGS "$@" "$TEST_IMG" 2>&1 333 else 334 $QEMU_IMG check "$@" -f $IMGFMT "$TEST_IMG" 2>&1 335 fi 336 ) | _filter_testdir | _filter_qemu_img_check 337} 338 339_img_info() 340{ 341 if [[ "$1" == "--format-specific" ]]; then 342 local format_specific=1 343 shift 344 else 345 local format_specific=0 346 fi 347 348 discard=0 349 regex_json_spec_start='^ *"format-specific": \{' 350 $QEMU_IMG info $QEMU_IMG_EXTRA_ARGS "$@" "$TEST_IMG" 2>&1 | \ 351 sed -e "s#$REMOTE_TEST_DIR#TEST_DIR#g" \ 352 -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \ 353 -e "s#$TEST_DIR#TEST_DIR#g" \ 354 -e "s#$IMGFMT#IMGFMT#g" \ 355 -e "/^disk size:/ D" \ 356 -e "/actual-size/ D" | \ 357 while IFS='' read -r line; do 358 if [[ $format_specific == 1 ]]; then 359 discard=0 360 elif [[ $line == "Format specific information:" ]]; then 361 discard=1 362 elif [[ $line =~ $regex_json_spec_start ]]; then 363 discard=2 364 regex_json_spec_end="^${line%%[^ ]*}\\},? *$" 365 fi 366 if [[ $discard == 0 ]]; then 367 echo "$line" 368 elif [[ $discard == 1 && ! $line ]]; then 369 echo 370 discard=0 371 elif [[ $discard == 2 && $line =~ $regex_json_spec_end ]]; then 372 discard=0 373 fi 374 done 375} 376 377# bail out, setting up .notrun file 378# 379_notrun() 380{ 381 echo "$*" >"$OUTPUT_DIR/$seq.notrun" 382 echo "$seq not run: $*" 383 status=0 384 exit 385} 386 387# just plain bail out 388# 389_fail() 390{ 391 echo "$*" | tee -a "$OUTPUT_DIR/$seq.full" 392 echo "(see $seq.full for details)" 393 status=1 394 exit 1 395} 396 397# tests whether $IMGFMT is one of the supported image formats for a test 398# 399_supported_fmt() 400{ 401 # "generic" is suitable for most image formats. For some formats it doesn't 402 # work, however (most notably read-only formats), so they can opt out by 403 # setting IMGFMT_GENERIC to false. 404 for f; do 405 if [ "$f" = "$IMGFMT" -o "$f" = "generic" -a "$IMGFMT_GENERIC" = "true" ]; then 406 return 407 fi 408 done 409 410 _notrun "not suitable for this image format: $IMGFMT" 411} 412 413# tests whether $IMGFMT is one of the unsupported image format for a test 414# 415_unsupported_fmt() 416{ 417 for f; do 418 if [ "$f" = "$IMGFMT" ]; then 419 _notrun "not suitable for this image format: $IMGFMT" 420 fi 421 done 422} 423 424# tests whether $IMGPROTO is one of the supported image protocols for a test 425# 426_supported_proto() 427{ 428 for f; do 429 if [ "$f" = "$IMGPROTO" -o "$f" = "generic" ]; then 430 return 431 fi 432 done 433 434 _notrun "not suitable for this image protocol: $IMGPROTO" 435} 436 437# tests whether $IMGPROTO is specified as an unsupported image protocol for a test 438# 439_unsupported_proto() 440{ 441 for f; do 442 if [ "$f" = "$IMGPROTO" ]; then 443 _notrun "not suitable for this image protocol: $IMGPROTO" 444 return 445 fi 446 done 447} 448 449# tests whether the host OS is one of the supported OSes for a test 450# 451_supported_os() 452{ 453 for h 454 do 455 if [ "$h" = "$HOSTOS" ] 456 then 457 return 458 fi 459 done 460 461 _notrun "not suitable for this OS: $HOSTOS" 462} 463 464_supported_cache_modes() 465{ 466 for mode; do 467 if [ "$mode" = "$CACHEMODE" ]; then 468 return 469 fi 470 done 471 _notrun "not suitable for cache mode: $CACHEMODE" 472} 473 474_default_cache_mode() 475{ 476 if $CACHEMODE_IS_DEFAULT; then 477 CACHEMODE="$1" 478 QEMU_IO="$QEMU_IO --cache $1" 479 return 480 fi 481} 482 483_unsupported_imgopts() 484{ 485 for bad_opt 486 do 487 if echo "$IMGOPTS" | grep -q 2>/dev/null "$bad_opt" 488 then 489 _notrun "not suitable for image option: $bad_opt" 490 fi 491 done 492} 493 494# this test requires that a specified command (executable) exists 495# 496_require_command() 497{ 498 if [ "$1" = "QEMU" ]; then 499 c=$QEMU_PROG 500 elif [ "$1" = "QEMU_IMG" ]; then 501 c=$QEMU_IMG_PROG 502 elif [ "$1" = "QEMU_IO" ]; then 503 c=$QEMU_IO_PROG 504 elif [ "$1" = "QEMU_NBD" ]; then 505 c=$QEMU_NBD_PROG 506 else 507 eval c=\$$1 508 fi 509 [ -x "$c" ] || _notrun "$1 utility required, skipped this test" 510} 511 512# make sure this script returns success 513true 514