File: LVM

package info (click to toggle)
resource-agents 1%3A4.0.0~rc1-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,644 kB
  • ctags: 2,191
  • sloc: sh: 47,713; ansic: 4,074; perl: 3,457; makefile: 663; xml: 89
file content (713 lines) | stat: -rwxr-xr-x 17,730 bytes parent folder | download | duplicates (2)
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
#!/bin/sh
#
# 
# LVM
#
# Description:	Manages an LVM volume as an HA resource
#
#
# Author:	Alan Robertson
# Support:	users@clusterlabs.org
# License:	GNU General Public License (GPL)
# Copyright:	(C) 2002 - 2005 International Business Machines, Inc.
#
#	This code significantly inspired by the LVM resource
#	in FailSafe by Lars Marowsky-Bree
#
#
# An example usage in /etc/ha.d/haresources: 
#			 node1	10.0.0.170 ServeRAID::1::1 LVM::myvolname
#
# See usage() function below for more details...
#
#		OCF parameters are as below:
#		OCF_RESKEY_volgrpname
#		
#######################################################################
# Initialization:

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

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


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

	$0 manages an	Linux Volume Manager volume (LVM) 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 'meta-data' operation show meta data 
	The 'methods' operation reports on the methods $0 supports

EOF
}

# default for "tag"
OUR_TAG="pacemaker"

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

<longdesc lang="en">
Resource script for LVM. It manages an Linux Volume Manager volume (LVM) 
as an HA resource. 
</longdesc>
<shortdesc lang="en">Controls the availability of an LVM Volume Group</shortdesc>

<parameters>
<parameter name="volgrpname" unique="1" 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.  This option works one of
two ways.  If the volume group has the cluster attribute set, then the volume group
will be activated exclusively using clvmd across the cluster.  If the cluster attribute
is not set, the volume group will be activated exclusively using a tag and the volume_list 
filter. When the tag option is in use, the volume_list in lvm.con must be initialized. This 
can be as simple as setting 'volume_list = []' depending on your setup.
</longdesc>
<shortdesc lang="en">Exclusive activation</shortdesc>
<content type="boolean" default="false" />
</parameter>

<parameter name="tag" unique="0" required="0">
<longdesc lang="en">
If "exclusive" is set on a non clustered volume group, this overrides the tag to be used.
</longdesc>
<shortdesc lang="en">Exclusive activation tag</shortdesc>
<content type="string" default="$OUR_TAG" />
</parameter>

<parameter name="partial_activation" unique="0" required="0">
<longdesc lang="en">
If set, the volume group will be activated partially even with some
physical volumes missing. It helps to set to true when using mirrored
logical volumes.
</longdesc>
<shortdesc lang="en">Activate VG partially when missing PVs</shortdesc>
<content type="string" 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?
#
LVM_methods() {
	cat <<EOF
	start
	stop
	status
	monitor
	methods
	validate-all
	meta-data
	usage
EOF
}

##
# returns mode
#
# 0 = normal (non-exclusive) local activation
# 1 = tagged-exclusive activation
# 2 = clvm-exclusive activation
##
VG_MODE=
get_vg_mode()
{
	if [ -n "$VG_MODE" ]; then
		echo "$VG_MODE"
		return
	fi

	VG_MODE=0
	if ocf_is_true "$OCF_RESKEY_exclusive"; then
		case $(vgs -o attr --noheadings $OCF_RESKEY_volgrpname | tr -d ' ') in
		?????c*)
			VG_MODE=2 ;;
		*)
			VG_MODE=1 ;;
		esac
	fi

	echo "$VG_MODE"
}

