1#!/usr/bin/env bash
2# group: rw quick
3#
4# Test qemu-img check for parallels format
5#
6# Copyright (C) 2022 Virtuozzo International GmbH
7#
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.
12#
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.
17#
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/>.
20#
21
22# creator
23owner=alexander.ivanov@virtuozzo.com
24
25seq=`basename $0`
26echo "QA output created by $seq"
27
28status=1	# failure is the default!
29
30_cleanup()
31{
32    _cleanup_test_img
33}
34trap "_cleanup; exit \$status" 0 1 2 3 15
35
36# get standard environment, filters and checks
37. ../common.rc
38. ../common.filter
39
40_supported_fmt parallels
41_supported_proto file
42_supported_os Linux
43
44SIZE=$((4 * 1024 * 1024))
45IMGFMT=parallels
46CLUSTER_SIZE_OFFSET=28
47DATA_OFF_OFFSET=48
48BAT_OFFSET=64
49
50_make_test_img $SIZE
51
52CLUSTER_SIZE=$(peek_file_le $TEST_IMG $CLUSTER_SIZE_OFFSET 4)
53CLUSTER_SIZE=$((CLUSTER_SIZE * 512))
54LAST_CLUSTER_OFF=$((SIZE - CLUSTER_SIZE))
55LAST_CLUSTER=$((LAST_CLUSTER_OFF/CLUSTER_SIZE))
56
57echo "== TEST OUT OF IMAGE CHECK =="
58
59echo "== write pattern =="
60{ $QEMU_IO -c "write -P 0x11 0 $SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir
61
62echo "== corrupt image =="
63cluster=$(($LAST_CLUSTER + 2))
64poke_file "$TEST_IMG" "$BAT_OFFSET" "\x$cluster\x00\x00\x00"
65
66echo "== read corrupted image with repairing =="
67{ $QEMU_IO -c "read -P 0x00 0 $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir
68
69# Clear image
70_make_test_img $SIZE
71
72echo "== TEST LEAK CHECK =="
73
74echo "== write pattern to last cluster =="
75echo "write -P 0x11 $LAST_CLUSTER_OFF $CLUSTER_SIZE"
76{ $QEMU_IO -c "write -P 0x11 $LAST_CLUSTER_OFF $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir
77
78file_size=`stat --printf="%s" "$TEST_IMG"`
79echo "file size: $file_size"
80
81echo "== extend image by 1 cluster =="
82fallocate -xl $((file_size + CLUSTER_SIZE)) "$TEST_IMG"
83
84file_size=`stat --printf="%s" "$TEST_IMG"`
85echo "file size: $file_size"
86
87echo "== repair image =="
88_check_test_img -r all
89
90file_size=`stat --printf="%s" "$TEST_IMG"`
91echo "file size: $file_size"
92
93echo "== check last cluster =="
94{ $QEMU_IO -r -c "read -P 0x11 $LAST_CLUSTER_OFF $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir
95
96# Clear image
97_make_test_img $SIZE
98
99echo "== TEST DUPLICATION CHECK =="
100
101echo "== write pattern to whole image =="
102{ $QEMU_IO -c "write -P 0x11 0 $SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir
103
104echo "== write another pattern to second cluster =="
105{ $QEMU_IO -c "write -P 0x55 $CLUSTER_SIZE $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir
106
107echo "== check second cluster =="
108{ $QEMU_IO -r -c "read -P 0x55 $CLUSTER_SIZE $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir
109
110
111echo "== corrupt image =="
112poke_file "$TEST_IMG" "$(($BAT_OFFSET + 4))" "\x01\x00\x00\x00"
113
114echo "== check second cluster =="
115{ $QEMU_IO -r -c "read -P 0x11 $CLUSTER_SIZE $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir
116
117echo "== repair image =="
118_check_test_img -r all
119
120echo "== check the first cluster =="
121{ $QEMU_IO -r -c "read -P 0x11 0 $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir
122
123echo "== check second cluster =="
124{ $QEMU_IO -r -c "read -P 0x11 $CLUSTER_SIZE $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir
125
126echo "== write another pattern to the first clusters =="
127{ $QEMU_IO -c "write -P 0x66 0 $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir
128
129echo "== check the first cluster =="
130{ $QEMU_IO -r -c "read -P 0x66 0 $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir
131
132echo "== check the second cluster (deduplicated) =="
133{ $QEMU_IO -r -c "read -P 0x11 $CLUSTER_SIZE $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir
134
135# Clear image
136_make_test_img $SIZE
137
138echo "== TEST DUPLICATION SELF-CURE =="
139
140echo "== write pattern to whole image =="
141{ $QEMU_IO -c "write -P 0x11 0 $SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir
142
143echo "== write another pattern to second cluster =="
144{ $QEMU_IO -c "write -P 0x55 $CLUSTER_SIZE $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir
145
146echo "== check second cluster =="
147{ $QEMU_IO -r -c "read -P 0x55 $CLUSTER_SIZE $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir
148
149
150echo "== corrupt image =="
151poke_file "$TEST_IMG" "$(($BAT_OFFSET + 4))" "\x01\x00\x00\x00"
152
153echo "== check second cluster =="
154{ $QEMU_IO -r -c "read -P 0x11 $CLUSTER_SIZE $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir
155
156echo "== check the first cluster with self-repair =="
157{ $QEMU_IO -c "read -P 0x11 0 $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir
158
159echo "== check second cluster =="
160{ $QEMU_IO -r -c "read -P 0x11 $CLUSTER_SIZE $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir
161
162echo "== write another pattern to the first clusters =="
163{ $QEMU_IO -c "write -P 0x66 0 $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir
164
165echo "== check the first cluster =="
166{ $QEMU_IO -r -c "read -P 0x66 0 $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir
167
168echo "== check the second cluster (deduplicated) =="
169{ $QEMU_IO -r -c "read -P 0x11 $CLUSTER_SIZE $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir
170
171# Clear image
172_make_test_img $SIZE
173
174echo "== TEST DATA_OFF CHECK =="
175
176echo "== write pattern to first cluster =="
177{ $QEMU_IO -c "write -P 0x55 0 $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir
178
179echo "== spoil data_off field =="
180poke_file "$TEST_IMG" "$DATA_OFF_OFFSET" "\xff\xff\xff\xff"
181
182echo "== check first cluster =="
183{ $QEMU_IO -c "read -P 0x55 0 $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir
184
185# Clear image
186_make_test_img $SIZE
187
188echo "== TEST DATA_OFF THROUGH REPAIR =="
189
190echo "== write pattern to first cluster =="
191{ $QEMU_IO -c "write -P 0x55 0 $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir
192
193echo "== spoil data_off field =="
194poke_file "$TEST_IMG" "$DATA_OFF_OFFSET" "\xff\xff\xff\xff"
195
196echo "== repair image =="
197_check_test_img -r all
198
199echo "== check first cluster =="
200{ $QEMU_IO -r -c "read -P 0x55 0 $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir
201
202# success, all done
203echo "*** done"
204rm -f $seq.full
205status=0
206