Add Gentoo/Lunar/Redhat Init Scripts
[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 }
11
12 CACHEFILE=/etc/zfs/zpool.cache
13 ZPOOL=/usr/sbin/zpool
14 ZFS=/usr/sbin/zfs
15 ZFS_MODULE=zfs
16 LOCKFILE=/var/lock/zfs/zfs_lockfile
17
18 checksystem()
19 {
20         /sbin/modinfo $ZFS_MODULE &>/dev/null
21         if [[ $? -ne 0 ]]
22         then
23                 eerror "$ZFS_MODULE not found. Is the ZFS package installed?"
24                 return 1
25         fi
26         if [[ ! -x $ZPOOL ]]
27         then
28                 eerror "$ZPOOL binary not found."
29                 return 1
30         fi
31         if [[ ! -x $ZFS ]]
32         then
33                 eerror "$ZFS binary not found."
34                 return 1
35         fi
36
37         # create the lockdir if not there
38         lockdir=$(dirname ${LOCKFILE})
39         if [[ ! -d ${lockdir} ]]
40         then
41                 mkdir -p ${lockdir} &>/dev/null
42         fi
43         return 0
44 }
45
46 start()
47 {
48         if [[ -f $LOCKFILE ]]
49         then
50                 einfo "ZFS already running, please stop it first. Delete $LOCKFILE if its not so."
51                 eend 3
52                 return 3
53         fi
54         ebegin "Starting ZFS"
55         checksystem || return 1
56         if ! grep -q $ZFS_MODULE /proc/modules
57         then
58                 /sbin/modprobe $ZFS_MODULE &>/dev/null
59                 rv=$?
60                 if [[ $rv -ne 0 ]]
61                 then
62                         eerror "Failed to load the $ZFS_MODULE module, check 'dmesg|tail'."
63                         eend $rv
64                         return $rv
65                 fi
66         fi
67
68         # Ensure / exists in /etc/mtab, if not update mtab accordingly.
69         # This should be handled by rc.sysinit but lets be paranoid.
70         awk '$2 == "/" { exit 1 }' /etc/mtab
71         RETVAL=$?
72         if [[ $RETVAL -eq 0 ]]
73         then
74                 /bin/mount -f /
75         fi
76
77         # Import all pools described by the cache file, and then mount
78         # all filesystem based on their properties.
79         if [[ -f $CACHEFILE ]]
80         then
81                 einfo "Importing ZFS pools"
82
83                 # as per fedora script, import can fail if all pools are already imported
84                 # The check for $rv makes no sense...but someday, it will work right.
85                 $ZPOOL import -c $CACHEFILE -aN 2>/dev/null || true
86                 rv=$?
87                 if [[ $rv -ne 0 ]]
88                 then
89                         eerror "Failed to import not-yet imported pools."
90                         eend $rv
91                         return $rv
92                 fi
93         fi
94
95         einfo "Mounting ZFS filesystems"
96         $ZFS mount -a
97         rv=$?
98         if [[ $rv -ne 0 ]]
99         then
100                 eerror "Failed to mount ZFS filesystems."
101                 eend $rv
102                 return $rv
103         fi
104
105         # hack to read mounted file systems because otherwise
106         # zfs returns EPERM when a non-root user reads a mounted filesystem before root did
107         savepwd="$PWD"
108         mount | grep " type zfs " | sed 's/.*on //' | sed 's/ type zfs.*$//' | \
109         while read line
110         do
111                 cd "$line" &> /dev/null
112                 ls &> /dev/null
113         done
114         cd "$savepwd"
115
116         touch $LOCKFILE
117         eend 0
118         return 0
119 }
120
121 stop()
122 {
123         if [[ ! -f $LOCKFILE ]]
124         then
125                 einfo "ZFS is not started, remove $LOCKFILE if its not so."
126                 eend 3
127                 return 3
128         fi
129         ebegin "Unmounting ZFS filesystems"
130         sync
131         $ZFS umount -a
132         if [[ $rv -ne 0 ]]
133         then
134                 eerror "Failed to umount ZFS filesystems."
135         fi
136         rm -f $LOCKFILE
137         eend $rv
138 }
139
140 status()
141 {
142         if [[ ! -f $LOCKFILE ]]
143         then
144                 einfo "ZFS is not started, remove $LOCKFILE if its not so."
145                 eend 3
146                 return 3
147         fi
148
149         # show pool status and list
150         $ZPOOL status && echo && $ZPOOL list
151 }