4 # Test FUSE exports (in ways that are not captured by the generic
7 # Copyright (C) 2020 Red Hat, Inc.
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.
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.
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/>.
24 echo "QA output created by $seq"
26 status
=1 # failure is the default!
32 rmdir "$EXT_MP" 2>/dev
/null
36 trap "_cleanup; exit \$status" 0 1 2 3 15
38 # get standard environment, filters and checks
43 # Generic format, but needs a plain filename
44 _supported_fmt generic
45 if [ "$IMGOPTSSYNTAX" = "true" ]; then
46 _unsupported_fmt
$IMGFMT
48 # We need the image to have exactly the specified size, and VPC does
49 # not allow that by default
52 _supported_proto
file # We create the FUSE export manually
53 _supported_os Linux
# We need /dev/urandom
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')
61 # The grep -v is a filter for errors when /etc/fuse.conf does not contain
62 # user_allow_other. (The error is benign, but it is printed by fusermount
63 # on the first mount attempt, so our export code cannot hide it.)
64 _send_qemu_cmd
$QEMU_HANDLE \
65 "{'execute': 'block-export-add',
69 'node-name': '${4:-node-format}',
74 |
grep -v 'option allow_other only allowed if'
80 capture_events
="BLOCK_EXPORT_DELETED" \
81 _send_qemu_cmd
$QEMU_HANDLE \
82 "{'execute': 'block-export-del',
88 _wait_event
$QEMU_HANDLE \
89 'BLOCK_EXPORT_DELETED'
92 # Return the length of the protocol file
93 # $1: Protocol node export mount point
94 # $2: Original file (to compare)
97 len1
=$
(stat
-c '%s' "$1")
98 len2
=$
(stat
-c '%s' "$2")
100 if [ "$len1" != "$len2" ]; then
101 echo 'ERROR: Length of export and original differ:' >&2
102 echo "$len1 != $len2" >&2
104 echo '(OK: Lengths of export and original are the same)' >&2
110 COPIED_IMG
="$TEST_IMG.copy"
111 EXT_MP
="$TEST_IMG.fuse"
113 echo '=== Set up ==='
115 # Create image with random data
117 $QEMU_IO -c 'write -s /dev/urandom 0 64M' "$TEST_IMG" | _filter_qemu_io
120 _send_qemu_cmd
$QEMU_HANDLE \
121 "{'execute': 'qmp_capabilities'}" \
124 # Separate blockdev-add calls for format and protocol so we can remove
125 # the format layer later on
126 _send_qemu_cmd
$QEMU_HANDLE \
127 "{'execute': 'blockdev-add',
130 'node-name': 'node-protocol',
131 'filename': '$TEST_IMG'
135 _send_qemu_cmd
$QEMU_HANDLE \
136 "{'execute': 'blockdev-add',
139 'node-name': 'node-format',
140 'file': 'node-protocol'
145 echo '=== Mountpoint not present ==='
147 rmdir "$EXT_MP" 2>/dev
/null
149 output
=$
(fuse_export_add
'export-err' "'mountpoint': '$EXT_MP'" error
)
151 if echo "$output" |
grep -q "Parameter 'type' does not accept value 'fuse'"; then
152 _notrun
'No FUSE support'
158 echo '=== Mountpoint is a directory ==='
161 fuse_export_add
'export-err' "'mountpoint': '$EXT_MP'" error
165 echo '=== Mountpoint is a regular file ==='
168 fuse_export_add
'export-mp' "'mountpoint': '$EXT_MP'"
170 # Check that the export presents the same data as the original image
171 $QEMU_IMG compare
-f raw
-F $IMGFMT -U "$EXT_MP" "$TEST_IMG"
173 # Some quick chmod tests
174 stat
-c 'Permissions pre-chmod: %a' "$EXT_MP"
176 # Verify that we cannot set +w
177 chmod u
+w
"$EXT_MP" 2>&1 | _filter_testdir | _filter_imgfmt
178 stat
-c 'Permissions post-+w: %a' "$EXT_MP"
180 # But that we can set, say, +x (if we are so inclined)
181 chmod u
+x
"$EXT_MP" 2>&1 | _filter_testdir | _filter_imgfmt
182 stat
-c 'Permissions post-+x: %a' "$EXT_MP"
185 echo '=== Mount over existing file ==='
187 # This is the coolest feature of FUSE exports: You can transparently
188 # make images in any format appear as raw images
189 fuse_export_add
'export-img' "'mountpoint': '$TEST_IMG'"
191 # Accesses both exports at the same time, so we get a concurrency test
192 $QEMU_IMG compare
-f raw
-F raw
-U "$EXT_MP" "$TEST_IMG"
194 # Just to be sure, we later want to compare the data offline. Also,
195 # this allows us to see that cp works without complaining.
196 # (This is not a given, because cp will expect a short read at EOF.
197 # Internally, qemu does not allow short reads, so we have to check
198 # whether the FUSE export driver lets them work.)
199 cp "$TEST_IMG" "$COPIED_IMG"
201 # $TEST_IMG will be in mode 0400 because it is read-only; we are going
202 # to write to the copy, so make it writable
203 chmod 0600 "$COPIED_IMG"
206 echo '=== Double export ==='
208 # We have already seen that exporting a node twice works fine, but you
209 # cannot export anything twice on the same mount point. The reason is
210 # that qemu has to stat the given mount point, and this would have to
211 # be answered by the same qemu instance if it already has an export
212 # there. However, it cannot answer the stat because it is itself
213 # caught up in that same stat.
214 fuse_export_add
'export-err' "'mountpoint': '$EXT_MP'" error
217 echo '=== Remove export ==='
219 # Double-check that $EXT_MP appears as a non-empty file (the raw image)
220 $QEMU_IMG info
-f raw
"$EXT_MP" |
grep 'virtual size' |
head -n 1
222 fuse_export_del
'export-mp'
224 # See that the file appears empty again
225 $QEMU_IMG info
-f raw
"$EXT_MP" |
grep 'virtual size' |
head -n 1
228 echo '=== Writable export ==='
230 fuse_export_add
'export-mp' "'mountpoint': '$EXT_MP', 'writable': true"
232 # Check that writing to the read-only export fails
233 output
=$
($QEMU_IO -f raw
-c 'write -P 42 1M 64k' "$TEST_IMG" 2>&1 \
234 | _filter_qemu_io | _filter_testdir | _filter_imgfmt
)
236 # Expected reference output: Opening the file fails because it has no
238 reference
="Could not open 'TEST_DIR/t.IMGFMT': Permission denied"
240 if echo "$output" |
grep -q "$reference"; then
241 echo "Writing to read-only export failed: OK"
242 elif echo "$output" |
grep -q "write failed: Permission denied"; then
243 # With CAP_DAC_OVERRIDE (e.g. when running this test as root), the export
244 # can be opened regardless of its file permissions, but writing will then
245 # fail. This is not the result for which we want to test, so count this as
247 _casenotrun
"Opening RO export as R/W succeeded, perhaps because of" \
250 # Still, write this to the reference output to make the test pass
251 echo "Writing to read-only export failed: OK"
253 echo "Writing to read-only export failed: ERROR"
257 # But here it should work
258 $QEMU_IO -f raw
-c 'write -P 42 1M 64k' "$EXT_MP" | _filter_qemu_io
260 # (Adjust the copy, too)
261 $QEMU_IO -f raw
-c 'write -P 42 1M 64k' "$COPIED_IMG" | _filter_qemu_io
264 echo '=== Resizing exports ==='
266 # Here, we need to export the protocol node -- the format layer may
267 # not be growable, simply because the format does not support it.
269 # Remove all exports and the format node first so permissions will not
271 fuse_export_del
'export-mp'
272 fuse_export_del
'export-img'
274 _send_qemu_cmd
$QEMU_HANDLE \
275 "{'execute': 'blockdev-del',
277 'node-name': 'node-format'
281 # Now export the protocol node
284 "'mountpoint': '$EXT_MP', 'writable': true" \
289 echo '--- Try growing non-growable export ---'
291 # Get the current size so we can write beyond the EOF
292 orig_len
=$
(get_proto_len
"$EXT_MP" "$TEST_IMG")
293 orig_disk_usage
=$
(stat
-c '%b' "$TEST_IMG")
295 # Should fail (exports are non-growable by default)
296 # (Note that qemu-io can never write beyond the EOF, so we have to use
298 dd if=/dev
/zero of
="$EXT_MP" bs
=1 count
=64k seek
=$orig_len 2>&1 \
299 | _filter_testdir | _filter_imgfmt
302 echo '--- Resize export ---'
304 # But we can truncate it explicitly; even with fallocate
305 fallocate
-o "$orig_len" -l 64k
"$EXT_MP"
307 new_len
=$
(get_proto_len
"$EXT_MP" "$TEST_IMG")
308 if [ "$new_len" != "$((orig_len + 65536))" ]; then
309 echo 'ERROR: Unexpected post-truncate image size:'
310 echo "$new_len != $((orig_len + 65536))"
312 echo 'OK: Post-truncate image size is as expected'
315 new_disk_usage
=$
(stat
-c '%b' "$TEST_IMG")
316 if [ "$new_disk_usage" -gt "$orig_disk_usage" ]; then
317 echo 'OK: Disk usage grew with fallocate'
319 echo 'ERROR: Disk usage did not grow despite fallocate:'
320 echo "$orig_disk_usage => $new_disk_usage"
324 echo '--- Try growing growable export ---'
326 # Now export as growable
327 fuse_export_del
'export-mp'
330 "'mountpoint': '$EXT_MP', 'writable': true, 'growable': true" \
334 # Now we should be able to write beyond the EOF
335 dd if=/dev
/zero of
="$EXT_MP" bs
=1 count
=64k seek
=$new_len 2>&1 \
336 | _filter_testdir | _filter_imgfmt
338 new_len
=$
(get_proto_len
"$EXT_MP" "$TEST_IMG")
339 if [ "$new_len" != "$((orig_len + 131072))" ]; then
340 echo 'ERROR: Unexpected post-grow image size:'
341 echo "$new_len != $((orig_len + 131072))"
343 echo 'OK: Post-grow image size is as expected'
347 echo '--- Shrink export ---'
349 # Now go back to the original size
350 truncate
-s "$orig_len" "$EXT_MP"
352 new_len
=$
(get_proto_len
"$EXT_MP" "$TEST_IMG")
353 if [ "$new_len" != "$orig_len" ]; then
354 echo 'ERROR: Unexpected post-truncate image size:'
355 echo "$new_len != $orig_len"
357 echo 'OK: Post-truncate image size is as expected'
361 echo '=== Tear down ==='
363 _send_qemu_cmd
$QEMU_HANDLE \
364 "{'execute': 'quit'}" \
367 wait=yes _cleanup_qemu
370 echo '=== Compare copy with original ==='
372 $QEMU_IMG compare
-f raw
-F $IMGFMT "$COPIED_IMG" "$TEST_IMG"
376 echo '=== Writing zeroes while unmapping ==='
377 # Regression test for https://gitlab.com/qemu-project/qemu/-/issues/1507
379 $QEMU_IO -c 'write -s /dev/urandom 0 64M' "$TEST_IMG" | _filter_qemu_io
382 _send_qemu_cmd
$QEMU_HANDLE \
383 "{'execute': 'qmp_capabilities'}" \
386 _send_qemu_cmd
$QEMU_HANDLE \
387 "{'execute': 'blockdev-add',
390 'node-name': 'node-format',
393 'filename': '$TEST_IMG'
398 fuse_export_add
'export' "'mountpoint': '$EXT_MP', 'writable': true"
400 # Try writing zeroes by unmapping
401 $QEMU_IO -f raw
-c 'write -zu 0 64M' "$EXT_MP" | _filter_qemu_io
404 $QEMU_IO -f raw
-c 'read -P 0 0 64M' "$EXT_MP" | _filter_qemu_io
406 _send_qemu_cmd
$QEMU_HANDLE \
407 "{'execute': 'quit'}" \
410 wait=yes _cleanup_qemu
412 # Check the original image
413 $QEMU_IO -c 'read -P 0 0 64M' "$TEST_IMG" | _filter_qemu_io