]>
Commit | Line | Data |
---|---|---|
ba898078 FZ |
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" | |
fcca5dce | 35 | rm -f "${TEST_IMG}.overlay" |
ba898078 FZ |
36 | rm -f "${TEST_IMG}.convert" |
37 | rm -f "${TEST_IMG}.a" | |
38 | rm -f "${TEST_IMG}.b" | |
6a1e9096 | 39 | rm -f "${TEST_IMG}.c" |
ba898078 FZ |
40 | rm -f "${TEST_IMG}.lnk" |
41 | } | |
42 | trap "_cleanup; exit \$status" 0 1 2 3 15 | |
43 | ||
44 | # get standard environment, filters and checks | |
45 | . ./common.rc | |
46 | . ./common.filter | |
47 | . ./common.qemu | |
48 | ||
49 | size=32M | |
50 | ||
51 | _check_ofd() | |
52 | { | |
53 | _make_test_img $size >/dev/null | |
54 | if $QEMU_IMG_PROG info --image-opts "driver=file,locking=on,filename=$TEST_IMG" 2>&1 | | |
55 | grep -q 'falling back to POSIX file'; then | |
56 | return 1 | |
57 | else | |
58 | return 0 | |
59 | fi | |
60 | } | |
61 | ||
62 | _check_ofd || _notrun "OFD lock not available" | |
63 | ||
64 | _supported_fmt qcow2 | |
65 | _supported_proto file | |
66 | _supported_os Linux | |
67 | ||
68 | _run_cmd() | |
69 | { | |
70 | echo | |
71 | (echo "$@"; "$@" 2>&1 1>/dev/null) | _filter_testdir | |
72 | } | |
73 | ||
74 | function _do_run_qemu() | |
75 | { | |
76 | ( | |
77 | if ! test -t 0; then | |
78 | while read cmd; do | |
79 | echo $cmd | |
80 | done | |
81 | fi | |
82 | echo quit | |
83 | ) | $QEMU -nographic -monitor stdio -serial none "$@" 1>/dev/null | |
84 | } | |
85 | ||
86 | function _run_qemu_with_images() | |
87 | { | |
88 | _do_run_qemu \ | |
89 | $(for i in $@; do echo "-drive if=none,file=$i"; done) 2>&1 \ | |
90 | | _filter_testdir | _filter_qemu | |
91 | } | |
92 | ||
93 | echo "== readonly=off,force-share=on should be rejected ==" | |
94 | _run_qemu_with_images null-co://,readonly=off,force-share=on | |
95 | ||
96 | for opts1 in "" "read-only=on" "read-only=on,force-share=on"; do | |
97 | echo | |
98 | echo "== Creating base image ==" | |
99 | TEST_IMG="${TEST_IMG}.base" _make_test_img $size | |
100 | ||
101 | echo | |
102 | echo "== Creating test image ==" | |
103 | $QEMU_IMG create -f $IMGFMT "${TEST_IMG}" -b ${TEST_IMG}.base | _filter_img_create | |
104 | ||
105 | echo | |
106 | echo "== Launching QEMU, opts: '$opts1' ==" | |
107 | _launch_qemu -drive file="${TEST_IMG}",if=none,$opts1 | |
108 | h=$QEMU_HANDLE | |
109 | ||
110 | for opts2 in "" "read-only=on" "read-only=on,force-share=on"; do | |
111 | echo | |
112 | echo "== Launching another QEMU, opts: '$opts2' ==" | |
113 | echo "quit" | \ | |
114 | $QEMU -nographic -monitor stdio \ | |
115 | -drive file="${TEST_IMG}",if=none,$opts2 2>&1 1>/dev/null | \ | |
116 | _filter_testdir | _filter_qemu | |
117 | done | |
118 | ||
119 | for L in "" "-U"; do | |
120 | ||
121 | echo | |
122 | echo "== Running utility commands $L ==" | |
123 | _run_cmd $QEMU_IO $L -c "read 0 512" "${TEST_IMG}" | |
124 | _run_cmd $QEMU_IO $L -r -c "read 0 512" "${TEST_IMG}" | |
125 | _run_cmd $QEMU_IO -c "open $L ${TEST_IMG}" -c "read 0 512" | |
126 | _run_cmd $QEMU_IO -c "open -r $L ${TEST_IMG}" -c "read 0 512" | |
127 | _run_cmd $QEMU_IMG info $L "${TEST_IMG}" | |
128 | _run_cmd $QEMU_IMG check $L "${TEST_IMG}" | |
129 | _run_cmd $QEMU_IMG compare $L "${TEST_IMG}" "${TEST_IMG}" | |
130 | _run_cmd $QEMU_IMG map $L "${TEST_IMG}" | |
131 | _run_cmd $QEMU_IMG amend -o "" $L "${TEST_IMG}" | |
132 | _run_cmd $QEMU_IMG commit $L "${TEST_IMG}" | |
133 | _run_cmd $QEMU_IMG resize $L "${TEST_IMG}" $size | |
134 | _run_cmd $QEMU_IMG rebase $L "${TEST_IMG}" -b "${TEST_IMG}.base" | |
135 | _run_cmd $QEMU_IMG snapshot -l $L "${TEST_IMG}" | |
136 | _run_cmd $QEMU_IMG convert $L "${TEST_IMG}" "${TEST_IMG}.convert" | |
137 | _run_cmd $QEMU_IMG dd $L if="${TEST_IMG}" of="${TEST_IMG}.convert" bs=512 count=1 | |
138 | _run_cmd $QEMU_IMG bench $L -c 1 "${TEST_IMG}" | |
139 | _run_cmd $QEMU_IMG bench $L -w -c 1 "${TEST_IMG}" | |
140 | done | |
141 | _send_qemu_cmd $h "{ 'execute': 'quit', }" "" | |
142 | echo | |
143 | echo "Round done" | |
144 | _cleanup_qemu | |
145 | done | |
146 | ||
147 | for opt1 in $test_opts; do | |
148 | for opt2 in $test_opts; do | |
149 | echo | |
150 | echo "== Two devices with the same image ($opt1 - $opt2) ==" | |
151 | _run_qemu_with_images "${TEST_IMG},$opt1" "${TEST_IMG},$opt2" | |
152 | done | |
153 | done | |
154 | ||
155 | echo "== Creating ${TEST_IMG}.[abc] ==" | _filter_testdir | |
156 | ( | |
157 | $QEMU_IMG create -f qcow2 "${TEST_IMG}.a" -b "${TEST_IMG}" | |
158 | $QEMU_IMG create -f qcow2 "${TEST_IMG}.b" -b "${TEST_IMG}" | |
159 | $QEMU_IMG create -f qcow2 "${TEST_IMG}.c" -b "${TEST_IMG}.b" | |
160 | ) | _filter_img_create | |
161 | ||
162 | echo | |
163 | echo "== Two devices sharing the same file in backing chain ==" | |
164 | _run_qemu_with_images "${TEST_IMG}.a" "${TEST_IMG}.b" | |
165 | _run_qemu_with_images "${TEST_IMG}.a" "${TEST_IMG}.c" | |
166 | ||
167 | echo | |
168 | echo "== Backing image also as an active device ==" | |
169 | _run_qemu_with_images "${TEST_IMG}.a" "${TEST_IMG}" | |
170 | ||
171 | echo | |
172 | echo "== Backing image also as an active device (ro) ==" | |
173 | _run_qemu_with_images "${TEST_IMG}.a" "${TEST_IMG},readonly=on" | |
174 | ||
175 | echo | |
176 | echo "== Symbolic link ==" | |
177 | rm -f "${TEST_IMG}.lnk" &>/dev/null | |
178 | ln -s ${TEST_IMG} "${TEST_IMG}.lnk" || echo "Failed to create link" | |
179 | _run_qemu_with_images "${TEST_IMG}.lnk" "${TEST_IMG}" | |
180 | ||
de963500 FZ |
181 | echo |
182 | echo "== Active commit to intermediate layer should work when base in use ==" | |
183 | _launch_qemu -drive format=$IMGFMT,file="${TEST_IMG}.a",id=drive0,if=none \ | |
184 | -device virtio-blk,drive=drive0 | |
185 | ||
186 | _send_qemu_cmd $QEMU_HANDLE \ | |
187 | "{ 'execute': 'qmp_capabilities' }" \ | |
188 | 'return' | |
189 | _run_cmd $QEMU_IMG commit -b "${TEST_IMG}.b" "${TEST_IMG}.c" | |
190 | ||
191 | _cleanup_qemu | |
192 | ||
ba898078 FZ |
193 | _launch_qemu |
194 | ||
195 | _send_qemu_cmd $QEMU_HANDLE \ | |
196 | "{ 'execute': 'qmp_capabilities' }" \ | |
197 | 'return' | |
198 | ||
199 | echo "Adding drive" | |
200 | _send_qemu_cmd $QEMU_HANDLE \ | |
201 | "{ 'execute': 'human-monitor-command', | |
202 | 'arguments': { 'command-line': 'drive_add 0 if=none,id=d0,file=${TEST_IMG}' } }" \ | |
203 | "" | |
204 | ||
205 | _run_cmd $QEMU_IO "${TEST_IMG}" -c 'write 0 512' | |
206 | ||
fcca5dce FZ |
207 | echo "Creating overlay with qemu-img when the guest is running should be allowed" |
208 | _run_cmd $QEMU_IMG create -f $IMGFMT -b "${TEST_IMG}" "${TEST_IMG}.overlay" | |
209 | ||
210 | echo "== Closing an image should unlock it ==" | |
ba898078 FZ |
211 | _send_qemu_cmd $QEMU_HANDLE \ |
212 | "{ 'execute': 'human-monitor-command', | |
213 | 'arguments': { 'command-line': 'drive_del d0' } }" \ | |
214 | "" | |
215 | ||
216 | _run_cmd $QEMU_IO "${TEST_IMG}" -c 'write 0 512' | |
217 | ||
218 | echo "Adding two and closing one" | |
219 | for d in d0 d1; do | |
220 | _send_qemu_cmd $QEMU_HANDLE \ | |
221 | "{ 'execute': 'human-monitor-command', | |
222 | 'arguments': { 'command-line': 'drive_add 0 if=none,id=$d,file=${TEST_IMG},readonly=on' } }" \ | |
223 | "" | |
224 | done | |
225 | ||
226 | _run_cmd $QEMU_IMG info "${TEST_IMG}" | |
227 | ||
228 | _send_qemu_cmd $QEMU_HANDLE \ | |
229 | "{ 'execute': 'human-monitor-command', | |
230 | 'arguments': { 'command-line': 'drive_del d0' } }" \ | |
231 | "" | |
232 | ||
233 | _run_cmd $QEMU_IO "${TEST_IMG}" -c 'write 0 512' | |
234 | ||
235 | echo "Closing the other" | |
236 | _send_qemu_cmd $QEMU_HANDLE \ | |
237 | "{ 'execute': 'human-monitor-command', | |
238 | 'arguments': { 'command-line': 'drive_del d1' } }" \ | |
239 | "" | |
240 | ||
241 | _run_cmd $QEMU_IO "${TEST_IMG}" -c 'write 0 512' | |
242 | ||
243 | _cleanup_qemu | |
244 | ||
245 | # success, all done | |
246 | echo "*** done" | |
247 | rm -f $seq.full | |
248 | status=0 |