Statistics
| Branch: | Revision:

root / tests / qemu-iotests / common.rc @ b3ce604e

History | View | Annotate | Download (7.3 kB)

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

    
20
dd()
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
# we need common.config
38
if [ "$iam" != "check" ]
39
then
40
    if ! . ./common.config
41
        then
42
        echo "$iam: failed to source common.config"
43
        exit 1
44
    fi
45
fi
46

    
47
# make sure we have a standard umask
48
umask 022
49

    
50
if [ "$IMGPROTO" = "file" ]; then
51
    TEST_IMG=$TEST_DIR/t.$IMGFMT
52
else
53
    TEST_IMG=$IMGPROTO:$TEST_DIR/t.$IMGFMT
54
fi
55

    
56
_optstr_add()
57
{
58
    if [ -n "$1" ]; then
59
        echo "$1,$2"
60
    else
61
        echo "$2"
62
    fi
63
}
64

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

    
72
_make_test_img()
73
{
74
    # extra qemu-img options can be added by tests
75
    # at least one argument (the image size) needs to be added
76
    local extra_img_options=""
77
    local image_size=$*
78
    local optstr=""
79

    
80
    if [ -n "$IMGOPTS" ]; then
81
        optstr=$(_optstr_add "$optstr" "$IMGOPTS")
82
    fi
83

    
84
    if [ "$1" = "-b" ]; then
85
        extra_img_options="$1 $2"
86
        image_size=$3
87
    fi
88
    if [ \( "$IMGFMT" = "qcow2" -o "$IMGFMT" = "qed" \) -a -n "$CLUSTER_SIZE" ]; then
89
        optstr=$(_optstr_add "$optstr" "cluster_size=$CLUSTER_SIZE")
90
    fi
91

    
92
    if [ -n "$optstr" ]; then
93
        extra_img_options="-o $optstr $extra_img_options"
94
    fi
95

    
96
    # XXX(hch): have global image options?
97
    $QEMU_IMG create -f $IMGFMT $extra_img_options $TEST_IMG $image_size | \
98
    	sed -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" | \
99
    	sed -e "s#$TEST_DIR#TEST_DIR#g" | \
100
    	sed -e "s#$IMGFMT#IMGFMT#g" | \
101
	sed -e "s# encryption=off##g" | \
102
	sed -e "s# cluster_size=[0-9]\\+##g" | \
103
	sed -e "s# table_size=0##g" | \
104
	sed -e "s# compat='[^']*'##g" | \
105
	sed -e "s# compat6=off##g" | \
106
	sed -e "s# static=off##g"
107
}
108

    
109
_cleanup_test_img()
110
{
111
    case "$IMGPROTO" in
112

    
113
        file)
114
            rm -f $TEST_DIR/t.$IMGFMT
115
            rm -f $TEST_DIR/t.$IMGFMT.orig
116
            rm -f $TEST_DIR/t.$IMGFMT.base
117
            ;;
118

    
119
        rbd)
120
            rbd rm $TEST_DIR/t.$IMGFMT > /dev/null
121
            ;;
122

    
123
        sheepdog)
124
            collie vdi delete $TEST_DIR/t.$IMGFMT
125
            ;;
126

    
127
    esac
