common.rc 11.8 KB
Newer Older
1
#!/bin/bash
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#
# Copyright (C) 2009 Red Hat, Inc.
# Copyright (c) 2000-2006 Silicon Graphics, Inc.  All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# 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 20 21 22
#

dd()
{
   if [ "$HOSTOS" == "Linux" ]
K
Kevin Wolf 已提交
23 24 25 26 27 28 29 30 31
   then
        command dd --help | grep noxfer > /dev/null 2>&1

        if [ "$?" -eq 0 ]
            then
                command dd status=noxfer $@
            else
                command dd $@
            fi
32
   else
K
Kevin Wolf 已提交
33
        command dd $@
34 35 36
   fi
}

37 38 39 40 41 42
# poke_file 'test.img' 512 '\xff\xfe'
poke_file()
{
    printf "$3" | dd "of=$1" bs=1 "seek=$2" conv=notrunc &>/dev/null
}

43 44 45 46 47 48 49 50 51 52 53 54 55
# we need common.config
if [ "$iam" != "check" ]
then
    if ! . ./common.config
        then
        echo "$iam: failed to source common.config"
        exit 1
    fi
fi

# make sure we have a standard umask
umask 022

56 57
if [ "$IMGPROTO" = "file" ]; then
    TEST_IMG=$TEST_DIR/t.$IMGFMT
58 59 60
elif [ "$IMGPROTO" = "nbd" ]; then
    TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
    TEST_IMG="nbd:127.0.0.1:10810"
61 62 63
elif [ "$IMGPROTO" = "ssh" ]; then
    TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
    TEST_IMG="ssh://127.0.0.1$TEST_IMG_FILE"
64 65 66
elif [ "$IMGPROTO" = "nfs" ]; then
    TEST_DIR="nfs://127.0.0.1/$TEST_DIR"
    TEST_IMG=$TEST_DIR/t.$IMGFMT
67 68
elif [ "$IMGPROTO" = "archipelago" ]; then
    TEST_IMG="archipelago:at.$IMGFMT"
69 70 71
else
    TEST_IMG=$IMGPROTO:$TEST_DIR/t.$IMGFMT
fi
72

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
_optstr_add()
{
    if [ -n "$1" ]; then
        echo "$1,$2"
    else
        echo "$2"
    fi
}

_set_default_imgopts()
{
    if [ "$IMGFMT" == "qcow2" ] && ! (echo "$IMGOPTS" | grep "compat=" > /dev/null); then
        IMGOPTS=$(_optstr_add "$IMGOPTS" "compat=1.1")
    fi
}

89 90 91 92 93 94 95 96 97 98 99 100
_use_sample_img()
{
    SAMPLE_IMG_FILE="${1%\.bz2}"
    TEST_IMG="$TEST_DIR/$SAMPLE_IMG_FILE"
    bzcat "$SAMPLE_IMG_DIR/$1" > "$TEST_IMG"
    if [ $? -ne 0 ]
    then
        echo "_use_sample_img error, cannot extract '$SAMPLE_IMG_DIR/$1'"
        exit 1
    fi
}

101 102 103 104
_make_test_img()
{
    # extra qemu-img options can be added by tests
    # at least one argument (the image size) needs to be added
105 106
    local extra_img_options=""
    local image_size=$*
107
    local optstr=""
108
    local img_name=""
109 110
    local use_backing=0
    local backing_file=""
111 112 113 114 115 116

    if [ -n "$TEST_IMG_FILE" ]; then
        img_name=$TEST_IMG_FILE
    else
        img_name=$TEST_IMG
    fi
117 118 119 120

    if [ -n "$IMGOPTS" ]; then
        optstr=$(_optstr_add "$optstr" "$IMGOPTS")
    fi
121

122
    if [ "$1" = "-b" ]; then
123 124
        use_backing=1
        backing_file=$2
125 126
        image_size=$3
    fi
127
    if [ \( "$IMGFMT" = "qcow2" -o "$IMGFMT" = "qed" \) -a -n "$CLUSTER_SIZE" ]; then
128 129 130 131 132
        optstr=$(_optstr_add "$optstr" "cluster_size=$CLUSTER_SIZE")
    fi

    if [ -n "$optstr" ]; then
        extra_img_options="-o $optstr $extra_img_options"
133 134
    fi

135
    # XXX(hch): have global image options?
136 137 138 139 140 141
    (
     if [ $use_backing = 1 ]; then
        $QEMU_IMG create -f $IMGFMT $extra_img_options -b "$backing_file" "$img_name" $image_size 2>&1
     else
        $QEMU_IMG create -f $IMGFMT $extra_img_options "$img_name" $image_size 2>&1
     fi
142
    ) | _filter_img_create
143 144 145

    # Start an NBD server on the image file, which is what we'll be talking to
    if [ $IMGPROTO = "nbd" ]; then
M
Max Reitz 已提交
146
        eval "$QEMU_NBD -v -t -b 127.0.0.1 -p 10810 -f $IMGFMT  $TEST_IMG_FILE &"
147 148
        sleep 1 # FIXME: qemu-nbd needs to be listening before we continue
    fi
149 150
}

