Improve OpenRC init script
[zfs.git] / etc / init.d / zfs.gentoo.in
1 #!/sbin/runscript
2 # Copyright 1999-2011 Gentoo Foundation
3 # Released under the 2-clause BSD license.
4 # $Header: /var/cvsroot/gentoo-x86/sys-fs/zfs/files/zfs,v 0.9 2011/04/30 10:13:43 devsk Exp $
5
6 if [ -z "$init" ]; then
7     # Not interactive
8     grep -Eqi 'zfs=off|zfs=no' /proc/cmdline && exit 3
9 fi
10
11 depend()
12 {
13         # Try to allow people to mix and match fstab with ZFS in a way that makes sense.
14         if [ "$(mountinfo -s /)" = 'zfs' ]
15         then
16                 before localmount
17         else
18                 after localmount
19         fi
20
21         # bootmisc will log to /var which may be a different zfs than root.
22         before bootmisc logger
23         keyword -lxc -openvz -prefix -vserver
24 }
25
26 ZFS="@sbindir@/zfs"
27 ZPOOL="@sbindir@/zpool"
28 ZPOOL_CACHE="@sysconfdir@/zfs/zpool.cache"
29 ZFS_MODULE=zfs
30
31 checksystem() {
32         if [ ! -c /dev/zfs ]; then
33                 einfo "Checking if ZFS modules present"
34                 if ! modinfo zfs > /dev/null 2>&1 ; then
35                         eerror "$ZFS_MODULE not found. Is the ZFS package installed?"
36                         return 1
37                 fi
38         fi
39         einfo "Checking if zfs userspace tools present"
40                 if [ ! -x $ZPOOL ]; then
41                         eerror "$ZPOOL binary not found."
42                         return 1
43                 fi
44                 if [ ! -x $ZFS ]; then
45                         eerror "$ZFS binary not found."
46                         return 1
47                 fi
48         return 0
49 }
50
51 start() {
52         ebegin "Starting ZFS"
53         checksystem || return 1
54
55         # Delay until all required block devices are present.
56         udevadm settle
57
58         if [ ! -c /dev/zfs ]; then
59                 modprobe $ZFS_MODULE
60                 rv=$?
61                 if [ $rv -ne 0 ]; 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         # Import all pools described by the cache file, and then mount
69         # all filesystem based on their properties.
70         if [ -f $ZPOOL_CACHE ]; then
71                 einfo "Importing ZFS pools"
72                 # as per fedora script, import can fail if all pools are already imported
73                 # The check for $rv makes no sense...but someday, it will work right.
74                 $ZPOOL import -c $ZPOOL_CACHE -aN 2>/dev/null || true
75                 rv=$?
76                 if [ $rv -ne 0 ]; then
77                         eerror "Failed to import not-yet imported pools."
78                         eend $rv
79                         return $rv
80                 fi
81         fi
82
83         einfo "Mounting ZFS filesystems"
84         $ZFS mount -a
85         rv=$?
86         if [ $rv -ne 0 ]; then
87                 eerror "Failed to mount ZFS filesystems."
88                 eend $rv
89                 return $rv
90         fi
91
92         einfo "Exporting ZFS filesystems"
93         $ZFS share -a
94         rv=$?
95         if [ $rv -ne 0 ]; then
96                 eerror "Failed to export ZFS filesystems."
97                 eend $rv
98                 return $rv
99         fi
100
101         eend 0
102         return 0
103 }
104
105 stop()
106 {
107         ebegin "Unmounting ZFS filesystems"
108         $ZFS umount -a
109         rv=$?
110         if [ $rv -ne 0 ]; then
111                 einfo "Some ZFS filesystems not unmounted"
112         fi
113
114         # Don't fail if we couldn't umount everything.  /usr might be in use.
115         eend 0
116         return 0
117 }
118
119 status()
120 {
121         # show pool status and list
122         $ZPOOL status && echo && $ZPOOL list
123 }