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