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 24*34eeb82dSMax Reitzseq="$(basename $0)" 25ca0eca91SMax Reitzecho "QA output created by $seq" 26ca0eca91SMax Reitz 27*34eeb82dSMax 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 43ca0eca91SMax Reitz_supported_proto generic 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) 50*34eeb82dSMax Reitzl2_offset_after_snapshot=524288 # 0x80000 (XXX: just an assumption) 51ca0eca91SMax Reitz 52ca0eca91SMax ReitzIMGOPTS="compat=1.1" 53ca0eca91SMax Reitz 54*34eeb82dSMax ReitzOPEN_RW="open -o overlap-check=all $TEST_IMG" 55*34eeb82dSMax Reitz# Overlap checks are done before write operations only, therefore opening an 56*34eeb82dSMax Reitz# image read-only makes the overlap-check option irrelevant 57*34eeb82dSMax ReitzOPEN_RO="open -r $TEST_IMG" 58*34eeb82dSMax 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 71ca0eca91SMax Reitz./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 74*34eeb82dSMax 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 77ca0eca91SMax Reitz./qcow2.py "$TEST_IMG" dump-header | grep incompatible_features 78ca0eca91SMax Reitz 79ca0eca91SMax Reitz# Try to open the image R/W (which should fail) 80*34eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RW" -c "read 0 512" 2>&1 | _filter_qemu_io \ 81*34eeb82dSMax Reitz | _filter_testdir \ 82*34eeb82dSMax Reitz | _filter_imgfmt 83ca0eca91SMax Reitz 84ca0eca91SMax Reitz# Try to open it RO (which should succeed) 85*34eeb82dSMax 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 102ca0eca91SMax Reitz./qcow2.py "$TEST_IMG" dump-header | grep incompatible_features 103*34eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RW" -c "write -P 0x2a 0 512" | _filter_qemu_io 104ca0eca91SMax Reitz./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 110ca0eca91SMax Reitz./qcow2.py "$TEST_IMG" dump-header | grep incompatible_features 111ca0eca91SMax Reitz 112ca0eca91SMax Reitz# Look if it's really really fixed 113*34eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RW" -c "write -P 0x2a 0 512" | _filter_qemu_io 114ca0eca91SMax Reitz./qcow2.py "$TEST_IMG" dump-header | grep incompatible_features 115ca0eca91SMax Reitz 116*34eeb82dSMax Reitzecho 117*34eeb82dSMax Reitzecho "=== Testing cluster data reference into inactive L2 table ===" 118*34eeb82dSMax Reitzecho 119*34eeb82dSMax Reitz_make_test_img 64M 120*34eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RW" -c "write -P 1 0 512" | _filter_qemu_io 121*34eeb82dSMax Reitz$QEMU_IMG snapshot -c foo "$TEST_IMG" 122*34eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RW" -c "write -P 2 0 512" | _filter_qemu_io 123*34eeb82dSMax Reitz# The inactive L2 table remains at its old offset 124*34eeb82dSMax Reitzpoke_file "$TEST_IMG" "$l2_offset_after_snapshot" \ 125*34eeb82dSMax Reitz "\x80\x00\x00\x00\x00\x04\x00\x00" 126*34eeb82dSMax Reitz_check_test_img 127*34eeb82dSMax Reitz./qcow2.py "$TEST_IMG" dump-header | grep incompatible_features 128*34eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RW" -c "write -P 3 0 512" | _filter_qemu_io 129*34eeb82dSMax Reitz./qcow2.py "$TEST_IMG" dump-header | grep incompatible_features 130*34eeb82dSMax Reitz_check_test_img -r all 131*34eeb82dSMax Reitz./qcow2.py "$TEST_IMG" dump-header | grep incompatible_features 132*34eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RW" -c "write -P 4 0 512" | _filter_qemu_io 133*34eeb82dSMax Reitz./qcow2.py "$TEST_IMG" dump-header | grep incompatible_features 134*34eeb82dSMax Reitz 135*34eeb82dSMax Reitz# Check data 136*34eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RO" -c "read -P 4 0 512" | _filter_qemu_io 137*34eeb82dSMax Reitz$QEMU_IMG snapshot -a foo "$TEST_IMG" 138*34eeb82dSMax Reitz_check_test_img 139*34eeb82dSMax Reitz$QEMU_IO -c "$OPEN_RO" -c "read -P 1 0 512" | _filter_qemu_io 140*34eeb82dSMax Reitz 141ca0eca91SMax Reitz# success, all done 142ca0eca91SMax Reitzecho "*** done" 143ca0eca91SMax Reitzrm -f $seq.full 144ca0eca91SMax Reitzstatus=0 145