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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
|
#!/usr/bin/env bash
#
# Copyright (C) 2015 Ubuntu Kylin
#
# Author: Min Chen <minchen@ubuntukylin.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Library Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Library Public License for more details.
#
# unit test case for rbd-recover-tool
#prepare:
# - write config files: config/osd_host, config/mon_host, config/storage_path, config/mds_host if exist mds
#step 1. rbd export all images as you need
#step 2. stop all ceph services
#step 3. use ceph_rbd_recover_tool to recover all images
#step 4. compare md5sum of recover image with that of export image who has the same image name
ssh_opt="-o ConnectTimeout=1"
my_dir=$(dirname "$0")
tool_dir=$my_dir
#storage_path=$my_dir/config/storage_path
mon_host=$my_dir/config/mon_host
osd_host=$my_dir/config/osd_host
mds_host=$my_dir/config/mds_host
test_dir= # `cat $storage_path`
export_dir= #$test_dir/export
recover_dir= #$test_dir/recover
image_names= #$test_dir/image_names
online_images= #$test_dir/online_images, all images on ceph rbd pool
gen_db= #$test_dir/gen_db, label database if exist
pool=rbd
pool_id=2
function get_pool_id()
{
local pool_id_file=/tmp/pool_id_file.$$$$
ceph osd pool stats $pool|head -n 1|awk '{print $4}' >$pool_id_file
if [ $? -ne 0 ];then
echo "$func: get pool id failed: pool = $pool"
rm -f $pool_id_file
exit
fi
pool_id=`cat $pool_id_file`
echo "$func: pool_id = $pool_id"
rm -f $pool_id_file
}
function init()
{
local func="init"
if [ $# -eq 0 ];then
echo "$func: must input <path> to storage images, enough disk space is good"
exit
fi
if [ ! -s $osd_host ];then
echo "$func: config/osd_host not exists or empty"
exit
fi
if [ ! -s $mon_host ];then
echo "$func: config/mon_host not exists or empty"
exit
fi
if [ ! -e $mds_host ];then
echo "$func: config/mds_host not exists"
exit
fi
test_dir=$1
export_dir=$test_dir/export
recover_dir=$test_dir/recover
image_names=$test_dir/image_names
online_images=$test_dir/online_images
gen_db=$test_dir/gen_db
trap 'echo "ceph cluster is stopped ..."; exit;' INT
ceph -s >/dev/null
get_pool_id
mkdir -p $test_dir
mkdir -p $export_dir
mkdir -p $recover_dir
rm -rf $export_dir/*
rm -rf $recover_dir/*
}
function do_gen_database()
{
local func="do_gen_database"
if [ -s $gen_db ] && [ `cat $gen_db` = 1 ];then
echo "$func: database already existed"
exit
fi
bash $tool_dir/rbd-recover-tool database
echo 1 >$gen_db
}
#check if all ceph processes are stopped
function check_ceph_service()
{
local func="check_ceph_service"
local res=`cat $osd_host $mon_host $mds_host|sort -u|tr -d [:blank:]|xargs -n 1 -I @ ssh $ssh_opt @ "ps aux|grep -E \"(ceph-osd|ceph-mon|ceph-mds)\"|grep -v grep"`
if [ "$res"x != ""x ];then
echo "$func: NOT all ceph services are stopped"
return 1
exit
fi
echo "$func: all ceph services are stopped"
return 0
}
function stop_ceph()
{
local func="stop_ceph"
#cat osd_host|xargs -n 1 -I @ ssh $ssh_opt @ "killall ceph-osd"
while read osd
do
{
osd=`echo $osd|tr -d [:blank:]`
if [ "$osd"x = ""x ];then
continue
fi
#ssh $ssh_opt $osd "killall ceph-osd ceph-mon ceph-mds" </dev/null
ssh $ssh_opt $osd "killall ceph-osd" </dev/null
} &
done < $osd_host
wait
echo "waiting kill all osd ..."
sleep 1
#cat $mon_host|xargs -n 1 -I @ ssh $ssh_opt @ "killall ceph-mon ceph-osd ceph-mds"
cat $mon_host|xargs -n 1 -I @ ssh $ssh_opt @ "killall ceph-mon"
#cat $mds_host|xargs -n 1 -I @ ssh $ssh_opt @ "killall ceph-mds ceph-mon ceph-osd"
cat $mds_host|xargs -n 1 -I @ ssh $ssh_opt @ "killall ceph-mds"
}
function create_image()
{
local func="create_image"
if [ ${#} -lt 3 ];then
echo "create_image: parameters: <image_name> <size> <image_format>"
exit
fi
local image_name=$1
local size=$2
local image_format=$3
if [ $image_format -lt 1 ] || [ $image_format -gt 2 ];then
echo "$func: image_format must be 1 or 2"
exit
fi
local res=`rbd list|grep -E "^$1$"`
echo "$func $image_name ..."
if [ "$res"x = ""x ];then
rbd -p $pool create $image_name --size $size --image_format $image_format
else
if [ $image_format -eq 2 ];then
rbd snap ls $image_name|tail -n +2|awk '{print $2}'|xargs -n 1 -I % rbd snap unprotect $image_name@%
fi
rbd snap purge $image_name
#rbd rm $image_name
rbd -p $pool resize --allow-shrink --size $size $image_name
fi
}
function export_image()
{
local func="export_image"
if [ $# -lt 2 ];then
echo "$func: parameters: <image_name> <image_format> [<image_size>]"
exit
fi
local image_name=$1
local format=$(($2))
local size=$(($3)) #MB
if [ $format -ne 1 ] && [ $format -ne 2 ];then
echo "$func: image format must be 1 or 2"
exit
fi
if [ $size -eq 0 ];then
size=24 #MB
echo "$func: size = $size"
fi
local mnt=/rbdfuse
mount |grep "rbd-fuse on /rbdfuse" &>/dev/null
if [ $? -ne 0 ];then
rbd-fuse $mnt
fi
create_image $image_name $size $format
dd conv=notrunc if=/dev/urandom of=$mnt/$image_name bs=4M count=$(($size/4))
local export_image_dir=$export_dir/pool_$pool_id/$image_name
mkdir -p $export_image_dir
local export_md5_nosnap=$export_image_dir/@md5_nosnap
>$export_md5_nosnap
local export_image_path=$export_image_dir/$image_name
rm -f $export_image_path
rbd export $pool/$image_name $export_image_path
md5sum $export_image_path |awk '{print $1}' >$export_md5_nosnap
}
function recover_image()
{
local func="recover_snapshots"
if [ $# -lt 1 ];then
echo "$func: parameters: <image_name>"
exit
fi
local image_name=$1
#pool_id=29
local recover_image_dir=$recover_dir/pool_$pool_id/$image_name
mkdir -p $recover_image_dir
local recover_md5_nosnap=$recover_image_dir/@md5_nosnap
>$recover_md5_nosnap
local snapshot=
bash $tool_dir/rbd-recover-tool recover $pool_id/$image_name $recover_dir
md5sum $recover_image_dir/$image_name|awk '{print $1}' >$recover_md5_nosnap
}
function make_snapshot()
{
local func="make_snapshot"
if [ $# -lt 5 ];then
echo "$func: parameters: <ofile> <seek> <count> <snap> <export_image_dir>"
exit
fi
local ofile=$1
local seek=$(($2))
local count=$(($3))
local snap=$4
local export_image_dir=$5
if [ $seek -lt 0 ];then
echo "$func: seek can not be minus"
exit
fi
if [ $count -lt 1 ];then
echo "$func: count must great than zero"
exit
fi
echo "[$snap] $func ..."
echo "$1 $2 $3 $4"
rbd snap ls $image_name|grep $snap;
local res=$?
if [ $res -eq 0 ];then
return $res
fi
dd conv=notrunc if=/dev/urandom of=$ofile bs=1M count=$count seek=$seek 2>/dev/null
snapshot=$image_name@$snap
rbd snap create $snapshot
rm -f $export_image_dir/$snapshot
rbd export $pool/$image_name $export_image_dir/$snapshot
pushd $export_image_dir >/dev/null
md5sum $snapshot >> @md5
popd >/dev/null
}
function recover_snapshots()
{
local func="recover_snapshots"
if [ $# -lt 1 ];then
echo "$func: parameters: <image_name>"
exit
fi
local image_name=$1
#pool_id=29
local recover_image_dir=$recover_dir/pool_$pool_id/$image_name
mkdir -p $recover_image_dir
local recover_md5=$recover_image_dir/@md5
>$recover_md5
local snapshot=
# recover head
bash $tool_dir/rbd-recover-tool recover $pool_id/$image_name $recover_dir
# recover snapshots
for((i=1; i<10; i++))
do
snapshot=snap$i
bash $tool_dir/rbd-recover-tool recover $pool_id/$image_name@$snapshot $recover_dir
pushd $recover_image_dir >/dev/null
local chksum=`md5sum $image_name|awk '{print $1}'`
echo "$chksum $image_name@$snapshot" >>@md5
popd >/dev/null
done
}
function export_snapshots()
{
local func="export_snapshots"
if [ $# -lt 2 ];then
echo "$func: parameters: <image_name> <image_format> [<image_size>]"
exit
fi
local image_name=$1
local format=$(($2))
local size=$(($3)) #MB
if [ $format -ne 1 ] && [ $format -ne 2 ];then
echo "$func: image format must be 1 or 2"
exit
fi
if [ $size -eq 0 ];then
size=24 #MB
echo "$func: size = $size"
fi
local mnt=/rbdfuse
mount |grep "rbd-fuse on /rbdfuse" &>/dev/null
if [ $? -ne 0 ];then
rbd-fuse $mnt
fi
create_image $image_name $size $format
local export_image_dir=$export_dir/pool_$pool_id/$image_name
mkdir -p $export_image_dir
local export_md5=$export_image_dir/@md5
>$export_md5
# create 9 snapshots
# image = {object0, object1, object2, object3, object4, object5, ...}
#
# snap1 : init/write all objects
# snap2 : write object0
# snap3 : write object1
# snap4 : write object2
# snap5 : write object3
# snap6 : write object4
# snap7 : write object5
# snap8 : write object0
# snap9 : write object3
make_snapshot $mnt/$image_name 0 $size snap1 $export_image_dir
make_snapshot $mnt/$image_name 0 1 snap2 $export_image_dir
make_snapshot $mnt/$image_name 4 1 snap3 $export_image_dir
make_snapshot $mnt/$image_name 8 1 snap4 $export_image_dir
make_snapshot $mnt/$image_name 12 1 snap5 $export_image_dir
make_snapshot $mnt/$image_name 16 1 snap6 $export_image_dir
make_snapshot $mnt/$image_name 20 1 snap7 $export_image_dir
make_snapshot $mnt/$image_name 1 1 snap8 $export_image_dir
make_snapshot $mnt/$image_name 13 1 snap9 $export_image_dir
}
function check_recover_nosnap()
{
local func="check_recover_nosnap"
if [ $# -lt 3 ];then
echo "$func: parameters: <export_md5_file> <recover_md5_file> <image_name>"
fi
local export_md5=$1
local recover_md5=$2
local image_name=$3
local ifpassed="FAILED"
echo "================ < $image_name nosnap > ================"
local export_md5sum=`cat $export_md5`
local recover_md5sum=`cat $recover_md5`
if [ "$export_md5sum"x != ""x ] && [ "$export_md5sum"x = "$recover_md5sum"x ];then
ifpassed="PASSED"
fi
echo "export: $export_md5sum"
echo "recover: $recover_md5sum $ifpassed"
}
function check_recover_snapshots()
{
local func="check_recover_snapshots"
if [ $# -lt 3 ];then
echo "$func: parameters: <export_md5_file> <recover_md5_file> <image_name>"
fi
local export_md5=$1
local recover_md5=$2
local image_name=$3
local ifpassed="FAILED"
echo "================ < $image_name snapshots > ================"
OIFS=$IFS
IFS=$'\n'
local export_md5s=(`cat $export_md5`)
local recover_md5s=(`cat $recover_md5`)
for((i=0; i<9; i++))
do
OOIFS=$IFS
IFS=$' '
local x=$(($i+1))
snapshot=snap$x
local export_arr=(`echo ${export_md5s[$i]}`)
local recover_arr=(`echo ${recover_md5s[$i]}`)
echo "export: ${export_md5s[$i]}"
if [ "${export_arr[1]}"x != ""x ] && [ "${export_arr[1]}"x = "${recover_arr[1]}"x ];then
ifpassed="PASSED"
fi
echo "recover: ${recover_md5s[$i]} $ifpassed"
IFS=$OOIFS
done
IFS=$OIFS
}
# step 1: export image, snapshot
function do_export_nosnap()
{
export_image image_v1_nosnap 1
export_image image_v2_nosnap 2
}
function do_export_snap()
{
export_snapshots image_v1_snap 1
export_snapshots image_v2_snap 2
}
# step 2: stop ceph cluster and gen database
function stop_cluster_gen_database()
{
trap 'echo stop ceph cluster failed; exit;' INT HUP
stop_ceph
sleep 2
check_ceph_service
local res=$?
while [ $res -ne 0 ]
do
stop_ceph
sleep 2
check_ceph_service
res=$?
done
echo 0 >$gen_db
do_gen_database
}
# step 3: recover image,snapshot
function do_recover_nosnap()
{
recover_image image_v1_nosnap
recover_image image_v2_nosnap
}
function do_recover_snap()
{
recover_snapshots image_v1_snap
recover_snapshots image_v2_snap
}
# step 4: check md5sum pair<export_md5sum, recover_md5sum>
function do_check_recover_nosnap()
{
local image1=image_v1_nosnap
local image2=image_v2_nosnap
local export_md5_1=$export_dir/pool_$pool_id/$image1/@md5_nosnap
local export_md5_2=$export_dir/pool_$pool_id/$image2/@md5_nosnap
local recover_md5_1=$recover_dir/pool_$pool_id/$image1/@md5_nosnap
local recover_md5_2=$recover_dir/pool_$pool_id/$image2/@md5_nosnap
check_recover_nosnap $export_md5_1 $recover_md5_1 $image1
check_recover_nosnap $export_md5_2 $recover_md5_2 $image2
}
function do_check_recover_snap()
{
local image1=image_v1_snap
local image2=image_v2_snap
local export_md5_1=$export_dir/pool_$pool_id/$image1/@md5
local export_md5_2=$export_dir/pool_$pool_id/$image2/@md5
local recover_md5_1=$recover_dir/pool_$pool_id/$image1/@md5
local recover_md5_2=$recover_dir/pool_$pool_id/$image2/@md5
check_recover_snapshots $export_md5_1 $recover_md5_1 $image1
check_recover_snapshots $export_md5_2 $recover_md5_2 $image2
}
function test_case_1()
{
do_export_nosnap
stop_cluster_gen_database
do_recover_nosnap
do_check_recover_nosnap
}
function test_case_2()
{
do_export_snap
stop_cluster_gen_database
do_recover_snap
do_check_recover_snap
}
function test_case_3()
{
do_export_nosnap
do_export_snap
stop_cluster_gen_database
do_recover_nosnap
do_recover_snap
do_check_recover_nosnap
do_check_recover_snap
}
init $*
test_case_3
|