128
}
129

    
130
_check_test_img()
131
{
132
    $QEMU_IMG check -f $IMGFMT $TEST_IMG 2>&1 | \
133
        grep -v "fragmented$" | \
134
    	sed -e 's/qemu-img\: This image format does not support checks/No errors were found on the image./'
135
}
136

    
137
_get_pids_by_name()
138
{
139
    if [ $# -ne 1 ]
140
    then
141
	echo "Usage: _get_pids_by_name process-name" 1>&2
142
	exit 1
143
    fi
144

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

    
152
    ps $PS_ALL_FLAGS \
153
    | sed -n \
154
	-e 's/$/ /' \
155
	-e 's/[ 	][ 	]*/ /g' \
156
	-e 's/^ //' \
157
	-e 's/^[^ ]* //' \
158
	-e "/[0-9]:[0-9][0-9]  *[^ ]*\/$1 /s/ .*//p" \
159
	-e "/[0-9]:[0-9][0-9]  *$1 /s/ .*//p"
160
}
161

    
162
# fqdn for localhost
163
#
164
_get_fqdn()
165
{
166
    host=`hostname`
167
    $NSLOOKUP_PROG $host | $AWK_PROG '{ if ($1 == "Name:") print $2 }'
168
}
169

    
170
# check if run as root
171
#
172
_need_to_be_root()
173
{
174
    id=`id | $SED_PROG -e 's/(.*//' -e 's/.*=//'`
175
    if [ "$id" -ne 0 ]
176
    then
177
	echo "Arrgh ... you need to be root (not uid=$id) to run this test"
178
	exit 1
179
    fi
180
}
181

    
182

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

    
193
_do()
194
{
195
    if [ $# -eq 1 ]; then
196
	_cmd=$1
197
    elif [ $# -eq 2 ]; then
198
	_note=$1
199
	_cmd=$2
200
	echo -n "$_note... "
201
    else
202
	echo "Usage: _do [note] cmd" 1>&2
203
	status=1; exit
204
    fi
205

    
206
    (eval "echo '---' \"$_cmd\"") >>$here/$seq.full
207
    (eval "$_cmd") >$tmp._out 2>&1; ret=$?
208
    cat $tmp._out >>$here/$seq.full
209
    if [ $# -eq 2 ]; then
210
	if [ $ret -eq 0 ]; then
211
	    echo "done"
212
	else
213
	    echo "fail"
214
	fi
215
    fi
216
    if [ $ret -ne 0  ] \
217
	&& [ "$_do_die_on_error" = "always" \
218
	    -o \( $# -eq 2 -a "$_do_die_on_error" = "message_only" \) ]
219
    then
220
	[ $# -ne 2 ] && echo
221
	eval "echo \"$_cmd\" failed \(returned $ret\): see $seq.full"
222
	status=1; exit
223
    fi
224

    
225
    return $ret
226
}
227

    
228
# bail out, setting up .notrun file
229
#
230
_notrun()
231
{
232
    echo "$*" >$seq.notrun
233
    echo "$seq not run: $*"
234
    status=0
235
    exit
236
}
237

    
238
# just plain bail out
239
#
240
_fail()
241
{
242
    echo "$*" | tee -a $here/$seq.full
243
    echo "(see $seq.full for details)"
244
    status=1
245
    exit 1
246
}
247

    
248
# tests whether $IMGFMT is one of the supported image formats for a test
249
#
250
_supported_fmt()
251
{
252
    for f; do
253
	if [ "$f" = "$IMGFMT" -o "$f" = "generic" ]; then
254
	    return
255
	fi
256
    done
257

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

    
261
# tests whether $IMGPROTO is one of the supported image protocols for a test
262
#
263
_supported_proto()
264
{
265
    for f; do
266
	if [ "$f" = "$IMGPROTO" -o "$f" = "generic" ]; then
267
	    return
268
	fi
269
    done
270

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

    
274
# tests whether the host OS is one of the supported OSes for a test
275
#
276
_supported_os()
277
{
278
    for h
279
    do
280
	if [ "$h" = "$HOSTOS" ]
281
	then
282
	    return
283
	fi
284
    done
285

    
286
    _notrun "not suitable for this OS: $HOSTOS"
287
}
288

    
289
# this test requires that a specified command (executable) exists
290
#
291
_require_command()
292
{
293
    [ -x "$1" ] || _notrun "$1 utility required, skipped this test"
294
}
295

    
296
_full_imgfmt_details()
297
{
298
    if [ -n "$IMGOPTS" ]; then
299
        echo "$IMGFMT ($IMGOPTS)"
300
    else
301
        echo "$IMGFMT"
302
    fi
303
}
304

    
305
_full_imgproto_details()
306
{
307
    echo "$IMGPROTO"
308
}
309

    
310
_full_platform_details()
311
{
312
    os=`uname -s`
313
    host=`hostname -s`
314
    kernel=`uname -r`
315
    platform=`uname -m`
316
    echo "$os/$platform $host $kernel"
317
}
318

    
319
_link_out_file()
320
{
321
   if [ -z "$1" ]; then
322
      echo Error must pass \$seq.
323
      exit
324
   fi
325
   rm -f $1
326
   if [ "`uname`" == "IRIX64" ] || [ "`uname`" == "IRIX" ]; then
327
      ln -s $1.irix $1
328
   elif [ "`uname`" == "Linux" ]; then
329
      ln -s $1.linux $1
330
   else
331
      echo Error test $seq does not run on the operating system: `uname`
332
      exit
333
   fi
334
}
335

    
336
_die()
337
{
338
        echo $@
339
        exit 1
340
}
341

    
342
# make sure this script returns success
343
/bin/true