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# This script is based on debian/zfsutils.zfs.init from the
27# Debian GNU/kFreeBSD zfsutils 8.1-3 package, written by Aurelien Jarno.
28
29# Source the common init script
30. @sysconfdir@/zfs/zfs-functions
31
32# ----------------------------------------------------
33
34chkroot() {
35	while read -r _ mp _; do
36		if [ "$mp" = "/" ]; then
37			return 0
38		fi
39	done < /proc/self/mounts
40
41	return 1
42}
43
44do_depend()
45{
46	# Try to allow people to mix and match fstab with ZFS in a way that makes sense.
47	if [ "$(mountinfo -s /)" = 'zfs' ]
48	then
49		before localmount
50	else
51		after localmount
52	fi
53
54	# bootmisc will log to /var which may be a different zfs than root.
55	before bootmisc logger
56
57	after zfs-import sysfs
58	use mtab
59	keyword -lxc -openvz -prefix -vserver
60}
61
62# Mount all datasets/filesystems
63do_mount()
64{
65	local verbose overlay i mntpt
66
67	check_boolean "$VERBOSE_MOUNT" && verbose=v
68	check_boolean "$DO_OVERLAY_MOUNTS" && overlay=O
69
70	zfs_action "Mounting ZFS filesystem(s)" \
71	    "$ZFS" mount -a$verbose$overlay "$MOUNT_EXTRA_OPTIONS"
72
73	# Require each volume/filesystem to have 'noauto' and no fsck
74	# option. This shouldn't really be necessary, as long as one
75	# can get zfs-import to run sufficiently early on in the boot
76	# process - before local mounts. This is just here in case/if
77	# this isn't possible.
78	check_boolean "$VERBOSE_MOUNT" && \
79	    zfs_log_begin_msg "Mounting volumes and filesystems registered in fstab"
80
81	read_mtab  "^/dev/(zd|zvol)"
82	read_fstab "^/dev/(zd|zvol)"
83	i=0; var="FSTAB_0"
84	while [ -n "$(eval echo "\$$var")" ]
85	do
86		mntpt=$(eval echo "\$$var")
87		dev=$(eval echo "\$FSTAB_dev_$i")
88		if ! in_mtab "$mntpt" && ! is_mounted "$mntpt" && [ -e "$dev" ]
89		then
90			check_boolean "$VERBOSE_MOUNT" && \
91				zfs_log_progress_msg "$mntpt "
92			fsck "$dev" && mount "$mntpt"
93		fi
94
95		i=$((i + 1))
96		var=$(eval echo "FSTAB_$i")
97	done
98
99	read_mtab  "[[:space:]]zfs[[:space:]]"
100	read_fstab "[[:space:]]zfs[[:space:]]"
101	i=0; var=$(eval echo "FSTAB_$i")
102	while [ -n "$(eval echo "\$$var")" ]
103	do
104		mntpt=$(eval echo "\$$var")
105		if ! in_mtab "$mntpt" && ! is_mounted "$mntpt"
106		then
107			check_boolean "$VERBOSE_MOUNT" && \
108				zfs_log_progress_msg "$mntpt "
109			mount "$mntpt"
110		fi
111
112		i=$((i + 1))
113		var=$(eval echo "FSTAB_$i")
114	done
115	check_boolean "$VERBOSE_MOUNT" && zfs_log_end_msg 0
116
117	return 0
118}
119
120# Unmount all filesystems
121do_unmount()
122{
123	local i var mntpt
124
125	# This shouldn't really be necessary, as long as one can get
126	# zfs-import to run sufficiently late in the shutdown/reboot process
127	# - after unmounting local filesystems. This is just here in case/if
128	# this isn't possible.
129	zfs_action "Unmounting ZFS filesystems" "$ZFS" unmount -a
130
131	check_boolean "$VERBOSE_MOUNT" && \
132	    zfs_log_begin_msg "Unmounting volumes and filesystems registered in fstab"
133
134	read_mtab  "^/dev/(zd|zvol)"
135	read_fstab "^/dev/(zd|zvol)"
136	i=0; var="FSTAB_0"
137	while [ -n "$(eval echo "\$$var")" ]
138	do
139		mntpt=$(eval echo "\$$var")
140		dev=$(eval echo "\$FSTAB_dev_$i")
141		if in_mtab "$mntpt"
142		then
143			check_boolean "$VERBOSE_MOUNT" && \
144				zfs_log_progress_msg "$mntpt "
145			umount "$mntpt"
146		fi
147
148		i=$((i + 1))
149		var=$(eval echo "FSTAB_$i")
150	done
151
152	read_mtab  "[[:space:]]zfs[[:space:]]"
153	read_fstab "[[:space:]]zfs[[:space:]]"
154	i=0; var="FSTAB_0"
155	while [ -n "$(eval echo "\$$var")" ]
156	do
157		mntpt=$(eval echo "\$$var")
158		if in_mtab "$mntpt"; then
159			check_boolean "$VERBOSE_MOUNT" && \
160			    zfs_log_progress_msg "$mntpt "
161			umount "$mntpt"
162		fi
163
164		i=$((i + 1))
165		var=$(eval echo "FSTAB_$i")
166	done
167	check_boolean "$VERBOSE_MOUNT" && zfs_log_end_msg 0
168
169	return 0
170}
171
172do_start()
173{
174	check_boolean "$ZFS_MOUNT" || exit 0
175
176	check_module_loaded "zfs" || exit 0
177
178	# Ensure / exists in /proc/self/mounts.
179	# This should be handled by rc.sysinit but lets be paranoid.
180	if ! chkroot
181	then
182		mount -f /
183	fi
184
185	do_mount
186}
187
188do_stop()
189{
190	check_boolean "$ZFS_UNMOUNT" || exit 0
191
192	check_module_loaded "zfs" || exit 0
193
194	do_unmount
195}
196
197# ----------------------------------------------------
198
199if [ ! -e /sbin/openrc-run ]
200then
201	case "$1" in
202		start)
203			do_start
204			;;
205		stop)
206			do_stop
207			;;
208		force-reload|condrestart|reload|restart|status)
209			# no-op
210			;;
211		*)
212			[ -n "$1" ] && echo "Error: Unknown command $1."
213			echo "Usage: $0 {start|stop}"
214			exit 3
215			;;
216	esac
217
218	exit $?
219else
220	# Create wrapper functions since Gentoo don't use the case part.
221	depend() { do_depend; }
222	start() { do_start; }
223	stop() { do_stop; }
224fi
225