Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / tools / rbd_recover_tool / epoch_h
1 #!/bin/bash
2 # file: epoch_h
3 #
4 # Copyright (C) 2015 Ubuntu Kylin
5 #
6 # Author: Min Chen <minchen@ubuntukylin.com>
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU Library Public License as published by
10 # the Free Software Foundation; either version 2, or (at your option)
11 # 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 Library Public License for more details.
17 #
18
19 my_dir=$(dirname "$0")
20 . $my_dir/common_h
21
22 #pgid_list=$single_node/$cluster-$id/pgid_list
23 function get_pgid_list()
24 {
25   find $osd_data/current/ -type d -name "*_head"|\
26         sed -n 's/\(.*\)\/current\/\([0-9a-fA-F]\+\.[0-9a-fA-F]\+\)_head/\2 \1/p'|\
27         sort -t ' ' -k 1.1,1h -k 2.1,2 > $pgid_list;
28 }
29
30 function get_pgid()
31 {
32   hobject_path=$1
33   echo $hobject_path| sed -n 's/\(.*\)\/\([0-9a-fA-F]\+\.[0-9a-fA-F]\+\)_head\(.*\)/\2/p'
34 }
35
36 infos_seq=
37 function get_infos_seq()
38 {
39   local func="get_infos_seq"
40   
41   local keyword=":infos." 
42   local infos_key=`get_map_header_key $keyword`
43
44   if [ "$infos_key"x = ""x ];then
45     echo "$func: keyword not input or infos_key not exists"
46     exit 
47   fi
48   local prefix=`get_map_header_prefix`
49   local key=$infos_key
50
51   infos_seq=`get_header_seq $prefix $key`
52   if [ "$infos_seq"x = ""x ];then
53     echo "$func: infos_seq not exists"
54     exit
55   fi
56 }
57
58 pg_epoch=
59 function get_pg_epoch()
60 {
61   local func="get_pg_epoch"
62   if [ "$1"x = ""x ];then
63     echo "$func: no pgid input"
64     exit
65   fi
66
67   get_pg_epoch_firefly "$1"
68   if [ "$pg_epoch"x != ""x ]; then
69     # echo "Epoch for $1: $pg_epoch (firefly)"
70     return
71   fi
72
73   get_pg_epoch_hammer "$1"
74   if [ "$pg_epoch"x != ""x ]; then
75     # echo "Epoch for $1: $pg_epoch (hammer)"
76     return
77   fi
78
79   echo "$func: Couldn't find epoch for $1"
80   exit
81 }
82
83 function get_pg_epoch_firefly()
84 {
85   local func="get_pg_epoch_firefly"
86   if [ "$1"x = ""x ];then
87     echo "$func: no pgid input"
88     exit
89   fi
90   local pgid=$1
91   local key=$pgid"_epoch"
92
93   #get_infos_seq;
94   # infos_seq default to 1
95   infos_seq=1
96   local infos_seq=`printf "%016d" $infos_seq`
97   local prefix="_USER_"$infos_seq"_USER_"
98
99   pg_epoch=`get_header_kv $prefix $key int`
100 }
101
102 function get_pg_epoch_hammer()
103 {
104   local func="get_pg_epoch_hammer"
105   if [ "$1"x = ""x ];then
106     echo "$func: no pgid input"
107     exit
108   fi
109   local pgid="$1"
110   local hkey_prefix="$(get_map_header_prefix)"
111   local hkey="$(printf '...head.%x.%08X' "$(echo "$pgid"|cut -d'.' -f1)" "$((0x$(echo "$pgid"|cut -d'.' -f2)))")"
112
113   local infos_seq="$(get_header_seq "$hkey_prefix" "$hkey")"
114   local infos_seq=`printf "%016d" $infos_seq`
115   local prefix="_USER_"$infos_seq"_USER_"
116   local key="_epoch"
117
118   pg_epoch=`get_header_kv $prefix $key int`
119 }