1*76b90e23SKevin Wolf#!/bin/bash 2*76b90e23SKevin Wolf# 3*76b90e23SKevin Wolf# Test qcow2 with external data files 4*76b90e23SKevin Wolf# 5*76b90e23SKevin Wolf# Copyright (C) 2019 Red Hat, Inc. 6*76b90e23SKevin Wolf# 7*76b90e23SKevin Wolf# This program is free software; you can redistribute it and/or modify 8*76b90e23SKevin Wolf# it under the terms of the GNU General Public License as published by 9*76b90e23SKevin Wolf# the Free Software Foundation; either version 2 of the License, or 10*76b90e23SKevin Wolf# (at your option) any later version. 11*76b90e23SKevin Wolf# 12*76b90e23SKevin Wolf# This program is distributed in the hope that it will be useful, 13*76b90e23SKevin Wolf# but WITHOUT ANY WARRANTY; without even the implied warranty of 14*76b90e23SKevin Wolf# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15*76b90e23SKevin Wolf# GNU General Public License for more details. 16*76b90e23SKevin Wolf# 17*76b90e23SKevin Wolf# You should have received a copy of the GNU General Public License 18*76b90e23SKevin Wolf# along with this program. If not, see <http://www.gnu.org/licenses/>. 19*76b90e23SKevin Wolf# 20*76b90e23SKevin Wolf 21*76b90e23SKevin Wolf# creator 22*76b90e23SKevin Wolfowner=kwolf@redhat.com 23*76b90e23SKevin Wolf 24*76b90e23SKevin Wolfseq=$(basename $0) 25*76b90e23SKevin Wolfecho "QA output created by $seq" 26*76b90e23SKevin Wolf 27*76b90e23SKevin Wolfstatus=1 # failure is the default! 28*76b90e23SKevin Wolf 29*76b90e23SKevin Wolf_cleanup() 30*76b90e23SKevin Wolf{ 31*76b90e23SKevin Wolf _cleanup_test_img 32*76b90e23SKevin Wolf rm -f $TEST_IMG.data 33*76b90e23SKevin Wolf rm -f $TEST_IMG.src 34*76b90e23SKevin Wolf} 35*76b90e23SKevin Wolftrap "_cleanup; exit \$status" 0 1 2 3 15 36*76b90e23SKevin Wolf 37*76b90e23SKevin Wolf# get standard environment, filters and checks 38*76b90e23SKevin Wolf. ./common.rc 39*76b90e23SKevin Wolf. ./common.filter 40*76b90e23SKevin Wolf 41*76b90e23SKevin Wolf_supported_fmt qcow2 42*76b90e23SKevin Wolf_supported_proto file 43*76b90e23SKevin Wolf_supported_os Linux 44*76b90e23SKevin Wolf 45*76b90e23SKevin Wolfecho 46*76b90e23SKevin Wolfecho "=== Create and open image with external data file ===" 47*76b90e23SKevin Wolfecho 48*76b90e23SKevin Wolf 49*76b90e23SKevin Wolfecho "With data file name in the image:" 50*76b90e23SKevin WolfIMGOPTS="data_file=$TEST_IMG.data" _make_test_img 64M 51*76b90e23SKevin Wolf_check_test_img 52*76b90e23SKevin Wolf 53*76b90e23SKevin Wolf$QEMU_IO -c "open $TEST_IMG" -c "read -P 0 0 64k" 2>&1 | _filter_qemu_io | _filter_testdir 54*76b90e23SKevin 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 55*76b90e23SKevin Wolf$QEMU_IO -c "open -odata-file.filename=inexistent $TEST_IMG" -c "read -P 0 0 64k" 2>&1 | _filter_qemu_io | _filter_testdir 56*76b90e23SKevin Wolf 57*76b90e23SKevin Wolfecho 58*76b90e23SKevin Wolfecho "Data file required, but without data file name in the image:" 59*76b90e23SKevin Wolf$QEMU_IMG amend -odata_file= $TEST_IMG 60*76b90e23SKevin Wolf 61*76b90e23SKevin Wolf$QEMU_IO -c "open $TEST_IMG" -c "read -P 0 0 64k" 2>&1 | _filter_qemu_io | _filter_testdir 62*76b90e23SKevin 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 63*76b90e23SKevin Wolf$QEMU_IO -c "open -odata-file.filename=inexistent $TEST_IMG" -c "read -P 0 0 64k" 2>&1 | _filter_qemu_io | _filter_testdir 64*76b90e23SKevin Wolf 65*76b90e23SKevin Wolfecho 66*76b90e23SKevin Wolfecho "Setting data-file for an image with internal data:" 67*76b90e23SKevin Wolf_make_test_img 64M 68*76b90e23SKevin Wolf 69*76b90e23SKevin 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 70*76b90e23SKevin Wolf$QEMU_IO -c "open -odata-file.filename=inexistent $TEST_IMG" -c "read -P 0 0 64k" 2>&1 | _filter_qemu_io | _filter_testdir 71*76b90e23SKevin Wolf 72*76b90e23SKevin Wolfecho 73*76b90e23SKevin Wolfecho "=== Conflicting features ===" 74*76b90e23SKevin Wolfecho 75*76b90e23SKevin Wolf 76*76b90e23SKevin Wolfecho "Convert to compressed target with data file:" 77*76b90e23SKevin WolfTEST_IMG="$TEST_IMG.src" _make_test_img 64M 78*76b90e23SKevin Wolf 79*76b90e23SKevin Wolf$QEMU_IO -c 'write -P 0x11 0 1M' \ 80*76b90e23SKevin Wolf -f $IMGFMT "$TEST_IMG.src" | 81*76b90e23SKevin Wolf _filter_qemu_io 82*76b90e23SKevin Wolf 83*76b90e23SKevin Wolf$QEMU_IMG convert -f $IMGFMT -O $IMGFMT -c -odata_file="$TEST_IMG.data" \ 84*76b90e23SKevin Wolf "$TEST_IMG.src" "$TEST_IMG" 85*76b90e23SKevin Wolf 86*76b90e23SKevin Wolfecho 87*76b90e23SKevin Wolfecho "Convert uncompressed, then write compressed data manually:" 88*76b90e23SKevin Wolf$QEMU_IMG convert -f $IMGFMT -O $IMGFMT -odata_file="$TEST_IMG.data" \ 89*76b90e23SKevin Wolf "$TEST_IMG.src" "$TEST_IMG" 90*76b90e23SKevin Wolf$QEMU_IMG compare "$TEST_IMG.src" "$TEST_IMG" 91*76b90e23SKevin Wolf 92*76b90e23SKevin Wolf$QEMU_IO -c 'write -c -P 0x22 0 1M' \ 93*76b90e23SKevin Wolf -f $IMGFMT "$TEST_IMG" | 94*76b90e23SKevin Wolf _filter_qemu_io 95*76b90e23SKevin Wolf_check_test_img 96*76b90e23SKevin Wolf 97*76b90e23SKevin Wolfecho 98*76b90e23SKevin Wolfecho "Take an internal snapshot:" 99*76b90e23SKevin Wolf 100*76b90e23SKevin Wolf$QEMU_IMG snapshot -c test "$TEST_IMG" 101*76b90e23SKevin Wolf_check_test_img 102*76b90e23SKevin Wolf 103*76b90e23SKevin Wolfecho 104*76b90e23SKevin Wolfecho "=== Standalone image with external data file (efficient) ===" 105*76b90e23SKevin Wolfecho 106*76b90e23SKevin Wolf 107*76b90e23SKevin WolfIMGOPTS="data_file=$TEST_IMG.data" _make_test_img 64M 108*76b90e23SKevin Wolf 109*76b90e23SKevin Wolfecho -n "qcow2 file size before I/O: " 110*76b90e23SKevin Wolfdu -b $TEST_IMG | cut -f1 111*76b90e23SKevin Wolf 112*76b90e23SKevin Wolf# Create image with the following layout 113*76b90e23SKevin Wolf# 0-1 MB: Unallocated 114*76b90e23SKevin Wolf# 1-2 MB: Written (pattern 0x11) 115*76b90e23SKevin Wolf# 2-3 MB: Discarded 116*76b90e23SKevin Wolf# 3-4 MB: Zero write over discarded space 117*76b90e23SKevin Wolf# 4-5 MB: Zero write over written space 118*76b90e23SKevin Wolf# 5-6 MB: Zero write over unallocated space 119*76b90e23SKevin Wolf 120*76b90e23SKevin Wolfecho 121*76b90e23SKevin Wolf$QEMU_IO -c 'write -P 0x11 1M 4M' \ 122*76b90e23SKevin Wolf -c 'discard 2M 2M' \ 123*76b90e23SKevin Wolf -c 'write -z 3M 3M' \ 124*76b90e23SKevin Wolf -f $IMGFMT "$TEST_IMG" | 125*76b90e23SKevin Wolf _filter_qemu_io 126*76b90e23SKevin Wolf_check_test_img 127*76b90e23SKevin Wolf 128*76b90e23SKevin Wolfecho 129*76b90e23SKevin Wolf$QEMU_IMG map --output=json "$TEST_IMG" 130*76b90e23SKevin Wolf 131*76b90e23SKevin Wolfecho 132*76b90e23SKevin Wolf$QEMU_IO -c 'read -P 0 0 1M' \ 133*76b90e23SKevin Wolf -c 'read -P 0x11 1M 1M' \ 134*76b90e23SKevin Wolf -c 'read -P 0 2M 4M' \ 135*76b90e23SKevin Wolf -f $IMGFMT "$TEST_IMG" | 136*76b90e23SKevin Wolf _filter_qemu_io 137*76b90e23SKevin Wolf 138*76b90e23SKevin Wolf# Zero clusters are only marked as such in the qcow2 metadata, but contain 139*76b90e23SKevin Wolf# stale data in the external data file 140*76b90e23SKevin Wolfecho 141*76b90e23SKevin Wolf$QEMU_IO -c 'read -P 0 0 1M' \ 142*76b90e23SKevin Wolf -c 'read -P 0x11 1M 1M' \ 143*76b90e23SKevin Wolf -c 'read -P 0 2M 2M' \ 144*76b90e23SKevin Wolf -c 'read -P 0x11 4M 1M' \ 145*76b90e23SKevin Wolf -c 'read -P 0 5M 1M' \ 146*76b90e23SKevin Wolf -f raw "$TEST_IMG.data" | 147*76b90e23SKevin Wolf _filter_qemu_io 148*76b90e23SKevin Wolf 149*76b90e23SKevin Wolf 150*76b90e23SKevin Wolfecho -n "qcow2 file size after I/O: " 151*76b90e23SKevin Wolfdu -b $TEST_IMG | cut -f1 152*76b90e23SKevin Wolf 153*76b90e23SKevin Wolfecho 154*76b90e23SKevin Wolfecho "=== Standalone image with external data file (valid raw) ===" 155*76b90e23SKevin Wolfecho 156*76b90e23SKevin Wolf 157*76b90e23SKevin WolfIMGOPTS="data_file=$TEST_IMG.data,data_file_raw=on" _make_test_img 64M 158*76b90e23SKevin Wolf 159*76b90e23SKevin Wolfecho -n "qcow2 file size before I/O: " 160*76b90e23SKevin Wolfdu -b $TEST_IMG | cut -f1 161*76b90e23SKevin Wolf 162*76b90e23SKevin Wolfecho 163*76b90e23SKevin Wolf$QEMU_IO -c 'write -P 0x11 1M 4M' \ 164*76b90e23SKevin Wolf -c 'discard 2M 2M' \ 165*76b90e23SKevin Wolf -c 'write -z 3M 3M' \ 166*76b90e23SKevin Wolf -f $IMGFMT "$TEST_IMG" | 167*76b90e23SKevin Wolf _filter_qemu_io 168*76b90e23SKevin Wolf_check_test_img 169*76b90e23SKevin Wolf 170*76b90e23SKevin Wolfecho 171*76b90e23SKevin Wolf$QEMU_IMG map --output=json "$TEST_IMG" 172*76b90e23SKevin Wolf 173*76b90e23SKevin Wolfecho 174*76b90e23SKevin Wolf$QEMU_IO -c 'read -P 0 0 1M' \ 175*76b90e23SKevin Wolf -c 'read -P 0x11 1M 1M' \ 176*76b90e23SKevin Wolf -c 'read -P 0 2M 4M' \ 177*76b90e23SKevin Wolf -f $IMGFMT "$TEST_IMG" | 178*76b90e23SKevin Wolf _filter_qemu_io 179*76b90e23SKevin Wolf 180*76b90e23SKevin Wolfecho 181*76b90e23SKevin Wolf$QEMU_IMG compare "$TEST_IMG" "$TEST_IMG.data" 182*76b90e23SKevin Wolf 183*76b90e23SKevin Wolfecho -n "qcow2 file size after I/O: " 184*76b90e23SKevin Wolfdu -b $TEST_IMG | cut -f1 185*76b90e23SKevin Wolf 186*76b90e23SKevin Wolfecho 187*76b90e23SKevin Wolfecho "=== bdrv_co_block_status test for file and offset=0 ===" 188*76b90e23SKevin Wolfecho 189*76b90e23SKevin Wolf 190*76b90e23SKevin WolfIMGOPTS="data_file=$TEST_IMG.data" _make_test_img 64M 191*76b90e23SKevin Wolf 192*76b90e23SKevin Wolf$QEMU_IO -c 'write -P 0x11 0 1M' -f $IMGFMT "$TEST_IMG" | _filter_qemu_io 193*76b90e23SKevin Wolf$QEMU_IO -c 'read -P 0x11 0 1M' -f $IMGFMT "$TEST_IMG" | _filter_qemu_io 194*76b90e23SKevin Wolf$QEMU_IMG map --output=human "$TEST_IMG" | _filter_testdir 195*76b90e23SKevin Wolf$QEMU_IMG map --output=json "$TEST_IMG" 196*76b90e23SKevin Wolf 197*76b90e23SKevin Wolf# success, all done 198*76b90e23SKevin Wolfecho "*** done" 199*76b90e23SKevin Wolfrm -f $seq.full 200*76b90e23SKevin Wolfstatus=0 201