180f5c011SAlberto Garcia#!/usr/bin/env bash 280f5c011SAlberto Garcia# 380f5c011SAlberto Garcia# Test how 'qemu-io -c discard' behaves on v2 and v3 qcow2 images 480f5c011SAlberto Garcia# 580f5c011SAlberto Garcia# Copyright (C) 2020 Igalia, S.L. 680f5c011SAlberto Garcia# Author: Alberto Garcia <berto@igalia.com> 780f5c011SAlberto Garcia# 880f5c011SAlberto Garcia# This program is free software; you can redistribute it and/or modify 980f5c011SAlberto Garcia# it under the terms of the GNU General Public License as published by 1080f5c011SAlberto Garcia# the Free Software Foundation; either version 2 of the License, or 1180f5c011SAlberto Garcia# (at your option) any later version. 1280f5c011SAlberto Garcia# 1380f5c011SAlberto Garcia# This program is distributed in the hope that it will be useful, 1480f5c011SAlberto Garcia# but WITHOUT ANY WARRANTY; without even the implied warranty of 1580f5c011SAlberto Garcia# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1680f5c011SAlberto Garcia# GNU General Public License for more details. 1780f5c011SAlberto Garcia# 1880f5c011SAlberto Garcia# You should have received a copy of the GNU General Public License 1980f5c011SAlberto Garcia# along with this program. If not, see <http://www.gnu.org/licenses/>. 2080f5c011SAlberto Garcia# 2180f5c011SAlberto Garcia 2280f5c011SAlberto Garcia# creator 2380f5c011SAlberto Garciaowner=berto@igalia.com 2480f5c011SAlberto Garcia 2580f5c011SAlberto Garciaseq=`basename $0` 2680f5c011SAlberto Garciaecho "QA output created by $seq" 2780f5c011SAlberto Garcia 2880f5c011SAlberto Garciastatus=1 # failure is the default! 2980f5c011SAlberto Garcia 3080f5c011SAlberto Garcia_cleanup() 3180f5c011SAlberto Garcia{ 3280f5c011SAlberto Garcia _cleanup_test_img 3380f5c011SAlberto Garcia} 3480f5c011SAlberto Garciatrap "_cleanup; exit \$status" 0 1 2 3 15 3580f5c011SAlberto Garcia 3680f5c011SAlberto Garcia# get standard environment, filters and checks 3780f5c011SAlberto Garcia. ./common.rc 3880f5c011SAlberto Garcia. ./common.filter 3980f5c011SAlberto Garcia 4080f5c011SAlberto Garcia_supported_fmt qcow2 4180f5c011SAlberto Garcia_supported_proto file 4280f5c011SAlberto Garcia_supported_os Linux 4380f5c011SAlberto Garcia_unsupported_imgopts 'compat=0.10' refcount_bits data_file 4480f5c011SAlberto Garcia 4580f5c011SAlberto Garciaecho 4680f5c011SAlberto Garciaecho "### Test 'qemu-io -c discard' on a QCOW2 image without a backing file" 4780f5c011SAlberto Garciaecho 4880f5c011SAlberto Garciafor qcow2_compat in 0.10 1.1; do 4980f5c011SAlberto Garcia echo "# Create an image with compat=$qcow2_compat without a backing file" 5080f5c011SAlberto Garcia _make_test_img -o "compat=$qcow2_compat" 128k 5180f5c011SAlberto Garcia 5280f5c011SAlberto Garcia echo "# Fill all clusters with data and then discard them" 5380f5c011SAlberto Garcia $QEMU_IO -c 'write -P 0x01 0 128k' "$TEST_IMG" | _filter_qemu_io 5480f5c011SAlberto Garcia $QEMU_IO -c 'discard 0 128k' "$TEST_IMG" | _filter_qemu_io 5580f5c011SAlberto Garcia 5680f5c011SAlberto Garcia echo "# Read the data from the discarded clusters" 5780f5c011SAlberto Garcia $QEMU_IO -c 'read -P 0x00 0 128k' "$TEST_IMG" | _filter_qemu_io 5880f5c011SAlberto Garcia 5980f5c011SAlberto Garcia echo "# Output of qemu-img map" 6080f5c011SAlberto Garcia $QEMU_IMG map "$TEST_IMG" | _filter_testdir 6180f5c011SAlberto Garciadone 6280f5c011SAlberto Garcia 6380f5c011SAlberto Garciaecho 6480f5c011SAlberto Garciaecho "### Test 'qemu-io -c discard' on a QCOW2 image with a backing file" 6580f5c011SAlberto Garciaecho 6680f5c011SAlberto Garcia 6780f5c011SAlberto Garciaecho "# Create a backing image and fill it with data" 6880f5c011SAlberto GarciaBACKING_IMG="$TEST_IMG.base" 6980f5c011SAlberto GarciaTEST_IMG="$BACKING_IMG" _make_test_img 128k 7080f5c011SAlberto Garcia$QEMU_IO -c 'write -P 0xff 0 128k' "$BACKING_IMG" | _filter_qemu_io 7180f5c011SAlberto Garcia 7280f5c011SAlberto Garciafor qcow2_compat in 0.10 1.1; do 7380f5c011SAlberto Garcia echo "# Create an image with compat=$qcow2_compat and a backing file" 74*b66ff2c2SEric Blake _make_test_img -o "compat=$qcow2_compat" -b "$BACKING_IMG" -F $IMGFMT 7580f5c011SAlberto Garcia 7680f5c011SAlberto Garcia echo "# Fill all clusters with data and then discard them" 7780f5c011SAlberto Garcia $QEMU_IO -c 'write -P 0x01 0 128k' "$TEST_IMG" | _filter_qemu_io 7880f5c011SAlberto Garcia $QEMU_IO -c 'discard 0 128k' "$TEST_IMG" | _filter_qemu_io 7980f5c011SAlberto Garcia 8080f5c011SAlberto Garcia echo "# Read the data from the discarded clusters" 8180f5c011SAlberto Garcia if [ "$qcow2_compat" = "1.1" ]; then 8280f5c011SAlberto Garcia # In qcow2 v3 clusters are zeroed (with QCOW_OFLAG_ZERO) 8380f5c011SAlberto Garcia $QEMU_IO -c 'read -P 0x00 0 128k' "$TEST_IMG" | _filter_qemu_io 8480f5c011SAlberto Garcia else 8580f5c011SAlberto Garcia # In qcow2 v2 if there's a backing image we cannot zero the clusters 8680f5c011SAlberto Garcia # without exposing the backing file data so discard does nothing 8780f5c011SAlberto Garcia $QEMU_IO -c 'read -P 0x01 0 128k' "$TEST_IMG" | _filter_qemu_io 8880f5c011SAlberto Garcia fi 8980f5c011SAlberto Garcia 9080f5c011SAlberto Garcia echo "# Output of qemu-img map" 9180f5c011SAlberto Garcia $QEMU_IMG map "$TEST_IMG" | _filter_testdir 9280f5c011SAlberto Garciadone 9380f5c011SAlberto Garcia 9480f5c011SAlberto Garcia# success, all done 9580f5c011SAlberto Garciaecho "*** done" 9680f5c011SAlberto Garciarm -f $seq.full 9780f5c011SAlberto Garciastatus=0 98