##
# Verify tags setup
##
verify_tags_environment()
{
	##
	# The volume_list must be initialized to something in order to
	# guarantee our tag will be filtered on startup
	##
	if ! lvm dumpconfig activation/volume_list; then
		ocf_log err  "LVM: Improper setup detected"
		ocf_exit_reason "The volume_list filter must be initialized in lvm.conf for exclusive activation without clvmd"
		return $OCF_ERR_GENERIC
	fi

	##
	# Our tag must _NOT_ be in the volume_list.  This agent
	# overrides the volume_list during activation using the
	# special tag reserved for cluster activation
	##
	if lvm dumpconfig activation/volume_list | grep -e "\"@$OUR_TAG\"" -e "\"${OCF_RESKEY_volgrpname}\""; then
		ocf_log err "LVM:  Improper setup detected"
		ocf_exit_reason "The volume_list in lvm.conf must not contain the cluster tag, \"$OUR_TAG\", or volume group, $OCF_RESKEY_volgrpname"
		return $OCF_ERR_GENERIC
	fi

	return $OCF_SUCCESS
}

check_initrd_warning()
{
	# First check to see if there is an initrd img we can safely
	# compare timestamps agaist.  If not, don't even bother with
	# this check.  This is known to work in rhel/fedora distros
	ls "/boot/*$(uname -r)*.img" > /dev/null 2>&1
	if [ $? -ne 0 ]; then
		return
	fi

	##
	# Now check to see if the initrd has been updated.
	# If not, the machine could boot and activate the VG outside
	# the control of pacemaker
	##
	if [ "$(find /boot -name *.img -newer /etc/lvm/lvm.conf)" = "" ]; then
		ocf_log warn "LVM:  Improper setup detected"
		ocf_log warn "* initrd image needs to be newer than lvm.conf"

		# While dangerous if not done the first time, there are many
		# cases where we don't simply want to fail here.  Instead,
		# keep warning until the user remakes the initrd - or has
		# it done for them by upgrading the kernel.
		#
		# initrd can be updated using this command.
		# dracut -H -f /boot/initramfs-$(uname -r).img $(uname -r)
		#
	fi
}

##
# does this vg have our tag
##
check_tags()
{
	local owner=`vgs -o tags --noheadings $OCF_RESKEY_volgrpname | tr -d ' '`

	if [ -z "$owner" ]; then
		# No-one owns this VG yet
		return 1
	fi

	if [ "$OUR_TAG" = "$owner" ]; then
		# yep, this is ours
		return 0
	fi

	# some other tag is set on this vg
	return 2
}

strip_tags()
{
	local i

	for i in `vgs --noheadings -o tags $OCF_RESKEY_volgrpname | sed s/","/" "/g`; do
		ocf_log info "Stripping tag, $i"

		# LVM version 2.02.98 allows changing tags if PARTIAL
		vgchange --deltag $i $OCF_RESKEY_volgrpname
	done

	if [ ! -z `vgs -o tags --noheadings $OCF_RESKEY_volgrpname | tr -d ' '` ]; then
		ocf_exit_reason "Failed to remove ownership tags from $OCF_RESKEY_volgrpname"
		return $OCF_ERR_GENERIC
	fi

	return $OCF_SUCCESS
}

set_tags()
{
	check_tags
	case $? in
	0)
		# we already own it.
		return $OCF_SUCCESS
		;;
	2)
		# other tags are set, strip them before setting
		if ! strip_tags; then
			return $OCF_ERR_GENERIC
		fi
		;;
	*)
		: ;;
	esac

	vgchange --addtag $OUR_TAG $OCF_RESKEY_volgrpname
	if [ $? -ne 0 ]; then
		ocf_exit_reason "Failed to add ownership tag to $OCF_RESKEY_volgrpname"
		return $OCF_ERR_GENERIC
	fi

	ocf_log info "New tag \"$OUR_TAG\" added to $OCF_RESKEY_volgrpname"
	return $OCF_SUCCESS
}

