4 # Test qemu-img convert --salvage
6 # Copyright (C) 2019 Red Hat, Inc.
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 owner
=mreitz@redhat.com
26 echo "QA output created by $seq"
28 status
=1 # failure is the default!
34 trap "_cleanup; exit \$status" 0 1 2 3 15
36 # get standard environment, filters and checks
41 _supported_fmt generic
44 _unsupported_imgopts
"subformat=streamOptimized"
46 if [ "$IMGOPTSSYNTAX" = "true" ]; then
47 # We use json:{} filenames here, so we cannot work with additional options.
48 _unsupported_fmt
$IMGFMT
50 # - With VDI, the output is ordered differently. Just disable it.
51 # - VHDX has large clusters; because qemu-img convert tries to
52 # align the requests to the cluster size, the output is ordered
53 # differently, so disable it, too.
54 _unsupported_fmt vdi vhdx
58 TEST_IMG
="$TEST_IMG.orig" _make_test_img
64M
60 $QEMU_IO -c 'write -P 42 0 64M' "$TEST_IMG.orig" | _filter_qemu_io
65 # Offsets on which to fail block-status. Keep in ascending order so
66 # the indexing done by _filter_offsets will appear in ascending order
67 # in the output as well.
68 status_fail_offsets
="$((16 * 1024 * 1024 + 8192))
69 $((33 * 1024 * 1024 + 512))"
71 # Offsets on which to fail reads. Keep in ascending order for the
73 # The second element is shared with $status_fail_offsets on purpose.
74 # Starting with the third element, we test what happens when a
75 # continuous range of sectors is inaccessible.
76 read_fail_offsets
="$((32 * 1024 * 1024 - 65536))
77 $((33 * 1024 * 1024 + 512))
78 $(seq $((34 * 1024 * 1024)) $sector_size \
79 $((34 * 1024 * 1024 + 4096 - $sector_size)))"
82 # blkdebug must be above the format layer so it can intercept all
84 source_img
="json:{'driver': 'blkdebug',
89 'filename': '$TEST_IMG.orig'
94 for ofs
in $status_fail_offsets
96 source_img
+="{ 'event': 'none',
97 'iotype': 'block-status',
99 'sector': $((ofs / sector_size)) },"
102 for ofs
in $read_fail_offsets
104 source_img
+="{ 'event': 'none',
107 'sector': $((ofs / sector_size)) },"
110 # Remove the trailing comma and terminate @inject-error and json:{}
111 source_img
="${source_img%,} ] }"
123 filters
+=" -e s/$ofs/status_fail_offset_$index/"
130 filters
+=" -e s/$ofs/read_fail_offset_$index/"
137 # While determining the number of allocated sectors in the input
138 # image, we should see one block status warning per element of
139 # $status_fail_offsets.
141 # Then, the image is read. Since the block status is queried in
142 # basically the same way, the same warnings as in the previous step
143 # should reappear. Interleaved with those we should see a read
144 # warning per element of $read_fail_offsets.
145 # Note that $read_fail_offsets and $status_fail_offsets share an
146 # element (read_fail_offset_1 == status_fail_offset_1), so
147 # "status_fail_offset_1" in the output is the same as
148 # "read_fail_offset_1".
149 $QEMU_IMG convert
--salvage "$source_img" "$TEST_IMG" 2>&1 \
150 | _filter_offsets
"$status_fail_offsets" "$read_fail_offsets"
154 # The offsets where the block status could not be determined should
155 # have been treated as containing data and thus should be correct in
157 # The offsets where reading failed altogether should be 0. Make them
158 # 0 in the input image, too, so we can compare both images.
159 for ofs
in $read_fail_offsets
161 $QEMU_IO -c "write -z $ofs $sector_size" "$TEST_IMG.orig" \
163 | _filter_offsets
'' "$read_fail_offsets"
168 # These should be equal now.
169 $QEMU_IMG compare
"$TEST_IMG.orig" "$TEST_IMG"