]> git.proxmox.com Git - mirror_qemu.git/blame - tests/qemu-iotests/common.rc
qemu-iotests: Add basic ability to use binary sample images
[mirror_qemu.git] / tests / qemu-iotests / common.rc
CommitLineData
908eaf68 1#!/bin/bash
6bf19c94
CH
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
e8c212d6 17# along with this program. If not, see <http://www.gnu.org/licenses/>.
6bf19c94
CH
18#
19
20dd()
21{
22 if [ "$HOSTOS" == "Linux" ]
79e40ab1
KW
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
6bf19c94 32 else
79e40ab1 33 command dd $@
6bf19c94
CH
34 fi
35}
36
23ea2ecc
SH
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
6bf19c94
CH
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
9cdfa1b3
MK
56if [ "$IMGPROTO" = "file" ]; then
57 TEST_IMG=$TEST_DIR/t.$IMGFMT
a9660664
NT
58elif [ "$IMGPROTO" = "nbd" ]; then
59 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
60 TEST_IMG="nbd:127.0.0.1:10810"
342809e8
RJ
61elif [ "$IMGPROTO" = "ssh" ]; then
62 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
63 TEST_IMG="ssh://127.0.0.1$TEST_IMG_FILE"
9cdfa1b3
MK
64else
65 TEST_IMG=$IMGPROTO:$TEST_DIR/t.$IMGFMT
66fi
6bf19c94 67
2f24e8fb
KW
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
89004368
KW
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
85edbd37
JC
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
6bf19c94
CH
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
21af8148 110 local extra_img_options=""
21af8148 111 local image_size=$*
89004368 112 local optstr=""
a9660664
NT
113 local img_name=""
114
115 if [ -n "$TEST_IMG_FILE" ]; then
116 img_name=$TEST_IMG_FILE
117 else
118 img_name=$TEST_IMG
119 fi
89004368
KW
120
121 if [ -n "$IMGOPTS" ]; then
122 optstr=$(_optstr_add "$optstr" "$IMGOPTS")
123 fi
6bf19c94 124
21af8148
SW
125 if [ "$1" = "-b" ]; then
126 extra_img_options="$1 $2"
127 image_size=$3
128 fi
f5a4bbd9 129 if [ \( "$IMGFMT" = "qcow2" -o "$IMGFMT" = "qed" \) -a -n "$CLUSTER_SIZE" ]; then
89004368
KW
130 optstr=$(_optstr_add "$optstr" "cluster_size=$CLUSTER_SIZE")
131 fi
132
133 if [ -n "$optstr" ]; then
134 extra_img_options="-o $optstr $extra_img_options"
8fc1024c
KW
135 fi
136
6bf19c94 137 # XXX(hch): have global image options?
2c78857b 138 $QEMU_IMG create -f $IMGFMT $extra_img_options $img_name $image_size 2>&1 | \
353a41be
KW
139 sed -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \
140 -e "s#$TEST_DIR#TEST_DIR#g" \
141 -e "s#$IMGFMT#IMGFMT#g" \
142 -e "s# encryption=off##g" \
143 -e "s# cluster_size=[0-9]\\+##g" \
144 -e "s# table_size=[0-9]\\+##g" \
145 -e "s# compat='[^']*'##g" \
146 -e "s# compat6=\\(on\\|off\\)##g" \
147 -e "s# static=\\(on\\|off\\)##g" \
50522d96
FZ
148 -e "s# zeroed_grain=\\(on\\|off\\)##g" \
149 -e "s# subformat='[^']*'##g" \
86abefd6 150 -e "s# adapter_type='[^']*'##g" \
353a41be 151 -e "s# lazy_refcounts=\\(on\\|off\\)##g"
a9660664
NT
152
153 # Start an NBD server on the image file, which is what we'll be talking to
154 if [ $IMGPROTO = "nbd" ]; then
155 eval "$QEMU_NBD -v -t -b 127.0.0.1 -p 10810 $TEST_IMG_FILE &"
156 QEMU_NBD_PID=$!
157 sleep 1 # FIXME: qemu-nbd needs to be listening before we continue
158 fi
6bf19c94
CH
159}
160
161_cleanup_test_img()
162{
9cdfa1b3
MK
163 case "$IMGPROTO" in
164
a9660664
NT
165 nbd)
166 kill $QEMU_NBD_PID
167 rm -f $TEST_IMG_FILE
168 ;;
9cdfa1b3
MK
169 file)
170 rm -f $TEST_DIR/t.$IMGFMT
171 rm -f $TEST_DIR/t.$IMGFMT.orig
172 rm -f $TEST_DIR/t.$IMGFMT.base
85edbd37
JC
173 if [ -n "$SAMPLE_IMG_FILE" ]
174 then
175 rm -f "$TEST_DIR/$SAMPLE_IMG_FILE"
176 fi
9cdfa1b3
MK
177 ;;
178
179 rbd)
180 rbd rm $TEST_DIR/t.$IMGFMT > /dev/null
181 ;;
182
183 sheepdog)
184 collie vdi delete $TEST_DIR/t.$IMGFMT
185 ;;
186
187 esac
6bf19c94
CH
188}
189
190_check_test_img()
191{
bd91ecbf 192 $QEMU_IMG check "$@" -f $IMGFMT $TEST_IMG 2>&1 | _filter_testdir | \
e6439d78 193 sed -e '/allocated.*fragmented.*compressed clusters/d' \
c6bb9ad1
FS
194 -e 's/qemu-img: This image format does not support checks/No errors were found on the image./' \
195 -e '/Image end offset: [0-9]\+/d'
6bf19c94
CH
196}
197
514d9da5
SH
198_img_info()
199{
200 $QEMU_IMG info "$@" $TEST_IMG 2>&1 | \
201 sed -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \
202 -e "s#$TEST_DIR#TEST_DIR#g" \
203 -e "s#$IMGFMT#IMGFMT#g" \
204 -e "/^disk size:/ D" \
205 -e "/actual-size/ D"
206}
207
6bf19c94
CH
208_get_pids_by_name()
209{
210 if [ $# -ne 1 ]
211 then
79e40ab1
KW
212 echo "Usage: _get_pids_by_name process-name" 1>&2
213 exit 1
6bf19c94
CH
214 fi
215
216 # Algorithm ... all ps(1) variants have a time of the form MM:SS or
217 # HH:MM:SS before the psargs field, use this as the search anchor.
218 #
219 # Matches with $1 (process-name) occur if the first psarg is $1
220 # or ends in /$1 ... the matching uses sed's regular expressions,
221 # so passing a regex into $1 will work.
222
223 ps $PS_ALL_FLAGS \
224 | sed -n \
79e40ab1
KW
225 -e 's/$/ /' \
226 -e 's/[ ][ ]*/ /g' \
227 -e 's/^ //' \
228 -e 's/^[^ ]* //' \
229 -e "/[0-9]:[0-9][0-9] *[^ ]*\/$1 /s/ .*//p" \
230 -e "/[0-9]:[0-9][0-9] *$1 /s/ .*//p"
6bf19c94
CH
231}
232
233# fqdn for localhost
234#
235_get_fqdn()
236{
237 host=`hostname`
238 $NSLOOKUP_PROG $host | $AWK_PROG '{ if ($1 == "Name:") print $2 }'
239}
240
241# check if run as root
242#
243_need_to_be_root()
244{
245 id=`id | $SED_PROG -e 's/(.*//' -e 's/.*=//'`
246 if [ "$id" -ne 0 ]
247 then
79e40ab1
KW
248 echo "Arrgh ... you need to be root (not uid=$id) to run this test"
249 exit 1
6bf19c94
CH
250 fi
251}
252
253
254# Do a command, log it to $seq.full, optionally test return status
255# and die if command fails. If called with one argument _do executes the
256# command, logs it, and returns its exit status. With two arguments _do
257# first prints the message passed in the first argument, and then "done"
258# or "fail" depending on the return status of the command passed in the
259# second argument. If the command fails and the variable _do_die_on_error
260# is set to "always" or the two argument form is used and _do_die_on_error
261# is set to "message_only" _do will print an error message to
262# $seq.out and exit.
263
264_do()
265{
266 if [ $# -eq 1 ]; then
79e40ab1 267 _cmd=$1
6bf19c94 268 elif [ $# -eq 2 ]; then
79e40ab1
KW
269 _note=$1
270 _cmd=$2
271 echo -n "$_note... "
6bf19c94 272 else
79e40ab1
KW
273 echo "Usage: _do [note] cmd" 1>&2
274 status=1; exit
6bf19c94
CH
275 fi
276
277 (eval "echo '---' \"$_cmd\"") >>$here/$seq.full
278 (eval "$_cmd") >$tmp._out 2>&1; ret=$?
279 cat $tmp._out >>$here/$seq.full
280 if [ $# -eq 2 ]; then
79e40ab1
KW
281 if [ $ret -eq 0 ]; then
282 echo "done"
283 else
284 echo "fail"
285 fi
6bf19c94
CH
286 fi
287 if [ $ret -ne 0 ] \
79e40ab1
KW
288 && [ "$_do_die_on_error" = "always" \
289 -o \( $# -eq 2 -a "$_do_die_on_error" = "message_only" \) ]
6bf19c94 290 then
79e40ab1
KW
291 [ $# -ne 2 ] && echo
292 eval "echo \"$_cmd\" failed \(returned $ret\): see $seq.full"
293 status=1; exit
6bf19c94
CH
294 fi
295
296 return $ret
297}
298
299# bail out, setting up .notrun file
300#
301_notrun()
302{
303 echo "$*" >$seq.notrun
304 echo "$seq not run: $*"
305 status=0
306 exit
307}
308
309# just plain bail out
310#
311_fail()
312{
313 echo "$*" | tee -a $here/$seq.full
314 echo "(see $seq.full for details)"
315 status=1
316 exit 1
317}
318
319# tests whether $IMGFMT is one of the supported image formats for a test
320#
321_supported_fmt()
322{
323 for f; do
79e40ab1
KW
324 if [ "$f" = "$IMGFMT" -o "$f" = "generic" ]; then
325 return
326 fi
6bf19c94
CH
327 done
328
329 _notrun "not suitable for this image format: $IMGFMT"
330}
331
9cdfa1b3
MK
332# tests whether $IMGPROTO is one of the supported image protocols for a test
333#
334_supported_proto()
335{
336 for f; do
79e40ab1
KW
337 if [ "$f" = "$IMGPROTO" -o "$f" = "generic" ]; then
338 return
339 fi
9cdfa1b3
MK
340 done
341
342 _notrun "not suitable for this image protocol: $IMGPROTO"
343}
344
6bf19c94
CH
345# tests whether the host OS is one of the supported OSes for a test
346#
347_supported_os()
348{
349 for h
350 do
79e40ab1
KW
351 if [ "$h" = "$HOSTOS" ]
352 then
353 return
354 fi
6bf19c94
CH
355 done
356
357 _notrun "not suitable for this OS: $HOSTOS"
358}
359
166f3c7b
SH
360_unsupported_qemu_io_options()
361{
362 for bad_opt
363 do
364 for opt in $QEMU_IO_OPTIONS
365 do
366 if [ "$bad_opt" = "$opt" ]
367 then
368 _notrun "not suitable for qemu-io option: $bad_opt"
369 fi
370 done
371 done
372}
373
6bf19c94
CH
374# this test requires that a specified command (executable) exists
375#
376_require_command()
377{
378 [ -x "$1" ] || _notrun "$1 utility required, skipped this test"
379}
380
381_full_imgfmt_details()
382{
89004368
KW
383 if [ -n "$IMGOPTS" ]; then
384 echo "$IMGFMT ($IMGOPTS)"
385 else
386 echo "$IMGFMT"
387 fi
6bf19c94
CH
388}
389
9cdfa1b3
MK
390_full_imgproto_details()
391{
392 echo "$IMGPROTO"
393}
394
6bf19c94
CH
395_full_platform_details()
396{
397 os=`uname -s`
398 host=`hostname -s`
399 kernel=`uname -r`
400 platform=`uname -m`
401 echo "$os/$platform $host $kernel"
402}
403
404_link_out_file()
405{
406 if [ -z "$1" ]; then
407 echo Error must pass \$seq.
408 exit
409 fi
410 rm -f $1
411 if [ "`uname`" == "IRIX64" ] || [ "`uname`" == "IRIX" ]; then
412 ln -s $1.irix $1
413 elif [ "`uname`" == "Linux" ]; then
414 ln -s $1.linux $1
415 else
416 echo Error test $seq does not run on the operating system: `uname`
417 exit
418 fi
419}
420
421_die()
422{
423 echo $@
424 exit 1
425}
426
427# make sure this script returns success
428/bin/true