1f3d07ce8SThomas Huth#!/usr/bin/env bash 276b90e23SKevin Wolf# 376b90e23SKevin Wolf# Test qcow2 with external data files 476b90e23SKevin Wolf# 576b90e23SKevin Wolf# Copyright (C) 2019 Red Hat, Inc. 676b90e23SKevin Wolf# 776b90e23SKevin Wolf# This program is free software; you can redistribute it and/or modify 876b90e23SKevin Wolf# it under the terms of the GNU General Public License as published by 976b90e23SKevin Wolf# the Free Software Foundation; either version 2 of the License, or 1076b90e23SKevin Wolf# (at your option) any later version. 1176b90e23SKevin Wolf# 1276b90e23SKevin Wolf# This program is distributed in the hope that it will be useful, 1376b90e23SKevin Wolf# but WITHOUT ANY WARRANTY; without even the implied warranty of 1476b90e23SKevin Wolf# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1576b90e23SKevin Wolf# GNU General Public License for more details. 1676b90e23SKevin Wolf# 1776b90e23SKevin Wolf# You should have received a copy of the GNU General Public License 1876b90e23SKevin Wolf# along with this program. If not, see <http://www.gnu.org/licenses/>. 1976b90e23SKevin Wolf# 2076b90e23SKevin Wolf 2176b90e23SKevin Wolf# creator 2276b90e23SKevin Wolfowner=kwolf@redhat.com 2376b90e23SKevin Wolf 2476b90e23SKevin Wolfseq=$(basename $0) 2576b90e23SKevin Wolfecho "QA output created by $seq" 2676b90e23SKevin Wolf 2776b90e23SKevin Wolfstatus=1 # failure is the default! 2876b90e23SKevin Wolf 2976b90e23SKevin Wolf_cleanup() 3076b90e23SKevin Wolf{ 3176b90e23SKevin Wolf _cleanup_test_img 32f91ecbd7SMax Reitz _rm_test_img "$TEST_IMG.data" 33f91ecbd7SMax Reitz _rm_test_img "$TEST_IMG.src" 3476b90e23SKevin Wolf} 3576b90e23SKevin Wolftrap "_cleanup; exit \$status" 0 1 2 3 15 3676b90e23SKevin Wolf 3776b90e23SKevin Wolf# get standard environment, filters and checks 3876b90e23SKevin Wolf. ./common.rc 3976b90e23SKevin Wolf. ./common.filter 4076b90e23SKevin Wolf 4176b90e23SKevin Wolf_supported_fmt qcow2 4276b90e23SKevin Wolf_supported_proto file 4376b90e23SKevin Wolf_supported_os Linux 443be2024aSMax Reitz# External data files do not work with compat=0.10, and because we use 453be2024aSMax Reitz# our own external data file, we cannot let the user specify one 463be2024aSMax Reitz_unsupported_imgopts 'compat=0.10' data_file 4776b90e23SKevin Wolf 4876b90e23SKevin Wolfecho 4976b90e23SKevin Wolfecho "=== Create and open image with external data file ===" 5076b90e23SKevin Wolfecho 5176b90e23SKevin Wolf 5276b90e23SKevin Wolfecho "With data file name in the image:" 53407fb56aSMax Reitz_make_test_img -o "data_file=$TEST_IMG.data" 64M 5476b90e23SKevin Wolf_check_test_img 5576b90e23SKevin Wolf 5676b90e23SKevin Wolf$QEMU_IO -c "open $TEST_IMG" -c "read -P 0 0 64k" 2>&1 | _filter_qemu_io | _filter_testdir 5776b90e23SKevin Wolf$QEMU_IO -c "open -odata-file.filename=$TEST_IMG.data $TEST_IMG" -c "read -P 0 0 64k" 2>&1 | _filter_qemu_io | _filter_testdir 5876b90e23SKevin Wolf$QEMU_IO -c "open -odata-file.filename=inexistent $TEST_IMG" -c "read -P 0 0 64k" 2>&1 | _filter_qemu_io | _filter_testdir 5976b90e23SKevin Wolf 6076b90e23SKevin Wolfecho 6176b90e23SKevin Wolfecho "Data file required, but without data file name in the image:" 6276b90e23SKevin Wolf$QEMU_IMG amend -odata_file= $TEST_IMG 6376b90e23SKevin Wolf 6476b90e23SKevin Wolf$QEMU_IO -c "open $TEST_IMG" -c "read -P 0 0 64k" 2>&1 | _filter_qemu_io | _filter_testdir 6576b90e23SKevin Wolf$QEMU_IO -c "open -odata-file.filename=$TEST_IMG.data $TEST_IMG" -c "read -P 0 0 64k" 2>&1 | _filter_qemu_io | _filter_testdir 6676b90e23SKevin Wolf$QEMU_IO -c "open -odata-file.filename=inexistent $TEST_IMG" -c "read -P 0 0 64k" 2>&1 | _filter_qemu_io | _filter_testdir 6776b90e23SKevin Wolf 6876b90e23SKevin Wolfecho 6976b90e23SKevin Wolfecho "Setting data-file for an image with internal data:" 7076b90e23SKevin Wolf_make_test_img 64M 7176b90e23SKevin Wolf 7276b90e23SKevin Wolf$QEMU_IO -c "open -odata-file.filename=$TEST_IMG.data $TEST_IMG" -c "read -P 0 0 64k" 2>&1 | _filter_qemu_io | _filter_testdir 7376b90e23SKevin Wolf$QEMU_IO -c "open -odata-file.filename=inexistent $TEST_IMG" -c "read -P 0 0 64k" 2>&1 | _filter_qemu_io | _filter_testdir 7476b90e23SKevin Wolf 7576b90e23SKevin Wolfecho 7676b90e23SKevin Wolfecho "=== Conflicting features ===" 7776b90e23SKevin Wolfecho 7876b90e23SKevin Wolf 7976b90e23SKevin Wolfecho "Convert to compressed target with data file:" 8076b90e23SKevin WolfTEST_IMG="$TEST_IMG.src" _make_test_img 64M 8176b90e23SKevin Wolf 8276b90e23SKevin Wolf$QEMU_IO -c 'write -P 0x11 0 1M' \ 8376b90e23SKevin Wolf -f $IMGFMT "$TEST_IMG.src" | 8476b90e23SKevin Wolf _filter_qemu_io 8576b90e23SKevin Wolf 8676b90e23SKevin Wolf$QEMU_IMG convert -f $IMGFMT -O $IMGFMT -c -odata_file="$TEST_IMG.data" \ 8776b90e23SKevin Wolf "$TEST_IMG.src" "$TEST_IMG" 8876b90e23SKevin Wolf 8976b90e23SKevin Wolfecho 9076b90e23SKevin Wolfecho "Convert uncompressed, then write compressed data manually:" 9176b90e23SKevin Wolf$QEMU_IMG convert -f $IMGFMT -O $IMGFMT -odata_file="$TEST_IMG.data" \ 9276b90e23SKevin Wolf "$TEST_IMG.src" "$TEST_IMG" 9376b90e23SKevin Wolf$QEMU_IMG compare "$TEST_IMG.src" "$TEST_IMG" 9476b90e23SKevin Wolf 9576b90e23SKevin Wolf$QEMU_IO -c 'write -c -P 0x22 0 1M' \ 9676b90e23SKevin Wolf -f $IMGFMT "$TEST_IMG" | 9776b90e23SKevin Wolf _filter_qemu_io 9876b90e23SKevin Wolf_check_test_img 9976b90e23SKevin Wolf 10076b90e23SKevin Wolfecho 10176b90e23SKevin Wolfecho "Take an internal snapshot:" 10276b90e23SKevin Wolf 10376b90e23SKevin Wolf$QEMU_IMG snapshot -c test "$TEST_IMG" 10476b90e23SKevin Wolf_check_test_img 10576b90e23SKevin Wolf 10676b90e23SKevin Wolfecho 10776b90e23SKevin Wolfecho "=== Standalone image with external data file (efficient) ===" 10876b90e23SKevin Wolfecho 10976b90e23SKevin Wolf 110407fb56aSMax Reitz_make_test_img -o "data_file=$TEST_IMG.data" 64M 11176b90e23SKevin Wolf 11276b90e23SKevin Wolfecho -n "qcow2 file size before I/O: " 11376b90e23SKevin Wolfdu -b $TEST_IMG | cut -f1 11476b90e23SKevin Wolf 11576b90e23SKevin Wolf# Create image with the following layout 11676b90e23SKevin Wolf# 0-1 MB: Unallocated 11776b90e23SKevin Wolf# 1-2 MB: Written (pattern 0x11) 11876b90e23SKevin Wolf# 2-3 MB: Discarded 11976b90e23SKevin Wolf# 3-4 MB: Zero write over discarded space 12076b90e23SKevin Wolf# 4-5 MB: Zero write over written space 12176b90e23SKevin Wolf# 5-6 MB: Zero write over unallocated space 12276b90e23SKevin Wolf 12376b90e23SKevin Wolfecho 12476b90e23SKevin Wolf$QEMU_IO -c 'write -P 0x11 1M 4M' \ 12576b90e23SKevin Wolf -c 'discard 2M 2M' \ 12676b90e23SKevin Wolf -c 'write -z 3M 3M' \ 12776b90e23SKevin Wolf -f $IMGFMT "$TEST_IMG" | 12876b90e23SKevin Wolf _filter_qemu_io 12976b90e23SKevin Wolf_check_test_img 13076b90e23SKevin Wolf 13176b90e23SKevin Wolfecho 13276b90e23SKevin Wolf$QEMU_IMG map --output=json "$TEST_IMG" 13376b90e23SKevin Wolf 13476b90e23SKevin Wolfecho 13576b90e23SKevin Wolf$QEMU_IO -c 'read -P 0 0 1M' \ 13676b90e23SKevin Wolf -c 'read -P 0x11 1M 1M' \ 13776b90e23SKevin Wolf -c 'read -P 0 2M 4M' \ 13876b90e23SKevin Wolf -f $IMGFMT "$TEST_IMG" | 13976b90e23SKevin Wolf _filter_qemu_io 14076b90e23SKevin Wolf 14176b90e23SKevin Wolf# Zero clusters are only marked as such in the qcow2 metadata, but contain 14276b90e23SKevin Wolf# stale data in the external data file 14376b90e23SKevin Wolfecho 14476b90e23SKevin Wolf$QEMU_IO -c 'read -P 0 0 1M' \ 14576b90e23SKevin Wolf -c 'read -P 0x11 1M 1M' \ 14676b90e23SKevin Wolf -c 'read -P 0x11 4M 1M' \ 14776b90e23SKevin Wolf -c 'read -P 0 5M 1M' \ 14876b90e23SKevin Wolf -f raw "$TEST_IMG.data" | 14976b90e23SKevin Wolf _filter_qemu_io 15076b90e23SKevin Wolf 15176b90e23SKevin Wolf 15276b90e23SKevin Wolfecho -n "qcow2 file size after I/O: " 15376b90e23SKevin Wolfdu -b $TEST_IMG | cut -f1 15476b90e23SKevin Wolf 15576b90e23SKevin Wolfecho 15676b90e23SKevin Wolfecho "=== Standalone image with external data file (valid raw) ===" 15776b90e23SKevin Wolfecho 15876b90e23SKevin Wolf 159407fb56aSMax Reitz_make_test_img -o "data_file=$TEST_IMG.data,data_file_raw=on" 64M 16076b90e23SKevin Wolf 16176b90e23SKevin Wolfecho -n "qcow2 file size before I/O: " 16276b90e23SKevin Wolfdu -b $TEST_IMG | cut -f1 16376b90e23SKevin Wolf 16476b90e23SKevin Wolfecho 16576b90e23SKevin Wolf$QEMU_IO -c 'write -P 0x11 1M 4M' \ 16676b90e23SKevin Wolf -c 'discard 2M 2M' \ 16776b90e23SKevin Wolf -c 'write -z 3M 3M' \ 16876b90e23SKevin Wolf -f $IMGFMT "$TEST_IMG" | 16976b90e23SKevin Wolf _filter_qemu_io 17076b90e23SKevin Wolf_check_test_img 17176b90e23SKevin Wolf 17276b90e23SKevin Wolfecho 17376b90e23SKevin Wolf$QEMU_IMG map --output=json "$TEST_IMG" 17476b90e23SKevin Wolf 17576b90e23SKevin Wolfecho 17676b90e23SKevin Wolf$QEMU_IO -c 'read -P 0 0 1M' \ 17776b90e23SKevin Wolf -c 'read -P 0x11 1M 1M' \ 17876b90e23SKevin Wolf -c 'read -P 0 2M 4M' \ 17976b90e23SKevin Wolf -f $IMGFMT "$TEST_IMG" | 18076b90e23SKevin Wolf _filter_qemu_io 18176b90e23SKevin Wolf 182*3fb61087SPaolo Bonzini# Discarded clusters are only marked as such in the qcow2 metadata, but 183*3fb61087SPaolo Bonzini# they can contain stale data in the external data file. Instead, zero 184*3fb61087SPaolo Bonzini# clusters must be zeroed in the external data file too. 18576b90e23SKevin Wolfecho 186*3fb61087SPaolo Bonzini$QEMU_IO -c 'read -P 0 0 1M' \ 187*3fb61087SPaolo Bonzini -c 'read -P 0x11 1M 1M' \ 188*3fb61087SPaolo Bonzini -c 'read -P 0 3M 3M' \ 189*3fb61087SPaolo Bonzini -f raw "$TEST_IMG".data | 190*3fb61087SPaolo Bonzini _filter_qemu_io 19176b90e23SKevin Wolf 19276b90e23SKevin Wolfecho -n "qcow2 file size after I/O: " 19376b90e23SKevin Wolfdu -b $TEST_IMG | cut -f1 19476b90e23SKevin Wolf 19576b90e23SKevin Wolfecho 19676b90e23SKevin Wolfecho "=== bdrv_co_block_status test for file and offset=0 ===" 19776b90e23SKevin Wolfecho 19876b90e23SKevin Wolf 199407fb56aSMax Reitz_make_test_img -o "data_file=$TEST_IMG.data" 64M 20076b90e23SKevin Wolf 20176b90e23SKevin Wolf$QEMU_IO -c 'write -P 0x11 0 1M' -f $IMGFMT "$TEST_IMG" | _filter_qemu_io 20276b90e23SKevin Wolf$QEMU_IO -c 'read -P 0x11 0 1M' -f $IMGFMT "$TEST_IMG" | _filter_qemu_io 20376b90e23SKevin Wolf$QEMU_IMG map --output=human "$TEST_IMG" | _filter_testdir 20476b90e23SKevin Wolf$QEMU_IMG map --output=json "$TEST_IMG" 20576b90e23SKevin Wolf 206a0cf8dafSKevin Wolfecho 207a0cf8dafSKevin Wolfecho "=== Copy offloading ===" 208a0cf8dafSKevin Wolfecho 209a0cf8dafSKevin Wolf 210a0cf8dafSKevin Wolf# Make use of copy offloading if the test host can provide it 211a0cf8dafSKevin Wolf_make_test_img -o "data_file=$TEST_IMG.data" 64M 212a0cf8dafSKevin Wolf$QEMU_IMG convert -f $IMGFMT -O $IMGFMT -n -C "$TEST_IMG.src" "$TEST_IMG" 213a0cf8dafSKevin Wolf$QEMU_IMG compare -f $IMGFMT -F $IMGFMT "$TEST_IMG.src" "$TEST_IMG" 214a0cf8dafSKevin Wolf 215a0cf8dafSKevin Wolf# blkdebug doesn't support copy offloading, so this tests the error path 216a0cf8dafSKevin Wolf$QEMU_IMG amend -f $IMGFMT -o "data_file=blkdebug::$TEST_IMG.data" "$TEST_IMG" 217a0cf8dafSKevin Wolf$QEMU_IMG convert -f $IMGFMT -O $IMGFMT -n -C "$TEST_IMG.src" "$TEST_IMG" 218a0cf8dafSKevin Wolf$QEMU_IMG compare -f $IMGFMT -F $IMGFMT "$TEST_IMG.src" "$TEST_IMG" 219a0cf8dafSKevin Wolf 22076b90e23SKevin Wolf# success, all done 22176b90e23SKevin Wolfecho "*** done" 22276b90e23SKevin Wolfrm -f $seq.full 22376b90e23SKevin Wolfstatus=0 224