#
#	Return LVM status (silently)
#
LVM_status() {
	local rc=1
	loglevel="debug"

	# Set the log level of the error message
	if [ "X${2}" = "X" ]; then
		loglevel="err"
		if ocf_is_probe; then
			loglevel="warn"
		else 
			if [ ${OP_METHOD} = "stop" ]; then
				loglevel="info"
			fi
		fi
	fi
	
	if [ -d /dev/$1 ]; then
		test "`cd /dev/$1 && ls`" != ""
		rc=$?
		if [ $rc -ne 0 ]; then
			ocf_exit_reason "VG $1 with no logical volumes is not supported by this RA!"
		fi
	fi

	if [ $rc -ne 0 ]; then
		ocf_log $loglevel "LVM Volume $1 is not available (stopped)"
		rc=$OCF_NOT_RUNNING
	else
		case $(get_vg_mode) in
		1) # exclusive with tagging.
			# If vg is running, make sure the correct tag is present. Otherwise we
			# can not guarantee exclusive activation.
			if ! check_tags; then
				ocf_exit_reason "WARNING: $OCF_RESKEY_volgrpname is active without the cluster tag, \"$OUR_TAG\""
				rc=$OCF_ERR_GENERIC
			fi

			# make sure the environment for tags activation is still valid
			if ! verify_tags_environment; then
				rc=$OCF_ERR_GENERIC
			fi
			# let the user know if their initrd is older than lvm.conf.
			check_initrd_warning
			;;
		*)
			: ;;
		esac
	fi

	if [ "X${2}" = "X" ]; then
		# status call return
		return $rc
	fi

	# Report on LVM volume status to stdout...
	if [ $rc -eq 0 ]; then
		echo "Volume $1 is available (running)"
	else
		echo "Volume $1 is not available (stopped)"
	fi
	return $rc
}

get_activate_options()
{
	local options="-a"

	case $(get_vg_mode) in
	0) options="${options}ly";;
	1) options="${options}y --config activation{volume_list=[\"@${OUR_TAG}\"]}";;
	2) options="${options}ey";;
	esac

	if ocf_is_true "$OCF_RESKEY_partial_activation" ; then
		options="${options} --partial"
	fi

	# for clones (clustered volume groups), we'll also have to force
	# monitoring, even if disabled in lvm.conf.
	if ocf_is_clone; then
		options="$options --monitor y"
	fi
	
	echo $options
}

##
# Attempt to deactivate vg cluster wide and then start the vg exclusively
##
retry_exclusive_start()
{
	local vgchange_options="$(get_activate_options)"

	# Deactivate each LV in the group one by one cluster wide
	set -- $(lvs -o name,attr --noheadings $OCF_RESKEY_volgrpname 2> /dev/null)
	while [ $# -ge 2 ]; do
		case $2 in
		????ao*)
			# open LVs cannot be deactivated.
			return $OCF_ERR_GENERIC;;
		*)
			if ! lvchange -an $OCF_RESKEY_volgrpname/$1; then
				ocf_exit_reason "Unable to perform required deactivation of $OCF_RESKEY_volgrpname/$1 before starting"
				return $OCF_ERR_GENERIC
			fi
			;;
		esac
		shift 2
	done

	ocf_run vgchange $vgchange_options $OCF_RESKEY_volgrpname
}

#
#	Enable LVM volume
#
LVM_start() {
	local vgchange_options="$(get_activate_options)"
	local vg=$1
	local clvmd=0

	# TODO: This MUST run vgimport as well
	ocf_log info "Activating volume group $vg"
	if [ "$LVM_MAJOR" -eq "1" ]; then
		ocf_run vgscan $vg
	else
		ocf_run vgscan
	fi

	case $(get_vg_mode) in
	2)
		clvmd=1
		;;
	1)
		if ! set_tags; then
			return $OCF_ERR_GENERIC
		fi
		;;
	*)
		: ;;
	esac

	if ! ocf_run vgchange $vgchange_options $vg; then
		if [ $clvmd -eq 0 ]; then
			return $OCF_ERR_GENERIC
		fi

		# Failure to exclusively activate cluster vg.:
		# This could be caused by a remotely active LV, Attempt
		# to disable volume group cluster wide and try again.
		# Allow for some settling
		sleep 5
		if ! retry_exclusive_start; then
			return $OCF_ERR_GENERIC
		fi
	fi

	if LVM_status $vg; then
		: OK Volume $vg activated just fine!
		return $OCF_SUCCESS 
	else
		ocf_exit_reason "LVM: $vg did not activate correctly"
		return $OCF_NOT_RUNNING
	fi
}

