3 # Copyright (C) 2018-2022 Red Hat Inc.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
16 # * Neither the name of Red Hat nor the names of its contributors may be
17 # used to endorse or promote products derived from this software without
18 # specific prior written permission.
20 # THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
21 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
24 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
27 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 requires
$STAT --version
39 sock2
=$
(mktemp
-u /tmp
/nbdkit-test-sock.XXXXXX
)
40 sock3
=$
(mktemp
-u /tmp
/nbdkit-test-sock.XXXXXX
)
41 sock4a
=$
(mktemp
-u /tmp
/nbdkit-test-sock.XXXXXX
)
42 sock4b
=$
(mktemp
-u /tmp
/nbdkit-test-sock.XXXXXX
)
43 sock5
=$
(mktemp
-u /tmp
/nbdkit-test-sock.XXXXXX
)
44 files
="nozero1.img nozero1.log
45 nozero2.img nozero2.log $sock2 nozero2.pid
46 nozero3.img nozero3.log $sock3 nozero3.pid
47 nozero4.img nozero4a.log nozero4b.log $sock4a $sock4b
48 nozero4a.pid nozero4b.pid
49 nozero5.img nozero5.log $sock5 nozero5.pid"
53 # For easier debugging, dump the final log files before removing them
57 echo "Log 1 file contents:"
59 echo "Log 2 file contents:"
61 echo "Log 3 file contents:"
63 echo "Log 4a file contents:"
65 echo "Log 4b file contents:"
67 echo "Log 5 file contents:"
75 printf %$
((2*1024*1024))s .
> nozero1.img
76 cp nozero1.img nozero2.img
77 cp nozero1.img nozero3.img
78 cp nozero1.img nozero4.img
79 cp nozero1.img nozero5.img
81 # Debug number of blocks and block size in the images.
83 $STAT -c "%n: %b allocated blocks of size %B bytes, total size %s" \
85 sizes
[$f]=$
($STAT -c %b nozero
$f.img
)
88 # Check that zero with trim results in a sparse image.
89 requires nbdkit
-U - --filter=log
file logfile
=nozero1.log nozero1.img \
90 --run 'nbdsh -u "$uri" -c "h.zero(1024*1024, 0)"'
91 if test "$($STAT -c %b nozero1.img)" = "${sizes[1]}"; then
92 echo "$0: can't trim file by writing zeroes"
96 # Run several parallel nbdkit; to compare the logs and see what changes.
97 # 1: unfiltered (above), check that nbdsh sends ZERO request and plugin trims
98 # 2: log before filter with zeromode=none (default), to ensure no ZERO request
99 # 3: log after filter with zeromode=emulate, to ensure no ZERO to plugin
100 # 4a/b: both sides of nbd plugin: even though server side does not advertise
101 # ZERO, the client side still exposes it, and just skips calling nbd's .zero
102 # 5: log after filter with zeromode=notrim, to ensure plugin does not trim
103 start_nbdkit
-P nozero2.pid
-U $sock2 --filter=log
--filter=nozero \
104 file logfile
=nozero2.log nozero2.img
105 start_nbdkit
-P nozero3.pid
-U $sock3 --filter=nozero
--filter=log \
106 file logfile
=nozero3.log nozero3.img zeromode
=emulate
107 # Start 4b before 4a so that cleanup visits the client before the server
108 start_nbdkit
-P nozero4b.pid
-U $sock4b --filter=log \
109 nbd logfile
=nozero4b.log socket
=$sock4a
110 start_nbdkit
-P nozero4a.pid
-U $sock4a --filter=log
--filter=nozero \
111 file logfile
=nozero4a.log nozero4.img
112 start_nbdkit
-P nozero5.pid
-U $sock5 --filter=nozero
--filter=log \
113 file logfile
=nozero5.log nozero5.img zeromode
=notrim
115 # Perform the zero write.
116 nbdsh
-u "nbd+unix://?socket=$sock2" -c '
117 assert not h.can_zero()
118 h.pwrite (bytearray(1024*1024), 0)
120 nbdsh
-u "nbd+unix://?socket=$sock3" -c 'h.zero(1024*1024, 0)'
121 nbdsh
-u "nbd+unix://?socket=$sock4b" -c 'h.zero(1024*1024, 0)'
122 nbdsh
-u "nbd+unix://?socket=$sock5" -c 'h.zero(1024*1024, 0)'
124 # Check for expected ZERO vs. WRITE results
125 grep 'connection=1 Zero' nozero1.log || fail
=1
126 if grep 'connection=1 Zero' nozero2.log
; then
127 echo "filter should have prevented zero"
130 if grep 'connection=1 Zero' nozero3.log
; then
131 echo "filter should have converted zero into write"
134 if grep 'connection=1 Zero' nozero4a.log
; then
135 echo "nbdkit should have converted zero into write before nbd plugin"
138 grep 'connection=1 Zero' nozero5.log
140 # Sanity check on contents - all 5 files should read identically
141 cmp nozero1.img nozero2.img
142 cmp nozero2.img nozero3.img
143 cmp nozero3.img nozero4.img
144 cmp nozero4.img nozero5.img
146 # Sanity check on sparseness: images 2-5 should not be sparse (although the
147 # filesystem may have reserved additional space due to our writes)
149 if test "$($STAT -c %b nozero$i.img)" -lt "${sizes[$i]}"; then
150 echo "nozero$i.img was trimmed by mistake"