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