#
#	Disable the LVM volume
#
LVM_stop() {
	local res=$OCF_ERR_GENERIC
	local vgchange_options="-aln"
	local vg=$1

	if ! vgs $vg > /dev/null 2>&1; then
		ocf_log info "Volume group $vg not found"
		return $OCF_SUCCESS
	fi

	ocf_log info "Deactivating volume group $vg"

	case $(get_vg_mode) in
		1) vgchange_options="-an" ;;
	esac

	for i in $(seq 10)
	do
		ocf_run vgchange $vgchange_options $vg
		res=$?
		if LVM_status $vg; then
			ocf_exit_reason "LVM: $vg did not stop correctly"
			res=1
		fi

		if [ $res -eq 0 ]; then
			break
		fi

		res=$OCF_ERR_GENERIC
		ocf_log warn "$vg still Active"
		ocf_log info "Retry deactivating volume group $vg"
		sleep 1
		which udevadm > /dev/null 2>&1 && udevadm settle --timeout=5
	done

	case $(get_vg_mode) in
	1)
		if [ $res -eq 0 ]; then
			strip_tags
			res=$?
		fi
		;;
	esac

	return $res
}

#
#	Check whether the OCF instance parameters are valid
#
LVM_validate_all() {
	check_binary $AWK

	##
	# lvmetad is a daemon that caches lvm metadata to improve the
	# performance of LVM commands. This daemon should never be used when
	# volume groups exist that are being managed by the cluster. The lvmetad
	# daemon introduces a response lag, where certain LVM commands look like
	# they have completed (like vg activation) when in fact the command
	# is still in progress by the lvmetad.  This can cause reliability issues
	# when managing volume groups in the cluster.  For Example, if you have a
	# volume group that is a dependency for another application, it is possible
	# the cluster will think the volume group is activated and attempt to start
	# the application before volume group is really accesible... lvmetad is bad.
	##
	lvm dumpconfig global/use_lvmetad | grep 'use_lvmetad.*=.*1' > /dev/null 2>&1
	if [ $? -eq 0 ]; then
		# for now warn users that lvmetad is enabled and that they should disable it. In the
		# future we may want to consider refusing to start, or killing the lvmetad daemon.
		ocf_log warn "Disable lvmetad in lvm.conf. lvmetad should never be enabled in a clustered environment. Set use_lvmetad=0 and kill the lvmetad process"
	fi
	
	##
	# Off-the-shelf tests...
	##
	VGOUT=`vgck ${VOLUME} 2>&1`
	if [ $? -ne 0 ]; then
		# Inconsistency might be due to missing physical volumes, which doesn't 
		# automatically mean we should fail.  If partial_activation=true then 
		# we should let start try to handle it, or if no PVs are listed as
		# "unknown device" then another node may have marked a device missing 
		# where we have access to all of them and can start without issue. 
		if vgs -o pv_attr --noheadings $OCF_RESKEY_volgrpname 2>/dev/null | grep 'm' > /dev/null 2>&1; then
			if vgs -o pv_name --noheadings $OCF_RESKEY_volgrpname 2>/dev/null | grep -E "unknown device|Couldn't find device|Device mismatch detected" > /dev/null 2>&1; then
				if ! ocf_is_true "$OCF_RESKEY_partial_activation" ; then
					# We are missing devices and cannot activate partially
					ocf_exit_reason "Volume group [$VOLUME] has devices missing.  Consider partial_activation=true to attempt to activate partially"
					exit $OCF_ERR_GENERIC
				else
					# We are missing devices but are allowed to activate partially. 
					# Assume that caused the vgck failure and carry on
					ocf_log warn "Volume group inconsistency detected with missing device(s) and partial_activation enabled.  Proceeding with requested action."
				fi
			fi
			# else the vg is partial but all devices are accounted for, so another 
			# node must have marked the device missing.  Proceed.
		else
			# vgck failure was for something other than missing devices
			ocf_exit_reason "Volume group [$VOLUME] does not exist or contains error! ${VGOUT}"
			exit $OCF_ERR_GENERIC
		fi
	fi

	##
	# Does the Volume Group exist?
	##
	if [ "$LVM_MAJOR" = "1" ]; then
		VGOUT=`vgdisplay ${VOLUME} 2>&1`
	else
		VGOUT=`vgdisplay -v ${VOLUME} 2>&1`
	fi
	if [ $? -ne 0 ]; then
		ocf_exit_reason "Volume group [$VOLUME] does not exist or contains error! ${VGOUT}"
		exit $OCF_ERR_GENERIC
	fi

	##
	# If exclusive activation is not enabled, then
	# further checking of proper setup is not necessary
	##
	if ! ocf_is_true "$OCF_RESKEY_exclusive"; then
		return $OCF_SUCCESS;
	fi

	##
	# Having cloned lvm resources with exclusive vg activation makes no sense at all.
	##
	if ocf_is_clone; then
		ocf_exit_reason "cloned lvm resources can not be activated exclusively"
		exit $OCF_ERR_CONFIGURED
	fi

	##
	# Make sure the cluster attribute is set and clvmd is up when exclusive
	# activation is enabled. Otherwise we can't exclusively activate the volume group.
	##
	case $(get_vg_mode) in
	1)  # exclusive activation using tags
		if ! verify_tags_environment; then
			exit $OCF_ERR_GENERIC
		fi
		;;
	2)  # exclusive activation with clvmd
		##
		# verify is clvmd running
		##
		if ! ps -C clvmd > /dev/null 2>&1; then
			ocf_exit_reason "$OCF_RESKEY_volgrpname has the cluster attribute set, but 'clvmd' is not running"
			exit $OCF_ERR_GENERIC
		fi
		;;
	*)
		: ;;
	esac

	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)	LVM_methods
		exit $?;;

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

