aboutsummaryrefslogtreecommitdiff
path: root/tests/qemu-iotests/common.rc
blob: cb5fa14e7f8f71405b56d62275019eae8a937eac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
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
495
496
497
#!/bin/bash
#
# 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
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

dd()
{
   if [ "$HOSTOS" == "Linux" ]
   then
        command dd --help | grep noxfer > /dev/null 2>&1

        if [ "$?" -eq 0 ]
            then
                command dd status=noxfer $@
            else
                command dd $@
            fi
   else
        command dd $@
   fi
}

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


if ! . ./common.config
    then
    echo "$0: failed to source common.config"
    exit 1
fi

_qemu_wrapper()
{
    (
        if [ -n "${QEMU_NEED_PID}" ]; then
            echo $BASHPID > "${QEMU_TEST_DIR}/qemu-${_QEMU_HANDLE}.pid"
        fi
        exec "$QEMU_PROG" $QEMU_OPTIONS "$@"
    )
}

_qemu_img_wrapper()
{
    (exec "$QEMU_IMG_PROG" $QEMU_IMG_OPTIONS "$@")
}

_qemu_io_wrapper()
{
    local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind
    local QEMU_IO_ARGS="$QEMU_IO_OPTIONS"
    if [ "$IMGOPTSSYNTAX" = "true" ]; then
        QEMU_IO_ARGS="--image-opts $QEMU_IO_ARGS"
        if [ -n "$IMGKEYSECRET" ]; then
            QEMU_IO_ARGS="--object secret,id=keysec0,data=$IMGKEYSECRET $QEMU_IO_ARGS"
        fi
    fi
    local RETVAL
    (
        if [ "${VALGRIND_QEMU}" == "y" ]; then
            exec valgrind --log-file="${VALGRIND_LOGFILE}" --error-exitcode=99 "$QEMU_IO_PROG" $QEMU_IO_ARGS "$@"
        else
            exec "$QEMU_IO_PROG" $QEMU_IO_ARGS "$@"
        fi
    )
    RETVAL=$?
    if [ "${VALGRIND_QEMU}" == "y" ]; then
        if [ $RETVAL == 99 ]; then
            cat "${VALGRIND_LOGFILE}"
        fi
        rm -f "${VALGRIND_LOGFILE}"
    fi
    (exit $RETVAL)
}

_qemu_nbd_wrapper()
{
    (
        echo $BASHPID > "${QEMU_TEST_DIR}/qemu-nbd.pid"
        exec "$QEMU_NBD_PROG" $QEMU_NBD_OPTIONS "$@"
    )
}

_qemu_vxhs_wrapper()
{
    (
        echo $BASHPID > "${TEST_DIR}/qemu-vxhs.pid"
        exec "$QEMU_VXHS_PROG" $QEMU_VXHS_OPTIONS "$@"
    )
}

export QEMU=_qemu_wrapper
export QEMU_IMG=_qemu_img_wrapper
export QEMU_IO=_qemu_io_wrapper
export QEMU_NBD=_qemu_nbd_wrapper
export QEMU_VXHS=_qemu_vxhs_wrapper

if [ "$IMGOPTSSYNTAX" = "true" ]; then
    DRIVER="driver=$IMGFMT"
    QEMU_IMG_EXTRA_ARGS="--image-opts $QEMU_IMG_EXTRA_ARGS"
    if [ -n "$IMGKEYSECRET" ]; then
        QEMU_IMG_EXTRA_ARGS="--object secret,id=keysec0,data=$IMGKEYSECRET $QEMU_IMG_EXTRA_ARGS"
    fi
    if [ "$IMGFMT" = "luks" ]; then
        DRIVER="$DRIVER,key-secret=keysec0"
    fi
    if [ "$IMGPROTO" = "file" ]; then
        TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
        TEST_IMG="$DRIVER,file.filename=$TEST_DIR/t.$IMGFMT"
    elif [ "$IMGPROTO" = "nbd" ]; then
        TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
        TEST_IMG="$DRIVER,file.driver=nbd,file.host=127.0.0.1,file.port=10810"
    elif [ "$IMGPROTO" = "ssh" ]; then
        TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
        TEST_IMG="$DRIVER,file.driver=ssh,file.host=127.0.0.1,file.path=$TEST_IMG_FILE"
    elif [ "$IMGPROTO" = "nfs" ]; then
        TEST_DIR="$DRIVER,file.driver=nfs,file.filename=nfs://127.0.0.1/$TEST_DIR"
        TEST_IMG=$TEST_DIR/t.$IMGFMT
    else
        TEST_IMG="$DRIVER,file.driver=$IMGPROTO,file.filename=$TEST_DIR/t.$IMGFMT"
    fi
