]>
Commit | Line | Data |
---|---|---|
e6c79647 | 1 | #!/usr/bin/env bash |
9dd003a9 | 2 | # group: rw |
e6c79647 HR |
3 | # |
4 | # Test FUSE exports (in ways that are not captured by the generic | |
5 | # tests) | |
6 | # | |
7 | # Copyright (C) 2020 Red Hat, Inc. | |
8 | # | |
9 | # This program is free software; you can redistribute it and/or modify | |
10 | # it under the terms of the GNU General Public License as published by | |
11 | # the Free Software Foundation; either version 2 of the License, or | |
12 | # (at your option) any later version. | |
13 | # | |
14 | # This program is distributed in the hope that it will be useful, | |
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | # GNU General Public License for more details. | |
18 | # | |
19 | # You should have received a copy of the GNU General Public License | |
20 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
21 | # | |
22 | ||
23 | seq=$(basename "$0") | |
24 | echo "QA output created by $seq" | |
25 | ||
26 | status=1 # failure is the default! | |
27 | ||
28 | _cleanup() | |
29 | { | |
30 | _cleanup_qemu | |
31 | _cleanup_test_img | |
32 | rmdir "$EXT_MP" 2>/dev/null | |
33 | rm -f "$EXT_MP" | |
34 | rm -f "$COPIED_IMG" | |
35 | } | |
36 | trap "_cleanup; exit \$status" 0 1 2 3 15 | |
37 | ||
38 | # get standard environment, filters and checks | |
39 | . ./common.rc | |
40 | . ./common.filter | |
41 | . ./common.qemu | |
42 | ||
43 | # Generic format, but needs a plain filename | |
44 | _supported_fmt generic | |
45 | if [ "$IMGOPTSSYNTAX" = "true" ]; then | |
46 | _unsupported_fmt $IMGFMT | |
47 | fi | |
48 | # We need the image to have exactly the specified size, and VPC does | |
49 | # not allow that by default | |
50 | _unsupported_fmt vpc | |
51 | ||
52 | _supported_proto file # We create the FUSE export manually | |
53 | _supported_os Linux # We need /dev/urandom | |
54 | ||
55 | # $1: Export ID | |
56 | # $2: Options (beyond the node-name and ID) | |
57 | # $3: Expected return value (defaults to 'return') | |
58 | # $4: Node to export (defaults to 'node-format') | |
59 | fuse_export_add() | |
60 | { | |
61 | _send_qemu_cmd $QEMU_HANDLE \ | |
62 | "{'execute': 'block-export-add', | |
63 | 'arguments': { | |
64 | 'type': 'fuse', | |
65 | 'id': '$1', | |
66 | 'node-name': '${4:-node-format}', | |
67 | $2 | |
68 | } }" \ | |
69 | "${3:-return}" \ | |
70 | | _filter_imgfmt | |
71 | } | |
72 | ||
73 | # $1: Export ID | |
74 | fuse_export_del() | |
75 | { | |
76 | _send_qemu_cmd $QEMU_HANDLE \ | |
77 | "{'execute': 'block-export-del', | |
78 | 'arguments': { | |
79 | 'id': '$1' | |
80 | } }" \ | |
81 | 'return' | |
82 | ||
83 | _send_qemu_cmd $QEMU_HANDLE \ | |
84 | '' \ | |
85 | 'BLOCK_EXPORT_DELETED' | |
86 | } | |
87 | ||
88 | # Return the length of the protocol file | |
89 | # $1: Protocol node export mount point | |
90 | # $2: Original file (to compare) | |
91 | get_proto_len() | |
92 | { | |
93 | len1=$(stat -c '%s' "$1") | |
94 | len2=$(stat -c '%s' "$2") | |
95 | ||
96 | if [ "$len1" != "$len2" ]; then | |
97 | echo 'ERROR: Length of export and original differ:' >&2 | |
98 | echo "$len1 != $len2" >&2 | |
99 | else | |
100 | echo '(OK: Lengths of export and original are the same)' >&2 | |
101 | fi | |
102 | ||
103 | echo "$len1" | |
104 | } | |
105 | ||
106 | COPIED_IMG="$TEST_IMG.copy" | |
107 | EXT_MP="$TEST_IMG.fuse" | |
108 | ||
109 | echo '=== Set up ===' | |
110 | ||
111 | # Create image with random data | |
112 | _make_test_img 64M | |
113 | $QEMU_IO -c 'write -s /dev/urandom 0 64M' "$TEST_IMG" | _filter_qemu_io | |
114 | ||
115 | _launch_qemu | |
116 | _send_qemu_cmd $QEMU_HANDLE \ | |
117 | "{'execute': 'qmp_capabilities'}" \ | |
118 | 'return' | |
119 | ||
120 | # Separate blockdev-add calls for format and protocol so we can remove | |
121 | # the format layer later on | |
122 | _send_qemu_cmd $QEMU_HANDLE \ | |
123 | "{'execute': 'blockdev-add', | |
124 | 'arguments': { | |
125 | 'driver': 'file', | |
126 | 'node-name': 'node-protocol', | |
127 | 'filename': '$TEST_IMG' | |
128 | } }" \ | |
129 | 'return' | |
130 | ||
131 | _send_qemu_cmd $QEMU_HANDLE \ | |
132 | "{'execute': 'blockdev-add', | |
133 | 'arguments': { | |
134 | 'driver': '$IMGFMT', | |
135 | 'node-name': 'node-format', | |
136 | 'file': 'node-protocol' | |
137 | } }" \ | |
138 | 'return' | |
139 | ||
140 | echo | |
141 | echo '=== Mountpoint not present ===' | |
142 | ||
143 | rmdir "$EXT_MP" 2>/dev/null | |
144 | rm -f "$EXT_MP" | |
145 | output=$(fuse_export_add 'export-err' "'mountpoint': '$EXT_MP'" error) | |
146 | ||
147 | if echo "$output" | grep -q "Invalid parameter 'fuse'"; then | |
148 | _notrun 'No FUSE support' | |
149 | fi | |
150 | ||
151 | echo "$output" | |
152 | ||
153 | echo | |
154 | echo '=== Mountpoint is a directory ===' | |
155 | ||
156 | mkdir "$EXT_MP" | |
157 | fuse_export_add 'export-err' "'mountpoint': '$EXT_MP'" error | |
158 | rmdir "$EXT_MP" | |
159 | ||
160 | echo | |
161 | echo '=== Mountpoint is a regular file ===' | |
162 | ||
163 | touch "$EXT_MP" | |
164 | fuse_export_add 'export-mp' "'mountpoint': '$EXT_MP'" | |
165 | ||
166 | # Check that the export presents the same data as the original image | |
167 | $QEMU_IMG compare -f raw -F $IMGFMT -U "$EXT_MP" "$TEST_IMG" | |
168 | ||
169 | echo | |
170 | echo '=== Mount over existing file ===' | |
171 | ||
172 | # This is the coolest feature of FUSE exports: You can transparently | |
173 | # make images in any format appear as raw images | |
174 | fuse_export_add 'export-img' "'mountpoint': '$TEST_IMG'" | |
175 | ||
176 | # Accesses both exports at the same time, so we get a concurrency test | |
177 | $QEMU_IMG compare -f raw -F raw -U "$EXT_MP" "$TEST_IMG" | |
178 | ||
179 | # Just to be sure, we later want to compare the data offline. Also, | |
180 | # this allows us to see that cp works without complaining. | |
181 | # (This is not a given, because cp will expect a short read at EOF. | |
182 | # Internally, qemu does not allow short reads, so we have to check | |
183 | # whether the FUSE export driver lets them work.) | |
184 | cp "$TEST_IMG" "$COPIED_IMG" | |
185 | ||
186 | # $TEST_IMG will be in mode 0400 because it is read-only; we are going | |
187 | # to write to the copy, so make it writable | |
188 | chmod 0600 "$COPIED_IMG" | |
189 | ||
190 | echo | |
191 | echo '=== Double export ===' | |
192 | ||
193 | # We have already seen that exporting a node twice works fine, but you | |
194 | # cannot export anything twice on the same mount point. The reason is | |
195 | # that qemu has to stat the given mount point, and this would have to | |
196 | # be answered by the same qemu instance if it already has an export | |
197 | # there. However, it cannot answer the stat because it is itself | |
198 | # caught up in that same stat. | |
199 | fuse_export_add 'export-err' "'mountpoint': '$EXT_MP'" error | |
200 | ||
201 | echo | |
202 | echo '=== Remove export ===' | |
203 | ||
204 | # Double-check that $EXT_MP appears as a non-empty file (the raw image) | |
205 | $QEMU_IMG info -f raw "$EXT_MP" | grep 'virtual size' | |
206 | ||
207 | fuse_export_del 'export-mp' | |
208 | ||
209 | # See that the file appears empty again | |
210 | $QEMU_IMG info -f raw "$EXT_MP" | grep 'virtual size' | |
211 | ||
212 | echo | |
213 | echo '=== Writable export ===' | |
214 | ||
215 | fuse_export_add 'export-mp' "'mountpoint': '$EXT_MP', 'writable': true" | |
216 | ||
217 | # Check that writing to the read-only export fails | |
2c7dd057 HR |
218 | $QEMU_IO -f raw -c 'write -P 42 1M 64k' "$TEST_IMG" 2>&1 \ |
219 | | _filter_qemu_io | _filter_testdir | _filter_imgfmt | |
e6c79647 HR |
220 | |
221 | # But here it should work | |
222 | $QEMU_IO -f raw -c 'write -P 42 1M 64k' "$EXT_MP" | _filter_qemu_io | |
223 | ||
224 | # (Adjust the copy, too) | |
225 | $QEMU_IO -f raw -c 'write -P 42 1M 64k' "$COPIED_IMG" | _filter_qemu_io | |
226 | ||
227 | echo | |
228 | echo '=== Resizing exports ===' | |
229 | ||
230 | # Here, we need to export the protocol node -- the format layer may | |
231 | # not be growable, simply because the format does not support it. | |
232 | ||
233 | # Remove all exports and the format node first so permissions will not | |
234 | # get in the way | |
235 | fuse_export_del 'export-mp' | |
236 | fuse_export_del 'export-img' | |
237 | ||
238 | _send_qemu_cmd $QEMU_HANDLE \ | |
239 | "{'execute': 'blockdev-del', | |
240 | 'arguments': { | |
241 | 'node-name': 'node-format' | |
242 | } }" \ | |
243 | 'return' | |
244 | ||
245 | # Now export the protocol node | |
246 | fuse_export_add \ | |
247 | 'export-mp' \ | |
248 | "'mountpoint': '$EXT_MP', 'writable': true" \ | |
249 | 'return' \ | |
250 | 'node-protocol' | |
251 | ||
252 | echo | |
253 | echo '--- Try growing non-growable export ---' | |
254 | ||
255 | # Get the current size so we can write beyond the EOF | |
256 | orig_len=$(get_proto_len "$EXT_MP" "$TEST_IMG") | |
257 | orig_disk_usage=$(stat -c '%b' "$TEST_IMG") | |
258 | ||
259 | # Should fail (exports are non-growable by default) | |
260 | # (Note that qemu-io can never write beyond the EOF, so we have to use | |
261 | # dd here) | |
262 | dd if=/dev/zero of="$EXT_MP" bs=1 count=64k seek=$orig_len 2>&1 \ | |
263 | | _filter_testdir | _filter_imgfmt | |
264 | ||
265 | echo | |
266 | echo '--- Resize export ---' | |
267 | ||
268 | # But we can truncate it explicitly; even with fallocate | |
269 | fallocate -o "$orig_len" -l 64k "$EXT_MP" | |
270 | ||
271 | new_len=$(get_proto_len "$EXT_MP" "$TEST_IMG") | |
272 | if [ "$new_len" != "$((orig_len + 65536))" ]; then | |
273 | echo 'ERROR: Unexpected post-truncate image size:' | |
274 | echo "$new_len != $((orig_len + 65536))" | |
275 | else | |
276 | echo 'OK: Post-truncate image size is as expected' | |
277 | fi | |
278 | ||
279 | new_disk_usage=$(stat -c '%b' "$TEST_IMG") | |
280 | if [ "$new_disk_usage" -gt "$orig_disk_usage" ]; then | |
281 | echo 'OK: Disk usage grew with fallocate' | |
282 | else | |
283 | echo 'ERROR: Disk usage did not grow despite fallocate:' | |
284 | echo "$orig_disk_usage => $new_disk_usage" | |
285 | fi | |
286 | ||
287 | echo | |
288 | echo '--- Try growing growable export ---' | |
289 | ||
290 | # Now export as growable | |
291 | fuse_export_del 'export-mp' | |
292 | fuse_export_add \ | |
293 | 'export-mp' \ | |
294 | "'mountpoint': '$EXT_MP', 'writable': true, 'growable': true" \ | |
295 | 'return' \ | |
296 | 'node-protocol' | |
297 | ||
298 | # Now we should be able to write beyond the EOF | |
299 | dd if=/dev/zero of="$EXT_MP" bs=1 count=64k seek=$new_len 2>&1 \ | |
300 | | _filter_testdir | _filter_imgfmt | |
301 | ||
302 | new_len=$(get_proto_len "$EXT_MP" "$TEST_IMG") | |
303 | if [ "$new_len" != "$((orig_len + 131072))" ]; then | |
304 | echo 'ERROR: Unexpected post-grow image size:' | |
305 | echo "$new_len != $((orig_len + 131072))" | |
306 | else | |
307 | echo 'OK: Post-grow image size is as expected' | |
308 | fi | |
309 | ||
310 | echo | |
311 | echo '--- Shrink export ---' | |
312 | ||
313 | # Now go back to the original size | |
314 | truncate -s "$orig_len" "$EXT_MP" | |
315 | ||
316 | new_len=$(get_proto_len "$EXT_MP" "$TEST_IMG") | |
317 | if [ "$new_len" != "$orig_len" ]; then | |
318 | echo 'ERROR: Unexpected post-truncate image size:' | |
319 | echo "$new_len != $orig_len" | |
320 | else | |
321 | echo 'OK: Post-truncate image size is as expected' | |
322 | fi | |
323 | ||
324 | echo | |
325 | echo '=== Tear down ===' | |
326 | ||
327 | _send_qemu_cmd $QEMU_HANDLE \ | |
328 | "{'execute': 'quit'}" \ | |
329 | 'return' | |
330 | ||
331 | wait=yes _cleanup_qemu | |
332 | ||
333 | echo | |
334 | echo '=== Compare copy with original ===' | |
335 | ||
336 | $QEMU_IMG compare -f raw -F $IMGFMT "$COPIED_IMG" "$TEST_IMG" | |
337 | ||
338 | # success, all done | |
339 | echo "*** done" | |
340 | rm -f $seq.full | |
341 | status=0 |