1 if [ ! "$_MEDIA_NFS_SUBR" ]; then _MEDIA_NFS_SUBR=1
3 # Copyright (c) 2012-2013 Devin Teske
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
15 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 ############################################################ INCLUDES
31 BSDCFG_SHARE="/usr/share/bsdconfig"
32 . $BSDCFG_SHARE/common.subr || exit 1
33 f_dprintf "%s: loading includes..." media/nfs.subr
34 f_include $BSDCFG_SHARE/device.subr
35 f_include $BSDCFG_SHARE/dialog.subr
36 f_include $BSDCFG_SHARE/media/common.subr
37 f_include $BSDCFG_SHARE/media/network.subr
38 f_include $BSDCFG_SHARE/media/tcpip.subr
39 f_include $BSDCFG_SHARE/struct.subr
40 f_include $BSDCFG_SHARE/variable.subr
42 BSDCFG_LIBE="/usr/libexec/bsdconfig"
43 f_include_lang $BSDCFG_LIBE/include/messages.subr
45 ############################################################ GLOBALS
49 ############################################################ FUNCTIONS
53 # Return success if we both found and set the media type to be an NFS server.
54 # Variables from variable.subr that can be used to script user input:
57 # The NFS path specification (host:path) to use when mounting the
59 # VAR_NAMESERVER [Optional]
60 # Automatically populated from resolv.conf(5) but can be
61 # overridden. If set, the host portion of VAR_NFS_PATH is
62 # looked up using f_host_lookup() from `tcpip.subr'.
64 # Meanwhile, the following variables from variable.subr are set after
65 # successful execution:
68 # The host portion of the NFS path specification, parsed from
77 f_variable_get_value $VAR_NFS_PATH \
78 "$msg_please_enter_the_full_nfs_file_specification"
79 f_getvar $VAR_NFS_PATH nfs
80 [ "$nfs" ] || return $FAILURE
83 *:*) : valid NFS path ;;
85 f_show_msg "$msg_invalid_nfs_path_specification"
89 f_struct_new DEVICE device_nfs
90 device_nfs set name "$nfs"
92 if ! f_struct device_network ||
93 ! f_dialog_yesno "$msg_youve_already_done_the_network_configuration"
95 f_struct device_network &&
96 f_device_shutdown device_network
97 f_device_select_tcp || return $FAILURE
99 f_getvar $VAR_NETWORK_DEVICE if
100 f_device_find -1 "$if" $DEVICE_TYPE_NETWORK dev
101 f_struct_copy "$dev" device_network
103 f_device_init device_network ||
104 f_dprintf "%s: $msg_net_device_init_failed\n" f_media_set_nfs
106 local hostname="${nfs%%:*}"
107 if f_isset $VAR_NAMESERVER && ! {
108 f_validate_ipaddr "$hostname" || f_validate_ipaddr6 "$hostname"
110 f_show_info "$msg_looking_up_host" "$hostname"
111 f_dprintf "%s Looking up hostname, %s, using host(1)" \
112 "f_media_set_nfs" "$hostname"
113 if ! f_quietly f_host_lookup "$hostname"; then
114 f_show_msg "$msg_cannot_resolve_hostname" "$hostname"
115 f_struct device_network &&
116 f_device_shutdown device_network
117 f_struct_free device_network
121 f_dprintf "Found DNS entry for %s successfully." "$hostname"
124 setvar $VAR_NFS_HOST "$hostname"
126 device_nfs set type $DEVICE_TYPE_NFS
127 device_nfs set init f_media_init_nfs
128 device_nfs set get f_media_get_nfs
129 device_nfs set shutdown f_media_shutdown_nfs
130 device_nfs set private device_network # in name only (deref'd later)
132 f_struct_copy device_nfs device_media
133 f_struct_free device_nfs
138 # f_media_init_nfs $device
140 # Initializes the NFS media device. Returns success if able to mount the NFS
141 # device using mount_nfs(1).
143 # The variables (from variable.subr) used to initialize the NFS mount are as
144 # follows (all of which are configured manually/optionally from the options
147 # VAR_NFS_TCP [Optional]
148 # If non-NULL, adds the "tcp" option via `-o' to mount_nfs(8).
149 # VAR_NFS_V3 [Optional]
150 # If non-NULL, adds the "nfsv3" option via `-o' to mount_nfs(8).
151 # VAR_NFS_SECURE [Optional]
152 # If non-NULL, adds the "-P" flag to mount_nfs(8).
153 # VAR_SLOW_ETHER [Optional]
154 # If non-NULL, adjusts the read/write size to avoid timeouts.
158 local funcname=f_media_init_nfs
159 local dev="$1" name err
161 $dev get name name || return $FAILURE
162 f_dprintf "Init routine called for NFS device. name=[%s]" \
165 if [ "$NFS_MOUNTED" ]; then
166 f_dprintf "NFS device already mounted."
170 if ! f_device_init device_network; then
171 f_dprintf "f_media_init_nfs: %s" "$msg_net_device_init_failed"
175 if [ ! -e "$MOUNTPOINT" ]; then
176 f_eval_catch $funcname mkdir 'mkdir -p "%s"' "$MOUNTPOINT" ||
180 local cp tcp="" use3="" secure="" readsize=4096 writesize=4096
181 f_getvar $VAR_NFS_TCP cp
182 [ "$cp" = "YES" ] && tcp=1
183 f_getvar $VAR_NFS_V3 cp
184 [ "$cp" = "YES" ] && use3=1
185 f_getvar $VAR_NFS_SECURE cp
186 [ "$cp" = "YES" ] && secure=1
187 f_getvar $VAR_SLOW_ETHER cp
188 [ "$cp" = "YES" ] && readsize=1024 writesize=1024
190 local options="rsize=$readsize,wsize=$writesize"
191 [ "$use3" ] && options="$options,nfsv3"
192 [ "$tcp" ] && options="$options,tcp"
194 if ! f_eval_catch -dk err $funcname mount_nfs \
195 'mount_nfs %s -o "%s" "%s" "%s"' \
196 "${secure:+-P}" "$options" "$name" "$MOUNTPOINT"
198 err="${err#mount_nfs: }"
199 f_show_msg "$msg_error_mounting_device" \
200 "$name" "$MOUNTPOINT" "$err"
201 f_struct device_network &&
202 f_device_shutdown device_network
207 f_dprintf "Mounted NFS device %s onto %s" "$name" "$MOUNTPOINT"
212 # f_media_get_nfs $device $file [$probe_type]
214 # Returns data from $file on a mounted NFS device. Similar to cat(1). If
215 # $probe_type is present and non-NULL, returns success if $file exists. If
216 # $probe_type is equal to $PROBE_SIZE, prints the size of $file in bytes to
221 local dev="$1" file="$2" probe_type="$3"
225 f_dprintf "f_media_get_nfs: dev=[%s] file=[%s] probe_type=%s" \
226 "$name" "$file" "$probe_type"
228 f_media_generic_get "$MOUNTPOINT" "$file" "$probe_type"
231 # f_media_shutdown_nfs $device
233 # Shuts down the NFS device using umount(8). Return status should be ignored.
235 f_media_shutdown_nfs()
237 local funcname=f_media_shutdown_nfs
240 [ "$NFS_MOUNTED" ] || return $FAILURE
242 f_dprintf "Unmounting NFS partition on %s" "$MOUNTPOINT"
243 if ! f_eval_catch -dk err $funcname umount \
244 'umount -f "%s"' "$MOUNTPOINT"
246 err="${err#umount: }"; err="${err#*: }"
247 f_show_msg "$msg_could_not_unmount_the_nfs_partition" \
254 ############################################################ MAIN
256 f_dprintf "%s: Successfully loaded." media/nfs.subr
258 fi # ! $_MEDIA_NFS_SUBR