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