# Two interfaces (LAN and WiFi) in laptop

## sgarcia

My laptop has two interfaces -- eth0, which is a standard LAN interface, and eth1, which is a WiFi card.

Both are set to startup using DHCP, and both work fine.  However, only one is ever hooked up at a time -- that is to say, I only plug in the CAT5 if there isn't a WiFi access point around.  Sometimes, of course, I start up with no interfaces hooked up, completely standalone.

This does mean that the startup scripts always take a long time to bring the system up, since *one* of them always hangs on the inactive interface until it times out.  That's a pain, but one I can live with.  It sorta comes with the territory (although KNOPPIX is able to background the DHCP negotiations without holding up the show...)

The problem is that a *lot* of other scripts depend on the net scripts coming up.  During initial startup this doesn't seem to be a problem, but if I need to restart one of these services by hand later, the script always tries to restart the inactive interface again, resulting in several more minutes of waiting for the DHCP to time out.

Is there a way to design this "need net" dependency to check to make sure at least one interface is up, and only attempt to start net services if *no* interfaces are running?

Also, can someone tell me how these dependencies are written in?  I'm trying to get ntpd to start on this laptop, and even after commenting out the "need net" and "use dns logger" lines from both the "ntpd" and the "ntp-client" scripts, running "/etc/init.d/ntpd start" *still* tries to start the net services again.

----------

## UberLord

Look into ifplugd - it can bring up interfaces when you plug in a cable and take them down when you remove it.

This removes the need for them to be added to the default runlevel  :Smile: 

----------

## sgarcia

Hmm.  That sounds interesting.  Does this mean that you don't have any net.* entries in the default runlevel?  Just ifplugd?

For Gentoo, does this mean that you would change all init scripts that have "need net" in them to instead say something like "need ifplugd"?

----------

## russo79

Yes, you need to :

```

emerge ifplugd

```

Change the /etc/conf.d/ifplugd file according to your configuration...

Then :

```

rc-update del net.eth0

rc-update del net.eth1

rc-update add ifplugd default

```

if you have problems (specially when using dhcp) , try to modify your /usr/sbin/ifplugd.action

Here is mine :

```

#!/bin/sh

# Copyright 1999-2003 Gentoo Technologies, Inc.

# Distributed under the terms of the GNU General Public License v2

# $Header: /home/cvsroot/gentoo-x86/sys-apps/ifplugd/files/ifplugd.action,v 1.2 2003/03/21 15:58:05 wmertens Exp $

#

# Gentoo-specific ifplugd.action

#

# This file gets called by ifplugd when it wants to bring an interface

# up or down.

#

 

case "$2" in

        up)

                state=start

                dhcpcd $1

                ;;

        down)

                state=stop

                /sbin/ifconfig $1 down

                kill `cat /var/run/dhcpcd-$1.pid`

                ;;

        *)

                echo "$0: wrong arguments"

                echo "Call with <interface> <up|down>"

                exit 1

                ;;

esac

 

# [ -x /etc/init.d/net.$1 ] && /etc/init.d/net.$1 --quiet $state

 

exit 0

```

Hope this helps...Last edited by russo79 on Fri Feb 18, 2005 9:54 pm; edited 1 time in total

----------

## Toertel

Regarding problems with DHCP. I had such (dhcpcd was never executed) and solved the problem by changing only one line in /etc/init.d/net.eth0:

Before:

```

status_IFACE="$(ifconfig ${1} 2>${devnull} | gawk '$1 == "UP" {print "up"}')"

```

After:

```

status_IFACE="$(ifconfig ${1} 2>${devnull} | gawk '/addr:/{a=1}/ UP /{ if (a == 1) print "up" }')"

```

The result is that an interface is only seen as "up" when ifconfig says it is UP and it has an IP address. IMHO a cleaner solution. What do you think?

----------

## UberLord

 *Toertel wrote:*   

> The result is that an interface is only seen as "up" when ifconfig says it is UP and it has an IP address. IMHO a cleaner solution. What do you think?

 

I like the looks of that.

Why not submit it to bugs.gentoo.org giving your reasons why.

If the dev's don't like it, they should at least respond by telling you why.

----------

## Toertel

I just filed a bug report proposing the solution I mentioned above. The issue has already been reported multiple times. I added my report to bug 44013 and 34484.

----------

## ronmon

Nice solution people, ifplugd sets up and runs great.

I didn't have to edit ifplugd.action, but did replace the net.eth* line with Toertel's. There was no discernible delay while booting my iBook with or without the network cable plugged in and all services started without errors. An Airport card is on the way and should be here this afternoon. I'll post my experience with that later.

----------

## sgarcia

This is great.  I've hacked at my scripts for a month now trying to get them to behave like this.

Not only can I boot without delay with only a single interface running, I can even fire it up with no network in sight.  That's *never* happened!

Thanks!

----------

## kurifu

Hrml... well the script that russo provided was very helpful if getting my interfaces to go up and down properly. One thing I should point out though is that it seems that eth1 (my WLAN) will never actually go down.

```

tail -f /var/log/messages

```

Shows that when eth0 goes up and down it reports finding and our loosing a link beat, and e100_watchdog reports link up or link down. My eth1 device reports AP Out of Range and AP in Range, but it never actually looses an IP address in the process, this makes me wonder if it will work well when moving from 1 device to a nother without shutting down (though that is not likely to happen). I may experiment one day and find out.

Another thing is that my PCMCIA card manager services seem to complain about devices already being brought up or down now that I am using ifplugd, probably a simple dependency error in the init script, I will take a look into that later and likely just send the output to /dev/null to make it be quiet.  :Razz: 

----------

## kurifu

Here is a small change that will allow you to override interface eth1 if you know your gateway. If you do not know it, you could change the scipt to pull it from a call to route, or some such anyway, this is simple, but post something more robust if you would like.

```

#!/bin/sh

#File: /usr/sbin/ifplugd.action

case "$2" in

   up)

      state=start

      dhcpcd $1

      #bring down the gateway for eth1 if eth0 going up

      /sbin/route del default gw 192.168.1.1 dev eth1

      echo "gateway for eth1 going down" >> /tmp/ifplugd_log

      ;;

   down)

      state=stop

      /sbin/ifconfig $1 down

      kill `cat /var/run/dhcpcd-$1.pid`

      #if eth0 is oging down, bring eth1 back up

      case "$1" in

         eth0)

            /sbin/route add default gw 192.168.1.1 dev eth1

            echo "gateway for eth1 going up" >> /tmp/ifplugd_log

            ;;

      esac

      ;;

   *)

      echo "$0: wrong arguments"

      echo "Call with <interface> <up|down>"

      exit 1

      ;;

esac

#Just dump some debugging stuff to a file

echo "ifplugd action: $2 $1" >> /tmp/ifplugd_log

exit 0

```

----------

## teedog

 *Toertel wrote:*   

> Regarding problems with DHCP. I had such (dhcpcd was never executed) and solved the problem by changing only one line in /etc/init.d/net.eth0:
> 
> Before:
> 
> ```
> ...

 

According to derf, the above patch is still flawed.

"Patching net.eth0 in the suggested manner doesn't really solve the problem. When the cable gets unplugged, net.eth0 just takes the interface down again, so ifplugd will never notice if it is ever plugged back in."

Scroll to the bottom of this page for derf's solution:

https://bugs.gentoo.org/show_bug.cgi?id=34484

----------

