summaryrefslogtreecommitdiff
path: root/scripts/VolumeGroup.ocf
blob: 8a39582fd82a91d8c49a900a18de26dc0045a9eb (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
#!/bin/sh
#
# VolumeGroup
#
# Description:	Manages an LVM2 volume group as an HA resource in
#               an OCF-compliant cluster
#
#
# Authors:	Alan Robertson, Lars Marowsky-Bree, Florian Haas,
#               and others from the Linux-HA project
# License:	GNU General Public License (GPL)
# Copyright:	(C) 2002 - 2005 International Business Machines, Inc.
#               (C) 2010 LINBIT HA-Solutions GmbH
#
#	This code significantly inspired by the LVM resource
#	in FailSafe by Lars Marowsky-Bree
#
#######################################################################
# Initialization:

: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/resource.d/heartbeat}
. ${OCF_FUNCTIONS_DIR}/.ocf-shellfuncs

#######################################################################


usage() {
  methods=`VolumeGroup_methods`
  methods=`echo $methods | tr ' ' '|'`
  cat <<EOF
	usage: $0 $methods

	$0 manages an LVM Volume Group (VG) as an HA resource

	The 'start' operation brings the given volume online
	The 'stop' operation takes the given volume offline
	The 'status' operation reports whether the volume is available
	The 'monitor' operation reports whether the volume seems present
	The 'validate-all' operation checks whether the OCF parameters are valid
	The 'methods' operation reports on the methods $0 supports

EOF
}

meta_data() {
	cat <<EOF
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="VolumeGroup">
<version>1.0</version>

<longdesc lang="en">
Resource script for an LVM Volume Group.
</longdesc>
<shortdesc lang="en">Controls the availability of an LVM Volume Group</shortdesc>

<parameters>
<parameter name="volgrpname" unique="0" required="1">
<longdesc lang="en">
The name of volume group.
</longdesc>
<shortdesc lang="en">Volume group name</shortdesc>
<content type="string" default="" />
</parameter>
<parameter name="exclusive" unique="0" required="0">
<longdesc lang="en">
If set, the volume group will be activated exclusively.
</longdesc>
<shortdesc lang="en">Exclusive activation</shortdesc>
<content type="boolean" default="false" />
</parameter>
</parameters>

<actions>
<action name="start" timeout="30" />
<action name="stop" timeout="30" />
<action name="status" timeout="30" />
<action name="monitor" depth="0" timeout="30" interval="10" />
<action name="methods" timeout="5" />
<action name="meta-data" timeout="5" />
<action name="validate-all" timeout="5" />
</actions>
</resource-agent>
EOF
}

#
# methods: What methods/operations do we support?
#
VolumeGroup_methods() {
  cat <<EOF
	start
	stop
	status
	monitor
	methods
	validate-all
	usage
EOF
}

#
# Report on LVM volume status. VG may be reported as active
# ($OCF_SUCCESS) or inactive ($OCF_NOT_RUNNING)
#
VolumeGroup_status() {

    VGOUT=`vgdisplay -v $OCF_RESKEY_volgrpname 2>&1` || exit $OCF_ERR_GENERIC
    echo "$VGOUT" | grep -i 'Status[ \t]*available' >/dev/null
    rc=$?

    if [ $rc -eq 0 ]; then
	ocf_log debug "LVM Volume Group $OCF_RESKEY_volgrpname is available (started)"
    else
	ocf_log debug "LVM Volume Group $OCF_RESKEY_volgrpname is not available (stopped)"
	return $OCF_NOT_RUNNING
    fi

    if echo "$VGOUT" | grep -i 'Access.*read/write' >/dev/null; then
	ocf_log debug "Volume $OCF_RESKEY_volgrpname is available read/write (running)"
    else
	ocf_log debug "Volume $OCF_RESKEY_volgrpname is available read-only (running)"
    fi

    return $OCF_SUCCESS
}

#
# Monitor the volume - does it really seem to be working? May report
# $OCF_SUCCESS or $OCF_NOT_RUNNING like VolumeGroup_status, plus
# $OCF_ERR_GENERIC in case vgck reports an error.
#
VolumeGroup_monitor() {
    if ! VolumeGroup_status $OCF_RESKEY_volgrpname; then
	ocf_log info "LVM Volume Group $OCF_RESKEY_volgrpname is offline"
	return $OCF_NOT_RUNNING
    fi

    ocf_run vgck $OCF_RESKEY_volgrpname || exit $OCF_ERR_GENERIC

    return $OCF_SUCCESS
}

