aboutsummaryrefslogtreecommitdiff
blob: d05f3a301308c1de81ba5f7dae5b2521d068e7c4 (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
#!/sbin/openrc-run
# Copyright 1999-2016 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id: a09b0efac60ad04485b1b54275adba1fc802390e $

_get_lvm_path() {
	local lvm_path

	for lvm_path in /bin/lvm /sbin/lvm ; do
		[ -x "${lvm_path}" ] && break
	done
	if ! [ -x "${lvm_path}" ]; then
		eerror "Cannot find lvm binary in /sbin or /bin!"
		return 1
	fi

	echo "${lvm_path}"
	return 0
}

_need_lvmetad()
{
	local lvm_path="$(_get_lvm_path)"
	[ ! -x "${lvm_path}" ] && return 1
	${lvm_path} dumpconfig global 2>/dev/null | grep -q 'use_lvmetad=1'
}

depend() {
	local _need

	before checkfs fsck
	after modules device-mapper
	# We may use lvmetad based on the configuration. If we added lvmetad
	# support while lvm2 is running then we aren't dependent on it. For the
	# more common case, if its disabled in the config we aren't dependent
	# on it.
	config /etc/lvm/lvm.conf

	if service_started; then
		_need=$(service_get_value need)
	else
		if _need_lvmetad; then
			_need="${_need} lvmetad"
		fi
	fi
	need sysfs ${_need}
}

dm_in_proc() {
	local x
	local retval
	declare -i retval=0

	for x in devices misc ; do
		grep -qs 'device-mapper' /proc/"${x}"
		retval=$(( retval + ${?} ))
	done

	return ${retval}
}


if [ -d /var/lock/lvm ] && [ -w /var/lock/lvm ] && ! [ -d /dev/.lvm ]; then
	export lvm_config='global { locking_dir = "/var/lock/lvm" }'
else
	export lvm_config='global { locking_dir = "/dev/.lvm" }'
fi

start() {
	local lvm_path lvm_commands

	# LVM support for /usr, /home, /opt ....
	# This should be done *before* checking local
	# volumes, or they never get checked.

	# NOTE: Add needed modules for LVM or RAID, etc.
	#       to /etc/modules.autoload if needed
	lvm_path="$(_get_lvm_path)" || return 1

	if [ -z "${CDBOOT}" ] ; then
		if [ -e /proc/modules ] && ! dm_in_proc ; then
			modprobe dm-mod 2>/dev/null
		fi
		if [ -d /proc/lvm ] || dm_in_proc ; then
			ebegin "Setting up Logical Volume Manager"
			#einfo "Using configuration directive '${lvm_config}'"
			# Keep stderr, for debugging
			lvm_commands="#! ${lvm_path} --config '${lvm_config}'\n"
			# Perform extra pvscan pass since some devices might not have been available until very recently
			lvm_commands="${lvm_commands}pvscan\n"
			# Now make the nodes
			lvm_commands="${lvm_commands}vgscan --mknodes\n"
			# And turn them on!
			lvm_commands="${lvm_commands}vgchange --sysinit -a ly\n"
			# Order of this is important, have to work around dash and LVM readline
			printf "%b\n" "${lvm_commands}" | "${lvm_path}" /proc/self/fd/0 --config "${lvm_config}" >/dev/null
			eend ${?} "Failed to setup LVM"
		fi
	fi
}

start_post() {
	# Remember whether we needed lvmetad...
	if _need_lvmetad; then
		service_set_value need lvmetad
	fi
}

stop() {
	local lvm_path lvs vgs lvm_commands

	lvm_path="$(_get_lvm_path)" || return 1

	# Stop LVM2
	if [ -x "${lvm_path}" ] &&
	   [ -f /etc/lvmtab -o -d /etc/lvm ] &&
	   [ -d /proc/lvm  -o -n "$( grep device-mapper /proc/misc 2>/dev/null )" ]
	then
		vgs=$("${lvm_path}" vgs --config "${lvm_config}" -o vg_name --noheadings --nosuffix --rows 2> /dev/null | cut -d' ' -f 3-)
		lvs=$("${lvm_path}" lvs --config "${lvm_config}" -o vg_name,lv_name --noheadings --nosuffix 2> /dev/null | cut -d' ' -f 3- | sed 's| |/|')

		if [ -n "${vgs}" ] ; then
			ebegin "Shutting down Logical Volume Manager"
			#einfo "Using configuration directive '${lvm_config}'"

			# Keep stderr, for debugging
			lvm_commands="#! ${lvm_path} --config '${lvm_config}'\n"
			lvm_commands="${lvm_commands}lvchange --sysinit -a ln ${lvs}\n"
			lvm_commands="${lvm_commands}vgchange --sysinit -a ln ${vgs}\n"
			# Order of this is important, have to work around dash and LVM readline
			printf "%b\n" "${lvm_commands}" | "${lvm_path}" /proc/self/fd/0 --config "${lvm_config}" >/dev/null
			eend ${?} "Failed (some LVs may still be required for system-essential volumes)"
		fi
	fi
}

# vim:ts=4