]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - usr.sbin/bsdinstall/scripts/netconfig_ipv4
MFC r322374: bsdinstall: record DHCP config after obtaining lease
[FreeBSD/stable/10.git] / usr.sbin / bsdinstall / scripts / netconfig_ipv4
1 #!/bin/sh
2 #-
3 # Copyright (c) 2011 Nathan Whitehorn
4 # Copyright (c) 2013-2015 Devin Teske
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 # 1. Redistributions of source code must retain the above copyright
11 #    notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 #    notice, this list of conditions and the following disclaimer in the
14 #    documentation and/or other materials provided with the distribution.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 # SUCH DAMAGE.
27 #
28 # $FreeBSD$
29 #
30 ############################################################ INCLUDES
31
32 BSDCFG_SHARE="/usr/share/bsdconfig"
33 . $BSDCFG_SHARE/common.subr || exit 1
34 f_dprintf "%s: loading includes..." "$0"
35 f_include $BSDCFG_SHARE/dialog.subr
36
37 ############################################################ MAIN
38
39 INTERFACE=$1
40 IFCONFIG_PREFIX="$2"
41 test -z "$IFCONFIG_PREFIX" || IFCONFIG_PREFIX="$2 "
42 case "${INTERFACE}" in
43 "")     dialog --backtitle 'FreeBSD Installer' --title 'Network Configuration' \
44             --msgbox 'No interface specified for IPv4 configuration.' 0 0
45         exit 1
46         ;;
47 esac
48
49 dialog --backtitle 'FreeBSD Installer' --title 'Network Configuration' --yesno 'Would you like to use DHCP to configure this interface?' 0 0
50 if [ $? -eq $DIALOG_OK ]; then
51         if [ ! -z $BSDINSTALL_CONFIGCURRENT ]; then
52                 dialog --backtitle 'FreeBSD Installer' --infobox "Acquiring DHCP lease..." 0 0
53                 err=$( dhclient $INTERFACE 2>&1 )
54                 if [ $? -ne 0 ]; then
55                         f_dprintf "%s" "$err"
56                         dialog --backtitle 'FreeBSD Installer' --msgbox "DHCP lease acquisition failed." 0 0
57                         exec $0 ${INTERFACE} "${IFCONFIG_PREFIX}"
58                 fi
59         fi
60         echo ifconfig_$INTERFACE=\"${IFCONFIG_PREFIX}DHCP\" >> $BSDINSTALL_TMPETC/._rc.conf.net
61         exit 0
62 fi
63
64 IP_ADDRESS=`ifconfig $INTERFACE inet | awk '/inet/ {printf("%s\n", $2); }'`
65 NETMASK=`ifconfig $INTERFACE inet | awk '/inet/ {printf("%s\n", $4); }'`
66 ROUTER=`netstat -rn -f inet | awk '/default/ {printf("%s\n", $2);}'`
67
68 exec 3>&1
69 IF_CONFIG=$(dialog --backtitle 'FreeBSD Installer' --title 'Network Configuration' --form 'Static Network Interface Configuration' 0 0 0 \
70         'IP Address' 1 0 "$IP_ADDRESS" 1 20 16 0 \
71         'Subnet Mask' 2 0 "$NETMASK" 2 20 16 0 \
72         'Default Router' 3 0 "$ROUTER" 3 20 16 0 \
73 2>&1 1>&3)
74 if [ $? -eq $DIALOG_CANCEL ]; then exit 1; fi
75 exec 3>&-
76
77 echo $INTERFACE $IF_CONFIG | 
78     awk -v prefix="$IFCONFIG_PREFIX" '{
79         printf("ifconfig_%s=\"%s\inet %s netmask %s\"\n", $1, prefix, $2, $3);
80         printf("defaultrouter=\"%s\"\n", $4);
81     }' >> $BSDINSTALL_TMPETC/._rc.conf.net
82 retval=$?
83
84 if [ "$BSDINSTALL_CONFIGCURRENT" ]; then
85         . $BSDINSTALL_TMPETC/._rc.conf.net
86         ifconfig $INTERFACE `eval echo \\\$ifconfig_$INTERFACE`
87         if [ "$defaultrouter" ]; then
88                 route delete -inet default
89                 route add -inet default $defaultrouter
90                 retval=$?
91         fi
92 fi
93
94 exit $retval
95
96 ################################################################################
97 # END
98 ################################################################################