summaryrefslogtreecommitdiffstats
path: root/src/ocf/rbd.in
blob: cbcc1d5b3a9c53ee10805c77ae1cc01303845e91 (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
#!/bin/sh
#
#   OCF resource agent for mapping and unmapping
#   RADOS Block Devices (RBDs)
#
#   License:      GNU Lesser General Public License (LGPL) 2.1 or 3.0
#   (c) 2012 Florian Haas, hastexo
#

# Initialization:
: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs

# Convenience variables
# When sysconfdir isn't passed in as a configure flag,
# it's defined in terms of prefix
prefix=@prefix@

# Defaults
OCF_RESKEY_pool_default="rbd"
OCF_RESKEY_cephconf_default="@sysconfdir@/@PACKAGE_TARNAME@/@PACKAGE_TARNAME@.conf"
: ${OCF_RESKEY_pool=${OCF_RESKEY_pool_default}}
: ${OCF_RESKEY_cephconf=${OCF_RESKEY_cephconf_default}}

rbd_meta_data() {
    cat <<EOF
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="rbd" version="0.1">
  <version>0.1</version>
  <longdesc lang="en">
Manages RADOS Block Devices (RBDs) as a highly available
resource. Maps and unmaps RBDs as needed.
  </longdesc>
  <shortdesc lang="en">Maps and unmaps RADOS Block Devices</shortdesc>
  <parameters>
    <parameter name="name" unique="0" required="1">
      <longdesc lang="en">
      Name of the RBD device.
      </longdesc>
      <shortdesc lang="en">RBD device name</shortdesc>
      <content type="string"/>
    </parameter>
    <parameter name="pool_namespace" unique="0" required="0">
      <longdesc lang="en">
      Name of the RADOS pool namespace where the RBD has been created
      </longdesc>
      <shortdesc lang="en">RADOS pool namespace name</shortdesc>
    </parameter>
    <parameter name="pool" unique="0" required="0">
      <longdesc lang="en">
      Name of the RADOS pool where the RBD has been created
      </longdesc>
      <shortdesc lang="en">RADOS pool name</shortdesc>
      <content type="string" default="${OCF_RESKEY_pool_default}"/>
    </parameter>
    <parameter name="snap" unique="0" required="0">
      <longdesc lang="en">
      Name of the device snapshot to map.
      </longdesc>
      <shortdesc lang="en">Snapshot name</shortdesc>
      <content type="string"/>
    </parameter>
    <parameter name="cephconf" unique="0" required="0">
      <longdesc lang="en">
      Location of the Ceph configuration file
      </longdesc>
      <shortdesc lang="en">Ceph configuration file</shortdesc>
      <content type="string" default="${OCF_RESKEY_cephconf_default}"/>
    </parameter>
    <parameter name="mon" unique="0" required="0">
      <longdesc lang="en">
      Address (or comma-separated list of addresses) of
      monitor servers to connect to. Overrides values from
      configuration file.
      </longdesc>
      <shortdesc lang="en">Monitor address(es)</shortdesc>
      <content type="string"/>
    </parameter>
    <parameter name="user" unique="0" required="0">
      <longdesc lang="en">
      Username to use when mapping the device. Required
      if Ceph authentication is enabled on the monitor.
      </longdesc>
      <shortdesc lang="en">Authentication username</shortdesc>
      <content type="string"/>
    </parameter>
    <parameter name="secret" unique="0" required="0">
      <longdesc lang="en">
      File containing an authentication secret. Required
      if Ceph authentication is enabled on the monitor.
      </longdesc>
      <shortdesc lang="en">Authentication secret file</shortdesc>
      <content type="string"/>
    </parameter>
  </parameters>
  <actions>
    <action name="start"        timeout="20" />
    <action name="stop"         timeout="20" />
    <action name="monitor"      timeout="20"
                                interval="10" depth="0" />
    <action name="meta-data"    timeout="5" />
    <action name="validate-all"   timeout="20" />
  </actions>
</resource-agent>
EOF
}

rbd_usage() {
        cat <<EOF
usage: $0 {start|stop|status|monitor|validate-all|meta-data}

Expects to have a fully populated OCF RA-compliant environment set.
EOF
}

get_rbd_options() {
    local rbd_options

    if [ -n "${OCF_RESKEY_cephconf}" ]; then
        rbd_options="$rbd_options -c ${OCF_RESKEY_cephconf}"
    fi
    if [ -n "${OCF_RESKEY_mon}" ]; then
        rbd_options="$rbd_options -m ${OCF_RESKEY_mon}"
    fi

    echo "${rbd_options}"
}


# rbd command wrapper: builds an option string for invoking RBD based
# on resource parameters, and invokes rbd through ocf_run.
do_rbd() {
    local rbd_options="$(get_rbd_options)"

    ocf_run rbd $rbd_options $@
}

# Convenience function that uses "rbd device list" to retrieve the
# mapped device name from the pool, RBD name, and snapshot.
find_rbd_dev() {
    local rbd_options="$(get_rbd_options)"
    local sedpat

    # Example output from "rbd device list" (tab separated):
    # id        pool    namespace	image   snap    device
    # 0         rbd     		test    -       /dev/rbd0
    # 1         rbd     ns1		test    -       /dev/rbd1

    # Build the sed pattern, substituting "-" for the snapshot name if
    # it's unset
    sedpat="[0-9]\+[ \t]\+${OCF_RESKEY_pool}[ \t]\+${OCF_RESKEY_pool_namespace}[ \t]\+${OCF_RESKEY_name}[ \t]\+${OCF_RESKEY_snap:--}[ \t]\+\(/dev/rbd[0-9]\+\).*"

    # Run "rbd device list", filter out the header line, then try to
    # extract the device name
    rbd $rbd_options device list | tail -n +2 | sed -n -e "s,$sedpat,\1,p"
}

rbd_validate_all() {
    # Test for configuration errors first
    if [ -z "$OCF_RESKEY_name" ]; then
       ocf_log err 'Required parameter "name" is unset!'
       exit $OCF_ERR_CONFIGURED
    fi

    # Test for required binaries
    check_binary rbd

    return $OCF_SUCCESS
}

rbd_monitor() {
    local rc
    local rbd_dev

    if ! [ -d /sys/bus/rbd ]; then
        ocf_log debug "rbd module is not loaded"
        return $OCF_NOT_RUNNING
    fi

    rbd_dev=`find_rbd_dev`

    if [ -z "$rbd_dev" ]; then
        ocf_log debug "RBD device is unmapped"
        rc=$OCF_NOT_RUNNING
    elif [ -b "$rbd_dev" ]; then
        ocf_log debug "RBD device is mapped to $rbd_dev"
        rc=$OCF_SUCCESS
    else
        # Device is listed, but the corresponding path is not a block
        # device.
        ocf_log err "$rbd_dev is not a block device!"
        rc=$OCF_ERR_GENERIC
    fi

    return $rc
}

rbd_start() {
    local rbd_map_options
    local rbd_name

    # if resource is already running, bail out early
    if rbd_monitor; then
        ocf_log info "Resource is already running"
        return $OCF_SUCCESS
    fi

    # actually start up the resource here (make sure to immediately
    # exit with an $OCF_ERR_ error code if anything goes seriously
    # wrong)

    if [ ! -d /sys/bus/rbd ]; then
        ocf_run modprobe -v rbd || exit $OCF_ERR_INSTALLED
    fi

    if [ -n "${OCF_RESKEY_user}" ]; then
        rbd_map_options="--id ${OCF_RESKEY_user}"
    fi
    if [ -n "${OCF_RESKEY_secret}" ]; then
        rbd_map_options="$rbd_map_options --keyfile ${OCF_RESKEY_secret}"
    fi

    rbd_name="${OCF_RESKEY_pool}"
    if [ -n "${OCF_RESKEY_pool_namespace}" ]; then
        rbd_name="${rbd_name}/${OCF_RESKEY_pool_namespace}"
    fi
    rbd_name="${rbd_name}/${OCF_RESKEY_name}"
    if [ -n "${OCF_RESKEY_snap}" ]; then
        rbd_name="${rbd_name}@${OCF_RESKEY_snap}"
    fi

    do_rbd device map $rbd_name $rbd_map_options || exit $OCF_ERR_GENERIC

    # After the resource has been started, check whether it started up
    # correctly. If the resource starts asynchronously, the agent may
    # spin on the monitor function here -- if the resource does not
    # start up within the defined timeout, the cluster manager will
    # consider the start action failed
    while ! rbd_monitor; do
        ocf_log debug "Resource has not started yet, waiting"
        sleep 1
    done

    # only return $OCF_SUCCESS if _everything_ succeeded as expected
    return $OCF_SUCCESS
}

rbd_stop() {
    local rc
    local rbd_dev

    rbd_monitor
    rc=$?
    case "$rc" in
        "$OCF_SUCCESS")
            # Currently running. Normal, expected behavior.
            ocf_log debug "Resource is currently running"
            ;;
        "$OCF_NOT_RUNNING")
            # Currently not running. Nothing to do.
            ocf_log info "Resource is already stopped"
            return $OCF_SUCCESS
            ;;
    esac

    # actually shut down the resource here (make sure to immediately
    # exit with an $OCF_ERR_ error code if anything goes seriously
    # wrong)
    rbd_dev=`find_rbd_dev`
    do_rbd device unmap $rbd_dev || exit $OCF_ERR_GENERIC

    # After the resource has been stopped, check whether it shut down
    # correctly. If the resource stops asynchronously, the agent may
    # spin on the monitor function here -- if the resource does not
    # shut down within the defined timeout, the cluster manager will
    # consider the stop action failed
    while rbd_monitor; do
        ocf_log debug "Resource has not stopped yet, waiting"
        sleep 1
    done

    # only return $OCF_SUCCESS if _everything_ succeeded as expected
    return $OCF_SUCCESS

}

# Make sure meta-data and usage always succeed
case $__OCF_ACTION in
meta-data)      rbd_meta_data
                exit $OCF_SUCCESS
                ;;
usage|help)     rbd_usage
                exit $OCF_SUCCESS
                ;;
esac

# Anything other than meta-data and usage must pass validation
rbd_validate_all || exit $?

# Translate each action into the appropriate function call
case $__OCF_ACTION in
start)          rbd_start;;
stop)           rbd_stop;;
status|monitor) rbd_monitor;;
validate-all)   ;;
*)              rbd_usage
                exit $OCF_ERR_UNIMPLEMENTED
                ;;
esac
rc=$?

# The resource agent may optionally log a debug message
ocf_log debug "${OCF_RESOURCE_INSTANCE} $__OCF_ACTION returned $rc"
exit $rc