1*80f5c011SAlberto Garcia#!/usr/bin/env bash 2*80f5c011SAlberto Garcia# 3*80f5c011SAlberto Garcia# Test how 'qemu-io -c discard' behaves on v2 and v3 qcow2 images 4*80f5c011SAlberto Garcia# 5*80f5c011SAlberto Garcia# Copyright (C) 2020 Igalia, S.L. 6*80f5c011SAlberto Garcia# Author: Alberto Garcia <berto@igalia.com> 7*80f5c011SAlberto Garcia# 8*80f5c011SAlberto Garcia# This program is free software; you can redistribute it and/or modify 9*80f5c011SAlberto Garcia# it under the terms of the GNU General Public License as published by 10*80f5c011SAlberto Garcia# the Free Software Foundation; either version 2 of the License, or 11*80f5c011SAlberto Garcia# (at your option) any later version. 12*80f5c011SAlberto Garcia# 13*80f5c011SAlberto Garcia# This program is distributed in the hope that it will be useful, 14*80f5c011SAlberto Garcia# but WITHOUT ANY WARRANTY; without even the implied warranty of 15*80f5c011SAlberto Garcia# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16*80f5c011SAlberto Garcia# GNU General Public License for more details. 17*80f5c011SAlberto Garcia# 18*80f5c011SAlberto Garcia# You should have received a copy of the GNU General Public License 19*80f5c011SAlberto Garcia# along with this program. If not, see <http://www.gnu.org/licenses/>. 20*80f5c011SAlberto Garcia# 21*80f5c011SAlberto Garcia 22*80f5c011SAlberto Garcia# creator 23*80f5c011SAlberto Garciaowner=berto@igalia.com 24*80f5c011SAlberto Garcia 25*80f5c011SAlberto Garciaseq=`basename $0` 26*80f5c011SAlberto Garciaecho "QA output created by $seq" 27*80f5c011SAlberto Garcia 28*80f5c011SAlberto Garciastatus=1 # failure is the default! 29*80f5c011SAlberto Garcia 30*80f5c011SAlberto Garcia_cleanup() 31*80f5c011SAlberto Garcia{ 32*80f5c011SAlberto Garcia _cleanup_test_img 33*80f5c011SAlberto Garcia} 34*80f5c011SAlberto Garciatrap "_cleanup; exit \$status" 0 1 2 3 15 35*80f5c011SAlberto Garcia 36*80f5c011SAlberto Garcia# get standard environment, filters and checks 37*80f5c011SAlberto Garcia. ./common.rc 38*80f5c011SAlberto Garcia. ./common.filter 39*80f5c011SAlberto Garcia 40*80f5c011SAlberto Garcia_supported_fmt qcow2 41*80f5c011SAlberto Garcia_supported_proto file 42*80f5c011SAlberto Garcia_supported_os Linux 43*80f5c011SAlberto Garcia_unsupported_imgopts 'compat=0.10' refcount_bits data_file 44*80f5c011SAlberto Garcia 45*80f5c011SAlberto Garciaecho 46*80f5c011SAlberto Garciaecho "### Test 'qemu-io -c discard' on a QCOW2 image without a backing file" 47*80f5c011SAlberto Garciaecho 48*80f5c011SAlberto Garciafor qcow2_compat in 0.10 1.1; do 49*80f5c011SAlberto Garcia echo "# Create an image with compat=$qcow2_compat without a backing file" 50*80f5c011SAlberto Garcia _make_test_img -o "compat=$qcow2_compat" 128k 51*80f5c011SAlberto Garcia 52*80f5c011SAlberto Garcia echo "# Fill all clusters with data and then discard them" 53*80f5c011SAlberto Garcia $QEMU_IO -c 'write -P 0x01 0 128k' "$TEST_IMG" | _filter_qemu_io 54*80f5c011SAlberto Garcia $QEMU_IO -c 'discard 0 128k' "$TEST_IMG" | _filter_qemu_io 55*80f5c011SAlberto Garcia 56*80f5c011SAlberto Garcia echo "# Read the data from the discarded clusters" 57*80f5c011SAlberto Garcia $QEMU_IO -c 'read -P 0x00 0 128k' "$TEST_IMG" | _filter_qemu_io 58*80f5c011SAlberto Garcia 59*80f5c011SAlberto Garcia echo "# Output of qemu-img map" 60*80f5c011SAlberto Garcia $QEMU_IMG map "$TEST_IMG" | _filter_testdir 61*80f5c011SAlberto Garciadone 62*80f5c011SAlberto Garcia 63*80f5c011SAlberto Garciaecho 64*80f5c011SAlberto Garciaecho "### Test 'qemu-io -c discard' on a QCOW2 image with a backing file" 65*80f5c011SAlberto Garciaecho 66*80f5c011SAlberto Garcia 67*80f5c011SAlberto Garciaecho "# Create a backing image and fill it with data" 68*80f5c011SAlberto GarciaBACKING_IMG="$TEST_IMG.base" 69*80f5c011SAlberto GarciaTEST_IMG="$BACKING_IMG" _make_test_img 128k 70*80f5c011SAlberto Garcia$QEMU_IO -c 'write -P 0xff 0 128k' "$BACKING_IMG" | _filter_qemu_io 71*80f5c011SAlberto Garcia 72*80f5c011SAlberto Garciafor qcow2_compat in 0.10 1.1; do 73*80f5c011SAlberto Garcia echo "# Create an image with compat=$qcow2_compat and a backing file" 74*80f5c011SAlberto Garcia _make_test_img -o "compat=$qcow2_compat" -b "$BACKING_IMG" 75*80f5c011SAlberto Garcia 76*80f5c011SAlberto Garcia echo "# Fill all clusters with data and then discard them" 77*80f5c011SAlberto Garcia $QEMU_IO -c 'write -P 0x01 0 128k' "$TEST_IMG" | _filter_qemu_io 78*80f5c011SAlberto Garcia $QEMU_IO -c 'discard 0 128k' "$TEST_IMG" | _filter_qemu_io 79*80f5c011SAlberto Garcia 80*80f5c011SAlberto Garcia echo "# Read the data from the discarded clusters" 81*80f5c011SAlberto Garcia if [ "$qcow2_compat" = "1.1" ]; then 82*80f5c011SAlberto Garcia # In qcow2 v3 clusters are zeroed (with QCOW_OFLAG_ZERO) 83*80f5c011SAlberto Garcia $QEMU_IO -c 'read -P 0x00 0 128k' "$TEST_IMG" | _filter_qemu_io 84*80f5c011SAlberto Garcia else 85*80f5c011SAlberto Garcia # In qcow2 v2 if there's a backing image we cannot zero the clusters 86*80f5c011SAlberto Garcia # without exposing the backing file data so discard does nothing 87*80f5c011SAlberto Garcia $QEMU_IO -c 'read -P 0x01 0 128k' "$TEST_IMG" | _filter_qemu_io 88*80f5c011SAlberto Garcia fi 89*80f5c011SAlberto Garcia 90*80f5c011SAlberto Garcia echo "# Output of qemu-img map" 91*80f5c011SAlberto Garcia $QEMU_IMG map "$TEST_IMG" | _filter_testdir 92*80f5c011SAlberto Garciadone 93*80f5c011SAlberto Garcia 94*80f5c011SAlberto Garcia# success, all done 95*80f5c011SAlberto Garciaecho "*** done" 96*80f5c011SAlberto Garciarm -f $seq.full 97*80f5c011SAlberto Garciastatus=0 98