]> Git Repo - qemu.git/blob - tests/qemu-iotests/153
Merge tag 'block-pull-request' into staging
[qemu.git] / tests / qemu-iotests / 153
1 #!/bin/bash
2 #
3 # Test image locking
4 #
5 # Copyright 2016, 2017 Red Hat, Inc.
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20
21 # creator
22 [email protected]
23
24 seq="$(basename $0)"
25 echo "QA output created by $seq"
26
27 here="$PWD"
28 tmp=/tmp/$$
29 status=1        # failure is the default!
30
31 _cleanup()
32 {
33     _cleanup_test_img
34     rm -f "${TEST_IMG}.base"
35     rm -f "${TEST_IMG}.convert"
36     rm -f "${TEST_IMG}.a"
37     rm -f "${TEST_IMG}.b"
38     rm -f "${TEST_IMG}.lnk"
39 }
40 trap "_cleanup; exit \$status" 0 1 2 3 15
41
42 # get standard environment, filters and checks
43 . ./common.rc
44 . ./common.filter
45 . ./common.qemu
46
47 size=32M
48
49 _check_ofd()
50 {
51     _make_test_img $size >/dev/null
52     if $QEMU_IMG_PROG info --image-opts "driver=file,locking=on,filename=$TEST_IMG" 2>&1 |
53         grep -q 'falling back to POSIX file'; then
54         return 1
55     else
56         return 0
57     fi
58 }
59
60 _check_ofd || _notrun "OFD lock not available"
61
62 _supported_fmt qcow2
63 _supported_proto file
64 _supported_os Linux
65
66 _run_cmd()
67 {
68     echo
69     (echo "$@"; "$@" 2>&1 1>/dev/null) | _filter_testdir
70 }
71
72 function _do_run_qemu()
73 {
74     (
75         if ! test -t 0; then
76             while read cmd; do
77                 echo $cmd
78             done
79         fi
80         echo quit
81     ) | $QEMU -nographic -monitor stdio -serial none "$@" 1>/dev/null
82 }
83
84 function _run_qemu_with_images()
85 {
86     _do_run_qemu \
87         $(for i in $@; do echo "-drive if=none,file=$i"; done) 2>&1 \
88         | _filter_testdir | _filter_qemu
89 }
90
91 echo "== readonly=off,force-share=on should be rejected =="
92 _run_qemu_with_images null-co://,readonly=off,force-share=on
93
94 for opts1 in "" "read-only=on" "read-only=on,force-share=on"; do
95     echo
96     echo "== Creating base image =="
97     TEST_IMG="${TEST_IMG}.base" _make_test_img $size
98
99     echo
100     echo "== Creating test image =="
101     $QEMU_IMG create -f $IMGFMT "${TEST_IMG}" -b ${TEST_IMG}.base | _filter_img_create
102
103     echo
104     echo "== Launching QEMU, opts: '$opts1' =="
105     _launch_qemu -drive file="${TEST_IMG}",if=none,$opts1
106     h=$QEMU_HANDLE
107
108     for opts2 in "" "read-only=on" "read-only=on,force-share=on"; do
109         echo
110         echo "== Launching another QEMU, opts: '$opts2' =="
111         echo "quit" | \
112             $QEMU -nographic -monitor stdio \
113             -drive file="${TEST_IMG}",if=none,$opts2 2>&1 1>/dev/null | \
114             _filter_testdir | _filter_qemu
115     done
116
117     for L in "" "-U"; do
118
119         echo
120         echo "== Running utility commands $L =="
121         _run_cmd $QEMU_IO $L -c "read 0 512" "${TEST_IMG}"
122         _run_cmd $QEMU_IO $L -r -c "read 0 512" "${TEST_IMG}"
123         _run_cmd $QEMU_IO -c "open $L ${TEST_IMG}" -c "read 0 512"
124         _run_cmd $QEMU_IO -c "open -r $L ${TEST_IMG}" -c "read 0 512"
125         _run_cmd $QEMU_IMG info        $L "${TEST_IMG}"
126         _run_cmd $QEMU_IMG check       $L "${TEST_IMG}"
127         _run_cmd $QEMU_IMG compare     $L "${TEST_IMG}" "${TEST_IMG}"
128         _run_cmd $QEMU_IMG map         $L "${TEST_IMG}"
129         _run_cmd $QEMU_IMG amend -o "" $L "${TEST_IMG}"
130         _run_cmd $QEMU_IMG commit      $L "${TEST_IMG}"
131         _run_cmd $QEMU_IMG resize      $L "${TEST_IMG}" $size
132         _run_cmd $QEMU_IMG rebase      $L "${TEST_IMG}" -b "${TEST_IMG}.base"
133         _run_cmd $QEMU_IMG snapshot -l $L "${TEST_IMG}"
134         _run_cmd $QEMU_IMG convert     $L "${TEST_IMG}" "${TEST_IMG}.convert"
135         _run_cmd $QEMU_IMG dd          $L if="${TEST_IMG}" of="${TEST_IMG}.convert" bs=512 count=1
136         _run_cmd $QEMU_IMG bench       $L -c 1 "${TEST_IMG}"
137         _run_cmd $QEMU_IMG bench       $L -w -c 1 "${TEST_IMG}"
138     done
139     _send_qemu_cmd $h "{ 'execute': 'quit', }" ""
140     echo
141     echo "Round done"
142     _cleanup_qemu
143 done
144
145 for opt1 in $test_opts; do
146     for opt2 in $test_opts; do
147         echo
148         echo "== Two devices with the same image ($opt1 - $opt2) =="
149         _run_qemu_with_images "${TEST_IMG},$opt1" "${TEST_IMG},$opt2"
150     done
151 done
152
153 echo "== Creating ${TEST_IMG}.[abc] ==" | _filter_testdir
154 (
155     $QEMU_IMG create -f qcow2 "${TEST_IMG}.a" -b "${TEST_IMG}"
156     $QEMU_IMG create -f qcow2 "${TEST_IMG}.b" -b "${TEST_IMG}"
157     $QEMU_IMG create -f qcow2 "${TEST_IMG}.c" -b "${TEST_IMG}.b"
158 ) | _filter_img_create
159
160 echo
161 echo "== Two devices sharing the same file in backing chain =="
162 _run_qemu_with_images "${TEST_IMG}.a" "${TEST_IMG}.b"
163 _run_qemu_with_images "${TEST_IMG}.a" "${TEST_IMG}.c"
164
165 echo
166 echo "== Backing image also as an active device =="
167 _run_qemu_with_images "${TEST_IMG}.a" "${TEST_IMG}"
168
169 echo
170 echo "== Backing image also as an active device (ro) =="
171 _run_qemu_with_images "${TEST_IMG}.a" "${TEST_IMG},readonly=on"
172
173 echo
174 echo "== Symbolic link =="
175 rm -f "${TEST_IMG}.lnk" &>/dev/null
176 ln -s ${TEST_IMG} "${TEST_IMG}.lnk" || echo "Failed to create link"
177 _run_qemu_with_images "${TEST_IMG}.lnk" "${TEST_IMG}"
178
179 echo
180 echo "== Closing an image should unlock it =="
181 _launch_qemu
182
183 _send_qemu_cmd $QEMU_HANDLE \
184     "{ 'execute': 'qmp_capabilities' }" \
185     'return'
186
187 echo "Adding drive"
188 _send_qemu_cmd $QEMU_HANDLE \
189     "{ 'execute': 'human-monitor-command',
190        'arguments': { 'command-line': 'drive_add 0 if=none,id=d0,file=${TEST_IMG}' } }" \
191     ""
192
193 _run_cmd $QEMU_IO "${TEST_IMG}" -c 'write 0 512'
194
195 echo "Closing drive"
196 _send_qemu_cmd $QEMU_HANDLE \
197     "{ 'execute': 'human-monitor-command',
198        'arguments': { 'command-line': 'drive_del d0' } }" \
199     ""
200
201 _run_cmd $QEMU_IO "${TEST_IMG}" -c 'write 0 512'
202
203 echo "Adding two and closing one"
204 for d in d0 d1; do
205     _send_qemu_cmd $QEMU_HANDLE \
206         "{ 'execute': 'human-monitor-command',
207            'arguments': { 'command-line': 'drive_add 0 if=none,id=$d,file=${TEST_IMG},readonly=on' } }" \
208         ""
209 done
210
211 _run_cmd $QEMU_IMG info "${TEST_IMG}"
212
213 _send_qemu_cmd $QEMU_HANDLE \
214     "{ 'execute': 'human-monitor-command',
215        'arguments': { 'command-line': 'drive_del d0' } }" \
216     ""
217
218 _run_cmd $QEMU_IO "${TEST_IMG}" -c 'write 0 512'
219
220 echo "Closing the other"
221 _send_qemu_cmd $QEMU_HANDLE \
222     "{ 'execute': 'human-monitor-command',
223        'arguments': { 'command-line': 'drive_del d1' } }" \
224     ""
225
226 _run_cmd $QEMU_IO "${TEST_IMG}" -c 'write 0 512'
227
228 _cleanup_qemu
229
230 # success, all done
231 echo "*** done"
232 rm -f $seq.full
233 status=0
This page took 0.039853 seconds and 4 git commands to generate.