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