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