1*6cba5377SMax Reitz#!/bin/bash 2*6cba5377SMax Reitz# 3*6cba5377SMax Reitz# Test qcow2 image compression 4*6cba5377SMax Reitz# 5*6cba5377SMax Reitz# Copyright (C) 2018 Igalia, S.L. 6*6cba5377SMax Reitz# Author: Alberto Garcia <berto@igalia.com> 7*6cba5377SMax Reitz# 8*6cba5377SMax Reitz# This program is free software; you can redistribute it and/or modify 9*6cba5377SMax Reitz# it under the terms of the GNU General Public License as published by 10*6cba5377SMax Reitz# the Free Software Foundation; either version 2 of the License, or 11*6cba5377SMax Reitz# (at your option) any later version. 12*6cba5377SMax Reitz# 13*6cba5377SMax Reitz# This program is distributed in the hope that it will be useful, 14*6cba5377SMax Reitz# but WITHOUT ANY WARRANTY; without even the implied warranty of 15*6cba5377SMax Reitz# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16*6cba5377SMax Reitz# GNU General Public License for more details. 17*6cba5377SMax Reitz# 18*6cba5377SMax Reitz# You should have received a copy of the GNU General Public License 19*6cba5377SMax Reitz# along with this program. If not, see <http://www.gnu.org/licenses/>. 20*6cba5377SMax Reitz# 21*6cba5377SMax Reitz 22*6cba5377SMax Reitzseq=$(basename "$0") 23*6cba5377SMax Reitzecho "QA output created by $seq" 24*6cba5377SMax Reitz 25*6cba5377SMax Reitzhere=$PWD 26*6cba5377SMax Reitzstatus=1 # failure is the default! 27*6cba5377SMax Reitz 28*6cba5377SMax Reitz_cleanup() 29*6cba5377SMax Reitz{ 30*6cba5377SMax Reitz _cleanup_test_img 31*6cba5377SMax Reitz} 32*6cba5377SMax Reitztrap "_cleanup; exit \$status" 0 1 2 3 15 33*6cba5377SMax Reitz 34*6cba5377SMax Reitz# get standard environment, filters and checks 35*6cba5377SMax Reitz. ./common.rc 36*6cba5377SMax Reitz. ./common.filter 37*6cba5377SMax Reitz 38*6cba5377SMax Reitz_supported_fmt qcow2 39*6cba5377SMax Reitz_supported_proto file 40*6cba5377SMax Reitz_supported_os Linux 41*6cba5377SMax Reitz 42*6cba5377SMax Reitz# Repairing the corrupted image requires qemu-img check to store a 43*6cba5377SMax Reitz# refcount up to 3, which requires at least two refcount bits. 44*6cba5377SMax Reitz_unsupported_imgopts 'refcount_bits=1[^0-9]' 45*6cba5377SMax Reitz 46*6cba5377SMax Reitz 47*6cba5377SMax Reitzecho 48*6cba5377SMax Reitzecho "=== Corrupted size field in compressed cluster descriptor ===" 49*6cba5377SMax Reitzecho 50*6cba5377SMax Reitz# Create an empty image and fill half of it with compressed data. 51*6cba5377SMax Reitz# The L2 entries of the two compressed clusters are located at 52*6cba5377SMax Reitz# 0x800000 and 0x800008, their original values are 0x4008000000a00000 53*6cba5377SMax Reitz# and 0x4008000000a00802 (5 sectors for compressed data each). 54*6cba5377SMax Reitz_make_test_img 8M -o cluster_size=2M 55*6cba5377SMax Reitz$QEMU_IO -c "write -c -P 0x11 0 2M" -c "write -c -P 0x11 2M 2M" "$TEST_IMG" \ 56*6cba5377SMax Reitz 2>&1 | _filter_qemu_io | _filter_testdir 57*6cba5377SMax Reitz 58*6cba5377SMax Reitz# Reduce size of compressed data to 4 sectors: this corrupts the image. 59*6cba5377SMax Reitzpoke_file "$TEST_IMG" $((0x800000)) "\x40\x06" 60*6cba5377SMax Reitz$QEMU_IO -c "read -P 0x11 0 4M" "$TEST_IMG" 2>&1 | _filter_qemu_io | _filter_testdir 61*6cba5377SMax Reitz 62*6cba5377SMax Reitz# 'qemu-img check' however doesn't see anything wrong because it 63*6cba5377SMax Reitz# doesn't try to decompress the data and the refcounts are consistent. 64*6cba5377SMax Reitz# TODO: update qemu-img so this can be detected. 65*6cba5377SMax Reitz_check_test_img 66*6cba5377SMax Reitz 67*6cba5377SMax Reitz# Increase size of compressed data to the maximum (8192 sectors). 68*6cba5377SMax Reitz# This makes QEMU read more data (8192 sectors instead of 5, host 69*6cba5377SMax Reitz# addresses [0xa00000, 0xdfffff]), but the decompression algorithm 70*6cba5377SMax Reitz# stops once we have enough to restore the uncompressed cluster, so 71*6cba5377SMax Reitz# the rest of the data is ignored. 72*6cba5377SMax Reitzpoke_file "$TEST_IMG" $((0x800000)) "\x7f\xfe" 73*6cba5377SMax Reitz# Do it also for the second compressed cluster (L2 entry at 0x800008). 74*6cba5377SMax Reitz# In this case the compressed data would span 3 host clusters 75*6cba5377SMax Reitz# (host addresses: [0xa00802, 0xe00801]) 76*6cba5377SMax Reitzpoke_file "$TEST_IMG" $((0x800008)) "\x7f\xfe" 77*6cba5377SMax Reitz 78*6cba5377SMax Reitz# Here the image is too small so we're asking QEMU to read beyond the 79*6cba5377SMax Reitz# end of the image. 80*6cba5377SMax Reitz$QEMU_IO -c "read -P 0x11 0 4M" "$TEST_IMG" 2>&1 | _filter_qemu_io | _filter_testdir 81*6cba5377SMax Reitz# But if we grow the image we won't be reading beyond its end anymore. 82*6cba5377SMax Reitz$QEMU_IO -c "write -P 0x22 4M 4M" "$TEST_IMG" 2>&1 | _filter_qemu_io | _filter_testdir 83*6cba5377SMax Reitz$QEMU_IO -c "read -P 0x11 0 4M" "$TEST_IMG" 2>&1 | _filter_qemu_io | _filter_testdir 84*6cba5377SMax Reitz 85*6cba5377SMax Reitz# The refcount data is however wrong because due to the increased size 86*6cba5377SMax Reitz# of the compressed data it now reaches the following host clusters. 87*6cba5377SMax Reitz# This can be repaired by qemu-img check by increasing the refcount of 88*6cba5377SMax Reitz# those clusters. 89*6cba5377SMax Reitz# TODO: update qemu-img to correct the compressed cluster size instead. 90*6cba5377SMax Reitz_check_test_img -r all 91*6cba5377SMax Reitz$QEMU_IO -c "read -P 0x11 0 4M" "$TEST_IMG" 2>&1 | _filter_qemu_io | _filter_testdir 92*6cba5377SMax Reitz$QEMU_IO -c "read -P 0x22 4M 4M" "$TEST_IMG" 2>&1 | _filter_qemu_io | _filter_testdir 93*6cba5377SMax Reitz 94*6cba5377SMax Reitz# success, all done 95*6cba5377SMax Reitzecho '*** done' 96*6cba5377SMax Reitzrm -f $seq.full 97*6cba5377SMax Reitzstatus=0 98