summaryrefslogtreecommitdiffstats
path: root/heartbeat/lvmlockd
blob: f4b299f2879dedd48058472a0c4682aee0fbb7c9 (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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#!/bin/sh
#
#
#	lvmlockd OCF Resource Agent
#
# Copyright (c) 2017 SUSE LINUX, Eric Ren
#			All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it would be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# Further, this software is distributed without any warranty that it is
# free of the rightful claim of any third person regarding infringement
# or the like.	Any license provided herein, whether implied or
# otherwise, applies only to this software file.  Patent licenses, if
# any, provided herein do not apply to combinations of this program with
# other software, or any other product whatsoever.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
#

#######################################################################
# Initialization:

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

# Parameter defaults

OCF_RESKEY_pidfile_default="/run/lvmlockd.pid"
OCF_RESKEY_socket_path_default="/run/lvm/lvmlockd.socket"
OCF_RESKEY_syslog_priority_default="warning"
OCF_RESKEY_adopt_default="1"

: ${OCF_RESKEY_pidfile=${OCF_RESKEY_pidfile_default}}
: ${OCF_RESKEY_socket_path=${OCF_RESKEY_socket_path_default}}
: ${OCF_RESKEY_syslog_priority=${OCF_RESKEY_syslog_priority_default}}
: ${OCF_RESKEY_adopt=${OCF_RESKEY_adopt_default}}

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

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

<longdesc lang="en">
This agent manages the lvmlockd daemon. "lvmlockd" is like "clvmd". Both
are used by LVM commands to coordinate access to shared storage, but with
different design and implementations. "lvmlockd" can use two lock managers:
dlm and sanlock. This agent only supports "dlm + lvmlockd". If dlm (or corosync)
are already being used by other cluster software, you are advised to select
dlm, then configure "controld" resource agent for dlm and this agent for "lvmlockd".
Otherwise, consider sanlock for "lvmlockd" if dlm/corosync is not required.

Using lvmlockd requires the settings in LVM configuration file (/etc/lvm/lvm.conf):
"locking_type = 1" and "use_lvmlockd = 1". This RA will change the settings
respectively if needed.

For more information, refer to manpage lvmlockd.8.
</longdesc>
<shortdesc lang="en">This agent manages the lvmlockd daemon</shortdesc>

<parameters>
<parameter name="pidfile" unique="0">
<longdesc lang="en">pid file</longdesc>
<shortdesc lang="en">pid file</shortdesc>
<content type="string" default="${OCF_RESKEY_pidfile_default}"/>
</parameter>

<parameter name="socket_path" unique="0">
<longdesc lang="en">Set the socket path to listen on.</longdesc>
<shortdesc lang="en">socket path</shortdesc>
<content type="string" default="${OCF_RESKEY_socket_path_default}"/>
</parameter>

<parameter name="syslog_priority" unique="0">
<longdesc lang="en">Write log messages from this level up to syslog.</longdesc>
<shortdesc lang="en">syslog priority</shortdesc>
<content type="string" default="${OCF_RESKEY_syslog_priority_default}"/>
</parameter>

<parameter name="adopt" unique="0">
<longdesc lang="en">
Adopt locks from a previous instance of lvmlockd.
</longdesc>
<shortdesc lang="en">Adopt locks from a previous instance of lvmlockd</shortdesc>
<content type="integer" default="${OCF_RESKEY_adopt_default}"/>
</parameter>
</parameters>

<actions>
<action name="start"		timeout="90s" />
<action name="stop"		timeout="90s" />
<action name="monitor"		timeout="90s" interval="30s" depth="0" />
<action name="meta-data"	timeout="10s" />
<action name="validate-all"	timeout="20s" />
</actions>
</resource-agent>
END
}

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

LOCKD="lvmlockd"
# 0.5s sleep each count
TIMEOUT_COUNT=20

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

get_pid()
{
	if [ -f ${OCF_RESKEY_pidfile} ] ; then
		cat ${OCF_RESKEY_pidfile}
	else
		false
	fi
}

