xref: /openbmc/qemu/tests/qemu-iotests/066 (revision b043b07ce3057ace905e6f22692fa110b45d10eb)
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
42*b043b07cSMax Reitz# We need zero clusters and snapshots
43*b043b07cSMax Reitz_unsupported_imgopts 'compat=0.10' 'refcount_bits=1[^0-9]'
44975a93c0SMax Reitz
45048c5fd1SEric Blake# Intentionally create an unaligned image
46048c5fd1SEric BlakeIMG_SIZE=$((64 * 1024 * 1024 + 512))
47975a93c0SMax Reitz
48975a93c0SMax Reitzecho
49048c5fd1SEric Blakeecho "=== Testing cluster discards ==="
50975a93c0SMax Reitzecho
51975a93c0SMax Reitz_make_test_img $IMG_SIZE
52048c5fd1SEric Blake# Write some normal clusters, zero some of them (creating preallocated
53048c5fd1SEric Blake# zero clusters) and discard everything. Everything should now read as 0.
54048c5fd1SEric Blake$QEMU_IO -c "write 0 256k" -c "write -z 0 256k" -c "write 64M 512" \
55048c5fd1SEric Blake	 -c "discard 0 $IMG_SIZE" -c "read -P 0 0 $IMG_SIZE" "$TEST_IMG" \
56975a93c0SMax Reitz         | _filter_qemu_io
57aa93c834SMax Reitz
58975a93c0SMax Reitz# Check the image (there shouldn't be any leaks)
59975a93c0SMax Reitz_check_test_img
60aa93c834SMax Reitz# Map the image (we want all clusters to be gone)
61aa93c834SMax Reitz$QEMU_IMG map "$TEST_IMG"
62aa93c834SMax Reitz
63aa93c834SMax Reitz_cleanup_test_img
64aa93c834SMax Reitz
65aa93c834SMax Reitz
66aa93c834SMax Reitzecho
67aa93c834SMax Reitzecho '=== Writing to preallocated zero clusters ==='
68aa93c834SMax Reitzecho
69aa93c834SMax Reitz
70aa93c834SMax Reitz_make_test_img $IMG_SIZE
71aa93c834SMax Reitz
72aa93c834SMax Reitz# Create data clusters (not aligned to an L2 table)
73aa93c834SMax Reitz$QEMU_IO -c 'write -P 42 1M 256k' "$TEST_IMG" | _filter_qemu_io
74aa93c834SMax Reitzorig_map=$($QEMU_IMG map --output=json "$TEST_IMG")
75aa93c834SMax Reitz
76aa93c834SMax Reitz# Convert the data clusters to preallocated zero clusters
77aa93c834SMax Reitz$QEMU_IO -c 'write -z 1M 256k' "$TEST_IMG" | _filter_qemu_io
78aa93c834SMax Reitz
79aa93c834SMax Reitz# Now write to them (with a COW needed for the head and tail)
80aa93c834SMax Reitz$QEMU_IO -c "write -P 23 $(((1024 + 32) * 1024)) 192k" "$TEST_IMG" \
81aa93c834SMax Reitz    | _filter_qemu_io
82aa93c834SMax Reitz
83aa93c834SMax Reitz# Check metadata correctness
84aa93c834SMax Reitz_check_test_img
85aa93c834SMax Reitz
86aa93c834SMax Reitz# Check data correctness
87aa93c834SMax Reitz$QEMU_IO -c "read -P  0 $(( 1024             * 1024)) 32k" \
88aa93c834SMax Reitz         -c "read -P 23 $(((1024 + 32)       * 1024)) 192k" \
89aa93c834SMax Reitz         -c "read -P  0 $(((1024 + 32 + 192) * 1024)) 32k" \
90aa93c834SMax Reitz         "$TEST_IMG" \
91aa93c834SMax Reitz         | _filter_qemu_io
92aa93c834SMax Reitz
93aa93c834SMax Reitz# Check that we have actually reused the original area
94aa93c834SMax Reitznew_map=$($QEMU_IMG map --output=json "$TEST_IMG")
95aa93c834SMax Reitzif [ "$new_map" = "$orig_map" ]; then
96aa93c834SMax Reitz    echo 'Successfully reused original clusters.'
97aa93c834SMax Reitzelse
98aa93c834SMax Reitz    echo 'Failed to reuse original clusters.'
99aa93c834SMax Reitz    echo 'Original map:'
100aa93c834SMax Reitz    echo "$orig_map"
101aa93c834SMax Reitz    echo 'New map:'
102aa93c834SMax Reitz    echo "$new_map"
103aa93c834SMax Reitzfi
104aa93c834SMax Reitz
105aa93c834SMax Reitz_cleanup_test_img
106aa93c834SMax Reitz
107aa93c834SMax Reitz
108aa93c834SMax Reitzecho
109aa93c834SMax Reitzecho '=== Writing to a snapshotted preallocated zero cluster ==='
110aa93c834SMax Reitzecho
111aa93c834SMax Reitz
112aa93c834SMax Reitz_make_test_img 64k
113aa93c834SMax Reitz
114aa93c834SMax Reitz# Create a preallocated zero cluster
115aa93c834SMax Reitz$QEMU_IO -c 'write -P 42 0 64k' -c 'write -z 0 64k' "$TEST_IMG" \
116aa93c834SMax Reitz    | _filter_qemu_io
117aa93c834SMax Reitz
118aa93c834SMax Reitz# Snapshot it
119aa93c834SMax Reitz$QEMU_IMG snapshot -c foo "$TEST_IMG"
120aa93c834SMax Reitz
121aa93c834SMax Reitz# Write to the cluster
122aa93c834SMax Reitz$QEMU_IO -c 'write -P 23 0 64k' "$TEST_IMG" | _filter_qemu_io
123aa93c834SMax Reitz
124aa93c834SMax Reitz# Check metadata correctness
125aa93c834SMax Reitz_check_test_img
126aa93c834SMax Reitz
127aa93c834SMax Reitz# Check data correctness
128aa93c834SMax Reitz$QEMU_IO -c 'read -P 23 0 64k' "$TEST_IMG" | _filter_qemu_io
129aa93c834SMax Reitz$QEMU_IMG snapshot -a foo "$TEST_IMG"
130aa93c834SMax Reitz$QEMU_IO -c 'read -P 0 0 64k' "$TEST_IMG" | _filter_qemu_io
131aa93c834SMax Reitz
132aa93c834SMax Reitz_cleanup_test_img
133aa93c834SMax Reitz
134aa93c834SMax Reitz
135aa93c834SMax Reitzecho
136aa93c834SMax Reitzecho '=== Consecutive write to a preallocated zero cluster ==='
137aa93c834SMax Reitzecho
138aa93c834SMax Reitz
139aa93c834SMax Reitz_make_test_img 192k
140aa93c834SMax Reitz
141aa93c834SMax Reitz# Create three normal clusters
142aa93c834SMax Reitz$QEMU_IO -c 'write -P 42 0 192k' "$TEST_IMG" | _filter_qemu_io
143aa93c834SMax Reitzorig_map=$($QEMU_IMG map --output=json "$TEST_IMG")
144aa93c834SMax Reitz
145aa93c834SMax Reitz# Make the middle cluster a preallocated zero cluster
146aa93c834SMax Reitz$QEMU_IO -c 'write -z 64k 64k' "$TEST_IMG" | _filter_qemu_io
147aa93c834SMax Reitz
148aa93c834SMax Reitz# Try to overwrite everything: This should reuse the whole range. To test that
149aa93c834SMax Reitz# this only issues a single continuous write request, use blkdebug.
150aa93c834SMax Reitz$QEMU_IO -c 'write -P 42 0 192k' \
151aa93c834SMax Reitz    "json:{
152aa93c834SMax Reitz        'driver': '$IMGFMT',
153aa93c834SMax Reitz        'file': {
154aa93c834SMax Reitz            'driver': 'blkdebug',
155aa93c834SMax Reitz            'image.filename': '$TEST_IMG',
156aa93c834SMax Reitz            'set-state': [{
157aa93c834SMax Reitz                'event': 'write_aio',
158aa93c834SMax Reitz                'new_state': 2
159aa93c834SMax Reitz            }],
160aa93c834SMax Reitz            'inject-error': [{
161aa93c834SMax Reitz                'event': 'write_aio',
162aa93c834SMax Reitz                'state': 2
163aa93c834SMax Reitz            }]
164aa93c834SMax Reitz        }
165aa93c834SMax Reitz    }" \
166aa93c834SMax Reitz    | _filter_qemu_io
167aa93c834SMax Reitz
168aa93c834SMax Reitz# Check metadata correctness
169aa93c834SMax Reitz_check_test_img
170aa93c834SMax Reitz
171aa93c834SMax Reitz# Check that we have actually reused the original area
172aa93c834SMax Reitznew_map=$($QEMU_IMG map --output=json "$TEST_IMG")
173aa93c834SMax Reitzif [ "$new_map" = "$orig_map" ]; then
174aa93c834SMax Reitz    echo 'Successfully reused original clusters.'
175aa93c834SMax Reitzelse
176aa93c834SMax Reitz    echo 'Failed to reuse original clusters.'
177aa93c834SMax Reitz    echo 'Original map:'
178aa93c834SMax Reitz    echo "$orig_map"
179aa93c834SMax Reitz    echo 'New map:'
180aa93c834SMax Reitz    echo "$new_map"
181aa93c834SMax Reitzfi
182aa93c834SMax Reitz
183aa93c834SMax Reitz_cleanup_test_img
184aa93c834SMax Reitz
185975a93c0SMax Reitz
186975a93c0SMax Reitz# success, all done
187975a93c0SMax Reitzecho "*** done"
188975a93c0SMax Reitzrm -f $seq.full
189975a93c0SMax Reitzstatus=0
190