1d2eed8c6SMax Reitz#!/bin/bash 2d2eed8c6SMax Reitz# 3d2eed8c6SMax Reitz# Test cases for different refcount_bits values 4d2eed8c6SMax Reitz# 5d2eed8c6SMax Reitz# Copyright (C) 2015 Red Hat, Inc. 6d2eed8c6SMax Reitz# 7d2eed8c6SMax Reitz# This program is free software; you can redistribute it and/or modify 8d2eed8c6SMax Reitz# it under the terms of the GNU General Public License as published by 9d2eed8c6SMax Reitz# the Free Software Foundation; either version 2 of the License, or 10d2eed8c6SMax Reitz# (at your option) any later version. 11d2eed8c6SMax Reitz# 12d2eed8c6SMax Reitz# This program is distributed in the hope that it will be useful, 13d2eed8c6SMax Reitz# but WITHOUT ANY WARRANTY; without even the implied warranty of 14d2eed8c6SMax Reitz# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15d2eed8c6SMax Reitz# GNU General Public License for more details. 16d2eed8c6SMax Reitz# 17d2eed8c6SMax Reitz# You should have received a copy of the GNU General Public License 18d2eed8c6SMax Reitz# along with this program. If not, see <http://www.gnu.org/licenses/>. 19d2eed8c6SMax Reitz# 20d2eed8c6SMax Reitz 21d2eed8c6SMax Reitz# creator 22d2eed8c6SMax Reitzowner=mreitz@redhat.com 23d2eed8c6SMax Reitz 24d2eed8c6SMax Reitzseq="$(basename $0)" 25d2eed8c6SMax Reitzecho "QA output created by $seq" 26d2eed8c6SMax Reitz 27d2eed8c6SMax Reitzhere="$PWD" 28d2eed8c6SMax Reitztmp=/tmp/$$ 29d2eed8c6SMax Reitzstatus=1 # failure is the default! 30d2eed8c6SMax Reitz 31d2eed8c6SMax Reitz_cleanup() 32d2eed8c6SMax Reitz{ 33d2eed8c6SMax Reitz _cleanup_test_img 34d2eed8c6SMax Reitz} 35d2eed8c6SMax Reitztrap "_cleanup; exit \$status" 0 1 2 3 15 36d2eed8c6SMax Reitz 37d2eed8c6SMax Reitz# get standard environment, filters and checks 38d2eed8c6SMax Reitz. ./common.rc 39d2eed8c6SMax Reitz. ./common.filter 40d2eed8c6SMax Reitz 41d2eed8c6SMax Reitz# This tests qcow2-specific low-level functionality 42d2eed8c6SMax Reitz_supported_fmt qcow2 43d2eed8c6SMax Reitz_supported_proto file 44d2eed8c6SMax Reitz_supported_os Linux 45d2eed8c6SMax Reitz# This test will set refcount_bits on its own which would conflict with the 46d2eed8c6SMax Reitz# manual setting; compat will be overridden as well 47d2eed8c6SMax Reitz_unsupported_imgopts refcount_bits 'compat=0.10' 48d2eed8c6SMax Reitz 49d2eed8c6SMax Reitzfunction print_refcount_bits() 50d2eed8c6SMax Reitz{ 51d2eed8c6SMax Reitz $QEMU_IMG info "$TEST_IMG" | sed -n '/refcount bits:/ s/^ *//p' 52d2eed8c6SMax Reitz} 53d2eed8c6SMax Reitz 54d2eed8c6SMax Reitzecho 55d2eed8c6SMax Reitzecho '=== refcount_bits limits ===' 56d2eed8c6SMax Reitzecho 57d2eed8c6SMax Reitz 58d2eed8c6SMax Reitz# Must be positive (non-zero) 59d2eed8c6SMax ReitzIMGOPTS="$IMGOPTS,refcount_bits=0" _make_test_img 64M 60d2eed8c6SMax Reitz# Must be positive (non-negative) 61d2eed8c6SMax ReitzIMGOPTS="$IMGOPTS,refcount_bits=-1" _make_test_img 64M 62d2eed8c6SMax Reitz# May not exceed 64 63d2eed8c6SMax ReitzIMGOPTS="$IMGOPTS,refcount_bits=128" _make_test_img 64M 64d2eed8c6SMax Reitz# Must be a power of two 65d2eed8c6SMax ReitzIMGOPTS="$IMGOPTS,refcount_bits=42" _make_test_img 64M 66d2eed8c6SMax Reitz 67d2eed8c6SMax Reitz# 1 is the minimum 68d2eed8c6SMax ReitzIMGOPTS="$IMGOPTS,refcount_bits=1" _make_test_img 64M 69d2eed8c6SMax Reitzprint_refcount_bits 70d2eed8c6SMax Reitz 71d2eed8c6SMax Reitz# 64 is the maximum 72d2eed8c6SMax ReitzIMGOPTS="$IMGOPTS,refcount_bits=64" _make_test_img 64M 73d2eed8c6SMax Reitzprint_refcount_bits 74d2eed8c6SMax Reitz 75d2eed8c6SMax Reitz# 16 is the default 76d2eed8c6SMax Reitz_make_test_img 64M 77d2eed8c6SMax Reitzprint_refcount_bits 78d2eed8c6SMax Reitz 79d2eed8c6SMax Reitzecho 80d2eed8c6SMax Reitzecho '=== refcount_bits and compat=0.10 ===' 81d2eed8c6SMax Reitzecho 82d2eed8c6SMax Reitz 83d2eed8c6SMax Reitz# Should work 84d2eed8c6SMax ReitzIMGOPTS="$IMGOPTS,compat=0.10,refcount_bits=16" _make_test_img 64M 85d2eed8c6SMax Reitzprint_refcount_bits 86d2eed8c6SMax Reitz 87d2eed8c6SMax Reitz# Should not work 88d2eed8c6SMax ReitzIMGOPTS="$IMGOPTS,compat=0.10,refcount_bits=1" _make_test_img 64M 89d2eed8c6SMax ReitzIMGOPTS="$IMGOPTS,compat=0.10,refcount_bits=64" _make_test_img 64M 90d2eed8c6SMax Reitz 91d2eed8c6SMax Reitz 92d2eed8c6SMax Reitzecho 93d2eed8c6SMax Reitzecho '=== Snapshot limit on refcount_bits=1 ===' 94d2eed8c6SMax Reitzecho 95d2eed8c6SMax Reitz 96d2eed8c6SMax ReitzIMGOPTS="$IMGOPTS,refcount_bits=1" _make_test_img 64M 97d2eed8c6SMax Reitzprint_refcount_bits 98d2eed8c6SMax Reitz 99d2eed8c6SMax Reitz$QEMU_IO -c 'write 0 512' "$TEST_IMG" | _filter_qemu_io 100d2eed8c6SMax Reitz 101d2eed8c6SMax Reitz# Should fail for now; in the future, this might be supported by automatically 102d2eed8c6SMax Reitz# copying all clusters with overflowing refcount 103d2eed8c6SMax Reitz$QEMU_IMG snapshot -c foo "$TEST_IMG" 104d2eed8c6SMax Reitz 105d2eed8c6SMax Reitz# The new L1 table could/should be leaked 106d2eed8c6SMax Reitz_check_test_img 107d2eed8c6SMax Reitz 108d2eed8c6SMax Reitzecho 109d2eed8c6SMax Reitzecho '=== Snapshot limit on refcount_bits=2 ===' 110d2eed8c6SMax Reitzecho 111d2eed8c6SMax Reitz 112d2eed8c6SMax ReitzIMGOPTS="$IMGOPTS,refcount_bits=2" _make_test_img 64M 113d2eed8c6SMax Reitzprint_refcount_bits 114d2eed8c6SMax Reitz 115d2eed8c6SMax Reitz$QEMU_IO -c 'write 0 512' "$TEST_IMG" | _filter_qemu_io 116d2eed8c6SMax Reitz 117d2eed8c6SMax Reitz# Should succeed 118d2eed8c6SMax Reitz$QEMU_IMG snapshot -c foo "$TEST_IMG" 119d2eed8c6SMax Reitz$QEMU_IMG snapshot -c bar "$TEST_IMG" 120d2eed8c6SMax Reitz# Should fail (4th reference) 121d2eed8c6SMax Reitz$QEMU_IMG snapshot -c baz "$TEST_IMG" 122d2eed8c6SMax Reitz 123d2eed8c6SMax Reitz# The new L1 table could/should be leaked 124d2eed8c6SMax Reitz_check_test_img 125d2eed8c6SMax Reitz 126d2eed8c6SMax Reitzecho 127d2eed8c6SMax Reitzecho '=== Compressed clusters with refcount_bits=1 ===' 128d2eed8c6SMax Reitzecho 129d2eed8c6SMax Reitz 130d2eed8c6SMax ReitzIMGOPTS="$IMGOPTS,refcount_bits=1" _make_test_img 64M 131d2eed8c6SMax Reitzprint_refcount_bits 132d2eed8c6SMax Reitz 133d2eed8c6SMax Reitz# Both should fit into a single host cluster; instead of failing to increase the 134d2eed8c6SMax Reitz# refcount of that cluster, qemu should just allocate a new cluster and make 135d2eed8c6SMax Reitz# this operation succeed 136d2eed8c6SMax Reitz$QEMU_IO -c 'write -P 0 -c 0 64k' \ 137d2eed8c6SMax Reitz -c 'write -P 1 -c 64k 64k' \ 138d2eed8c6SMax Reitz "$TEST_IMG" | _filter_qemu_io 139d2eed8c6SMax Reitz 140d2eed8c6SMax Reitz_check_test_img 141d2eed8c6SMax Reitz 142d2eed8c6SMax Reitzecho 143d2eed8c6SMax Reitzecho '=== MSb set in 64 bit refcount ===' 144d2eed8c6SMax Reitzecho 145d2eed8c6SMax Reitz 146d2eed8c6SMax ReitzIMGOPTS="$IMGOPTS,refcount_bits=64" _make_test_img 64M 147d2eed8c6SMax Reitzprint_refcount_bits 148d2eed8c6SMax Reitz 149d2eed8c6SMax Reitz$QEMU_IO -c 'write 0 512' "$TEST_IMG" | _filter_qemu_io 150d2eed8c6SMax Reitz 151d2eed8c6SMax Reitz# Set the MSb in the refblock entry of the data cluster 152d2eed8c6SMax Reitzpoke_file "$TEST_IMG" $((0x20028)) "\x80\x00\x00\x00\x00\x00\x00\x00" 153d2eed8c6SMax Reitz 154d2eed8c6SMax Reitz# Clear OFLAG_COPIED in the L2 entry of the data cluster 155d2eed8c6SMax Reitzpoke_file "$TEST_IMG" $((0x40000)) "\x00\x00\x00\x00\x00\x05\x00\x00" 156d2eed8c6SMax Reitz 157d2eed8c6SMax Reitz# Try to write to that cluster (should work, even though the MSb is set) 158d2eed8c6SMax Reitz$QEMU_IO -c 'write 0 512' "$TEST_IMG" | _filter_qemu_io 159d2eed8c6SMax Reitz 160d2eed8c6SMax Reitzecho 161d2eed8c6SMax Reitzecho '=== Snapshot on maximum 64 bit refcount value ===' 162d2eed8c6SMax Reitzecho 163d2eed8c6SMax Reitz 164d2eed8c6SMax ReitzIMGOPTS="$IMGOPTS,refcount_bits=64" _make_test_img 64M 165d2eed8c6SMax Reitzprint_refcount_bits 166d2eed8c6SMax Reitz 167d2eed8c6SMax Reitz$QEMU_IO -c 'write 0 512' "$TEST_IMG" | _filter_qemu_io 168d2eed8c6SMax Reitz 169d2eed8c6SMax Reitz# Set the refblock entry to the maximum value possible 170d2eed8c6SMax Reitzpoke_file "$TEST_IMG" $((0x20028)) "\xff\xff\xff\xff\xff\xff\xff\xff" 171d2eed8c6SMax Reitz 172d2eed8c6SMax Reitz# Clear OFLAG_COPIED in the L2 entry of the data cluster 173d2eed8c6SMax Reitzpoke_file "$TEST_IMG" $((0x40000)) "\x00\x00\x00\x00\x00\x05\x00\x00" 174d2eed8c6SMax Reitz 175d2eed8c6SMax Reitz# Try a snapshot (should correctly identify the overflow; may work in the future 176d2eed8c6SMax Reitz# by falling back to COW) 177d2eed8c6SMax Reitz$QEMU_IMG snapshot -c foo "$TEST_IMG" 178d2eed8c6SMax Reitz 179d2eed8c6SMax Reitz# The new L1 table could/should be leaked; and obviously the data cluster is 180d2eed8c6SMax Reitz# leaked (refcount=UINT64_MAX reference=1) 181d2eed8c6SMax Reitz_check_test_img 182d2eed8c6SMax Reitz 183*e9dbdc5eSMax Reitzecho 184*e9dbdc5eSMax Reitzecho '=== Amend from refcount_bits=16 to refcount_bits=1 ===' 185*e9dbdc5eSMax Reitzecho 186*e9dbdc5eSMax Reitz 187*e9dbdc5eSMax Reitz_make_test_img 64M 188*e9dbdc5eSMax Reitzprint_refcount_bits 189*e9dbdc5eSMax Reitz 190*e9dbdc5eSMax Reitz$QEMU_IO -c 'write 16M 32M' "$TEST_IMG" | _filter_qemu_io 191*e9dbdc5eSMax Reitz$QEMU_IMG amend -o refcount_bits=1 "$TEST_IMG" 192*e9dbdc5eSMax Reitz_check_test_img 193*e9dbdc5eSMax Reitzprint_refcount_bits 194*e9dbdc5eSMax Reitz 195*e9dbdc5eSMax Reitzecho 196*e9dbdc5eSMax Reitzecho '=== Amend from refcount_bits=1 to refcount_bits=64 ===' 197*e9dbdc5eSMax Reitzecho 198*e9dbdc5eSMax Reitz 199*e9dbdc5eSMax Reitz$QEMU_IMG amend -o refcount_bits=64 "$TEST_IMG" 200*e9dbdc5eSMax Reitz_check_test_img 201*e9dbdc5eSMax Reitzprint_refcount_bits 202*e9dbdc5eSMax Reitz 203*e9dbdc5eSMax Reitzecho 204*e9dbdc5eSMax Reitzecho '=== Amend to compat=0.10 ===' 205*e9dbdc5eSMax Reitzecho 206*e9dbdc5eSMax Reitz 207*e9dbdc5eSMax Reitz# Should not work because refcount_bits needs to be 16 for compat=0.10 208*e9dbdc5eSMax Reitz$QEMU_IMG amend -o compat=0.10 "$TEST_IMG" 209*e9dbdc5eSMax Reitzprint_refcount_bits 210*e9dbdc5eSMax Reitz# Should work 211*e9dbdc5eSMax Reitz$QEMU_IMG amend -o compat=0.10,refcount_bits=16 "$TEST_IMG" 212*e9dbdc5eSMax Reitz_check_test_img 213*e9dbdc5eSMax Reitzprint_refcount_bits 214*e9dbdc5eSMax Reitz 215*e9dbdc5eSMax Reitz# Get back to compat=1.1 and refcount_bits=16 216*e9dbdc5eSMax Reitz$QEMU_IMG amend -o compat=1.1 "$TEST_IMG" 217*e9dbdc5eSMax Reitzprint_refcount_bits 218*e9dbdc5eSMax Reitz# Should not work 219*e9dbdc5eSMax Reitz$QEMU_IMG amend -o refcount_bits=32,compat=0.10 "$TEST_IMG" 220*e9dbdc5eSMax Reitzprint_refcount_bits 221*e9dbdc5eSMax Reitz 222*e9dbdc5eSMax Reitzecho 223*e9dbdc5eSMax Reitzecho '=== Amend with snapshot ===' 224*e9dbdc5eSMax Reitzecho 225*e9dbdc5eSMax Reitz 226*e9dbdc5eSMax Reitz$QEMU_IMG snapshot -c foo "$TEST_IMG" 227*e9dbdc5eSMax Reitz# Just to have different refcounts across the image 228*e9dbdc5eSMax Reitz$QEMU_IO -c 'write 0 16M' "$TEST_IMG" | _filter_qemu_io 229*e9dbdc5eSMax Reitz 230*e9dbdc5eSMax Reitz# Should not work (may work in the future by first decreasing all refcounts so 231*e9dbdc5eSMax Reitz# they fit into the target range by copying them) 232*e9dbdc5eSMax Reitz$QEMU_IMG amend -o refcount_bits=1 "$TEST_IMG" 233*e9dbdc5eSMax Reitz_check_test_img 234*e9dbdc5eSMax Reitzprint_refcount_bits 235*e9dbdc5eSMax Reitz 236*e9dbdc5eSMax Reitz# Should work 237*e9dbdc5eSMax Reitz$QEMU_IMG amend -o refcount_bits=2 "$TEST_IMG" 238*e9dbdc5eSMax Reitz_check_test_img 239*e9dbdc5eSMax Reitzprint_refcount_bits 240*e9dbdc5eSMax Reitz 241*e9dbdc5eSMax Reitzecho 242*e9dbdc5eSMax Reitzecho '=== Testing too many references for check ===' 243*e9dbdc5eSMax Reitzecho 244*e9dbdc5eSMax Reitz 245*e9dbdc5eSMax ReitzIMGOPTS="$IMGOPTS,refcount_bits=1" _make_test_img 64M 246*e9dbdc5eSMax Reitzprint_refcount_bits 247*e9dbdc5eSMax Reitz 248*e9dbdc5eSMax Reitz# This cluster should be created at 0x50000 249*e9dbdc5eSMax Reitz$QEMU_IO -c 'write 0 64k' "$TEST_IMG" | _filter_qemu_io 250*e9dbdc5eSMax Reitz# Now make the second L2 entry (the L2 table should be at 0x40000) point to that 251*e9dbdc5eSMax Reitz# cluster, so we have two references 252*e9dbdc5eSMax Reitzpoke_file "$TEST_IMG" $((0x40008)) "\x80\x00\x00\x00\x00\x05\x00\x00" 253*e9dbdc5eSMax Reitz 254*e9dbdc5eSMax Reitz# This should say "please use amend" 255*e9dbdc5eSMax Reitz_check_test_img -r all 256*e9dbdc5eSMax Reitz 257*e9dbdc5eSMax Reitz# So we do that 258*e9dbdc5eSMax Reitz$QEMU_IMG amend -o refcount_bits=2 "$TEST_IMG" 259*e9dbdc5eSMax Reitzprint_refcount_bits 260*e9dbdc5eSMax Reitz 261*e9dbdc5eSMax Reitz# And try again 262*e9dbdc5eSMax Reitz_check_test_img -r all 263*e9dbdc5eSMax Reitz 264*e9dbdc5eSMax Reitzecho 265*e9dbdc5eSMax Reitzecho '=== Multiple walks necessary during amend ===' 266*e9dbdc5eSMax Reitzecho 267*e9dbdc5eSMax Reitz 268*e9dbdc5eSMax ReitzIMGOPTS="$IMGOPTS,refcount_bits=1,cluster_size=512" _make_test_img 64k 269*e9dbdc5eSMax Reitz 270*e9dbdc5eSMax Reitz# Cluster 0 is the image header, clusters 1 to 4 are used by the L1 table, a 271*e9dbdc5eSMax Reitz# single L2 table, the reftable and a single refblock. This creates 58 data 272*e9dbdc5eSMax Reitz# clusters (actually, the L2 table is created here, too), so in total there are 273*e9dbdc5eSMax Reitz# then 63 used clusters in the image. With a refcount width of 64, one refblock 274*e9dbdc5eSMax Reitz# describes 64 clusters (512 bytes / 64 bits/entry = 64 entries), so this will 275*e9dbdc5eSMax Reitz# make the first refblock in the amended image have exactly one free entry. 276*e9dbdc5eSMax Reitz$QEMU_IO -c "write 0 $((58 * 512))" "$TEST_IMG" | _filter_qemu_io 277*e9dbdc5eSMax Reitz 278*e9dbdc5eSMax Reitz# Now change the refcount width; since the first new refblock will have exactly 279*e9dbdc5eSMax Reitz# one free entry, that entry will be used to store its own reference. No other 280*e9dbdc5eSMax Reitz# refblocks are needed, so then the new reftable will be allocated; since the 281*e9dbdc5eSMax Reitz# first new refblock is completely filled up, this will require a new refblock 282*e9dbdc5eSMax Reitz# which is why the refcount width changing function will need to run through 283*e9dbdc5eSMax Reitz# everything one more time until the allocations are stable. 284*e9dbdc5eSMax Reitz# Having more walks than usual should be visible as regressing progress (from 285*e9dbdc5eSMax Reitz# 66.67 % (2/3 walks) to 50.00 % (2/4 walks)). 286*e9dbdc5eSMax Reitz$QEMU_IMG amend -o refcount_bits=64 -p "$TEST_IMG" | tr '\r' '\n' \ 287*e9dbdc5eSMax Reitz | grep -A 1 '66.67' 288*e9dbdc5eSMax Reitzprint_refcount_bits 289*e9dbdc5eSMax Reitz 290*e9dbdc5eSMax Reitz_check_test_img 291*e9dbdc5eSMax Reitz 292d2eed8c6SMax Reitz 293d2eed8c6SMax Reitz# success, all done 294d2eed8c6SMax Reitzecho '*** done' 295d2eed8c6SMax Reitzrm -f $seq.full 296d2eed8c6SMax Reitzstatus=0 297