]>
Commit | Line | Data |
---|---|---|
11a82d14 | 1 | #!/usr/bin/env bash |
ba898078 FZ |
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 | ||
ba898078 FZ |
27 | tmp=/tmp/$$ |
28 | status=1 # failure is the default! | |
29 | ||
30 | _cleanup() | |
31 | { | |
32 | _cleanup_test_img | |
33 | rm -f "${TEST_IMG}.base" | |
fcca5dce | 34 | rm -f "${TEST_IMG}.overlay" |
ba898078 FZ |
35 | rm -f "${TEST_IMG}.convert" |
36 | rm -f "${TEST_IMG}.a" | |
37 | rm -f "${TEST_IMG}.b" | |
6a1e9096 | 38 | rm -f "${TEST_IMG}.c" |
ba898078 FZ |
39 | rm -f "${TEST_IMG}.lnk" |
40 | } | |
41 | trap "_cleanup; exit \$status" 0 1 2 3 15 | |
42 | ||
43 | # get standard environment, filters and checks | |
44 | . ./common.rc | |
45 | . ./common.filter | |
46 | . ./common.qemu | |
47 | ||
48 | size=32M | |
49 | ||
50 | _check_ofd() | |
51 | { | |
52 | _make_test_img $size >/dev/null | |
53 | if $QEMU_IMG_PROG info --image-opts "driver=file,locking=on,filename=$TEST_IMG" 2>&1 | | |
54 | grep -q 'falling back to POSIX file'; then | |
55 | return 1 | |
56 | else | |
57 | return 0 | |
58 | fi | |
59 | } | |
60 | ||
61 | _check_ofd || _notrun "OFD lock not available" | |
62 | ||
63 | _supported_fmt qcow2 | |
64 | _supported_proto file | |
ba898078 FZ |
65 | |
66 | _run_cmd() | |
67 | { | |
68 | echo | |
69 | (echo "$@"; "$@" 2>&1 1>/dev/null) | _filter_testdir | |
70 | } | |
71 | ||
8cedcffd | 72 | _do_run_qemu() |
ba898078 FZ |
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 | ||
8cedcffd | 84 | _run_qemu_with_images() |
ba898078 FZ |
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}" | |
f45b638f HR |
138 | |
139 | # qemu-img create does not support -U | |
140 | if [ -z "$L" ]; then | |
141 | _run_cmd $QEMU_IMG create -f $IMGFMT "${TEST_IMG}" \ | |
142 | -b ${TEST_IMG}.base | |
143 | # Read the file format. It used to be the case that | |
144 | # file-posix simply truncated the file, but the qcow2 | |
145 | # driver then failed to format it because it was unable | |
146 | # to acquire the necessary WRITE permission. However, the | |
147 | # truncation was already wrong, and the whole process | |
148 | # resulted in the file being completely empty and thus its | |
149 | # format would be detected to be raw. | |
150 | # So we read it here to see that creation either completed | |
151 | # successfully (thus the format is qcow2) or it aborted | |
152 | # before the file was changed at all (thus the format stays | |
153 | # qcow2). | |
154 | _img_info -U | grep 'file format' | |
155 | fi | |
ba898078 | 156 | done |
9cd97956 | 157 | _send_qemu_cmd $h "{ 'execute': 'quit' }" '' |
ba898078 FZ |
158 | echo |
159 | echo "Round done" | |
160 | _cleanup_qemu | |
161 | done | |
162 | ||
0e1a5827 | 163 | test_opts="read-only=off read-only=on read-only=on,force-share=on" |
ba898078 FZ |
164 | for opt1 in $test_opts; do |
165 | for opt2 in $test_opts; do | |
166 | echo | |
167 | echo "== Two devices with the same image ($opt1 - $opt2) ==" | |
168 | _run_qemu_with_images "${TEST_IMG},$opt1" "${TEST_IMG},$opt2" | |
169 | done | |
170 | done | |
171 | ||
0e1a5827 | 172 | echo |
ba898078 FZ |
173 | echo "== Creating ${TEST_IMG}.[abc] ==" | _filter_testdir |
174 | ( | |
175 | $QEMU_IMG create -f qcow2 "${TEST_IMG}.a" -b "${TEST_IMG}" | |
176 | $QEMU_IMG create -f qcow2 "${TEST_IMG}.b" -b "${TEST_IMG}" | |
177 | $QEMU_IMG create -f qcow2 "${TEST_IMG}.c" -b "${TEST_IMG}.b" | |
178 | ) | _filter_img_create | |
179 | ||
180 | echo | |
181 | echo "== Two devices sharing the same file in backing chain ==" | |
182 | _run_qemu_with_images "${TEST_IMG}.a" "${TEST_IMG}.b" | |
183 | _run_qemu_with_images "${TEST_IMG}.a" "${TEST_IMG}.c" | |
184 | ||
185 | echo | |
186 | echo "== Backing image also as an active device ==" | |
187 | _run_qemu_with_images "${TEST_IMG}.a" "${TEST_IMG}" | |
188 | ||
189 | echo | |
190 | echo "== Backing image also as an active device (ro) ==" | |
191 | _run_qemu_with_images "${TEST_IMG}.a" "${TEST_IMG},readonly=on" | |
192 | ||
193 | echo | |
194 | echo "== Symbolic link ==" | |
195 | rm -f "${TEST_IMG}.lnk" &>/dev/null | |
196 | ln -s ${TEST_IMG} "${TEST_IMG}.lnk" || echo "Failed to create link" | |
197 | _run_qemu_with_images "${TEST_IMG}.lnk" "${TEST_IMG}" | |
198 | ||
de963500 FZ |
199 | echo |
200 | echo "== Active commit to intermediate layer should work when base in use ==" | |
201 | _launch_qemu -drive format=$IMGFMT,file="${TEST_IMG}.a",id=drive0,if=none \ | |
202 | -device virtio-blk,drive=drive0 | |
203 | ||
204 | _send_qemu_cmd $QEMU_HANDLE \ | |
205 | "{ 'execute': 'qmp_capabilities' }" \ | |
206 | 'return' | |
207 | _run_cmd $QEMU_IMG commit -b "${TEST_IMG}.b" "${TEST_IMG}.c" | |
208 | ||
209 | _cleanup_qemu | |
210 | ||
ba898078 FZ |
211 | _launch_qemu |
212 | ||
213 | _send_qemu_cmd $QEMU_HANDLE \ | |
214 | "{ 'execute': 'qmp_capabilities' }" \ | |
215 | 'return' | |
216 | ||
217 | echo "Adding drive" | |
218 | _send_qemu_cmd $QEMU_HANDLE \ | |
219 | "{ 'execute': 'human-monitor-command', | |
220 | 'arguments': { 'command-line': 'drive_add 0 if=none,id=d0,file=${TEST_IMG}' } }" \ | |
9cd97956 | 221 | 'return' |
ba898078 FZ |
222 | |
223 | _run_cmd $QEMU_IO "${TEST_IMG}" -c 'write 0 512' | |
224 | ||
fcca5dce FZ |
225 | echo "Creating overlay with qemu-img when the guest is running should be allowed" |
226 | _run_cmd $QEMU_IMG create -f $IMGFMT -b "${TEST_IMG}" "${TEST_IMG}.overlay" | |
227 | ||
228 | echo "== Closing an image should unlock it ==" | |
ba898078 FZ |
229 | _send_qemu_cmd $QEMU_HANDLE \ |
230 | "{ 'execute': 'human-monitor-command', | |
231 | 'arguments': { 'command-line': 'drive_del d0' } }" \ | |
9cd97956 | 232 | 'return' |
ba898078 FZ |
233 | |
234 | _run_cmd $QEMU_IO "${TEST_IMG}" -c 'write 0 512' | |
235 | ||
236 | echo "Adding two and closing one" | |
237 | for d in d0 d1; do | |
238 | _send_qemu_cmd $QEMU_HANDLE \ | |
239 | "{ 'execute': 'human-monitor-command', | |
240 | 'arguments': { 'command-line': 'drive_add 0 if=none,id=$d,file=${TEST_IMG},readonly=on' } }" \ | |
9cd97956 | 241 | 'return' |
ba898078 FZ |
242 | done |
243 | ||
244 | _run_cmd $QEMU_IMG info "${TEST_IMG}" | |
245 | ||
246 | _send_qemu_cmd $QEMU_HANDLE \ | |
247 | "{ 'execute': 'human-monitor-command', | |
248 | 'arguments': { 'command-line': 'drive_del d0' } }" \ | |
9cd97956 | 249 | 'return' |
ba898078 FZ |
250 | |
251 | _run_cmd $QEMU_IO "${TEST_IMG}" -c 'write 0 512' | |
252 | ||
253 | echo "Closing the other" | |
254 | _send_qemu_cmd $QEMU_HANDLE \ | |
255 | "{ 'execute': 'human-monitor-command', | |
256 | 'arguments': { 'command-line': 'drive_del d1' } }" \ | |
9cd97956 | 257 | 'return' |
ba898078 FZ |
258 | |
259 | _run_cmd $QEMU_IO "${TEST_IMG}" -c 'write 0 512' | |
260 | ||
261 | _cleanup_qemu | |
262 | ||
4e7d73c5 HR |
263 | echo |
264 | echo "== Detecting -U and force-share conflicts ==" | |
265 | ||
266 | echo | |
267 | echo 'No conflict:' | |
268 | $QEMU_IMG info -U --image-opts driver=null-co,force-share=on | |
269 | echo | |
270 | echo 'Conflict:' | |
271 | $QEMU_IMG info -U --image-opts driver=null-co,force-share=off | |
272 | ||
273 | echo | |
274 | echo 'No conflict:' | |
275 | $QEMU_IO -c 'open -r -U -o driver=null-co,force-share=on' | |
276 | echo | |
277 | echo 'Conflict:' | |
278 | $QEMU_IO -c 'open -r -U -o driver=null-co,force-share=off' | |
279 | ||
ba898078 FZ |
280 | # success, all done |
281 | echo "*** done" | |
282 | rm -f $seq.full | |
283 | status=0 |