Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 21 May 2010 23:08:54 +0000 (UTC)
From:      Randi Harper <randi@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-8@freebsd.org
Subject:   svn commit: r208406 - stable/8/usr.sbin/sysinstall
Message-ID:  <201005212308.o4LN8sRF063833@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: randi
Date: Fri May 21 23:08:53 2010
New Revision: 208406
URL: http://svn.freebsd.org/changeset/base/208406

Log:
  MFC r198317,206995:
    Introduce 'netDev=ANY' support for scripted (install.cfg) installs, which
    results in the first ethernet interface with physical link being selected.
  
  Approved by: cperciva (mentor)

Modified:
  stable/8/usr.sbin/sysinstall/tcpip.c
Directory Properties:
  stable/8/usr.sbin/sysinstall/   (props changed)

Modified: stable/8/usr.sbin/sysinstall/tcpip.c
==============================================================================
--- stable/8/usr.sbin/sysinstall/tcpip.c	Fri May 21 20:46:01 2010	(r208405)
+++ stable/8/usr.sbin/sysinstall/tcpip.c	Fri May 21 23:08:53 2010	(r208406)
@@ -40,10 +40,17 @@
 #include "sysinstall.h"
 #include <sys/param.h>
 #include <sys/sysctl.h>
+#include <sys/types.h>
 #include <sys/socket.h>
+#include <sys/sockio.h>
+
 #include <netinet/in.h>
+#include <net/if.h>
+#include <net/if_media.h>
+
 #include <netdb.h>
 #include <paths.h>
+#include <ifaddrs.h>
 
 /* The help file for the TCP/IP setup screen */
 #define TCP_HELPFILE		"tcp"
@@ -636,6 +643,53 @@ netHook(dialogMenuItem *self)
     return devs ? DITEM_LEAVE_MENU : DITEM_FAILURE;
 }
 
+static char *
+tcpDeviceScan(void)
+{
+	int s;
+	struct ifmediareq ifmr;
+	struct ifaddrs *ifap, *ifa;
+	struct if_data *ifd;
+	char *network_dev;
+
+	if ((s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0)
+		return (NULL);
+
+	if (getifaddrs(&ifap) < 0)
+		return (NULL);
+
+	for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
+		memset(&ifmr, 0, sizeof(ifmr));
+		strlcpy(ifmr.ifm_name, ifa->ifa_name, sizeof(ifmr.ifm_name));
+
+		if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
+			continue;	/* some devices don't support this */ 
+
+		if ((ifmr.ifm_status & IFM_AVALID) == 0)
+			continue;	/* not active */
+
+		if (IFM_TYPE(ifmr.ifm_active) != IFM_ETHER)
+			continue;	/* not an ethernet device */
+
+		if (ifmr.ifm_status & IFM_ACTIVE) {
+			network_dev = strdup(ifa->ifa_name);
+			freeifaddrs(ifap);
+
+			if (!variable_get(VAR_NONINTERACTIVE)) 
+				msgConfirm("Using interface %s", network_dev);
+
+			msgDebug("tcpDeviceScan found %s", network_dev);
+			return (network_dev);
+		}
+	}
+
+	close(s);
+
+	freeifaddrs(ifap);
+
+	return (NULL);
+}
+
 /* Get a network device */
 Device *
 tcpDeviceSelect(void)
@@ -647,27 +701,38 @@ tcpDeviceSelect(void)
 
     rval = NULL;
 
-    if (variable_get(VAR_NONINTERACTIVE) && variable_get(VAR_NETWORK_DEVICE)) {
+    if (variable_get(VAR_NETWORK_DEVICE)) {
 	network_dev = variable_get(VAR_NETWORK_DEVICE);
 
+	/* 
+	 * netDev can be set to several types of values.
+	 * If netDev is set to ANY, scan all network devices
+	 * looking for a valid link, and go with the first
+	 * device found. netDev can also be specified as a
+	 * comma delimited list, with each network device
+	 * tried in order. netDev can also be set to a single
+	 * network device.
+	 */
+	if (!strcmp(network_dev, "ANY")) 
+		network_dev = strdup(tcpDeviceScan()); 
+
 	while ((dev = strsep(&network_dev, ",")) != NULL) {
 	    devs = deviceFind(dev, DEVICE_TYPE_NETWORK);
 	    cnt = deviceCount(devs);
+
 	    if (cnt) {
-		if (DITEM_STATUS(tcpOpenDialog(devs[0]) == DITEM_SUCCESS))
-		    return(devs[0]);
+		if (DITEM_STATUS(tcpOpenDialog(devs[0])) == DITEM_SUCCESS) 
+		    return (devs[0]);
 	    }
 	}
-    }
 
-    devs = deviceFind(variable_get(VAR_NETWORK_DEVICE), DEVICE_TYPE_NETWORK);
-    cnt = deviceCount(devs);
+	if (!variable_get(VAR_NONINTERACTIVE))
+		msgConfirm("No network devices available!");
 
-    if (!cnt) {
-	msgConfirm("No network devices available!");
-	return NULL;
+	return (NULL);
     }
-    else if ((!RunningAsInit) && (variable_check("NETWORK_CONFIGURED=NO") != TRUE)) {
+
+    if ((!RunningAsInit) && (variable_check("NETWORK_CONFIGURED=NO") != TRUE)) {
 	if (!msgYesNo("Running multi-user, assume that the network is already configured?"))
 	    return devs[0];
     }



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201005212308.o4LN8sRF063833>