ae797c11a5c28d8aa3436c03eba81bdb9aca8e61
[zfs.git] / etc / init.d / zfs.redhat.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:
15 # Required-Stop:
16 # Should-Start:
17 # Should-Stop:
18 # Default-Start: 2 3 4 5
19 # Default-Stop: 1
20 # Short-Description: Mount/umount the zfs filesystems
21 # Description: ZFS is an advanced filesystem designed to simplify managing
22 #              and protecting your data.  This service mounts the ZFS
23 #              filesystems and starts all related zfs services.
24 ### END INIT INFO
25
26 export PATH=/usr/local/sbin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin
27
28 # Source function library & LSB routines
29 . /etc/rc.d/init.d/functions
30
31 # script variables
32 RETVAL=0
33 ZFS="@sbindir@/zfs"
34 ZPOOL="@sbindir@/zpool"
35 ZPOOL_CACHE="@sysconfdir@/zfs/zpool.cache"
36 servicename=zfs
37 LOCKFILE=/var/lock/subsys/$servicename
38
39 # functions
40 zfs_installed() {
41         modinfo zfs > /dev/null 2>&1 || return 5
42         $ZPOOL  > /dev/null 2>&1
43         [ $? == 127 ] && return 5
44         $ZFS    > /dev/null 2>&1
45         [ $? == 127 ] && return 5
46         return 0
47 }
48
49 # i need a bash guru to simplify this, since this is copy and paste, but donno how
50 # to correctly dereference variable names in bash, or how to do this right
51
52 # first parameter is a regular expression that filters fstab
53 read_fstab() {
54         unset FSTAB
55         n=0
56         while read -r fs mntpnt fstype opts blah ; do
57                 fs=`printf '%b\n' "$fs"`
58                 FSTAB[$n]=$fs
59                 let n++
60         done < <(egrep "$1" /etc/fstab)
61 }
62
63 start()
64 {
65         # Disable lockfile check
66         # if [ -f "$LOCKFILE" ] ; then return 0 ; fi
67
68         # check if ZFS is installed.  If not, comply to FC standards and bail
69         zfs_installed || {
70                 action $"Checking if ZFS is installed: not installed" /bin/false
71                 return 5
72         }
73
74         # Requires selinux policy which has not been written.
75         if [ -r "/selinux/enforce" ] &&
76            [ "$(cat /selinux/enforce)" = "1" ]; then
77                 action $"SELinux ZFS policy required: " /bin/false || return 6
78         fi
79
80         # Delay until all required block devices are present.
81         if [ -x /sbin/udevadm ]; then
82                 /sbin/udevadm settle
83         elif [ -x /sbin/udevsettle ]; then
84                 /sbin/udevsettle
85         fi
86
87         # load kernel module infrastructure
88         if ! grep -q zfs /proc/modules ; then
89                 action $"Loading kernel ZFS infrastructure: " modprobe zfs || return 5
90         fi
91         sleep 1
92
93         action $"Mounting automounted ZFS filesystems: " $ZFS mount -a || return 152
94
95         action $"Exporting ZFS filesystems: " $ZFS share -a || return 153
96
97         # Read fstab, try to mount zvols ignoring error
98         read_fstab "^/dev/(zd|zvol)"
99         template=$"Mounting volume %s registered in fstab: "
100         for volume in "${FSTAB[@]}" ; do
101                 string=`printf "$template" "$volume"`
102                 action "$string" mount "$volume" 2>/dev/null || /bin/true
103         done
104
105         # touch "$LOCKFILE"
106 }
107
108 stop()
109 {
110         # Disable lockfile check
111         # if [ ! -f "$LOCKFILE" ] ; then return 0 ; fi
112
113         # check if ZFS is installed.  If not, comply to FC standards and bail
114         zfs_installed || {
115                 action $"Checking if ZFS is installed: not installed" /bin/false
116                 return 5
117         }
118
119         # the poweroff of the system takes care of this
120         # but it never unmounts the root filesystem itself
121         # shit
122
123         action $"Syncing ZFS filesystems: " sync
124              # about the only thing we can do, and then we
125              # hope that the umount process will succeed
126              # unfortunately the umount process does not dismount
127              # the root file system, there ought to be some way
128              # we can tell zfs to just flush anything in memory
129              # when a request to remount,ro comes in
130
131         #echo -n $"Unmounting ZFS filesystems: "
132         #$ZFS umount -a
133         #RETVAL=$?
134         #if [ $RETVAL -ne 0 ]; then
135         #       failure
136
137         #       return 8
138         #fi
139         #success
140
141         rm -f "$LOCKFILE"
142 }
143
144 # See how we are called
145 case "$1" in
146         start)
147                 start
148                 RETVAL=$?
149                 ;;
150         stop)
151                 stop
152                 RETVAL=$?
153                 ;;
154         status)
155                 lsmod | grep -q zfs || RETVAL=3
156                 $ZPOOL status && echo && $ZFS list || {
157                         [ -f "$LOCKFILE" ] && RETVAL=2 || RETVAL=4
158                 }
159                 ;;
160         restart)
161                 stop
162                 start
163                 ;;
164         condrestart)
165                 if [ -f "$LOCKFILE" ] ; then
166                         stop
167                         start
168                 fi
169                 ;;
170         *)
171                 echo $"Usage: $0 {start|stop|status|restart|condrestart}"
172                 RETVAL=3
173                 ;;
174 esac
175
176 exit $RETVAL