Whamcloud - gitweb
LU-6635 lfsck: block replacing the OST-object for test
[fs/lustre-release.git] / lustre / scripts / lhbadm
1 #!/bin/bash
2
3 # lhbadm - handle some common heartbeat/lustre failover ops
4
5 PATH=/sbin:/usr/sbin:/usr/bin:$PATH:/usr/lib64/heartbeat:/usr/lib/heartbeat
6
7 declare -r prog=lhbadm
8
9 die ()
10 {
11     echo "$prog: $@"
12     exit 1
13 }
14
15 warn ()
16 {
17     echo "$prog: $@"
18 }
19
20 usage ()
21 {
22     echo "Usage: $prog status|lstatus|failback|failover"
23     echo "  status -   print one-line heartbeat-lustre status"
24     echo "  failover - fail all my active resources over to partner"
25     echo "  failback - fail my normal resources back"
26     exit 1
27 }
28
29 test_mounts ()
30 {
31     local label
32     local lcount=0
33     local fcount=0
34     local ltot=0
35     local ftot=0
36
37     for label in $(ldev -l); do
38         ltot=$((ltot + 1))
39         if [ "$(service lustre status $label)" == "running" ]; then
40             lcount=$((lcount + 1))
41         fi
42     done
43     for label in $(ldev -f); do
44         ftot=$((ftot+ 1))
45         if [ "$(service lustre status $label)" == "running" ]; then
46             fcount=$((fcount + 1))
47         fi
48     done
49
50     if [ $(($lcount + $fcount)) == 0 ]; then
51         echo none
52     elif [ $lcount == $ltot -a $fcount == 0 ]; then
53         echo local
54     elif [ $lcount == 0     -a $fcount == $ftot ]; then
55         echo foreign
56     elif [ $lcount == $ltot -a $fcount == $ftot ]; then
57         echo all
58     else
59         echo partial
60     fi
61 }
62
63 status ()
64 {
65     local rstat fstat
66     local labels
67
68     rstat=$(cl_status rscstatus) || die "cl_status rscstatus failed"
69     fstat=$(service lustre status)
70
71     if [ "$fstat" == "running" ]; then
72         fstat=$(test_mounts)
73     fi
74
75     echo $rstat-$fstat
76 }
77
78 wait_for_transition ()
79 {
80     while sleep 5; do
81         state=$(cl_status rscstatus) || die "cl_status rscstatus failed"
82         [ "$state" == "transition" ] || break
83     done
84 }
85
86 failover ()
87 {
88     local s
89
90     [ "$(id -un)" == "root" ] || die "failover requires root privileges"
91     [ $# -gt 0 ] || die "please include a descriptive reason for the logs"
92
93     s=$(status)
94     logger -s -t Lustre-ha -p user.err "failover start, status=$s, reason: $*"
95
96     hb_standby all 2>/dev/null 1>&2 || die "hb_standby all failed"
97     wait_for_transition
98
99     s=$(status)
100     logger -s -t Lustre-ha -p user.err "failover complete, status=$s"
101 }
102
103 failback ()
104 {
105     local s
106
107     [ "$(id -un)" == "root" ] || die "failback requires root privileges"
108     [ $# -gt 0 ] || die "please include a descriptive reason for the logs"
109
110     s=$(status)
111     logger -s -t Lustre-ha -p user.err "failback start, status=$s, reason: $*"
112
113     hb_takeover local || die "hb_takeover local failed"
114     wait_for_transition
115
116     s=$(status)
117     logger -s -t Lustre-ha -p user.err "failover complete, status=$s"
118 }
119
120
121 #
122 # MAIN
123 #
124
125 [ $# == 0 ] && usage
126 [ -x /usr/bin/cl_status ] || die "Heartbeat is not installed"
127 hstat=$(cl_status hbstatus) || die "$hstat"
128
129 case "$1" in
130     status)   status ;;
131     lstatus)  lstatus ;;
132     failback) shift; failback $*;;
133     failover) shift; failover $*;;
134     *) usage ;;
135 esac
136
137 #  vi: ts=4 sw=4 expandtab