d43a5a06e8c0d7a5f886fb2d8c4648591b34d606
[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 localmount
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
45         # Delay until all required block devices are present.
46         udevadm settle
47
48         if [ ! -c /dev/zfs ]; then
49                 modprobe $ZFS_MODULE
50                 rv=$?
51                 if [ $rv -ne 0 ]; then
52                         eerror "Failed to load the $ZFS_MODULE module, check 'dmesg|tail'."
53                         eend $rv
54                         return $rv
55                 fi
56         fi
57
58         # Import all pools described by the cache file, and then mount
59         # all filesystem based on their properties.
60         if [ -f $CACHEFILE ]; then
61                 einfo "Importing ZFS pools"
62                 # as per fedora script, import can fail if all pools are already imported
63                 # The check for $rv makes no sense...but someday, it will work right.
64                 $ZPOOL import -c $CACHEFILE -aN 2>/dev/null || true
65                 rv=$?
66                 if [ $rv -ne 0 ]; then
67                         eerror "Failed to import not-yet imported pools."
68                         eend $rv
69                         return $rv
70                 fi
71         fi
72
73         einfo "Mounting ZFS filesystems"
74         $ZFS mount -a
75         rv=$?
76         if [ $rv -ne 0 ]; then
77                 eerror "Failed to mount ZFS filesystems."
78                 eend $rv
79                 return $rv
80         fi
81
82         eend 0
83         return 0
84 }
85
86 stop()
87 {
88         ebegin "Unmounting ZFS filesystems"
89         $ZFS umount -a
90         rv=$?
91         if [ $rv -ne 0 ]; then
92                 eerror "Failed to umount ZFS filesystems."
93         fi
94
95         eend $rv
96 }
97
98 status()
99 {
100         # show pool status and list
101         $ZPOOL status && echo && $ZPOOL list
102 }