# laptops and net.eth0 in the boot or default runlevel

## FlipFlop

It's a PITA !!

when starting the system with no network attached the boot process is delayed while dhcp times out .. on the other hand you have to start the net.eth0 service manually when you do have a network attached and the net.eth0 script is not in the runlevels..

WindowsXP seems to be able to tell whether there's a network cable attached to the computer .. Is Linux able to do this too ?

Wouldn't it be nice if the net.eth0 script checked for a connection before it tried to start dhcpd ?

----------

## ScubaStreb

Just hit Crtl + C when it is looking for the network and it stops looking.

----------

## smiler.se

I would like something similar.. check if the network cable is in _or_ is it possible somehow via grub to make two different entires - one loading net.eth0 and one loading net.eth1 (wireless)

----------

## FlipFlop

 *ScubaStreb wrote:*   

> Just hit Crtl + C when it is looking for the network and it stops looking.

 

I thought all keyboard input was ignored during startup .. i'll try it..

----------

## smiler.se

Hitting CTRL + C wont help me, also, all services depending on networking fails because of net.eth0 wont get up (no cable) but if I could choose at boot all these problems would be gone.

----------

## Salze

Hi FlipFlop!

Have a look at mitt-tool and swatch and write (yourself) a small script.

bye

Salze

----------

## petardi

Try ifplugd. It can start a network interface when a cable is plugged in, and stop it when it's plugged out.

----------

## ronmon

If you use dhcp you can set a short timeout option in /etc/conf.d/net.

dhcpcd_eth0="-t 15"

The default is 60 seconds, way too long. That will at least speed things up a bit. It's how I have my laptop set since I usually use a wireless card which is often inserted after startup.

----------

## jaloha

Dude, I'm with you, this sucks.  Fortunately, for you (and me) I wrote something to prevent this problem.

Modify your /etc/init.d/net.eth? as follows:

Change your setup_env functions as follows:

```

setup_env() {

   # No reason to check these multiple times in the file

   iface="${1/\./_}"

   iface_IFACE="$(eval echo \$\{iface_${iface}\})"

   dhcpcd_IFACE="$(eval echo \$\{dhcpcd_${iface}\})"

   inet6_IFACE="$(eval echo \$\{inet6_${iface}\})"

   alias_IFACE="$(eval echo \$\{alias_${iface}\})"

   status_IFACE="$(ifconfig | gawk -v IFACE="${iface}" '/Link/ { if ($1 == IFACE) print "up" }')"

   vlans="$(eval echo \$\{iface_${IFACE}_vlans\})"

   mii_IFACE="$(eval echo \$\{mii_${iface}\})"

}

```

Then modify iface_start to read:

```

iface_start() {

   local retval=0

   setup_env ${1}

   checkconfig || return 1

   if [ "${mii_IFACE}" != "false" ]

   then

      cable_check=`mii-tool ${IFACE} | grep 'link ok'`

      if [ -z "${cable_check}" ]

      then

         eend 1 "Cable not connected, not starting Adapter"

         return 1

      fi

   fi

   local IFACE="${1}"

   ebegin "Bringing ${IFACE} up"

   if [ "${iface_IFACE}" != "dhcp" ]

   then

      /sbin/ifconfig ${IFACE} ${iface_IFACE} >/dev/null || {

         retval=$?

         eend ${retval} "Failed to bring ${IFACE} up"

         return ${retval}

      }

      # ifconfig do not always return failure ..

      /sbin/ifconfig ${IFACE} &> /dev/null || {

         retval=$?

         eend ${retval} "Failed to bring ${IFACE} up"

         return ${retval}

      }

   else

      # Check that eth0 was not brough up by the kernel ...

      if [ "${status_IFACE}" != "up" ]

      then

         /sbin/dhcpcd ${dhcpcd_IFACE} ${IFACE} >/dev/null || {

            retval=$?

            eend ${retval} "Failed to bring ${IFACE} up"

            return ${retval}

         }

      fi

   fi

   eend 0

   if [ -n "${alias_IFACE}" ]

   then

      local x=""

      local num=0

      local aliasbcast=""

      local aliasnmask=""

      ebegin "  Adding aliases"

      for x in ${alias_IFACE}

      do

         aliasbcast="$(eval echo \$\{broadcast_${iface}\} \| awk \'\{ print \$$((num + 1)) \}\')"

         if [ -n "${aliasbcast}" ]

         then

            aliasbcast="broadcast ${aliasbcast}"

         fi

         aliasnmask="$(eval echo \$\{netmask_${iface}\} \| awk \'\{ print \$$((num + 1)) \}\')"

         if [ -n "${aliasnmask}" ]

         then

            aliasnmask="netmask ${aliasnmask}"

         fi

      

         ebegin "    ${IFACE}:${num}"

         /sbin/ifconfig ${IFACE}:${num} ${x} \

            ${aliasbcast} ${aliasnmask} >/dev/null

         num=$((num + 1))

         eend 0

      done

      save_options "alias" "${alias_IFACE}"

   fi

   if [ -n "${inet6_IFACE}" ]

   then

      local x=""

      ebegin "  Adding inet6 addresses"

      for x in ${inet6_IFACE}

      do

         ebegin "    ${IFACE} inet6 add ${x}"

         /sbin/ifconfig ${IFACE} inet6 add ${x} >/dev/null

         eend 0

      done

      save_options "inet6" "${inet6_IFACE}"

   fi

   

   if [ -n "${gateway}" ] && [ "${gateway%/*}" = "${IFACE}" ]

   then

      ebegin "  Setting default gateway"

      # First delete any existing routes if it was setup by kernel ..

      /sbin/route del default dev ${gateway%/*} &>/dev/null

      /sbin/route add default gw ${gateway#*/} dev ${gateway%/*} \

         netmask 0.0.0.0 metric 1 >/dev/null || {

         

         local error=$?

         ifconfig ${IFACE} down &>/dev/null

         eend ${error} "Failed to bring ${IFACE} up"

         stop

         return ${error}

      }

      eend 0

   fi

   # Enabling rp_filter causes wacky packets to be auto-dropped by

   # the kernel.  Note that we only do this if it is not set via

   # /etc/sysctl.conf ...

   if [ -e /proc/sys/net/ipv4/conf/${IFACE}/rp_filter ] && \

      [ -z "$(egrep '^[^#]*rp_filter' /etc/sysctl.conf 2>/dev/null)" ]

   then

      echo 1 > /proc/sys/net/ipv4/conf/${IFACE}/rp_filter

   fi

}

```

