xref: /qemu/tests/qemu-iotests/251 (revision 0b1eb0ce7d734affdc181ca583d12947d17e47bf)
1*0b1eb0ceSMax Reitz#!/usr/bin/env bash
2*0b1eb0ceSMax Reitz#
3*0b1eb0ceSMax Reitz# Test qemu-img convert --salvage
4*0b1eb0ceSMax Reitz#
5*0b1eb0ceSMax Reitz# Copyright (C) 2019 Red Hat, Inc.
6*0b1eb0ceSMax Reitz#
7*0b1eb0ceSMax Reitz# This program is free software; you can redistribute it and/or modify
8*0b1eb0ceSMax Reitz# it under the terms of the GNU General Public License as published by
9*0b1eb0ceSMax Reitz# the Free Software Foundation; either version 2 of the License, or
10*0b1eb0ceSMax Reitz# (at your option) any later version.
11*0b1eb0ceSMax Reitz#
12*0b1eb0ceSMax Reitz# This program is distributed in the hope that it will be useful,
13*0b1eb0ceSMax Reitz# but WITHOUT ANY WARRANTY; without even the implied warranty of
14*0b1eb0ceSMax Reitz# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*0b1eb0ceSMax Reitz# GNU General Public License for more details.
16*0b1eb0ceSMax Reitz#
17*0b1eb0ceSMax Reitz# You should have received a copy of the GNU General Public License
18*0b1eb0ceSMax Reitz# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19*0b1eb0ceSMax Reitz#
20*0b1eb0ceSMax Reitz
21*0b1eb0ceSMax Reitz# creator
22*0b1eb0ceSMax Reitzowner=mreitz@redhat.com
23*0b1eb0ceSMax Reitz
24*0b1eb0ceSMax Reitzseq=$(basename $0)
25*0b1eb0ceSMax Reitzecho "QA output created by $seq"
26*0b1eb0ceSMax Reitz
27*0b1eb0ceSMax Reitzstatus=1	# failure is the default!
28*0b1eb0ceSMax Reitz
29*0b1eb0ceSMax Reitz_cleanup()
30*0b1eb0ceSMax Reitz{
31*0b1eb0ceSMax Reitz    _cleanup_test_img
32*0b1eb0ceSMax Reitz}
33*0b1eb0ceSMax Reitztrap "_cleanup; exit \$status" 0 1 2 3 15
34*0b1eb0ceSMax Reitz
35*0b1eb0ceSMax Reitz# get standard environment, filters and checks
36*0b1eb0ceSMax Reitz. ./common.rc
37*0b1eb0ceSMax Reitz. ./common.filter
38*0b1eb0ceSMax Reitz. ./common.qemu
39*0b1eb0ceSMax Reitz
40*0b1eb0ceSMax Reitz_supported_fmt generic
41*0b1eb0ceSMax Reitz_supported_proto file
42*0b1eb0ceSMax Reitz_supported_os Linux
43*0b1eb0ceSMax Reitz
44*0b1eb0ceSMax Reitzif [ "$IMGOPTSSYNTAX" = "true" ]; then
45*0b1eb0ceSMax Reitz    # We use json:{} filenames here, so we cannot work with additional options.
46*0b1eb0ceSMax Reitz    _unsupported_fmt $IMGFMT
47*0b1eb0ceSMax Reitzelse
48*0b1eb0ceSMax Reitz    # With VDI, the output is ordered differently.  Just disable it.
49*0b1eb0ceSMax Reitz    _unsupported_fmt vdi
50*0b1eb0ceSMax Reitzfi
51*0b1eb0ceSMax Reitz
52*0b1eb0ceSMax Reitz
53*0b1eb0ceSMax ReitzTEST_IMG="$TEST_IMG.orig" _make_test_img 64M
54*0b1eb0ceSMax Reitz
55*0b1eb0ceSMax Reitz$QEMU_IO -c 'write -P 42 0 64M' "$TEST_IMG.orig" | _filter_qemu_io
56*0b1eb0ceSMax Reitz
57*0b1eb0ceSMax Reitz
58*0b1eb0ceSMax Reitzsector_size=512
59*0b1eb0ceSMax Reitz
60*0b1eb0ceSMax Reitz# Offsets on which to fail block-status.  Keep in ascending order so
61*0b1eb0ceSMax Reitz# the indexing done by _filter_offsets will appear in ascending order
62*0b1eb0ceSMax Reitz# in the output as well.
63*0b1eb0ceSMax Reitzstatus_fail_offsets="$((16 * 1024 * 1024 + 8192))
64*0b1eb0ceSMax Reitz                     $((33 * 1024 * 1024 + 512))"
65*0b1eb0ceSMax Reitz
66*0b1eb0ceSMax Reitz# Offsets on which to fail reads.  Keep in ascending order for the
67*0b1eb0ceSMax Reitz# same reason.
68*0b1eb0ceSMax Reitz# The second element is shared with $status_fail_offsets on purpose.
69*0b1eb0ceSMax Reitz# Starting with the third element, we test what happens when a
70*0b1eb0ceSMax Reitz# continuous range of sectors is inaccessible.
71*0b1eb0ceSMax Reitzread_fail_offsets="$((32 * 1024 * 1024 - 65536))
72*0b1eb0ceSMax Reitz                   $((33 * 1024 * 1024 + 512))
73*0b1eb0ceSMax Reitz                   $(seq $((34 * 1024 * 1024)) $sector_size \
74*0b1eb0ceSMax Reitz                         $((34 * 1024 * 1024 + 4096 - $sector_size)))"
75*0b1eb0ceSMax Reitz
76*0b1eb0ceSMax Reitz
77*0b1eb0ceSMax Reitz# blkdebug must be above the format layer so it can intercept all
78*0b1eb0ceSMax Reitz# block-status events
79*0b1eb0ceSMax Reitzsource_img="json:{'driver': 'blkdebug',
80*0b1eb0ceSMax Reitz                  'image': {
81*0b1eb0ceSMax Reitz                      'driver': '$IMGFMT',
82*0b1eb0ceSMax Reitz                      'file': {
83*0b1eb0ceSMax Reitz                          'driver': 'file',
84*0b1eb0ceSMax Reitz                          'filename': '$TEST_IMG.orig'
85*0b1eb0ceSMax Reitz                      }
86*0b1eb0ceSMax Reitz                  },
87*0b1eb0ceSMax Reitz                  'inject-error': ["
88*0b1eb0ceSMax Reitz
89*0b1eb0ceSMax Reitzfor ofs in $status_fail_offsets
90*0b1eb0ceSMax Reitzdo
91*0b1eb0ceSMax Reitz    source_img+="{ 'event': 'none',
92*0b1eb0ceSMax Reitz                   'iotype': 'block-status',
93*0b1eb0ceSMax Reitz                   'errno': 5,
94*0b1eb0ceSMax Reitz                   'sector': $((ofs / sector_size)) },"
95*0b1eb0ceSMax Reitzdone
96*0b1eb0ceSMax Reitz
97*0b1eb0ceSMax Reitzfor ofs in $read_fail_offsets
98*0b1eb0ceSMax Reitzdo
99*0b1eb0ceSMax Reitz    source_img+="{ 'event': 'none',
100*0b1eb0ceSMax Reitz                   'iotype': 'read',
101*0b1eb0ceSMax Reitz                   'errno': 5,
102*0b1eb0ceSMax Reitz                   'sector': $((ofs / sector_size)) },"
103*0b1eb0ceSMax Reitzdone
104*0b1eb0ceSMax Reitz
105*0b1eb0ceSMax Reitz# Remove the trailing comma and terminate @inject-error and json:{}
106*0b1eb0ceSMax Reitzsource_img="${source_img%,} ] }"
107*0b1eb0ceSMax Reitz
108*0b1eb0ceSMax Reitz
109*0b1eb0ceSMax Reitzecho
110*0b1eb0ceSMax Reitz
111*0b1eb0ceSMax Reitz
112*0b1eb0ceSMax Reitz_filter_offsets() {
113*0b1eb0ceSMax Reitz    filters=
114*0b1eb0ceSMax Reitz
115*0b1eb0ceSMax Reitz    index=0
116*0b1eb0ceSMax Reitz    for ofs in $1
117*0b1eb0ceSMax Reitz    do
118*0b1eb0ceSMax Reitz        filters+=" -e s/$ofs/status_fail_offset_$index/"
119*0b1eb0ceSMax Reitz        index=$((index + 1))
120*0b1eb0ceSMax Reitz    done
121*0b1eb0ceSMax Reitz
122*0b1eb0ceSMax Reitz    index=0
123*0b1eb0ceSMax Reitz    for ofs in $2
124*0b1eb0ceSMax Reitz    do
125*0b1eb0ceSMax Reitz        filters+=" -e s/$ofs/read_fail_offset_$index/"
126*0b1eb0ceSMax Reitz        index=$((index + 1))
127*0b1eb0ceSMax Reitz    done
128*0b1eb0ceSMax Reitz
129*0b1eb0ceSMax Reitz    sed $filters
130*0b1eb0ceSMax Reitz}
131*0b1eb0ceSMax Reitz
132*0b1eb0ceSMax Reitz# While determining the number of allocated sectors in the input
133*0b1eb0ceSMax Reitz# image, we should see one block status warning per element of
134*0b1eb0ceSMax Reitz# $status_fail_offsets.
135*0b1eb0ceSMax Reitz#
136*0b1eb0ceSMax Reitz# Then, the image is read.  Since the block status is queried in
137*0b1eb0ceSMax Reitz# basically the same way, the same warnings as in the previous step
138*0b1eb0ceSMax Reitz# should reappear.  Interleaved with those we should see a read
139*0b1eb0ceSMax Reitz# warning per element of $read_fail_offsets.
140*0b1eb0ceSMax Reitz# Note that $read_fail_offsets and $status_fail_offsets share an
141*0b1eb0ceSMax Reitz# element (read_fail_offset_1 == status_fail_offset_1), so
142*0b1eb0ceSMax Reitz# "status_fail_offset_1" in the output is the same as
143*0b1eb0ceSMax Reitz# "read_fail_offset_1".
144*0b1eb0ceSMax Reitz$QEMU_IMG convert --salvage "$source_img" "$TEST_IMG" 2>&1 \
145*0b1eb0ceSMax Reitz    | _filter_offsets "$status_fail_offsets" "$read_fail_offsets"
146*0b1eb0ceSMax Reitz
147*0b1eb0ceSMax Reitzecho
148*0b1eb0ceSMax Reitz
149*0b1eb0ceSMax Reitz# The offsets where the block status could not be determined should
150*0b1eb0ceSMax Reitz# have been treated as containing data and thus should be correct in
151*0b1eb0ceSMax Reitz# the output image.
152*0b1eb0ceSMax Reitz# The offsets where reading failed altogether should be 0.  Make them
153*0b1eb0ceSMax Reitz# 0 in the input image, too, so we can compare both images.
154*0b1eb0ceSMax Reitzfor ofs in $read_fail_offsets
155*0b1eb0ceSMax Reitzdo
156*0b1eb0ceSMax Reitz    $QEMU_IO -c "write -z $ofs $sector_size" "$TEST_IMG.orig" \
157*0b1eb0ceSMax Reitz        | _filter_qemu_io \
158*0b1eb0ceSMax Reitz        | _filter_offsets '' "$read_fail_offsets"
159*0b1eb0ceSMax Reitzdone
160*0b1eb0ceSMax Reitz
161*0b1eb0ceSMax Reitzecho
162*0b1eb0ceSMax Reitz
163*0b1eb0ceSMax Reitz# These should be equal now.
164*0b1eb0ceSMax Reitz$QEMU_IMG compare "$TEST_IMG.orig" "$TEST_IMG"
165*0b1eb0ceSMax Reitz
166*0b1eb0ceSMax Reitz
167*0b1eb0ceSMax Reitz# success, all done
168*0b1eb0ceSMax Reitzecho "*** done"
169*0b1eb0ceSMax Reitzrm -f $seq.full
170*0b1eb0ceSMax Reitzstatus=0
171