daemon_is_running()
{
	local pid=$1

	# Use /proc if it exists there
	if [ -d /proc ] && [ -d /proc/1 ] ; then
		[ -d /proc/"$pid" ]
	else
		kill -s 0 "$pid" >/dev/null 2>&1
	fi
}

silent_status()
{
	local pid=$(get_pid)

	if [ -n "$pid" ] ; then
		daemon_is_running "$pid"
		rc=$?
		mirror_rc=$rc

		# If these ever don't match, return error to force recovery
		if [ $mirror_rc -ne $rc ]; then
			return $OCF_ERR_GENERIC
		fi

		return $rc
	else
		# No pid file
		false
	fi
}

# change /etc/lvm/lvm.conf to use lvmlockd
setup_lvm_config()
{
	local out=""
	local use_lvmlockd=""
	local lock_type=""

	# To use lvmlockd, ensure configure lvm.conf:
	# locking_type = 1
	# use_lvmlockd = 1
	out=$(lvmconfig 'global/use_lvmlockd' 2> /dev/null)
	use_lvmlockd=$(echo "$out" | cut -d'=' -f2)

	out=$(lvmconfig 'global/locking_type' 2> /dev/null)
	lock_type=$(echo "$out" | cut -d'=' -f2)

	if [ -z "$use_lvmlockd" ]; then
		ocf_log info "adding \"use_lvmlockd=1\" to /etc/lvm/lvm.conf ..."
		cat >> /etc/lvm/lvm.conf << EOF

global {
    use_lvmlockd = 1
}
EOF

		if [ $? -ne 0 ]; then
			ocf_exit_reason "unable to add \"use_lvmlockd=1\" to /etc/lvm/lvm.conf ..."
			exit $OCF_ERR_CONFIGURED
		fi
	elif [ "$use_lvmlockd" != 1 ] ; then
		ocf_log info "setting \"use_lvmlockd=1\" in /etc/lvm/lvm.conf ..."
		sed -i 's,^[[:blank:]]*use_lvmlockd[[:blank:]]*=.*,\ \ \ \ use_lvmlockd = 1,g' /etc/lvm/lvm.conf
	fi

	if [ -n "$lock_type" ] ; then
		# locking_type was removed from config in v2.03
		ocf_version_cmp "$(lvmconfig --version | awk '/LVM ver/ {sub(/\(.*/, "", $3); print $3}')" "2.03"
		case "$?" in
			1|2)
				ocf_log info "removing \"locking_type\" from /etc/lvm/lvm.conf ..."
				sed -i '/^[[:blank:]]*locking_type[[:blank:]]*=.*/d' /etc/lvm/lvm.conf
				;;
			0)
				if [ "$lock_type" != 1 ] ; then
					ocf_log info "setting \"locking_type=1\" in /etc/lvm/lvm.conf ..."
					sed -i 's,^[[:blank:]]*locking_type[[:blank:]]*=.*,\ \ \ \ locking_type = 1,g' /etc/lvm/lvm.conf
				fi
				;;
		esac
	fi

	return $OCF_SUCCESS
}

check_dlm_controld()
{
	local pid=""

	# dlm daemon should have only one instance, but for safe...
	pid=$(pgrep dlm_controld | head -n1)
	if ! daemon_is_running $pid ; then
		ocf_exit_reason "DLM is not running. Is it configured?"
		exit $OCF_ERR_CONFIGURED
	fi

	return $OCF_SUCCESS
}

