xref: /openbmc/qemu/tests/qemu-iotests/060 (revision 57284d2a)
111a82d14SPhilippe Mathieu-Daudé#!/usr/bin/env bash
2ca0eca91SMax Reitz#
3ca0eca91SMax Reitz# Test case for image corruption (overlapping data structures) in qcow2
4ca0eca91SMax Reitz#
5ca0eca91SMax Reitz# Copyright (C) 2013 Red Hat, Inc.
6ca0eca91SMax Reitz#
7ca0eca91SMax Reitz# This program is free software; you can redistribute it and/or modify
8ca0eca91SMax Reitz# it under the terms of the GNU General Public License as published by
9ca0eca91SMax Reitz# the Free Software Foundation; either version 2 of the License, or
10ca0eca91SMax Reitz# (at your option) any later version.
11ca0eca91SMax Reitz#
12ca0eca91SMax Reitz# This program is distributed in the hope that it will be useful,
13ca0eca91SMax Reitz# but WITHOUT ANY WARRANTY; without even the implied warranty of
14ca0eca91SMax Reitz# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15ca0eca91SMax Reitz# GNU General Public License for more details.
16ca0eca91SMax Reitz#
17ca0eca91SMax Reitz# You should have received a copy of the GNU General Public License
18ca0eca91SMax Reitz# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19ca0eca91SMax Reitz#
20ca0eca91SMax Reitz
21ca0eca91SMax Reitz# creator
22ca0eca91SMax Reitzowner=mreitz@redhat.com
23ca0eca91SMax Reitz
2434eeb82dSMax Reitzseq="$(basename $0)"
25ca0eca91SMax Reitzecho "QA output created by $seq"
26ca0eca91SMax Reitz
27ca0eca91SMax Reitzstatus=1	# failure is the default!
28ca0eca91SMax Reitz
29ca0eca91SMax Reitz_cleanup()
30ca0eca91SMax Reitz{
31ca0eca91SMax Reitz	_cleanup_test_img
32ca0eca91SMax Reitz}
33ca0eca91SMax Reitztrap "_cleanup; exit \$status" 0 1 2 3 15
34ca0eca91SMax Reitz
35cbc4ae2dSPeter Xu# Sometimes the error line might be dumped before/after an event
36cbc4ae2dSPeter Xu# randomly.  Mask it out for specific test that may trigger this
37cbc4ae2dSPeter Xu# uncertainty for current test for now.
38cbc4ae2dSPeter Xu_filter_io_error()
39cbc4ae2dSPeter Xu{
40cbc4ae2dSPeter Xu    sed '/Input\/output error/d'
41cbc4ae2dSPeter Xu}
42cbc4ae2dSPeter Xu
43ca0eca91SMax Reitz# get standard environment, filters and checks
44ca0eca91SMax Reitz. ./common.rc
45ca0eca91SMax Reitz. ./common.filter
46ca0eca91SMax Reitz
47e696f335SMax Reitz# This tests qcow2-specific low-level functionality
48ca0eca91SMax Reitz_supported_fmt qcow2
49*57284d2aSMax Reitz_supported_proto file fuse
50ca0eca91SMax Reitz_supported_os Linux
513be2024aSMax Reitz# These tests only work for compat=1.1 images without an external
523be2024aSMax Reitz# data file with refcount_bits=16
533be2024aSMax Reitz_unsupported_imgopts 'compat=0.10' data_file \
543be2024aSMax Reitz    'refcount_bits=\([^1]\|.\([^6]\|$\)\)'
55ca0eca91SMax Reitz
5624eba765SThomas Huth# The repair process will create a large file - so check for availability first
5724eba765SThomas Huth_require_large_file 64G
5824eba765SThomas Huth
59ca0eca91SMax Reitzrt_offset=65536  # 0x10000 (XXX: just an assumption)
60ca0eca91SMax Reitzrb_offset=131072 # 0x20000 (XXX: just an assumption)
61ca0eca91SMax Reitzl1_offset=196608 # 0x30000 (XXX: just an assumption)
62ca0eca91SMax Reitzl2_offset=262144 # 0x40000 (XXX: just an assumption)
6334eeb82dSMax Reitzl2_offset_after_snapshot=524288 # 0x80000 (XXX: just an assumption)
64ca0eca91SMax Reitz
6534eeb82dSMax ReitzOPEN_RW="open -o overlap-check=all $TEST_IMG"
6634eeb82dSMax Reitz# Overlap checks are done before write operations only, therefore opening an
6734eeb82dSMax Reitz# image read-only makes the overlap-check option irrelevant
6834eeb82dSMax ReitzOPEN_RO="open -r $TEST_IMG"
6934eeb82dSMax Reitz
70ca0eca91SMax Reitzecho
71ca0eca91SMax Reitzecho "=== Testing L2 reference into L1 ==="
72ca0eca91SMax Reitzecho
73ca0eca91SMax Reitz_make_test_img 64M
74ca0eca91SMax Reitz# Link first L1 entry (first L2 table) onto itself
75ca0eca91SMax Reitz# (Note the MSb in the L1 entry is set, ensuring the refcount is one - else any
76ca0eca91SMax Reitz# later write will result in a COW operation, effectively ruining this attempt
77ca0eca91SMax Reitz# on image corruption)
78ca0eca91SMax Reitzpoke_file "$TEST_IMG" "$l1_offset" "\x80\x00\x00\x00\x00\x03\x00\x00"
79ca0eca91SMax Reitz_check_test_img
80ca0eca91SMax Reitz
81ca0eca91SMax Reitz# The corrupt bit should not be set anyway
82ea81ca9dSMax Reitz$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
83ca0eca91SMax Reitz
84ca0eca91SMax Reitz# Try to write something, thereby forcing the corrupt bit to be set
8534eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RW" -c "write -P 0x2a 0 512" | _filter_qemu_io
86ca0eca91SMax Reitz
87ca0eca91SMax Reitz# The corrupt bit must now be set
88ea81ca9dSMax Reitz$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
89ca0eca91SMax Reitz
90f383611aSMax Reitz# This information should be available through qemu-img info
91e800e5d4SKevin Wolf_img_info --format-specific
92f383611aSMax Reitz
93ca0eca91SMax Reitz# Try to open the image R/W (which should fail)
9434eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RW" -c "read 0 512" 2>&1 | _filter_qemu_io \
9534eeb82dSMax Reitz                                            | _filter_testdir \
9634eeb82dSMax Reitz                                            | _filter_imgfmt
97ca0eca91SMax Reitz
98ca0eca91SMax Reitz# Try to open it RO (which should succeed)
9934eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RO" -c "read 0 512" | _filter_qemu_io
100ca0eca91SMax Reitz
101ca0eca91SMax Reitz# We could now try to fix the image, but this would probably fail (how should an
102ca0eca91SMax Reitz# L2 table linked onto the L1 table be fixed?)
103ca0eca91SMax Reitz
104ca0eca91SMax Reitzecho
105ca0eca91SMax Reitzecho "=== Testing cluster data reference into refcount block ==="
106ca0eca91SMax Reitzecho
107ca0eca91SMax Reitz_make_test_img 64M
108ca0eca91SMax Reitz# Allocate L2 table
109ca0eca91SMax Reitztruncate -s "$(($l2_offset+65536))" "$TEST_IMG"
110ca0eca91SMax Reitzpoke_file "$TEST_IMG" "$l1_offset" "\x80\x00\x00\x00\x00\x04\x00\x00"
111ca0eca91SMax Reitz# Mark cluster as used
112ca0eca91SMax Reitzpoke_file "$TEST_IMG" "$(($rb_offset+8))" "\x00\x01"
113ca0eca91SMax Reitz# Redirect new data cluster onto refcount block
114ca0eca91SMax Reitzpoke_file "$TEST_IMG" "$l2_offset" "\x80\x00\x00\x00\x00\x02\x00\x00"
115ca0eca91SMax Reitz_check_test_img
116ea81ca9dSMax Reitz$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
11734eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RW" -c "write -P 0x2a 0 512" | _filter_qemu_io
118ea81ca9dSMax Reitz$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
119ca0eca91SMax Reitz
120ca0eca91SMax Reitz# Try to fix it
121ca0eca91SMax Reitz_check_test_img -r all
122ca0eca91SMax Reitz
123ca0eca91SMax Reitz# The corrupt bit should be cleared
124ea81ca9dSMax Reitz$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
125ca0eca91SMax Reitz
126ca0eca91SMax Reitz# Look if it's really really fixed
12734eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RW" -c "write -P 0x2a 0 512" | _filter_qemu_io
128ea81ca9dSMax Reitz$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
129ca0eca91SMax Reitz
13034eeb82dSMax Reitzecho
13134eeb82dSMax Reitzecho "=== Testing cluster data reference into inactive L2 table ==="
13234eeb82dSMax Reitzecho
13334eeb82dSMax Reitz_make_test_img 64M
13434eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RW" -c "write -P 1 0 512" | _filter_qemu_io
13534eeb82dSMax Reitz$QEMU_IMG snapshot -c foo "$TEST_IMG"
13634eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RW" -c "write -P 2 0 512" | _filter_qemu_io
13734eeb82dSMax Reitz# The inactive L2 table remains at its old offset
13834eeb82dSMax Reitzpoke_file "$TEST_IMG" "$l2_offset_after_snapshot" \
13934eeb82dSMax Reitz                      "\x80\x00\x00\x00\x00\x04\x00\x00"
14034eeb82dSMax Reitz_check_test_img
141ea81ca9dSMax Reitz$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
14234eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RW" -c "write -P 3 0 512" | _filter_qemu_io
143ea81ca9dSMax Reitz$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
14434eeb82dSMax Reitz_check_test_img -r all
145ea81ca9dSMax Reitz$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
14634eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RW" -c "write -P 4 0 512" | _filter_qemu_io
147ea81ca9dSMax Reitz$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
14834eeb82dSMax Reitz
14934eeb82dSMax Reitz# Check data
15034eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RO" -c "read -P 4 0 512" | _filter_qemu_io
15134eeb82dSMax Reitz$QEMU_IMG snapshot -a foo "$TEST_IMG"
15234eeb82dSMax Reitz_check_test_img
15334eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RO" -c "read -P 1 0 512" | _filter_qemu_io
15434eeb82dSMax Reitz
15598d39e34SMax Reitzecho
15698d39e34SMax Reitzecho "=== Testing overlap while COW is in flight ==="
15798d39e34SMax Reitzecho
158c8bb23cbSAnton NefedovBACKING_IMG=$TEST_IMG.base
159c8bb23cbSAnton NefedovTEST_IMG=$BACKING_IMG _make_test_img 1G
160c8bb23cbSAnton Nefedov
161c8bb23cbSAnton Nefedov$QEMU_IO -c 'write 0k 64k' "$BACKING_IMG" | _filter_qemu_io
162c8bb23cbSAnton Nefedov
163b66ff2c2SEric Blake_make_test_img -b "$BACKING_IMG" -F $IMGFMT 1G
16498d39e34SMax Reitz# Write two clusters, the second one enforces creation of an L2 table after
16598d39e34SMax Reitz# the first data cluster.
16698d39e34SMax Reitz$QEMU_IO -c 'write 0k 64k' -c 'write 512M 64k' "$TEST_IMG" | _filter_qemu_io
16780f5c011SAlberto Garcia# Free the first cluster. This cluster will soon enough be reallocated and
16898d39e34SMax Reitz# used for COW.
16980f5c011SAlberto Garciapoke_file "$TEST_IMG" "$l2_offset" "\x00\x00\x00\x00\x00\x00\x00\x00"
17080f5c011SAlberto Garciapoke_file "$TEST_IMG" "$(($rb_offset+10))" "\x00\x00"
17198d39e34SMax Reitz# Now, corrupt the image by marking the second L2 table cluster as free.
17280f5c011SAlberto Garciapoke_file "$TEST_IMG" "$(($rb_offset+12))" "\x00\x00"
17398d39e34SMax Reitz# Start a write operation requiring COW on the image stopping it right before
17498d39e34SMax Reitz# doing the read; then, trigger the corruption prevention by writing anything to
17598d39e34SMax Reitz# any unallocated cluster, leading to an attempt to overwrite the second L2
17698d39e34SMax Reitz# table. Finally, resume the COW write and see it fail (but not crash).
17798d39e34SMax Reitzecho "open -o file.driver=blkdebug $TEST_IMG
17898d39e34SMax Reitzbreak cow_read 0
17998d39e34SMax Reitzaio_write 0k 1k
18098d39e34SMax Reitzwait_break 0
18198d39e34SMax Reitzwrite 64k 64k
18298d39e34SMax Reitzresume 0" | $QEMU_IO | _filter_qemu_io
18398d39e34SMax Reitz
184a42f8a3dSMax Reitzecho
185a42f8a3dSMax Reitzecho "=== Testing unallocated image header ==="
186a42f8a3dSMax Reitzecho
187a42f8a3dSMax Reitz_make_test_img 64M
188a42f8a3dSMax Reitz# Create L1/L2
1895b0ed2beSMax Reitz$QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io
190a42f8a3dSMax Reitzpoke_file "$TEST_IMG" "$rb_offset" "\x00\x00"
1915b0ed2beSMax Reitz$QEMU_IO -c "write 64k 64k" "$TEST_IMG" | _filter_qemu_io
1925b0ed2beSMax Reitz
1935b0ed2beSMax Reitzecho
1945b0ed2beSMax Reitzecho "=== Testing unaligned L1 entry ==="
1955b0ed2beSMax Reitzecho
1965b0ed2beSMax Reitz_make_test_img 64M
1975b0ed2beSMax Reitz$QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io
1985b0ed2beSMax Reitz# This will be masked with ~(512 - 1) = ~0x1ff, so whether the lower 9 bits are
1995b0ed2beSMax Reitz# aligned or not does not matter
2005b0ed2beSMax Reitzpoke_file "$TEST_IMG" "$l1_offset" "\x80\x00\x00\x00\x00\x04\x2a\x00"
2015b0ed2beSMax Reitz$QEMU_IO -c "read 0 64k" "$TEST_IMG" | _filter_qemu_io
2025b0ed2beSMax Reitz
203f30136b3SMax Reitz# Test how well zero cluster expansion can cope with this
204f30136b3SMax Reitz_make_test_img 64M
205f30136b3SMax Reitz$QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io
206f30136b3SMax Reitzpoke_file "$TEST_IMG" "$l1_offset" "\x80\x00\x00\x00\x00\x04\x2a\x00"
207f30136b3SMax Reitz$QEMU_IMG amend -o compat=0.10 "$TEST_IMG"
208f30136b3SMax Reitz
2095b0ed2beSMax Reitzecho
2105b0ed2beSMax Reitzecho "=== Testing unaligned L2 entry ==="
2115b0ed2beSMax Reitzecho
2125b0ed2beSMax Reitz_make_test_img 64M
2135b0ed2beSMax Reitz$QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io
2145b0ed2beSMax Reitzpoke_file "$TEST_IMG" "$l2_offset" "\x80\x00\x00\x00\x00\x05\x2a\x00"
2155b0ed2beSMax Reitz$QEMU_IO -c "read 0 64k" "$TEST_IMG" | _filter_qemu_io
2165b0ed2beSMax Reitz
2175b0ed2beSMax Reitzecho
218f30136b3SMax Reitzecho "=== Testing unaligned pre-allocated zero cluster ==="
219f30136b3SMax Reitzecho
220f30136b3SMax Reitz_make_test_img 64M
221f30136b3SMax Reitz$QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io
222f30136b3SMax Reitzpoke_file "$TEST_IMG" "$l2_offset" "\x80\x00\x00\x00\x00\x05\x2a\x01"
223f30136b3SMax Reitz# zero cluster expansion
224f30136b3SMax Reitz$QEMU_IMG amend -o compat=0.10 "$TEST_IMG"
225f30136b3SMax Reitz
226f30136b3SMax Reitzecho
2275b0ed2beSMax Reitzecho "=== Testing unaligned reftable entry ==="
2285b0ed2beSMax Reitzecho
2295b0ed2beSMax Reitz_make_test_img 64M
2305b0ed2beSMax Reitzpoke_file "$TEST_IMG" "$rt_offset" "\x00\x00\x00\x00\x00\x02\x2a\x00"
2315b0ed2beSMax Reitz$QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io
2325b0ed2beSMax Reitz
2335b0ed2beSMax Reitzecho
2345b0ed2beSMax Reitzecho "=== Testing non-fatal corruption on freeing ==="
2355b0ed2beSMax Reitzecho
2365b0ed2beSMax Reitz_make_test_img 64M
2375b0ed2beSMax Reitz$QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io
2385b0ed2beSMax Reitzpoke_file "$TEST_IMG" "$l2_offset" "\x80\x00\x00\x00\x00\x05\x2a\x00"
2395b0ed2beSMax Reitz$QEMU_IO -c "discard 0 64k" "$TEST_IMG" | _filter_qemu_io
2405b0ed2beSMax Reitz
2415b0ed2beSMax Reitzecho
2425b0ed2beSMax Reitzecho "=== Testing read-only corruption report ==="
2435b0ed2beSMax Reitzecho
2445b0ed2beSMax Reitz_make_test_img 64M
2455b0ed2beSMax Reitz$QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io
2465b0ed2beSMax Reitzpoke_file "$TEST_IMG" "$l2_offset" "\x80\x00\x00\x00\x00\x05\x2a\x00"
2475b0ed2beSMax Reitz# Should only emit a single error message
2485b0ed2beSMax Reitz$QEMU_IO -c "$OPEN_RO" -c "read 0 64k" -c "read 0 64k" | _filter_qemu_io
2495b0ed2beSMax Reitz
2505b0ed2beSMax Reitzecho
2515b0ed2beSMax Reitzecho "=== Testing non-fatal and then fatal corruption report ==="
2525b0ed2beSMax Reitzecho
2535b0ed2beSMax Reitz_make_test_img 64M
2545b0ed2beSMax Reitz$QEMU_IO -c "write 0 128k" "$TEST_IMG" | _filter_qemu_io
2555b0ed2beSMax Reitzpoke_file "$TEST_IMG" "$l2_offset"        "\x80\x00\x00\x00\x00\x05\x2a\x00"
2565b0ed2beSMax Reitzpoke_file "$TEST_IMG" "$(($l2_offset+8))" "\x80\x00\x00\x00\x00\x06\x2a\x00"
2575b0ed2beSMax Reitz# Should emit two error messages
2585b0ed2beSMax Reitz$QEMU_IO -c "discard 0 64k" -c "read 64k 64k" "$TEST_IMG" | _filter_qemu_io
259a42f8a3dSMax Reitz
2606bf45d59SAlberto Garciaecho
261ef083f61SAlberto Garciaecho "=== Testing empty refcount table ==="
262ef083f61SAlberto Garciaecho
263ef083f61SAlberto Garcia_make_test_img 64M
264ef083f61SAlberto Garciapoke_file "$TEST_IMG" "$rt_offset"        "\x00\x00\x00\x00\x00\x00\x00\x00"
265ef083f61SAlberto Garcia$QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io
266bcb5270cSAlberto Garcia# Repair the image
267bcb5270cSAlberto Garcia_check_test_img -r all
268ef083f61SAlberto Garcia
269ef083f61SAlberto Garciaecho
2706bf45d59SAlberto Garciaecho "=== Testing empty refcount table with valid L1 and L2 tables ==="
2716bf45d59SAlberto Garciaecho
2726bf45d59SAlberto Garcia_make_test_img 64M
2736bf45d59SAlberto Garcia$QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io
2746bf45d59SAlberto Garciapoke_file "$TEST_IMG" "$rt_offset"        "\x00\x00\x00\x00\x00\x00\x00\x00"
2756bf45d59SAlberto Garcia# Since the first data cluster is already allocated this triggers an
2766bf45d59SAlberto Garcia# allocation with an explicit offset (using qcow2_alloc_clusters_at())
2776bf45d59SAlberto Garcia# causing a refcount block to be allocated at offset 0
2786bf45d59SAlberto Garcia$QEMU_IO -c "write 0 128k" "$TEST_IMG" | _filter_qemu_io
279bcb5270cSAlberto Garcia# Repair the image
280bcb5270cSAlberto Garcia_check_test_img -r all
2816bf45d59SAlberto Garcia
28298839750SAlberto Garciaecho
28398839750SAlberto Garciaecho "=== Testing empty refcount block ==="
28498839750SAlberto Garciaecho
28598839750SAlberto Garcia_make_test_img 64M
28698839750SAlberto Garciapoke_file "$TEST_IMG" "$rb_offset"        "\x00\x00\x00\x00\x00\x00\x00\x00"
28798839750SAlberto Garcia$QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io
288bcb5270cSAlberto Garcia# Repair the image
289bcb5270cSAlberto Garcia_check_test_img -r all
29098839750SAlberto Garcia
2918aa34834SAlberto Garciaecho
2928aa34834SAlberto Garciaecho "=== Testing empty refcount block with compressed write ==="
2938aa34834SAlberto Garciaecho
2948aa34834SAlberto Garcia_make_test_img 64M
2958aa34834SAlberto Garcia$QEMU_IO -c "write 64k 64k" "$TEST_IMG" | _filter_qemu_io
2968aa34834SAlberto Garciapoke_file "$TEST_IMG" "$rb_offset"        "\x00\x00\x00\x00\x00\x00\x00\x00"
2978aa34834SAlberto Garcia# The previous write already allocated an L2 table, so now this new
2988aa34834SAlberto Garcia# write will try to allocate a compressed data cluster at offset 0.
2998aa34834SAlberto Garcia$QEMU_IO -c "write -c 0k 64k" "$TEST_IMG" | _filter_qemu_io
300bcb5270cSAlberto Garcia# Repair the image
301bcb5270cSAlberto Garcia_check_test_img -r all
3028aa34834SAlberto Garcia
303951053a9SAlberto Garciaecho
304951053a9SAlberto Garciaecho "=== Testing zero refcount table size ==="
305951053a9SAlberto Garciaecho
306951053a9SAlberto Garcia_make_test_img 64M
307951053a9SAlberto Garciapoke_file "$TEST_IMG" "56"                "\x00\x00\x00\x00"
308951053a9SAlberto Garcia$QEMU_IO -c "write 0 64k" "$TEST_IMG" 2>&1 | _filter_testdir | _filter_imgfmt
309bcb5270cSAlberto Garcia# Repair the image
310bcb5270cSAlberto Garcia_check_test_img -r all
311951053a9SAlberto Garcia
3125a45da5eSAlberto Garciaecho
3135a45da5eSAlberto Garciaecho "=== Testing incorrect refcount table offset ==="
3145a45da5eSAlberto Garciaecho
3155a45da5eSAlberto Garcia_make_test_img 64M
3165a45da5eSAlberto Garciapoke_file "$TEST_IMG" "48"                "\x00\x00\x00\x00\x00\x00\x00\x00"
3175a45da5eSAlberto Garcia$QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io
3185a45da5eSAlberto Garcia
319791fff50SMax Reitzecho
320791fff50SMax Reitzecho "=== Testing dirty corrupt image ==="
321791fff50SMax Reitzecho
322791fff50SMax Reitz
323791fff50SMax Reitz_make_test_img 64M
324791fff50SMax Reitz
325791fff50SMax Reitz# Let the refblock appear unaligned
326791fff50SMax Reitzpoke_file "$TEST_IMG" "$rt_offset"        "\x00\x00\x00\x00\xff\xff\x2a\x00"
327791fff50SMax Reitz# Mark the image dirty, thus forcing an automatic check when opening it
328791fff50SMax Reitzpoke_file "$TEST_IMG" 72 "\x00\x00\x00\x00\x00\x00\x00\x01"
329791fff50SMax Reitz# Open the image (qemu should refuse to do so)
330791fff50SMax Reitz$QEMU_IO -c close "$TEST_IMG" 2>&1 | _filter_testdir | _filter_imgfmt
331791fff50SMax Reitz
332791fff50SMax Reitzecho '--- Repairing ---'
333791fff50SMax Reitz
334791fff50SMax Reitz# The actual repair should have happened (because of the dirty bit),
335791fff50SMax Reitz# but some cleanup may have failed (like freeing the old reftable)
336791fff50SMax Reitz# because the image was already marked corrupt by that point
337791fff50SMax Reitz_check_test_img -r all
338791fff50SMax Reitz
33993bbaf03SMax Reitzecho
34093bbaf03SMax Reitzecho "=== Writing to an unaligned preallocated zero cluster ==="
34193bbaf03SMax Reitzecho
34293bbaf03SMax Reitz
34393bbaf03SMax Reitz_make_test_img 64M
34493bbaf03SMax Reitz
34593bbaf03SMax Reitz# Allocate the L2 table
34693bbaf03SMax Reitz$QEMU_IO -c "write 0 64k" -c "discard 0 64k" "$TEST_IMG" | _filter_qemu_io
34793bbaf03SMax Reitz# Pretend there is a preallocated zero cluster somewhere inside the
34893bbaf03SMax Reitz# image header
34993bbaf03SMax Reitzpoke_file "$TEST_IMG" "$l2_offset" "\x80\x00\x00\x00\x00\x00\x2a\x01"
35093bbaf03SMax Reitz# Let's write to it!
35193bbaf03SMax Reitz$QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io
35293bbaf03SMax Reitz
353ac5b787aSMax Reitzecho '--- Repairing ---'
354ac5b787aSMax Reitz_check_test_img -r all
35593bbaf03SMax Reitz
356d470ad42SMax Reitzecho
357d470ad42SMax Reitzecho '=== Discarding with an unaligned refblock ==='
358d470ad42SMax Reitzecho
359d470ad42SMax Reitz
360d470ad42SMax Reitz_make_test_img 64M
361d470ad42SMax Reitz
362d470ad42SMax Reitz$QEMU_IO -c "write 0 128k" "$TEST_IMG" | _filter_qemu_io
363d470ad42SMax Reitz# Make our refblock unaligned
364d470ad42SMax Reitzpoke_file "$TEST_IMG" "$(($rt_offset))" "\x00\x00\x00\x00\x00\x00\x2a\x00"
365d470ad42SMax Reitz# Now try to discard something that will be submitted as two requests
366d470ad42SMax Reitz# (main part + tail)
367d470ad42SMax Reitz$QEMU_IO -c "discard 0 65537" "$TEST_IMG"
368d470ad42SMax Reitz
369d470ad42SMax Reitzecho '--- Repairing ---'
370d470ad42SMax Reitz# Fails the first repair because the corruption prevents the check
371d470ad42SMax Reitz# function from double-checking
372d470ad42SMax Reitz# (Using -q for the first invocation, because otherwise the
373d470ad42SMax Reitz#  double-check error message appears above the summary for some
374d470ad42SMax Reitz#  reason -- so let's just hide the summary)
375d470ad42SMax Reitz_check_test_img -q -r all
376d470ad42SMax Reitz_check_test_img -r all
377d470ad42SMax Reitz
37823482f8aSMax Reitzecho
37923482f8aSMax Reitzecho "=== Discarding an out-of-bounds refblock ==="
38023482f8aSMax Reitzecho
38123482f8aSMax Reitz
38223482f8aSMax Reitz_make_test_img 64M
38323482f8aSMax Reitz
38423482f8aSMax Reitz# Pretend there's a refblock really up high
38523482f8aSMax Reitzpoke_file "$TEST_IMG" "$(($rt_offset+8))" "\x00\xff\xff\xff\x00\x00\x00\x00"
38623482f8aSMax Reitz# Let's try to shrink the qcow2 image so that the block driver tries
38723482f8aSMax Reitz# to discard that refblock (and see what happens!)
38823482f8aSMax Reitz$QEMU_IMG resize --shrink "$TEST_IMG" 32M
38923482f8aSMax Reitz
39023482f8aSMax Reitzecho '--- Checking and retrying ---'
39123482f8aSMax Reitz# Image should not be resized
39223482f8aSMax Reitz_img_info | grep 'virtual size'
39323482f8aSMax Reitz# But it should pass this check, because the "partial" resize has
39423482f8aSMax Reitz# already overwritten refblocks past the end
39523482f8aSMax Reitz_check_test_img -r all
39623482f8aSMax Reitz# So let's try again
39723482f8aSMax Reitz$QEMU_IMG resize --shrink "$TEST_IMG" 32M
39823482f8aSMax Reitz_img_info | grep 'virtual size'
39923482f8aSMax Reitz
40023482f8aSMax Reitzecho
40123482f8aSMax Reitzecho "=== Discarding a non-covered in-bounds refblock ==="
40223482f8aSMax Reitzecho
40323482f8aSMax Reitz
404407fb56aSMax Reitz_make_test_img -o 'refcount_bits=1' 64M
40523482f8aSMax Reitz
40623482f8aSMax Reitz# Pretend there's a refblock somewhere where there is no refblock to
40723482f8aSMax Reitz# cover it (but the covering refblock has a valid index in the
40823482f8aSMax Reitz# reftable)
40923482f8aSMax Reitz# Every refblock covers 65536 * 8 * 65536 = 32 GB, so we have to point
41023482f8aSMax Reitz# to 0x10_0000_0000 (64G) to point to the third refblock
41123482f8aSMax Reitzpoke_file "$TEST_IMG" "$(($rt_offset+8))" "\x00\x00\x00\x10\x00\x00\x00\x00"
41223482f8aSMax Reitz$QEMU_IMG resize --shrink "$TEST_IMG" 32M
41323482f8aSMax Reitz
41423482f8aSMax Reitzecho '--- Checking and retrying ---'
41523482f8aSMax Reitz# Image should not be resized
41623482f8aSMax Reitz_img_info | grep 'virtual size'
41723482f8aSMax Reitz# But it should pass this check, because the "partial" resize has
41823482f8aSMax Reitz# already overwritten refblocks past the end
41923482f8aSMax Reitz_check_test_img -r all
42023482f8aSMax Reitz# So let's try again
42123482f8aSMax Reitz$QEMU_IMG resize --shrink "$TEST_IMG" 32M
42223482f8aSMax Reitz_img_info | grep 'virtual size'
42323482f8aSMax Reitz
4244efb1f7cSMax Reitzecho
4254efb1f7cSMax Reitzecho "=== Discarding a refblock covered by an unaligned refblock ==="
4264efb1f7cSMax Reitzecho
4274efb1f7cSMax Reitz
428407fb56aSMax Reitz_make_test_img -o 'refcount_bits=1' 64M
4294efb1f7cSMax Reitz
4304efb1f7cSMax Reitz# Same as above
4314efb1f7cSMax Reitzpoke_file "$TEST_IMG" "$(($rt_offset+8))" "\x00\x00\x00\x10\x00\x00\x00\x00"
4324efb1f7cSMax Reitz# But now we actually "create" an unaligned third refblock
4334efb1f7cSMax Reitzpoke_file "$TEST_IMG" "$(($rt_offset+16))" "\x00\x00\x00\x00\x00\x00\x02\x00"
4344efb1f7cSMax Reitz$QEMU_IMG resize --shrink "$TEST_IMG" 32M
4354efb1f7cSMax Reitz
4364efb1f7cSMax Reitzecho '--- Repairing ---'
4374efb1f7cSMax Reitz# Fails the first repair because the corruption prevents the check
4384efb1f7cSMax Reitz# function from double-checking
4394efb1f7cSMax Reitz# (Using -q for the first invocation, because otherwise the
4404efb1f7cSMax Reitz#  double-check error message appears above the summary for some
4414efb1f7cSMax Reitz#  reason -- so let's just hide the summary)
4424efb1f7cSMax Reitz_check_test_img -q -r all
4434efb1f7cSMax Reitz_check_test_img -r all
4444efb1f7cSMax Reitz
44550a3efb0SAlberto Garciaecho
44650a3efb0SAlberto Garciaecho "=== Testing the QEMU shutdown with a corrupted image ==="
44750a3efb0SAlberto Garciaecho
44850a3efb0SAlberto Garcia_make_test_img 64M
44950a3efb0SAlberto Garciapoke_file "$TEST_IMG" "$rt_offset"        "\x00\x00\x00\x00\x00\x00\x00\x00"
45050a3efb0SAlberto Garciaecho "{'execute': 'qmp_capabilities'}
45150a3efb0SAlberto Garcia      {'execute': 'human-monitor-command',
45250a3efb0SAlberto Garcia       'arguments': {'command-line': 'qemu-io drive \"write 0 512\"'}}
45350a3efb0SAlberto Garcia      {'execute': 'quit'}" \
45450a3efb0SAlberto Garcia    | $QEMU -qmp stdio -nographic -nodefaults \
45550a3efb0SAlberto Garcia            -drive if=none,node-name=drive,file="$TEST_IMG",driver=qcow2 \
45650a3efb0SAlberto Garcia    | _filter_qmp | _filter_qemu_io
45750a3efb0SAlberto Garcia
458c50abd17SMax Reitzecho
459c50abd17SMax Reitzecho "=== Testing incoming inactive corrupted image ==="
460c50abd17SMax Reitzecho
461c50abd17SMax Reitz
462c50abd17SMax Reitz_make_test_img 64M
463c50abd17SMax Reitz# Create an unaligned L1 entry, so qemu will signal a corruption when
464c50abd17SMax Reitz# reading from the covered area
465c50abd17SMax Reitzpoke_file "$TEST_IMG" "$l1_offset" "\x00\x00\x00\x00\x2a\x2a\x2a\x2a"
466c50abd17SMax Reitz
467c50abd17SMax Reitz# Inactive images are effectively read-only images, so this should be a
468c50abd17SMax Reitz# non-fatal corruption (which does not modify the image)
469c50abd17SMax Reitzecho "{'execute': 'qmp_capabilities'}
470c50abd17SMax Reitz      {'execute': 'human-monitor-command',
471c50abd17SMax Reitz       'arguments': {'command-line': 'qemu-io drive \"read 0 512\"'}}
472c50abd17SMax Reitz      {'execute': 'quit'}" \
473c50abd17SMax Reitz    | $QEMU -qmp stdio -nographic -nodefaults \
474c50abd17SMax Reitz            -blockdev "{'node-name': 'drive',
475c50abd17SMax Reitz                        'driver': 'qcow2',
476c50abd17SMax Reitz                        'file': {
477c50abd17SMax Reitz                            'driver': 'file',
478c50abd17SMax Reitz                            'filename': '$TEST_IMG'
479c50abd17SMax Reitz                        }}" \
480c50abd17SMax Reitz            -incoming exec:'cat /dev/null' \
481c50abd17SMax Reitz            2>&1 \
482cbc4ae2dSPeter Xu    | _filter_qmp | _filter_qemu_io | _filter_io_error
483c50abd17SMax Reitz
484c50abd17SMax Reitzecho
485c50abd17SMax Reitz# Image should not have been marked corrupt
486c50abd17SMax Reitz_img_info --format-specific | grep 'corrupt:'
487c50abd17SMax Reitz
488ca0eca91SMax Reitz# success, all done
489ca0eca91SMax Reitzecho "*** done"
490ca0eca91SMax Reitzrm -f $seq.full
491ca0eca91SMax Reitzstatus=0
492