freebsd-dev/usr.sbin/bsdconfig/share/media/nfs.subr
Devin Teske 7323adac99 Import media selection/preparation framework (sysinstall inspired). Makes
accessing files from various types of media nice and abstracted away from
the wet-work involved in preparing, validating, and initializing those
types of media. This will be used for the package management system module
and other modules that need access to files and want to allow the user to
decide where those files come from (either in a scripted fashion, prompted
fashion, or any combination thereof).

Heavily inspired by sysinstall and even uses the same reserved words so
that scripts are portable. Coded over months, tested continuously through-
out, and reviewed several times.

Some notes about the changes:
- Move network-setting acquisition/validation routines to media/tcpip.subr
- The options screen from sysinstall has been converted to a dialog menu
- The "UFS" media choice is renamed to "Directory" to reflect how sysinstall
  treats the choice and a new [true] "UFS" media choice has been added that
  acts on real UFS partitions (such as external disks with disklabels).
- Many more help files have been resurrected from sysinstall (I noticed that
  some of the content seems a bit dated; I gave them a once-over but they
  could really use an update).
- A total of 10 media choices are presented (via mediaGetType) including:
  CD/DVD, FTP, FTP Passive, HTTP Proxy, Directory, NFS, DOS, UFS, Floppy, USB
- Novel struct/device management layer for managing the issue of passing
  more information than can comfortably fit in an argument list.
2013-02-25 19:55:32 +00:00

252 lines
7.4 KiB
Plaintext

