xref: /openbmc/qemu/tests/qemu-iotests/060 (revision 98839750)
1ca0eca91SMax Reitz#!/bin/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
2734eeb82dSMax Reitzhere="$PWD"
28ca0eca91SMax Reitzstatus=1	# failure is the default!
29ca0eca91SMax Reitz
30ca0eca91SMax Reitz_cleanup()
31ca0eca91SMax Reitz{
32ca0eca91SMax Reitz	_cleanup_test_img
33ca0eca91SMax Reitz}
34ca0eca91SMax Reitztrap "_cleanup; exit \$status" 0 1 2 3 15
35ca0eca91SMax Reitz
36ca0eca91SMax Reitz# get standard environment, filters and checks
37ca0eca91SMax Reitz. ./common.rc
38ca0eca91SMax Reitz. ./common.filter
39ca0eca91SMax Reitz
40ca0eca91SMax Reitz# This tests qocw2-specific low-level functionality
41ca0eca91SMax Reitz_supported_fmt qcow2
421f7bf7d0SPeter Lieven_supported_proto file
43ca0eca91SMax Reitz_supported_os Linux
44ca0eca91SMax Reitz
45ca0eca91SMax Reitzrt_offset=65536  # 0x10000 (XXX: just an assumption)
46ca0eca91SMax Reitzrb_offset=131072 # 0x20000 (XXX: just an assumption)
47ca0eca91SMax Reitzl1_offset=196608 # 0x30000 (XXX: just an assumption)
48ca0eca91SMax Reitzl2_offset=262144 # 0x40000 (XXX: just an assumption)
4934eeb82dSMax Reitzl2_offset_after_snapshot=524288 # 0x80000 (XXX: just an assumption)
50ca0eca91SMax Reitz
51ca0eca91SMax ReitzIMGOPTS="compat=1.1"
52ca0eca91SMax Reitz
5334eeb82dSMax ReitzOPEN_RW="open -o overlap-check=all $TEST_IMG"
5434eeb82dSMax Reitz# Overlap checks are done before write operations only, therefore opening an
5534eeb82dSMax Reitz# image read-only makes the overlap-check option irrelevant
5634eeb82dSMax ReitzOPEN_RO="open -r $TEST_IMG"
5734eeb82dSMax Reitz
58ca0eca91SMax Reitzecho
59ca0eca91SMax Reitzecho "=== Testing L2 reference into L1 ==="
60ca0eca91SMax Reitzecho
61ca0eca91SMax Reitz_make_test_img 64M
62ca0eca91SMax Reitz# Link first L1 entry (first L2 table) onto itself
63ca0eca91SMax Reitz# (Note the MSb in the L1 entry is set, ensuring the refcount is one - else any
64ca0eca91SMax Reitz# later write will result in a COW operation, effectively ruining this attempt
65ca0eca91SMax Reitz# on image corruption)
66ca0eca91SMax Reitzpoke_file "$TEST_IMG" "$l1_offset" "\x80\x00\x00\x00\x00\x03\x00\x00"
67ca0eca91SMax Reitz_check_test_img
68ca0eca91SMax Reitz
69ca0eca91SMax Reitz# The corrupt bit should not be set anyway
70ea81ca9dSMax Reitz$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
71ca0eca91SMax Reitz
72ca0eca91SMax Reitz# Try to write something, thereby forcing the corrupt bit to be set
7334eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RW" -c "write -P 0x2a 0 512" | _filter_qemu_io
74ca0eca91SMax Reitz
75ca0eca91SMax Reitz# The corrupt bit must now be set
76ea81ca9dSMax Reitz$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
77ca0eca91SMax Reitz
78f383611aSMax Reitz# This information should be available through qemu-img info
79e800e5d4SKevin Wolf_img_info --format-specific
80f383611aSMax Reitz
81ca0eca91SMax Reitz# Try to open the image R/W (which should fail)
8234eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RW" -c "read 0 512" 2>&1 | _filter_qemu_io \
8334eeb82dSMax Reitz                                            | _filter_testdir \
8434eeb82dSMax Reitz                                            | _filter_imgfmt
85ca0eca91SMax Reitz
86ca0eca91SMax Reitz# Try to open it RO (which should succeed)
8734eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RO" -c "read 0 512" | _filter_qemu_io
88ca0eca91SMax Reitz
89ca0eca91SMax Reitz# We could now try to fix the image, but this would probably fail (how should an
90ca0eca91SMax Reitz# L2 table linked onto the L1 table be fixed?)
91ca0eca91SMax Reitz
92ca0eca91SMax Reitzecho
93ca0eca91SMax Reitzecho "=== Testing cluster data reference into refcount block ==="
94ca0eca91SMax Reitzecho
95ca0eca91SMax Reitz_make_test_img 64M
96ca0eca91SMax Reitz# Allocate L2 table
97ca0eca91SMax Reitztruncate -s "$(($l2_offset+65536))" "$TEST_IMG"
98ca0eca91SMax Reitzpoke_file "$TEST_IMG" "$l1_offset" "\x80\x00\x00\x00\x00\x04\x00\x00"
99ca0eca91SMax Reitz# Mark cluster as used
100ca0eca91SMax Reitzpoke_file "$TEST_IMG" "$(($rb_offset+8))" "\x00\x01"
101ca0eca91SMax Reitz# Redirect new data cluster onto refcount block
102ca0eca91SMax Reitzpoke_file "$TEST_IMG" "$l2_offset" "\x80\x00\x00\x00\x00\x02\x00\x00"
103ca0eca91SMax Reitz_check_test_img
104ea81ca9dSMax Reitz$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
10534eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RW" -c "write -P 0x2a 0 512" | _filter_qemu_io
106ea81ca9dSMax Reitz$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
107ca0eca91SMax Reitz
108ca0eca91SMax Reitz# Try to fix it
109ca0eca91SMax Reitz_check_test_img -r all
110ca0eca91SMax Reitz
111ca0eca91SMax Reitz# The corrupt bit should be cleared
112ea81ca9dSMax Reitz$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
113ca0eca91SMax Reitz
114ca0eca91SMax Reitz# Look if it's really really fixed
11534eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RW" -c "write -P 0x2a 0 512" | _filter_qemu_io
116ea81ca9dSMax Reitz$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
117ca0eca91SMax Reitz
11834eeb82dSMax Reitzecho
11934eeb82dSMax Reitzecho "=== Testing cluster data reference into inactive L2 table ==="
12034eeb82dSMax Reitzecho
12134eeb82dSMax Reitz_make_test_img 64M
12234eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RW" -c "write -P 1 0 512" | _filter_qemu_io
12334eeb82dSMax Reitz$QEMU_IMG snapshot -c foo "$TEST_IMG"
12434eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RW" -c "write -P 2 0 512" | _filter_qemu_io
12534eeb82dSMax Reitz# The inactive L2 table remains at its old offset
12634eeb82dSMax Reitzpoke_file "$TEST_IMG" "$l2_offset_after_snapshot" \
12734eeb82dSMax Reitz                      "\x80\x00\x00\x00\x00\x04\x00\x00"
12834eeb82dSMax Reitz_check_test_img
129ea81ca9dSMax Reitz$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
13034eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RW" -c "write -P 3 0 512" | _filter_qemu_io
131ea81ca9dSMax Reitz$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
13234eeb82dSMax Reitz_check_test_img -r all
133ea81ca9dSMax Reitz$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
13434eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RW" -c "write -P 4 0 512" | _filter_qemu_io
135ea81ca9dSMax Reitz$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
13634eeb82dSMax Reitz
13734eeb82dSMax Reitz# Check data
13834eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RO" -c "read -P 4 0 512" | _filter_qemu_io
13934eeb82dSMax Reitz$QEMU_IMG snapshot -a foo "$TEST_IMG"
14034eeb82dSMax Reitz_check_test_img
14134eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RO" -c "read -P 1 0 512" | _filter_qemu_io
14234eeb82dSMax Reitz
14398d39e34SMax Reitzecho
14498d39e34SMax Reitzecho "=== Testing overlap while COW is in flight ==="
14598d39e34SMax Reitzecho
14698d39e34SMax Reitz# compat=0.10 is required in order to make the following discard actually
14798d39e34SMax Reitz# unallocate the sector rather than make it a zero sector - we want COW, after
14898d39e34SMax Reitz# all.
14998d39e34SMax ReitzIMGOPTS='compat=0.10' _make_test_img 1G
15098d39e34SMax Reitz# Write two clusters, the second one enforces creation of an L2 table after
15198d39e34SMax Reitz# the first data cluster.
15298d39e34SMax Reitz$QEMU_IO -c 'write 0k 64k' -c 'write 512M 64k' "$TEST_IMG" | _filter_qemu_io
15398d39e34SMax Reitz# Discard the first cluster. This cluster will soon enough be reallocated and
15498d39e34SMax Reitz# used for COW.
15598d39e34SMax Reitz$QEMU_IO -c 'discard 0k 64k' "$TEST_IMG" | _filter_qemu_io
15698d39e34SMax Reitz# Now, corrupt the image by marking the second L2 table cluster as free.
15798d39e34SMax Reitzpoke_file "$TEST_IMG" '131084' "\x00\x00" # 0x2000c
15898d39e34SMax Reitz# Start a write operation requiring COW on the image stopping it right before
15998d39e34SMax Reitz# doing the read; then, trigger the corruption prevention by writing anything to
16098d39e34SMax Reitz# any unallocated cluster, leading to an attempt to overwrite the second L2
16198d39e34SMax Reitz# table. Finally, resume the COW write and see it fail (but not crash).
16298d39e34SMax Reitzecho "open -o file.driver=blkdebug $TEST_IMG
16398d39e34SMax Reitzbreak cow_read 0
16498d39e34SMax Reitzaio_write 0k 1k
16598d39e34SMax Reitzwait_break 0
16698d39e34SMax Reitzwrite 64k 64k
16798d39e34SMax Reitzresume 0" | $QEMU_IO | _filter_qemu_io
16898d39e34SMax Reitz
169a42f8a3dSMax Reitzecho
170a42f8a3dSMax Reitzecho "=== Testing unallocated image header ==="
171a42f8a3dSMax Reitzecho
172a42f8a3dSMax Reitz_make_test_img 64M
173a42f8a3dSMax Reitz# Create L1/L2
1745b0ed2beSMax Reitz$QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io
175a42f8a3dSMax Reitzpoke_file "$TEST_IMG" "$rb_offset" "\x00\x00"
1765b0ed2beSMax Reitz$QEMU_IO -c "write 64k 64k" "$TEST_IMG" | _filter_qemu_io
1775b0ed2beSMax Reitz
1785b0ed2beSMax Reitzecho
1795b0ed2beSMax Reitzecho "=== Testing unaligned L1 entry ==="
1805b0ed2beSMax Reitzecho
1815b0ed2beSMax Reitz_make_test_img 64M
1825b0ed2beSMax Reitz$QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io
1835b0ed2beSMax Reitz# This will be masked with ~(512 - 1) = ~0x1ff, so whether the lower 9 bits are
1845b0ed2beSMax Reitz# aligned or not does not matter
1855b0ed2beSMax Reitzpoke_file "$TEST_IMG" "$l1_offset" "\x80\x00\x00\x00\x00\x04\x2a\x00"
1865b0ed2beSMax Reitz$QEMU_IO -c "read 0 64k" "$TEST_IMG" | _filter_qemu_io
1875b0ed2beSMax Reitz
188f30136b3SMax Reitz# Test how well zero cluster expansion can cope with this
189f30136b3SMax Reitz_make_test_img 64M
190f30136b3SMax Reitz$QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io
191f30136b3SMax Reitzpoke_file "$TEST_IMG" "$l1_offset" "\x80\x00\x00\x00\x00\x04\x2a\x00"
192f30136b3SMax Reitz$QEMU_IMG amend -o compat=0.10 "$TEST_IMG"
193f30136b3SMax Reitz
1945b0ed2beSMax Reitzecho
1955b0ed2beSMax Reitzecho "=== Testing unaligned L2 entry ==="
1965b0ed2beSMax Reitzecho
1975b0ed2beSMax Reitz_make_test_img 64M
1985b0ed2beSMax Reitz$QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io
1995b0ed2beSMax Reitzpoke_file "$TEST_IMG" "$l2_offset" "\x80\x00\x00\x00\x00\x05\x2a\x00"
2005b0ed2beSMax Reitz$QEMU_IO -c "read 0 64k" "$TEST_IMG" | _filter_qemu_io
2015b0ed2beSMax Reitz
2025b0ed2beSMax Reitzecho
203f30136b3SMax Reitzecho "=== Testing unaligned pre-allocated zero cluster ==="
204f30136b3SMax Reitzecho
205f30136b3SMax Reitz_make_test_img 64M
206f30136b3SMax Reitz$QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io
207f30136b3SMax Reitzpoke_file "$TEST_IMG" "$l2_offset" "\x80\x00\x00\x00\x00\x05\x2a\x01"
208f30136b3SMax Reitz# zero cluster expansion
209f30136b3SMax Reitz$QEMU_IMG amend -o compat=0.10 "$TEST_IMG"
210f30136b3SMax Reitz
211f30136b3SMax Reitzecho
2125b0ed2beSMax Reitzecho "=== Testing unaligned reftable entry ==="
2135b0ed2beSMax Reitzecho
2145b0ed2beSMax Reitz_make_test_img 64M
2155b0ed2beSMax Reitzpoke_file "$TEST_IMG" "$rt_offset" "\x00\x00\x00\x00\x00\x02\x2a\x00"
2165b0ed2beSMax Reitz$QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io
2175b0ed2beSMax Reitz
2185b0ed2beSMax Reitzecho
2195b0ed2beSMax Reitzecho "=== Testing non-fatal corruption on freeing ==="
2205b0ed2beSMax Reitzecho
2215b0ed2beSMax Reitz_make_test_img 64M
2225b0ed2beSMax Reitz$QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io
2235b0ed2beSMax Reitzpoke_file "$TEST_IMG" "$l2_offset" "\x80\x00\x00\x00\x00\x05\x2a\x00"
2245b0ed2beSMax Reitz$QEMU_IO -c "discard 0 64k" "$TEST_IMG" | _filter_qemu_io
2255b0ed2beSMax Reitz
2265b0ed2beSMax Reitzecho
2275b0ed2beSMax Reitzecho "=== Testing read-only corruption report ==="
2285b0ed2beSMax Reitzecho
2295b0ed2beSMax Reitz_make_test_img 64M
2305b0ed2beSMax Reitz$QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io
2315b0ed2beSMax Reitzpoke_file "$TEST_IMG" "$l2_offset" "\x80\x00\x00\x00\x00\x05\x2a\x00"
2325b0ed2beSMax Reitz# Should only emit a single error message
2335b0ed2beSMax Reitz$QEMU_IO -c "$OPEN_RO" -c "read 0 64k" -c "read 0 64k" | _filter_qemu_io
2345b0ed2beSMax Reitz
2355b0ed2beSMax Reitzecho
2365b0ed2beSMax Reitzecho "=== Testing non-fatal and then fatal corruption report ==="
2375b0ed2beSMax Reitzecho
2385b0ed2beSMax Reitz_make_test_img 64M
2395b0ed2beSMax Reitz$QEMU_IO -c "write 0 128k" "$TEST_IMG" | _filter_qemu_io
2405b0ed2beSMax Reitzpoke_file "$TEST_IMG" "$l2_offset"        "\x80\x00\x00\x00\x00\x05\x2a\x00"
2415b0ed2beSMax Reitzpoke_file "$TEST_IMG" "$(($l2_offset+8))" "\x80\x00\x00\x00\x00\x06\x2a\x00"
2425b0ed2beSMax Reitz# Should emit two error messages
2435b0ed2beSMax Reitz$QEMU_IO -c "discard 0 64k" -c "read 64k 64k" "$TEST_IMG" | _filter_qemu_io
244a42f8a3dSMax Reitz
2456bf45d59SAlberto Garciaecho
2466bf45d59SAlberto Garciaecho "=== Testing empty refcount table with valid L1 and L2 tables ==="
2476bf45d59SAlberto Garciaecho
2486bf45d59SAlberto Garcia_make_test_img 64M
2496bf45d59SAlberto Garcia$QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io
2506bf45d59SAlberto Garciapoke_file "$TEST_IMG" "$rt_offset"        "\x00\x00\x00\x00\x00\x00\x00\x00"
2516bf45d59SAlberto Garcia# Since the first data cluster is already allocated this triggers an
2526bf45d59SAlberto Garcia# allocation with an explicit offset (using qcow2_alloc_clusters_at())
2536bf45d59SAlberto Garcia# causing a refcount block to be allocated at offset 0
2546bf45d59SAlberto Garcia$QEMU_IO -c "write 0 128k" "$TEST_IMG" | _filter_qemu_io
2556bf45d59SAlberto Garcia
256*98839750SAlberto Garciaecho
257*98839750SAlberto Garciaecho "=== Testing empty refcount block ==="
258*98839750SAlberto Garciaecho
259*98839750SAlberto Garcia_make_test_img 64M
260*98839750SAlberto Garciapoke_file "$TEST_IMG" "$rb_offset"        "\x00\x00\x00\x00\x00\x00\x00\x00"
261*98839750SAlberto Garcia$QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io
262*98839750SAlberto Garcia
263ca0eca91SMax Reitz# success, all done
264ca0eca91SMax Reitzecho "*** done"
265ca0eca91SMax Reitzrm -f $seq.full
266ca0eca91SMax Reitzstatus=0
267