]> git.proxmox.com Git - mirror_zfs-debian.git/blob - etc/init.d/zfs.fedora.in
Imported Upstream version 0.6.4.2
[mirror_zfs-debian.git] / etc / init.d / zfs.fedora.in
1 #!/bin/bash
2 #
3 # zfs This script will mount/umount the zfs filesystems.
4 #
5 # chkconfig: 2345 01 99
6 # description: This script will mount/umount the zfs filesystems during
7 # system boot/shutdown. Configuration of which filesystems
8 # should be mounted is handled by the zfs 'mountpoint' and
9 # 'canmount' properties. See the zfs(8) man page for details.
10 # It is also responsible for all userspace zfs services.
11 #
12 ### BEGIN INIT INFO
13 # Provides: zfs
14 # Required-Start:
15 # Required-Stop:
16 # Should-Start:
17 # Should-Stop:
18 # Default-Start: 2 3 4 5
19 # Default-Stop: 1
20 # Short-Description: Mount/umount the zfs filesystems
21 # Description: ZFS is an advanced filesystem designed to simplify managing
22 # and protecting your data. This service mounts the ZFS
23 # filesystems and starts all related zfs services.
24 ### END INIT INFO
25
26 export PATH=/usr/local/sbin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin
27
28 if [ -z "$init" ]; then
29 # Not interactive
30 grep -qE '(^|[^\\](\\\\)* )zfs=(off|no)( |$)' /proc/cmdline && exit 3
31 fi
32
33 # Source function library & LSB routines
34 . /etc/rc.d/init.d/functions
35
36 # script variables
37 RETVAL=0
38 ZFS="@sbindir@/zfs"
39 ZPOOL="@sbindir@/zpool"
40 ZPOOL_CACHE="@sysconfdir@/zfs/zpool.cache"
41 servicename=zfs
42 LOCKFILE=/var/lock/subsys/$servicename
43
44 # functions
45 zfs_installed() {
46 modinfo zfs > /dev/null 2>&1 || return 5
47 $ZPOOL > /dev/null 2>&1
48 [ $? == 127 ] && return 5
49 $ZFS > /dev/null 2>&1
50 [ $? == 127 ] && return 5
51 return 0
52 }
53
54 reregister_mounts() {
55 cat /etc/mtab | while read -r fs mntpnt fstype opts rest ; do
56 fs=`printf '%b\n' "$fs"`
57 mntpnt=`printf '%b\n' "$mntpnt"`
58 if [ "$fstype" == "zfs" ] ; then
59 if [ "$mntpnt" == "/" ] ; then
60 mount -f -o zfsutil -t zfs --move / /removethismountpointhoweverpossible
61 umount --fake /removethismountpointhoweverpossible
62 else
63 umount --fake "$mntpnt"
64 fi
65 elif echo "$fs" | grep -q "^/dev/zd" ; then
66 if [ "$mntpnt" == "/" ] ; then
67 mount -f -t "$fstype" --move / /removethismountpointhoweverpossible
68 umount --fake /removethismountpointhoweverpossible
69 else
70 umount --fake "$mntpnt"
71 fi
72 fi
73 done
74 cat /proc/mounts | while read -r fs mntpnt fstype opts rest ; do
75 fs=`printf '%b\n' "$fs"`
76 mntpnt=`printf '%b\n' "$mntpnt"`
77 if [ "$fstype" == "zfs" ] ; then
78 mount -f -t zfs -o zfsutil "$fs" "$mntpnt"
79 elif echo "$fs" | grep -q "^/dev/zd" ; then
80 mount -f -t "$fstype" -o "$opts" "$fs" "$mntpnt"
81 fi
82 done
83 }
84
85 # i need a bash guru to simplify this, since this is copy and paste, but donno how
86 # to correctly dereference variable names in bash, or how to do this right
87
88 declare -A MTAB
89 declare -A FSTAB
90
91 # first parameter is a regular expression that filters mtab
92 read_mtab() {
93 for fs in "${!MTAB[@]}" ; do unset MTAB["$fs"] ; done
94 while read -r fs mntpnt fstype opts blah ; do
95 fs=`printf '%b\n' "$fs"`
96 MTAB["$fs"]=$mntpnt
97 done < <(grep "$1" /etc/mtab)
98 }
99
100 in_mtab() {
101 [ "${MTAB[$1]}" != "" ]
102 return $?
103 }
104
105 # first parameter is a regular expression that filters fstab
106 read_fstab() {
107 for fs in "${!FSTAB[@]}" ; do unset FSTAB["$fs"] ; done
108 while read -r fs mntpnt fstype opts blah ; do
109 fs=`printf '%b\n' "$fs"`
110 FSTAB["$fs"]=$mntpnt
111 done < <(grep "$1" /etc/fstab)
112 }
113
114 in_fstab() {
115 [ "${FSTAB[$1]}" != "" ]
116 return $?
117 }
118
119 start()
120 {
121 if [ -f "$LOCKFILE" ] ; then return 0 ; fi
122
123 # check if ZFS is installed. If not, comply to FC standards and bail
124 zfs_installed || {
125 action $"Checking if ZFS is installed: not installed" /bin/false
126 return 5
127 }
128
129 # Delay until all required block devices are present.
130 udevadm settle
131
132 # load kernel module infrastructure
133 if ! grep -q zfs /proc/modules ; then
134 action $"Loading kernel ZFS infrastructure: " modprobe zfs || return 5
135 fi
136
137 # fix mtab to include already-mounted fs filesystems, in case there are any
138 # we ONLY do this if mtab does not point to /proc/mounts
139 # which is the case in some systems (systemd may bring that soon)
140 if ! readlink /etc/mtab | grep -q /proc ; then
141 if grep -qE "(^/dev/zd| zfs )" /proc/mounts ; then
142 action $"Registering already-mounted ZFS filesystems and volumes: " reregister_mounts || return 150
143 fi
144 fi
145
146 if [ -f $ZPOOL_CACHE ] ; then
147
148 echo -n $"Importing ZFS pools not yet imported: "
149 $ZPOOL import -c $ZPOOL_CACHE -aN || true # stupid zpool will fail if all pools are already imported
150 RETVAL=$?
151 if [ $RETVAL -ne 0 ]; then
152 failure "Importing ZFS pools not yet imported: "
153 return 151
154 fi
155 success "Importing ZFS pools not yet imported: "
156
157 fi
158
159 action $"Mounting ZFS filesystems not yet mounted: " $ZFS mount -a || return 152
160
161 action $"Exporting ZFS filesystems: " $ZFS share -a || return 153
162
163 read_mtab "^/dev/zd"
164 read_fstab "^/dev/zd"
165
166 template=$"Mounting volume %s registered in fstab: "
167 for volume in "${!FSTAB[@]}" ; do
168 if in_mtab "$volume" ; then continue ; fi
169 string=`printf "$template" "$volume"`
170 action "$string" mount "$volume"
171 done
172
173 touch "$LOCKFILE"
174 }
175
176 stop()
177 {
178 if [ ! -f "$LOCKFILE" ] ; then return 0 ; fi
179
180 # check if ZFS is installed. If not, comply to FC standards and bail
181 zfs_installed || {
182 action $"Checking if ZFS is installed: not installed" /bin/false
183 return 5
184 }
185
186 # the poweroff of the system takes care of this
187 # but it never unmounts the root filesystem itself
188 # shit
189
190 action $"Syncing ZFS filesystems: " sync
191 # about the only thing we can do, and then we
192 # hope that the umount process will succeed
193 # unfortunately the umount process does not dismount
194 # the root file system, there ought to be some way
195 # we can tell zfs to just flush anything in memory
196 # when a request to remount,ro comes in
197
198 #echo -n $"Unmounting ZFS filesystems: "
199 #$ZFS umount -a
200 #RETVAL=$?
201 #if [ $RETVAL -ne 0 ]; then
202 # failure
203
204 # return 8
205 #fi
206 #success
207
208 rm -f "$LOCKFILE"
209 }
210
211 # See how we are called
212 case "$1" in
213 start)
214 start
215 RETVAL=$?
216 ;;
217 stop)
218 stop
219 RETVAL=$?
220 ;;
221 status)
222 lsmod | grep -q zfs || RETVAL=3
223 $ZPOOL status && echo && $ZFS list || {
224 [ -f "$LOCKFILE" ] && RETVAL=2 || RETVAL=4
225 }
226 ;;
227 restart)
228 stop
229 start
230 ;;
231 condrestart)
232 if [ -f "$LOCKFILE" ] ; then
233 stop
234 start
235 fi
236 ;;
237 *)
238 echo $"Usage: $0 {start|stop|status|restart|condrestart}"
239 RETVAL=3
240 ;;
241 esac
242
243 exit $RETVAL