Avoid small buffer copying on write
[zfs.git] / etc / init.d / zfs-mount.in
blob000619b6717cf74c764fefe2815c11f70a4bc9ce
1 #!@DEFAULT_INIT_SHELL@
3 # zfs-mount     This script will mount/umount the zfs filesystems.
5 # chkconfig:    2345 06 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 # probe: true
13 ### BEGIN INIT INFO
14 # Provides:          zfs-mount
15 # Required-Start:    $local_fs zfs-import
16 # Required-Stop:     $local_fs zfs-import
17 # Default-Start:     2 3 4 5
18 # Default-Stop:      0 1 6
19 # X-Stop-After:      zfs-zed
20 # Short-Description: Mount ZFS filesystems and volumes
21 # Description: Run the `zfs mount -a` or `zfs umount -a` commands.
22 ### END INIT INFO
24 # Released under the 2-clause BSD license.
26 # This script is based on debian/zfsutils.zfs.init from the
27 # Debian GNU/kFreeBSD zfsutils 8.1-3 package, written by Aurelien Jarno.
29 # Source the common init script
30 . @sysconfdir@/zfs/zfs-functions
32 # ----------------------------------------------------
34 chkroot() {
35         while read -r _ mp _; do
36                 if [ "$mp" = "/" ]; then
37                         return 0
38                 fi
39         done < /proc/self/mounts
41         return 1
44 do_depend()
46         # Try to allow people to mix and match fstab with ZFS in a way that makes sense.
47         if [ "$(mountinfo -s /)" = 'zfs' ]
48         then
49                 before localmount
50         else
51                 after localmount
52         fi
54         # bootmisc will log to /var which may be a different zfs than root.
55         before bootmisc logger
57         after zfs-import sysfs
58         use mtab
59         keyword -lxc -openvz -prefix -vserver
62 # Mount all datasets/filesystems
63 do_mount()
65         local verbose overlay i mntpt
67         check_boolean "$VERBOSE_MOUNT" && verbose=v
68         check_boolean "$DO_OVERLAY_MOUNTS" && overlay=O
70         zfs_action "Mounting ZFS filesystem(s)" \
71             "$ZFS" mount -a$verbose$overlay "$MOUNT_EXTRA_OPTIONS"
73         # Require each volume/filesystem to have 'noauto' and no fsck
74         # option. This shouldn't really be necessary, as long as one
75         # can get zfs-import to run sufficiently early on in the boot
76         # process - before local mounts. This is just here in case/if
77         # this isn't possible.
78         check_boolean "$VERBOSE_MOUNT" && \
79             zfs_log_begin_msg "Mounting volumes and filesystems registered in fstab"
81         read_mtab  "^/dev/(zd|zvol)"
82         read_fstab "^/dev/(zd|zvol)"
83         i=0; var="FSTAB_0"
84         while [ -n "$(eval echo "\$$var")" ]
85         do
86                 mntpt=$(eval echo "\$$var")
87                 dev=$(eval echo "\$FSTAB_dev_$i")
88                 if ! in_mtab "$mntpt" && ! is_mounted "$mntpt" && [ -e "$dev" ]
89                 then
90                         check_boolean "$VERBOSE_MOUNT" && \
91                                 zfs_log_progress_msg "$mntpt "
92                         fsck "$dev" && mount "$mntpt"
93                 fi
95                 i=$((i + 1))
96                 var=$(eval echo "FSTAB_$i")
97         done
99         read_mtab  "[[:space:]]zfs[[:space:]]"
100         read_fstab "[[:space:]]zfs[[:space:]]"
101         i=0; var=$(eval echo "FSTAB_$i")
102         while [ -n "$(eval echo "\$$var")" ]
103         do
104                 mntpt=$(eval echo "\$$var")
105                 if ! in_mtab "$mntpt" && ! is_mounted "$mntpt"
106                 then
107                         check_boolean "$VERBOSE_MOUNT" && \
108                                 zfs_log_progress_msg "$mntpt "
109                         mount "$mntpt"
110                 fi
112                 i=$((i + 1))
113                 var=$(eval echo "FSTAB_$i")
114         done
115         check_boolean "$VERBOSE_MOUNT" && zfs_log_end_msg 0
117         return 0
120 # Unmount all filesystems
121 do_unmount()
123         local i var mntpt
125         # This shouldn't really be necessary, as long as one can get
126         # zfs-import to run sufficiently late in the shutdown/reboot process
127         # - after unmounting local filesystems. This is just here in case/if
128         # this isn't possible.
129         zfs_action "Unmounting ZFS filesystems" "$ZFS" unmount -a
131         check_boolean "$VERBOSE_MOUNT" && \
132             zfs_log_begin_msg "Unmounting volumes and filesystems registered in fstab"
134         read_mtab  "^/dev/(zd|zvol)"
135         read_fstab "^/dev/(zd|zvol)"
136         i=0; var="FSTAB_0"
137         while [ -n "$(eval echo "\$$var")" ]
138         do
139                 mntpt=$(eval echo "\$$var")
140                 dev=$(eval echo "\$FSTAB_dev_$i")
141                 if in_mtab "$mntpt"
142                 then
143                         check_boolean "$VERBOSE_MOUNT" && \
144                                 zfs_log_progress_msg "$mntpt "
145                         umount "$mntpt"
146                 fi
148                 i=$((i + 1))
149                 var=$(eval echo "FSTAB_$i")
150         done
152         read_mtab  "[[:space:]]zfs[[:space:]]"
153         read_fstab "[[:space:]]zfs[[:space:]]"
154         i=0; var="FSTAB_0"
155         while [ -n "$(eval echo "\$$var")" ]
156         do
157                 mntpt=$(eval echo "\$$var")
158                 if in_mtab "$mntpt"; then
159                         check_boolean "$VERBOSE_MOUNT" && \
160                             zfs_log_progress_msg "$mntpt "
161                         umount "$mntpt"
162                 fi
164                 i=$((i + 1))
165                 var=$(eval echo "FSTAB_$i")
166         done
167         check_boolean "$VERBOSE_MOUNT" && zfs_log_end_msg 0
169         return 0
172 do_start()
174         check_boolean "$ZFS_MOUNT" || exit 0
176         check_module_loaded "zfs" || exit 0
178         # Ensure / exists in /proc/self/mounts.
179         # This should be handled by rc.sysinit but lets be paranoid.
180         if ! chkroot
181         then
182                 mount -f /
183         fi
185         do_mount
188 do_stop()
190         check_boolean "$ZFS_UNMOUNT" || exit 0
192         check_module_loaded "zfs" || exit 0
194         do_unmount
197 # ----------------------------------------------------
199 if [ ! -e /sbin/openrc-run ]
200 then
201         case "$1" in
202                 start)
203                         do_start
204                         ;;
205                 stop)
206                         do_stop
207                         ;;
208                 force-reload|condrestart|reload|restart|status)
209                         # no-op
210                         ;;
211                 *)
212                         [ -n "$1" ] && echo "Error: Unknown command $1."
213                         echo "Usage: $0 {start|stop}"
214                         exit 3
215                         ;;
216         esac
218         exit $?
219 else
220         # Create wrapper functions since Gentoo don't use the case part.
221         depend() { do_depend; }
222         start() { do_start; }
223         stop() { do_stop; }