else
    QEMU_IMG_EXTRA_ARGS=
    if [ "$IMGPROTO" = "file" ]; then
        TEST_IMG=$TEST_DIR/t.$IMGFMT
    elif [ "$IMGPROTO" = "nbd" ]; then
        TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
        TEST_IMG="nbd:127.0.0.1:10810"
    elif [ "$IMGPROTO" = "ssh" ]; then
        TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
        TEST_IMG="ssh://127.0.0.1$TEST_IMG_FILE"
    elif [ "$IMGPROTO" = "nfs" ]; then
        TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
        TEST_IMG="nfs://127.0.0.1$TEST_IMG_FILE"
    elif [ "$IMGPROTO" = "vxhs" ]; then
        TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
        TEST_IMG="vxhs://127.0.0.1:9999/t.$IMGFMT"
    else
        TEST_IMG=$IMGPROTO:$TEST_DIR/t.$IMGFMT
    fi
fi
ORIG_TEST_IMG="$TEST_IMG"

if [ -z "$TEST_DIR" ]; then
        TEST_DIR=`pwd`/scratch
fi

QEMU_TEST_DIR="${TEST_DIR}"

if [ ! -e "$TEST_DIR" ]; then
        mkdir "$TEST_DIR"
fi

if [ ! -d "$TEST_DIR" ]; then
    echo "common.config: Error: \$TEST_DIR ($TEST_DIR) is not a directory"
    exit 1
fi

if [ ! -d "$SAMPLE_IMG_DIR" ]; then
    echo "common.config: Error: \$SAMPLE_IMG_DIR ($SAMPLE_IMG_DIR) is not a directory"
    exit 1
fi

_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
}

_make_test_img()
{
    # extra qemu-img options can be added by tests
    # at least one argument (the image size) needs to be added
    local extra_img_options=""
    local image_size=$*
    local optstr=""
    local img_name=""
    local use_backing=0
    local backing_file=""
    local object_options=""

    if [ -n "$TEST_IMG_FILE" ]; then
        img_name=$TEST_IMG_FILE
    else
        img_name=$TEST_IMG
    fi

    if [ -n "$IMGOPTS" ]; then
        optstr=$(_optstr_add "$optstr" "$IMGOPTS")
    fi
    if [ -n "$IMGKEYSECRET" ]; then
        object_options="--object secret,id=keysec0,data=$IMGKEYSECRET"
        optstr=$(_optstr_add "$optstr" "key-secret=keysec0")
    fi

    if [ "$1" = "-b" ]; then
        use_backing=1
        backing_file=$2
        image_size=$3
    fi
    if [ \( "$IMGFMT" = "qcow2" -o "$IMGFMT" = "qed" \) -a -n "$CLUSTER_SIZE" ]; then
        optstr=$(_optstr_add "$optstr" "cluster_size=$CLUSTER_SIZE")
    fi

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

    # XXX(hch): have global image options?
    (
     if [ $use_backing = 1 ]; then
        $QEMU_IMG create $object_options -f $IMGFMT $extra_img_options -b "$backing_file" "$img_name" $image_size 2>&1
     else
        $QEMU_IMG create $object_options -f $IMGFMT $extra_img_options "$img_name" $image_size 2>&1
     fi
    ) | _filter_img_create

    # Start an NBD server on the image file, which is what we'll be talking to
    if [ $IMGPROTO = "nbd" ]; then
        # Pass a sufficiently high number to -e that should be enough for all
        # tests
        eval "$QEMU_NBD -v -t -b 127.0.0.1 -p 10810 -f $IMGFMT -e 42 -x '' $TEST_IMG_FILE >/dev/null &"
        sleep 1 # FIXME: qemu-nbd needs to be listening before we continue
    fi

    # Start QNIO server on image directory for vxhs protocol
    if [ $IMGPROTO = "vxhs" ]; then
        eval "$QEMU_VXHS -d  $TEST_DIR > /dev/null &"
        sleep 1 # Wait for server to come up.
    fi
}

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

