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