if [ ! "$_MEDIA_NFS_SUBR" ]; then _MEDIA_NFS_SUBR=1
#
# Copyright (c) 2012-2013 Devin Teske
# All Rights Reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# $FreeBSD$
#
############################################################ INCLUDES
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
f_dprintf "%s: loading includes..." media/nfs.subr
f_include $BSDCFG_SHARE/struct.subr
f_include $BSDCFG_SHARE/device.subr
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/variable.subr
f_include $BSDCFG_SHARE/media/common.subr
f_include $BSDCFG_SHARE/media/tcpip.subr
f_include $BSDCFG_SHARE/media/network.subr
BSDCFG_LIBE="/usr/libexec/bsdconfig"
f_include_lang $BSDCFG_LIBE/include/messages.subr
############################################################ GLOBALS
NFS_MOUNTED=
############################################################ FUNCTIONS
# f_media_set_nfs
#
# Return success if we both found and set the media type to be an NFS server.
# Variables from variable.subr that can be used to script user input:
#
# VAR_NFS_PATH
# The NFS path specification (host:path) to use when mounting the
# remote repository.
# VAR_NAMESERVER [Optional]
# Automatically populated from resolv.conf(5) but can be
# overridden. If set, the host portion of VAR_NFS_PATH is
# looked up using f_host_lookup() from `tcpip.subr'.
#
# Meanwhile, the following variables from variable.subr are set after
# successful execution:
#
# VAR_NFS_HOST
# The host portion of the NFS path specification, parsed from
# VAR_NFS_PATH.
#
f_media_set_nfs()
{
local nfs
f_media_close
f_variable_get_value $VAR_NFS_PATH \
"$msg_please_enter_the_full_nfs_file_specification"
f_getvar $VAR_NFS_PATH nfs
[ "$nfs" ] || return $FAILURE
case "$nfs" in
*:*) : valid NFS path ;;
*)
f_dialog_msgbox "$msg_invalid_nfs_path_specification"
return $FAILURE
esac
f_struct_new DEVICE device_nfs
device_nfs set name "$nfs"
if ! f_struct device_network ||
! f_dialog_yesno "$msg_youve_already_done_the_network_configuration"
then
f_struct device_network &&
f_device_shutdown network
f_device_select_tcp || return $FAILURE
local dev
f_getvar $VAR_NETWORK_DEVICE dev
f_struct_copy "device_$dev" device_network
fi
f_device_init network ||
f_dprintf "%s: $msg_net_device_init_failed\n" f_media_set_nfs
local hostname="${nfs%%:*}"
if f_quietly f_getvar $VAR_NAMESERVER && ! {
f_validate_ipaddr "$hostname" ||
f_validate_ipaddr6 "$hostname"
}; then
f_show_info "$msg_looking_up_host" "$hostname"
f_dprintf "%s Looking up hostname, %s, using host(1)" \
"f_media_set_nfs" "$hostname"
if ! f_quietly f_host_lookup "$hostname"; then
f_show_msg "$msg_cannot_resolve_hostname" "$hostname"
f_struct device_network &&
f_device_shutdown network
f_struct_free device_network
unset $VAR_NFS_PATH
return $FAILURE
fi
f_dprintf "Found DNS entry for %s successfully." "$hostname"
fi
setvar $VAR_NFS_HOST "$hostname"
device_nfs set type $DEVICE_TYPE_NFS
device_nfs set init f_media_init_nfs
device_nfs set get f_media_get_nfs
device_nfs set shutdown f_media_shutdown_nfs
device_nfs set private device_network # in name only (deref'd later)
f_struct_copy device_nfs device_media
f_struct_free device_nfs
return $SUCCESS
}
# f_media_init_nfs $device
#
# Initializes the NFS media device. Returns success if able to mount the NFS
# device using mount_nfs(1).
#
# The variables (from variable.subr) used to initialize the NFS mount are as
# follows (all of which are configured manually/optionally from the options
# menu):
#
# VAR_NFS_TCP [Optional]
# If non-NULL, adds the "tcp" option via `-o' to mount_nfs(8).
# VAR_NFS_V3 [Optional]
# If non-NULL, adds the "nfsv3" option via `-o' to mount_nfs(8).
# VAR_NFS_SECURE [Optional]
# If non-NULL, adds the "-P" flag to mount_nfs(8).
# VAR_SLOW_ETHER [Optional]
# If non-NULL, adjusts the read/write size to avoid timeouts.
#
f_media_init_nfs()
{
local dev="$1" name err
device_$dev get name name || return $FAILURE
f_dprintf "Init routine called for NFS device. name=[%s]" \
"$name"
if [ "$NFS_MOUNTED" ]; then
f_dprintf "NFS device already mounted."
return $SUCCESS
fi
if ! f_device_init network; then
f_dprintf "f_media_init_nfs: %s" "$msg_net_device_init_failed"
return $FAILURE
fi
if [ ! -e "$MOUNTPOINT" ] &&
! err=$( mkdir -p "$MOUNTPOINT" 2>&1 )
then
f_dialog_msgbox "$err"
return $FAILURE
fi
local cp tcp="" use3="" secure="" readsize=4096 writesize=4096
f_getvar $VAR_NFS_TCP cp
[ "$cp" = "YES" ] && tcp=1
f_getvar $VAR_NFS_V3 cp
[ "$cp" = "YES" ] && use3=1
f_getvar $VAR_NFS_SECURE cp
[ "$cp" = "YES" ] && secure=1
f_getvar $VAR_SLOW_ETHER cp
[ "$cp" = "YES" ] && readsize=1024 writesize=1024
local options="rsize=$readsize,wsize=$writesize"
[ "$use3" ] && options="$options,nfsv3"
[ "$tcp" ] && options="$options,tcp"
if ! err=$( mount_nfs \
${secure:+-P} -o "$options" "$name" "$MOUNTPOINT" 2>&1 )
then
err="${err#mount_nfs: }"
f_show_msg "$msg_error_mounting_device" \
"$name" "$MOUNTPOINT" "$err"
f_struct device_network &&
f_device_shutdown network
return $FAILURE
fi
NFS_MOUNTED=1
f_dprintf "Mounted NFS device %s onto %s" "$name" "$MOUNTPOINT"
return $SUCCESS
}
# f_media_get_nfs $device $file [$probe_only]
#
# Returns data from $file on a mounted NFS device. Similar to cat(1).
# $probe_only is currently unused by this media type.
#
f_media_get_nfs()
{
local dev="$1" file="$2" probe_only="$3"
f_dprintf "f_media_get_nfs: dev=[%s] file=[%s] probe_only=%s" \
"$dev" "$file" "$probe_only"
f_media_generic_get "$MOUNTPOINT" "$file"
}
# f_media_shutdown_nfs $device
#
# Shuts down the NFS device using umount(8). Return status should be ignored.
#
f_media_shutdown_nfs()
{
local dev="$1" err
[ "$NFS_MOUNTED" ] || return
f_dprintf "Unmounting NFS partition on %s" "$MOUNTPOINT"
if ! err=$( umount -f "$MOUNTPOINT" 2>&1 ); then
err="${err#umount: }"; err="${err#*: }"
f_show_msg "$msg_could_not_unmount_the_nfs_partition" \
"$MOUNTPOINT" "$err"
else
NFS_MOUNTED=
fi
}
############################################################ MAIN
f_dprintf "%s: Successfully loaded." media/nfs.subr
fi # ! $_MEDIA_NFS_SUBR