Source /etc/default/zfs after setting defaults.
[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 start()
40 {
41         [ -f $LOCKFILE ] && return 3
42
43         # Requires selinux policy which has not been written.
44         if [ -r "/selinux/enforce" ] &&
45            [ "$(cat /selinux/enforce)" = "1" ]; then
46
47                 log_failure_msg "SELinux ZFS policy required"
48                 return 4
49         fi
50
51         # Delay until all required block devices are present.
52         udevadm settle
53
54         # Load the zfs module stack
55         /sbin/modprobe zfs
56
57         # Ensure / exists in /etc/mtab, if not update mtab accordingly.
58         # This should be handled by rc.sysinit but lets be paranoid.
59         awk '$2 == "/" { exit 1 }' /etc/mtab
60         RETVAL=$?
61         if [ $RETVAL -eq 0 ]; then
62                 /bin/mount -f /
63         fi
64
65         # Import all pools described by the cache file, and then mount
66         # all filesystem based on their properties.
67         if [ -f $ZPOOL_CACHE ] ; then
68                 log_begin_msg "Importing ZFS pools"
69                 $ZPOOL import -c $ZPOOL_CACHE -aN 2>/dev/null
70                 log_end_msg $?
71
72                 log_begin_msg "Mounting ZFS filesystems"
73                 $ZFS mount -a
74                 log_end_msg $?
75
76                 log_begin_msg "Exporting ZFS filesystems"
77                 $ZFS share -a
78                 log_end_msg $?
79         fi
80
81         touch $LOCKFILE
82 }
83
84 stop()
85 {
86         [ ! -f $LOCKFILE ] && return 3
87
88         log_begin_msg "Unmounting ZFS filesystems"
89         $ZFS umount -a
90         log_end_msg $?
91
92         rm -f $LOCKFILE
93 }
94
95 status()
96 {
97         [ ! -f $LOCKFILE ] && return 3
98
99         $ZPOOL status && echo && $ZPOOL list
100 }
101
102 case "$1" in
103         start)
104                 start
105                 RETVAL=$?
106                 ;;
107         stop)
108                 stop
109                 RETVAL=$?
110                 ;;
111         status)
112                 status
113                 RETVAL=$?
114                 ;;
115         restart)
116                 stop
117                 start
118                 ;;
119         condrestart)
120                 if [ -f $LOCKFILE ]; then
121                         stop
122                         start
123                 fi
124                 ;;
125         *)
126                 echo $"Usage: $0 {start|stop|status|restart|condrestart}"
127                 ;;
128 esac
129
130 exit $RETVAL