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 -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 -c "read -P 0x55 $CLUSTER_SIZE $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir
109
110echo "== corrupt image =="
111poke_file "$TEST_IMG" "$(($BAT_OFFSET + 4))" "\x01\x00\x00\x00"
112
113echo "== check second cluster =="
114{ $QEMU_IO -c "read -P 0x11 $CLUSTER_SIZE $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir
115
116echo "== repair image =="
117_check_test_img -r all
118
119echo "== check second cluster =="
120{ $QEMU_IO -c "read -P 0x11 $CLUSTER_SIZE $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir
121
122echo "== check first cluster on host =="
123printf "content: 0x%02x\n" `peek_file_le $TEST_IMG $(($CLUSTER_SIZE)) 1`
124
125echo "== check second cluster on host =="
126printf "content: 0x%02x\n" `peek_file_le $TEST_IMG $(($CLUSTER_SIZE)) 1`
127
128# Clear image
129_make_test_img $SIZE
130
131echo "== TEST DATA_OFF CHECK =="
132
133echo "== write pattern to first cluster =="
134{ $QEMU_IO -c "write -P 0x55 0 $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir
135
136echo "== spoil data_off field =="
137poke_file "$TEST_IMG" "$DATA_OFF_OFFSET" "\xff\xff\xff\xff"
138
139echo "== check first cluster =="
140{ $QEMU_IO -c "read -P 0x55 0 $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir
141
142# success, all done
143echo "*** done"
144rm -f $seq.full
145status=0
146