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