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