Deleted Added
full compact
nfs.subr (252987) nfs.subr (253333)
1if [ ! "$_MEDIA_NFS_SUBR" ]; then _MEDIA_NFS_SUBR=1
2#
3# Copyright (c) 2012-2013 Devin Teske
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
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.
14#
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
25# SUCH DAMAGE.
26#
1if [ ! "$_MEDIA_NFS_SUBR" ]; then _MEDIA_NFS_SUBR=1
2#
3# Copyright (c) 2012-2013 Devin Teske
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
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.
14#
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
25# SUCH DAMAGE.
26#
27# $FreeBSD: head/usr.sbin/bsdconfig/share/media/nfs.subr 252987 2013-07-07 18:51:44Z dteske $
27# $FreeBSD: head/usr.sbin/bsdconfig/share/media/nfs.subr 253333 2013-07-14 03:08:52Z dteske $
28#
29############################################################ INCLUDES
30
31BSDCFG_SHARE="/usr/share/bsdconfig"
32. $BSDCFG_SHARE/common.subr || exit 1
33f_dprintf "%s: loading includes..." media/nfs.subr
34f_include $BSDCFG_SHARE/device.subr
35f_include $BSDCFG_SHARE/dialog.subr
36f_include $BSDCFG_SHARE/media/common.subr
37f_include $BSDCFG_SHARE/media/network.subr
38f_include $BSDCFG_SHARE/media/tcpip.subr
39f_include $BSDCFG_SHARE/struct.subr
40f_include $BSDCFG_SHARE/variable.subr
41
42BSDCFG_LIBE="/usr/libexec/bsdconfig"
43f_include_lang $BSDCFG_LIBE/include/messages.subr
44
45############################################################ GLOBALS
46
47NFS_MOUNTED=
48
49############################################################ FUNCTIONS
50
51# f_media_set_nfs
52#
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:
55#
56# VAR_NFS_PATH
57# The NFS path specification (host:path) to use when mounting the
58# remote repository.
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'.
63#
64# Meanwhile, the following variables from variable.subr are set after
65# successful execution:
66#
67# VAR_NFS_HOST
68# The host portion of the NFS path specification, parsed from
69# VAR_NFS_PATH.
70#
71f_media_set_nfs()
72{
73 local nfs
74
75 f_media_close
76
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
81
82 case "$nfs" in
83 *:*) : valid NFS path ;;
84 *)
85 f_show_msg "$msg_invalid_nfs_path_specification"
86 return $FAILURE
87 esac
88
89 f_struct_new DEVICE device_nfs
90 device_nfs set name "$nfs"
91
92 if ! f_struct device_network ||
93 ! f_dialog_yesno "$msg_youve_already_done_the_network_configuration"
94 then
95 f_struct device_network &&
96 f_device_shutdown network
97 f_device_select_tcp || return $FAILURE
98 local dev
99 f_getvar $VAR_NETWORK_DEVICE dev
100 f_struct_copy "device_$dev" device_network
101 fi
102 f_device_init network ||
103 f_dprintf "%s: $msg_net_device_init_failed\n" f_media_set_nfs
104
105 local hostname="${nfs%%:*}"
106 if f_isset $VAR_NAMESERVER && ! {
107 f_validate_ipaddr "$hostname" || f_validate_ipaddr6 "$hostname"
108 }; then
109 f_show_info "$msg_looking_up_host" "$hostname"
110 f_dprintf "%s Looking up hostname, %s, using host(1)" \
111 "f_media_set_nfs" "$hostname"
112 if ! f_quietly f_host_lookup "$hostname"; then
113 f_show_msg "$msg_cannot_resolve_hostname" "$hostname"
114 f_struct device_network &&
115 f_device_shutdown network
116 f_struct_free device_network
117 unset $VAR_NFS_PATH
118 return $FAILURE
119 fi
120 f_dprintf "Found DNS entry for %s successfully." "$hostname"
121 fi
122
123 setvar $VAR_NFS_HOST "$hostname"
124
125 device_nfs set type $DEVICE_TYPE_NFS
126 device_nfs set init f_media_init_nfs
127 device_nfs set get f_media_get_nfs
128 device_nfs set shutdown f_media_shutdown_nfs
129 device_nfs set private device_network # in name only (deref'd later)
130
131 f_struct_copy device_nfs device_media
132 f_struct_free device_nfs
133
134 return $SUCCESS
135}
136
137# f_media_init_nfs $device
138#
139# Initializes the NFS media device. Returns success if able to mount the NFS
140# device using mount_nfs(1).
141#
142# The variables (from variable.subr) used to initialize the NFS mount are as
143# follows (all of which are configured manually/optionally from the options
144# menu):
145#
146# VAR_NFS_TCP [Optional]
147# If non-NULL, adds the "tcp" option via `-o' to mount_nfs(8).
148# VAR_NFS_V3 [Optional]
149# If non-NULL, adds the "nfsv3" option via `-o' to mount_nfs(8).
150# VAR_NFS_SECURE [Optional]
151# If non-NULL, adds the "-P" flag to mount_nfs(8).
152# VAR_SLOW_ETHER [Optional]
153# If non-NULL, adjusts the read/write size to avoid timeouts.
154#
155f_media_init_nfs()
156{
157 local dev="$1" name err
158
159 device_$dev get name name || return $FAILURE
160 f_dprintf "Init routine called for NFS device. name=[%s]" \
161 "$name"
162
163 if [ "$NFS_MOUNTED" ]; then
164 f_dprintf "NFS device already mounted."
165 return $SUCCESS
166 fi
167
168 if ! f_device_init network; then
169 f_dprintf "f_media_init_nfs: %s" "$msg_net_device_init_failed"
170 return $FAILURE
171 fi
172
173 if [ ! -e "$MOUNTPOINT" ] &&
174 ! err=$( mkdir -p "$MOUNTPOINT" 2>&1 )
175 then
176 f_dialog_msgbox "$err"
177 return $FAILURE
178 fi
179
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
189
190 local options="rsize=$readsize,wsize=$writesize"
191 [ "$use3" ] && options="$options,nfsv3"
192 [ "$tcp" ] && options="$options,tcp"
193
194 if ! err=$( mount_nfs \
195 ${secure:+-P} -o "$options" "$name" "$MOUNTPOINT" 2>&1 )
196 then
197 err="${err#mount_nfs: }"
198 f_show_msg "$msg_error_mounting_device" \
199 "$name" "$MOUNTPOINT" "$err"
200 f_struct device_network &&
201 f_device_shutdown network
202 return $FAILURE
203 fi
204 NFS_MOUNTED=1
205
206 f_dprintf "Mounted NFS device %s onto %s" "$name" "$MOUNTPOINT"
207
208 return $SUCCESS
209}
210
28#
29############################################################ INCLUDES
30
31BSDCFG_SHARE="/usr/share/bsdconfig"
32. $BSDCFG_SHARE/common.subr || exit 1
33f_dprintf "%s: loading includes..." media/nfs.subr
34f_include $BSDCFG_SHARE/device.subr
35f_include $BSDCFG_SHARE/dialog.subr
36f_include $BSDCFG_SHARE/media/common.subr
37f_include $BSDCFG_SHARE/media/network.subr
38f_include $BSDCFG_SHARE/media/tcpip.subr
39f_include $BSDCFG_SHARE/struct.subr
40f_include $BSDCFG_SHARE/variable.subr
41
42BSDCFG_LIBE="/usr/libexec/bsdconfig"
43f_include_lang $BSDCFG_LIBE/include/messages.subr
44
45############################################################ GLOBALS
46
47NFS_MOUNTED=
48
49############################################################ FUNCTIONS
50
51# f_media_set_nfs
52#
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:
55#
56# VAR_NFS_PATH
57# The NFS path specification (host:path) to use when mounting the
58# remote repository.
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'.
63#
64# Meanwhile, the following variables from variable.subr are set after
65# successful execution:
66#
67# VAR_NFS_HOST
68# The host portion of the NFS path specification, parsed from
69# VAR_NFS_PATH.
70#
71f_media_set_nfs()
72{
73 local nfs
74
75 f_media_close
76
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
81
82 case "$nfs" in
83 *:*) : valid NFS path ;;
84 *)
85 f_show_msg "$msg_invalid_nfs_path_specification"
86 return $FAILURE
87 esac
88
89 f_struct_new DEVICE device_nfs
90 device_nfs set name "$nfs"
91
92 if ! f_struct device_network ||
93 ! f_dialog_yesno "$msg_youve_already_done_the_network_configuration"
94 then
95 f_struct device_network &&
96 f_device_shutdown network
97 f_device_select_tcp || return $FAILURE
98 local dev
99 f_getvar $VAR_NETWORK_DEVICE dev
100 f_struct_copy "device_$dev" device_network
101 fi
102 f_device_init network ||
103 f_dprintf "%s: $msg_net_device_init_failed\n" f_media_set_nfs
104
105 local hostname="${nfs%%:*}"
106 if f_isset $VAR_NAMESERVER && ! {
107 f_validate_ipaddr "$hostname" || f_validate_ipaddr6 "$hostname"
108 }; then
109 f_show_info "$msg_looking_up_host" "$hostname"
110 f_dprintf "%s Looking up hostname, %s, using host(1)" \
111 "f_media_set_nfs" "$hostname"
112 if ! f_quietly f_host_lookup "$hostname"; then
113 f_show_msg "$msg_cannot_resolve_hostname" "$hostname"
114 f_struct device_network &&
115 f_device_shutdown network
116 f_struct_free device_network
117 unset $VAR_NFS_PATH
118 return $FAILURE
119 fi
120 f_dprintf "Found DNS entry for %s successfully." "$hostname"
121 fi
122
123 setvar $VAR_NFS_HOST "$hostname"
124
125 device_nfs set type $DEVICE_TYPE_NFS
126 device_nfs set init f_media_init_nfs
127 device_nfs set get f_media_get_nfs
128 device_nfs set shutdown f_media_shutdown_nfs
129 device_nfs set private device_network # in name only (deref'd later)
130
131 f_struct_copy device_nfs device_media
132 f_struct_free device_nfs
133
134 return $SUCCESS
135}
136
137# f_media_init_nfs $device
138#
139# Initializes the NFS media device. Returns success if able to mount the NFS
140# device using mount_nfs(1).
141#
142# The variables (from variable.subr) used to initialize the NFS mount are as
143# follows (all of which are configured manually/optionally from the options
144# menu):
145#
146# VAR_NFS_TCP [Optional]
147# If non-NULL, adds the "tcp" option via `-o' to mount_nfs(8).
148# VAR_NFS_V3 [Optional]
149# If non-NULL, adds the "nfsv3" option via `-o' to mount_nfs(8).
150# VAR_NFS_SECURE [Optional]
151# If non-NULL, adds the "-P" flag to mount_nfs(8).
152# VAR_SLOW_ETHER [Optional]
153# If non-NULL, adjusts the read/write size to avoid timeouts.
154#
155f_media_init_nfs()
156{
157 local dev="$1" name err
158
159 device_$dev get name name || return $FAILURE
160 f_dprintf "Init routine called for NFS device. name=[%s]" \
161 "$name"
162
163 if [ "$NFS_MOUNTED" ]; then
164 f_dprintf "NFS device already mounted."
165 return $SUCCESS
166 fi
167
168 if ! f_device_init network; then
169 f_dprintf "f_media_init_nfs: %s" "$msg_net_device_init_failed"
170 return $FAILURE
171 fi
172
173 if [ ! -e "$MOUNTPOINT" ] &&
174 ! err=$( mkdir -p "$MOUNTPOINT" 2>&1 )
175 then
176 f_dialog_msgbox "$err"
177 return $FAILURE
178 fi
179
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
189
190 local options="rsize=$readsize,wsize=$writesize"
191 [ "$use3" ] && options="$options,nfsv3"
192 [ "$tcp" ] && options="$options,tcp"
193
194 if ! err=$( mount_nfs \
195 ${secure:+-P} -o "$options" "$name" "$MOUNTPOINT" 2>&1 )
196 then
197 err="${err#mount_nfs: }"
198 f_show_msg "$msg_error_mounting_device" \
199 "$name" "$MOUNTPOINT" "$err"
200 f_struct device_network &&
201 f_device_shutdown network
202 return $FAILURE
203 fi
204 NFS_MOUNTED=1
205
206 f_dprintf "Mounted NFS device %s onto %s" "$name" "$MOUNTPOINT"
207
208 return $SUCCESS
209}
210
211# f_media_get_nfs $device $file [$probe_only]
211# f_media_get_nfs $device $file [$probe_type]
212#
213# Returns data from $file on a mounted NFS device. Similar to cat(1). If
212#
213# Returns data from $file on a mounted NFS device. Similar to cat(1). If
214# $probe_only is present and non-NULL, returns success if $file exists.
214# $probe_type is present and non-NULL, returns success if $file exists. If
215# $probe_type is equal to $PROBE_SIZE, prints the size of $file in bytes to
216# standard-out.
215#
216f_media_get_nfs()
217{
217#
218f_media_get_nfs()
219{
218 local dev="$1" file="$2" probe_only="$3"
220 local dev="$1" file="$2" probe_type="$3"
219
221
220 f_dprintf "f_media_get_nfs: dev=[%s] file=[%s] probe_only=%s" \
221 "$dev" "$file" "$probe_only"
222 f_dprintf "f_media_get_nfs: dev=[%s] file=[%s] probe_type=%s" \
223 "$dev" "$file" "$probe_type"
222
224
223 f_media_generic_get "$MOUNTPOINT" "$file" "$probe_only"
225 f_media_generic_get "$MOUNTPOINT" "$file" "$probe_type"
224}
225
226# f_media_shutdown_nfs $device
227#
228# Shuts down the NFS device using umount(8). Return status should be ignored.
229#
230f_media_shutdown_nfs()
231{
232 local dev="$1" err
233
234 [ "$NFS_MOUNTED" ] || return
235
236 f_dprintf "Unmounting NFS partition on %s" "$MOUNTPOINT"
237 if ! err=$( umount -f "$MOUNTPOINT" 2>&1 ); then
238 err="${err#umount: }"; err="${err#*: }"
239 f_show_msg "$msg_could_not_unmount_the_nfs_partition" \
240 "$MOUNTPOINT" "$err"
241 else
242 NFS_MOUNTED=
243 fi
244}
245
246############################################################ MAIN
247
248f_dprintf "%s: Successfully loaded." media/nfs.subr
249
250fi # ! $_MEDIA_NFS_SUBR
226}
227
228# f_media_shutdown_nfs $device
229#
230# Shuts down the NFS device using umount(8). Return status should be ignored.
231#
232f_media_shutdown_nfs()
233{
234 local dev="$1" err
235
236 [ "$NFS_MOUNTED" ] || return
237
238 f_dprintf "Unmounting NFS partition on %s" "$MOUNTPOINT"
239 if ! err=$( umount -f "$MOUNTPOINT" 2>&1 ); then
240 err="${err#umount: }"; err="${err#*: }"
241 f_show_msg "$msg_could_not_unmount_the_nfs_partition" \
242 "$MOUNTPOINT" "$err"
243 else
244 NFS_MOUNTED=
245 fi
246}
247
248############################################################ MAIN
249
250f_dprintf "%s: Successfully loaded." media/nfs.subr
251
252fi # ! $_MEDIA_NFS_SUBR