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