xref: /freebsd/sys/contrib/openzfs/etc/zfs/zfs-functions.in (revision eda14cbc264d6969b02f2b1994cef11148e914f1)
1*eda14cbcSMatt Macy# This is a script with common functions etc used by zfs-import, zfs-mount,
2*eda14cbcSMatt Macy# zfs-share and zfs-zed.
3*eda14cbcSMatt Macy#
4*eda14cbcSMatt Macy# It is _NOT_ to be called independently
5*eda14cbcSMatt Macy#
6*eda14cbcSMatt Macy# Released under the 2-clause BSD license.
7*eda14cbcSMatt Macy#
8*eda14cbcSMatt Macy# The original script that acted as a template for this script came from
9*eda14cbcSMatt Macy# the Debian GNU/Linux kFreeBSD ZFS packages (which did not include a
10*eda14cbcSMatt Macy# licensing stansa) in the commit dated Mar 24, 2011:
11*eda14cbcSMatt Macy#   https://github.com/zfsonlinux/pkg-zfs/commit/80a3ae582b59c0250d7912ba794dca9e669e605a
12*eda14cbcSMatt Macy
13*eda14cbcSMatt MacyPATH=/sbin:/bin:/usr/bin:/usr/sbin
14*eda14cbcSMatt Macy
15*eda14cbcSMatt Macy# Source function library
16*eda14cbcSMatt Macyif [ -f /etc/rc.d/init.d/functions ]; then
17*eda14cbcSMatt Macy	# RedHat and derivates
18*eda14cbcSMatt Macy	. /etc/rc.d/init.d/functions
19*eda14cbcSMatt Macyelif [ -L /etc/init.d/functions.sh ]; then
20*eda14cbcSMatt Macy	# Gentoo
21*eda14cbcSMatt Macy	. /etc/init.d/functions.sh
22*eda14cbcSMatt Macyelif [ -f /lib/lsb/init-functions ]; then
23*eda14cbcSMatt Macy	# LSB, Debian GNU/Linux and derivates
24*eda14cbcSMatt Macy	. /lib/lsb/init-functions
25*eda14cbcSMatt Macyfi
26*eda14cbcSMatt Macy
27*eda14cbcSMatt Macy# Of course the functions we need are called differently
28*eda14cbcSMatt Macy# on different distributions - it would be way too easy
29*eda14cbcSMatt Macy# otherwise!!
30*eda14cbcSMatt Macyif type log_failure_msg > /dev/null 2>&1 ; then
31*eda14cbcSMatt Macy	# LSB functions - fall through
32*eda14cbcSMatt Macy	zfs_log_begin_msg() { log_begin_msg "$1"; }
33*eda14cbcSMatt Macy	zfs_log_end_msg() { log_end_msg "$1"; }
34*eda14cbcSMatt Macy	zfs_log_failure_msg() { log_failure_msg "$1"; }
35*eda14cbcSMatt Macy	zfs_log_progress_msg() { log_progress_msg "$1"; }
36*eda14cbcSMatt Macyelif type success > /dev/null 2>&1 ; then
37*eda14cbcSMatt Macy	# Fedora/RedHat functions
38*eda14cbcSMatt Macy	zfs_set_ifs() {
39*eda14cbcSMatt Macy		# For some reason, the init function library have a problem
40*eda14cbcSMatt Macy		# with a changed IFS, so this function goes around that.
41*eda14cbcSMatt Macy		local tIFS="$1"
42*eda14cbcSMatt Macy		if [ -n "$tIFS" ]
43*eda14cbcSMatt Macy		then
44*eda14cbcSMatt Macy			TMP_IFS="$IFS"
45*eda14cbcSMatt Macy			IFS="$tIFS"
46*eda14cbcSMatt Macy		fi
47*eda14cbcSMatt Macy	}
48*eda14cbcSMatt Macy
49*eda14cbcSMatt Macy	zfs_log_begin_msg() { echo -n "$1 "; }
50*eda14cbcSMatt Macy	zfs_log_end_msg() {
51*eda14cbcSMatt Macy		zfs_set_ifs "$OLD_IFS"
52*eda14cbcSMatt Macy		if [ "$1" -eq 0 ]; then
53*eda14cbcSMatt Macy			success
54*eda14cbcSMatt Macy		else
55*eda14cbcSMatt Macy			failure
56*eda14cbcSMatt Macy		fi
57*eda14cbcSMatt Macy		echo
58*eda14cbcSMatt Macy		zfs_set_ifs "$TMP_IFS"
59*eda14cbcSMatt Macy	}
60*eda14cbcSMatt Macy	zfs_log_failure_msg() {
61*eda14cbcSMatt Macy		zfs_set_ifs "$OLD_IFS"
62*eda14cbcSMatt Macy		failure
63*eda14cbcSMatt Macy		echo
64*eda14cbcSMatt Macy		zfs_set_ifs "$TMP_IFS"
65*eda14cbcSMatt Macy	}
66*eda14cbcSMatt Macy	zfs_log_progress_msg() { echo -n $"$1"; }
67*eda14cbcSMatt Macyelif type einfo > /dev/null 2>&1 ; then
68*eda14cbcSMatt Macy	# Gentoo functions
69*eda14cbcSMatt Macy	zfs_log_begin_msg() { ebegin "$1"; }
70*eda14cbcSMatt Macy	zfs_log_end_msg() { eend "$1"; }
71*eda14cbcSMatt Macy	zfs_log_failure_msg() { eend "$1"; }
72*eda14cbcSMatt Macy#	zfs_log_progress_msg() { echo -n "$1"; }
73*eda14cbcSMatt Macy	zfs_log_progress_msg() { echo -n; }
74*eda14cbcSMatt Macyelse
75*eda14cbcSMatt Macy	# Unknown - simple substitutes.
76*eda14cbcSMatt Macy	zfs_log_begin_msg() { echo -n "$1"; }
77*eda14cbcSMatt Macy	zfs_log_end_msg() {
78*eda14cbcSMatt Macy		ret=$1
79*eda14cbcSMatt Macy		if [ "$ret" -ge 1 ]; then
80*eda14cbcSMatt Macy			echo " failed!"
81*eda14cbcSMatt Macy		else
82*eda14cbcSMatt Macy			echo " success"
83*eda14cbcSMatt Macy		fi
84*eda14cbcSMatt Macy		return "$ret"
85*eda14cbcSMatt Macy	}
86*eda14cbcSMatt Macy	zfs_log_failure_msg() { echo "$1"; }
87*eda14cbcSMatt Macy	zfs_log_progress_msg() { echo -n "$1"; }
88*eda14cbcSMatt Macyfi
89*eda14cbcSMatt Macy
90*eda14cbcSMatt Macy# Paths to what we need
91*eda14cbcSMatt MacyZFS="@sbindir@/zfs"
92*eda14cbcSMatt MacyZED="@sbindir@/zed"
93*eda14cbcSMatt MacyZPOOL="@sbindir@/zpool"
94*eda14cbcSMatt MacyZPOOL_CACHE="@sysconfdir@/zfs/zpool.cache"
95*eda14cbcSMatt Macy
96*eda14cbcSMatt Macy# Sensible defaults
97*eda14cbcSMatt MacyZFS_MOUNT='yes'
98*eda14cbcSMatt MacyZFS_UNMOUNT='yes'
99*eda14cbcSMatt MacyZFS_SHARE='yes'
100*eda14cbcSMatt MacyZFS_UNSHARE='yes'
101*eda14cbcSMatt Macy
102*eda14cbcSMatt Macy# Source zfs configuration, overriding the defaults
103*eda14cbcSMatt Macyif [ -f @initconfdir@/zfs ]; then
104*eda14cbcSMatt Macy	. @initconfdir@/zfs
105*eda14cbcSMatt Macyfi
106*eda14cbcSMatt Macy
107*eda14cbcSMatt Macy# ----------------------------------------------------
108*eda14cbcSMatt Macy
109*eda14cbcSMatt Macyexport ZFS ZED ZPOOL ZPOOL_CACHE ZFS_MOUNT ZFS_UNMOUNT ZFS_SHARE ZFS_UNSHARE
110*eda14cbcSMatt Macy
111*eda14cbcSMatt Macyzfs_action()
112*eda14cbcSMatt Macy{
113*eda14cbcSMatt Macy	local MSG="$1";	shift
114*eda14cbcSMatt Macy	local CMD="$*"
115*eda14cbcSMatt Macy	local ret
116*eda14cbcSMatt Macy
117*eda14cbcSMatt Macy	zfs_log_begin_msg "$MSG "
118*eda14cbcSMatt Macy	$CMD
119*eda14cbcSMatt Macy	ret=$?
120*eda14cbcSMatt Macy	if [ "$ret" -eq 0 ]; then
121*eda14cbcSMatt Macy		zfs_log_end_msg $ret
122*eda14cbcSMatt Macy	else
123*eda14cbcSMatt Macy		zfs_log_failure_msg $ret
124*eda14cbcSMatt Macy	fi
125*eda14cbcSMatt Macy
126*eda14cbcSMatt Macy	return $ret
127*eda14cbcSMatt Macy}
128*eda14cbcSMatt Macy
129*eda14cbcSMatt Macy# Returns
130*eda14cbcSMatt Macy#   0 if daemon has been started
131*eda14cbcSMatt Macy#   1 if daemon was already running
132*eda14cbcSMatt Macy#   2 if daemon could not be started
133*eda14cbcSMatt Macy#   3 if unsupported
134*eda14cbcSMatt Macy#
135*eda14cbcSMatt Macyzfs_daemon_start()
136*eda14cbcSMatt Macy{
137*eda14cbcSMatt Macy	local PIDFILE="$1";	shift
138*eda14cbcSMatt Macy	local DAEMON_BIN="$1";	shift
139*eda14cbcSMatt Macy	local DAEMON_ARGS="$*"
140*eda14cbcSMatt Macy
141*eda14cbcSMatt Macy	if type start-stop-daemon > /dev/null 2>&1 ; then
142*eda14cbcSMatt Macy		# LSB functions
143*eda14cbcSMatt Macy		start-stop-daemon --start --quiet --pidfile "$PIDFILE" \
144*eda14cbcSMatt Macy		    --exec "$DAEMON_BIN" --test > /dev/null || return 1
145*eda14cbcSMatt Macy
146*eda14cbcSMatt Macy	        start-stop-daemon --start --quiet --exec "$DAEMON_BIN" -- \
147*eda14cbcSMatt Macy		    $DAEMON_ARGS || return 2
148*eda14cbcSMatt Macy
149*eda14cbcSMatt Macy		# On Debian GNU/Linux, there's a 'sendsigs' script that will
150*eda14cbcSMatt Macy		# kill basically everything quite early and zed is stopped
151*eda14cbcSMatt Macy		# much later than that. We don't want zed to be among them,
152*eda14cbcSMatt Macy		# so add the zed pid to list of pids to ignore.
153*eda14cbcSMatt Macy		if [ -f "$PIDFILE" -a -d /run/sendsigs.omit.d ]
154*eda14cbcSMatt Macy		then
155*eda14cbcSMatt Macy			ln -sf "$PIDFILE" /run/sendsigs.omit.d/zed
156*eda14cbcSMatt Macy		fi
157*eda14cbcSMatt Macy	elif type daemon > /dev/null 2>&1 ; then
158*eda14cbcSMatt Macy	        # Fedora/RedHat functions
159*eda14cbcSMatt Macy		daemon --pidfile "$PIDFILE" "$DAEMON_BIN" $DAEMON_ARGS
160*eda14cbcSMatt Macy		return $?
161*eda14cbcSMatt Macy	else
162*eda14cbcSMatt Macy		# Unsupported
163*eda14cbcSMatt Macy		return 3
164*eda14cbcSMatt Macy	fi
165*eda14cbcSMatt Macy
166*eda14cbcSMatt Macy	return 0
167*eda14cbcSMatt Macy}
168*eda14cbcSMatt Macy
169*eda14cbcSMatt Macy# Returns
170*eda14cbcSMatt Macy#   0 if daemon has been stopped
171*eda14cbcSMatt Macy#   1 if daemon was already stopped
172*eda14cbcSMatt Macy#   2 if daemon could not be stopped
173*eda14cbcSMatt Macy#   3 if unsupported
174*eda14cbcSMatt Macy#
175*eda14cbcSMatt Macyzfs_daemon_stop()
176*eda14cbcSMatt Macy{
177*eda14cbcSMatt Macy	local PIDFILE="$1"
178*eda14cbcSMatt Macy	local DAEMON_BIN="$2"
179*eda14cbcSMatt Macy	local DAEMON_NAME="$3"
180*eda14cbcSMatt Macy
181*eda14cbcSMatt Macy	if type start-stop-daemon > /dev/null 2>&1 ; then
182*eda14cbcSMatt Macy		# LSB functions
183*eda14cbcSMatt Macy		start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 \
184*eda14cbcSMatt Macy		    --pidfile "$PIDFILE" --name "$DAEMON_NAME"
185*eda14cbcSMatt Macy		[ "$?" = 0 ] && rm -f "$PIDFILE"
186*eda14cbcSMatt Macy
187*eda14cbcSMatt Macy		return $?
188*eda14cbcSMatt Macy	elif type killproc > /dev/null 2>&1 ; then
189*eda14cbcSMatt Macy		# Fedora/RedHat functions
190*eda14cbcSMatt Macy		killproc -p "$PIDFILE" "$DAEMON_NAME"
191*eda14cbcSMatt Macy		[ "$?" = 0 ] && rm -f "$PIDFILE"
192*eda14cbcSMatt Macy
193*eda14cbcSMatt Macy		return $?
194*eda14cbcSMatt Macy	else
195*eda14cbcSMatt Macy		# Unsupported
196*eda14cbcSMatt Macy		return 3
197*eda14cbcSMatt Macy	fi
198*eda14cbcSMatt Macy
199*eda14cbcSMatt Macy	return 0
200*eda14cbcSMatt Macy}
201*eda14cbcSMatt Macy
202*eda14cbcSMatt Macy# Returns status
203*eda14cbcSMatt Macyzfs_daemon_status()
204*eda14cbcSMatt Macy{
205*eda14cbcSMatt Macy	local PIDFILE="$1"
206*eda14cbcSMatt Macy	local DAEMON_BIN="$2"
207*eda14cbcSMatt Macy	local DAEMON_NAME="$3"
208*eda14cbcSMatt Macy
209*eda14cbcSMatt Macy	if type status_of_proc > /dev/null 2>&1 ; then
210*eda14cbcSMatt Macy		# LSB functions
211*eda14cbcSMatt Macy		status_of_proc "$DAEMON_NAME" "$DAEMON_BIN"
212*eda14cbcSMatt Macy		return $?
213*eda14cbcSMatt Macy	elif type status > /dev/null 2>&1 ; then
214*eda14cbcSMatt Macy		# Fedora/RedHat functions
215*eda14cbcSMatt Macy		status -p "$PIDFILE" "$DAEMON_NAME"
216*eda14cbcSMatt Macy		return $?
217*eda14cbcSMatt Macy	else
218*eda14cbcSMatt Macy		# Unsupported
219*eda14cbcSMatt Macy		return 3
220*eda14cbcSMatt Macy	fi
221*eda14cbcSMatt Macy
222*eda14cbcSMatt Macy	return 0
223*eda14cbcSMatt Macy}
224*eda14cbcSMatt Macy
225*eda14cbcSMatt Macyzfs_daemon_reload()
226*eda14cbcSMatt Macy{
227*eda14cbcSMatt Macy	local PIDFILE="$1"
228*eda14cbcSMatt Macy	local DAEMON_NAME="$2"
229*eda14cbcSMatt Macy
230*eda14cbcSMatt Macy	if type start-stop-daemon > /dev/null 2>&1 ; then
231*eda14cbcSMatt Macy		# LSB functions
232*eda14cbcSMatt Macy		start-stop-daemon --stop --signal 1 --quiet \
233*eda14cbcSMatt Macy		    --pidfile "$PIDFILE" --name "$DAEMON_NAME"
234*eda14cbcSMatt Macy		return $?
235*eda14cbcSMatt Macy	elif type killproc > /dev/null 2>&1 ; then
236*eda14cbcSMatt Macy		# Fedora/RedHat functions
237*eda14cbcSMatt Macy                killproc -p "$PIDFILE" "$DAEMON_NAME" -HUP
238*eda14cbcSMatt Macy		return $?
239*eda14cbcSMatt Macy	else
240*eda14cbcSMatt Macy		# Unsupported
241*eda14cbcSMatt Macy		return 3
242*eda14cbcSMatt Macy	fi
243*eda14cbcSMatt Macy
244*eda14cbcSMatt Macy	return 0
245*eda14cbcSMatt Macy}
246*eda14cbcSMatt Macy
247*eda14cbcSMatt Macyzfs_installed()
248*eda14cbcSMatt Macy{
249*eda14cbcSMatt Macy	if [ ! -x "$ZPOOL" ]; then
250*eda14cbcSMatt Macy		return 1
251*eda14cbcSMatt Macy	else
252*eda14cbcSMatt Macy		# Test if it works (will catch missing/broken libs etc)
253*eda14cbcSMatt Macy		"$ZPOOL" -? > /dev/null 2>&1
254*eda14cbcSMatt Macy		return $?
255*eda14cbcSMatt Macy	fi
256*eda14cbcSMatt Macy
257*eda14cbcSMatt Macy	if [ ! -x "$ZFS" ]; then
258*eda14cbcSMatt Macy		return 2
259*eda14cbcSMatt Macy	else
260*eda14cbcSMatt Macy		# Test if it works (will catch missing/broken libs etc)
261*eda14cbcSMatt Macy		"$ZFS" -? > /dev/null 2>&1
262*eda14cbcSMatt Macy		return $?
263*eda14cbcSMatt Macy	fi
264*eda14cbcSMatt Macy
265*eda14cbcSMatt Macy	return 0
266*eda14cbcSMatt Macy}
267*eda14cbcSMatt Macy
268*eda14cbcSMatt Macy# Trigger udev and wait for it to settle.
269*eda14cbcSMatt Macyudev_trigger()
270*eda14cbcSMatt Macy{
271*eda14cbcSMatt Macy	if [ -x /sbin/udevadm ]; then
272*eda14cbcSMatt Macy		/sbin/udevadm trigger --action=change --subsystem-match=block
273*eda14cbcSMatt Macy		/sbin/udevadm settle
274*eda14cbcSMatt Macy	elif [ -x /sbin/udevsettle ]; then
275*eda14cbcSMatt Macy		/sbin/udevtrigger
276*eda14cbcSMatt Macy		/sbin/udevsettle
277*eda14cbcSMatt Macy	fi
278*eda14cbcSMatt Macy}
279*eda14cbcSMatt Macy
280*eda14cbcSMatt Macy# Do a lot of checks to make sure it's 'safe' to continue with the import.
281*eda14cbcSMatt Macychecksystem()
282*eda14cbcSMatt Macy{
283*eda14cbcSMatt Macy	if grep -qiE '(^|[^\\](\\\\)* )zfs=(off|no|0)( |$)' /proc/cmdline;
284*eda14cbcSMatt Macy	then
285*eda14cbcSMatt Macy		# Called with zfs=(off|no|0) - bail because we don't
286*eda14cbcSMatt Macy		# want anything import, mounted or shared.
287*eda14cbcSMatt Macy		# HOWEVER, only do this if we're called at the boot up
288*eda14cbcSMatt Macy		# (from init), not if we're running interactively (as in
289*eda14cbcSMatt Macy		# from the shell - we know what we're doing).
290*eda14cbcSMatt Macy		[ -n "$init" ] && exit 3
291*eda14cbcSMatt Macy	fi
292*eda14cbcSMatt Macy
293*eda14cbcSMatt Macy	# Check if ZFS is installed.
294*eda14cbcSMatt Macy	zfs_installed || return 5
295*eda14cbcSMatt Macy
296*eda14cbcSMatt Macy	# Just make sure that /dev/zfs is created.
297*eda14cbcSMatt Macy	udev_trigger
298*eda14cbcSMatt Macy
299*eda14cbcSMatt Macy	return 0
300*eda14cbcSMatt Macy}
301*eda14cbcSMatt Macy
302*eda14cbcSMatt Macyget_root_pool()
303*eda14cbcSMatt Macy{
304*eda14cbcSMatt Macy	set -- $(mount | grep ' on / ')
305*eda14cbcSMatt Macy	[ "$5" = "zfs" ] && echo "${1%%/*}"
306*eda14cbcSMatt Macy}
307*eda14cbcSMatt Macy
308*eda14cbcSMatt Macy# Check if a variable is 'yes' (any case) or '1'
309*eda14cbcSMatt Macy# Returns TRUE if set.
310*eda14cbcSMatt Macycheck_boolean()
311*eda14cbcSMatt Macy{
312*eda14cbcSMatt Macy	local var="$1"
313*eda14cbcSMatt Macy
314*eda14cbcSMatt Macy	echo "$var" | grep -Eiq "^yes$|^on$|^true$|^1$" && return 0 || return 1
315*eda14cbcSMatt Macy}
316*eda14cbcSMatt Macy
317*eda14cbcSMatt Macycheck_module_loaded()
318*eda14cbcSMatt Macy{
319*eda14cbcSMatt Macy	module="$1"
320*eda14cbcSMatt Macy
321*eda14cbcSMatt Macy	[ -r "/sys/module/${module}/version" ] && return 0 || return 1
322*eda14cbcSMatt Macy}
323*eda14cbcSMatt Macy
324*eda14cbcSMatt Macyload_module()
325*eda14cbcSMatt Macy{
326*eda14cbcSMatt Macy	module="$1"
327*eda14cbcSMatt Macy
328*eda14cbcSMatt Macy	# Load the zfs module stack
329*eda14cbcSMatt Macy	if ! check_module_loaded "$module"; then
330*eda14cbcSMatt Macy		if ! /sbin/modprobe "$module"; then
331*eda14cbcSMatt Macy			return 5
332*eda14cbcSMatt Macy		fi
333*eda14cbcSMatt Macy	fi
334*eda14cbcSMatt Macy	return 0
335*eda14cbcSMatt Macy}
336*eda14cbcSMatt Macy
337*eda14cbcSMatt Macy# first parameter is a regular expression that filters mtab
338*eda14cbcSMatt Macyread_mtab()
339*eda14cbcSMatt Macy{
340*eda14cbcSMatt Macy	local match="$1"
341*eda14cbcSMatt Macy	local fs mntpnt fstype opts rest TMPFILE
342*eda14cbcSMatt Macy
343*eda14cbcSMatt Macy	# Unset all MTAB_* variables
344*eda14cbcSMatt Macy	unset $(env | grep ^MTAB_ | sed 's,=.*,,')
345*eda14cbcSMatt Macy
346*eda14cbcSMatt Macy	while read -r fs mntpnt fstype opts rest; do
347*eda14cbcSMatt Macy		if echo "$fs $mntpnt $fstype $opts" | grep -qE "$match"; then
348*eda14cbcSMatt Macy			# * Fix problems (!?) in the mounts file. It will record
349*eda14cbcSMatt Macy			#   'rpool 1' as 'rpool\0401' instead of 'rpool\00401'
350*eda14cbcSMatt Macy			#   which seems to be the correct (at least as far as
351*eda14cbcSMatt Macy			#   'printf' is concerned).
352*eda14cbcSMatt Macy			# * We need to use the external echo, because the
353*eda14cbcSMatt Macy			#   internal one would interpret the backslash code
354*eda14cbcSMatt Macy			#   (incorrectly), giving us a  instead.
355*eda14cbcSMatt Macy			mntpnt=$(/bin/echo "$mntpnt" | sed "s,\\\0,\\\00,g")
356*eda14cbcSMatt Macy			fs=$(/bin/echo "$fs" | sed "s,\\\0,\\\00,")
357*eda14cbcSMatt Macy
358*eda14cbcSMatt Macy			# Remove 'unwanted' characters.
359*eda14cbcSMatt Macy			mntpnt=$(printf '%b\n' "$mntpnt" | sed -e 's,/,,g' \
360*eda14cbcSMatt Macy			    -e 's,-,,g' -e 's,\.,,g' -e 's, ,,g')
361*eda14cbcSMatt Macy			fs=$(printf '%b\n' "$fs")
362*eda14cbcSMatt Macy
363*eda14cbcSMatt Macy			# Set the variable.
364*eda14cbcSMatt Macy			eval export MTAB_$mntpnt=\"$fs\"
365*eda14cbcSMatt Macy		fi
366*eda14cbcSMatt Macy	done < /proc/self/mounts
367*eda14cbcSMatt Macy}
368*eda14cbcSMatt Macy
369*eda14cbcSMatt Macyin_mtab()
370*eda14cbcSMatt Macy{
371*eda14cbcSMatt Macy	local mntpnt="$1"
372*eda14cbcSMatt Macy	# Remove 'unwanted' characters.
373*eda14cbcSMatt Macy	mntpnt=$(printf '%b\n' "$mntpnt" | sed -e 's,/,,g' \
374*eda14cbcSMatt Macy	    -e 's,-,,g' -e 's,\.,,g' -e 's, ,,g')
375*eda14cbcSMatt Macy	local var
376*eda14cbcSMatt Macy
377*eda14cbcSMatt Macy	var="$(eval echo MTAB_$mntpnt)"
378*eda14cbcSMatt Macy	[ "$(eval echo "$""$var")" != "" ]
379*eda14cbcSMatt Macy	return "$?"
380*eda14cbcSMatt Macy}
381*eda14cbcSMatt Macy
382*eda14cbcSMatt Macy# first parameter is a regular expression that filters fstab
383*eda14cbcSMatt Macyread_fstab()
384*eda14cbcSMatt Macy{
385*eda14cbcSMatt Macy	local match="$1"
386*eda14cbcSMatt Macy	local i var TMPFILE
387*eda14cbcSMatt Macy
388*eda14cbcSMatt Macy	# Unset all FSTAB_* variables
389*eda14cbcSMatt Macy	unset $(env | grep ^FSTAB_ | sed 's,=.*,,')
390*eda14cbcSMatt Macy
391*eda14cbcSMatt Macy	i=0
392*eda14cbcSMatt Macy	while read -r fs mntpnt fstype opts; do
393*eda14cbcSMatt Macy		echo "$fs" | egrep -qE '^#|^$' && continue
394*eda14cbcSMatt Macy		echo "$mntpnt" | egrep -qE '^none|^swap' && continue
395*eda14cbcSMatt Macy		echo "$fstype" | egrep -qE '^swap' && continue
396*eda14cbcSMatt Macy
397*eda14cbcSMatt Macy		if echo "$fs $mntpnt $fstype $opts" | grep -qE "$match"; then
398*eda14cbcSMatt Macy			eval export FSTAB_dev_$i="$fs"
399*eda14cbcSMatt Macy			fs=$(printf '%b\n' "$fs" | sed 's,/,_,g')
400*eda14cbcSMatt Macy			eval export FSTAB_$i="$mntpnt"
401*eda14cbcSMatt Macy
402*eda14cbcSMatt Macy			i=$((i + 1))
403*eda14cbcSMatt Macy		fi
404*eda14cbcSMatt Macy	done < /etc/fstab
405*eda14cbcSMatt Macy}
406*eda14cbcSMatt Macy
407*eda14cbcSMatt Macyin_fstab()
408*eda14cbcSMatt Macy{
409*eda14cbcSMatt Macy	local var
410*eda14cbcSMatt Macy
411*eda14cbcSMatt Macy	var="$(eval echo FSTAB_$1)"
412*eda14cbcSMatt Macy	[ "${var}" != "" ]
413*eda14cbcSMatt Macy	return $?
414*eda14cbcSMatt Macy}
415*eda14cbcSMatt Macy
416*eda14cbcSMatt Macyis_mounted()
417*eda14cbcSMatt Macy{
418*eda14cbcSMatt Macy	local mntpt="$1"
419*eda14cbcSMatt Macy	local line
420*eda14cbcSMatt Macy
421*eda14cbcSMatt Macy	mount | \
422*eda14cbcSMatt Macy	    while read line; do
423*eda14cbcSMatt Macy		if echo "$line" | grep -q " on $mntpt "; then
424*eda14cbcSMatt Macy		    # returns:
425*eda14cbcSMatt Macy		    #   0 on unsuccessful match
426*eda14cbcSMatt Macy		    #   1 on a successful match
427*eda14cbcSMatt Macy		    return 1
428*eda14cbcSMatt Macy		fi
429*eda14cbcSMatt Macy	    done
430*eda14cbcSMatt Macy
431*eda14cbcSMatt Macy	# The negation will flip the subshell return result where the default
432*eda14cbcSMatt Macy	# return value is 0 when a match is not found.
433*eda14cbcSMatt Macy	return $(( !$? ))
434*eda14cbcSMatt Macy}
435