xref: /qemu/tests/qemu-iotests/308 (revision 2c7dd057aa7bd7a875e9b1a53975c220d6380bc4)
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{
61e6c79647SMax Reitz    _send_qemu_cmd $QEMU_HANDLE \
62e6c79647SMax Reitz        "{'execute': 'block-export-add',
63e6c79647SMax Reitz          'arguments': {
64e6c79647SMax Reitz              'type': 'fuse',
65e6c79647SMax Reitz              'id': '$1',
66e6c79647SMax Reitz              'node-name': '${4:-node-format}',
67e6c79647SMax Reitz              $2
68e6c79647SMax Reitz          } }" \
69e6c79647SMax Reitz        "${3:-return}" \
70e6c79647SMax Reitz        | _filter_imgfmt
71e6c79647SMax Reitz}
72e6c79647SMax Reitz
73e6c79647SMax Reitz# $1: Export ID
74e6c79647SMax Reitzfuse_export_del()
75e6c79647SMax Reitz{
76e6c79647SMax Reitz    _send_qemu_cmd $QEMU_HANDLE \
77e6c79647SMax Reitz        "{'execute': 'block-export-del',
78e6c79647SMax Reitz          'arguments': {
79e6c79647SMax Reitz              'id': '$1'
80e6c79647SMax Reitz          } }" \
81e6c79647SMax Reitz        'return'
82e6c79647SMax Reitz
83e6c79647SMax Reitz    _send_qemu_cmd $QEMU_HANDLE \
84e6c79647SMax Reitz        '' \
85e6c79647SMax Reitz        'BLOCK_EXPORT_DELETED'
86e6c79647SMax Reitz}
87e6c79647SMax Reitz
88e6c79647SMax Reitz# Return the length of the protocol file
89e6c79647SMax Reitz# $1: Protocol node export mount point
90e6c79647SMax Reitz# $2: Original file (to compare)
91e6c79647SMax Reitzget_proto_len()
92e6c79647SMax Reitz{
93e6c79647SMax Reitz    len1=$(stat -c '%s' "$1")
94e6c79647SMax Reitz    len2=$(stat -c '%s' "$2")
95e6c79647SMax Reitz
96e6c79647SMax Reitz    if [ "$len1" != "$len2" ]; then
97e6c79647SMax Reitz        echo 'ERROR: Length of export and original differ:' >&2
98e6c79647SMax Reitz        echo "$len1 != $len2" >&2
99e6c79647SMax Reitz    else
100e6c79647SMax Reitz        echo '(OK: Lengths of export and original are the same)' >&2
101e6c79647SMax Reitz    fi
102e6c79647SMax Reitz
103e6c79647SMax Reitz    echo "$len1"
104e6c79647SMax Reitz}
105e6c79647SMax Reitz
106e6c79647SMax ReitzCOPIED_IMG="$TEST_IMG.copy"
107e6c79647SMax ReitzEXT_MP="$TEST_IMG.fuse"
108e6c79647SMax Reitz
109e6c79647SMax Reitzecho '=== Set up ==='
110e6c79647SMax Reitz
111e6c79647SMax Reitz# Create image with random data
112e6c79647SMax Reitz_make_test_img 64M
113e6c79647SMax Reitz$QEMU_IO -c 'write -s /dev/urandom 0 64M' "$TEST_IMG" | _filter_qemu_io
114e6c79647SMax Reitz
115e6c79647SMax Reitz_launch_qemu
116e6c79647SMax Reitz_send_qemu_cmd $QEMU_HANDLE \
117e6c79647SMax Reitz    "{'execute': 'qmp_capabilities'}" \
118e6c79647SMax Reitz    'return'
119e6c79647SMax Reitz
120e6c79647SMax Reitz# Separate blockdev-add calls for format and protocol so we can remove
121e6c79647SMax Reitz# the format layer later on
122e6c79647SMax Reitz_send_qemu_cmd $QEMU_HANDLE \
123e6c79647SMax Reitz    "{'execute': 'blockdev-add',
124e6c79647SMax Reitz      'arguments': {
125e6c79647SMax Reitz          'driver': 'file',
126e6c79647SMax Reitz          'node-name': 'node-protocol',
127e6c79647SMax Reitz          'filename': '$TEST_IMG'
128e6c79647SMax Reitz      } }" \
129e6c79647SMax Reitz    'return'
130e6c79647SMax Reitz
131e6c79647SMax Reitz_send_qemu_cmd $QEMU_HANDLE \
132e6c79647SMax Reitz    "{'execute': 'blockdev-add',
133e6c79647SMax Reitz      'arguments': {
134e6c79647SMax Reitz          'driver': '$IMGFMT',
135e6c79647SMax Reitz          'node-name': 'node-format',
136e6c79647SMax Reitz          'file': 'node-protocol'
137e6c79647SMax Reitz      } }" \
138e6c79647SMax Reitz    'return'
139e6c79647SMax Reitz
140e6c79647SMax Reitzecho
141e6c79647SMax Reitzecho '=== Mountpoint not present ==='
142e6c79647SMax Reitz
143e6c79647SMax Reitzrmdir "$EXT_MP" 2>/dev/null
144e6c79647SMax Reitzrm -f "$EXT_MP"
145e6c79647SMax Reitzoutput=$(fuse_export_add 'export-err' "'mountpoint': '$EXT_MP'" error)
146e6c79647SMax Reitz
147e6c79647SMax Reitzif echo "$output" | grep -q "Invalid parameter 'fuse'"; then
148e6c79647SMax Reitz    _notrun 'No FUSE support'
149e6c79647SMax Reitzfi
150e6c79647SMax Reitz
151e6c79647SMax Reitzecho "$output"
152e6c79647SMax Reitz
153e6c79647SMax Reitzecho
154e6c79647SMax Reitzecho '=== Mountpoint is a directory ==='
155e6c79647SMax Reitz
156e6c79647SMax Reitzmkdir "$EXT_MP"
157e6c79647SMax Reitzfuse_export_add 'export-err' "'mountpoint': '$EXT_MP'" error
158e6c79647SMax Reitzrmdir "$EXT_MP"
159e6c79647SMax Reitz
160e6c79647SMax Reitzecho
161e6c79647SMax Reitzecho '=== Mountpoint is a regular file ==='
162e6c79647SMax Reitz
163e6c79647SMax Reitztouch "$EXT_MP"
164e6c79647SMax Reitzfuse_export_add 'export-mp' "'mountpoint': '$EXT_MP'"
165e6c79647SMax Reitz
166e6c79647SMax Reitz# Check that the export presents the same data as the original image
167e6c79647SMax Reitz$QEMU_IMG compare -f raw -F $IMGFMT -U "$EXT_MP" "$TEST_IMG"
168e6c79647SMax Reitz
169e6c79647SMax Reitzecho
170e6c79647SMax Reitzecho '=== Mount over existing file ==='
171e6c79647SMax Reitz
172e6c79647SMax Reitz# This is the coolest feature of FUSE exports: You can transparently
173e6c79647SMax Reitz# make images in any format appear as raw images
174e6c79647SMax Reitzfuse_export_add 'export-img' "'mountpoint': '$TEST_IMG'"
175e6c79647SMax Reitz
176e6c79647SMax Reitz# Accesses both exports at the same time, so we get a concurrency test
177e6c79647SMax Reitz$QEMU_IMG compare -f raw -F raw -U "$EXT_MP" "$TEST_IMG"
178e6c79647SMax Reitz
179e6c79647SMax Reitz# Just to be sure, we later want to compare the data offline.  Also,
180e6c79647SMax Reitz# this allows us to see that cp works without complaining.
181e6c79647SMax Reitz# (This is not a given, because cp will expect a short read at EOF.
182e6c79647SMax Reitz# Internally, qemu does not allow short reads, so we have to check
183e6c79647SMax Reitz# whether the FUSE export driver lets them work.)
184e6c79647SMax Reitzcp "$TEST_IMG" "$COPIED_IMG"
185e6c79647SMax Reitz
186e6c79647SMax Reitz# $TEST_IMG will be in mode 0400 because it is read-only; we are going
187e6c79647SMax Reitz# to write to the copy, so make it writable
188e6c79647SMax Reitzchmod 0600 "$COPIED_IMG"
189e6c79647SMax Reitz
190e6c79647SMax Reitzecho
191e6c79647SMax Reitzecho '=== Double export ==='
192e6c79647SMax Reitz
193e6c79647SMax Reitz# We have already seen that exporting a node twice works fine, but you
194e6c79647SMax Reitz# cannot export anything twice on the same mount point.  The reason is
195e6c79647SMax Reitz# that qemu has to stat the given mount point, and this would have to
196e6c79647SMax Reitz# be answered by the same qemu instance if it already has an export
197e6c79647SMax Reitz# there.  However, it cannot answer the stat because it is itself
198e6c79647SMax Reitz# caught up in that same stat.
199e6c79647SMax Reitzfuse_export_add 'export-err' "'mountpoint': '$EXT_MP'" error
200e6c79647SMax Reitz
201e6c79647SMax Reitzecho
202e6c79647SMax Reitzecho '=== Remove export ==='
203e6c79647SMax Reitz
204e6c79647SMax Reitz# Double-check that $EXT_MP appears as a non-empty file (the raw image)
205e6c79647SMax Reitz$QEMU_IMG info -f raw "$EXT_MP" | grep 'virtual size'
206e6c79647SMax Reitz
207e6c79647SMax Reitzfuse_export_del 'export-mp'
208e6c79647SMax Reitz
209e6c79647SMax Reitz# See that the file appears empty again
210e6c79647SMax Reitz$QEMU_IMG info -f raw "$EXT_MP" | grep 'virtual size'
211e6c79647SMax Reitz
212e6c79647SMax Reitzecho
213e6c79647SMax Reitzecho '=== Writable export ==='
214e6c79647SMax Reitz
215e6c79647SMax Reitzfuse_export_add 'export-mp' "'mountpoint': '$EXT_MP', 'writable': true"
216e6c79647SMax Reitz
217e6c79647SMax Reitz# Check that writing to the read-only export fails
218*2c7dd057SMax Reitz$QEMU_IO -f raw -c 'write -P 42 1M 64k' "$TEST_IMG" 2>&1 \
219*2c7dd057SMax Reitz    | _filter_qemu_io | _filter_testdir | _filter_imgfmt
220e6c79647SMax Reitz
221e6c79647SMax Reitz# But here it should work
222e6c79647SMax Reitz$QEMU_IO -f raw -c 'write -P 42 1M 64k' "$EXT_MP" | _filter_qemu_io
223e6c79647SMax Reitz
224e6c79647SMax Reitz# (Adjust the copy, too)
225e6c79647SMax Reitz$QEMU_IO -f raw -c 'write -P 42 1M 64k' "$COPIED_IMG" | _filter_qemu_io
226e6c79647SMax Reitz
227e6c79647SMax Reitzecho
228e6c79647SMax Reitzecho '=== Resizing exports ==='
229e6c79647SMax Reitz
230e6c79647SMax Reitz# Here, we need to export the protocol node -- the format layer may
231e6c79647SMax Reitz# not be growable, simply because the format does not support it.
232e6c79647SMax Reitz
233e6c79647SMax Reitz# Remove all exports and the format node first so permissions will not
234e6c79647SMax Reitz# get in the way
235e6c79647SMax Reitzfuse_export_del 'export-mp'
236e6c79647SMax Reitzfuse_export_del 'export-img'
237e6c79647SMax Reitz
238e6c79647SMax Reitz_send_qemu_cmd $QEMU_HANDLE \
239e6c79647SMax Reitz    "{'execute': 'blockdev-del',
240e6c79647SMax Reitz      'arguments': {
241e6c79647SMax Reitz          'node-name': 'node-format'
242e6c79647SMax Reitz      } }" \
243e6c79647SMax Reitz    'return'
244e6c79647SMax Reitz
245e6c79647SMax Reitz# Now export the protocol node
246e6c79647SMax Reitzfuse_export_add \
247e6c79647SMax Reitz    'export-mp' \
248e6c79647SMax Reitz    "'mountpoint': '$EXT_MP', 'writable': true" \
249e6c79647SMax Reitz    'return' \
250e6c79647SMax Reitz    'node-protocol'
251e6c79647SMax Reitz
252e6c79647SMax Reitzecho
253e6c79647SMax Reitzecho '--- Try growing non-growable export ---'
254e6c79647SMax Reitz
255e6c79647SMax Reitz# Get the current size so we can write beyond the EOF
256e6c79647SMax Reitzorig_len=$(get_proto_len "$EXT_MP" "$TEST_IMG")
257e6c79647SMax Reitzorig_disk_usage=$(stat -c '%b' "$TEST_IMG")
258e6c79647SMax Reitz
259e6c79647SMax Reitz# Should fail (exports are non-growable by default)
260e6c79647SMax Reitz# (Note that qemu-io can never write beyond the EOF, so we have to use
261e6c79647SMax Reitz# dd here)
262e6c79647SMax Reitzdd if=/dev/zero of="$EXT_MP" bs=1 count=64k seek=$orig_len 2>&1 \
263e6c79647SMax Reitz    | _filter_testdir | _filter_imgfmt
264e6c79647SMax Reitz
265e6c79647SMax Reitzecho
266e6c79647SMax Reitzecho '--- Resize export ---'
267e6c79647SMax Reitz
268e6c79647SMax Reitz# But we can truncate it explicitly; even with fallocate
269e6c79647SMax Reitzfallocate -o "$orig_len" -l 64k "$EXT_MP"
270e6c79647SMax Reitz
271e6c79647SMax Reitznew_len=$(get_proto_len "$EXT_MP" "$TEST_IMG")
272e6c79647SMax Reitzif [ "$new_len" != "$((orig_len + 65536))" ]; then
273e6c79647SMax Reitz    echo 'ERROR: Unexpected post-truncate image size:'
274e6c79647SMax Reitz    echo "$new_len != $((orig_len + 65536))"
275e6c79647SMax Reitzelse
276e6c79647SMax Reitz    echo 'OK: Post-truncate image size is as expected'
277e6c79647SMax Reitzfi
278e6c79647SMax Reitz
279e6c79647SMax Reitznew_disk_usage=$(stat -c '%b' "$TEST_IMG")
280e6c79647SMax Reitzif [ "$new_disk_usage" -gt "$orig_disk_usage" ]; then
281e6c79647SMax Reitz    echo 'OK: Disk usage grew with fallocate'
282e6c79647SMax Reitzelse
283e6c79647SMax Reitz    echo 'ERROR: Disk usage did not grow despite fallocate:'
284e6c79647SMax Reitz    echo "$orig_disk_usage => $new_disk_usage"
285e6c79647SMax Reitzfi
286e6c79647SMax Reitz
287e6c79647SMax Reitzecho
288e6c79647SMax Reitzecho '--- Try growing growable export ---'
289e6c79647SMax Reitz
290e6c79647SMax Reitz# Now export as growable
291e6c79647SMax Reitzfuse_export_del 'export-mp'
292e6c79647SMax Reitzfuse_export_add \
293e6c79647SMax Reitz    'export-mp' \
294e6c79647SMax Reitz    "'mountpoint': '$EXT_MP', 'writable': true, 'growable': true" \
295e6c79647SMax Reitz    'return' \
296e6c79647SMax Reitz    'node-protocol'
297e6c79647SMax Reitz
298e6c79647SMax Reitz# Now we should be able to write beyond the EOF
299e6c79647SMax Reitzdd if=/dev/zero of="$EXT_MP" bs=1 count=64k seek=$new_len 2>&1 \
300e6c79647SMax Reitz    | _filter_testdir | _filter_imgfmt
301e6c79647SMax Reitz
302e6c79647SMax Reitznew_len=$(get_proto_len "$EXT_MP" "$TEST_IMG")
303e6c79647SMax Reitzif [ "$new_len" != "$((orig_len + 131072))" ]; then
304e6c79647SMax Reitz    echo 'ERROR: Unexpected post-grow image size:'
305e6c79647SMax Reitz    echo "$new_len != $((orig_len + 131072))"
306e6c79647SMax Reitzelse
307e6c79647SMax Reitz    echo 'OK: Post-grow image size is as expected'
308e6c79647SMax Reitzfi
309e6c79647SMax Reitz
310e6c79647SMax Reitzecho
311e6c79647SMax Reitzecho '--- Shrink export ---'
312e6c79647SMax Reitz
313e6c79647SMax Reitz# Now go back to the original size
314e6c79647SMax Reitztruncate -s "$orig_len" "$EXT_MP"
315e6c79647SMax Reitz
316e6c79647SMax Reitznew_len=$(get_proto_len "$EXT_MP" "$TEST_IMG")
317e6c79647SMax Reitzif [ "$new_len" != "$orig_len" ]; then
318e6c79647SMax Reitz    echo 'ERROR: Unexpected post-truncate image size:'
319e6c79647SMax Reitz    echo "$new_len != $orig_len"
320e6c79647SMax Reitzelse
321e6c79647SMax Reitz    echo 'OK: Post-truncate image size is as expected'
322e6c79647SMax Reitzfi
323e6c79647SMax Reitz
324e6c79647SMax Reitzecho
325e6c79647SMax Reitzecho '=== Tear down ==='
326e6c79647SMax Reitz
327e6c79647SMax Reitz_send_qemu_cmd $QEMU_HANDLE \
328e6c79647SMax Reitz    "{'execute': 'quit'}" \
329e6c79647SMax Reitz    'return'
330e6c79647SMax Reitz
331e6c79647SMax Reitzwait=yes _cleanup_qemu
332e6c79647SMax Reitz
333e6c79647SMax Reitzecho
334e6c79647SMax Reitzecho '=== Compare copy with original ==='
335e6c79647SMax Reitz
336e6c79647SMax Reitz$QEMU_IMG compare -f raw -F $IMGFMT "$COPIED_IMG" "$TEST_IMG"
337e6c79647SMax Reitz
338e6c79647SMax Reitz# success, all done
339e6c79647SMax Reitzecho "*** done"
340e6c79647SMax Reitzrm -f $seq.full
341e6c79647SMax Reitzstatus=0
342