4e8f0cc07caf2dfe520bc272ba4c40ef5fa5de5d
[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 # Should-Stop:
17 # Short-Description: Mount/umount the zfs filesystems
18 # Description: ZFS is an advanced filesystem designed to simplify managing
19 #              and protecting your data.  This service mounts the ZFS
20 #              filesystems and starts all related zfs services.
21 ### END INIT INFO
22
23 # Source function library.
24 . /lib/lsb/init-functions
25
26 # Source zfs configuration.
27 [ -f /etc/defaults/zfs ] &&  . /etc/defaults/zfs
28
29 RETVAL=0
30
31 LOCKFILE=/var/lock/zfs
32 CACHEFILE=/etc/zfs/zpool.cache
33 ZPOOL=/usr/sbin/zpool
34 ZFS=/usr/sbin/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         # Load the zfs module stack
52         /sbin/modprobe zfs
53
54         # Ensure / exists in /etc/mtab, if not update mtab accordingly.
55         # This should be handled by rc.sysinit but lets be paranoid.
56         awk '$2 == "/" { exit 1 }' /etc/mtab
57         RETVAL=$?
58         if [ $RETVAL -eq 0 ]; then
59                 /bin/mount -f /
60         fi
61
62         # Import all pools described by the cache file, and then mount
63         # all filesystem based on their properties.
64         if [ -f $CACHEFILE ] ; then
65                 log_begin_msg "Importing ZFS pools"
66                 $ZPOOL import -c $CACHEFILE -aN 2>/dev/null
67                 log_end_msg $?
68
69                 log_begin_msg "Mounting ZFS filesystems"
70                 $ZFS mount -a
71                 log_end_msg $?
72         fi
73         
74         touch $LOCKFILE
75 }
76
77 stop()
78 {
79         [ ! -f $LOCKFILE ] && return 3
80
81         log_begin_msg "Unmounting ZFS filesystems"
82         $ZFS umount -a
83         log_end_msg $?
84
85         rm -f $LOCKFILE
86 }
87
88 status()
89 {
90         [ ! -f $LOCKFILE ] && return 3
91
92         $ZPOOL status && echo && $ZPOOL list
93 }
94
95 case "$1" in
96         start)
97                 start
98                 RETVAL=$?
99                 ;;
100         stop)
101                 stop
102                 RETVAL=$?
103                 ;;
104         status)
105                 status
106                 RETVAL=$?
107                 ;;
108         restart)
109                 stop
110                 start
111                 ;;
112         condrestart)
113                 if [ -f $LOCKFILE ]; then
114                         stop
115                         start
116                 fi
117                 ;;
118         *)
119                 echo $"Usage: $0 {start|stop|status|restart|condrestart}"
120                 ;;
121 esac
122
123 exit $RETVAL