Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Remove wait when network cable is not plugged in.
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
Safrax
Guru
Guru


Joined: 23 Apr 2002
Posts: 422

PostPosted: Mon Mar 03, 2003 5:06 pm    Post subject: Remove wait when network cable is not plugged in. Reply with quote

For an updated version please see Rojaro's post below.

I was looking for a solution to the problem I have when I boot my laptop up without a network connection. In my search I came across this thread: https://forums.gentoo.org/viewtopic.php?t=35245. I figured I could probably adapt that script so that I could avoid the network detection wait.

What follows is the changes I made to /etc/init.d/net.eth0.

Original code:

Code:

start() {
        checkconfig || return 1
        local iface_args="$(eval echo \$\{iface_${IFACE}\})"
        local dhcp_args="$(eval echo \$\{dhcpcd_${IFACE}\})"
        local retval=0
        ebegin "Bringing ${IFACE} up"
        if [ "$(eval echo \$\{iface_${IFACE}\})" != "dhcp" ]
        then
                /sbin/ifconfig ${IFACE} ${iface_args} >/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
                /sbin/dhcpcd ${dhcp_args} ${IFACE} >/dev/null || {
                        retval=$?
                        eend ${retval} "Failed to bring ${IFACE} up"
                        return ${retval}
                }
        fi
        eend 0

        if [ -n "$(eval echo \$\{alias_${IFACE}\})" ]
        then
                local x=""
                local num=0


Heres my hack:
NOTE: This requires the mii-diag program to work!

Code:

                /sbin/ifconfig ${IFACE} &> /dev/null || {
                        retval=$?
                        eend ${retval} "Failed to bring ${IFACE} up"
                        return ${retval}
                }
        else
               BEAT=`mii-diag --status eth0 | grep "link beat" | grep "OK"`
                if test -n "$BEAT"
                        then
                                /sbin/dhcpcd ${dhcp_args} ${IFACE} >/dev/null
                        else
                                eend "No ethernet link on ${IFACE} detected.
                                eend "Network Not Initialized"
                                return 2
                fi
        fi
        eend 0


I'm a beginner programmer so this is probably a really nasty hack. Anyways an explanation is in order:

I first added a variable BEAT, which uses the program mii-diag to check for the presence of a network link. If the link is detected then BEAT has a value associated with it. Without a link BEAT is empty. I then put in an if statement to check BEAT for a value. If there is something in BEAT then the script will execute dhcpcd on the proper interface. If BEAT is empty then the script exits and the computer continues to boot. Unfortunately I don't have a clue what this would do to people that have static configurations.

If anyone has any suggestions on how to improve this PLEASE POST THEM!

Edit: Title Change


Last edited by Safrax on Thu Apr 10, 2003 4:31 pm; edited 1 time in total
Back to top
View user's profile Send private message
TheCoop
Veteran
Veteran


Joined: 15 Jun 2002
Posts: 1814
Location: Where you least expect it

PostPosted: Mon Mar 03, 2003 7:21 pm    Post subject: Reply with quote

cool :D

no reason why this shouldnt be put into baselayout soonish is there? (ofc it needs to be tested, im right on it :D)

do you stick it between lines 31 and 49?
_________________
95% of all computer errors occur between chair and keyboard (TM)

"One World, One web, One program" - Microsoft Promo ad.
"Ein Volk, Ein Reich, Ein Führer" - Adolf Hitler

Change the world - move a rock
Back to top
View user's profile Send private message
Safrax
Guru
Guru


Joined: 23 Apr 2002
Posts: 422

PostPosted: Mon Mar 03, 2003 11:37 pm    Post subject: Reply with quote

What I did was replace this:

Code:

else
                /sbin/dhcpcd ${dhcp_args} ${IFACE} >/dev/null || {
                        retval=$?
                        eend ${retval} "Failed to bring ${IFACE} up"
                        return ${retval}
                }
        fi


with this:

Code:

else
               BEAT=`mii-diag --status eth0 | grep "link beat" | grep "OK"`
                if test -n "$BEAT"
                        then
                                /sbin/dhcpcd ${dhcp_args} ${IFACE} >/dev/null
                        else
                                eend "No ethernet link on ${IFACE} detected.
                                eend "Network Not Initialized"
                                return 2
                fi
Back to top
View user's profile Send private message
El_Presidente_Pufferfish
Veteran
Veteran


Joined: 11 Jul 2002
Posts: 1179
Location: Seattle

PostPosted: Wed Mar 05, 2003 2:06 am    Post subject: Reply with quote

i get Error : /etc/init.d/net.eth0 has sytax errors"(not exact copy)

:-(
Back to top
View user's profile Send private message
fleed
l33t
l33t


Joined: 28 Aug 2002
Posts: 756
Location: London

PostPosted: Wed Mar 05, 2003 10:40 am    Post subject: Reply with quote

Did you copy/paste the code?
Even if you did, make sure that you've used ` right after the BEAT=. Do not use ' since that will not work.
Hope that helps.
Back to top
View user's profile Send private message
rojaro
l33t
l33t


Joined: 06 May 2002
Posts: 732

PostPosted: Wed Mar 05, 2003 6:35 pm    Post subject: Reply with quote

using the so called backquotes or backticks ( ` ) for command substitutions is the old syntax which is still valid (and will remain valid) but should be replaced with $( ... ).

using this syntax also gives you the possibility of nesting command substitutions inside of nested command substitutions -e.g. FOO=$(for i in $(ls); do echo $i ; done) - (which are impossible with the backquote ticks). the use of the BEAT variable is also deprecated as it is only used once.

i also don't get the point for removing the dhcpcd error handling so i added it again as well as a simple check to see if mii-diag exists somewhere in the path - so if there is no mii-diag installed the dhcpcd will be started without checking for the link and if it is installed we're going to take advantage of it :)

Code:
else
  if [ ! -n "$(which mii-diag 2> /dev/null)" -o -n "$(mii-diag --status ${IFACE} | grep "link beat" | grep "OK")" ]
  then
    /sbin/dhcpcd ${dhcp_args} ${IFACE} >/dev/null || {
      retval=$?
      eend ${retval} "dhcpcd failed to bring up ${IFACE}"
      return ${retval}
    }
  else
    eend 2 "No ethernet link on ${IFACE} detected - Network Not Initialized"
    return 2
  fi
fi


oh, and i didn't test that code (no shell right here where i am and this is an untrusted computer environment -internet cafe- so i wont even try to login on one of my machines from here), but i assume it will work as expected. would be nice if somebody could confirm this ...
_________________
A mathematician is a machine for turning coffee into theorems. ~ Alfred Renyi (*1921 - †1970)
Back to top
View user's profile Send private message
panthar
n00b
n00b


Joined: 01 Sep 2002
Posts: 9

PostPosted: Fri Mar 07, 2003 11:18 pm    Post subject: mii-tool Reply with quote

the mii-tool application works well for this script also, and is not masked :)
e.g.

if [ ! -n "$(which mii-tool 2> /dev/null)" -o -n "$(mii-tool ${IFACE} | grep "link ok")" ]
.....
dhcp stuff...
.....
_________________
There are 11 types of people - those who can count in binary and those who cannot.
Back to top
View user's profile Send private message
RoelVanDijk
n00b
n00b


Joined: 15 Dec 2002
Posts: 21

PostPosted: Wed Mar 12, 2003 12:01 pm    Post subject: It works! Reply with quote

rojaro wrote:

oh, and i didn't test that code (no shell right here where i am and this is an untrusted computer environment -internet cafe- so i wont even try to login on one of my machines from here), but i assume it will work as expected. would be nice if somebody could confirm this ...

I tested it and it works perfectly. I really like this tip because I have gentoo installed on a laptop and it took forever to boot without a network cable plugged in.
Thanks rojara and Safrax!
Back to top
View user's profile Send private message
rijidij
n00b
n00b


Joined: 05 Mar 2003
Posts: 5
Location: Leeds, UK

PostPosted: Wed Mar 19, 2003 8:31 am    Post subject: Erm, perhaps I'm missing something here... Reply with quote

...but wouldn't it be much easier just to boot to NONETWORK (runlevel 2, I believe)? :?

You can just add an extra line to your GRUB or LILO menu, with the runlevel number at the end of the "kernel" command.

HTH
Back to top
View user's profile Send private message
rojaro
l33t
l33t


Joined: 06 May 2002
Posts: 732

PostPosted: Wed Mar 19, 2003 2:13 pm    Post subject: Re: Erm, perhaps I'm missing something here... Reply with quote

rijidij wrote:
...but wouldn't it be much easier just to boot to NONETWORK (runlevel 2, I believe)? :?

You can just add an extra line to your GRUB or LILO menu, with the runlevel number at the end of the "kernel" command.

right, that would work too, but then you would have to detect yourself wether you have a network connection or not. this is about having a real detection of the problem and how to avoid it. i also don't use any bootmenu (well, i've got a backup kernel there but i boot to my standard kernel with a timeout of zero) and i don't care if something is connected or not.

also think of situations where you think you got a network connection - e.g. your networkcable is connected to your machine - but you actually dont have one as the switch was turned off by somebody else without you knowing ...
_________________
A mathematician is a machine for turning coffee into theorems. ~ Alfred Renyi (*1921 - †1970)
Back to top
View user's profile Send private message
DesertFox
n00b
n00b


Joined: 14 Nov 2002
Posts: 53

PostPosted: Thu Mar 20, 2003 1:16 am    Post subject: Reply with quote

With dhcp, all you have to do is edit your /etc/conf.d/net

and put in

Code:
dhcpcd_eth0="-t 5"


assuming that eth0 is your nic, and have the delay be 5 seconds (you can change the number to any amount of seconds you wish)

edit: default timeout for dhcpcd is 60 seconds
Back to top
View user's profile Send private message
rojaro
l33t
l33t


Joined: 06 May 2002
Posts: 732

PostPosted: Thu Mar 20, 2003 7:48 pm    Post subject: Reply with quote

right, but the idea of the script is to have no delay at all (if there is no ethernet connection we don't even try starting the dhcpd). and the timeout for the dhcpcd with 60 seconds is just fine - especially if your dhcp server is some old borked NT4 machine which usually responds very slowly to dhcp requests, although a timeout of 10 or 15 seconds should be enough.
_________________
A mathematician is a machine for turning coffee into theorems. ~ Alfred Renyi (*1921 - †1970)
Back to top
View user's profile Send private message
Safrax
Guru
Guru


Joined: 23 Apr 2002
Posts: 422

PostPosted: Thu Mar 20, 2003 9:20 pm    Post subject: Reply with quote

rojaro wrote:
right, but the idea of the script is to have no delay at all (if there is no ethernet connection we don't even try starting the dhcpd). and the timeout for the dhcpcd with 60 seconds is just fine - especially if your dhcp server is some old borked NT4 machine which usually responds very slowly to dhcp requests, although a timeout of 10 or 15 seconds should be enough.


This is exactly what I was going after. The point was to totally avoid the wait. There is no point in starting dhcp and waiting 5 seconds or 2 seconds or any amount of time if the cable for the network isn't plugged into a machine. Hence the reason why I hacked the original script together.

On another note: I wish to thank everyone that has contributed to this thread. This is what makes the open source community so great.
Back to top
View user's profile Send private message
MadEagle
n00b
n00b


Joined: 03 Jun 2002
Posts: 59
Location: Hamburg, Germany

PostPosted: Fri Mar 21, 2003 12:40 pm    Post subject: Reply with quote

Nice solution for a common problem. Safrax or rojaro, did you submit this as a feature enhancement at https://bugs.gentoo.org?

MadEagle
Back to top
View user's profile Send private message
Safrax
Guru
Guru


Joined: 23 Apr 2002
Posts: 422

PostPosted: Fri Mar 21, 2003 4:55 pm    Post subject: Reply with quote

MadEagle wrote:
Nice solution for a common problem. Safrax or rojaro, did you submit this as a feature enhancement at https://bugs.gentoo.org?

MadEagle


I haven't. I don't see a reason to.
Back to top
View user's profile Send private message
bombcar
Guru
Guru


Joined: 08 Apr 2003
Posts: 446
Location: Lincoln Acres, CA

PostPosted: Wed Apr 09, 2003 12:06 am    Post subject: This doesn't work for me. Reply with quote

If eth0 is not already configured, then mii-tools (and mii-diag) gives me:

SIOCGMIIPHY on 'eth0' failed: Invalid argument
no MII Interfaces found

:(

I'd really like to use this trick, but I can't seem to get it to work.

Did I miss something in a kernel compile?

After I load eth0, then mii-tools works :(
Back to top
View user's profile Send private message
Safrax
Guru
Guru


Joined: 23 Apr 2002
Posts: 422

PostPosted: Wed Apr 09, 2003 3:05 am    Post subject: Re: This doesn't work for me. Reply with quote

bombcar wrote:
If eth0 is not already configured, then mii-tools (and mii-diag) gives me:

SIOCGMIIPHY on 'eth0' failed: Invalid argument
no MII Interfaces found

:(

I'd really like to use this trick, but I can't seem to get it to work.

Did I miss something in a kernel compile?

After I load eth0, then mii-tools works :(


Simple load the network driver module before attempting to use the network.
Back to top
View user's profile Send private message
bombcar
Guru
Guru


Joined: 08 Apr 2003
Posts: 446
Location: Lincoln Acres, CA

PostPosted: Wed Apr 09, 2003 3:57 pm    Post subject: Hmmm, Reply with quote

I guess that means I have to not compile the driver into the kernel, but run a module? Well, I'm willing to try it. :)
Back to top
View user's profile Send private message
comexos
n00b
n00b


Joined: 15 Apr 2003
Posts: 4
Location: LinZ, AT

PostPosted: Thu Apr 24, 2003 12:11 pm    Post subject: Re: Hmmm, Reply with quote

bombcar wrote:
I guess that means I have to not compile the driver into the kernel, but run a module? Well, I'm willing to try it. :)


nono, i also have the driver compiled into the kernel =)
i had the same problem, i had to bring up eth0 with an default ip before using the mii-tools, else it won't work
_________________
it's better to burn out than to fade away
Back to top
View user's profile Send private message
dredd
n00b
n00b


Joined: 27 Nov 2003
Posts: 63
Location: Switzerland

PostPosted: Thu Dec 04, 2003 8:06 pm    Post subject: Reply with quote

doesn anybody know, why I there is this timeout problem on boot with a 2.6.0 kernel, while I never had those troubles with the 2.4.20-gentoo-r8 kernel?
Back to top
View user's profile Send private message
bdraw
n00b
n00b


Joined: 24 Nov 2003
Posts: 21
Location: Tampa

PostPosted: Tue Dec 09, 2003 3:15 pm    Post subject: Reply with quote

Ok I had the same problem with mii-diag error if the adaptor wasn't up.
So here is the code I used to fix it. I don't think anyone will have a problem using this, even if they don't get the error.

Code:

then
 /sbin/ifconfig ${IFACE} up  &> /dev/null
 if [ ! -n "$(which mii-diag 2> /dev/null)" -o -n "$(mii-diag --status ${IFACE} | grep "link beat" | grep "OK")" ]
 then
    /sbin/dhcpcd ${dhcp_args} ${IFACE} >/dev/null || {
retval=$?
eend ${retval} "dhcpcd failed to bring up ${IFACE}"
return ${retval}
    }
  else
    eend 2 "No ethernet link on ${IFACE} detected - Network Not Initialized"
    /sbin/ifconfig ${IFACE} down
    return 2
  fi
fi

_________________
If it's too easy it's boring.
Ben
Back to top
View user's profile Send private message
ewan.paton
Veteran
Veteran


Joined: 29 Jul 2003
Posts: 1219
Location: glasgow, scotland

PostPosted: Mon Dec 22, 2003 1:41 pm    Post subject: Reply with quote

this is really usefull for me as the 2.6 kernel swaps eth0 to eth1 so i can just bring both up and it doesnt matter thanks
_________________
Giay tay nam | Giay nam cao cap | Giay luoi
Back to top
View user's profile Send private message
UberLord
Retired Dev
Retired Dev


Joined: 18 Sep 2003
Posts: 6759
Location: Blighty

PostPosted: Mon Jan 12, 2004 9:49 pm    Post subject: Reply with quote

I prefer using ifplugd myself as it brings up and down the interface if the cable is in or out - handy!
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum