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