if 
	[ -z "$OCF_RESKEY_volgrpname" ]
then
	ocf_exit_reason "You must identify the volume group name!"
	exit $OCF_ERR_CONFIGURED 
fi

# Get the LVM version number, for this to work we assume(thanks to panjiam):
# 
# LVM1 outputs like this
#
#	# vgchange --version
#	vgchange: Logical Volume Manager 1.0.3
#	Heinz Mauelshagen, Sistina Software	19/02/2002 (IOP 10)
#
# LVM2 and higher versions output in this format
#
#	# vgchange --version
#	LVM version:		 2.00.15 (2004-04-19)
#	Library version: 1.00.09-ioctl (2004-03-31)
#	Driver version:	4.1.0

LVM_VERSION=`vgchange --version 2>&1 | \
	$AWK '/Logical Volume Manager/ {print $5"\n"; exit; }
			 /LVM version:/ {printf $3"\n"; exit;}'`
rc=$?

if
	( [ $rc -ne 0 ] || [ -z "$LVM_VERSION" ] )
then
	ocf_exit_reason "LVM: $1 could not determine LVM version. Try 'vgchange --version' manually and modify $0 ?"
	exit $OCF_ERR_INSTALLED
fi
LVM_MAJOR="${LVM_VERSION%%.*}"

VOLUME=$OCF_RESKEY_volgrpname
OP_METHOD=$1

if [ -n "$OCF_RESKEY_tag" ]; then
	OUR_TAG=$OCF_RESKEY_tag
fi

# What kind of method was invoked?
case "$1" in

	start)
		LVM_validate_all
		LVM_start $VOLUME
		exit $?;;

	stop)	LVM_stop $VOLUME
		exit $?;;

	status)	LVM_status $VOLUME $1
		exit $?;;

	monitor)	LVM_status $VOLUME
		exit $?;;

	validate-all)	LVM_validate_all
		;;

	*)		usage
		exit $OCF_ERR_UNIMPLEMENTED;;
esac