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