111a82d14SPhilippe Mathieu-Daudé#!/usr/bin/env bash 2097b500cSMax Reitz# 3097b500cSMax Reitz# General test case for qcow2's image check 4097b500cSMax Reitz# 5097b500cSMax Reitz# Copyright (C) 2015 Red Hat, Inc. 6097b500cSMax Reitz# 7097b500cSMax Reitz# This program is free software; you can redistribute it and/or modify 8097b500cSMax Reitz# it under the terms of the GNU General Public License as published by 9097b500cSMax Reitz# the Free Software Foundation; either version 2 of the License, or 10097b500cSMax Reitz# (at your option) any later version. 11097b500cSMax Reitz# 12097b500cSMax Reitz# This program is distributed in the hope that it will be useful, 13097b500cSMax Reitz# but WITHOUT ANY WARRANTY; without even the implied warranty of 14097b500cSMax Reitz# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15097b500cSMax Reitz# GNU General Public License for more details. 16097b500cSMax Reitz# 17097b500cSMax Reitz# You should have received a copy of the GNU General Public License 18097b500cSMax Reitz# along with this program. If not, see <http://www.gnu.org/licenses/>. 19097b500cSMax Reitz# 20097b500cSMax Reitz 21097b500cSMax Reitz# creator 22097b500cSMax Reitzowner=mreitz@redhat.com 23097b500cSMax Reitz 24097b500cSMax Reitzseq="$(basename $0)" 25097b500cSMax Reitzecho "QA output created by $seq" 26097b500cSMax Reitz 27097b500cSMax Reitzstatus=1 # failure is the default! 28097b500cSMax Reitz 29097b500cSMax Reitz_cleanup() 30097b500cSMax Reitz{ 31097b500cSMax Reitz _cleanup_test_img 32097b500cSMax Reitz} 33097b500cSMax Reitztrap "_cleanup; exit \$status" 0 1 2 3 15 34097b500cSMax Reitz 35097b500cSMax Reitz# get standard environment, filters and checks 36097b500cSMax Reitz. ./common.rc 37097b500cSMax Reitz. ./common.filter 38097b500cSMax Reitz 39e696f335SMax Reitz# This tests qcow2-specific low-level functionality 40097b500cSMax Reitz_supported_fmt qcow2 41097b500cSMax Reitz_supported_proto file 42097b500cSMax Reitz_supported_os Linux 433be2024aSMax Reitz# With an external data file, data clusters are not refcounted 44*a507c517SMax Reitz# (so qemu-img check would not do much); 45*a507c517SMax Reitz# we want to modify the refcounts, so we need them to have a specific 46*a507c517SMax Reitz# format (namely u16) 47*a507c517SMax Reitz_unsupported_imgopts data_file 'refcount_bits=\([^1]\|.\([^6]\|$\)\)' 48097b500cSMax Reitz 49097b500cSMax Reitzecho 50097b500cSMax Reitzecho '=== Check on an image with a multiple of 2^32 clusters ===' 51097b500cSMax Reitzecho 52097b500cSMax Reitz 53407fb56aSMax Reitz_make_test_img -o "cluster_size=512" 512 54097b500cSMax Reitz 55097b500cSMax Reitz# Allocate L2 table 56097b500cSMax Reitz$QEMU_IO -c 'write 0 512' "$TEST_IMG" | _filter_qemu_io 57097b500cSMax Reitz 58097b500cSMax Reitz# Put the data cluster at a multiple of 2 TB, resulting in the image apparently 59097b500cSMax Reitz# having a multiple of 2^32 clusters 60097b500cSMax Reitz# (To be more specific: It is at 32 PB) 61a5fff8d4SVladimir Sementsov-Ogievskiypoke_file "$TEST_IMG" $((2048 + 8)) "\x00\x80\x00\x00\x00\x00\x00\x00" 62097b500cSMax Reitz 63097b500cSMax Reitz# An offset of 32 PB results in qemu-img check having to allocate an in-memory 64a5fff8d4SVladimir Sementsov-Ogievskiy# refcount table of 128 TB (16 bit refcounts, 512 byte clusters), if qemu-img 65a5fff8d4SVladimir Sementsov-Ogievskiy# don't check that referenced data cluster is far beyond the end of file. 66a5fff8d4SVladimir Sementsov-Ogievskiy# But starting from 4.0, qemu-img does this check, and instead of "Cannot 67a5fff8d4SVladimir Sementsov-Ogievskiy# allocate memory", we have an error showing that l2 entry is invalid. 68097b500cSMax Reitz_check_test_img 69097b500cSMax Reitz 70*a507c517SMax Reitzecho 71*a507c517SMax Reitzecho '=== Check leaks-fixed/corruptions-fixed report' 72*a507c517SMax Reitzecho 73*a507c517SMax Reitz 74*a507c517SMax Reitz# After leaks and corruptions were fixed, those numbers should be 75*a507c517SMax Reitz# reported by qemu-img check 76*a507c517SMax Reitz_make_test_img 64k 77*a507c517SMax Reitz 78*a507c517SMax Reitz# Allocate data cluster 79*a507c517SMax Reitz$QEMU_IO -c 'write 0 64k' "$TEST_IMG" | _filter_qemu_io 80*a507c517SMax Reitz 81*a507c517SMax Reitzreftable_ofs=$(peek_file_be "$TEST_IMG" 48 8) 82*a507c517SMax Reitzrefblock_ofs=$(peek_file_be "$TEST_IMG" $reftable_ofs 8) 83*a507c517SMax Reitz 84*a507c517SMax Reitz# Introduce a leak: Make the image header's refcount 2 85*a507c517SMax Reitzpoke_file_be "$TEST_IMG" "$refblock_ofs" 2 2 86*a507c517SMax Reitz 87*a507c517SMax Reitzl1_ofs=$(peek_file_be "$TEST_IMG" 40 8) 88*a507c517SMax Reitz 89*a507c517SMax Reitz# Introduce a corruption: Drop the COPIED flag from the (first) L1 entry 90*a507c517SMax Reitzl1_entry=$(peek_file_be "$TEST_IMG" $l1_ofs 8) 91*a507c517SMax Reitzl1_entry=$((l1_entry & ~(1 << 63))) 92*a507c517SMax Reitzpoke_file_be "$TEST_IMG" $l1_ofs 8 $l1_entry 93*a507c517SMax Reitz 94*a507c517SMax Reitzecho 95*a507c517SMax Reitz# Should print the number of corruptions and leaks fixed 96*a507c517SMax Reitz# (Filter out all JSON fields (recognizable by their four-space 97*a507c517SMax Reitz# indentation), but keep the "-fixed" fields (by removing two spaces 98*a507c517SMax Reitz# from their indentation)) 99*a507c517SMax Reitz# (Also filter out the L1 entry, because why not) 100*a507c517SMax Reitz_check_test_img -r all --output=json \ 101*a507c517SMax Reitz | sed -e 's/^ \(.*\)-fixed"/\1-fixed"/' \ 102*a507c517SMax Reitz -e '/^ /d' \ 103*a507c517SMax Reitz -e "s/\\([^0-9a-f]\\)$(printf %x $l1_entry)\\([^0-9a-f]\\)/\1L1_ENTRY_VALUE\2/" 104*a507c517SMax Reitz 105097b500cSMax Reitz# success, all done 106097b500cSMax Reitzecho "*** done" 107097b500cSMax Reitzrm -f $seq.full 108097b500cSMax Reitzstatus=0 109