xref: /freebsd/sys/contrib/openzfs/etc/init.d/zfs-mount.in (revision 1323ec571215a77ddd21294f0871979d5ad6b992)
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 i mntpt
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	# Require each volume/filesystem to have 'noauto' and no fsck
75	# option. This shouldn't really be necessary, as long as one
76	# can get zfs-import to run sufficiently early on in the boot
77	# process - before local mounts. This is just here in case/if
78	# this isn't possible.
79	check_boolean "$VERBOSE_MOUNT" && \
80	    zfs_log_begin_msg "Mounting volumes and filesystems registered in fstab"
81
82	read_mtab  "^/dev/(zd|zvol)"
83	read_fstab "^/dev/(zd|zvol)"
84	i=0; var="FSTAB_0"
85	while [ -n "$(eval echo "\$$var")" ]
86	do
87		mntpt=$(eval echo "\$$var")
88		dev=$(eval echo "\$FSTAB_dev_$i")
89		if ! in_mtab "$mntpt" && ! is_mounted "$mntpt" && [ -e "$dev" ]
90		then
91			check_boolean "$VERBOSE_MOUNT" && \
92				zfs_log_progress_msg "$mntpt "
93			fsck "$dev" && mount "$mntpt"
94		fi
95
96		i=$((i + 1))
97		var=$(eval echo "FSTAB_$i")
98	done
99
100	read_mtab  "[[:space:]]zfs[[:space:]]"
101	read_fstab "[[:space:]]zfs[[:space:]]"
102	i=0; var=$(eval echo "FSTAB_$i")
103	while [ -n "$(eval echo "\$$var")" ]
104	do
105		mntpt=$(eval echo "\$$var")
106		if ! in_mtab "$mntpt" && ! is_mounted "$mntpt"
107		then
108			check_boolean "$VERBOSE_MOUNT" && \
109				zfs_log_progress_msg "$mntpt "
110			mount "$mntpt"
111		fi
112
113		i=$((i + 1))
114		var=$(eval echo "FSTAB_$i")
115	done
116	check_boolean "$VERBOSE_MOUNT" && zfs_log_end_msg 0
117
118	return 0
119}
120
121# Unmount all filesystems
122do_unmount()
123{
124	local i var mntpt
125
126	# This shouldn't really be necessary, as long as one can get
127	# zfs-import to run sufficiently late in the shutdown/reboot process
128	# - after unmounting local filesystems. This is just here in case/if
129	# this isn't possible.
130	zfs_action "Unmounting ZFS filesystems" "$ZFS" unmount -a
131
132	check_boolean "$VERBOSE_MOUNT" && \
133	    zfs_log_begin_msg "Unmounting volumes and filesystems registered in fstab"
134
135	read_mtab  "^/dev/(zd|zvol)"
136	read_fstab "^/dev/(zd|zvol)"
137	i=0; var="FSTAB_0"
138	while [ -n "$(eval echo "\$$var")" ]
139	do
140		mntpt=$(eval echo "\$$var")
141		dev=$(eval echo "\$FSTAB_dev_$i")
142		if in_mtab "$mntpt"
143		then
144			check_boolean "$VERBOSE_MOUNT" && \
145				zfs_log_progress_msg "$mntpt "
146			umount "$mntpt"
147		fi
148
149		i=$((i + 1))
150		var=$(eval echo "FSTAB_$i")
151	done
152
153	read_mtab  "[[:space:]]zfs[[:space:]]"
154	read_fstab "[[:space:]]zfs[[:space:]]"
155	i=0; var="FSTAB_0"
156	while [ -n "$(eval echo "\$$var")" ]
157	do
158		mntpt=$(eval echo "\$$var")
159		if in_mtab "$mntpt"; then
160			check_boolean "$VERBOSE_MOUNT" && \
161			    zfs_log_progress_msg "$mntpt "
162			umount "$mntpt"
163		fi
164
165		i=$((i + 1))
166		var=$(eval echo "FSTAB_$i")
167	done
168	check_boolean "$VERBOSE_MOUNT" && zfs_log_end_msg 0
169
170	return 0
171}
172
173do_start()
174{
175	check_boolean "$ZFS_MOUNT" || exit 0
176
177	check_module_loaded "zfs" || exit 0
178
179	# Ensure / exists in /proc/self/mounts.
180	# This should be handled by rc.sysinit but lets be paranoid.
181	if ! chkroot
182	then
183		mount -f /
184	fi
185
186	do_mount
187}
188
189do_stop()
190{
191	check_boolean "$ZFS_UNMOUNT" || exit 0
192
193	check_module_loaded "zfs" || exit 0
194
195	do_unmount
196}
197
198# ----------------------------------------------------
199
200if [ ! -e /sbin/openrc-run ]
201then
202	case "$1" in
203		start)
204			do_start
205			;;
206		stop)
207			do_stop
208			;;
209		force-reload|condrestart|reload|restart|status)
210			# no-op
211			;;
212		*)
213			[ -n "$1" ] && echo "Error: Unknown command $1."
214			echo "Usage: $0 {start|stop}"
215			exit 3
216			;;
217	esac
218
219	exit $?
220else
221	# Create wrapper functions since Gentoo don't use the case part.
222	depend() { do_depend; }
223	start() { do_start; }
224	stop() { do_stop; }
225fi
226