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