lvmlockd_start() {
	local extras=""

	setup_lvm_config

	ocf_log info "checking if DLM is started first..."
	check_dlm_controld

	if silent_status ; then
		ocf_log info "${LOCKD} already started (pid=$(get_pid))"
		return $OCF_SUCCESS
	fi

	if [ ! -z "$OCF_RESKEY_socket_path" ] ; then
		extras="$extras -s ${OCF_RESKEY_socket_path}"
	fi
	if [ ! -z "$OCF_RESKEY_syslog_priority" ] ; then
		extras="$extras -S ${OCF_RESKEY_syslog_priority}"
	fi
	if [ ! -z "$OCF_RESKEY_adopt" ] ; then
		extras="$extras -A ${OCF_RESKEY_adopt}"
	else
		# Inside lvmlockd daemon, this option defaults to 0. But, we
		# want it defaults to 1 for resource agent. When RA monitor pulls
		# this daemon up, we expect it to adopt locks from a previous
		# instance of lvmlockd.
		extras="$extras -A 1"
	fi
	# This client only support "dlm" lock manager
	extras="$extras -g dlm"

	ocf_log info "starting ${LOCKD}..."
	ocf_run ${LOCKD} -p ${OCF_RESKEY_pidfile} $extras
	rc=$?
	if [ $rc -ne $OCF_SUCCESS ] ; then
		ocf_exit_reason "Failed to start ${LOCKD}, exit code: $rc"
		return $OCF_ERR_GENERIC
	fi

	return $OCF_SUCCESS
}

# Each shared VG has its own lockspace. Besides, lvm_global lockspace
# is for global use, and it should be the last one to close. It should
# be enough to only check on lvm_global.
wait_lockspaces_close()
{
	local retries=0

	ocf_log info "Waiting for all lockspaces to be closed"
	while [ $retries -lt "$TIMEOUT_COUNT" ]
	do
		if ! dlm_tool ls lvm_global | grep -Eqs "^name[[:space:]]+lvm_global" ; then
			return $OCF_SUCCESS
		fi

		sleep 0.5
		retries=$((retries + 1))
	done

	ocf_exit_reason "Failed to close all lockspaces clearly"
	exit $OCF_ERR_GENERIC
}

kill_stop()
{
	local proc=$1
	local pid=$2
	local retries=0

	ocf_log info "Killing $proc (pid=$pid)"
	while
		daemon_is_running $pid && [ $retries -lt "$TIMEOUT_COUNT" ]
	do
		if [ $retries -ne 0 ] ; then
			# don't sleep on the first try
			sleep 0.5
		fi
		kill -s TERM $pid >/dev/null 2>&1
		retries=$((retries + 1))
	done

}

lvmlockd_stop() {
	local pid=""

	if ! silent_status ; then
		ocf_log info "${LOCKD} is not running"
		return $OCF_SUCCESS
	fi

	if [ -n "$(dlm_tool ls)" ]; then
		# We are going to stop lvmlockd, at this moment, we hope all shared VG have
		# been deactivated, otherwise we are in trouble: the stop action will fail!
		ocf_log info "stop the lockspaces of shared VG(s)..."
		ocf_run lvmlockctl --stop-lockspaces
		rc=$?
		if [ $rc -ne $OCF_SUCCESS ] ; then
			ocf_exit_reason "Failed to close lockspace, exit code: $rc"
			return $OCF_ERR_GENERIC
		fi
	fi

	wait_lockspaces_close

	pid=$(get_pid)
	kill_stop $LOCKD $pid

	if silent_status ; then
		ocf_exit_reason "Failed to stop, ${LOCKD} still running."
		return $OCF_ERR_GENERIC
	fi

	return $OCF_SUCCESS
}

lvmlockd_monitor() {
	if silent_status ; then
		return $OCF_SUCCESS
	fi

	ocf_log info "${LOCKD} not running"
	return $OCF_NOT_RUNNING
}

lvmlockd_validate() {
	check_binary ${LOCKD}
	check_binary lvm
	check_binary dlm_tool
	check_binary pgrep
	check_binary lvmlockctl

	return $OCF_SUCCESS
}


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

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

# Translate each action into the appropriate function call
case $__OCF_ACTION in
start)			lvmlockd_start
			;;
stop)			lvmlockd_stop
			;;
monitor)		lvmlockd_monitor
			;;
validate-all)		lvmlockd_validate
			;;
*)			usage
			exit $OCF_ERR_UNIMPLEMENTED
			;;
esac
rc=$?

ocf_log debug "${OCF_RESOURCE_INSTANCE} $__OCF_ACTION : $rc"
exit $rc