151 152 153 154 155
_rm_test_img()
{
    local img=$1
    if [ "$IMGFMT" = "vmdk" ]; then
        # Remove all the extents for vmdk
156
        "$QEMU_IMG" info "$img" 2>/dev/null | grep 'filename:' | cut -f 2 -d: \
157 158
            | xargs -I {} rm -f "{}"
    fi
159
    rm -f "$img"
160 161
}

162 163
_cleanup_test_img()
{
164 165
    case "$IMGPROTO" in

166
        nbd)
167 168 169 170 171
            if [ -f "${TEST_DIR}/qemu-nbd.pid" ]; then
                local QEMU_NBD_PID
                read QEMU_NBD_PID < "${TEST_DIR}/qemu-nbd.pid"
                kill ${QEMU_NBD_PID}
                rm -f "${TEST_DIR}/qemu-nbd.pid"
172
            fi
173
            rm -f "$TEST_IMG_FILE"
174
            ;;
175
        file)
176 177 178
            _rm_test_img "$TEST_DIR/t.$IMGFMT"
            _rm_test_img "$TEST_DIR/t.$IMGFMT.orig"
            _rm_test_img "$TEST_DIR/t.$IMGFMT.base"
179 180 181 182
            if [ -n "$SAMPLE_IMG_FILE" ]
            then
                rm -f "$TEST_DIR/$SAMPLE_IMG_FILE"
            fi
183 184 185
            ;;

        rbd)
186
            rbd --no-progress rm "$TEST_DIR/t.$IMGFMT" > /dev/null
187 188
            ;;

189 190 191 192
        archipelago)
            vlmc remove "at.$IMGFMT" > /dev/null
            ;;

193
        sheepdog)
194
            collie vdi delete "$TEST_DIR/t.$IMGFMT"
195 196 197
            ;;

    esac
198 199 200 201
}

_check_test_img()
{
202
    $QEMU_IMG check "$@" -f $IMGFMT "$TEST_IMG" 2>&1 | _filter_testdir | \
203
        sed -e '/allocated.*fragmented.*compressed clusters/d' \
204 205
            -e 's/qemu-img: This image format does not support checks/No errors were found on the image./' \
            -e '/Image end offset: [0-9]\+/d'
206 207
}

208 209
_img_info()
{
210 211 212 213 214 215 216
    if [[ "$1" == "--format-specific" ]]; then
        local format_specific=1
        shift
    else
        local format_specific=0
    fi

217 218
    discard=0
    regex_json_spec_start='^ *"format-specific": \{'
219
    $QEMU_IMG info "$@" "$TEST_IMG" 2>&1 | \
220 221 222 223
        sed -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \
            -e "s#$TEST_DIR#TEST_DIR#g" \
            -e "s#$IMGFMT#IMGFMT#g" \
            -e "/^disk size:/ D" \
224 225
            -e "/actual-size/ D" | \
        while IFS='' read line; do
226 227 228
            if [[ $format_specific == 1 ]]; then
                discard=0
            elif [[ $line == "Format specific information:" ]]; then
229 230 231 232 233 234 235 236 237 238 239 240 241 242
                discard=1
            elif [[ $line =~ $regex_json_spec_start ]]; then
                discard=2
                regex_json_spec_end="^${line%%[^ ]*}\\},? *$"
            fi
            if [[ $discard == 0 ]]; then
                echo "$line"
            elif [[ $discard == 1 && ! $line ]]; then
                echo
                discard=0
            elif [[ $discard == 2 && $line =~ $regex_json_spec_end ]]; then
                discard=0
            fi
        done
243 244
}