#
# Activate the volume group, either locally (if $OCF_RESKEY_exclusive
# is false or unset), or exclusively (if $OCF_RESKEY_exclusive is
# true).
# Either returns successfully, or exits with $OCF_ERR_GENERIC.
#
VolumeGroup_start() {

  ocf_log info "Activating volume group $OCF_RESKEY_volgrpname"
  ocf_run vgscan

  local active_mode
  active_mode="ly"
  if ocf_is_true "$OCF_RESKEY_exclusive" ; then
      active_mode="ey"
  fi

  ocf_run vgchange -a $active_mode $OCF_RESKEY_volgrpname || exit $OCF_ERR_GENERIC

  if ! VolumeGroup_status $OCF_RESKEY_volgrpname; then
    ocf_log err "LVM: $OCF_RESKEY_volgrpname did not activate correctly"
    exit $OCF_ERR_GENERIC
  fi

  return $OCF_SUCCESS
}

#
# Deactivate the volume group.
# Either returns successfully, or exits with $OCF_ERR_GENERIC.
#
VolumeGroup_stop() {
    if ! VolumeGroup_status; then
	ocf_log debug "Volume Group $OCF_RESKEY_volgrpname already stopped"
	return $OCF_SUCCESS
    fi

    ocf_log info "Deactivating volume group $OCF_RESKEY_volgrpname"
    ocf_run vgchange -a ln $OCF_RESKEY_volgrpname || exit $OCF_ERR_GENERIC

    if VolumeGroup_status; then
	ocf_log err "LVM: $OCF_RESKEY_volgrpname did not stop correctly"
	exit $OCF_ERR_GENERIC
    fi

    return $OCF_SUCCESS
}

#
# Check whether the OCF instance parameters are valid.
# Either returns successfully, or exits with
# $OCF_ERR_CONFIGURED if required parameters are missing;
# $OCF_ERR_INSTALLED if required binaries are missing;
# $OCF_ERR_GENERIC in case of any other error.
#
VolumeGroup_validate_all() {

    if [ -z $OCF_RESKEY_volgrpname ]; then
	ocf_log err 'Missing required parameter "volgrpname"!'
	exit $OCF_ERR_CONFIGURED
    fi

    check_binary vgchange
    check_binary vgck
    check_binary vgdisplay

    # Run the following tests only if we're not invoked by a probe
    # operation
    if ! ocf_is_probe; then
        # Off-the-shelf tests...
	vgck "$OCF_RESKEY_volgrpname" >/dev/null 2>&1
	if [ $? -ne 0 ]; then
	    ocf_log err "Volume group $OCF_RESKEY_volgrpname does not exist or contains error!"
	    exit $OCF_ERR_GENERIC
	fi

        # Double-check
	vgdisplay -v "$OCF_RESKEY_volgrpname" >/dev/null 2>&1
	if [ $? -ne 0 ]; then
	    ocf_log err "Volume group $OCF_RESKEY_volgrpname does not exist or contains error!"
	    exit $OCF_ERR_GENERIC
	fi
    fi

    return $OCF_SUCCESS
}

#
# 'main' starts here...
#
if [ $# -ne 1 ]; then
    usage
    exit $OCF_ERR_ARGS
fi

case $1 in
  meta-data)	meta_data
		exit $OCF_SUCCESS;;

  methods)	VolumeGroup_methods
		exit $OCF_SUCCESS;;

  usage)	usage
		exit $OCF_SUCCESS;;
  *)		;;
esac

# Everything except usage and meta-data must pass the validate test
VolumeGroup_validate_all

# What kind of method was invoked?
case "$1" in
    start)
	VolumeGroup_start
	;;
    stop)
	VolumeGroup_stop
	;;
    status)
	VolumeGroup_status
	;;
    monitor)
	VolumeGroup_monitor
	;;
    validate-all)
	;;
    notify|promote|demote|migrate_from|migrate_to)
	usage
	exit $OCF_ERR_UNIMPLEMENTED
	;;
    *)	usage
	exit $OCF_ERR_ARGS
	;;
esac

exit $?