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 79f383611aSMax Reitz# This information should be available through qemu-img info 80e800e5d4SKevin Wolf_img_info --format-specific 81f383611aSMax Reitz 82ca0eca91SMax Reitz# Try to open the image R/W (which should fail) 8334eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RW" -c "read 0 512" 2>&1 | _filter_qemu_io \ 8434eeb82dSMax Reitz | _filter_testdir \ 8534eeb82dSMax Reitz | _filter_imgfmt 86ca0eca91SMax Reitz 87ca0eca91SMax Reitz# Try to open it RO (which should succeed) 8834eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RO" -c "read 0 512" | _filter_qemu_io 89ca0eca91SMax Reitz 90ca0eca91SMax Reitz# We could now try to fix the image, but this would probably fail (how should an 91ca0eca91SMax Reitz# L2 table linked onto the L1 table be fixed?) 92ca0eca91SMax Reitz 93ca0eca91SMax Reitzecho 94ca0eca91SMax Reitzecho "=== Testing cluster data reference into refcount block ===" 95ca0eca91SMax Reitzecho 96ca0eca91SMax Reitz_make_test_img 64M 97ca0eca91SMax Reitz# Allocate L2 table 98ca0eca91SMax Reitztruncate -s "$(($l2_offset+65536))" "$TEST_IMG" 99ca0eca91SMax Reitzpoke_file "$TEST_IMG" "$l1_offset" "\x80\x00\x00\x00\x00\x04\x00\x00" 100ca0eca91SMax Reitz# Mark cluster as used 101ca0eca91SMax Reitzpoke_file "$TEST_IMG" "$(($rb_offset+8))" "\x00\x01" 102ca0eca91SMax Reitz# Redirect new data cluster onto refcount block 103ca0eca91SMax Reitzpoke_file "$TEST_IMG" "$l2_offset" "\x80\x00\x00\x00\x00\x02\x00\x00" 104ca0eca91SMax Reitz_check_test_img 105ea81ca9dSMax Reitz$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features 10634eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RW" -c "write -P 0x2a 0 512" | _filter_qemu_io 107ea81ca9dSMax Reitz$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features 108ca0eca91SMax Reitz 109ca0eca91SMax Reitz# Try to fix it 110ca0eca91SMax Reitz_check_test_img -r all 111ca0eca91SMax Reitz 112ca0eca91SMax Reitz# The corrupt bit should be cleared 113ea81ca9dSMax Reitz$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features 114ca0eca91SMax Reitz 115ca0eca91SMax Reitz# Look if it's really really fixed 11634eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RW" -c "write -P 0x2a 0 512" | _filter_qemu_io 117ea81ca9dSMax Reitz$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features 118ca0eca91SMax Reitz 11934eeb82dSMax Reitzecho 12034eeb82dSMax Reitzecho "=== Testing cluster data reference into inactive L2 table ===" 12134eeb82dSMax Reitzecho 12234eeb82dSMax Reitz_make_test_img 64M 12334eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RW" -c "write -P 1 0 512" | _filter_qemu_io 12434eeb82dSMax Reitz$QEMU_IMG snapshot -c foo "$TEST_IMG" 12534eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RW" -c "write -P 2 0 512" | _filter_qemu_io 12634eeb82dSMax Reitz# The inactive L2 table remains at its old offset 12734eeb82dSMax Reitzpoke_file "$TEST_IMG" "$l2_offset_after_snapshot" \ 12834eeb82dSMax Reitz "\x80\x00\x00\x00\x00\x04\x00\x00" 12934eeb82dSMax Reitz_check_test_img 130ea81ca9dSMax Reitz$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features 13134eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RW" -c "write -P 3 0 512" | _filter_qemu_io 132ea81ca9dSMax Reitz$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features 13334eeb82dSMax Reitz_check_test_img -r all 134ea81ca9dSMax Reitz$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features 13534eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RW" -c "write -P 4 0 512" | _filter_qemu_io 136ea81ca9dSMax Reitz$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features 13734eeb82dSMax Reitz 13834eeb82dSMax Reitz# Check data 13934eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RO" -c "read -P 4 0 512" | _filter_qemu_io 14034eeb82dSMax Reitz$QEMU_IMG snapshot -a foo "$TEST_IMG" 14134eeb82dSMax Reitz_check_test_img 14234eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RO" -c "read -P 1 0 512" | _filter_qemu_io 14334eeb82dSMax Reitz 14498d39e34SMax Reitzecho 14598d39e34SMax Reitzecho "=== Testing overlap while COW is in flight ===" 14698d39e34SMax Reitzecho 14798d39e34SMax Reitz# compat=0.10 is required in order to make the following discard actually 14898d39e34SMax Reitz# unallocate the sector rather than make it a zero sector - we want COW, after 14998d39e34SMax Reitz# all. 15098d39e34SMax ReitzIMGOPTS='compat=0.10' _make_test_img 1G 15198d39e34SMax Reitz# Write two clusters, the second one enforces creation of an L2 table after 15298d39e34SMax Reitz# the first data cluster. 15398d39e34SMax Reitz$QEMU_IO -c 'write 0k 64k' -c 'write 512M 64k' "$TEST_IMG" | _filter_qemu_io 15498d39e34SMax Reitz# Discard the first cluster. This cluster will soon enough be reallocated and 15598d39e34SMax Reitz# used for COW. 15698d39e34SMax Reitz$QEMU_IO -c 'discard 0k 64k' "$TEST_IMG" | _filter_qemu_io 15798d39e34SMax Reitz# Now, corrupt the image by marking the second L2 table cluster as free. 15898d39e34SMax Reitzpoke_file "$TEST_IMG" '131084' "\x00\x00" # 0x2000c 15998d39e34SMax Reitz# Start a write operation requiring COW on the image stopping it right before 16098d39e34SMax Reitz# doing the read; then, trigger the corruption prevention by writing anything to 16198d39e34SMax Reitz# any unallocated cluster, leading to an attempt to overwrite the second L2 16298d39e34SMax Reitz# table. Finally, resume the COW write and see it fail (but not crash). 16398d39e34SMax Reitzecho "open -o file.driver=blkdebug $TEST_IMG 16498d39e34SMax Reitzbreak cow_read 0 16598d39e34SMax Reitzaio_write 0k 1k 16698d39e34SMax Reitzwait_break 0 16798d39e34SMax Reitzwrite 64k 64k 16898d39e34SMax Reitzresume 0" | $QEMU_IO | _filter_qemu_io 16998d39e34SMax Reitz 170a42f8a3dSMax Reitzecho 171a42f8a3dSMax Reitzecho "=== Testing unallocated image header ===" 172a42f8a3dSMax Reitzecho 173a42f8a3dSMax Reitz_make_test_img 64M 174a42f8a3dSMax Reitz# Create L1/L2 1755b0ed2beSMax Reitz$QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io 176a42f8a3dSMax Reitzpoke_file "$TEST_IMG" "$rb_offset" "\x00\x00" 1775b0ed2beSMax Reitz$QEMU_IO -c "write 64k 64k" "$TEST_IMG" | _filter_qemu_io 1785b0ed2beSMax Reitz 1795b0ed2beSMax Reitzecho 1805b0ed2beSMax Reitzecho "=== Testing unaligned L1 entry ===" 1815b0ed2beSMax Reitzecho 1825b0ed2beSMax Reitz_make_test_img 64M 1835b0ed2beSMax Reitz$QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io 1845b0ed2beSMax Reitz# This will be masked with ~(512 - 1) = ~0x1ff, so whether the lower 9 bits are 1855b0ed2beSMax Reitz# aligned or not does not matter 1865b0ed2beSMax Reitzpoke_file "$TEST_IMG" "$l1_offset" "\x80\x00\x00\x00\x00\x04\x2a\x00" 1875b0ed2beSMax Reitz$QEMU_IO -c "read 0 64k" "$TEST_IMG" | _filter_qemu_io 1885b0ed2beSMax Reitz 189*f30136b3SMax Reitz# Test how well zero cluster expansion can cope with this 190*f30136b3SMax Reitz_make_test_img 64M 191*f30136b3SMax Reitz$QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io 192*f30136b3SMax Reitzpoke_file "$TEST_IMG" "$l1_offset" "\x80\x00\x00\x00\x00\x04\x2a\x00" 193*f30136b3SMax Reitz$QEMU_IMG amend -o compat=0.10 "$TEST_IMG" 194*f30136b3SMax Reitz 1955b0ed2beSMax Reitzecho 1965b0ed2beSMax Reitzecho "=== Testing unaligned L2 entry ===" 1975b0ed2beSMax Reitzecho 1985b0ed2beSMax Reitz_make_test_img 64M 1995b0ed2beSMax Reitz$QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io 2005b0ed2beSMax Reitzpoke_file "$TEST_IMG" "$l2_offset" "\x80\x00\x00\x00\x00\x05\x2a\x00" 2015b0ed2beSMax Reitz$QEMU_IO -c "read 0 64k" "$TEST_IMG" | _filter_qemu_io 2025b0ed2beSMax Reitz 2035b0ed2beSMax Reitzecho 204*f30136b3SMax Reitzecho "=== Testing unaligned pre-allocated zero cluster ===" 205*f30136b3SMax Reitzecho 206*f30136b3SMax Reitz_make_test_img 64M 207*f30136b3SMax Reitz$QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io 208*f30136b3SMax Reitzpoke_file "$TEST_IMG" "$l2_offset" "\x80\x00\x00\x00\x00\x05\x2a\x01" 209*f30136b3SMax Reitz# zero cluster expansion 210*f30136b3SMax Reitz$QEMU_IMG amend -o compat=0.10 "$TEST_IMG" 211*f30136b3SMax Reitz 212*f30136b3SMax Reitzecho 2135b0ed2beSMax Reitzecho "=== Testing unaligned reftable entry ===" 2145b0ed2beSMax Reitzecho 2155b0ed2beSMax Reitz_make_test_img 64M 2165b0ed2beSMax Reitzpoke_file "$TEST_IMG" "$rt_offset" "\x00\x00\x00\x00\x00\x02\x2a\x00" 2175b0ed2beSMax Reitz$QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io 2185b0ed2beSMax Reitz 2195b0ed2beSMax Reitzecho 2205b0ed2beSMax Reitzecho "=== Testing non-fatal corruption on freeing ===" 2215b0ed2beSMax Reitzecho 2225b0ed2beSMax Reitz_make_test_img 64M 2235b0ed2beSMax Reitz$QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io 2245b0ed2beSMax Reitzpoke_file "$TEST_IMG" "$l2_offset" "\x80\x00\x00\x00\x00\x05\x2a\x00" 2255b0ed2beSMax Reitz$QEMU_IO -c "discard 0 64k" "$TEST_IMG" | _filter_qemu_io 2265b0ed2beSMax Reitz 2275b0ed2beSMax Reitzecho 2285b0ed2beSMax Reitzecho "=== Testing read-only corruption report ===" 2295b0ed2beSMax Reitzecho 2305b0ed2beSMax Reitz_make_test_img 64M 2315b0ed2beSMax Reitz$QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io 2325b0ed2beSMax Reitzpoke_file "$TEST_IMG" "$l2_offset" "\x80\x00\x00\x00\x00\x05\x2a\x00" 2335b0ed2beSMax Reitz# Should only emit a single error message 2345b0ed2beSMax Reitz$QEMU_IO -c "$OPEN_RO" -c "read 0 64k" -c "read 0 64k" | _filter_qemu_io 2355b0ed2beSMax Reitz 2365b0ed2beSMax Reitzecho 2375b0ed2beSMax Reitzecho "=== Testing non-fatal and then fatal corruption report ===" 2385b0ed2beSMax Reitzecho 2395b0ed2beSMax Reitz_make_test_img 64M 2405b0ed2beSMax Reitz$QEMU_IO -c "write 0 128k" "$TEST_IMG" | _filter_qemu_io 2415b0ed2beSMax Reitzpoke_file "$TEST_IMG" "$l2_offset" "\x80\x00\x00\x00\x00\x05\x2a\x00" 2425b0ed2beSMax Reitzpoke_file "$TEST_IMG" "$(($l2_offset+8))" "\x80\x00\x00\x00\x00\x06\x2a\x00" 2435b0ed2beSMax Reitz# Should emit two error messages 2445b0ed2beSMax Reitz$QEMU_IO -c "discard 0 64k" -c "read 64k 64k" "$TEST_IMG" | _filter_qemu_io 245a42f8a3dSMax Reitz 246ca0eca91SMax Reitz# success, all done 247ca0eca91SMax Reitzecho "*** done" 248ca0eca91SMax Reitzrm -f $seq.full 249ca0eca91SMax Reitzstatus=0 250