111a82d14SPhilippe Mathieu-Daudé#!/usr/bin/env bash 2975a93c0SMax Reitz# 3aa93c834SMax Reitz# Test case for preallocated zero clusters in qcow2 4975a93c0SMax Reitz# 5975a93c0SMax Reitz# Copyright (C) 2013 Red Hat, Inc. 6975a93c0SMax Reitz# 7975a93c0SMax Reitz# This program is free software; you can redistribute it and/or modify 8975a93c0SMax Reitz# it under the terms of the GNU General Public License as published by 9975a93c0SMax Reitz# the Free Software Foundation; either version 2 of the License, or 10975a93c0SMax Reitz# (at your option) any later version. 11975a93c0SMax Reitz# 12975a93c0SMax Reitz# This program is distributed in the hope that it will be useful, 13975a93c0SMax Reitz# but WITHOUT ANY WARRANTY; without even the implied warranty of 14975a93c0SMax Reitz# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15975a93c0SMax Reitz# GNU General Public License for more details. 16975a93c0SMax Reitz# 17975a93c0SMax Reitz# You should have received a copy of the GNU General Public License 18975a93c0SMax Reitz# along with this program. If not, see <http://www.gnu.org/licenses/>. 19975a93c0SMax Reitz# 20975a93c0SMax Reitz 21975a93c0SMax Reitz# creator 22975a93c0SMax Reitzowner=mreitz@redhat.com 23975a93c0SMax Reitz 24975a93c0SMax Reitzseq="$(basename $0)" 25975a93c0SMax Reitzecho "QA output created by $seq" 26975a93c0SMax Reitz 27975a93c0SMax Reitzstatus=1 # failure is the default! 28975a93c0SMax Reitz 29975a93c0SMax Reitz_cleanup() 30975a93c0SMax Reitz{ 31975a93c0SMax Reitz _cleanup_test_img 32975a93c0SMax Reitz} 33975a93c0SMax Reitztrap "_cleanup; exit \$status" 0 1 2 3 15 34975a93c0SMax Reitz 35975a93c0SMax Reitz# get standard environment, filters and checks 36975a93c0SMax Reitz. ./common.rc 37975a93c0SMax Reitz. ./common.filter 38975a93c0SMax Reitz 39e696f335SMax Reitz# This tests qcow2-specific low-level functionality 40975a93c0SMax Reitz_supported_fmt qcow2 41975a93c0SMax Reitz_supported_proto generic 42b043b07cSMax Reitz# We need zero clusters and snapshots 43*3be2024aSMax Reitz# (TODO: Consider splitting the snapshot part into a separate test 44*3be2024aSMax Reitz# file, so this one runs with refcount_bits=1 and data_file) 45*3be2024aSMax Reitz_unsupported_imgopts 'compat=0.10' 'refcount_bits=1[^0-9]' data_file 46975a93c0SMax Reitz 47048c5fd1SEric Blake# Intentionally create an unaligned image 48048c5fd1SEric BlakeIMG_SIZE=$((64 * 1024 * 1024 + 512)) 49975a93c0SMax Reitz 50975a93c0SMax Reitzecho 51048c5fd1SEric Blakeecho "=== Testing cluster discards ===" 52975a93c0SMax Reitzecho 53975a93c0SMax Reitz_make_test_img $IMG_SIZE 54048c5fd1SEric Blake# Write some normal clusters, zero some of them (creating preallocated 55048c5fd1SEric Blake# zero clusters) and discard everything. Everything should now read as 0. 56048c5fd1SEric Blake$QEMU_IO -c "write 0 256k" -c "write -z 0 256k" -c "write 64M 512" \ 57048c5fd1SEric Blake -c "discard 0 $IMG_SIZE" -c "read -P 0 0 $IMG_SIZE" "$TEST_IMG" \ 58975a93c0SMax Reitz | _filter_qemu_io 59aa93c834SMax Reitz 60975a93c0SMax Reitz# Check the image (there shouldn't be any leaks) 61975a93c0SMax Reitz_check_test_img 62aa93c834SMax Reitz# Map the image (we want all clusters to be gone) 63aa93c834SMax Reitz$QEMU_IMG map "$TEST_IMG" 64aa93c834SMax Reitz 65aa93c834SMax Reitz_cleanup_test_img 66aa93c834SMax Reitz 67aa93c834SMax Reitz 68aa93c834SMax Reitzecho 69aa93c834SMax Reitzecho '=== Writing to preallocated zero clusters ===' 70aa93c834SMax Reitzecho 71aa93c834SMax Reitz 72aa93c834SMax Reitz_make_test_img $IMG_SIZE 73aa93c834SMax Reitz 74aa93c834SMax Reitz# Create data clusters (not aligned to an L2 table) 75aa93c834SMax Reitz$QEMU_IO -c 'write -P 42 1M 256k' "$TEST_IMG" | _filter_qemu_io 76aa93c834SMax Reitzorig_map=$($QEMU_IMG map --output=json "$TEST_IMG") 77aa93c834SMax Reitz 78aa93c834SMax Reitz# Convert the data clusters to preallocated zero clusters 79aa93c834SMax Reitz$QEMU_IO -c 'write -z 1M 256k' "$TEST_IMG" | _filter_qemu_io 80aa93c834SMax Reitz 81aa93c834SMax Reitz# Now write to them (with a COW needed for the head and tail) 82aa93c834SMax Reitz$QEMU_IO -c "write -P 23 $(((1024 + 32) * 1024)) 192k" "$TEST_IMG" \ 83aa93c834SMax Reitz | _filter_qemu_io 84aa93c834SMax Reitz 85aa93c834SMax Reitz# Check metadata correctness 86aa93c834SMax Reitz_check_test_img 87aa93c834SMax Reitz 88aa93c834SMax Reitz# Check data correctness 89aa93c834SMax Reitz$QEMU_IO -c "read -P 0 $(( 1024 * 1024)) 32k" \ 90aa93c834SMax Reitz -c "read -P 23 $(((1024 + 32) * 1024)) 192k" \ 91aa93c834SMax Reitz -c "read -P 0 $(((1024 + 32 + 192) * 1024)) 32k" \ 92aa93c834SMax Reitz "$TEST_IMG" \ 93aa93c834SMax Reitz | _filter_qemu_io 94aa93c834SMax Reitz 95aa93c834SMax Reitz# Check that we have actually reused the original area 96aa93c834SMax Reitznew_map=$($QEMU_IMG map --output=json "$TEST_IMG") 97aa93c834SMax Reitzif [ "$new_map" = "$orig_map" ]; then 98aa93c834SMax Reitz echo 'Successfully reused original clusters.' 99aa93c834SMax Reitzelse 100aa93c834SMax Reitz echo 'Failed to reuse original clusters.' 101aa93c834SMax Reitz echo 'Original map:' 102aa93c834SMax Reitz echo "$orig_map" 103aa93c834SMax Reitz echo 'New map:' 104aa93c834SMax Reitz echo "$new_map" 105aa93c834SMax Reitzfi 106aa93c834SMax Reitz 107aa93c834SMax Reitz_cleanup_test_img 108aa93c834SMax Reitz 109aa93c834SMax Reitz 110aa93c834SMax Reitzecho 111aa93c834SMax Reitzecho '=== Writing to a snapshotted preallocated zero cluster ===' 112aa93c834SMax Reitzecho 113aa93c834SMax Reitz 114aa93c834SMax Reitz_make_test_img 64k 115aa93c834SMax Reitz 116aa93c834SMax Reitz# Create a preallocated zero cluster 117aa93c834SMax Reitz$QEMU_IO -c 'write -P 42 0 64k' -c 'write -z 0 64k' "$TEST_IMG" \ 118aa93c834SMax Reitz | _filter_qemu_io 119aa93c834SMax Reitz 120aa93c834SMax Reitz# Snapshot it 121aa93c834SMax Reitz$QEMU_IMG snapshot -c foo "$TEST_IMG" 122aa93c834SMax Reitz 123aa93c834SMax Reitz# Write to the cluster 124aa93c834SMax Reitz$QEMU_IO -c 'write -P 23 0 64k' "$TEST_IMG" | _filter_qemu_io 125aa93c834SMax Reitz 126aa93c834SMax Reitz# Check metadata correctness 127aa93c834SMax Reitz_check_test_img 128aa93c834SMax Reitz 129aa93c834SMax Reitz# Check data correctness 130aa93c834SMax Reitz$QEMU_IO -c 'read -P 23 0 64k' "$TEST_IMG" | _filter_qemu_io 131aa93c834SMax Reitz$QEMU_IMG snapshot -a foo "$TEST_IMG" 132aa93c834SMax Reitz$QEMU_IO -c 'read -P 0 0 64k' "$TEST_IMG" | _filter_qemu_io 133aa93c834SMax Reitz 134aa93c834SMax Reitz_cleanup_test_img 135aa93c834SMax Reitz 136aa93c834SMax Reitz 137aa93c834SMax Reitzecho 138aa93c834SMax Reitzecho '=== Consecutive write to a preallocated zero cluster ===' 139aa93c834SMax Reitzecho 140aa93c834SMax Reitz 141aa93c834SMax Reitz_make_test_img 192k 142aa93c834SMax Reitz 143aa93c834SMax Reitz# Create three normal clusters 144aa93c834SMax Reitz$QEMU_IO -c 'write -P 42 0 192k' "$TEST_IMG" | _filter_qemu_io 145aa93c834SMax Reitzorig_map=$($QEMU_IMG map --output=json "$TEST_IMG") 146aa93c834SMax Reitz 147aa93c834SMax Reitz# Make the middle cluster a preallocated zero cluster 148aa93c834SMax Reitz$QEMU_IO -c 'write -z 64k 64k' "$TEST_IMG" | _filter_qemu_io 149aa93c834SMax Reitz 150aa93c834SMax Reitz# Try to overwrite everything: This should reuse the whole range. To test that 151aa93c834SMax Reitz# this only issues a single continuous write request, use blkdebug. 152aa93c834SMax Reitz$QEMU_IO -c 'write -P 42 0 192k' \ 153aa93c834SMax Reitz "json:{ 154aa93c834SMax Reitz 'driver': '$IMGFMT', 155aa93c834SMax Reitz 'file': { 156aa93c834SMax Reitz 'driver': 'blkdebug', 157aa93c834SMax Reitz 'image.filename': '$TEST_IMG', 158aa93c834SMax Reitz 'set-state': [{ 159aa93c834SMax Reitz 'event': 'write_aio', 160aa93c834SMax Reitz 'new_state': 2 161aa93c834SMax Reitz }], 162aa93c834SMax Reitz 'inject-error': [{ 163aa93c834SMax Reitz 'event': 'write_aio', 164aa93c834SMax Reitz 'state': 2 165aa93c834SMax Reitz }] 166aa93c834SMax Reitz } 167aa93c834SMax Reitz }" \ 168aa93c834SMax Reitz | _filter_qemu_io 169aa93c834SMax Reitz 170aa93c834SMax Reitz# Check metadata correctness 171aa93c834SMax Reitz_check_test_img 172aa93c834SMax Reitz 173aa93c834SMax Reitz# Check that we have actually reused the original area 174aa93c834SMax Reitznew_map=$($QEMU_IMG map --output=json "$TEST_IMG") 175aa93c834SMax Reitzif [ "$new_map" = "$orig_map" ]; then 176aa93c834SMax Reitz echo 'Successfully reused original clusters.' 177aa93c834SMax Reitzelse 178aa93c834SMax Reitz echo 'Failed to reuse original clusters.' 179aa93c834SMax Reitz echo 'Original map:' 180aa93c834SMax Reitz echo "$orig_map" 181aa93c834SMax Reitz echo 'New map:' 182aa93c834SMax Reitz echo "$new_map" 183aa93c834SMax Reitzfi 184aa93c834SMax Reitz 185aa93c834SMax Reitz_cleanup_test_img 186aa93c834SMax Reitz 187975a93c0SMax Reitz 188975a93c0SMax Reitz# success, all done 189975a93c0SMax Reitzecho "*** done" 190975a93c0SMax Reitzrm -f $seq.full 191975a93c0SMax Reitzstatus=0 192