xref: /openbmc/qemu/tests/qemu-iotests/060 (revision 35d08458)
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
22owner=mreitz@redhat.com
23
24seq="$(basename $0)"
25echo "QA output created by $seq"
26
27here="$PWD"
28tmp=/tmp/$$
29status=1	# failure is the default!
30
31_cleanup()
32{
33	_cleanup_test_img
34}
35trap "_cleanup; exit \$status" 0 1 2 3 15
36
37# get standard environment, filters and checks
38. ./common.env
39. ./common.rc
40. ./common.filter
41
42# This tests qocw2-specific low-level functionality
43_supported_fmt qcow2
44_supported_proto file
45_supported_os Linux
46
47rt_offset=65536  # 0x10000 (XXX: just an assumption)
48rb_offset=131072 # 0x20000 (XXX: just an assumption)
49l1_offset=196608 # 0x30000 (XXX: just an assumption)
50l2_offset=262144 # 0x40000 (XXX: just an assumption)
51l2_offset_after_snapshot=524288 # 0x80000 (XXX: just an assumption)
52
53IMGOPTS="compat=1.1"
54
55OPEN_RW="open -o overlap-check=all $TEST_IMG"
56# Overlap checks are done before write operations only, therefore opening an
57# image read-only makes the overlap-check option irrelevant
58OPEN_RO="open -r $TEST_IMG"
59
60echo
61echo "=== Testing L2 reference into L1 ==="
62echo
63_make_test_img 64M
64# Link first L1 entry (first L2 table) onto itself
65# (Note the MSb in the L1 entry is set, ensuring the refcount is one - else any
66# later write will result in a COW operation, effectively ruining this attempt
67# on image corruption)
68poke_file "$TEST_IMG" "$l1_offset" "\x80\x00\x00\x00\x00\x03\x00\x00"
69_check_test_img
70
71# The corrupt bit should not be set anyway
72$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
73
74# Try to write something, thereby forcing the corrupt bit to be set
75$QEMU_IO -c "$OPEN_RW" -c "write -P 0x2a 0 512" | _filter_qemu_io
76
77# The corrupt bit must now be set
78$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
79
80# Try to open the image R/W (which should fail)
81$QEMU_IO -c "$OPEN_RW" -c "read 0 512" 2>&1 | _filter_qemu_io \
82                                            | _filter_testdir \
83                                            | _filter_imgfmt
84
85# Try to open it RO (which should succeed)
86$QEMU_IO -c "$OPEN_RO" -c "read 0 512" | _filter_qemu_io
87
88# We could now try to fix the image, but this would probably fail (how should an
89# L2 table linked onto the L1 table be fixed?)
90
91echo
92echo "=== Testing cluster data reference into refcount block ==="
93echo
94_make_test_img 64M
95# Allocate L2 table
96truncate -s "$(($l2_offset+65536))" "$TEST_IMG"
97poke_file "$TEST_IMG" "$l1_offset" "\x80\x00\x00\x00\x00\x04\x00\x00"
98# Mark cluster as used
99poke_file "$TEST_IMG" "$(($rb_offset+8))" "\x00\x01"
100# Redirect new data cluster onto refcount block
101poke_file "$TEST_IMG" "$l2_offset" "\x80\x00\x00\x00\x00\x02\x00\x00"
102_check_test_img
103$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
104$QEMU_IO -c "$OPEN_RW" -c "write -P 0x2a 0 512" | _filter_qemu_io
105$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
106
107# Try to fix it
108_check_test_img -r all
109
110# The corrupt bit should be cleared
111$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
112
113# Look if it's really really fixed
114$QEMU_IO -c "$OPEN_RW" -c "write -P 0x2a 0 512" | _filter_qemu_io
115$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
116
117echo
118echo "=== Testing cluster data reference into inactive L2 table ==="
119echo
120_make_test_img 64M
121$QEMU_IO -c "$OPEN_RW" -c "write -P 1 0 512" | _filter_qemu_io
122$QEMU_IMG snapshot -c foo "$TEST_IMG"
123$QEMU_IO -c "$OPEN_RW" -c "write -P 2 0 512" | _filter_qemu_io
124# The inactive L2 table remains at its old offset
125poke_file "$TEST_IMG" "$l2_offset_after_snapshot" \
126                      "\x80\x00\x00\x00\x00\x04\x00\x00"
127_check_test_img
128$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
129$QEMU_IO -c "$OPEN_RW" -c "write -P 3 0 512" | _filter_qemu_io
130$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
131_check_test_img -r all
132$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
133$QEMU_IO -c "$OPEN_RW" -c "write -P 4 0 512" | _filter_qemu_io
134$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
135
136# Check data
137$QEMU_IO -c "$OPEN_RO" -c "read -P 4 0 512" | _filter_qemu_io
138$QEMU_IMG snapshot -a foo "$TEST_IMG"
139_check_test_img
140$QEMU_IO -c "$OPEN_RO" -c "read -P 1 0 512" | _filter_qemu_io
141
142echo
143echo "=== Testing overlap while COW is in flight ==="
144echo
145# compat=0.10 is required in order to make the following discard actually
146# unallocate the sector rather than make it a zero sector - we want COW, after
147# all.
148IMGOPTS='compat=0.10' _make_test_img 1G
149# Write two clusters, the second one enforces creation of an L2 table after
150# the first data cluster.
151$QEMU_IO -c 'write 0k 64k' -c 'write 512M 64k' "$TEST_IMG" | _filter_qemu_io
152# Discard the first cluster. This cluster will soon enough be reallocated and
153# used for COW.
154$QEMU_IO -c 'discard 0k 64k' "$TEST_IMG" | _filter_qemu_io
155# Now, corrupt the image by marking the second L2 table cluster as free.
156poke_file "$TEST_IMG" '131084' "\x00\x00" # 0x2000c
157# Start a write operation requiring COW on the image stopping it right before
158# doing the read; then, trigger the corruption prevention by writing anything to
159# any unallocated cluster, leading to an attempt to overwrite the second L2
160# table. Finally, resume the COW write and see it fail (but not crash).
161echo "open -o file.driver=blkdebug $TEST_IMG
162break cow_read 0
163aio_write 0k 1k
164wait_break 0
165write 64k 64k
166resume 0" | $QEMU_IO | _filter_qemu_io
167
168# success, all done
169echo "*** done"
170rm -f $seq.full
171status=0
172