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 246ef083f61SAlberto Garciaecho "=== Testing empty refcount table ===" 247ef083f61SAlberto Garciaecho 248ef083f61SAlberto Garcia_make_test_img 64M 249ef083f61SAlberto Garciapoke_file "$TEST_IMG" "$rt_offset" "\x00\x00\x00\x00\x00\x00\x00\x00" 250ef083f61SAlberto Garcia$QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io 251bcb5270cSAlberto Garcia# Repair the image 252bcb5270cSAlberto Garcia_check_test_img -r all 253ef083f61SAlberto Garcia 254ef083f61SAlberto Garciaecho 2556bf45d59SAlberto Garciaecho "=== Testing empty refcount table with valid L1 and L2 tables ===" 2566bf45d59SAlberto Garciaecho 2576bf45d59SAlberto Garcia_make_test_img 64M 2586bf45d59SAlberto Garcia$QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io 2596bf45d59SAlberto Garciapoke_file "$TEST_IMG" "$rt_offset" "\x00\x00\x00\x00\x00\x00\x00\x00" 2606bf45d59SAlberto Garcia# Since the first data cluster is already allocated this triggers an 2616bf45d59SAlberto Garcia# allocation with an explicit offset (using qcow2_alloc_clusters_at()) 2626bf45d59SAlberto Garcia# causing a refcount block to be allocated at offset 0 2636bf45d59SAlberto Garcia$QEMU_IO -c "write 0 128k" "$TEST_IMG" | _filter_qemu_io 264bcb5270cSAlberto Garcia# Repair the image 265bcb5270cSAlberto Garcia_check_test_img -r all 2666bf45d59SAlberto Garcia 26798839750SAlberto Garciaecho 26898839750SAlberto Garciaecho "=== Testing empty refcount block ===" 26998839750SAlberto Garciaecho 27098839750SAlberto Garcia_make_test_img 64M 27198839750SAlberto Garciapoke_file "$TEST_IMG" "$rb_offset" "\x00\x00\x00\x00\x00\x00\x00\x00" 27298839750SAlberto Garcia$QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io 273bcb5270cSAlberto Garcia# Repair the image 274bcb5270cSAlberto Garcia_check_test_img -r all 27598839750SAlberto Garcia 2768aa34834SAlberto Garciaecho 2778aa34834SAlberto Garciaecho "=== Testing empty refcount block with compressed write ===" 2788aa34834SAlberto Garciaecho 2798aa34834SAlberto Garcia_make_test_img 64M 2808aa34834SAlberto Garcia$QEMU_IO -c "write 64k 64k" "$TEST_IMG" | _filter_qemu_io 2818aa34834SAlberto Garciapoke_file "$TEST_IMG" "$rb_offset" "\x00\x00\x00\x00\x00\x00\x00\x00" 2828aa34834SAlberto Garcia# The previous write already allocated an L2 table, so now this new 2838aa34834SAlberto Garcia# write will try to allocate a compressed data cluster at offset 0. 2848aa34834SAlberto Garcia$QEMU_IO -c "write -c 0k 64k" "$TEST_IMG" | _filter_qemu_io 285bcb5270cSAlberto Garcia# Repair the image 286bcb5270cSAlberto Garcia_check_test_img -r all 2878aa34834SAlberto Garcia 288951053a9SAlberto Garciaecho 289951053a9SAlberto Garciaecho "=== Testing zero refcount table size ===" 290951053a9SAlberto Garciaecho 291951053a9SAlberto Garcia_make_test_img 64M 292951053a9SAlberto Garciapoke_file "$TEST_IMG" "56" "\x00\x00\x00\x00" 293951053a9SAlberto Garcia$QEMU_IO -c "write 0 64k" "$TEST_IMG" 2>&1 | _filter_testdir | _filter_imgfmt 294bcb5270cSAlberto Garcia# Repair the image 295bcb5270cSAlberto Garcia_check_test_img -r all 296951053a9SAlberto Garcia 2975a45da5eSAlberto Garciaecho 2985a45da5eSAlberto Garciaecho "=== Testing incorrect refcount table offset ===" 2995a45da5eSAlberto Garciaecho 3005a45da5eSAlberto Garcia_make_test_img 64M 3015a45da5eSAlberto Garciapoke_file "$TEST_IMG" "48" "\x00\x00\x00\x00\x00\x00\x00\x00" 3025a45da5eSAlberto Garcia$QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io 3035a45da5eSAlberto Garcia 304791fff50SMax Reitzecho 305791fff50SMax Reitzecho "=== Testing dirty corrupt image ===" 306791fff50SMax Reitzecho 307791fff50SMax Reitz 308791fff50SMax Reitz_make_test_img 64M 309791fff50SMax Reitz 310791fff50SMax Reitz# Let the refblock appear unaligned 311791fff50SMax Reitzpoke_file "$TEST_IMG" "$rt_offset" "\x00\x00\x00\x00\xff\xff\x2a\x00" 312791fff50SMax Reitz# Mark the image dirty, thus forcing an automatic check when opening it 313791fff50SMax Reitzpoke_file "$TEST_IMG" 72 "\x00\x00\x00\x00\x00\x00\x00\x01" 314791fff50SMax Reitz# Open the image (qemu should refuse to do so) 315791fff50SMax Reitz$QEMU_IO -c close "$TEST_IMG" 2>&1 | _filter_testdir | _filter_imgfmt 316791fff50SMax Reitz 317791fff50SMax Reitzecho '--- Repairing ---' 318791fff50SMax Reitz 319791fff50SMax Reitz# The actual repair should have happened (because of the dirty bit), 320791fff50SMax Reitz# but some cleanup may have failed (like freeing the old reftable) 321791fff50SMax Reitz# because the image was already marked corrupt by that point 322791fff50SMax Reitz_check_test_img -r all 323791fff50SMax Reitz 32493bbaf03SMax Reitzecho 32593bbaf03SMax Reitzecho "=== Writing to an unaligned preallocated zero cluster ===" 32693bbaf03SMax Reitzecho 32793bbaf03SMax Reitz 32893bbaf03SMax Reitz_make_test_img 64M 32993bbaf03SMax Reitz 33093bbaf03SMax Reitz# Allocate the L2 table 33193bbaf03SMax Reitz$QEMU_IO -c "write 0 64k" -c "discard 0 64k" "$TEST_IMG" | _filter_qemu_io 33293bbaf03SMax Reitz# Pretend there is a preallocated zero cluster somewhere inside the 33393bbaf03SMax Reitz# image header 33493bbaf03SMax Reitzpoke_file "$TEST_IMG" "$l2_offset" "\x80\x00\x00\x00\x00\x00\x2a\x01" 33593bbaf03SMax Reitz# Let's write to it! 33693bbaf03SMax Reitz$QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io 33793bbaf03SMax Reitz 33893bbaf03SMax Reitz# Can't repair this yet (TODO: We can just deallocate the cluster) 33993bbaf03SMax Reitz 340d470ad42SMax Reitzecho 341d470ad42SMax Reitzecho '=== Discarding with an unaligned refblock ===' 342d470ad42SMax Reitzecho 343d470ad42SMax Reitz 344d470ad42SMax Reitz_make_test_img 64M 345d470ad42SMax Reitz 346d470ad42SMax Reitz$QEMU_IO -c "write 0 128k" "$TEST_IMG" | _filter_qemu_io 347d470ad42SMax Reitz# Make our refblock unaligned 348d470ad42SMax Reitzpoke_file "$TEST_IMG" "$(($rt_offset))" "\x00\x00\x00\x00\x00\x00\x2a\x00" 349d470ad42SMax Reitz# Now try to discard something that will be submitted as two requests 350d470ad42SMax Reitz# (main part + tail) 351d470ad42SMax Reitz$QEMU_IO -c "discard 0 65537" "$TEST_IMG" 352d470ad42SMax Reitz 353d470ad42SMax Reitzecho '--- Repairing ---' 354d470ad42SMax Reitz# Fails the first repair because the corruption prevents the check 355d470ad42SMax Reitz# function from double-checking 356d470ad42SMax Reitz# (Using -q for the first invocation, because otherwise the 357d470ad42SMax Reitz# double-check error message appears above the summary for some 358d470ad42SMax Reitz# reason -- so let's just hide the summary) 359d470ad42SMax Reitz_check_test_img -q -r all 360d470ad42SMax Reitz_check_test_img -r all 361d470ad42SMax Reitz 36223482f8aSMax Reitzecho 36323482f8aSMax Reitzecho "=== Discarding an out-of-bounds refblock ===" 36423482f8aSMax Reitzecho 36523482f8aSMax Reitz 36623482f8aSMax Reitz_make_test_img 64M 36723482f8aSMax Reitz 36823482f8aSMax Reitz# Pretend there's a refblock really up high 36923482f8aSMax Reitzpoke_file "$TEST_IMG" "$(($rt_offset+8))" "\x00\xff\xff\xff\x00\x00\x00\x00" 37023482f8aSMax Reitz# Let's try to shrink the qcow2 image so that the block driver tries 37123482f8aSMax Reitz# to discard that refblock (and see what happens!) 37223482f8aSMax Reitz$QEMU_IMG resize --shrink "$TEST_IMG" 32M 37323482f8aSMax Reitz 37423482f8aSMax Reitzecho '--- Checking and retrying ---' 37523482f8aSMax Reitz# Image should not be resized 37623482f8aSMax Reitz_img_info | grep 'virtual size' 37723482f8aSMax Reitz# But it should pass this check, because the "partial" resize has 37823482f8aSMax Reitz# already overwritten refblocks past the end 37923482f8aSMax Reitz_check_test_img -r all 38023482f8aSMax Reitz# So let's try again 38123482f8aSMax Reitz$QEMU_IMG resize --shrink "$TEST_IMG" 32M 38223482f8aSMax Reitz_img_info | grep 'virtual size' 38323482f8aSMax Reitz 38423482f8aSMax Reitzecho 38523482f8aSMax Reitzecho "=== Discarding a non-covered in-bounds refblock ===" 38623482f8aSMax Reitzecho 38723482f8aSMax Reitz 38823482f8aSMax ReitzIMGOPTS='refcount_bits=1' _make_test_img 64M 38923482f8aSMax Reitz 39023482f8aSMax Reitz# Pretend there's a refblock somewhere where there is no refblock to 39123482f8aSMax Reitz# cover it (but the covering refblock has a valid index in the 39223482f8aSMax Reitz# reftable) 39323482f8aSMax Reitz# Every refblock covers 65536 * 8 * 65536 = 32 GB, so we have to point 39423482f8aSMax Reitz# to 0x10_0000_0000 (64G) to point to the third refblock 39523482f8aSMax Reitzpoke_file "$TEST_IMG" "$(($rt_offset+8))" "\x00\x00\x00\x10\x00\x00\x00\x00" 39623482f8aSMax Reitz$QEMU_IMG resize --shrink "$TEST_IMG" 32M 39723482f8aSMax Reitz 39823482f8aSMax Reitzecho '--- Checking and retrying ---' 39923482f8aSMax Reitz# Image should not be resized 40023482f8aSMax Reitz_img_info | grep 'virtual size' 40123482f8aSMax Reitz# But it should pass this check, because the "partial" resize has 40223482f8aSMax Reitz# already overwritten refblocks past the end 40323482f8aSMax Reitz_check_test_img -r all 40423482f8aSMax Reitz# So let's try again 40523482f8aSMax Reitz$QEMU_IMG resize --shrink "$TEST_IMG" 32M 40623482f8aSMax Reitz_img_info | grep 'virtual size' 40723482f8aSMax Reitz 408*4efb1f7cSMax Reitzecho 409*4efb1f7cSMax Reitzecho "=== Discarding a refblock covered by an unaligned refblock ===" 410*4efb1f7cSMax Reitzecho 411*4efb1f7cSMax Reitz 412*4efb1f7cSMax ReitzIMGOPTS='refcount_bits=1' _make_test_img 64M 413*4efb1f7cSMax Reitz 414*4efb1f7cSMax Reitz# Same as above 415*4efb1f7cSMax Reitzpoke_file "$TEST_IMG" "$(($rt_offset+8))" "\x00\x00\x00\x10\x00\x00\x00\x00" 416*4efb1f7cSMax Reitz# But now we actually "create" an unaligned third refblock 417*4efb1f7cSMax Reitzpoke_file "$TEST_IMG" "$(($rt_offset+16))" "\x00\x00\x00\x00\x00\x00\x02\x00" 418*4efb1f7cSMax Reitz$QEMU_IMG resize --shrink "$TEST_IMG" 32M 419*4efb1f7cSMax Reitz 420*4efb1f7cSMax Reitzecho '--- Repairing ---' 421*4efb1f7cSMax Reitz# Fails the first repair because the corruption prevents the check 422*4efb1f7cSMax Reitz# function from double-checking 423*4efb1f7cSMax Reitz# (Using -q for the first invocation, because otherwise the 424*4efb1f7cSMax Reitz# double-check error message appears above the summary for some 425*4efb1f7cSMax Reitz# reason -- so let's just hide the summary) 426*4efb1f7cSMax Reitz_check_test_img -q -r all 427*4efb1f7cSMax Reitz_check_test_img -r all 428*4efb1f7cSMax Reitz 429ca0eca91SMax Reitz# success, all done 430ca0eca91SMax Reitzecho "*** done" 431ca0eca91SMax Reitzrm -f $seq.full 432ca0eca91SMax Reitzstatus=0 433