aboutsummaryrefslogtreecommitdiffstats
path: root/etc/init.d/zfs-mount.in
blob: e108cb153f94054df91ea8e79d6b5bf631ba6d2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!@SHELL@
#
# zfs-mount     This script will mount/umount the zfs filesystems.
#
# chkconfig:    2345 06 99
# description:  This script will mount/umount the zfs filesystems during
#               system boot/shutdown. Configuration of which filesystems
#               should be mounted is handled by the zfs 'mountpoint' and
#               'canmount' properties. See the zfs(8) man page for details.
#               It is also responsible for all userspace zfs services.
# probe: true
#
### BEGIN INIT INFO
# Provides:          zfs-mount
# Required-Start:    $local_fs zfs-import
# Required-Stop:     $local_fs zfs-import
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# X-Stop-After:      zfs-share
# Short-Description: Mount ZFS filesystems and volumes
# Description: Run the `zfs mount -a` or `zfs umount -a` commands.
### END INIT INFO
#
# Released under the 2-clause BSD license.
#
# The original script that acted as a template for this script came from
# the Debian GNU/Linux kFreeBSD ZFS packages (which did not include a
# licensing stansa) in the commit dated Mar 24, 2011:
#   https://github.com/zfsonlinux/pkg-zfs/commit/80a3ae582b59c0250d7912ba794dca9e669e605a

# Source the common init script
. @sysconfdir@/zfs/zfs-functions

# ----------------------------------------------------

do_depend()
{
	after procfs zfs-import sysfs procps
	use mtab
	keyword -lxc -openvz -prefix -vserver
}

# Mount all datasets/filesystems
do_mount()
{
	local verbose overlay i mntpt val

	[ "$VERBOSE_MOUNT" = 'yes' ] && verbose=v
	[ "$OVERLAY_MOUNTS" = 'yes' ] && overlay=O

	zfs_action "Mounting ZFS filesystem(s)" \
	    "$ZFS" mount -a$verbose$overlay "$MOUNT_EXTRA_OPTIONS"

	# Require each volume/filesytem to have 'noauto' and no fsck
	# option. This shouldn't really be necessary, as long as one
	# can get zfs-import to run sufficiently early on in the boot
	# process - before local mounts. This is just here in case/if
	# this isn't possible.
	[ "$VERBOSE_MOUNT" = 'yes' ] && \
	    zfs_log_begin_msg "Mounting volumes and filesystems registered in fstab"

	read_mtab  "^/dev/(zd|zvol)"
	read_fstab "^/dev/(zd|zvol)"
	i=0; var=$(eval echo FSTAB_$i)
	while [ -n "$(eval echo "$""$var")" ]
	do
		mntpt=$(eval echo "$""$var")
		dev=$(eval echo "$"FSTAB_dev_$i)
		if ! in_mtab "$mntpt" && ! is_mounted "$mntpt" && [ -e "$dev" ]
		then
			[ "$VERBOSE_MOUNT" = 'yes' ] && \
				zfs_log_progress_msg "$mntpt "
			fsck "$dev" && mount "$mntpt"
		fi

		i=$((i + 1))
		var=$(eval echo FSTAB_$i)
	done

	read_mtab  "[[:space:]]zfs[[:space:]]"
	read_fstab "[[:space:]]zfs[[:space:]]"
	i=0; var=$(eval echo FSTAB_$i)
	while [ -n "$(eval echo "$""$var")" ]
	do
		mntpt=$(eval echo "$""$var")
		if ! in_mtab "$mntpt" && ! is_mounted "$mntpt"
		then
			[ "$VERBOSE_MOUNT" = 'yes' ] && \
				zfs_log_progress_msg "$mntpt "
			mount "$mntpt"
		fi

		i=$((i + 1))
		var=$(eval echo FSTAB_$i)
	done
	[ "$VERBOSE_MOUNT" = 'yes' ] && zfs_log_end_msg 0

	return 0
}

# Unmount all filesystems
do_unmount()
{
	local i var mntpt

	# This shouldn't really be necessary, as long as one can get
	# zfs-import to run sufficiently late in the shutdown/reboot process
	# - after unmounting local filesystems. This is just here in case/if
	# this isn't possible.
	zfs_action "Unmounting ZFS filesystems" "$ZFS" unmount -a

	[ "$VERBOSE_MOUNT" = 'yes' ] && \
	    zfs_log_begin_msg "Unmounting volumes and filesystems registered in fstab"

	read_mtab  "^/dev/(zd|zvol)"
	read_fstab "^/dev/(zd|zvol)"
	i=0; var=$(eval echo FSTAB_$i)
	while [ -n "$(eval echo "$""$var")" ]
	do
		mntpt=$(eval echo "$""$var")
		dev=$(eval echo "$"FSTAB_dev_$i)
		if in_mtab "$mntpt"
		then
			[ "$VERBOSE_MOUNT" = 'yes' ] && \
				zfs_log_progress_msg "$mntpt "
			umount "$mntpt"
		fi

		i=$((i + 1))
		var=$(eval echo FSTAB_$i)
	done

	read_mtab  "[[:space:]]zfs[[:space:]]"
	read_fstab "[[:space:]]zfs[[:space:]]"
	i=0; var=$(eval echo FSTAB_$i)
	while [ -n "$(eval echo "$""$var")" ]
	do
		mntpt=$(eval echo "$""$var")
		if in_mtab "$mntpt"; then
			[ "$VERBOSE_MOUNT" = 'yes' ] && \
			    zfs_log_progress_msg "$mntpt "
			umount "$mntpt"
		fi

		i=$((i + 1))
		var=$(eval echo FSTAB_$i)
	done
	[ "$VERBOSE_MOUNT" = 'yes' ] && zfs_log_end_msg 0

	return 0
}

do_start()
{
	check_module_loaded || exit 0

	# fix mtab to include already-mounted fs filesystems, in case there are any
	# we ONLY do this if mtab does not point to /proc/mounts
	# which is the case in some systems (systemd may bring that soon)
	if ! readlink /etc/mtab | grep -q /proc ; then
		if grep -qE "(^/dev/zd|^/dev/zvol| zfs )" /proc/mounts ; then
			zfs_action "Registering already-mounted ZFS filesystems and volumes" \
			    reregister_mounts
		fi
	fi

	# Ensure / exists in /etc/mtab, if not update mtab accordingly.
	# This should be handled by rc.sysinit but lets be paranoid.
	awk '$2 = "/" { exit 1 }' /etc/mtab
	RETVAL=$?
	if [ "$RETVAL" -eq 0 ]
	then
		mount -f /
	fi

	case "$ZFS_MOUNT" in
		[Oo][Ff][Ff]|[Nn][Oo]|''|0)
			exit 3
			;;
	esac

	do_mount
}

do_stop()
{
	case "$ZFS_UNMOUNT" in
		[Oo][Ff][Ff]|[Nn][Oo]|''|0)
			exit 0
			;;
	esac

	check_module_loaded || exit 0

	do_unmount
}

# ----------------------------------------------------

if [ ! -e /etc/gentoo-release ]
then
	case "$1" in
		start)
			do_start
			;;
		stop)
			do_stop
			;;
		force-reload|condrestart|reload|restart|status)
			# no-op
			;;
		*)
			[ -n "$1" ] && echo "Error: Unknown command $1."
			echo "Usage: $0 {start|stop}"
			exit 3
			;;
	esac

	exit $?
else
	# Create wrapper functions since Gentoo don't use the case part.
	depend() { do_depend; }
	start() { do_start; }
	stop() { do_stop; }
fi