-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.sh
executable file
·270 lines (228 loc) · 5.05 KB
/
test.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/bin/bash
cd "$(dirname "$0")"
# load state - loop device
if [ -f "loop.txt.test" ]; then
LOOP="$(cat loop.txt.test)"
fi
# load state - original md5
if [ -f "md5.txt.test" ]; then
MD5_ORIGINAL="$(cat md5.txt.test)"
fi
# check whether last command was successful
CATCH() {
if ! [ $? -eq 0 ]
then
exit 1
fi
echo $1
}
#
# INSTALL SW
#
install() {
python3 setup.py install
}
#
# FILESYSTEM CREATE
#
fs_create() {
FS="$1"
dd if=/dev/zero of=data_fs.img.test bs=1M count=250
CATCH "[OK] Created empty file."
if [ -z "$FS" ]; then
$FS="ext4"
elif ! [[ $FS =~ ^ext[2-4]$ ]]; then
echo "Unknown filesystem $FS. Only ext2, ext3, ext4 are supported."
exit
fi
eval "mkfs.$FS data_fs.img.test"
CATCH "[OK] Created $FS filesystem."
}
#
# FILESYSTEM UP
#
fs_up() {
if [ "$EUID" -ne 0 ]
then
echo "Please run as root"
exit 1
fi
# Create block device (/dev/loop0).
# losetup -a
LOOP="$(losetup -fP --show data_fs.img.test)"
CATCH "[OK] Created block device: $LOOP"
mkdir data_fs
CATCH "[OK] Created directory data_fs."
mount "$LOOP" data_fs
CATCH "[OK] Mounted $LOOP to data_fs."
chmod -R 777 data_fs data_fs.img.test
# save state
echo "$LOOP" > "loop.txt.test"
}
#
# FILESYSTEM DOWN
#
fs_down() {
if [ "$EUID" -ne 0 ]
then
echo "Please run as root"
exit 1
fi
umount data_fs
CATCH "[OK] Umounted data_fs."
rmdir data_fs
CATCH "[OK] Removed directory data_fs."
# detach loop device
losetup -d "$LOOP"
CATCH "[OK] Detached block device."
# remove fs image & snapshot
rm -f data_fs.img.test
CATCH "[OK] Removed filesystem image."
# clear state
rm -f loop.txt.test md5.txt.test
}
#
# CREATE TEST FILE
#
create_file() {
# remove pad files if existed
rm -f data_fs/pad_file_*
rm -rf data_fs/test
# add 15 random files
i="0"
while [ $i -lt 15 ]; do
dd if=/dev/zero of="data_fs/pad_file_$i" bs=1M count=10
CATCH "[OK] Created file: pad_file_$i"
i=$[$i+1]
done
# remove every second file
i="0"
while [ $i -lt 15 ]; do
rm -f "data_fs/pad_file_$i"
CATCH "[OK] Removed file: pad_file_$i"
i=$[$i+2]
done
# add big file
mkdir -p data_fs/test/dir
CATCH "[OK] Created test direcotry."
dd if=/dev/urandom of=data_fs/test/dir/tested_file bs=1M count=70
CATCH "[OK] Created test file with random data."
# save md5
MD5_ORIGINAL=$(md5sum "data_fs/test/dir/tested_file" | cut -d " " -f1)
CATCH "[OK] Computed MD5 of test file: $MD5_ORIGINAL"
# sync fs
sync -f data_fs
CATCH "[OK] Synced."
# save state
CATCH "$MD5_ORIGINAL" > "md5.txt.test"
}
#
# REMOVE TEST FILE
#
remove_file() {
# remove
rm -rf data_fs/test
CATCH "[OK] Recursively removed test folder."
# sync fs
sync -f data_fs
CATCH "[OK] Synced."
}
#
# CREATE SNAPSHOT
#
snapshot() {
ext4-backup-pointers create -i data_fs.img.test -o snapshot.out.test
CATCH "[OK] Created snapshot."
}
#
# RESTORE FILE
#
restore() {
ext4-backup-pointers recover -i data_fs.img.test -s snapshot.out.test /test/dir/tested_file -o recovered_file
CATCH "[OK] Recovered test file."
MD5_RECOVERED=$(md5sum "recovered_file" | cut -d " " -f1)
# remove fs image & others
rm -f recovered_file
response="Original: $MD5_ORIGINAL\nRecovered: $MD5_RECOVERED"
if [[ $MD5_ORIGINAL == $MD5_RECOVERED ]]
then
echo -e "\n\e[92mSUCCESS\e[39m\n$response"
exit 0
else
echo -e "\n\e[91mFAILURE\e[39m\n$response"
exit 1
fi
}
case $1 in
install) install;;
create_file) create_file;;
remove_file) remove_file;;
snapshot) snapshot;;
restore) restore;;
full)
install
if ! [ -f "loop.txt.test" ]; then
fs_create "$2"
fs_up
elif ! losetup "$LOOP"; then
echo "Filesystem is already created, mountig..."
fs_up
fi
create_file
snapshot
remove_file
restore
fs_down
;;
run)
if ! [ -f "loop.txt.test" ]; then
echo "Filesystem must be created first..."
exit 1
elif ! losetup "$LOOP"; then
echo "Filesystem is created, but loop device is not up..."
exit 1
fi
install
create_file
snapshot
remove_file
restore
;;
setup)
if ! [ -f "loop.txt.test" ]; then
fs_create "$2"
fs_up
elif ! losetup "$LOOP"; then
echo "Filesystem is already created, mountig..."
fs_up
else
echo "Filesystem is already created and mounted..."
fi
;;
clear)
if [ -f "loop.txt.test" ]; then
fs_down
else
echo "Nothing to clear..."
fi
;;
*)
echo '-- use it like this --'
echo ''
echo './test.sh setup [ext4] # setup test filesystem. (run as root)'
echo ' # - optional: (ext2, ext3, ext4)'
echo './test.sh run # run rests.'
echo './test.sh clear # clear test filesystem. (run as root)'
echo './test.sh full [ext4] # create & test & clear. (run as root)'
echo ' # - optional: (ext2, ext3, ext4)'
echo ''
echo '-- or run test steps individualy --'
echo ''
echo './test.sh install # python install src.'
echo './test.sh create_file # create test file.'
echo './test.sh snapshot # create snapshot.'
echo './test.sh remove_file # remove test file.'
echo './test.sh restore # restore removed file from snapshot.'
echo ''
;;
esac