]> git.proxmox.com Git - mirror_qemu.git/blame - tests/qemu-iotests/common.rc
qemu-iotests: cleanup and fix search for programs
[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
076003f5
DB
53if [ "$IMGOPTSSYNTAX" = "true" ]; then
54 DRIVER="driver=$IMGFMT"
4e9b25fb
DB
55 if [ "$IMGFMT" = "luks" ]; then
56 DRIVER="$DRIVER,key-secret=keysec0"
57 fi
076003f5
DB
58 if [ "$IMGPROTO" = "file" ]; then
59 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
60 TEST_IMG="$DRIVER,file.filename=$TEST_DIR/t.$IMGFMT"
61 elif [ "$IMGPROTO" = "nbd" ]; then
62 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
63 TEST_IMG="$DRIVER,file.driver=nbd,file.host=127.0.0.1,file.port=10810"
64 elif [ "$IMGPROTO" = "ssh" ]; then
65 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
66 TEST_IMG="$DRIVER,file.driver=ssh,file.host=127.0.0.1,file.path=$TEST_IMG_FILE"
67 elif [ "$IMGPROTO" = "nfs" ]; then
68 TEST_DIR="$DRIVER,file.driver=nfs,file.filename=nfs://127.0.0.1/$TEST_DIR"
e5b77eec 69 TEST_IMG=$TEST_DIR/t.$IMGFMT
076003f5
DB
70 else
71 TEST_IMG="$DRIVER,file.driver=$IMGPROTO,file.filename=$TEST_DIR/t.$IMGFMT"
72 fi
9cdfa1b3 73else
076003f5
DB
74 if [ "$IMGPROTO" = "file" ]; then
75 TEST_IMG=$TEST_DIR/t.$IMGFMT
76 elif [ "$IMGPROTO" = "nbd" ]; then
77 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
78 TEST_IMG="nbd:127.0.0.1:10810"
79 elif [ "$IMGPROTO" = "ssh" ]; then
80 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
81 TEST_IMG="ssh://127.0.0.1$TEST_IMG_FILE"
82 elif [ "$IMGPROTO" = "nfs" ]; then
83 TEST_DIR="nfs://127.0.0.1/$TEST_DIR"
84 TEST_IMG=$TEST_DIR/t.$IMGFMT
ae0c0a3d
AM
85 elif [ "$IMGPROTO" = "vxhs" ]; then
86 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
87 TEST_IMG="vxhs://127.0.0.1:9999/t.$IMGFMT"
076003f5
DB
88 else
89 TEST_IMG=$IMGPROTO:$TEST_DIR/t.$IMGFMT
90 fi
9cdfa1b3 91fi
59fa68f3 92ORIG_TEST_IMG="$TEST_IMG"
6bf19c94 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 113 local img_name=""
0018c03f
JC
114 local use_backing=0
115 local backing_file=""
b7e875b2 116 local object_options=""
a9660664
NT
117
118 if [ -n "$TEST_IMG_FILE" ]; then
119 img_name=$TEST_IMG_FILE
120 else
121 img_name=$TEST_IMG
122 fi
89004368
KW
123
124 if [ -n "$IMGOPTS" ]; then
125 optstr=$(_optstr_add "$optstr" "$IMGOPTS")
126 fi
b7e875b2
DB
127 if [ -n "$IMGKEYSECRET" ]; then
128 object_options="--object secret,id=keysec0,data=$IMGKEYSECRET"
129 optstr=$(_optstr_add "$optstr" "key-secret=keysec0")
130 fi
6bf19c94 131
21af8148 132 if [ "$1" = "-b" ]; then
0018c03f
JC
133 use_backing=1
134 backing_file=$2
21af8148
SW
135 image_size=$3
136 fi
f5a4bbd9 137 if [ \( "$IMGFMT" = "qcow2" -o "$IMGFMT" = "qed" \) -a -n "$CLUSTER_SIZE" ]; then
89004368
KW
138 optstr=$(_optstr_add "$optstr" "cluster_size=$CLUSTER_SIZE")
139 fi
140
141 if [ -n "$optstr" ]; then
142 extra_img_options="-o $optstr $extra_img_options"
8fc1024c
KW
143 fi
144
6bf19c94 145 # XXX(hch): have global image options?
0018c03f
JC
146 (
147 if [ $use_backing = 1 ]; then
b7e875b2 148 $QEMU_IMG create $object_options -f $IMGFMT $extra_img_options -b "$backing_file" "$img_name" $image_size 2>&1
0018c03f 149 else
b7e875b2 150 $QEMU_IMG create $object_options -f $IMGFMT $extra_img_options "$img_name" $image_size 2>&1
0018c03f 151 fi
6ffb4cb6 152 ) | _filter_img_create
a9660664
NT
153
154 # Start an NBD server on the image file, which is what we'll be talking to
155 if [ $IMGPROTO = "nbd" ]; then
03a0aa37
HR
156 # Pass a sufficiently high number to -e that should be enough for all
157 # tests
158 eval "$QEMU_NBD -v -t -b 127.0.0.1 -p 10810 -f $IMGFMT -e 42 $TEST_IMG_FILE >/dev/null &"
a9660664
NT
159 sleep 1 # FIXME: qemu-nbd needs to be listening before we continue
160 fi
ae0c0a3d
AM
161
162 # Start QNIO server on image directory for vxhs protocol
163 if [ $IMGPROTO = "vxhs" ]; then
164 eval "$QEMU_VXHS -d $TEST_DIR > /dev/null &"
165 sleep 1 # Wait for server to come up.
166 fi
6bf19c94
CH
167}
168
487c1910
FZ
169_rm_test_img()
170{
171 local img=$1
172 if [ "$IMGFMT" = "vmdk" ]; then
173 # Remove all the extents for vmdk
c5575274 174 "$QEMU_IMG" info "$img" 2>/dev/null | grep 'filename:' | cut -f 2 -d: \
487c1910
FZ
175 | xargs -I {} rm -f "{}"
176 fi
c5575274 177 rm -f "$img"
487c1910
FZ
178}
179
6bf19c94
CH
180_cleanup_test_img()
181{
9cdfa1b3
MK
182 case "$IMGPROTO" in
183
a9660664 184 nbd)
846a1d11 185 if [ -f "${QEMU_TEST_DIR}/qemu-nbd.pid" ]; then
f6c8c2e0 186 local QEMU_NBD_PID
846a1d11 187 read QEMU_NBD_PID < "${QEMU_TEST_DIR}/qemu-nbd.pid"
f6c8c2e0 188 kill ${QEMU_NBD_PID}
846a1d11 189 rm -f "${QEMU_TEST_DIR}/qemu-nbd.pid"
f798068c 190 fi
fef9c191 191 rm -f "$TEST_IMG_FILE"
a9660664 192 ;;
ae0c0a3d
AM
193 vxhs)
194 if [ -f "${TEST_DIR}/qemu-vxhs.pid" ]; then
195 local QEMU_VXHS_PID
196 read QEMU_VXHS_PID < "${TEST_DIR}/qemu-vxhs.pid"
197 kill ${QEMU_VXHS_PID} >/dev/null 2>&1
198 rm -f "${TEST_DIR}/qemu-vxhs.pid"
199 fi
200 rm -f "$TEST_IMG_FILE"
201 ;;
202
9cdfa1b3 203 file)
487c1910
FZ
204 _rm_test_img "$TEST_DIR/t.$IMGFMT"
205 _rm_test_img "$TEST_DIR/t.$IMGFMT.orig"
206 _rm_test_img "$TEST_DIR/t.$IMGFMT.base"
85edbd37
JC
207 if [ -n "$SAMPLE_IMG_FILE" ]
208 then
209 rm -f "$TEST_DIR/$SAMPLE_IMG_FILE"
59fa68f3
KW
210 SAMPLE_IMG_FILE=
211 TEST_IMG="$ORIG_TEST_IMG"
85edbd37 212 fi
9cdfa1b3
MK
213 ;;
214
215 rbd)
9147d019 216 rbd --no-progress rm "$TEST_DIR/t.$IMGFMT" > /dev/null
9cdfa1b3
MK
217 ;;
218
219 sheepdog)
fef9c191 220 collie vdi delete "$TEST_DIR/t.$IMGFMT"
9cdfa1b3
MK
221 ;;
222
223 esac
6bf19c94
CH
224}
225
226_check_test_img()
227{
076003f5
DB
228 (
229 if [ "$IMGOPTSSYNTAX" = "true" ]; then
230 $QEMU_IMG check $QEMU_IMG_EXTRA_ARGS "$@" "$TEST_IMG" 2>&1
231 else
232 $QEMU_IMG check "$@" -f $IMGFMT "$TEST_IMG" 2>&1
233 fi
86ce1f6e 234 ) | _filter_testdir | _filter_qemu_img_check
6bf19c94
CH
235}
236
514d9da5
SH
237_img_info()
238{
e800e5d4
KW
239 if [[ "$1" == "--format-specific" ]]; then
240 local format_specific=1
241 shift
242 else
243 local format_specific=0
244 fi
245
4c2e9465
HR
246 discard=0
247 regex_json_spec_start='^ *"format-specific": \{'
fef9c191 248 $QEMU_IMG info "$@" "$TEST_IMG" 2>&1 | \
514d9da5
SH
249 sed -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \
250 -e "s#$TEST_DIR#TEST_DIR#g" \
251 -e "s#$IMGFMT#IMGFMT#g" \
252 -e "/^disk size:/ D" \
4c2e9465
HR
253 -e "/actual-size/ D" | \
254 while IFS='' read line; do
e800e5d4
KW
255 if [[ $format_specific == 1 ]]; then
256 discard=0
257 elif [[ $line == "Format specific information:" ]]; then
4c2e9465
HR
258 discard=1
259 elif [[ $line =~ $regex_json_spec_start ]]; then
260 discard=2
261 regex_json_spec_end="^${line%%[^ ]*}\\},? *$"
262 fi
263 if [[ $discard == 0 ]]; then
264 echo "$line"
265 elif [[ $discard == 1 && ! $line ]]; then
266 echo
267 discard=0
268 elif [[ $discard == 2 && $line =~ $regex_json_spec_end ]]; then
269 discard=0
270 fi
271 done
514d9da5
SH
272}
273
6bf19c94
CH
274# bail out, setting up .notrun file
275#
276_notrun()
277{
e8f8624d 278 echo "$*" >"$OUTPUT_DIR/$seq.notrun"
6bf19c94
CH
279 echo "$seq not run: $*"
280 status=0
281 exit
282}
283
284# just plain bail out
285#
286_fail()
287{
e8f8624d 288 echo "$*" | tee -a "$OUTPUT_DIR/$seq.full"
6bf19c94
CH
289 echo "(see $seq.full for details)"
290 status=1
291 exit 1
292}
293
294# tests whether $IMGFMT is one of the supported image formats for a test
295#
296_supported_fmt()
297{
47f73da0
SH
298 # "generic" is suitable for most image formats. For some formats it doesn't
299 # work, however (most notably read-only formats), so they can opt out by
300 # setting IMGFMT_GENERIC to false.
6bf19c94 301 for f; do
89e91181 302 if [ "$f" = "$IMGFMT" -o "$f" = "generic" -a "$IMGFMT_GENERIC" = "true" ]; then
79e40ab1
KW
303 return
304 fi
6bf19c94
CH
305 done
306
307 _notrun "not suitable for this image format: $IMGFMT"
308}
309
b4a2caa4
NS
310# tests whether $IMGFMT is one of the unsupported image format for a test
311#
312_unsupported_fmt()
313{
314 for f; do
315 if [ "$f" = "$IMGFMT" ]; then
316 _notrun "not suitable for this image format: $IMGFMT"
317 fi
318 done
319}
320
9cdfa1b3
MK
321# tests whether $IMGPROTO is one of the supported image protocols for a test
322#
323_supported_proto()
324{
325 for f; do
79e40ab1
KW
326 if [ "$f" = "$IMGPROTO" -o "$f" = "generic" ]; then
327 return
328 fi
9cdfa1b3
MK
329 done
330
331 _notrun "not suitable for this image protocol: $IMGPROTO"
332}
333
dfac03dc
JC
334# tests whether $IMGPROTO is specified as an unsupported image protocol for a test
335#
336_unsupported_proto()
337{
338 for f; do
339 if [ "$f" = "$IMGPROTO" ]; then
340 _notrun "not suitable for this image protocol: $IMGPROTO"
341 return
342 fi
343 done
344}
345
6bf19c94
CH
346# tests whether the host OS is one of the supported OSes for a test
347#
348_supported_os()
349{
350 for h
351 do
79e40ab1
KW
352 if [ "$h" = "$HOSTOS" ]
353 then
354 return
355 fi
6bf19c94
CH
356 done
357
358 _notrun "not suitable for this OS: $HOSTOS"
359}
360
f210a83c 361_supported_cache_modes()
166f3c7b 362{
f210a83c
FZ
363 for mode; do
364 if [ "$mode" = "$CACHEMODE" ]; then
365 return
366 fi
166f3c7b 367 done
f210a83c
FZ
368 _notrun "not suitable for cache mode: $CACHEMODE"
369}
370
371_default_cache_mode()
372{
373 if $CACHEMODE_IS_DEFAULT; then
374 CACHEMODE="$1"
375 QEMU_IO="$QEMU_IO --cache $1"
376 return
377 fi
166f3c7b
SH
378}
379
2c77f52e
FZ
380_unsupported_imgopts()
381{
382 for bad_opt
383 do
384 if echo "$IMGOPTS" | grep -q 2>/dev/null "$bad_opt"
385 then
386 _notrun "not suitable for image option: $bad_opt"
387 fi
388 done
389}
390
6bf19c94
CH
391# this test requires that a specified command (executable) exists
392#
393_require_command()
394{
934659c4
HR
395 if [ "$1" = "QEMU" ]; then
396 c=$QEMU_PROG
397 elif [ "$1" = "QEMU_IMG" ]; then
398 c=$QEMU_IMG_PROG
399 elif [ "$1" = "QEMU_IO" ]; then
400 c=$QEMU_IO_PROG
401 elif [ "$1" = "QEMU_NBD" ]; then
402 c=$QEMU_NBD_PROG
403 else
404 eval c=\$$1
405 fi
9c468a01 406 [ -x "$c" ] || _notrun "$1 utility required, skipped this test"
6bf19c94
CH
407}
408
6bf19c94 409# make sure this script returns success
a2d9c0c4 410true