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# we need common.config 44if [ "$iam" != "check" ] 45then 46 if ! . ./common.config 47 then 48 echo "$iam: failed to source common.config" 49 exit 1 50 fi 51fi 52 53# make sure we have a standard umask 54umask 022 55 56if [ "$IMGPROTO" = "file" ]; then 57 TEST_IMG=$TEST_DIR/t.$IMGFMT 58elif [ "$IMGPROTO" = "nbd" ]; then 59 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT 60 TEST_IMG="nbd:127.0.0.1:10810" 61elif [ "$IMGPROTO" = "ssh" ]; then 62 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT 63 TEST_IMG="ssh://127.0.0.1$TEST_IMG_FILE" 64elif [ "$IMGPROTO" = "nfs" ]; then 65 TEST_DIR="nfs://127.0.0.1/$TEST_DIR" 66 TEST_IMG=$TEST_DIR/t.$IMGFMT 67elif [ "$IMGPROTO" = "archipelago" ]; then 68 TEST_IMG="archipelago:at.$IMGFMT" 69else 70 TEST_IMG=$IMGPROTO:$TEST_DIR/t.$IMGFMT 71fi 72 73function valgrind_qemu_io() 74{ 75 valgrind --log-file=/tmp/$$.valgrind --error-exitcode=99 $REAL_QEMU_IO "$@" 76 if [ $? != 0 ]; then 77 cat /tmp/$$.valgrind 78 fi 79 rm -f /tmp/$$.valgrind 80} 81 82 83_optstr_add() 84{ 85 if [ -n "$1" ]; then 86 echo "$1,$2" 87 else 88 echo "$2" 89 fi 90} 91 92_set_default_imgopts() 93{ 94 if [ "$IMGFMT" == "qcow2" ] && ! (echo "$IMGOPTS" | grep "compat=" > /dev/null); then 95 IMGOPTS=$(_optstr_add "$IMGOPTS" "compat=1.1") 96 fi 97} 98 99_use_sample_img() 100{ 101 SAMPLE_IMG_FILE="${1%\.bz2}" 102 TEST_IMG="$TEST_DIR/$SAMPLE_IMG_FILE" 103 bzcat "$SAMPLE_IMG_DIR/$1" > "$TEST_IMG" 104 if [ $? -ne 0 ] 105 then 106 echo "_use_sample_img error, cannot extract '$SAMPLE_IMG_DIR/$1'" 107 exit 1 108 fi 109} 110 111_make_test_img() 112{ 113 # extra qemu-img options can be added by tests 114 # at least one argument (the image size) needs to be added 115 local extra_img_options="" 116 local image_size=$* 117 local optstr="" 118 local img_name="" 119 local use_backing=0 120 local backing_file="" 121 122 if [ -n "$TEST_IMG_FILE" ]; then 123 img_name=$TEST_IMG_FILE 124 else 125 img_name=$TEST_IMG 126 fi 127 128 if [ -n "$IMGOPTS" ]; then 129 optstr=$(_optstr_add "$optstr" "$IMGOPTS") 130 fi 131 132 if [ "$1" = "-b" ]; then 133 use_backing=1 134 backing_file=$2 135 image_size=$3 136 fi 137 if [ \( "$IMGFMT" = "qcow2" -o "$IMGFMT" = "qed" \) -a -n "$CLUSTER_SIZE" ]; then 138 optstr=$(_optstr_add "$optstr" "cluster_size=$CLUSTER_SIZE") 139 fi 140 141 if [ -n "$optstr" ]; then 142 extra_img_options="-o $optstr $extra_img_options" 143 fi 144 145 # XXX(hch): have global image options? 146 ( 147 if [ $use_backing = 1 ]; then 148 $QEMU_IMG create -f $IMGFMT $extra_img_options -b "$backing_file" "$img_name" $image_size 2>&1 149 else 150 $QEMU_IMG create -f $IMGFMT $extra_img_options "$img_name" $image_size 2>&1 151 fi 152 ) | _filter_img_create 153 154 # Start an NBD server on the image file, which is what we'll be talking to 155 if [ $IMGPROTO = "nbd" ]; then 156 eval "$QEMU_NBD -v -t -b 127.0.0.1 -p 10810 $TEST_IMG_FILE &" 157 QEMU_NBD_PID=$! 158 sleep 1 # FIXME: qemu-nbd needs to be listening before we continue 159 fi 160} 161 162_rm_test_img() 163{ 164 local img=$1 165 if [ "$IMGFMT" = "vmdk" ]; then 166 # Remove all the extents for vmdk 167 "$QEMU_IMG" info "$img" 2>/dev/null | grep 'filename:' | cut -f 2 -d: \ 168 | xargs -I {} rm -f "{}" 169 fi 170 rm -f "$img" 171} 172 173_cleanup_test_img() 174{ 175 case "$IMGPROTO" in 176 177 nbd) 178 kill $QEMU_NBD_PID 179 rm -f "$TEST_IMG_FILE" 180 ;; 181 file) 182 _rm_test_img "$TEST_DIR/t.$IMGFMT" 183 _rm_test_img "$TEST_DIR/t.$IMGFMT.orig" 184 _rm_test_img "$TEST_DIR/t.$IMGFMT.base" 185 if [ -n "$SAMPLE_IMG_FILE" ] 186 then 187 rm -f "$TEST_DIR/$SAMPLE_IMG_FILE" 188 fi 189 ;; 190 191 rbd) 192 rbd --no-progress rm "$TEST_DIR/t.$IMGFMT" > /dev/null 193 ;; 194 195 archipelago) 196 vlmc remove "at.$IMGFMT" > /dev/null 197 ;; 198 199 sheepdog) 200 collie vdi delete "$TEST_DIR/t.$IMGFMT" 201 ;; 202 203 esac 204} 205 206_check_test_img() 207{ 208 $QEMU_IMG check "$@" -f $IMGFMT "$TEST_IMG" 2>&1 | _filter_testdir | \ 209 sed -e '/allocated.*fragmented.*compressed clusters/d' \ 210 -e 's/qemu-img: This image format does not support checks/No errors were found on the image./' \ 211 -e '/Image end offset: [0-9]\+/d' 212} 213 214_img_info() 215{ 216 discard=0 217 regex_json_spec_start='^ *"format-specific": \{' 218 $QEMU_IMG info "$@" "$TEST_IMG" 2>&1 | \ 219 sed -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \ 220 -e "s#$TEST_DIR#TEST_DIR#g" \ 221 -e "s#$IMGFMT#IMGFMT#g" \ 222 -e "/^disk size:/ D" \ 223 -e "/actual-size/ D" | \ 224 while IFS='' read line; do 225 if [[ $line == "Format specific information:" ]]; then 226 discard=1 227 elif [[ $line =~ $regex_json_spec_start ]]; then 228 discard=2 229 regex_json_spec_end="^${line%%[^ ]*}\\},? *$" 230 fi 231 if [[ $discard == 0 ]]; then 232 echo "$line" 233 elif [[ $discard == 1 && ! $line ]]; then 234 echo 235 discard=0 236 elif [[ $discard == 2 && $line =~ $regex_json_spec_end ]]; then 237 discard=0 238 fi 239 done 240} 241 242_get_pids_by_name() 243{ 244 if [ $# -ne 1 ] 245 then 246 echo "Usage: _get_pids_by_name process-name" 1>&2 247 exit 1 248 fi 249 250 # Algorithm ... all ps(1) variants have a time of the form MM:SS or 251 # HH:MM:SS before the psargs field, use this as the search anchor. 252 # 253 # Matches with $1 (process-name) occur if the first psarg is $1 254 # or ends in /$1 ... the matching uses sed's regular expressions, 255 # so passing a regex into $1 will work. 256 257 ps $PS_ALL_FLAGS \ 258 | sed -n \ 259 -e 's/$/ /' \ 260 -e 's/[ ][ ]*/ /g' \ 261 -e 's/^ //' \ 262 -e 's/^[^ ]* //' \ 263 -e "/[0-9]:[0-9][0-9] *[^ ]*\/$1 /s/ .*//p" \ 264 -e "/[0-9]:[0-9][0-9] *$1 /s/ .*//p" 265} 266 267# fqdn for localhost 268# 269_get_fqdn() 270{ 271 host=`hostname` 272 $NSLOOKUP_PROG $host | $AWK_PROG '{ if ($1 == "Name:") print $2 }' 273} 274 275# check if run as root 276# 277_need_to_be_root() 278{ 279 id=`id | $SED_PROG -e 's/(.*//' -e 's/.*=//'` 280 if [ "$id" -ne 0 ] 281 then 282 echo "Arrgh ... you need to be root (not uid=$id) to run this test" 283 exit 1 284 fi 285} 286 287 288# Do a command, log it to $seq.full, optionally test return status 289# and die if command fails. If called with one argument _do executes the 290# command, logs it, and returns its exit status. With two arguments _do 291# first prints the message passed in the first argument, and then "done" 292# or "fail" depending on the return status of the command passed in the 293# second argument. If the command fails and the variable _do_die_on_error 294# is set to "always" or the two argument form is used and _do_die_on_error 295# is set to "message_only" _do will print an error message to 296# $seq.out and exit. 297 298_do() 299{ 300 if [ $# -eq 1 ]; then 301 _cmd=$1 302 elif [ $# -eq 2 ]; then 303 _note=$1 304 _cmd=$2 305 echo -n "$_note... " 306 else 307 echo "Usage: _do [note] cmd" 1>&2 308 status=1; exit 309 fi 310 311 (eval "echo '---' \"$_cmd\"") >>"$OUTPUT_DIR/$seq.full" 312 (eval "$_cmd") >$tmp._out 2>&1; ret=$? 313 cat $tmp._out >>"$OUTPUT_DIR/$seq.full" 314 if [ $# -eq 2 ]; then 315 if [ $ret -eq 0 ]; then 316 echo "done" 317 else 318 echo "fail" 319 fi 320 fi 321 if [ $ret -ne 0 ] \ 322 && [ "$_do_die_on_error" = "always" \ 323 -o \( $# -eq 2 -a "$_do_die_on_error" = "message_only" \) ] 324 then 325 [ $# -ne 2 ] && echo 326 eval "echo \"$_cmd\" failed \(returned $ret\): see $seq.full" 327 status=1; exit 328 fi 329 330 return $ret 331} 332 333# bail out, setting up .notrun file 334# 335_notrun() 336{ 337 echo "$*" >"$OUTPUT_DIR/$seq.notrun" 338 echo "$seq not run: $*" 339 status=0 340 exit 341} 342 343# just plain bail out 344# 345_fail() 346{ 347 echo "$*" | tee -a "$OUTPUT_DIR/$seq.full" 348 echo "(see $seq.full for details)" 349 status=1 350 exit 1 351} 352 353# tests whether $IMGFMT is one of the supported image formats for a test 354# 355_supported_fmt() 356{ 357 # "generic" is suitable for most image formats. For some formats it doesn't 358 # work, however (most notably read-only formats), so they can opt out by 359 # setting IMGFMT_GENERIC to false. 360 for f; do 361 if [ "$f" = "$IMGFMT" -o "$f" = "generic" -a "$IMGFMT_GENERIC" = "true" ]; then 362 return 363 fi 364 done 365 366 _notrun "not suitable for this image format: $IMGFMT" 367} 368 369# tests whether $IMGPROTO is one of the supported image protocols for a test 370# 371_supported_proto() 372{ 373 for f; do 374 if [ "$f" = "$IMGPROTO" -o "$f" = "generic" ]; then 375 return 376 fi 377 done 378 379 _notrun "not suitable for this image protocol: $IMGPROTO" 380} 381 382# tests whether the host OS is one of the supported OSes for a test 383# 384_supported_os() 385{ 386 for h 387 do 388 if [ "$h" = "$HOSTOS" ] 389 then 390 return 391 fi 392 done 393 394 _notrun "not suitable for this OS: $HOSTOS" 395} 396 397_supported_cache_modes() 398{ 399 for mode; do 400 if [ "$mode" = "$CACHEMODE" ]; then 401 return 402 fi 403 done 404 _notrun "not suitable for cache mode: $CACHEMODE" 405} 406 407_default_cache_mode() 408{ 409 if $CACHEMODE_IS_DEFAULT; then 410 CACHEMODE="$1" 411 QEMU_IO="$QEMU_IO --cache $1" 412 return 413 fi 414} 415 416_unsupported_imgopts() 417{ 418 for bad_opt 419 do 420 if echo "$IMGOPTS" | grep -q 2>/dev/null "$bad_opt" 421 then 422 _notrun "not suitable for image option: $bad_opt" 423 fi 424 done 425} 426 427# this test requires that a specified command (executable) exists 428# 429_require_command() 430{ 431 eval c=\$$1 432 [ -x "$c" ] || _notrun "$1 utility required, skipped this test" 433} 434 435_full_imgfmt_details() 436{ 437 if [ -n "$IMGOPTS" ]; then 438 echo "$IMGFMT ($IMGOPTS)" 439 else 440 echo "$IMGFMT" 441 fi 442} 443 444_full_imgproto_details() 445{ 446 echo "$IMGPROTO" 447} 448 449_full_platform_details() 450{ 451 os=`uname -s` 452 host=`hostname -s` 453 kernel=`uname -r` 454 platform=`uname -m` 455 echo "$os/$platform $host $kernel" 456} 457 458_link_out_file() 459{ 460 if [ -z "$1" ]; then 461 echo Error must pass \$seq. 462 exit 463 fi 464 rm -f $1 465 if [ "`uname`" == "IRIX64" ] || [ "`uname`" == "IRIX" ]; then 466 ln -s $1.irix $1 467 elif [ "`uname`" == "Linux" ]; then 468 ln -s $1.linux $1 469 else 470 echo Error test $seq does not run on the operating system: `uname` 471 exit 472 fi 473} 474 475_die() 476{ 477 echo $@ 478 exit 1 479} 480 481# make sure this script returns success 482/bin/true 483