Possibility to disable (not start) zfs at bootup.
[zfs.git] / etc / init.d / zfs.lsb.in
1 #!/bin/bash
2 #
3 # zfs           This script will mount/umount the zfs filesystems.
4 #
5 # chkconfig:    2345 01 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 #
12 ### BEGIN INIT INFO
13 # Provides: zfs
14 # Required-Start: $local_fs
15 # Required-Stop: $local_fs
16 # Default-Start:     2 3 4 5
17 # Default-Stop:      0 1 6
18 # Should-Stop:
19 # Short-Description: Mount/umount the zfs filesystems
20 # Description: ZFS is an advanced filesystem designed to simplify managing
21 #              and protecting your data.  This service mounts the ZFS
22 #              filesystems and starts all related zfs services.
23 ### END INIT INFO
24
25 # Source function library.
26 . /lib/lsb/init-functions
27
28 LOCKFILE=/var/lock/zfs
29 ZFS="@sbindir@/zfs"
30 ZPOOL="@sbindir@/zpool"
31 ZPOOL_CACHE="@sysconfdir@/zfs/zpool.cache"
32
33 # Source zfs configuration.
34 [ -r '/etc/default/zfs' ] &&  . /etc/default/zfs
35
36 [ -x "$ZPOOL" ] || exit 1
37 [ -x "$ZFS" ] || exit 2
38
39 if [ -z "$init" ]; then
40     # Not interactive
41     grep -Eqi 'zfs=off|zfs=no' /proc/cmdline && exit 3
42 fi
43
44 start()
45 {
46         [ -f "$LOCKFILE" ] && return 3
47
48         # Requires selinux policy which has not been written.
49         if [ -r "/selinux/enforce" ] &&
50            [ "$(cat /selinux/enforce)" = "1" ]; then
51
52                 log_failure_msg "SELinux ZFS policy required"
53                 return 4
54         fi
55
56         # Delay until all required block devices are present.
57         udevadm settle
58
59         # Load the zfs module stack
60         /sbin/modprobe zfs
61
62         # Ensure / exists in /etc/mtab, if not update mtab accordingly.
63         # This should be handled by rc.sysinit but lets be paranoid.
64         awk '$2 == "/" { exit 1 }' /etc/mtab
65         RETVAL=$?
66         if [ "$RETVAL" -eq 0 ]; then
67                 /bin/mount -f /
68         fi
69
70         # Import all pools described by the cache file, and then mount
71         # all filesystem based on their properties.
72         if [ -f "$ZPOOL_CACHE" ] ; then
73                 log_begin_msg "Importing ZFS pools"
74                 "$ZPOOL" import -c "$ZPOOL_CACHE" -aN 2>/dev/null
75                 log_end_msg $?
76
77                 log_begin_msg "Mounting ZFS filesystems"
78                 "$ZFS" mount -a
79                 log_end_msg $?
80
81                 log_begin_msg "Exporting ZFS filesystems"
82                 "$ZFS" share -a
83                 log_end_msg $?
84         fi
85
86         touch "$LOCKFILE"
87 }
88
89 stop()
90 {
91         [ ! -f "$LOCKFILE" ] && return 3
92
93         log_begin_msg "Unmounting ZFS filesystems"
94         "$ZFS" umount -a
95         log_end_msg $?
96
97         rm -f "$LOCKFILE"
98 }
99
100 status()
101 {
102         [ ! -f "$LOCKFILE" ] && return 3
103
104         "$ZPOOL" status && echo "" && "$ZPOOL" list
105 }
106
107 case "$1" in
108         start)
109                 start
110                 RETVAL=$?
111                 ;;
112         stop)
113                 stop
114                 RETVAL=$?
115                 ;;
116         status)
117                 status
118                 RETVAL=$?
119                 ;;
120         restart)
121                 stop
122                 start
123                 ;;
124         condrestart)
125                 if [ -f "$LOCKFILE" ]; then
126                         stop
127                         start
128                 fi
129                 ;;
130         *)
131                 echo $"Usage: $0 {start|stop|status|restart|condrestart}"
132                 ;;
133 esac
134
135 exit $RETVAL