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