Then add:

```
mii_eth?="true"
```

or

```
mii_eth?="false"
```

to your /etc/conf.d/net file according (false to turn checking off and true to turn it on).  Of course, change the ? to 0,1,2,etc as appropriate.

----------

## xr31Daisy

I've created different runlevels : default ( for wireless ), 

cable ( for wired ethernet ), and

nonetwork

And I've mapped them to different init levels in inittab ( 3, 4 and 5 )

I've created grub entries to boot with the appropriate init level ( a simple integer in the kernel parameters).

It's not exactly what you asked for, since the user selects his favorite network interface, instead of relying on some auto-detection.

----------

## Pinto

On my system it is setup so I have to dhcpcd for an ip each time so it brings the network card up but doesn't look for it. Some people might call it a pain but it boots quick then all I hav eto do is dhcpcd eth0 and bam I am on.

----------

## jaloha

As petardi said, ifplugd is also a great solution to this problem.  However, I had to hack my ifplugd.action file to make it work with DHCP.  I don't know if this is required.  If someone else knows a better solution to make it work, please tell me.  Also, note that I might have had to do this since I modified the /etc/init.d/net.eth0 file.  However, when I removed the code I added to net.eth0, it still failed for me (but I might not have done that perfectly).

cat ifplugd.action :

```

!/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

                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

```

----------

## cephlen

jaloha's fix works great for me.

----------

## dan2003

me too, wicked hack  :Smile: 

----------

## MyZelF

 *jaloha wrote:*   

> As petardi said, ifplugd is also a great solution to this problem.  However, I had to hack my ifplugd.action file to make it work with DHCP.
> 
> 

 

Here's the solution I used:

1) add in the config file /etc/conf.d/ifplugd the options "-a -mm" to ARGS:

```
ARGS="-f -u0 -d5 -w -a -mm"
```

(you can find here more infos about these options)

2) edit /usr/sbin/ifplugd.action and make it shut down the interface when the cable is unplugged:

```
case "$2" in

        up)

                state=start

                ;;

        down)

                state=stop

                /sbin/ifconfig $1 down

                ;;

        *)

                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

```

----------

## Garbz

just to note:

Documentation tips and tricks forum has the following:

1) instuctions for ifplugd

2) a script that allows u to select the desired runlevel from grub so u can choose if eth0 gets loaded depending on it.

3) a script which asks you if you want to enable eth0 when the time comes on boot.

I used to use choice 2 (still do depending if i want to start x or not) i even wrote it  :Very Happy: : but the best option is to use ifplugd.  I've had no problems and it works like a charm.

----------

## MyZelF

I read those posts! I even created a link to one of them:

 *MyZelF wrote:*   

> 
> 
> (you can find here more infos about these options) 
> 
> 

 

However, my solution is a little bit different from the one already posted because you don't need to edit /etc/init.d/net.ethX and the interface is brought down by ifplugd.action

----------

## swordsaintzero

If the above code failed its because it was missing the "she" of the shebang. In other words !/bin/sh  should read #!/bin/sh at the top..

Hope that helps anyone whos not a bash head.

----------

## optilude

For those who want different network profiles (e.g. "home" and "work"), and start different network services or configure different options (e.g. wireless WEP keys) for each one, this is exactly the kind of problem I wrote hprofile (http://hprofile.sf.net) to solve!

The tarball, available on the above web site, also comes with example init scripts and profiles for Gentoo linux (including network profiles for wireless and ethernet network connections), so it should be extremely simple to set it up. Have a look at the FAQ and the exaples, and you should be in business. If you do use it, I'd appreciate any feedback, too.  :Smile: 

Martin

----------