_cleanup_test_img()
{
    case "$IMGPROTO" in

        nbd)
            if [ -f "${QEMU_TEST_DIR}/qemu-nbd.pid" ]; then
                local QEMU_NBD_PID
                read QEMU_NBD_PID < "${QEMU_TEST_DIR}/qemu-nbd.pid"
                kill ${QEMU_NBD_PID}
                rm -f "${QEMU_TEST_DIR}/qemu-nbd.pid"
            fi
            rm -f "$TEST_IMG_FILE"
            ;;
        vxhs)
            if [ -f "${TEST_DIR}/qemu-vxhs.pid" ]; then
                local QEMU_VXHS_PID
                read QEMU_VXHS_PID < "${TEST_DIR}/qemu-vxhs.pid"
                kill ${QEMU_VXHS_PID} >/dev/null 2>&1
                rm -f "${TEST_DIR}/qemu-vxhs.pid"
            fi
            rm -f "$TEST_IMG_FILE"
            ;;

        file)
            _rm_test_img "$TEST_DIR/t.$IMGFMT"
            _rm_test_img "$TEST_DIR/t.$IMGFMT.orig"
            _rm_test_img "$TEST_DIR/t.$IMGFMT.base"
            if [ -n "$SAMPLE_IMG_FILE" ]
            then
                rm -f "$TEST_DIR/$SAMPLE_IMG_FILE"
                SAMPLE_IMG_FILE=
                TEST_IMG="$ORIG_TEST_IMG"
            fi
            ;;

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

        sheepdog)
            collie vdi delete "$TEST_DIR/t.$IMGFMT"
            ;;

    esac
}

_check_test_img()
{
    (
        if [ "$IMGOPTSSYNTAX" = "true" ]; then
            $QEMU_IMG check $QEMU_IMG_EXTRA_ARGS "$@" "$TEST_IMG" 2>&1
        else
            $QEMU_IMG check "$@" -f $IMGFMT "$TEST_IMG" 2>&1
        fi
    ) | _filter_testdir | _filter_qemu_img_check
}

_img_info()
{
    if [[ "$1" == "--format-specific" ]]; then
        local format_specific=1
        shift
    else
        local format_specific=0
    fi

    discard=0
    regex_json_spec_start='^ *"format-specific": \{'
    $QEMU_IMG info $QEMU_IMG_EXTRA_ARGS "$@" "$TEST_IMG" 2>&1 | \
        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" \
            -e "/actual-size/ D" | \
        while IFS='' read -r line; do
            if [[ $format_specific == 1 ]]; then
                discard=0
            elif [[ $line == "Format specific information:" ]]; then
                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
}

# bail out, setting up .notrun file
#
_notrun()
{
    echo "$*" >"$OUTPUT_DIR/$seq.notrun"
    echo "$seq not run: $*"
    status=0
    exit
}

# just plain bail out
#
_fail()
{
    echo "$*" | tee -a "$OUTPUT_DIR/$seq.full"
    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()
{
    # "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.
    for f; do
        if [ "$f" = "$IMGFMT" -o "$f" = "generic" -a "$IMGFMT_GENERIC" = "true" ]; then
            return
        fi
    done

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

# tests whether $IMGFMT is one of the unsupported image format for a test
#
_unsupported_fmt()
{
    for f; do
        if [ "$f" = "$IMGFMT" ]; then
            _notrun "not suitable for this image format: $IMGFMT"
        fi
    done
}

# tests whether $IMGPROTO is one of the supported image protocols for a test
#
_supported_proto()
{
    for f; do
        if [ "$f" = "$IMGPROTO" -o "$f" = "generic" ]; then
            return
        fi
    done

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

# tests whether $IMGPROTO is specified as an unsupported image protocol for a test
#
_unsupported_proto()
{
    for f; do
        if [ "$f" = "$IMGPROTO" ]; then
            _notrun "not suitable for this image protocol: $IMGPROTO"
            return
        fi
    done
}

# tests whether the host OS is one of the supported OSes for a test
#
_supported_os()
{
    for h
    do
        if [ "$h" = "$HOSTOS" ]
        then
            return
        fi
    done

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

_supported_cache_modes()
{
    for mode; do
        if [ "$mode" = "$CACHEMODE" ]; then
            return
        fi
    done
    _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
}

_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
}

# this test requires that a specified command (executable) exists
#
_require_command()
{
    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
    [ -x "$c" ] || _notrun "$1 utility required, skipped this test"
}

# make sure this script returns success
true