Add qemu 2.4.0
[kvmfornfv.git] / qemu / tests / qemu-iotests / 060
1 #!/bin/bash
2 #
3 # Test case for image corruption (overlapping data structures) in qcow2
4 #
5 # Copyright (C) 2013 Red Hat, Inc.
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20
21 # creator
22 owner=mreitz@redhat.com
23
24 seq="$(basename $0)"
25 echo "QA output created by $seq"
26
27 here="$PWD"
28 tmp=/tmp/$$
29 status=1        # failure is the default!
30
31 _cleanup()
32 {
33         _cleanup_test_img
34 }
35 trap "_cleanup; exit \$status" 0 1 2 3 15
36
37 # get standard environment, filters and checks
38 . ./common.rc
39 . ./common.filter
40
41 # This tests qocw2-specific low-level functionality
42 _supported_fmt qcow2
43 _supported_proto file
44 _supported_os Linux
45
46 rt_offset=65536  # 0x10000 (XXX: just an assumption)
47 rb_offset=131072 # 0x20000 (XXX: just an assumption)
48 l1_offset=196608 # 0x30000 (XXX: just an assumption)
49 l2_offset=262144 # 0x40000 (XXX: just an assumption)
50 l2_offset_after_snapshot=524288 # 0x80000 (XXX: just an assumption)
51
52 IMGOPTS="compat=1.1"
53
54 OPEN_RW="open -o overlap-check=all $TEST_IMG"
55 # Overlap checks are done before write operations only, therefore opening an
56 # image read-only makes the overlap-check option irrelevant
57 OPEN_RO="open -r $TEST_IMG"
58
59 echo
60 echo "=== Testing L2 reference into L1 ==="
61 echo
62 _make_test_img 64M
63 # Link first L1 entry (first L2 table) onto itself
64 # (Note the MSb in the L1 entry is set, ensuring the refcount is one - else any
65 # later write will result in a COW operation, effectively ruining this attempt
66 # on image corruption)
67 poke_file "$TEST_IMG" "$l1_offset" "\x80\x00\x00\x00\x00\x03\x00\x00"
68 _check_test_img
69
70 # The corrupt bit should not be set anyway
71 $PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
72
73 # Try to write something, thereby forcing the corrupt bit to be set
74 $QEMU_IO -c "$OPEN_RW" -c "write -P 0x2a 0 512" | _filter_qemu_io
75
76 # The corrupt bit must now be set
77 $PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
78
79 # This information should be available through qemu-img info
80 _img_info --format-specific
81
82 # Try to open the image R/W (which should fail)
83 $QEMU_IO -c "$OPEN_RW" -c "read 0 512" 2>&1 | _filter_qemu_io \
84                                             | _filter_testdir \
85                                             | _filter_imgfmt
86
87 # Try to open it RO (which should succeed)
88 $QEMU_IO -c "$OPEN_RO" -c "read 0 512" | _filter_qemu_io
89
90 # We could now try to fix the image, but this would probably fail (how should an
91 # L2 table linked onto the L1 table be fixed?)
92
93 echo
94 echo "=== Testing cluster data reference into refcount block ==="
95 echo
96 _make_test_img 64M
97 # Allocate L2 table
98 truncate -s "$(($l2_offset+65536))" "$TEST_IMG"
99 poke_file "$TEST_IMG" "$l1_offset" "\x80\x00\x00\x00\x00\x04\x00\x00"
100 # Mark cluster as used
101 poke_file "$TEST_IMG" "$(($rb_offset+8))" "\x00\x01"
102 # Redirect new data cluster onto refcount block
103 poke_file "$TEST_IMG" "$l2_offset" "\x80\x00\x00\x00\x00\x02\x00\x00"
104 _check_test_img
105 $PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
106 $QEMU_IO -c "$OPEN_RW" -c "write -P 0x2a 0 512" | _filter_qemu_io
107 $PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
108
109 # Try to fix it
110 _check_test_img -r all
111
112 # The corrupt bit should be cleared
113 $PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
114
115 # Look if it's really really fixed
116 $QEMU_IO -c "$OPEN_RW" -c "write -P 0x2a 0 512" | _filter_qemu_io
117 $PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
118
119 echo
120 echo "=== Testing cluster data reference into inactive L2 table ==="
121 echo
122 _make_test_img 64M
123 $QEMU_IO -c "$OPEN_RW" -c "write -P 1 0 512" | _filter_qemu_io
124 $QEMU_IMG snapshot -c foo "$TEST_IMG"
125 $QEMU_IO -c "$OPEN_RW" -c "write -P 2 0 512" | _filter_qemu_io
126 # The inactive L2 table remains at its old offset
127 poke_file "$TEST_IMG" "$l2_offset_after_snapshot" \
128                       "\x80\x00\x00\x00\x00\x04\x00\x00"
129 _check_test_img
130 $PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
131 $QEMU_IO -c "$OPEN_RW" -c "write -P 3 0 512" | _filter_qemu_io
132 $PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
133 _check_test_img -r all
134 $PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
135 $QEMU_IO -c "$OPEN_RW" -c "write -P 4 0 512" | _filter_qemu_io
136 $PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
137
138 # Check data
139 $QEMU_IO -c "$OPEN_RO" -c "read -P 4 0 512" | _filter_qemu_io
140 $QEMU_IMG snapshot -a foo "$TEST_IMG"
141 _check_test_img
142 $QEMU_IO -c "$OPEN_RO" -c "read -P 1 0 512" | _filter_qemu_io
143
144 echo
145 echo "=== Testing overlap while COW is in flight ==="
146 echo
147 # compat=0.10 is required in order to make the following discard actually
148 # unallocate the sector rather than make it a zero sector - we want COW, after
149 # all.
150 IMGOPTS='compat=0.10' _make_test_img 1G
151 # Write two clusters, the second one enforces creation of an L2 table after
152 # the first data cluster.
153 $QEMU_IO -c 'write 0k 64k' -c 'write 512M 64k' "$TEST_IMG" | _filter_qemu_io
154 # Discard the first cluster. This cluster will soon enough be reallocated and
155 # used for COW.
156 $QEMU_IO -c 'discard 0k 64k' "$TEST_IMG" | _filter_qemu_io
157 # Now, corrupt the image by marking the second L2 table cluster as free.
158 poke_file "$TEST_IMG" '131084' "\x00\x00" # 0x2000c
159 # Start a write operation requiring COW on the image stopping it right before
160 # doing the read; then, trigger the corruption prevention by writing anything to
161 # any unallocated cluster, leading to an attempt to overwrite the second L2
162 # table. Finally, resume the COW write and see it fail (but not crash).
163 echo "open -o file.driver=blkdebug $TEST_IMG
164 break cow_read 0
165 aio_write 0k 1k
166 wait_break 0
167 write 64k 64k
168 resume 0" | $QEMU_IO | _filter_qemu_io
169
170 echo
171 echo "=== Testing unallocated image header ==="
172 echo
173 _make_test_img 64M
174 # Create L1/L2
175 $QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io
176 poke_file "$TEST_IMG" "$rb_offset" "\x00\x00"
177 $QEMU_IO -c "write 64k 64k" "$TEST_IMG" | _filter_qemu_io
178
179 echo
180 echo "=== Testing unaligned L1 entry ==="
181 echo
182 _make_test_img 64M
183 $QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io
184 # This will be masked with ~(512 - 1) = ~0x1ff, so whether the lower 9 bits are
185 # aligned or not does not matter
186 poke_file "$TEST_IMG" "$l1_offset" "\x80\x00\x00\x00\x00\x04\x2a\x00"
187 $QEMU_IO -c "read 0 64k" "$TEST_IMG" | _filter_qemu_io
188
189 # Test how well zero cluster expansion can cope with this
190 _make_test_img 64M
191 $QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io
192 poke_file "$TEST_IMG" "$l1_offset" "\x80\x00\x00\x00\x00\x04\x2a\x00"
193 $QEMU_IMG amend -o compat=0.10 "$TEST_IMG"
194
195 echo
196 echo "=== Testing unaligned L2 entry ==="
197 echo
198 _make_test_img 64M
199 $QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io
200 poke_file "$TEST_IMG" "$l2_offset" "\x80\x00\x00\x00\x00\x05\x2a\x00"
201 $QEMU_IO -c "read 0 64k" "$TEST_IMG" | _filter_qemu_io
202
203 echo
204 echo "=== Testing unaligned pre-allocated zero cluster ==="
205 echo
206 _make_test_img 64M
207 $QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io
208 poke_file "$TEST_IMG" "$l2_offset" "\x80\x00\x00\x00\x00\x05\x2a\x01"
209 # zero cluster expansion
210 $QEMU_IMG amend -o compat=0.10 "$TEST_IMG"
211
212 echo
213 echo "=== Testing unaligned reftable entry ==="
214 echo
215 _make_test_img 64M
216 poke_file "$TEST_IMG" "$rt_offset" "\x00\x00\x00\x00\x00\x02\x2a\x00"
217 $QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io
218
219 echo
220 echo "=== Testing non-fatal corruption on freeing ==="
221 echo
222 _make_test_img 64M
223 $QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io
224 poke_file "$TEST_IMG" "$l2_offset" "\x80\x00\x00\x00\x00\x05\x2a\x00"
225 $QEMU_IO -c "discard 0 64k" "$TEST_IMG" | _filter_qemu_io
226
227 echo
228 echo "=== Testing read-only corruption report ==="
229 echo
230 _make_test_img 64M
231 $QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io
232 poke_file "$TEST_IMG" "$l2_offset" "\x80\x00\x00\x00\x00\x05\x2a\x00"
233 # Should only emit a single error message
234 $QEMU_IO -c "$OPEN_RO" -c "read 0 64k" -c "read 0 64k" | _filter_qemu_io
235
236 echo
237 echo "=== Testing non-fatal and then fatal corruption report ==="
238 echo
239 _make_test_img 64M
240 $QEMU_IO -c "write 0 128k" "$TEST_IMG" | _filter_qemu_io
241 poke_file "$TEST_IMG" "$l2_offset"        "\x80\x00\x00\x00\x00\x05\x2a\x00"
242 poke_file "$TEST_IMG" "$(($l2_offset+8))" "\x80\x00\x00\x00\x00\x06\x2a\x00"
243 # Should emit two error messages
244 $QEMU_IO -c "discard 0 64k" -c "read 64k 64k" "$TEST_IMG" | _filter_qemu_io
245
246 # success, all done
247 echo "*** done"
248 rm -f $seq.full
249 status=0