1#!/bin/sh
2#
3# $NetBSD: lvm,v 1.8 2021/08/21 23:00:30 andvar Exp $
4#
5
6# PROVIDE: lvm
7# REQUIRE: root devpubd
8# BEFORE:  DISKS
9
10$_rc_subr_loaded . /etc/rc.subr
11
12name="lvm"
13rcvar=$name
14start_cmd="lvm_start"
15stop_cmd="lvm_stop"
16
17ifs_restore="$IFS";
18
19lvm_start()
20{
21	if [ -x /sbin/dmsetup ]; then
22		/sbin/dmsetup version >/dev/null
23		if [ $? -ne 0 ]; then
24			warn "Device-mapper not present in kernel"
25			return 1;
26		fi
27	fi
28
29	if [ -x /sbin/lvm ]; then
30		echo "Configuring lvm devices."
31
32		# Scan for all available VG's
33		/sbin/lvm vgscan --mknodes --ignorelockingfailure >/dev/null
34
35		# Activate all LV's and create appropriate nodes in /dev
36		/sbin/lvm vgchange --ignorelockingfailure -a y >/dev/null
37		LV_LIST=$(/sbin/lvm vgdisplay -C -o vg_name --noheadings 2>/dev/null)
38		echo " Activated Volume Groups:" $LV_LIST
39	fi
40}
41
42lvm_stop()
43{
44	if [ -x /sbin/dmsetup ]; then
45		/sbin/dmsetup version >/dev/null
46		if [ $? -ne 0 ]; then
47			warn "Device-mapper not present in kernel"
48			return 1;
49		fi
50	fi
51
52	if [ -x /sbin/lvm ]; then
53		echo "Unconfiguring lvm devices."
54
55		LOGICAL_VOLUMES=$(/sbin/lvm lvdisplay -C -o vg_name,lv_name \
56			-O vg_name --separator \/ --noheadings 2>/dev/null)
57		VOLUME_GROUPS=$(/sbin/lvm vgdisplay -C -o vg_name \
58			--separator " " --noheadings 2>/dev/null)
59
60		for lv in ${LOGICAL_VOLUMES}; do
61			LV_IS_ACTIVE=$(/sbin/lvm lvdisplay -C --noheadings \
62				-o lv_attr $lv)
63			case $LV_IS_ACTIVE in
64			*a*)
65				echo "  Shutting Down logical volume: ${lv}"
66				/sbin/lvm lvchange -an --ignorelockingfailure \
67					-P ${lv} >/dev/null
68				;;
69			esac
70		done
71
72		for vg in ${VOLUME_GROUPS}; do
73			# Set IFS to field separator
74			IFS=":"
75			set -- $(/sbin/lvm vgdisplay -cA ${vg} 2>/dev/null)
76			# The seventh parameter is number of opened LVs in a Volume Group
77			VG_HAS_ACTIVE_LV=$7
78			IFS="$ifs_restore";
79
80			if [ "${VG_HAS_ACTIVE_LV}" = 0 ]; then
81				echo "  Shutting Down volume group: ${vg}"
82				/sbin/lvm vgchange -an --ignorelockingfailure \
83					-P ${vg} >/dev/null
84			fi
85		done
86	fi
87}
88
89load_rc_config $name
90run_rc_command "$1"
91