xref: /freebsd/sys/contrib/openzfs/etc/init.d/zfs-mount.in (revision aa1a8ff2d6dbc51ef058f46f3db5a8bb77967145)
1#!@DEFAULT_INIT_SHELL@
2# shellcheck disable=SC2154
3#
4# zfs-mount     This script will mount/umount the zfs filesystems.
5#
6# chkconfig:    2345 06 99
7# description:  This script will mount/umount the zfs filesystems during
8#               system boot/shutdown. Configuration of which filesystems
9#               should be mounted is handled by the zfs 'mountpoint' and
10#               'canmount' properties. See the zfs(8) man page for details.
11#               It is also responsible for all userspace zfs services.
12# probe: true
13#
14### BEGIN INIT INFO
15# Provides:          zfs-mount
16# Required-Start:    zfs-import
17# Required-Stop:     $local_fs zfs-import
18# Default-Start:     S
19# Default-Stop:      0 1 6
20# X-Start-Before:    mountall
21# X-Stop-After:      zfs-zed
22# Short-Description: Mount ZFS filesystems and volumes
23# Description: Run the `zfs mount -a` or `zfs umount -a` commands.
24### END INIT INFO
25#
26# Released under the 2-clause BSD license.
27#
28# This script is based on debian/zfsutils.zfs.init from the
29# Debian GNU/kFreeBSD zfsutils 8.1-3 package, written by Aurelien Jarno.
30
31# Source the common init script
32. @sysconfdir@/zfs/zfs-functions
33
34# ----------------------------------------------------
35
36chkroot() {
37	while read -r _ mp _; do
38		if [ "$mp" = "/" ]; then
39			return 0
40		fi
41	done < /proc/self/mounts
42
43	return 1
44}
45
46do_depend()
47{
48	# Try to allow people to mix and match fstab with ZFS in a way that makes sense.
49	if [ "$(mountinfo -s /)" = 'zfs' ]
50	then
51		before localmount
52	else
53		after localmount
54	fi
55
56	# bootmisc will log to /var which may be a different zfs than root.
57	before bootmisc logger
58
59	after zfs-import sysfs
60	use mtab
61	keyword -lxc -openvz -prefix -vserver
62}
63
64# Mount all datasets/filesystems
65do_mount()
66{
67	local verbose overlay
68
69	check_boolean "$VERBOSE_MOUNT" && verbose=v
70	check_boolean "$DO_OVERLAY_MOUNTS" && overlay=O
71
72	zfs_action "Mounting ZFS filesystem(s)" \
73	    "$ZFS" mount "-a$verbose$overlay" "$MOUNT_EXTRA_OPTIONS"
74
75	return 0
76}
77
78# Unmount all filesystems
79do_unmount()
80{
81	# This shouldn't really be necessary, as long as one can get
82	# zfs-import to run sufficiently late in the shutdown/reboot process
83	# - after unmounting local filesystems. This is just here in case/if
84	# this isn't possible.
85	zfs_action "Unmounting ZFS filesystems" "$ZFS" unmount -a
86
87	return 0
88}
89
90do_start()
91{
92	check_boolean "$ZFS_MOUNT" || exit 0
93
94	check_module_loaded "zfs" || exit 0
95
96	# Ensure / exists in /proc/self/mounts.
97	# This should be handled by rc.sysinit but lets be paranoid.
98	if ! chkroot
99	then
100		mount -f /
101	fi
102
103	do_mount
104}
105
106do_stop()
107{
108	check_boolean "$ZFS_UNMOUNT" || exit 0
109
110	check_module_loaded "zfs" || exit 0
111
112	do_unmount
113}
114
115# ----------------------------------------------------
116
117if @IS_SYSV_RC@
118then
119	case "$1" in
120		start)
121			do_start
122			;;
123		stop)
124			do_stop
125			;;
126		force-reload|condrestart|reload|restart|status)
127			# no-op
128			;;
129		*)
130			[ -n "$1" ] && echo "Error: Unknown command $1."
131			echo "Usage: $0 {start|stop}"
132			exit 3
133			;;
134	esac
135
136	exit $?
137else
138	# Create wrapper functions since Gentoo don't use the case part.
139	depend() { do_depend; }
140	start() { do_start; }
141	stop() { do_stop; }
142fi
143