245 246 247 248
_get_pids_by_name()
{
    if [ $# -ne 1 ]
    then
K
Kevin Wolf 已提交
249 250
        echo "Usage: _get_pids_by_name process-name" 1>&2
        exit 1
251 252 253 254 255 256 257 258 259 260 261
    fi

    # Algorithm ... all ps(1) variants have a time of the form MM:SS or
    # HH:MM:SS before the psargs field, use this as the search anchor.
    #
    # Matches with $1 (process-name) occur if the first psarg is $1
    # or ends in /$1 ... the matching uses sed's regular expressions,
    # so passing a regex into $1 will work.

    ps $PS_ALL_FLAGS \
    | sed -n \
K
Kevin Wolf 已提交
262 263 264 265 266 267
        -e 's/$/ /' \
        -e 's/[         ][         ]*/ /g' \
        -e 's/^ //' \
        -e 's/^[^ ]* //' \
        -e "/[0-9]:[0-9][0-9]  *[^ ]*\/$1 /s/ .*//p" \
        -e "/[0-9]:[0-9][0-9]  *$1 /s/ .*//p"
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
}

# fqdn for localhost
#
_get_fqdn()
{
    host=`hostname`
    $NSLOOKUP_PROG $host | $AWK_PROG '{ if ($1 == "Name:") print $2 }'
}

# check if run as root
#
_need_to_be_root()
{
    id=`id | $SED_PROG -e 's/(.*//' -e 's/.*=//'`
    if [ "$id" -ne 0 ]
    then
K
Kevin Wolf 已提交
285 286
        echo "Arrgh ... you need to be root (not uid=$id) to run this test"
        exit 1
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
    fi
}


# Do a command, log it to $seq.full, optionally test return status
# and die if command fails. If called with one argument _do executes the
# command, logs it, and returns its exit status. With two arguments _do
# first prints the message passed in the first argument, and then "done"
# or "fail" depending on the return status of the command passed in the
# second argument. If the command fails and the variable _do_die_on_error
# is set to "always" or the two argument form is used and _do_die_on_error
# is set to "message_only" _do will print an error message to
# $seq.out and exit.

_do()
{
    if [ $# -eq 1 ]; then
K
Kevin Wolf 已提交
304
        _cmd=$1
305
    elif [ $# -eq 2 ]; then
K
Kevin Wolf 已提交
306 307 308
        _note=$1
        _cmd=$2
        echo -n "$_note... "
309
    else
K
Kevin Wolf 已提交
310 311
        echo "Usage: _do [note] cmd" 1>&2
        status=1; exit
312 313
    fi

M
Max Reitz 已提交
314
    (eval "echo '---' \"$_cmd\"") >>"$OUTPUT_DIR/$seq.full"
315
    (eval "$_cmd") >$tmp._out 2>&1; ret=$?
M
Max Reitz 已提交
316
    cat $tmp._out >>"$OUTPUT_DIR/$seq.full"
317
    if [ $# -eq 2 ]; then
K
Kevin Wolf 已提交
318 319 320 321 322
        if [ $ret -eq 0 ]; then
            echo "done"
        else
            echo "fail"
        fi
323 324
    fi
    if [ $ret -ne 0  ] \
K
Kevin Wolf 已提交
325 326
        && [ "$_do_die_on_error" = "always" \
            -o \( $# -eq 2 -a "$_do_die_on_error" = "message_only" \) ]
327
    then
K
Kevin Wolf 已提交
328 329 330
        [ $# -ne 2 ] && echo
        eval "echo \"$_cmd\" failed \(returned $ret\): see $seq.full"
        status=1; exit
331 332 333 334 335 336 337 338 339
    fi

    return $ret
}

# bail out, setting up .notrun file
#
_notrun()
{
M
Max Reitz 已提交
340
    echo "$*" >"$OUTPUT_DIR/$seq.notrun"
341 342 343 344 345 346 347 348 349
    echo "$seq not run: $*"
    status=0
    exit
}

# just plain bail out
#
_fail()
{
M
Max Reitz 已提交
350
    echo "$*" | tee -a "$OUTPUT_DIR/$seq.full"
351 352 353 354 355 356 357 358 359
    echo "(see $seq.full for details)"
    status=1
    exit 1
}

# tests whether $IMGFMT is one of the supported image formats for a test
#
_supported_fmt()
{
360 361 362
    # "generic" is suitable for most image formats. For some formats it doesn't
    # work, however (most notably read-only formats), so they can opt out by
    # setting IMGFMT_GENERIC to false.
363
    for f; do
364
        if [ "$f" = "$IMGFMT" -o "$f" = "generic" -a "$IMGFMT_GENERIC" = "true" ]; then
K
Kevin Wolf 已提交
365 366
            return
        fi
367 368 369 370 371
    done

    _notrun "not suitable for this image format: $IMGFMT"
}

372 373 374 375 376
# tests whether $IMGPROTO is one of the supported image protocols for a test
#
_supported_proto()
{
    for f; do
K
Kevin Wolf 已提交
377 378 379
        if [ "$f" = "$IMGPROTO" -o "$f" = "generic" ]; then
            return
        fi
380 381 382 383 384
    done

    _notrun "not suitable for this image protocol: $IMGPROTO"
}

385 386 387 388 389 390
# tests whether the host OS is one of the supported OSes for a test
#
_supported_os()
{
    for h
    do
K
Kevin Wolf 已提交
391 392 393 394
        if [ "$h" = "$HOSTOS" ]
        then
            return
        fi
395 396 397 398 399
    done

    _notrun "not suitable for this OS: $HOSTOS"
}

400
_supported_cache_modes()
401
{
402 403 404 405
    for mode; do
        if [ "$mode" = "$CACHEMODE" ]; then
            return
        fi
406
    done
407 408 409 410 411 412 413 414 415 416
    _notrun "not suitable for cache mode: $CACHEMODE"
}

_default_cache_mode()
{
    if $CACHEMODE_IS_DEFAULT; then
        CACHEMODE="$1"
        QEMU_IO="$QEMU_IO --cache $1"
        return
    fi
417 418
}

419 420 421 422 423 424 425 426 427 428 429
_unsupported_imgopts()
{
    for bad_opt
    do
        if echo "$IMGOPTS" | grep -q 2>/dev/null "$bad_opt"
        then
            _notrun "not suitable for image option: $bad_opt"
        fi
    done
}

430 431 432 433
# this test requires that a specified command (executable) exists
#
_require_command()
{
434 435 436 437 438 439 440 441 442 443 444
    if [ "$1" = "QEMU" ]; then
        c=$QEMU_PROG
    elif [ "$1" = "QEMU_IMG" ]; then
        c=$QEMU_IMG_PROG
    elif [ "$1" = "QEMU_IO" ]; then
        c=$QEMU_IO_PROG
    elif [ "$1" = "QEMU_NBD" ]; then
        c=$QEMU_NBD_PROG
    else
        eval c=\$$1
    fi
445
    [ -x "$c" ] || _notrun "$1 utility required, skipped this test"
446 447 448 449
}

_full_imgfmt_details()
{
450 451 452 453 454
    if [ -n "$IMGOPTS" ]; then
        echo "$IMGFMT ($IMGOPTS)"
    else
        echo "$IMGFMT"
    fi
455 456
}

457 458 459 460 461
_full_imgproto_details()
{
    echo "$IMGPROTO"
}

462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
_full_platform_details()
{
    os=`uname -s`
    host=`hostname -s`
    kernel=`uname -r`
    platform=`uname -m`
    echo "$os/$platform $host $kernel"
}

_link_out_file()
{
   if [ -z "$1" ]; then
      echo Error must pass \$seq.
      exit
   fi
   rm -f $1
   if [ "`uname`" == "IRIX64" ] || [ "`uname`" == "IRIX" ]; then
      ln -s $1.irix $1
   elif [ "`uname`" == "Linux" ]; then
      ln -s $1.linux $1
   else
      echo Error test $seq does not run on the operating system: `uname`
      exit
   fi
}

_die()
{
        echo $@
        exit 1
}

# make sure this script returns success
495
true