xref: /openbmc/qemu/tests/qemu-iotests/060 (revision 5accc840)
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.rc
39. ./common.filter
40
41# This tests qocw2-specific low-level functionality
42_supported_fmt qcow2
43_supported_proto generic
44_supported_os Linux
45
46rt_offset=65536  # 0x10000 (XXX: just an assumption)
47rb_offset=131072 # 0x20000 (XXX: just an assumption)
48l1_offset=196608 # 0x30000 (XXX: just an assumption)
49l2_offset=262144 # 0x40000 (XXX: just an assumption)
50
51IMGOPTS="compat=1.1"
52
53echo
54echo "=== Testing L2 reference into L1 ==="
55echo
56_make_test_img 64M
57# Link first L1 entry (first L2 table) onto itself
58# (Note the MSb in the L1 entry is set, ensuring the refcount is one - else any
59# later write will result in a COW operation, effectively ruining this attempt
60# on image corruption)
61poke_file "$TEST_IMG" "$l1_offset" "\x80\x00\x00\x00\x00\x03\x00\x00"
62_check_test_img
63
64# The corrupt bit should not be set anyway
65./qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
66
67# Try to write something, thereby forcing the corrupt bit to be set
68$QEMU_IO -c "write -P 0x2a 0 512" "$TEST_IMG" | _filter_qemu_io
69
70# The corrupt bit must now be set
71./qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
72
73# Try to open the image R/W (which should fail)
74$QEMU_IO -c "read 0 512" "$TEST_IMG" 2>&1 | _filter_qemu_io | sed -e "s/can't open device .*$/can't open device/"
75
76# Try to open it RO (which should succeed)
77$QEMU_IO -c "read 0 512" -r "$TEST_IMG" | _filter_qemu_io
78
79# We could now try to fix the image, but this would probably fail (how should an
80# L2 table linked onto the L1 table be fixed?)
81
82echo
83echo "=== Testing cluster data reference into refcount block ==="
84echo
85_make_test_img 64M
86# Allocate L2 table
87truncate -s "$(($l2_offset+65536))" "$TEST_IMG"
88poke_file "$TEST_IMG" "$l1_offset" "\x80\x00\x00\x00\x00\x04\x00\x00"
89# Mark cluster as used
90poke_file "$TEST_IMG" "$(($rb_offset+8))" "\x00\x01"
91# Redirect new data cluster onto refcount block
92poke_file "$TEST_IMG" "$l2_offset" "\x80\x00\x00\x00\x00\x02\x00\x00"
93_check_test_img
94./qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
95$QEMU_IO -c "write -P 0x2a 0 512" "$TEST_IMG" | _filter_qemu_io
96./qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
97
98# Try to fix it
99_check_test_img -r all
100
101# The corrupt bit should be cleared
102./qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
103
104# Look if it's really really fixed
105$QEMU_IO -c "write -P 0x2a 0 512" "$TEST_IMG" | _filter_qemu_io
106./qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
107
108# success, all done
109echo "*** done"
110rm -f $seq.full
111status=0
112