Fix autoconf variable substitution in init scripts.
[zfs.git] / etc / init.d / zfs.fedora.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="@initdir@/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 reregister_mounts() {
50         cat /etc/mtab | while read -r fs mntpnt fstype opts rest ; do
51                 fs=`printf '%b\n' "$fs"`
52                 mntpnt=`printf '%b\n' "$mntpnt"`
53                 if [ "$fstype" == "zfs" ] ; then
54                         if [ "$mntpnt" == "/" ] ; then
55                                 mount -f -o zfsutil -t zfs --move / /removethismountpointhoweverpossible
56                                 umount --fake /removethismountpointhoweverpossible
57                         else
58                                 umount --fake "$mntpnt"
59                         fi
60                 elif echo "$fs" | grep -q "^/dev/zd" ; then
61                         if [ "$mntpnt" == "/" ] ; then
62                                 mount -f -t "$fstype" --move / /removethismountpointhoweverpossible
63                                 umount --fake /removethismountpointhoweverpossible
64                         else
65                                 umount --fake "$mntpnt"
66                         fi
67                 fi
68         done
69         cat /proc/mounts | while read -r fs mntpnt fstype opts rest ; do
70                 fs=`printf '%b\n' "$fs"`
71                 mntpnt=`printf '%b\n' "$mntpnt"`
72                 if [ "$fstype" == "zfs" ] ; then
73                         mount -f -t zfs -o zfsutil "$fs" "$mntpnt"
74                 elif echo "$fs" | grep -q "^/dev/zd" ; then
75                         mount -f -t "$fstype" -o "$opts" "$fs" "$mntpnt"
76                 fi
77         done
78 }
79
80 # i need a bash guru to simplify this, since this is copy and paste, but donno how
81 # to correctly dereference variable names in bash, or how to do this right
82
83 declare -A MTAB
84 declare -A FSTAB
85
86 # first parameter is a regular expression that filters mtab
87 read_mtab() {
88         for fs in "${!MTAB[@]}" ; do unset MTAB["$fs"] ; done
89         while read -r fs mntpnt fstype opts blah ; do
90                 fs=`printf '%b\n' "$fs"`
91                 MTAB["$fs"]=$mntpnt
92         done < <(grep "$1" /etc/mtab)
93 }
94
95 in_mtab() {
96         [ "${MTAB[$1]}" != "" ]
97         return $?
98 }
99
100 # first parameter is a regular expression that filters fstab
101 read_fstab() {
102         for fs in "${!FSTAB[@]}" ; do unset FSTAB["$fs"] ; done
103         while read -r fs mntpnt fstype opts blah ; do
104                 fs=`printf '%b\n' "$fs"`
105                 FSTAB["$fs"]=$mntpnt
106         done < <(grep "$1" /etc/fstab)
107 }
108
109 in_fstab() {
110         [ "${FSTAB[$1]}" != "" ]
111         return $?
112 }
113
114 start()
115 {
116         if [ -f "$LOCKFILE" ] ; then return 0 ; fi
117
118         # check if ZFS is installed.  If not, comply to FC standards and bail
119         zfs_installed || {
120                 action $"Checking if ZFS is installed: not installed" /bin/false
121                 return 5
122         }
123
124         # Requires selinux policy which has not been written.
125         if [ -r "/selinux/enforce" ] &&
126            [ "$(cat /selinux/enforce)" = "1" ]; then
127                 action $"SELinux ZFS policy required: " /bin/false || return 6
128         fi
129
130         # Delay until all required block devices are present.
131         udevadm settle
132
133         # load kernel module infrastructure
134         if ! grep -q zfs /proc/modules ; then
135                 action $"Loading kernel ZFS infrastructure: " modprobe zfs || return 5
136         fi
137
138         # fix mtab to include already-mounted fs filesystems, in case there are any
139         # we ONLY do this if mtab does not point to /proc/mounts
140         # which is the case in some systems (systemd may bring that soon)
141         if ! readlink /etc/mtab | grep -q /proc ; then
142                 if grep -qE "(^/dev/zd| zfs )" /proc/mounts ; then
143                         action $"Registering already-mounted ZFS filesystems and volumes: " reregister_mounts || return 150
144                 fi
145         fi
146
147         if [ -f $ZPOOL_CACHE ] ; then
148
149                 echo -n $"Importing ZFS pools not yet imported: "
150                 $ZPOOL import -c $ZPOOL_CACHE -aN || true # stupid zpool will fail if all pools are already imported
151                 RETVAL=$?
152                 if [ $RETVAL -ne 0 ]; then
153                         failure "Importing ZFS pools not yet imported: "
154                         return 151
155                 fi
156                 success "Importing ZFS pools not yet imported: "
157
158         fi
159
160         action $"Mounting ZFS filesystems not yet mounted: " $ZFS mount -a || return 152
161
162         action $"Exporting ZFS filesystems: " $ZFS share -a || return 153
163
164         read_mtab  "^/dev/zd"
165         read_fstab "^/dev/zd"
166
167         template=$"Mounting volume %s registered in fstab: "
168         for volume in "${!FSTAB[@]}" ; do
169                 if in_mtab "$volume" ; then continue ; fi
170                 string=`printf "$template" "$volume"`
171                 action "$string" mount "$volume"
172         done
173
174         touch "$LOCKFILE"
175 }
176
177 stop()
178 {
179         if [ ! -f "$LOCKFILE" ] ; then return 0 ; fi
180
181         # check if ZFS is installed.  If not, comply to FC standards and bail
182         zfs_installed || {
183                 action $"Checking if ZFS is installed: not installed" /bin/false
184                 return 5
185         }
186
187         # the poweroff of the system takes care of this
188         # but it never unmounts the root filesystem itself
189         # shit
190
191         action $"Syncing ZFS filesystems: " sync
192              # about the only thing we can do, and then we
193              # hope that the umount process will succeed
194              # unfortunately the umount process does not dismount
195              # the root file system, there ought to be some way
196              # we can tell zfs to just flush anything in memory
197              # when a request to remount,ro comes in
198
199         #echo -n $"Unmounting ZFS filesystems: "
200         #$ZFS umount -a
201         #RETVAL=$?
202         #if [ $RETVAL -ne 0 ]; then
203         #       failure
204
205         #       return 8
206         #fi
207         #success
208
209         rm -f "$LOCKFILE"
210 }
211
212 # See how we are called
213 case "$1" in
214         start)
215                 start
216                 RETVAL=$?
217                 ;;
218         stop)
219                 stop
220                 RETVAL=$?
221                 ;;
222         status)
223                 lsmod | grep -q zfs || RETVAL=3
224                 $ZPOOL status && echo && $ZFS list || {
225                         [ -f "$LOCKFILE" ] && RETVAL=2 || RETVAL=4
226                 }
227                 ;;
228         restart)
229                 stop
230                 start
231                 ;;
232         condrestart)
233                 if [ -f "$LOCKFILE" ] ; then
234                         stop
235                         start
236                 fi
237                 ;;
238         *)
239                 echo $"Usage: $0 {start|stop|status|restart|condrestart}"
240                 RETVAL=3
241                 ;;
242 esac
243
244 exit $RETVAL