1f53b25dfSMax Reitz#!/usr/bin/env bash 2f53b25dfSMax Reitz# 3f53b25dfSMax Reitz# Test case for qcow2's handling of extra data in snapshot table entries 4f53b25dfSMax Reitz# (and more generally, how certain cases of broken snapshot tables are 5f53b25dfSMax Reitz# handled) 6f53b25dfSMax Reitz# 7f53b25dfSMax Reitz# Copyright (C) 2019 Red Hat, Inc. 8f53b25dfSMax Reitz# 9f53b25dfSMax Reitz# This program is free software; you can redistribute it and/or modify 10f53b25dfSMax Reitz# it under the terms of the GNU General Public License as published by 11f53b25dfSMax Reitz# the Free Software Foundation; either version 2 of the License, or 12f53b25dfSMax Reitz# (at your option) any later version. 13f53b25dfSMax Reitz# 14f53b25dfSMax Reitz# This program is distributed in the hope that it will be useful, 15f53b25dfSMax Reitz# but WITHOUT ANY WARRANTY; without even the implied warranty of 16f53b25dfSMax Reitz# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17f53b25dfSMax Reitz# GNU General Public License for more details. 18f53b25dfSMax Reitz# 19f53b25dfSMax Reitz# You should have received a copy of the GNU General Public License 20f53b25dfSMax Reitz# along with this program. If not, see <http://www.gnu.org/licenses/>. 21f53b25dfSMax Reitz# 22f53b25dfSMax Reitz 23f53b25dfSMax Reitz# creator 24f53b25dfSMax Reitzowner=mreitz@redhat.com 25f53b25dfSMax Reitz 26f53b25dfSMax Reitzseq=$(basename $0) 27f53b25dfSMax Reitzecho "QA output created by $seq" 28f53b25dfSMax Reitz 29f53b25dfSMax Reitzstatus=1 # failure is the default! 30f53b25dfSMax Reitz 31f53b25dfSMax Reitz_cleanup() 32f53b25dfSMax Reitz{ 33f53b25dfSMax Reitz _cleanup_test_img 34f53b25dfSMax Reitz rm -f "$TEST_IMG".v{2,3}.orig 35f53b25dfSMax Reitz rm -f "$TEST_DIR"/sn{0,1,2}{,-pre,-extra,-post} 36f53b25dfSMax Reitz} 37f53b25dfSMax Reitztrap "_cleanup; exit \$status" 0 1 2 3 15 38f53b25dfSMax Reitz 39f53b25dfSMax Reitz# get standard environment, filters and checks 40f53b25dfSMax Reitz. ./common.rc 41f53b25dfSMax Reitz. ./common.filter 42f53b25dfSMax Reitz 43e696f335SMax Reitz# This tests qcow2-specific low-level functionality 44f53b25dfSMax Reitz_supported_fmt qcow2 45f53b25dfSMax Reitz_supported_proto file 46f53b25dfSMax Reitz_supported_os Linux 47f53b25dfSMax Reitz# (1) We create a v2 image that supports nothing but refcount_bits=16 48f53b25dfSMax Reitz# (2) We do some refcount management on our own which expects 49f53b25dfSMax Reitz# refcount_bits=16 50*3be2024aSMax Reitz# As for data files, they do not support snapshots at all. 51*3be2024aSMax Reitz_unsupported_imgopts 'refcount_bits=\([^1]\|.\([^6]\|$\)\)' data_file 52f53b25dfSMax Reitz 53f53b25dfSMax Reitz# Parameters: 54f53b25dfSMax Reitz# $1: image filename 55f53b25dfSMax Reitz# $2: snapshot table entry offset in the image 56f53b25dfSMax Reitzsnapshot_table_entry_size() 57f53b25dfSMax Reitz{ 58f53b25dfSMax Reitz id_len=$(peek_file_be "$1" $(($2 + 12)) 2) 59f53b25dfSMax Reitz name_len=$(peek_file_be "$1" $(($2 + 14)) 2) 60f53b25dfSMax Reitz extra_len=$(peek_file_be "$1" $(($2 + 36)) 4) 61f53b25dfSMax Reitz 62f53b25dfSMax Reitz full_len=$((40 + extra_len + id_len + name_len)) 63f53b25dfSMax Reitz echo $(((full_len + 7) / 8 * 8)) 64f53b25dfSMax Reitz} 65f53b25dfSMax Reitz 66f53b25dfSMax Reitz# Parameter: 67f53b25dfSMax Reitz# $1: image filename 68f53b25dfSMax Reitzprint_snapshot_table() 69f53b25dfSMax Reitz{ 70f53b25dfSMax Reitz nb_entries=$(peek_file_be "$1" 60 4) 71f53b25dfSMax Reitz offset=$(peek_file_be "$1" 64 8) 72f53b25dfSMax Reitz 73f53b25dfSMax Reitz echo "Snapshots in $1:" | _filter_testdir | _filter_imgfmt 74f53b25dfSMax Reitz 75f53b25dfSMax Reitz for ((i = 0; i < nb_entries; i++)); do 76f53b25dfSMax Reitz id_len=$(peek_file_be "$1" $((offset + 12)) 2) 77f53b25dfSMax Reitz name_len=$(peek_file_be "$1" $((offset + 14)) 2) 78f53b25dfSMax Reitz extra_len=$(peek_file_be "$1" $((offset + 36)) 4) 79f53b25dfSMax Reitz 80f53b25dfSMax Reitz extra_ofs=$((offset + 40)) 81f53b25dfSMax Reitz id_ofs=$((extra_ofs + extra_len)) 82f53b25dfSMax Reitz name_ofs=$((id_ofs + id_len)) 83f53b25dfSMax Reitz 84f53b25dfSMax Reitz echo " [$i]" 85f53b25dfSMax Reitz echo " ID: $(peek_file_raw "$1" $id_ofs $id_len)" 86f53b25dfSMax Reitz echo " Name: $(peek_file_raw "$1" $name_ofs $name_len)" 87f53b25dfSMax Reitz echo " Extra data size: $extra_len" 88f53b25dfSMax Reitz if [ $extra_len -ge 8 ]; then 89f53b25dfSMax Reitz echo " VM state size: $(peek_file_be "$1" $extra_ofs 8)" 90f53b25dfSMax Reitz fi 91f53b25dfSMax Reitz if [ $extra_len -ge 16 ]; then 92f53b25dfSMax Reitz echo " Disk size: $(peek_file_be "$1" $((extra_ofs + 8)) 8)" 93f53b25dfSMax Reitz fi 94f53b25dfSMax Reitz if [ $extra_len -gt 16 ]; then 95f53b25dfSMax Reitz echo ' Unknown extra data:' \ 96f53b25dfSMax Reitz "$(peek_file_raw "$1" $((extra_ofs + 16)) $((extra_len - 16)) \ 97f53b25dfSMax Reitz | tr -d '\0')" 98f53b25dfSMax Reitz fi 99f53b25dfSMax Reitz 100f53b25dfSMax Reitz offset=$((offset + $(snapshot_table_entry_size "$1" $offset))) 101f53b25dfSMax Reitz done 102f53b25dfSMax Reitz} 103f53b25dfSMax Reitz 104f53b25dfSMax Reitz# Mark clusters as allocated; works only in refblock 0 (i.e. before 105f53b25dfSMax Reitz# cluster #32768). 106f53b25dfSMax Reitz# Parameters: 107f53b25dfSMax Reitz# $1: Start offset of what to allocate 108f53b25dfSMax Reitz# $2: End offset (exclusive) 109f53b25dfSMax Reitzrefblock0_allocate() 110f53b25dfSMax Reitz{ 111f53b25dfSMax Reitz reftable_ofs=$(peek_file_be "$TEST_IMG" 48 8) 112f53b25dfSMax Reitz refblock_ofs=$(peek_file_be "$TEST_IMG" $reftable_ofs 8) 113f53b25dfSMax Reitz 114f53b25dfSMax Reitz cluster=$(($1 / 65536)) 115f53b25dfSMax Reitz ecluster=$((($2 + 65535) / 65536)) 116f53b25dfSMax Reitz 117f53b25dfSMax Reitz while [ $cluster -lt $ecluster ]; do 118f53b25dfSMax Reitz if [ $cluster -ge 32768 ]; then 119f53b25dfSMax Reitz echo "*** Abort: Cluster $cluster exceeds refblock 0 ***" 120f53b25dfSMax Reitz exit 1 121f53b25dfSMax Reitz fi 122f53b25dfSMax Reitz poke_file "$TEST_IMG" $((refblock_ofs + cluster * 2)) '\x00\x01' 123f53b25dfSMax Reitz cluster=$((cluster + 1)) 124f53b25dfSMax Reitz done 125f53b25dfSMax Reitz} 126f53b25dfSMax Reitz 127f53b25dfSMax Reitz 128f53b25dfSMax Reitzecho 129f53b25dfSMax Reitzecho '=== Create v2 template ===' 130f53b25dfSMax Reitzecho 131f53b25dfSMax Reitz 132f53b25dfSMax Reitz# Create v2 image with a snapshot table with three entries: 133f53b25dfSMax Reitz# [0]: No extra data (valid with v2, not valid with v3) 134f53b25dfSMax Reitz# [1]: Has extra data unknown to qemu 135f53b25dfSMax Reitz# [2]: Has the 64-bit VM state size, but not the disk size (again, 136f53b25dfSMax Reitz# valid with v2, not valid with v3) 137f53b25dfSMax Reitz 138f53b25dfSMax ReitzTEST_IMG="$TEST_IMG.v2.orig" IMGOPTS='compat=0.10' _make_test_img 64M 139f53b25dfSMax Reitz$QEMU_IMG snapshot -c sn0 "$TEST_IMG.v2.orig" 140f53b25dfSMax Reitz$QEMU_IMG snapshot -c sn1 "$TEST_IMG.v2.orig" 141f53b25dfSMax Reitz$QEMU_IMG snapshot -c sn2 "$TEST_IMG.v2.orig" 142f53b25dfSMax Reitz 143f53b25dfSMax Reitz# Copy out all existing snapshot table entries 144f53b25dfSMax Reitzsn_table_ofs=$(peek_file_be "$TEST_IMG.v2.orig" 64 8) 145f53b25dfSMax Reitz 146f53b25dfSMax Reitz# ofs: Snapshot table entry offset 147f53b25dfSMax Reitz# eds: Extra data size 148f53b25dfSMax Reitz# ids: Name + ID size 149f53b25dfSMax Reitz# len: Total entry length 150f53b25dfSMax Reitzsn0_ofs=$sn_table_ofs 151f53b25dfSMax Reitzsn0_eds=$(peek_file_be "$TEST_IMG.v2.orig" $((sn0_ofs + 36)) 4) 152f53b25dfSMax Reitzsn0_ids=$(($(peek_file_be "$TEST_IMG.v2.orig" $((sn0_ofs + 12)) 2) + 153f53b25dfSMax Reitz $(peek_file_be "$TEST_IMG.v2.orig" $((sn0_ofs + 14)) 2))) 154f53b25dfSMax Reitzsn0_len=$(snapshot_table_entry_size "$TEST_IMG.v2.orig" $sn0_ofs) 155f53b25dfSMax Reitzsn1_ofs=$((sn0_ofs + sn0_len)) 156f53b25dfSMax Reitzsn1_eds=$(peek_file_be "$TEST_IMG.v2.orig" $((sn1_ofs + 36)) 4) 157f53b25dfSMax Reitzsn1_ids=$(($(peek_file_be "$TEST_IMG.v2.orig" $((sn1_ofs + 12)) 2) + 158f53b25dfSMax Reitz $(peek_file_be "$TEST_IMG.v2.orig" $((sn1_ofs + 14)) 2))) 159f53b25dfSMax Reitzsn1_len=$(snapshot_table_entry_size "$TEST_IMG.v2.orig" $sn1_ofs) 160f53b25dfSMax Reitzsn2_ofs=$((sn1_ofs + sn1_len)) 161f53b25dfSMax Reitzsn2_eds=$(peek_file_be "$TEST_IMG.v2.orig" $((sn2_ofs + 36)) 4) 162f53b25dfSMax Reitzsn2_ids=$(($(peek_file_be "$TEST_IMG.v2.orig" $((sn2_ofs + 12)) 2) + 163f53b25dfSMax Reitz $(peek_file_be "$TEST_IMG.v2.orig" $((sn2_ofs + 14)) 2))) 164f53b25dfSMax Reitzsn2_len=$(snapshot_table_entry_size "$TEST_IMG.v2.orig" $sn2_ofs) 165f53b25dfSMax Reitz 166f53b25dfSMax Reitz# Data before extra data 167f53b25dfSMax Reitzdd if="$TEST_IMG.v2.orig" of="$TEST_DIR/sn0-pre" bs=1 skip=$sn0_ofs count=40 \ 168f53b25dfSMax Reitz &> /dev/null 169f53b25dfSMax Reitzdd if="$TEST_IMG.v2.orig" of="$TEST_DIR/sn1-pre" bs=1 skip=$sn1_ofs count=40 \ 170f53b25dfSMax Reitz &> /dev/null 171f53b25dfSMax Reitzdd if="$TEST_IMG.v2.orig" of="$TEST_DIR/sn2-pre" bs=1 skip=$sn2_ofs count=40 \ 172f53b25dfSMax Reitz &> /dev/null 173f53b25dfSMax Reitz 174f53b25dfSMax Reitz# Extra data 175f53b25dfSMax Reitzdd if="$TEST_IMG.v2.orig" of="$TEST_DIR/sn0-extra" bs=1 \ 176f53b25dfSMax Reitz skip=$((sn0_ofs + 40)) count=$sn0_eds &> /dev/null 177f53b25dfSMax Reitzdd if="$TEST_IMG.v2.orig" of="$TEST_DIR/sn1-extra" bs=1 \ 178f53b25dfSMax Reitz skip=$((sn1_ofs + 40)) count=$sn1_eds &> /dev/null 179f53b25dfSMax Reitzdd if="$TEST_IMG.v2.orig" of="$TEST_DIR/sn2-extra" bs=1 \ 180f53b25dfSMax Reitz skip=$((sn2_ofs + 40)) count=$sn2_eds &> /dev/null 181f53b25dfSMax Reitz 182f53b25dfSMax Reitz# Data after extra data 183f53b25dfSMax Reitzdd if="$TEST_IMG.v2.orig" of="$TEST_DIR/sn0-post" bs=1 \ 184f53b25dfSMax Reitz skip=$((sn0_ofs + 40 + sn0_eds)) count=$sn0_ids \ 185f53b25dfSMax Reitz &> /dev/null 186f53b25dfSMax Reitzdd if="$TEST_IMG.v2.orig" of="$TEST_DIR/sn1-post" bs=1 \ 187f53b25dfSMax Reitz skip=$((sn1_ofs + 40 + sn1_eds)) count=$sn1_ids \ 188f53b25dfSMax Reitz &> /dev/null 189f53b25dfSMax Reitzdd if="$TEST_IMG.v2.orig" of="$TEST_DIR/sn2-post" bs=1 \ 190f53b25dfSMax Reitz skip=$((sn2_ofs + 40 + sn2_eds)) count=$sn2_ids \ 191f53b25dfSMax Reitz &> /dev/null 192f53b25dfSMax Reitz 193f53b25dfSMax Reitz# Amend them, one by one 194f53b25dfSMax Reitz# Set sn0's extra data size to 0 195f53b25dfSMax Reitzpoke_file "$TEST_DIR/sn0-pre" 36 '\x00\x00\x00\x00' 196f53b25dfSMax Reitztruncate -s 0 "$TEST_DIR/sn0-extra" 197f53b25dfSMax Reitz# Grow sn0-post to pad 198f53b25dfSMax Reitztruncate -s $(($(snapshot_table_entry_size "$TEST_DIR/sn0-pre") - 40)) \ 199f53b25dfSMax Reitz "$TEST_DIR/sn0-post" 200f53b25dfSMax Reitz 201f53b25dfSMax Reitz# Set sn1's extra data size to 42 202f53b25dfSMax Reitzpoke_file "$TEST_DIR/sn1-pre" 36 '\x00\x00\x00\x2a' 203f53b25dfSMax Reitztruncate -s 42 "$TEST_DIR/sn1-extra" 204f53b25dfSMax Reitzpoke_file "$TEST_DIR/sn1-extra" 16 'very important data' 205f53b25dfSMax Reitz# Grow sn1-post to pad 206f53b25dfSMax Reitztruncate -s $(($(snapshot_table_entry_size "$TEST_DIR/sn1-pre") - 82)) \ 207f53b25dfSMax Reitz "$TEST_DIR/sn1-post" 208f53b25dfSMax Reitz 209f53b25dfSMax Reitz# Set sn2's extra data size to 8 210f53b25dfSMax Reitzpoke_file "$TEST_DIR/sn2-pre" 36 '\x00\x00\x00\x08' 211f53b25dfSMax Reitztruncate -s 8 "$TEST_DIR/sn2-extra" 212f53b25dfSMax Reitz# Grow sn2-post to pad 213f53b25dfSMax Reitztruncate -s $(($(snapshot_table_entry_size "$TEST_DIR/sn2-pre") - 48)) \ 214f53b25dfSMax Reitz "$TEST_DIR/sn2-post" 215f53b25dfSMax Reitz 216f53b25dfSMax Reitz# Construct snapshot table 217f53b25dfSMax Reitzcat "$TEST_DIR"/sn0-{pre,extra,post} \ 218f53b25dfSMax Reitz "$TEST_DIR"/sn1-{pre,extra,post} \ 219f53b25dfSMax Reitz "$TEST_DIR"/sn2-{pre,extra,post} \ 220f53b25dfSMax Reitz | dd of="$TEST_IMG.v2.orig" bs=1 seek=$sn_table_ofs conv=notrunc \ 221f53b25dfSMax Reitz &> /dev/null 222f53b25dfSMax Reitz 223f53b25dfSMax Reitz# Done! 224f53b25dfSMax ReitzTEST_IMG="$TEST_IMG.v2.orig" _check_test_img 225f53b25dfSMax Reitzprint_snapshot_table "$TEST_IMG.v2.orig" 226f53b25dfSMax Reitz 227f53b25dfSMax Reitzecho 228f53b25dfSMax Reitzecho '=== Upgrade to v3 ===' 229f53b25dfSMax Reitzecho 230f53b25dfSMax Reitz 231f53b25dfSMax Reitzcp "$TEST_IMG.v2.orig" "$TEST_IMG.v3.orig" 232f53b25dfSMax Reitz$QEMU_IMG amend -o compat=1.1 "$TEST_IMG.v3.orig" 233f53b25dfSMax ReitzTEST_IMG="$TEST_IMG.v3.orig" _check_test_img 234f53b25dfSMax Reitzprint_snapshot_table "$TEST_IMG.v3.orig" 235f53b25dfSMax Reitz 236f53b25dfSMax Reitzecho 237f53b25dfSMax Reitzecho '=== Repair botched v3 ===' 238f53b25dfSMax Reitzecho 239f53b25dfSMax Reitz 240f53b25dfSMax Reitz# Force the v2 file to be v3. v3 requires each snapshot table entry 241f53b25dfSMax Reitz# to have at least 16 bytes of extra data, so it will not comply to 242f53b25dfSMax Reitz# the qcow2 v3 specification; but we can fix that. 243f53b25dfSMax Reitzcp "$TEST_IMG.v2.orig" "$TEST_IMG" 244f53b25dfSMax Reitz 245f53b25dfSMax Reitz# Set version 246f53b25dfSMax Reitzpoke_file "$TEST_IMG" 4 '\x00\x00\x00\x03' 247f53b25dfSMax Reitz# Increase header length (necessary for v3) 248f53b25dfSMax Reitzpoke_file "$TEST_IMG" 100 '\x00\x00\x00\x68' 249f53b25dfSMax Reitz# Set refcount order (necessary for v3) 250f53b25dfSMax Reitzpoke_file "$TEST_IMG" 96 '\x00\x00\x00\x04' 251f53b25dfSMax Reitz 252f53b25dfSMax Reitz_check_test_img -r all 253f53b25dfSMax Reitzprint_snapshot_table "$TEST_IMG" 254f53b25dfSMax Reitz 255f53b25dfSMax Reitz 256f53b25dfSMax Reitz# From now on, just test the qcow2 version we are supposed to test. 257f53b25dfSMax Reitz# (v3 by default, v2 by choice through $IMGOPTS.) 258f53b25dfSMax Reitz# That works because we always write all known extra data when 259f53b25dfSMax Reitz# updating the snapshot table, independent of the version. 260f53b25dfSMax Reitz 261f53b25dfSMax Reitzif echo "$IMGOPTS" | grep -q 'compat=\(0\.10\|v2\)' 2> /dev/null; then 262f53b25dfSMax Reitz subver=v2 263f53b25dfSMax Reitzelse 264f53b25dfSMax Reitz subver=v3 265f53b25dfSMax Reitzfi 266f53b25dfSMax Reitz 267f53b25dfSMax Reitzecho 268f53b25dfSMax Reitzecho '=== Add new snapshot ===' 269f53b25dfSMax Reitzecho 270f53b25dfSMax Reitz 271f53b25dfSMax Reitzcp "$TEST_IMG.$subver.orig" "$TEST_IMG" 272f53b25dfSMax Reitz$QEMU_IMG snapshot -c sn3 "$TEST_IMG" 273f53b25dfSMax Reitz_check_test_img 274f53b25dfSMax Reitzprint_snapshot_table "$TEST_IMG" 275f53b25dfSMax Reitz 276f53b25dfSMax Reitzecho 277f53b25dfSMax Reitzecho '=== Remove different snapshots ===' 278f53b25dfSMax Reitz 279f53b25dfSMax Reitzfor sn in sn0 sn1 sn2; do 280f53b25dfSMax Reitz echo 281f53b25dfSMax Reitz echo "--- $sn ---" 282f53b25dfSMax Reitz 283f53b25dfSMax Reitz cp "$TEST_IMG.$subver.orig" "$TEST_IMG" 284f53b25dfSMax Reitz $QEMU_IMG snapshot -d $sn "$TEST_IMG" 285f53b25dfSMax Reitz _check_test_img 286f53b25dfSMax Reitz print_snapshot_table "$TEST_IMG" 287f53b25dfSMax Reitzdone 288f53b25dfSMax Reitz 289f53b25dfSMax Reitzecho 290f53b25dfSMax Reitzecho '=== Reject too much unknown extra data ===' 291f53b25dfSMax Reitzecho 292f53b25dfSMax Reitz 293f53b25dfSMax Reitzcp "$TEST_IMG.$subver.orig" "$TEST_IMG" 294f53b25dfSMax Reitz$QEMU_IMG snapshot -c sn3 "$TEST_IMG" 295f53b25dfSMax Reitz 296f53b25dfSMax Reitzsn_table_ofs=$(peek_file_be "$TEST_IMG" 64 8) 297f53b25dfSMax Reitzsn0_ofs=$sn_table_ofs 298f53b25dfSMax Reitzsn1_ofs=$((sn0_ofs + $(snapshot_table_entry_size "$TEST_IMG" $sn0_ofs))) 299f53b25dfSMax Reitzsn2_ofs=$((sn1_ofs + $(snapshot_table_entry_size "$TEST_IMG" $sn1_ofs))) 300f53b25dfSMax Reitzsn3_ofs=$((sn2_ofs + $(snapshot_table_entry_size "$TEST_IMG" $sn2_ofs))) 301f53b25dfSMax Reitz 302f53b25dfSMax Reitz# 64 kB of extra data should be rejected 303f53b25dfSMax Reitz# (Note that this also induces a refcount error, because it spills 304f53b25dfSMax Reitz# over to the next cluster. That's a good way to test that we can 305f53b25dfSMax Reitz# handle simultaneous snapshot table and refcount errors.) 306f53b25dfSMax Reitzpoke_file "$TEST_IMG" $((sn3_ofs + 36)) '\x00\x01\x00\x00' 307f53b25dfSMax Reitz 308f53b25dfSMax Reitz# Print error 309f53b25dfSMax Reitz_img_info 310f53b25dfSMax Reitzecho 311f53b25dfSMax Reitz_check_test_img 312f53b25dfSMax Reitzecho 313f53b25dfSMax Reitz 314f53b25dfSMax Reitz# Should be repairable 315f53b25dfSMax Reitz_check_test_img -r all 316f53b25dfSMax Reitz 317f53b25dfSMax Reitzecho 318f53b25dfSMax Reitzecho '=== Snapshot table too big ===' 319f53b25dfSMax Reitzecho 320f53b25dfSMax Reitz 321f53b25dfSMax Reitzsn_table_ofs=$(peek_file_be "$TEST_IMG.v3.orig" 64 8) 322f53b25dfSMax Reitz 323f53b25dfSMax Reitz# Fill a snapshot with 1 kB of extra data, a 65535-char ID, and a 324f53b25dfSMax Reitz# 65535-char name, and repeat it as many times as necessary to fill 325f53b25dfSMax Reitz# 64 MB (the maximum supported by qemu) 326f53b25dfSMax Reitz 327f53b25dfSMax Reitztouch "$TEST_DIR/sn0" 328f53b25dfSMax Reitz 329f53b25dfSMax Reitz# Full size (fixed + extra + ID + name + padding) 330f53b25dfSMax Reitzsn_size=$((40 + 1024 + 65535 + 65535 + 2)) 331f53b25dfSMax Reitz 332f53b25dfSMax Reitz# We only need the fixed part, though. 333f53b25dfSMax Reitztruncate -s 40 "$TEST_DIR/sn0" 334f53b25dfSMax Reitz 335f53b25dfSMax Reitz# 65535-char ID string 336f53b25dfSMax Reitzpoke_file "$TEST_DIR/sn0" 12 '\xff\xff' 337f53b25dfSMax Reitz# 65535-char name 338f53b25dfSMax Reitzpoke_file "$TEST_DIR/sn0" 14 '\xff\xff' 339f53b25dfSMax Reitz# 1 kB of extra data 340f53b25dfSMax Reitzpoke_file "$TEST_DIR/sn0" 36 '\x00\x00\x04\x00' 341f53b25dfSMax Reitz 342f53b25dfSMax Reitz# Create test image 343f53b25dfSMax Reitz_make_test_img 64M 344f53b25dfSMax Reitz 345f53b25dfSMax Reitz# Hook up snapshot table somewhere safe (at 1 MB) 346f53b25dfSMax Reitzpoke_file "$TEST_IMG" 64 '\x00\x00\x00\x00\x00\x10\x00\x00' 347f53b25dfSMax Reitz 348f53b25dfSMax Reitzoffset=1048576 349f53b25dfSMax Reitzsize_written=0 350f53b25dfSMax Reitzsn_count=0 351f53b25dfSMax Reitzwhile [ $size_written -le $((64 * 1048576)) ]; do 352f53b25dfSMax Reitz dd if="$TEST_DIR/sn0" of="$TEST_IMG" bs=1 seek=$offset conv=notrunc \ 353f53b25dfSMax Reitz &> /dev/null 354f53b25dfSMax Reitz offset=$((offset + sn_size)) 355f53b25dfSMax Reitz size_written=$((size_written + sn_size)) 356f53b25dfSMax Reitz sn_count=$((sn_count + 1)) 357f53b25dfSMax Reitzdone 358f53b25dfSMax Reitztruncate -s "$offset" "$TEST_IMG" 359f53b25dfSMax Reitz 360f53b25dfSMax Reitz# Give the last snapshot (the one to be removed) an L1 table so we can 361f53b25dfSMax Reitz# see how that is handled when repairing the image 362f53b25dfSMax Reitz# (Put it two clusters before 1 MB, and one L2 table one cluster 363f53b25dfSMax Reitz# before 1 MB) 364f53b25dfSMax Reitzpoke_file "$TEST_IMG" $((offset - sn_size + 0)) \ 365f53b25dfSMax Reitz '\x00\x00\x00\x00\x00\x0e\x00\x00' 366f53b25dfSMax Reitzpoke_file "$TEST_IMG" $((offset - sn_size + 8)) \ 367f53b25dfSMax Reitz '\x00\x00\x00\x01' 368f53b25dfSMax Reitz 369f53b25dfSMax Reitz# Hook up the L2 table 370f53b25dfSMax Reitzpoke_file "$TEST_IMG" $((1048576 - 2 * 65536)) \ 371f53b25dfSMax Reitz '\x80\x00\x00\x00\x00\x0f\x00\x00' 372f53b25dfSMax Reitz 373f53b25dfSMax Reitz# Make sure all of the clusters we just hooked up are allocated: 374f53b25dfSMax Reitz# - The snapshot table 375f53b25dfSMax Reitz# - The last snapshot's L1 and L2 table 376f53b25dfSMax Reitzrefblock0_allocate $((1048576 - 2 * 65536)) $offset 377f53b25dfSMax Reitz 378f53b25dfSMax Reitzpoke_file "$TEST_IMG" 60 \ 379f53b25dfSMax Reitz "$(printf '%08x' $sn_count | sed -e 's/\(..\)/\\x\1/g')" 380f53b25dfSMax Reitz 381f53b25dfSMax Reitz# Print error 382f53b25dfSMax Reitz_img_info 383f53b25dfSMax Reitzecho 384f53b25dfSMax Reitz_check_test_img 385f53b25dfSMax Reitzecho 386f53b25dfSMax Reitz 387f53b25dfSMax Reitz# Should be repairable 388f53b25dfSMax Reitz_check_test_img -r all 389f53b25dfSMax Reitz 390f53b25dfSMax Reitzecho 391f53b25dfSMax Reitzecho "$((sn_count - 1)) snapshots should remain:" 392f53b25dfSMax Reitzecho " qemu-img info reports $(_img_info | grep -c '^ \{34\}') snapshots" 393f53b25dfSMax Reitzecho " Image header reports $(peek_file_be "$TEST_IMG" 60 4) snapshots" 394f53b25dfSMax Reitz 395f53b25dfSMax Reitzecho 396f53b25dfSMax Reitzecho '=== Snapshot table too big with one entry with too much extra data ===' 397f53b25dfSMax Reitzecho 398f53b25dfSMax Reitz 399f53b25dfSMax Reitz# For this test, we reuse the image from the previous case, which has 400f53b25dfSMax Reitz# a snapshot table that is right at the limit. 401f53b25dfSMax Reitz# Our layout looks like this: 402f53b25dfSMax Reitz# - (a number of snapshot table entries) 403f53b25dfSMax Reitz# - One snapshot with $extra_data_size extra data 404f53b25dfSMax Reitz# - One normal snapshot that breaks the 64 MB boundary 405f53b25dfSMax Reitz# - One normal snapshot beyond the 64 MB boundary 406f53b25dfSMax Reitz# 407f53b25dfSMax Reitz# $extra_data_size is calculated so that simply by virtue of it 408f53b25dfSMax Reitz# decreasing to 1 kB, the penultimate snapshot will fit into 64 MB 409f53b25dfSMax Reitz# limit again. The final snapshot will always be beyond the limit, so 410f53b25dfSMax Reitz# that we can see that the repair algorithm does still determine the 411f53b25dfSMax Reitz# limit to be somewhere, even when truncating one snapshot's extra 412f53b25dfSMax Reitz# data. 413f53b25dfSMax Reitz 414f53b25dfSMax Reitz# The last case has removed the last snapshot, so calculate 415f53b25dfSMax Reitz# $old_offset to get the current image's real length 416f53b25dfSMax Reitzold_offset=$((offset - sn_size)) 417f53b25dfSMax Reitz 418f53b25dfSMax Reitz# The layout from the previous test had one snapshot beyond the 64 MB 419f53b25dfSMax Reitz# limit; we want the same (after the oversized extra data has been 420f53b25dfSMax Reitz# truncated to 1 kB), so we drop the last three snapshots and 421f53b25dfSMax Reitz# construct them from scratch. 422f53b25dfSMax Reitzoffset=$((offset - 3 * sn_size)) 423f53b25dfSMax Reitzsn_count=$((sn_count - 3)) 424f53b25dfSMax Reitz 425f53b25dfSMax Reitz# Assuming we had already written one of the three snapshots 426f53b25dfSMax Reitz# (necessary so we can calculate $extra_data_size next). 427f53b25dfSMax Reitzsize_written=$((size_written - 2 * sn_size)) 428f53b25dfSMax Reitz 429f53b25dfSMax Reitz# Increase the extra data size so we go past the limit 430f53b25dfSMax Reitz# (The -1024 comes from the 1 kB of extra data we already have) 431f53b25dfSMax Reitzextra_data_size=$((64 * 1048576 + 8 - sn_size - (size_written - 1024))) 432f53b25dfSMax Reitz 433f53b25dfSMax Reitzpoke_file "$TEST_IMG" $((offset + 36)) \ 434f53b25dfSMax Reitz "$(printf '%08x' $extra_data_size | sed -e 's/\(..\)/\\x\1/g')" 435f53b25dfSMax Reitz 436f53b25dfSMax Reitzoffset=$((offset + sn_size - 1024 + extra_data_size)) 437f53b25dfSMax Reitzsize_written=$((size_written - 1024 + extra_data_size)) 438f53b25dfSMax Reitzsn_count=$((sn_count + 1)) 439f53b25dfSMax Reitz 440f53b25dfSMax Reitz# Write the two normal snapshots 441f53b25dfSMax Reitzfor ((i = 0; i < 2; i++)); do 442f53b25dfSMax Reitz dd if="$TEST_DIR/sn0" of="$TEST_IMG" bs=1 seek=$offset conv=notrunc \ 443f53b25dfSMax Reitz &> /dev/null 444f53b25dfSMax Reitz offset=$((offset + sn_size)) 445f53b25dfSMax Reitz size_written=$((size_written + sn_size)) 446f53b25dfSMax Reitz sn_count=$((sn_count + 1)) 447f53b25dfSMax Reitz 448f53b25dfSMax Reitz if [ $i = 0 ]; then 449f53b25dfSMax Reitz # Check that the penultimate snapshot is beyond the 64 MB limit 450f53b25dfSMax Reitz echo "Snapshot table size should equal $((64 * 1048576 + 8)):" \ 451f53b25dfSMax Reitz $size_written 452f53b25dfSMax Reitz echo 453f53b25dfSMax Reitz fi 454f53b25dfSMax Reitzdone 455f53b25dfSMax Reitz 456f53b25dfSMax Reitztruncate -s $offset "$TEST_IMG" 457f53b25dfSMax Reitzrefblock0_allocate $old_offset $offset 458f53b25dfSMax Reitz 459f53b25dfSMax Reitzpoke_file "$TEST_IMG" 60 \ 460f53b25dfSMax Reitz "$(printf '%08x' $sn_count | sed -e 's/\(..\)/\\x\1/g')" 461f53b25dfSMax Reitz 462f53b25dfSMax Reitz# Print error 463f53b25dfSMax Reitz_img_info 464f53b25dfSMax Reitzecho 465f53b25dfSMax Reitz_check_test_img 466f53b25dfSMax Reitzecho 467f53b25dfSMax Reitz 468f53b25dfSMax Reitz# Just truncating the extra data should be sufficient to shorten the 469f53b25dfSMax Reitz# snapshot table so only one snapshot exceeds the extra size 470f53b25dfSMax Reitz_check_test_img -r all 471f53b25dfSMax Reitz 472f53b25dfSMax Reitzecho 473f53b25dfSMax Reitzecho '=== Too many snapshots ===' 474f53b25dfSMax Reitzecho 475f53b25dfSMax Reitz 476f53b25dfSMax Reitz# Create a v2 image, for speeds' sake: All-zero snapshot table entries 477f53b25dfSMax Reitz# are only valid in v2. 478f53b25dfSMax ReitzIMGOPTS='compat=0.10' _make_test_img 64M 479f53b25dfSMax Reitz 480f53b25dfSMax Reitz# Hook up snapshot table somewhere safe (at 1 MB) 481f53b25dfSMax Reitzpoke_file "$TEST_IMG" 64 '\x00\x00\x00\x00\x00\x10\x00\x00' 482f53b25dfSMax Reitz# "Create" more than 65536 snapshots (twice that many here) 483f53b25dfSMax Reitzpoke_file "$TEST_IMG" 60 '\x00\x02\x00\x00' 484f53b25dfSMax Reitz 485f53b25dfSMax Reitz# 40-byte all-zero snapshot table entries are valid snapshots, but 486f53b25dfSMax Reitz# only in v2 (v3 needs 16 bytes of extra data, so we would have to 487f53b25dfSMax Reitz# write 131072x '\x10'). 488f53b25dfSMax Reitztruncate -s $((1048576 + 40 * 131072)) "$TEST_IMG" 489f53b25dfSMax Reitz 490f53b25dfSMax Reitz# But let us give one of the snapshots to be removed an L1 table so 491f53b25dfSMax Reitz# we can see how that is handled when repairing the image. 492f53b25dfSMax Reitz# (Put it two clusters before 1 MB, and one L2 table one cluster 493f53b25dfSMax Reitz# before 1 MB) 494f53b25dfSMax Reitzpoke_file "$TEST_IMG" $((1048576 + 40 * 65536 + 0)) \ 495f53b25dfSMax Reitz '\x00\x00\x00\x00\x00\x0e\x00\x00' 496f53b25dfSMax Reitzpoke_file "$TEST_IMG" $((1048576 + 40 * 65536 + 8)) \ 497f53b25dfSMax Reitz '\x00\x00\x00\x01' 498f53b25dfSMax Reitz 499f53b25dfSMax Reitz# Hook up the L2 table 500f53b25dfSMax Reitzpoke_file "$TEST_IMG" $((1048576 - 2 * 65536)) \ 501f53b25dfSMax Reitz '\x80\x00\x00\x00\x00\x0f\x00\x00' 502f53b25dfSMax Reitz 503f53b25dfSMax Reitz# Make sure all of the clusters we just hooked up are allocated: 504f53b25dfSMax Reitz# - The snapshot table 505f53b25dfSMax Reitz# - The last snapshot's L1 and L2 table 506f53b25dfSMax Reitzrefblock0_allocate $((1048576 - 2 * 65536)) $((1048576 + 40 * 131072)) 507f53b25dfSMax Reitz 508f53b25dfSMax Reitz# Print error 509f53b25dfSMax Reitz_img_info 510f53b25dfSMax Reitzecho 511f53b25dfSMax Reitz_check_test_img 512f53b25dfSMax Reitzecho 513f53b25dfSMax Reitz 514f53b25dfSMax Reitz# Should be repairable 515f53b25dfSMax Reitz_check_test_img -r all 516f53b25dfSMax Reitz 517f53b25dfSMax Reitzecho 518f53b25dfSMax Reitzecho '65536 snapshots should remain:' 519f53b25dfSMax Reitzecho " qemu-img info reports $(_img_info | grep -c '^ \{34\}') snapshots" 520f53b25dfSMax Reitzecho " Image header reports $(peek_file_be "$TEST_IMG" 60 4) snapshots" 521f53b25dfSMax Reitz 522f53b25dfSMax Reitz# success, all done 523f53b25dfSMax Reitzecho "*** done" 524f53b25dfSMax Reitzstatus=0 525