3 # Copyright (C) 2018 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
43 files
="nozero1.img nozero1.log $sock1 nozero1.pid
44 nozero2.img nozero2.log $sock2 nozero2.pid
45 nozero3.img nozero3.log $sock3 nozero3.pid
46 nozero4.img nozero4.log $sock4 nozero4.pid
47 nozero5.img nozero5a.log nozero5b.log $sock5a $sock5b
48 nozero5a.pid nozero5b.pid
49 nozero6.img nozero6.log $sock6 nozero6.pid"
52 # Prep images, and check that qemu-io understands the actions we plan on
53 # doing, and that zero with trim results in a sparse image.
54 for f
in {0.
.1023}; do printf '%1024s' .
>> nozero1.img
; done
55 cp nozero1.img nozero2.img
56 cp nozero1.img nozero3.img
57 cp nozero1.img nozero4.img
58 cp nozero1.img nozero5.img
59 cp nozero1.img nozero6.img
60 if ! qemu-io
-f raw
-d unmap
-c 'w -z -u 0 1M' nozero1.img
; then
61 echo "$0: missing or broken qemu-io"
65 if test "$(stat -c %b nozero1.img)" = "$(stat -c %b nozero2.img)"; then
66 echo "$0: can't trim file by writing zeroes"
70 cp nozero2.img nozero1.img
72 # For easier debugging, dump the final log files before removing them
76 echo "Log 1 file contents:"
78 echo "Log 2 file contents:"
80 echo "Log 3 file contents:"
82 echo "Log 4 file contents:"
84 echo "Log 5a file contents:"
86 echo "Log 5b file contents:"
88 echo "Log 6 file contents:"
94 # Run several parallel nbdkit; to compare the logs and see what changes.
95 # 1: unfiltered, to check that qemu-io sends ZERO request and plugin trims
96 # 2: log before filter with zeromode=none (default), to ensure no ZERO request
97 # 3: log before filter with zeromode=emulate, to ensure ZERO from client
98 # 4: log after filter with zeromode=emulate, to ensure no ZERO to plugin
99 # 5a/b: both sides of nbd plugin: even though server side does not advertise
100 # ZERO, the client side still exposes it, and just skips calling nbd's .zero
101 # 6: log after filter with zeromode=notrim, to ensure plugin does not trim
102 start_nbdkit
-P nozero1.pid
-U $sock1 --filter=log \
103 file logfile
=nozero1.log nozero1.img
104 start_nbdkit
-P nozero2.pid
-U $sock2 --filter=log
--filter=nozero \
105 file logfile
=nozero2.log nozero2.img
106 start_nbdkit
-P nozero3.pid
-U $sock3 --filter=log
--filter=nozero \
107 file logfile
=nozero3.log nozero3.img zeromode
=emulate
108 start_nbdkit
-P nozero4.pid
-U $sock4 --filter=nozero
--filter=log \
109 file logfile
=nozero4.log nozero4.img zeromode
=emulate
110 start_nbdkit
-P nozero5a.pid
-U $sock5a --filter=log
--filter=nozero \
111 file logfile
=nozero5a.log nozero5.img
112 start_nbdkit
-P nozero5b.pid
-U $sock5b --filter=log \
113 nbd logfile
=nozero5b.log socket
=$sock5a
114 start_nbdkit
-P nozero6.pid
-U $sock6 --filter=nozero
--filter=log \
115 file logfile
=nozero6.log nozero6.img zeromode
=notrim
117 # Perform the zero write.
118 qemu-io
-f raw
-c 'w -z -u 0 1M' "nbd+unix://?socket=$sock1"
119 qemu-io
-f raw
-c 'w -z -u 0 1M' "nbd+unix://?socket=$sock2"
120 qemu-io
-f raw
-c 'w -z -u 0 1M' "nbd+unix://?socket=$sock3"
121 qemu-io
-f raw
-c 'w -z -u 0 1M' "nbd+unix://?socket=$sock4"
122 qemu-io
-f raw
-c 'w -z -u 0 1M' "nbd+unix://?socket=$sock5b"
123 qemu-io
-f raw
-c 'w -z -u 0 1M' "nbd+unix://?socket=$sock6"
125 # Check for expected ZERO vs. WRITE results
126 grep 'connection=1 Zero' nozero1.log
127 if grep 'connection=1 Zero' nozero2.log
; then
128 echo "filter should have prevented zero"
131 grep 'connection=1 Zero' nozero3.log
132 if grep 'connection=1 Zero' nozero4.log
; then
133 echo "filter should have converted zero into write"
136 grep 'connection=1 Zero' nozero5b.log
137 if grep 'connection=1 Zero' nozero5a.log
; then
138 echo "nbdkit should have converted zero into write before nbd plugin"
141 grep 'connection=1 Zero' nozero6.log
143 # Sanity check on contents - all 5 files should read identically
144 cmp nozero1.img nozero2.img
145 cmp nozero2.img nozero3.img
146 cmp nozero3.img nozero4.img
147 cmp nozero4.img nozero5.img
148 cmp nozero5.img nozero6.img
150 # Sanity check on sparseness; only image 1 should be sparse
151 if test "$(stat -c %b nozero1.img)" = "$(stat -c %b nozero2.img)"; then
152 echo "nozero1.img was not trimmed"
156 if test "$(stat -c %b nozero2.img)" != "$(stat -c %b nozero$i.img)"; then
157 echo "nozero$i.img was trimmed by mistake"