Fix zfs.gentoo init script logic
[zfs.git] / etc / init.d / zfs.gentoo
1 #!/sbin/runscript
2 # Copyright 1999-2011 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
4 # $Header: /var/cvsroot/gentoo-x86/sys-fs/zfs/files/zfs,v 0.9 2011/04/30 10:13:43 devsk Exp $
5
6 depend()
7 {
8         before net
9         after udev
10         keyword -lxc -openvz -prefix -vserver
11 }
12
13 CACHEFILE=/etc/zfs/zpool.cache
14 ZPOOL=/usr/sbin/zpool
15 ZFS=/usr/sbin/zfs
16 ZFS_MODULE=zfs
17
18 checksystem() {
19         if [ -c /dev/zfs ]; then
20                 einfo "ZFS modules already loaded"
21                 return 0
22         else
23                 einfo "Checking if ZFS modules present"
24                 if [ "x$(modprobe -l  $ZFS_MODULE | grep $ZFS_MODULE)" == "x" ]; then
25                         eerror "$ZFS_MODULE not found. Is the ZFS package installed?"
26                         return 1
27                 fi
28         fi
29         einfo "Checking if zfs userspace tools present"
30                 if [ ! -x $ZPOOL ]; then
31                         eerror "$ZPOOL binary not found."
32                         return 1
33                 fi
34                 if [ ! -x $ZFS ]; then
35                         eerror "$ZFS binary not found."
36                         return 1
37                 fi
38         return 0
39 }
40
41 start() {
42         ebegin "Starting ZFS"
43         checksystem || return 1
44         if [ ! -c /dev/zfs ]; then
45                 modprobe $ZFS_MODULE
46                 rv=$?
47                 if [ $rv -ne 0 ]; then
48                         eerror "Failed to load the $ZFS_MODULE module, check 'dmesg|tail'."
49                         eend $rv
50                         return $rv
51                 fi
52         fi
53
54         # Import all pools described by the cache file, and then mount
55         # all filesystem based on their properties.
56         if [ -f $CACHEFILE ]; then
57                 einfo "Importing ZFS pools"
58                 # as per fedora script, import can fail if all pools are already imported
59                 # The check for $rv makes no sense...but someday, it will work right.
60                 $ZPOOL import -c $CACHEFILE -aN 2>/dev/null || true
61                 rv=$?
62                 if [ $rv -ne 0 ]; then
63                         eerror "Failed to import not-yet imported pools."
64                         eend $rv
65                         return $rv
66                 fi
67         fi
68
69         einfo "Mounting ZFS filesystems"
70         $ZFS mount -a
71         rv=$?
72         if [ $rv -ne 0 ]; then
73                 eerror "Failed to mount ZFS filesystems."
74                 eend $rv
75                 return $rv
76         fi
77
78         # hack to read mounted file systems because otherwise
79         # zfs returns EPERM when a non-root user reads a mounted filesystem before root did
80         savepwd="$PWD"
81         mount | grep " type zfs " | sed 's/.*on //' | sed 's/ type zfs.*$//' | \
82         while read line
83         do
84                 cd "$line" &> /dev/null
85                 ls &> /dev/null
86         done
87         cd "$savepwd"
88
89         eend 0
90         return 0
91 }
92
93 stop()
94 {
95         ebegin "Unmounting ZFS filesystems"
96         $ZFS umount -a
97         rv=$?
98         if [ $rv -ne 0 ]; then
99                 eerror "Failed to umount ZFS filesystems."
100         fi
101
102         eend $rv
103 }
104
105 status()
106 {
107         # show pool status and list
108         $ZPOOL status && echo && $ZPOOL list
109 }