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 Reitztmp=/tmp/$$ 29ca0eca91SMax Reitzstatus=1 # failure is the default! 30ca0eca91SMax Reitz 31ca0eca91SMax Reitz_cleanup() 32ca0eca91SMax Reitz{ 33ca0eca91SMax Reitz _cleanup_test_img 34ca0eca91SMax Reitz} 35ca0eca91SMax Reitztrap "_cleanup; exit \$status" 0 1 2 3 15 36ca0eca91SMax Reitz 37ca0eca91SMax Reitz# get standard environment, filters and checks 38ca0eca91SMax Reitz. ./common.rc 39ca0eca91SMax Reitz. ./common.filter 40ca0eca91SMax Reitz 41ca0eca91SMax Reitz# This tests qocw2-specific low-level functionality 42ca0eca91SMax Reitz_supported_fmt qcow2 431f7bf7d0SPeter Lieven_supported_proto file 44ca0eca91SMax Reitz_supported_os Linux 45ca0eca91SMax Reitz 46ca0eca91SMax Reitzrt_offset=65536 # 0x10000 (XXX: just an assumption) 47ca0eca91SMax Reitzrb_offset=131072 # 0x20000 (XXX: just an assumption) 48ca0eca91SMax Reitzl1_offset=196608 # 0x30000 (XXX: just an assumption) 49ca0eca91SMax Reitzl2_offset=262144 # 0x40000 (XXX: just an assumption) 5034eeb82dSMax Reitzl2_offset_after_snapshot=524288 # 0x80000 (XXX: just an assumption) 51ca0eca91SMax Reitz 52ca0eca91SMax ReitzIMGOPTS="compat=1.1" 53ca0eca91SMax Reitz 5434eeb82dSMax ReitzOPEN_RW="open -o overlap-check=all $TEST_IMG" 5534eeb82dSMax Reitz# Overlap checks are done before write operations only, therefore opening an 5634eeb82dSMax Reitz# image read-only makes the overlap-check option irrelevant 5734eeb82dSMax ReitzOPEN_RO="open -r $TEST_IMG" 5834eeb82dSMax Reitz 59ca0eca91SMax Reitzecho 60ca0eca91SMax Reitzecho "=== Testing L2 reference into L1 ===" 61ca0eca91SMax Reitzecho 62ca0eca91SMax Reitz_make_test_img 64M 63ca0eca91SMax Reitz# Link first L1 entry (first L2 table) onto itself 64ca0eca91SMax Reitz# (Note the MSb in the L1 entry is set, ensuring the refcount is one - else any 65ca0eca91SMax Reitz# later write will result in a COW operation, effectively ruining this attempt 66ca0eca91SMax Reitz# on image corruption) 67ca0eca91SMax Reitzpoke_file "$TEST_IMG" "$l1_offset" "\x80\x00\x00\x00\x00\x03\x00\x00" 68ca0eca91SMax Reitz_check_test_img 69ca0eca91SMax Reitz 70ca0eca91SMax Reitz# The corrupt bit should not be set anyway 71ea81ca9dSMax Reitz$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features 72ca0eca91SMax Reitz 73ca0eca91SMax Reitz# Try to write something, thereby forcing the corrupt bit to be set 7434eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RW" -c "write -P 0x2a 0 512" | _filter_qemu_io 75ca0eca91SMax Reitz 76ca0eca91SMax Reitz# The corrupt bit must now be set 77ea81ca9dSMax Reitz$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features 78ca0eca91SMax Reitz 79ca0eca91SMax Reitz# Try to open the image R/W (which should fail) 8034eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RW" -c "read 0 512" 2>&1 | _filter_qemu_io \ 8134eeb82dSMax Reitz | _filter_testdir \ 8234eeb82dSMax Reitz | _filter_imgfmt 83ca0eca91SMax Reitz 84ca0eca91SMax Reitz# Try to open it RO (which should succeed) 8534eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RO" -c "read 0 512" | _filter_qemu_io 86ca0eca91SMax Reitz 87ca0eca91SMax Reitz# We could now try to fix the image, but this would probably fail (how should an 88ca0eca91SMax Reitz# L2 table linked onto the L1 table be fixed?) 89ca0eca91SMax Reitz 90ca0eca91SMax Reitzecho 91ca0eca91SMax Reitzecho "=== Testing cluster data reference into refcount block ===" 92ca0eca91SMax Reitzecho 93ca0eca91SMax Reitz_make_test_img 64M 94ca0eca91SMax Reitz# Allocate L2 table 95ca0eca91SMax Reitztruncate -s "$(($l2_offset+65536))" "$TEST_IMG" 96ca0eca91SMax Reitzpoke_file "$TEST_IMG" "$l1_offset" "\x80\x00\x00\x00\x00\x04\x00\x00" 97ca0eca91SMax Reitz# Mark cluster as used 98ca0eca91SMax Reitzpoke_file "$TEST_IMG" "$(($rb_offset+8))" "\x00\x01" 99ca0eca91SMax Reitz# Redirect new data cluster onto refcount block 100ca0eca91SMax Reitzpoke_file "$TEST_IMG" "$l2_offset" "\x80\x00\x00\x00\x00\x02\x00\x00" 101ca0eca91SMax Reitz_check_test_img 102ea81ca9dSMax Reitz$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features 10334eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RW" -c "write -P 0x2a 0 512" | _filter_qemu_io 104ea81ca9dSMax Reitz$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features 105ca0eca91SMax Reitz 106ca0eca91SMax Reitz# Try to fix it 107ca0eca91SMax Reitz_check_test_img -r all 108ca0eca91SMax Reitz 109ca0eca91SMax Reitz# The corrupt bit should be cleared 110ea81ca9dSMax Reitz$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features 111ca0eca91SMax Reitz 112ca0eca91SMax Reitz# Look if it's really really fixed 11334eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RW" -c "write -P 0x2a 0 512" | _filter_qemu_io 114ea81ca9dSMax Reitz$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features 115ca0eca91SMax Reitz 11634eeb82dSMax Reitzecho 11734eeb82dSMax Reitzecho "=== Testing cluster data reference into inactive L2 table ===" 11834eeb82dSMax Reitzecho 11934eeb82dSMax Reitz_make_test_img 64M 12034eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RW" -c "write -P 1 0 512" | _filter_qemu_io 12134eeb82dSMax Reitz$QEMU_IMG snapshot -c foo "$TEST_IMG" 12234eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RW" -c "write -P 2 0 512" | _filter_qemu_io 12334eeb82dSMax Reitz# The inactive L2 table remains at its old offset 12434eeb82dSMax Reitzpoke_file "$TEST_IMG" "$l2_offset_after_snapshot" \ 12534eeb82dSMax Reitz "\x80\x00\x00\x00\x00\x04\x00\x00" 12634eeb82dSMax Reitz_check_test_img 127ea81ca9dSMax Reitz$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features 12834eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RW" -c "write -P 3 0 512" | _filter_qemu_io 129ea81ca9dSMax Reitz$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features 13034eeb82dSMax Reitz_check_test_img -r all 131ea81ca9dSMax Reitz$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features 13234eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RW" -c "write -P 4 0 512" | _filter_qemu_io 133ea81ca9dSMax Reitz$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features 13434eeb82dSMax Reitz 13534eeb82dSMax Reitz# Check data 13634eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RO" -c "read -P 4 0 512" | _filter_qemu_io 13734eeb82dSMax Reitz$QEMU_IMG snapshot -a foo "$TEST_IMG" 13834eeb82dSMax Reitz_check_test_img 13934eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RO" -c "read -P 1 0 512" | _filter_qemu_io 14034eeb82dSMax Reitz 14198d39e34SMax Reitzecho 14298d39e34SMax Reitzecho "=== Testing overlap while COW is in flight ===" 14398d39e34SMax Reitzecho 14498d39e34SMax Reitz# compat=0.10 is required in order to make the following discard actually 14598d39e34SMax Reitz# unallocate the sector rather than make it a zero sector - we want COW, after 14698d39e34SMax Reitz# all. 14798d39e34SMax ReitzIMGOPTS='compat=0.10' _make_test_img 1G 14898d39e34SMax Reitz# Write two clusters, the second one enforces creation of an L2 table after 14998d39e34SMax Reitz# the first data cluster. 15098d39e34SMax Reitz$QEMU_IO -c 'write 0k 64k' -c 'write 512M 64k' "$TEST_IMG" | _filter_qemu_io 15198d39e34SMax Reitz# Discard the first cluster. This cluster will soon enough be reallocated and 15298d39e34SMax Reitz# used for COW. 15398d39e34SMax Reitz$QEMU_IO -c 'discard 0k 64k' "$TEST_IMG" | _filter_qemu_io 15498d39e34SMax Reitz# Now, corrupt the image by marking the second L2 table cluster as free. 15598d39e34SMax Reitzpoke_file "$TEST_IMG" '131084' "\x00\x00" # 0x2000c 15698d39e34SMax Reitz# Start a write operation requiring COW on the image stopping it right before 15798d39e34SMax Reitz# doing the read; then, trigger the corruption prevention by writing anything to 15898d39e34SMax Reitz# any unallocated cluster, leading to an attempt to overwrite the second L2 15998d39e34SMax Reitz# table. Finally, resume the COW write and see it fail (but not crash). 16098d39e34SMax Reitzecho "open -o file.driver=blkdebug $TEST_IMG 16198d39e34SMax Reitzbreak cow_read 0 16298d39e34SMax Reitzaio_write 0k 1k 16398d39e34SMax Reitzwait_break 0 16498d39e34SMax Reitzwrite 64k 64k 16598d39e34SMax Reitzresume 0" | $QEMU_IO | _filter_qemu_io 16698d39e34SMax Reitz 167*a42f8a3dSMax Reitzecho 168*a42f8a3dSMax Reitzecho "=== Testing unallocated image header ===" 169*a42f8a3dSMax Reitzecho 170*a42f8a3dSMax Reitz_make_test_img 64M 171*a42f8a3dSMax Reitz# Create L1/L2 172*a42f8a3dSMax Reitz$QEMU_IO -c "$OPEN_RW" -c "write 0 64k" | _filter_qemu_io 173*a42f8a3dSMax Reitzpoke_file "$TEST_IMG" "$rb_offset" "\x00\x00" 174*a42f8a3dSMax Reitz$QEMU_IO -c "$OPEN_RW" -c "write 64k 64k" | _filter_qemu_io 175*a42f8a3dSMax Reitz 176ca0eca91SMax Reitz# success, all done 177ca0eca91SMax Reitzecho "*** done" 178ca0eca91SMax Reitzrm -f $seq.full 179ca0eca91SMax Reitzstatus=0 180