5b8671e0a399ebcec894901a7933ac364e7b8d2e
[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         # bootmisc will log to /var which may be a different zfs than root.
14         before net bootmisc
15         after udev localmount
16         keyword -lxc -openvz -prefix -vserver
17 }
18
19 ZFS="@sbindir@/zfs"
20 ZPOOL="@sbindir@/zpool"
21 ZPOOL_CACHE="@sysconfdir@/zfs/zpool.cache"
22 ZFS_MODULE=zfs
23
24 checksystem() {
25         if [ ! -c /dev/zfs ]; then
26                 einfo "Checking if ZFS modules present"
27                 if ! modinfo zfs > /dev/null 2>&1 ; then
28                         eerror "$ZFS_MODULE not found. Is the ZFS package installed?"
29                         return 1
30                 fi
31         fi
32         einfo "Checking if zfs userspace tools present"
33                 if [ ! -x $ZPOOL ]; then
34                         eerror "$ZPOOL binary not found."
35                         return 1
36                 fi
37                 if [ ! -x $ZFS ]; then
38                         eerror "$ZFS binary not found."
39                         return 1
40                 fi
41         return 0
42 }
43
44 start() {
45         ebegin "Starting ZFS"
46         checksystem || return 1
47
48         # Delay until all required block devices are present.
49         udevadm settle
50
51         if [ ! -c /dev/zfs ]; then
52                 modprobe $ZFS_MODULE
53                 rv=$?
54                 if [ $rv -ne 0 ]; then
55                         eerror "Failed to load the $ZFS_MODULE module, check 'dmesg|tail'."
56                         eend $rv
57                         return $rv
58                 fi
59         fi
60
61         # Import all pools described by the cache file, and then mount
62         # all filesystem based on their properties.
63         if [ -f $ZPOOL_CACHE ]; then
64                 einfo "Importing ZFS pools"
65                 # as per fedora script, import can fail if all pools are already imported
66                 # The check for $rv makes no sense...but someday, it will work right.
67                 $ZPOOL import -c $ZPOOL_CACHE -aN 2>/dev/null || true
68                 rv=$?
69                 if [ $rv -ne 0 ]; then
70                         eerror "Failed to import not-yet imported pools."
71                         eend $rv
72                         return $rv
73                 fi
74         fi
75
76         einfo "Mounting ZFS filesystems"
77         $ZFS mount -a
78         rv=$?
79         if [ $rv -ne 0 ]; then
80                 eerror "Failed to mount ZFS filesystems."
81                 eend $rv
82                 return $rv
83         fi
84
85         einfo "Exporting ZFS filesystems"
86         $ZFS share -a
87         rv=$?
88         if [ $rv -ne 0 ]; then
89                 eerror "Failed to export ZFS filesystems."
90                 eend $rv
91                 return $rv
92         fi
93
94         eend 0
95         return 0
96 }
97
98 stop()
99 {
100         ebegin "Unmounting ZFS filesystems"
101         $ZFS umount -a
102         rv=$?
103         if [ $rv -ne 0 ]; then
104                 einfo "Some ZFS filesystems not unmounted"
105         fi
106
107         # Don't fail if we couldn't umount everything.  /usr might be in use.
108         eend 0
109         return 0
110 }
111
112 status()
113 {
114         # show pool status and list
115         $ZPOOL status && echo && $ZPOOL list
116 }