summaryrefslogtreecommitdiffstats
path: root/src/tools/rbd_recover_tool/database_h
blob: 4ff20425ace528a71be408c2fc6231e495a70cdb (plain)
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
#!/usr/bin/env bash
# file: database_h
#
# 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.
#

my_dir=$(dirname "$0")

. $my_dir/common_h
. $my_dir/metadata_h
. $my_dir/epoch_h

db_image_prefix=
db_image_size=
db_order=
db_snap_id=
db_snap_image_size=
found=0

#init osd_data and get all objects path
function gen_database()
{
  local func="gen_database"
  rm -rf $database/*
  rm -rf $images
  rm -rf $raw
  mkdir -p $database
  local host=
  local data_path=

  trap 'echo $func failed; exit;' INT HUP
  while read line
  do
    {
      host=`echo $line|awk '{print $1}'`
      data_path=`echo $line|awk '{print $2}'`
      if [ "$host"x = ""x ] || [ "$data_path"x = ""x ];then
 	continue
      fi
      local cmds="find $data_path/current -type f"
      ssh $ssh_option $host $cmds > $database/$host
    } &
  done < $osd_host_path
  wait
  echo "$func: finish"
}

# collect hobjects from database 
# and choose the object whose epoch is latest
# then, sort the objects by their offsets in image 
function gather_hobject_common()
{
  func="gather_hobject_common"

  trap 'echo $func failed; exit;' INT HUP
  if [ $# -lt 2 ];then
    echo "$func: parameters: <pool_id> <image_prefix> [<snap_id>]"  
    exit
  fi

  local pool_id=$1
  local image_prefix=$2
  pool_id=$(($pool_id))
  local hex_pool_id=`printf "%x" $pool_id`
  # NOSNAP = uint64(-2)
  local snap_id=`printf "%u" -2`
  local hex_snap_id="head"
  local psuffix=
  local fsuffix="_head"
  if [ $# = 3 ];then
    snap_id=$(($3))
    hex_snap_id=`printf "%x" $snap_id`
    psuffix="_"$snap_id
    fsuffix="_"$snap_id
  fi
  local underline_image_prefix=`convert_underline $image_prefix`
  local dump_image_prefix=`dump_backslash $underline_image_prefix`
  local ddump_image_prefix=`dump_dump_backslash $underline_image_prefix`
  local images_raw_dir=$rbd_image/raw
  local image_hobjects_dir=$images/pool_$pool_id/$image_prefix
  # $images/raw/$image_prefix"_head"
  local image_hobjects_raw=$images_raw_dir/$image_prefix"$fsuffix"
  # $images/$image_prefix/$image_prefix"_head"
  local image_hobjects_stable=$image_hobjects_dir/$image_prefix"$fsuffix"

  if [ ! -e $images_raw_dir ];then
    mkdir -p $images_raw_dir
  fi
  if [ ! -e $image_hobjects_dir ];then
  local image_metadata=$images_meta/$image_name_in
    mkdir -p $image_hobjects_dir
  fi

  pushd $database >/dev/null
  local  pattern="\.[0-9a-f]+__"$hex_snap_id"_[0-9A-F]{8}__"$hex_pool_id
  >$image_hobjects_raw
  grep -r -E $dump_image_prefix""$pattern * >$image_hobjects_raw
  if [ ! -s $image_hobjects_raw ];then
    echo "$func: image snap [ $image_prefix"$psuffix" ] is empty"
    return 1 #no data available
  fi
  popd >/dev/null

  local offset_dir_temp=$images_raw_dir/$image_prefix"$fsuffix""_dir_temp"
  rm -rf $offset_dir_temp
  mkdir -p $offset_dir_temp

  echo "gather hobjects from database: snapid=$snap_id ..."

  # format: ceph2:/var/lib/ceph/osd/ceph-1/current/2.d3_head/rb.0.1293.6b8b4567.000000000002__head_FB425CD3__2
  local tmp_image=$offset_dir_temp/tmpimage.$$$$
  >$tmp_image
  cat $image_hobjects_raw | 
  awk -F ':' '
  BEGIN {
    pg_coll="'$pg_coll'"
    tmp_image="'$tmp_image'"
    osd_host_mapping="'$osd_host_mapping'"
    snapid="'$snap_id'"
  }{ 
      # $2 = /var/lib/ceph/osd/ceph-1/current/2.d3_head/rb.0.1293.6b8b4567.000000000002__head_FB425CD3__2

      split($2, arr1, "/current/");   # {/var/lib/ceph/osd/ceph-1/, 2.d3_head/rb.0.1293.6b8b4567.000000000002__head_FB425CD3__2}
      split(arr1[2], arr2, "/");     # {2.d3_head, rb.0.1293.6b8b4567.000000000002__head_FB425CD3__2} 
      split(arr2[1], arr3, "_head"); # {2.d3,} 

      hobject=$2;
      data_path=arr1[1];
      gsub(/\\u/, "\\\\\\\\u", hobject); # dump backslash to delay escape (\ -> \\)
      "awk \"\\$1 == \\\""$1"\\\" {print \\$2}\" "osd_host_mapping" | head -n 1" | getline node
      pgid = arr3[1];

      len=length(arr2);
      offset_hobject=arr2[len]             # rb.0.1293.6b8b4567.000000000002__head_FB425CD3__2
      split(offset_hobject, offarr1, "."); # {rb, 0, 1293, 6b8b4567, 000000000002__head_FB425CD3__2}
      len1=length(offarr1) 
      offset_p=offarr1[len1]               # 000000000002__head_FB425CD3__2
      split(offset_p, offarr2, "__");      # {000000000002, head_FB425CD3, 2}
      offset=offarr2[1];                   # 000000000002

      system("echo -n \""node" "pgid" "hobject" "offset" "snapid" \" >>"tmp_image);
      #system("echo -n \""node" "pgid" "hobject" "offset" "snapid" \"");
      #print node" "pgid" "hobject" "offset" "snapid
      
      # find pg_epoch from pg_coll database
      system("awk  \"\\$1 == \\\""node"\\\" && \\$2 == \\\""pgid"\\\" && \\$4 == \\\""data_path"\\\" {print \\$3}\" "pg_coll" >>"tmp_image);
      #system("awk  \"\\$1 == \\\""node"\\\" && \\$2 == \\\""pgid"\\\" && \\$4 == \\\""data_path"\\\" {print \\$3}\" "pg_coll);
  }'

  local sort_image=$offset_dir_temp/sortimage.$$$$
  >$sort_image
  sort -t ' ' -k 4.1,4 -k 6.1nr -k 1.1,1 $tmp_image >$sort_image
  sort -t ' ' -k 4.1,4 -u $sort_image > $image_hobjects_stable
  
  #rm -rf $offset_dir_temp
  return 0
}

function gather_hobject_nosnap()
{
  gather_hobject_common $1 $2
}

function gather_hobject_snap()
{
  gather_hobject_common $1 $2 $3
}

# select the max pg_epoch item of the same $field
# if no same $field, choose the first 
# format : "node $field pg_epoch"
function choose_epoch()
{
  cat $1|sort -t ' ' -k 3.1,3nr -k 2.1,2n |head -n 1;
}

# lookup image info , after scatter_node_jobs & gather_node_infos
function lookup_image()
{
  local func="lookup_image"
  if [ $# -lt 2 ];then
    echo "$func: parameters error <pool_id> <image_name> [<snap_name>]"
  fi
  local pool_id=$1
  local image_name=$2
  local snap_name=$3
  pool_id=$((pool_id))
  echo -e "$func: pool_id = $pool_id\timage_name = $image_name\tsnap_name = $snap_name"
  if [ $pool_id -lt 0 ];then
    echo "$func: pool_id must great than zero"
    exit
  fi
  local hex_pool_id=`printf "%x" $pool_id`
  input_image $image_name
  local node=
  local item=/tmp/item.$$$$
  local img_name=`dump_backslash $image_name`

  local image_format=0
  local image_id_hobject=
  local image_header_hobject=
  local result=/tmp/tmp_result.$$$$
  local res1=/tmp/tmp_res1.$$$$
  local res2=/tmp/tmp_res2.$$$$
  local data_path=

  # image format v1
  {
    cat $image_coll_v1|grep -E "/$img_name\.rbd__head_[0-9A-F]{8}__$hex_pool_id" >$res1
    if [ -s $res1 ];then
      echo -n "$func: rbd_header_hobject = "
      choose_epoch $res1| tee $item
      #choose_epoch $res1 > $item
      
      if [ -e $item ];then
        node=`cat $item|awk '{print $1}'`
        image_header_hobject=`cat $item|awk '{print $2}'`
        if [ "$node"x = ""x ];then
    	  echo "$func: v1 node is NULL"
 	  exit
	fi
	if [ "$image_header_hobject"x = ""x ];then
	  echo "$func: v1 image_header_hobject is NULL"
	  exit
  	fi
        rm -f $item
      fi

      image_format=1
      echo -e "image_name:\t$image_name_in"
      echo -e "image_format:\t$image_format"
      data_path=`echo $image_header_hobject|awk -F "/current" '{print $1}'`
 
      >$result
      cmds="bash $job_path/osd_job do_image_metadata_v1 $data_path `dump_backslash $image_header_hobject` $snap_name" 
      ssh $ssh_option $node $cmds | tee $result
    fi
  }

  # image format v2
  {
    cat $image_coll_v2|grep -E "/rbd\\\\uid\."$img_name"__head_[0-9A-F]{8}__$hex_pool_id" >$res2
    if [ -s $res2 ];then
      echo -n "$func: rbd_id_hobject = "
      choose_epoch $res2 | tee $item
      #choose_epoch $res2 > $item

      if [ -e $item ];then
        node=`cat $item|awk '{print $1}'`
        image_id_hobject=`cat $item|awk '{print $2}'`
        if [ "$node"x = ""x ];then
    	  echo "$func: v2 node is NULL(to get image_id_hobject)"
 	  exit
	fi
	if [ "$image_id_hobject"x = ""x ];then
	  echo "$func: v2 image_id_hobject is NULL"
	  exit
  	fi
        rm -f $item
      fi

      check_osd_process $node
      image_format=2
      
      local tid=/tmp/image_id.$$$$
      data_path=`echo $image_id_hobject|awk -F "/current" '{print $1}'`
      >$tid
      cmds="bash $job_path/osd_job do_image_id $data_path `dump_backslash $image_id_hobject`" 
      ssh $ssh_option $node $cmds > $tid

      local image_id=`cat $tid`
      rm -f $tid

      #get image_header_hobject
      pushd $database >/dev/null
      local pattern="header\."$image_id"__head_[0-9A-F]{8}__$hex_pool_id"
      local tcoll=/tmp/tmp_image_head_coll.$$$$

      # hostname(by command hostname) in $pg_coll  maybe different from hostname in tcoll(input by user) 
      # t_host:        hostname read from config file ($tcoll)
      # t_host_remote: $(hostname) on osd node ($pg_coll)
      grep -r -E $pattern * >$tcoll
      popd >/dev/null

      local t_host=(`cat $tcoll|awk -F ":" '{print $1}'`)
      local t_pgid=(`cat $tcoll|awk -F ":" '{print $2}'|sed -n 's/.*\/\([0-9a-fA-F]\+\.[0-9a-fA-F]\+\)_head\/.*/\1/p'`)
      local t_hobject=(`cat $tcoll|awk -F ":" '{print $2}'`)
      local t_data_path=(`cat $tcoll|awk -F ":" '{split($2, arr, "/current/"); print arr[1];}'`)
      rm -f $tcoll
      declare -a t_host_remote

      #if there is no failed pg migration, number of t_host is replica num
      #replica num : 3, 4, 5 ...
      local t_hostname=/tmp/t_hostname.$$$$
      for ((i=0; i<${#t_host[*]}; i++))
      do
	ssh $ssh_option ${t_host[$i]} "hostname" >$t_hostname
	if [ $? != 0 ];then
	  echo "$func: ${t_host[$i]} get host_remote failed"
	  exit
	fi
        t_host_remote[$i]=`cat $t_hostname`	
      done
      rm -f $t_hostname

      local t_item=/tmp/tmp_item.$$$$
      local tmp_item=/tmp/tmp_tmp_item.$$$$
      
      >$tmp_item
      for ((i=0; i<${#t_host_remote[*]}; i++ ))
      do
	local node=${t_host_remote[$i]}
	local pgid=${t_pgid[$i]}
	awk '$1 == "'"$node"'" && $2 == "'"$pgid"'" {print}' $pg_coll >>$tmp_item
      done

      # t_item: <remote_hostname> <pgid> <epoch> <data_path>
      sort -u $tmp_item >$t_item
      rm -f $tmp_item

      local entry=`choose_epoch $t_item` #t_host_remote
      rm -f $t_item

      node=`echo $entry|awk '{print $1}'`
      data_path=`echo $entry|awk '{print $4}'`
      if [ "$node"x = ""x ];then
        echo "$func: v2 node is NULL (to get image_header_hobject)"
        exit
      fi

      for ((i=0; i<${#t_host_remote[*]}; i++))
      do
	if [ "${t_host_remote[$i]}"x = "$node"x ] && [ "${t_data_path[$i]}"x = "$data_path"x ];then
	  image_header_hobject=${t_hobject[$i]}
	  break
	fi
      done
      
      if [ "$image_id_hobject"x = ""x ];then
        echo "$func: v2 image_header_hobject is NULL"
        exit
      fi

      check_osd_process $node
     
      echo "$func: rbd_header_hobject = $node $image_header_hobject"
      echo -e "image_name:\t$image_name_in"
      echo -e "image_format:\t$image_format"

      #data_path=`echo $image_header_hobject|awk -F "/current" '{print $1}'`
      >$result
      cmds="bash $job_path/osd_job do_image_metadata_v2 $data_path $image_id `dump_backslash $image_header_hobject` $snap_name" 
      ssh $ssh_option $node $cmds | tee $result
    fi
  }

  if [ ! -s $result ];then
    echo "$func: $image_name_in not exists" 
    exit
  fi
  
  # to assign value to global variable
  db_image_prefix=`cat $result|awk '/^(object_prefix|block_name):/{print $2}'`
  if [ "$db_image_prefix"x = ""x ];then
    echo "$func: image_prefix is NULL"
    exit
  fi

  db_image_size=`cat $result|awk '/^image_size:/{print $2}'`
  db_order=`cat $result|awk '/^order:/{print $2}'`
  if [ "$snap_name"x != ""x ];then
    db_snap_id=`cat $result|awk '/^snapshot:/{print $2}'`
    if [ "$db_snap_id"x = ""x ];then
      echo "$func: $image_name_in@$snap_name NOT EXISTS"
      exit
    fi
    db_snap_image_size=`cat $result|awk '/^snapshot:/{print $4}'`
  else
    #save snaplist
    local image_snaplist=$images/pool_$pool_id/$image_name_in/@snaplist
    local image_dir=$images/pool_$pool_id/$image_name_in
    if [ ! -e $image_dir ];then
      mkdir -p $image_dir
    fi
    cat $result|awk '/^snapshot:/{print $2" "$3" "$4}' >$image_snaplist
  fi
  found=1
  rm -f $result
}

function list_images()
{
   echo "=============== format =============="
   echo "format: <pool_id>/<image_name>"
   echo "================ v1: ================"
   #sed -n 's/\(.*\)\/\(.*\)\.rbd__\(.*\)/\2/p' $image_coll_v1|sort -u|sed -e 's/\\u/_/g'
   sed -n 's/.*\/\(.*\)\.rbd__head_[0-9A-F]\{8\}__\([0-9a-f]\+\).*/\2 \1/p' $image_coll_v1|sort -u|awk '{print strtonum("0x"$1)"/"$2;}'|sed -e 's/\\u/_/g'
   echo "================ v2: ================"
   #sed -n 's/\(.*\)\/rbd\\uid.\(.*\)__\(head.*\)/\2/p' $image_coll_v2|sort -u|sed 's/\\u/_/g'
   sed -n 's/.*\/rbd\\uid.\(.*\)__head_[0-9A-F]\{8\}__\([0-9a-f]\+\).*/\2 \1/p' $image_coll_v2|sort -u|awk '{print strtonum("0x"$1)"/"$2}'|sed 's/\\u/_/g'
}

# lookup image metadata
# and 
# collect hobjects of image with the latest pg epoch
function discover_image_nosnap()
{
  local func="discover_image_nosnap"
  echo "$func ..."
  local pool_id=$1
  local image_name=$2
  pool_id=$(($pool_id))
  lookup_image $pool_id $image_name # assign $image_prefix
  gather_hobject_nosnap $pool_id $db_image_prefix 
  if [ $? -ne 0 ];then
    exit
  fi
  local image_hobjects_stable_nosnap=$images/pool_$pool_id/$db_image_prefix/$db_image_prefix"_head"
  local image_hobjects_dir=$images/pool_$pool_id/$image_name_in
  if [ ! -e $image_hobjects_dir ];then
    mkdir -p $image_hobjects_dir
  fi
  # mv image_prefix to image_name
  mv $image_hobjects_stable_nosnap $image_hobjects_dir/$image_name_in
  rm -rf $images/pool_$pool_id/$db_image_prefix
}

# get the offset snapid object 
# if there is no object, choose the smallest snapid which is greater than current snapid
function get_object_clone()
{
  local func="get_object_clone"
  if [ $# -lt 4 ];then
    exit
  fi

  local object_offset_string=$1
  local snapid=$2
  local snaplist_path=$3
  local snapset_output_dir=$4

  # snapid in desc
  local snap_coll_arr=(`
  cat $snaplist_path|awk '{ if ($1 >= '"$snapid"') print "'"$snapset_output_dir"'/@"$1}'`) 

  local hex_snapid=`printf "%x" $snapid`
  pushd $snapset_output_dir >/dev/null
  # get object with the smallest snapid greater than current snapid
  awk '$4 == "'"$object_offset_string"'" && $5 >= '$snapid' {print}' `echo ${snap_coll_arr[@]}` |tail -n 1
  popd >/dev/null
}

# gather hobject for each snapid
function gen_snapset_hobject()
{
  local func="gen_image_snapset"
  echo "$func ..."
  if [ $# -lt 4 ];then
    echo "$func: parameters: <pool_id> <image_prefix> <snaplist_path> <snapset_output_dir>"
    exit
  fi
  local pool_id=$1
  local image_prefix=$2
  local snaplist_path=$3
  local snapset_output_dir=$4
  pool_id=$(($pool_id))
  OIFS=$IFS
  IFS=$'\n'
  local snaparr=(`cat $snaplist_path`) 
  # gather hobject for each snapshot
  trap 'echo $func failed; exit;' INT HUP
  for line in ${snaparr[@]}
  do
    OOIFS=$IFS
    IFS=$' '
    local field=(`echo $line`)
    local snapid=${field[0]}
    local image_hobjects_stable_snap=$images/pool_$pool_id/$image_prefix/$image_prefix"_"$snapid
    local image_snap=$snapset_output_dir/@$snapid
    gather_hobject_snap $pool_id $image_prefix $snapid 
    local res=$?
    if [ $res -ne 0 ];then
      touch $image_snap
    else 
      mv $image_hobjects_stable_snap $image_snap
    fi
    IFS=$OOIFS
  done
  IFS=$OIFS
}

# lookup image metadata and get snapid hobjects
function discover_image_snap()
{
  local func="discover_image_snap"
  echo "$func ..."
  if [ $# -lt 3 ];then
    echo "$func: parameters: <pool_id> <image_name> [<snap_name>]"
    exit
  fi
  local pool_id=$1
  local image_name=$2
  local snap_name=$3
  pool_id=$(($pool_id))
  #mkdir -p $images/$image_prefix
  lookup_image $pool_id $image_name $snap_name # input image_name and snap_name to lookup metadata and snap_id
  if [ "$db_snap_id"x = ""x ];then
    echo "$func: lookup image failed to gen snapid"
    exit
  fi
  local image_hobjects_dir_prefix=$images/pool_$pool_id/$db_image_prefix
  local image_nosnap=$images/pool_$pool_id/$image_name_in 
  #check if image nosnap recovered
  if [ ! -s $image_nosnap ];then
    echo "$func: please recover image nosnap before recover with snap"
    rm -rf $image_hobjects_dir_prefix
    exit
  fi
  local image_hobject_dir=$images/pool_$pool_id/$image_name_in
  local image_snap_hobject=$image_hobject_dir/$image_name_in@$db_snap_id
  local image_snap_hobject_head=$image_hobject_dir/$image_name_in@$db_snap_id@head
  local image_snaplist=$image_hobject_dir/@snaplist
  local image_snapset_dir=$image_hobject_dir/@snapset_dir
  local image_head=$image_hobject_dir/$image_name_in
  if [ ! -e $image_hobject_dir ];then
    mkdir -p $image_hobject_dir
  fi
  # only gen snapset one time
  if [ ! -e $image_snapset_dir ];then
    mkdir -p $image_snapset_dir
    gen_snapset_hobject $pool_id $db_image_prefix $image_snaplist $image_snapset_dir  
   
  fi

  echo "$func: will get object clone ..."
  >$image_snap_hobject
  >$image_snap_hobject_head

  trap 'echo $func failed; exit;' INT HUP
  # get each offset 's snapid hobject
  while read line
  do
    #echo $line
    OOIFS=$IFS
    IFS=$' '
    local field=(`echo $line`)
    local offset_string=${field[3]}
    IFS=$OOIFS
    local entry=`get_object_clone $offset_string $db_snap_id $image_snaplist $image_snapset_dir` 
    if [ "$entry"x != ""x ];then
      echo $entry >> $image_snap_hobject
      echo `dump_backslash $line` >> $image_snap_hobject_head
    fi
  done < $image_head
  rm -rf $image_hobjects_dir_prefix
}

# after discover_image_nosnap
# collect objects from osds one by one in sequence
function copy_image_nosnap_single_thread()
{
  local func="copy_image_nosnap_single_thread"
  echo "$func ..."
  if [ $# -lt 3 ];then
    echo "$func: parameters: <pool_id> <image_hobjects> <backup_dir>"
    exit
  fi
  local pool_id=$1
  local image_hobjects=$2
  local backup_dir=$3
  pool_id=$(($pool_id))

  # make sure lookup_image first
  if [ $found = 0 ];then
    echo "$func: image not found, maybe forget to discover_image"
    exit
  fi
  if [ ! -e $backup_dir ];then
    mkdir -p $backup_dir
  fi

  local image_dir=$backup_dir/pool_$pool_id/$image_name_in
  local image_file=$image_dir/$image_name_in
  local CURRENT=$image_dir/@CURRENT
  local LOCK=$image_dir/@LOCK
  if [ ! -e $image_dir ];then
    mkdir -p $image_dir
  fi
  if [ -e $LOCK ];then
    echo "$func: $LOCK is locked by other process"
    exit
  else
    touch $LOCK
  fi

  >$image_file
  truncate -s $db_image_size $image_file 
  echo "head">$CURRENT

  local count=$(($db_image_size >> $db_order))
  local start=`cat $image_hobjects|head -n 1|awk '{print $4}'`
  local end=`cat $image_hobjects|tail -n 1|awk '{print $4}'`
  local entry_count=`cat $image_hobjects|wc -l`

  local char_bits=$((`echo $start|wc -c` -1 ))
  local format="%0"$char_bits"x"
  
  local expect_start=`printf $format 0`
  local expect_end=`printf $format $(($count -1 ))`  

  echo -e "object_count\t$entry_count"
  echo -e "expect\t\t[$expect_start ~ $expect_end] count:$count"
  echo -e "range\t\t[$start ~ $end] count:$entry_count"

  local icount=0
  local istart=
  local iend=
  local percent=
  
  trap 'echo $func failed; exit;' INT HUP
  local unit=$((1<<$db_order))
  while read line
  do
  {
    icount=$(($icount+1))
    node=`echo $line|awk '{print $1}'` 
    hobject=`echo $line|awk '{print $3}'` 
    offset=`echo $line|awk '{print $4}'`
    off=$((16#$offset))
    if [ $icount = 1 ];then
      istart=$offset
    fi
    hobject=`dump_backslash $hobject`
    iend=$offset
    sshcmd="cat $hobject"
    ssh $ssh_option $node $sshcmd < /dev/null | dd of=$image_file bs=$unit seek=$off conv=notrunc 2>/dev/null
    percent=`echo "scale=3; 100*$icount/$entry_count"|bc`
    tput sc  #record current cursor
    echo -n -e "complete\t[$istart ~ $iend] $icount/$entry_count ==> "$percent"%"
    if [ $icount != $entry_count ];then
      tput rc # backport most recent cursor
    fi
  }
  done < $image_hobjects

  echo
  echo -n "size: "
  ls -lh $image_file|awk '{print  $5"\t"$9}'
  echo -n "du:   "
  du -h $image_file
  #unlock
  rm -f $LOCK
}


# ssh copy snap_object & head_object from osd to admin node
# copy all snapshot objects 
# and 
# all head objects which have the same offset as snapshot objects 
function collect_image_snap_objects()
{
  local func="collect_image_snap_objects"
  #$1=backup_dir, $2=snap_name, $3=snap_hobjects, $4=head_hobjects
  if [ $# -lt 6 ];then
    echo "$func: parameters: <pool_id> <image_name> <snap_id> <snap_hobjects> <head_hobjects> <backup_dir>"
    exit
  fi  

  local pool_id=$1
  local image_name=$2
  local snap_id=$3
  local snap_hobjects=$4 #snap hobjects info
  local head_hobjects=$5 #head hobjects info
  local backup_dir=$6
  pool_id=$(($pool_id))

  local head_dir=$backup_dir/pool_$pool_id/$image_name/@head
  local snap_dir=$backup_dir/pool_$pool_id/$image_name/@$snap_id
  local CURRENT=$backup_dir/pool_$pool_id/$image_name/@CURRENT
 
  if [ ! -e $head_dir ];then
    mkdir -p $head_dir
  fi
  if [ ! -e $snap_dir ];then
    mkdir -p $snap_dir
  fi

  local snap_node= #osd node
  local snap_hobject= #hobject path with snapid on osd
  local snap_offset=
  local snap_filename=

  local head_node=
  local head_hobject=
  local head_offset=
  local head_filename=

  # ignore if there is no object in snapshot(empty )
  if [ ! -s $snap_hobjects ];then
    echo "$func: $snap_hobjects is empty"
    return 0
  fi
  local start=`head -n 1 $snap_hobjects|awk '{print $4}'`
  local end=`tail -n 1 $snap_hobjects|awk '{print $4}'`
  local entry_count=`cat $snap_hobjects|wc -l`
  if [ $((16#$first_offset)) -gt $((16#$last_offset)) ];then
    echo "$func: $snap_hobjects not sorted"
    return 1
  fi

  # just assert if ignored empty snapshot
  if [ "$start"x = ""x ] || [ "$end"x = ""x ];then
    return 1
  fi
 
  # speed up copy snapshot
  # lookup the corresponding head hobject of snap hobject
  # use command: grep <offset> <head hobjects>
  # 
  # eg.
  # head hobjects: (32 objects, snapid = uint64(-2) = 18446744073709551614)
  # ceph1 29.4d /var/lib/ceph/osd/ceph-0/current/29.4d_head/rb.0.1c414.6b8b4567.000000000000__head_EC2C1C4D__1d 000000000000 18446744073709551614 869
  # ceph1 29.8c /var/lib/ceph/osd/ceph-0/current/29.8c_head/rb.0.1c414.6b8b4567.000000000001__head_0F439A8C__1d 000000000001 18446744073709551614 867
  # ceph1 29.6a /var/lib/ceph/osd/ceph-0/current/29.6a_head/rb.0.1c414.6b8b4567.000000000002__head_FC55706A__1d 000000000002 18446744073709551614 869
  # ceph1 29.8b /var/lib/ceph/osd/ceph-0/current/29.8b_head/rb.0.1c414.6b8b4567.000000000003__head_20A6328B__1d 000000000003 18446744073709551614 869
  # ceph2 29.75 /var/lib/ceph/osd/ceph-1/current/29.75_head/rb.0.1c414.6b8b4567.000000000004__head_AC5ADB75__1d 000000000004 18446744073709551614 867
  # ceph2 29.23 /var/lib/ceph/osd/ceph-1/current/29.23_head/rb.0.1c414.6b8b4567.000000000005__head_1FDEA823__1d 000000000005 18446744073709551614 867
  # ......
  # ceph1 29.34 /var/lib/ceph/osd/ceph-0/current/29.34_head/rb.0.1c414.6b8b4567.00000000001f__head_52373734__1d 00000000001f 18446744073709551614 869
  #
  # snap hobjects: (3 objects, snapid >= 29)
  # ceph1 29.8c /var/lib/ceph/osd/ceph-0/current/29.8c_head/rb.0.1c414.6b8b4567.000000000001__1f_0F439A8C__1d 000000000001 31 867
  # ceph1 29.6a /var/lib/ceph/osd/ceph-0/current/29.6a_head/rb.0.1c414.6b8b4567.000000000002__1e_FC55706A__1d 000000000002 30 869
  # ceph1 29.8b /var/lib/ceph/osd/ceph-0/current/29.8b_head/rb.0.1c414.6b8b4567.000000000003__1d_20A6328B__1d 000000000003 29 869
  #
  # so find out offset in head hobjects line number:
  # snap hobjects: 000000000001 ---> head hobjects: 2 (n1)
  # snap hobjects: 000000000003 ---> head hobjects: 4 (n2)
  # 
  # finally , grep range from the whole file [1 ~ N] shranked to part of file [n1 ~ n2]
  # the worst case : [n1 ~ n2] = [1 ~ N], means no shranking

  # get the line number of the start offset in head hobjects
  local n1=`grep -n $start $head_hobjects|head -n 1|cut -d ":" -f 1`
  # get the line number of the end offset in head hobjects
  local n2=`grep -n $end $head_hobjects|head -n 1|cut -d ":" -f 1`
 
  local icount=0
  local istart=
  local iend=
  local percent=

  OIFS=$IFS
  IFS=$'\n'

  #assume file:snap_hobjects is not very large, and can be loaded into memory
  local snap_arr=(`cat $snap_hobjects`)
  local snap_tmp=/tmp/snaptmp.$$$$

  # snap_tmp: 
  # consists of snap hobject or head hobject
  # select lineno range: [n1 ~ n2]
  head -n $n2 $head_hobjects|tail -n $(($n2-$n1+1)) >$snap_tmp 

  echo "copy image snap/head objects from osd ..."
  echo -e "object_count\t$entry_count"
  echo -e "range\t\t[$start ~ $end] count:$entry_count"

  trap 'echo $func failed; exit;' INT HUP
  for line in ${snap_arr[*]}
  do
    icount=$(($icount+1))    

    OOIFS=$IFS
    IFS=$' '

    local arr=(`echo $line`)
    snap_node=${arr[0]}
    snap_hobject=${arr[2]}
    snap_offset=${arr[3]}
    snap_filename=$snap_dir/$snap_offset

    if [ $icount = 1 ];then
      istart=$snap_offset
    fi
    iend=$snap_offset

    #lookup corresponding head hobject of snap hobject
    local res=`grep $snap_offset $snap_tmp|head -n 1` 
    if [ "$res"x = ""x ];then
      echo "$func: image object[ $snap_offset ] missing"
      exit
    fi
    
    local arr2=(`echo $res`)
    head_node=${arr2[0]}
    head_hobject=${arr2[2]}
    head_offset=${arr2[3]}
    head_filename=$head_dir/$head_offset

    # just copy object(snap/head) if it does not exist
    if [ ! -e $snap_filename ];then
      ssh $ssh_option $snap_node "cat $snap_hobject" > $snap_filename 
    fi
    if [ ! -e $head_filename ];then
      ssh $ssh_option $head_node "cat $head_hobject" > $head_filename 
    fi
    IFS=$OOIFS

    percent=`echo "scale=3; 100*$icount/$entry_count"|bc`
    tput sc  #record current cursor
    echo -n -e "complete\t[$istart ~ $iend] $icount/$entry_count ==> "$percent"%"
    if [ $icount != $entry_count ];then
      tput rc # backport most recent cursor
    fi
  done
  echo
  IFS=$OIFS 
  rm -f $snap_tmp
  return 0
}

# copy all snap objects and corresponding head objects from osds
# in single process
function copy_image_snap_single_thread()
{
  local func="copy_image_snap_single_thread"
  if [ $# -lt 6 ];then
    echo "$func: parameters: <pool_id> <image_name> <snap_id> <snap_hobjects> <head_hobjects> <backup_dir>" 
    exit
  fi
  local pool_id=$1
  local image_name=$2
  local snap_id=$3
  local snap_hobjects=$4
  local head_hobjects=$5
  local backup_dir=$6
  pool_id=$(($pool_id))

  local CURRENT=$backup_dir/pool_$pool_id/$image_name/@CURRENT
  local LOCK=$backup_dir/pool_$pool_id/$image_name/@LOCK
  #lock
  if [ -e $LOCK ];then
    echo "$func: $LOCK is locked by other process"
    exit
  else
    touch $LOCK
  fi
  collect_image_snap_objects $pool_id $image_name $snap_id $snap_hobjects $head_hobjects $backup_dir
  #unlock
  rm -f $LOCK
}

# after all snap objects and necessary head objects are copied,
# just pick appropriate head objects and snap objects and write them to image
# in order to rollback image to snapshot
#
# init: image is created by copy_image_nosnap_single_thread firstly
#
# all output include 3 parts:
# <image>  <head objects>      <snap objects>
# 
#          head objects1  ---  snap1 objects
#          head objects2  ---  snap2 objects
#  image   head objects3  ---  snap3 objects
#          ......
#          head objectsN  ---  snapN objects
#
# how to rollback:
# firstly rollback to head, secondly write <snapX objects>
# head  = <image> + <head objects>
# snap1 = <image> + <head objects> + <snap1 objects>
# snap2 = <image> + <head objects> + <snap2 objects>
# snap3 = <image> + <head objects> + <snap3 objects>
# ......
# snapN = <image> + <head objects> + <snapN objects>
# 
# improve rollback:
# there is intersection of head objects and snapX objects, if snapX objects are not empty
# and need to deduplicate the intersection.
# deduplicate steps:
# - get difference set of head objects and snapX objects
# - write the difference set objects to image
# - write the snapX objects to image
function rollback_image_snap()
{
  local func="rollback_image_snap"
  
  echo "$func ..."
  
  trap 'echo $func failed; exit;' INT HUP
  if [ $# -lt 6 ];then
    echo "$func: parameters <pool_id> <image_name> <snap_id> <snap_object_dir> <backup_dir> <image_unit>"
    exit
  fi
  local pool_id=$1
  local image_name=$2
  local snap_id=$3
  local snap_object_dir=$4
  local backup_dir=$5
  local image_unit=$6

  local need_diff_set=0

  local image_path=$backup_dir/pool_$pool_id/$image_name/$image_name
  local head_object_dir=$backup_dir/pool_$pool_id/$image_name/@head
  local CURRENT=$backup_dir/pool_$pool_id/$image_name/@CURRENT
  local LOCK=$backup_dir/pool_$pool_id/$image_name/@LOCK
  if [ -e $LOCK ];then
    echo "$func: $LOCK is locked by other process"
    exit
  else
    touch $LOCK
  fi
  if [ $snap_id -ne -2 ];then
    echo $snap_id > $CURRENT
  else
    echo "head" > $CURRENT
  fi 

  if [ ! -e $snap_object_dir ];then
    return 0
  fi

  if [ "$snap_object_dir"x != "$head_object_dir"x ];then
    echo "$func: need to compute diff_set of head"
    need_diff_set=1
  else
    echo "$func: NO diff_set"
    need_diff_set=0
  fi

  local entry_count=0
  local start=
  local end=
  local offset=
  local icount=0
  local istart=
  local iend=
  local percent=

  local snap_objects=
  local head_objects=
  local diff_set=

  snap_objects=(`ls $snap_object_dir`)

  # if need to compute difference set of head_objects and snap_objects
  if [ $need_diff_set -ne 0 ];then
    head_objects=(`ls $head_object_dir`) 

    #get the difference set: ( head_objects - snap_objects )
    diff_set=(`
    sort -m <(echo ${head_objects[@]}|xargs -n 1 echo) <(echo ${snap_objects[@]}|xargs -n 1 echo) \
  	<(echo ${snap_objects[@]}|xargs -n 1 echo) |uniq -u`) 

    # copy diff_set of head object to image
    pushd $head_object_dir >/dev/null

    echo "$func: copy diff_set head objects ..."
    entry_count=${#diff_set[@]}  
    start=${diff_set[0]}
    end=
    if [ $entry_count -gt 0 ];then
      end=${diff_set[$(($entry_count - 1))]}
    fi
    offset=
    icount=0
    istart=
    iend=
    percent=

    echo -e "object_count\t$entry_count"
    echo -e "range\t\t[$start ~ $end] count:$entry_count"

    for object in ${diff_set[@]}
    do
      icount=$(($icount+1))
      if [ $icount = 1 ];then
        istart=$object
      fi
      iend=$object

      local offset=$((16#$object))
      dd if=$object of=$image_path bs=$image_unit seek=$offset conv=notrunc 2>/dev/null

      percent=`echo "scale=3; 100*$icount/$entry_count"|bc`
      tput sc  #record current cursor
      echo -n -e "complete\t[$istart ~ $iend] $icount/$entry_count ==> "$percent"%"
      if [ $icount != $entry_count ];then
        tput rc # backport most recent cursor
      fi
    done
    if [ $entry_count -gt 0 ];then
      echo
    fi
    popd >/dev/null

    if [ $snap_id -ne -2 ];then
      echo -e "$image_name already rollback diff_set: (head - snap)"
    fi
  fi
  
  # copy snap object to image
  pushd $snap_object_dir >/dev/null 

  if [ $need_diff_set -ne 0 ];then
    echo "$func: copy snap objects ..."
  else
    echo "$func: copy head objects ..."
  fi
  entry_count=${#snap_objects[@]}  
  start=${snap_objects[0]}
  end=
  if [ $entry_count -gt 0 ];then
    end=${snap_objects[$(($entry_count - 1))]}
  fi
  offset=
  icount=0
  istart=
  iend=
  percent=

  echo -e "object_count\t$entry_count"
  echo -e "range\t\t[$start ~ $end] count:$entry_count"

  for object in ${snap_objects[@]}
  do
    icount=$(($icount+1))
    if [ $icount = 1 ];then
      istart=$object
    fi
    iend=$object

    local offset=$((16#$object))
    dd if=$object of=$image_path bs=$image_unit seek=$offset conv=notrunc 2>/dev/null

    percent=`echo "scale=3; 100*$icount/$entry_count"|bc`
    tput sc  #record current cursor
    echo -n -e "complete\t[$istart ~ $iend] $icount/$entry_count ==> "$percent"%"
    if [ $icount != $entry_count ];then
      tput rc # backport most recent cursor
    fi
  done
  if [ $entry_count -gt 0 ];then
    echo
  fi
  popd >/dev/null

  rm -f $LOCK
  if [ $snap_id -ne -2 ];then
    echo "$image_name rollback to snapid: $snap_id"
  else
    echo "$image_name rollback to head"
  fi
}

function recover_image()
{
  local func="recover_image"
  echo "$func ..."
  
  if [ $# -lt 3 ];then
    echo "$func: parameters: <pool_id> <image_name> <snap_name> [<backup_dir>]"
    exit
  fi

  local pool_id=$1
  local img_name=$2
  local snap_name=$3
  local backup_dir=$4
  pool_id=$(($pool_id))
  if [ "$snap_name"x = "@"x ];then
    snap_name=
  fi
  if [ "$backup_dir"x = ""x ];then
    backup_dir=$default_backup_dir
  fi

  #recover image with nosnap
  if [ "$snap_name"x = ""x ];then
    discover_image_nosnap $pool_id $img_name #input image_name 
    local image_hobjects=$images/pool_$pool_id/$image_name_in/$image_name_in
    copy_image_nosnap_single_thread $pool_id $image_hobjects $backup_dir

  #recover image with snap
  else

    # check if recovered head already
    local img_hobjects_path=$images/pool_$pool_id/$img_name/$img_name
    local img_file_path=$backup_dir/pool_$pool_id/$img_name/$img_name
    if [ ! -e $img_hobjects_path ] || [ ! -e $img_file_path ];then
      echo "$func: $img_name@$snap_name : can not rollback to snapshot, please recover image head first"
      exit
    fi

    # rollback to head
    if [ "$snap_name"x = "@@"x ];then
      local head_dir=$backup_dir/pool_$pool_id/$img_name/@head
      if [ -e $head_dir ];then
	local unit=`pushd $head_dir >/dev/null; ls|head -n 1|xargs -n 1 stat|awk '/Size:/{print $2}'`
        # rollback to head
        rollback_image_snap $pool_id $img_name -2 $backup_dir/$img_name/@head $backup_dir $unit
        echo "$image_name_in head : $backup_dir/$img_name/$img_name"
      else
	echo "$func: no need to rollback to head"
      fi
      return 0
    fi
    
    # rollback to snap
    discover_image_snap $pool_id $img_name $snap_name # get image meta & get snapid object
    local snap_hobjects=$images/pool_$pool_id/$image_name_in/$image_name_in@$db_snap_id
    local head_hobjects=$images/pool_$pool_id/$image_name_in/$image_name_in@$db_snap_id@head
    local snap_object_dir=$backup_dir/pool_$pool_id/$image_name_in/@$db_snap_id
    local image_path=$backup_dir/pool_$pool_id/$image_name_in/$image_name_in
    local image_unit=$((1<<$db_order))
    copy_image_snap_single_thread $pool_id $image_name_in $db_snap_id $snap_hobjects $head_hobjects $backup_dir
    rollback_image_snap $pool_id $image_name_in $db_snap_id $snap_object_dir $backup_dir $image_unit 
    echo "$image_name_in@$snap_name : $image_path"
  fi
}