Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / tools / rbd_recover_tool / osd_job
1 #!/bin/bash
2 # file: osd_job
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
21 . $my_dir/common_h
22 . $my_dir/metadata_h
23 . $my_dir/epoch_h
24
25 function check_ceph_osd()
26 {
27   local func="check_ceph_osd"
28   local host=`hostname`
29   # if ceph-osd service is still running, except flush-journal
30   if [ "`ps aux|grep ceph-osd|grep -v flush-journal|grep -v grep`"x != ""x ];then
31     echo "[$host]: $func: ceph-osd is running..., stop it"
32     exit 
33   fi
34 }
35
36 function cat_pg_epoch()
37 {
38   local func="cat_pg_epoch" 
39   init_env_osd $1
40   if [ -e $node_pg_epoch ];then
41     cat $node_pg_epoch
42   fi
43
44
45 function cat_image_v1()
46 {
47   local func="cat_image_v1" 
48   init_env_osd $1
49   if [ -e $image_v1 ];then
50     cat $image_v1
51   fi
52
53
54 function cat_image_v2()
55 {
56   local func="cat_image_v2" 
57   init_env_osd $1
58   if [ -e $image_v2 ];then
59     cat $image_v2
60   fi
61
62
63 function flush_osd_journal()
64 {
65   local func="flush_osd_journal"
66   init_env_osd $1
67   local osd_data_path=$osd_data
68   local osd_journal_path=$osd_data/journal 
69   local whoami_path=$osd_data/whoami
70   local host=`hostname`
71   if [ ! -e $whoami_path ];then
72     echo "[$host]: $func: $whoami_path not exists"
73     exit
74   fi
75   local whoami=`cat $whoami_path`
76   echo "[$host]: $func ..."
77   ceph-osd -i $whoami --osd-data $osd_data_path --osd-journal $osd_journal_path --flush-journal >/dev/null
78   if [ $? -ne 0 ];then
79     echo "[$host]: $func: flush osd journal failed"
80     exit
81   fi
82 }
83
84 function do_omap_list()
85 {
86   local func="do_omap_list"
87   init_env_osd $1
88   local host=`hostname`
89   echo "[$host]: $func ..."
90   get_omap_list
91 }
92
93 # get all pgs epoch 
94 function do_pg_epoch()
95 {
96   local func="do_pg_epoch"
97   init_env_osd $1
98   local node=`hostname`
99   get_pgid_list
100   >$node_pg_epoch
101   local pgid=
102   local data_path=
103   local host=`hostname`
104   echo "[$host]: $func ..."
105   while read line
106   do
107   {
108     pgid=`echo $line|awk '{print $1}'`
109     data_path=`echo $line|awk '{print $2}'`
110     get_pg_epoch $pgid
111     echo -e "$node $pgid $pg_epoch $data_path" >>$node_pg_epoch
112   } 
113   done < $pgid_list
114 }
115
116 # get an list of image in this osd node, pg epoch maybe not the latest, the admin node will do distinguish
117 function do_image_list()
118 {
119   local func="do_image_list"
120   init_env_osd $1
121   get_image_list   
122   local node=`hostname`
123   >$image_v1
124   >$image_v2
125   local host=`hostname`
126   echo "[$host]: $func ..."
127   for line in `cat $image_list_v1`
128   do
129     pgid=`get_pgid $line`
130     get_pg_epoch $pgid
131     echo "$node $line $pg_epoch" >> $image_v1
132   done
133   for line in `cat $image_list_v2`
134   do
135     pgid=`get_pgid $line`
136     get_pg_epoch $pgid
137     echo "$node $line $pg_epoch" >> $image_v2
138   done
139 }
140
141 function do_image_id()
142 {
143   local func="do_image_id"
144   init_env_osd $1
145   get_image_id $2
146 }
147
148 function do_image_metadata_v1()
149 {
150   local func="do_image_metadata_v1"
151   init_env_osd $1
152   local image_header_hobject=$2
153   local snap_name=$3
154   get_image_metadata_v1 $image_header_hobject $snap_name
155 }
156
157 function do_image_metadata_v2()
158 {
159   local func="do_image_metadata_v2"
160   init_env_osd $1
161   local image_id=$2
162   local image_header_hobject=$3
163   local snap_name=$4
164   get_map_header $image_id 
165   get_meta_header_seq $map_header_prefix $map_header_key
166   get_image_metadata_v2 $meta_header_seq $snap_name
167 }
168
169 check_ceph_osd
170 $*