1#! /bin/bash
2#
3# Copyright (C) 2014 FUJITSU LIMITED
4#
5# This library is free software; you can redistribute it and/or
6# modify it under the terms of version 2.1 of the GNU Lesser General Public
7# License as published by the Free Software Foundation.
8#
9# This library is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12# Lesser General Public License for more details.
13#
14# You should have received a copy of the GNU Lesser General Public
15# License along with this library; If not, see <http://www.gnu.org/licenses/>.
16#
17# Usage:
18#     block-drbd-probe devicename
19#
20# Return value:
21#     0: the device is drbd device
22#     1: the device is not drbd device
23#     2: unknown error
24#     3: the drbd device does not use protocol D
25#     4: the drbd device is not ready
26
27set -e
28
29drbd_res=
30
31function get_res_name()
32{
33    local drbd_dev=$1
34    local drbd_dev_list=($(drbdadm sh-dev all))
35    local drbd_res_list=($(drbdadm sh-resource all))
36    local temp_drbd_dev temp_drbd_res
37    local found=0
38
39    for temp_drbd_dev in ${drbd_dev_list[@]}; do
40        if [[ "$temp_drbd_dev" == "$drbd_dev" ]]; then
41            found=1
42            break
43        fi
44    done
45
46    if [[ $found -eq 0 ]]; then
47        return 1
48    fi
49
50    for temp_drbd_res in ${drbd_res_list[@]}; do
51        temp_drbd_dev=$(drbdadm sh-dev $temp_drbd_res)
52        if [[ "$temp_drbd_dev" == "$drbd_dev" ]]; then
53            drbd_res="$temp_drbd_res"
54            return 0
55        fi
56    done
57
58    # OOPS
59    return 2
60}
61
62get_res_name $1
63rc=$?
64if [[ $rc -ne 0 ]]; then
65    exit $rc
66fi
67
68# check protocol
69drbdsetup $1 show | grep -q "protocol D;"
70if [[ $? -ne 0 ]]; then
71    exit 3
72fi
73
74# check connect status
75state=$(drbdadm cstate "$drbd_res")
76if [[ "$state" != "Connected" ]]; then
77    exit 4
78fi
79
80# check role
81role=$(drbdadm role "$drbd_res")
82if [[ "$role" != "Primary/Secondary" ]]; then
83    exit 4
84fi
85
86exit 0
87