xref: /freebsd/sys/contrib/openzfs/etc/init.d/zfs-mount.in (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
1#!@DEFAULT_INIT_SHELL@
2#
3# zfs-mount     This script will mount/umount the zfs filesystems.
4#
5# chkconfig:    2345 06 99
6# description:  This script will mount/umount the zfs filesystems during
7#               system boot/shutdown. Configuration of which filesystems
8#               should be mounted is handled by the zfs 'mountpoint' and
9#               'canmount' properties. See the zfs(8) man page for details.
10#               It is also responsible for all userspace zfs services.
11# probe: true
12#
13### BEGIN INIT INFO
14# Provides:          zfs-mount
15# Required-Start:    $local_fs zfs-import
16# Required-Stop:     $local_fs zfs-import
17# Default-Start:     2 3 4 5
18# Default-Stop:      0 1 6
19# X-Stop-After:      zfs-zed
20# Short-Description: Mount ZFS filesystems and volumes
21# Description: Run the `zfs mount -a` or `zfs umount -a` commands.
22### END INIT INFO
23#
24# Released under the 2-clause BSD license.
25#
26# The original script that acted as a template for this script came from
27# the Debian GNU/Linux kFreeBSD ZFS packages (which did not include a
28# licensing stansa) in the commit dated Mar 24, 2011:
29#   https://github.com/zfsonlinux/pkg-zfs/commit/80a3ae582b59c0250d7912ba794dca9e669e605a
30
31# Source the common init script
32. @sysconfdir@/zfs/zfs-functions
33
34# ----------------------------------------------------
35
36chkroot() {
37	while read line; do
38		set -- $line
39		if [ "$2" = "/" ]; 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 i mntpt val
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	# Require each volume/filesystem to have 'noauto' and no fsck
77	# option. This shouldn't really be necessary, as long as one
78	# can get zfs-import to run sufficiently early on in the boot
79	# process - before local mounts. This is just here in case/if
80	# this isn't possible.
81	check_boolean "$VERBOSE_MOUNT" && \
82	    zfs_log_begin_msg "Mounting volumes and filesystems registered in fstab"
83
84	read_mtab  "^/dev/(zd|zvol)"
85	read_fstab "^/dev/(zd|zvol)"
86	i=0; var=$(eval echo FSTAB_$i)
87	while [ -n "$(eval echo "$""$var")" ]
88	do
89		mntpt=$(eval echo "$""$var")
90		dev=$(eval echo "$"FSTAB_dev_$i)
91		if ! in_mtab "$mntpt" && ! is_mounted "$mntpt" && [ -e "$dev" ]
92		then
93			check_boolean "$VERBOSE_MOUNT" && \
94				zfs_log_progress_msg "$mntpt "
95			fsck "$dev" && mount "$mntpt"
96		fi
97
98		i=$((i + 1))
99		var=$(eval echo FSTAB_$i)
100	done
101
102	read_mtab  "[[:space:]]zfs[[:space:]]"
103	read_fstab "[[:space:]]zfs[[:space:]]"
104	i=0; var=$(eval echo FSTAB_$i)
105	while [ -n "$(eval echo "$""$var")" ]
106	do
107		mntpt=$(eval echo "$""$var")
108		if ! in_mtab "$mntpt" && ! is_mounted "$mntpt"
109		then
110			check_boolean "$VERBOSE_MOUNT" && \
111				zfs_log_progress_msg "$mntpt "
112			mount "$mntpt"
113		fi
114
115		i=$((i + 1))
116		var=$(eval echo FSTAB_$i)
117	done
118	check_boolean "$VERBOSE_MOUNT" && zfs_log_end_msg 0
119
120	return 0
121}
122
123# Unmount all filesystems
124do_unmount()
125{
126	local i var mntpt
127
128	# This shouldn't really be necessary, as long as one can get
129	# zfs-import to run sufficiently late in the shutdown/reboot process
130	# - after unmounting local filesystems. This is just here in case/if
131	# this isn't possible.
132	zfs_action "Unmounting ZFS filesystems" "$ZFS" unmount -a
133
134	check_boolean "$VERBOSE_MOUNT" && \
135	    zfs_log_begin_msg "Unmounting volumes and filesystems registered in fstab"
136
137	read_mtab  "^/dev/(zd|zvol)"
138	read_fstab "^/dev/(zd|zvol)"
139	i=0; var=$(eval echo FSTAB_$i)
140	while [ -n "$(eval echo "$""$var")" ]
141	do
142		mntpt=$(eval echo "$""$var")
143		dev=$(eval echo "$"FSTAB_dev_$i)
144		if in_mtab "$mntpt"
145		then
146			check_boolean "$VERBOSE_MOUNT" && \
147				zfs_log_progress_msg "$mntpt "
148			umount "$mntpt"
149		fi
150
151		i=$((i + 1))
152		var=$(eval echo FSTAB_$i)
153	done
154
155	read_mtab  "[[:space:]]zfs[[:space:]]"
156	read_fstab "[[:space:]]zfs[[:space:]]"
157	i=0; var=$(eval echo FSTAB_$i)
158	while [ -n "$(eval echo "$""$var")" ]
159	do
160		mntpt=$(eval echo "$""$var")
161		if in_mtab "$mntpt"; then
162			check_boolean "$VERBOSE_MOUNT" && \
163			    zfs_log_progress_msg "$mntpt "
164			umount "$mntpt"
165		fi
166
167		i=$((i + 1))
168		var=$(eval echo FSTAB_$i)
169	done
170	check_boolean "$VERBOSE_MOUNT" && zfs_log_end_msg 0
171
172	return 0
173}
174
175do_start()
176{
177	check_boolean "$ZFS_MOUNT" || exit 0
178
179	check_module_loaded "zfs" || exit 0
180
181	# Ensure / exists in /proc/self/mounts.
182	# This should be handled by rc.sysinit but lets be paranoid.
183	if ! chkroot
184	then
185		mount -f /
186	fi
187
188	do_mount
189}
190
191do_stop()
192{
193	check_boolean "$ZFS_UNMOUNT" || exit 0
194
195	check_module_loaded "zfs" || exit 0
196
197	do_unmount
198}
199
200# ----------------------------------------------------
201
202if [ ! -e /sbin/openrc-run ]
203then
204	case "$1" in
205		start)
206			do_start
207			;;
208		stop)
209			do_stop
210			;;
211		force-reload|condrestart|reload|restart|status)
212			# no-op
213			;;
214		*)
215			[ -n "$1" ] && echo "Error: Unknown command $1."
216			echo "Usage: $0 {start|stop}"
217			exit 3
218			;;
219	esac
220
221	exit $?
222else
223	# Create wrapper functions since Gentoo don't use the case part.
224	depend() { do_depend; }
225	start() { do_start; }
226	stop() { do_stop; }
227fi
228