WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: I would like DHCP finished running before I run ghost  (Read 3865 times)

Offline funguy

  • Newbie
  • *
  • Posts: 29
I would like DHCP finished running before I run ghost
« on: March 11, 2012, 06:03:37 PM »
I noticed that when I boot TCL sometimes udhcpc isn't finished and i have to wait.  After I wait everything is fine.  Is there a kernel boot option to wait for udhcpd before startx continues.  If not what command other than sleep can I run to wait for the udhcpc to complete assigning ips before it runs the next command that needs network up first?

Offline maro

  • Hero Member
  • *****
  • Posts: 1228
Re: I would like DHCP finished running before I run ghost
« Reply #1 on: March 11, 2012, 06:38:27 PM »
I personally would probably not use an unconditional 'sleep' but rather something like
    while [ -z "$( ifconfig eth0 | grep 'inet addr' )" ] ; do sleep 1 ; done
to wait until the NIC in question shows an IP address. Any call to an application that requires a working NIC can follow afterwards.

Offline funguy

  • Newbie
  • *
  • Posts: 29
Re: I would like DHCP finished running before I run ghost
« Reply #2 on: March 11, 2012, 07:02:52 PM »
That looks good but imagine a server with say 6 nics and other machines with just 1 nic.  I need to account for all "present" nics.  It isn't always eth0.  Any additional ideas?

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11594
Re: I would like DHCP finished running before I run ghost
« Reply #3 on: March 11, 2012, 07:24:09 PM »
Hi funguy
You could loop through the /sys/class/net directory.
Skip the dummy and lo entries.
Check the carrier file on the remaining entries. When the NIC comes up, it will contain a 1, when it's down, the file is empty.

Offline funguy

  • Newbie
  • *
  • Posts: 29
Re: I would like DHCP finished running before I run ghost
« Reply #4 on: March 11, 2012, 11:03:11 PM »
do you have code for that loop?  I just wish there was a way to tell by the original process, or undo the seperated dhcp method that allows it to fork to the background if needed.
« Last Edit: March 11, 2012, 11:06:21 PM by funguy »

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11594
Re: I would like DHCP finished running before I run ghost
« Reply #5 on: March 12, 2012, 02:08:28 AM »
Hi funguy
I think this should work:
Code: [Select]
#!/bin/sh

NicCount=0
Timeout=10

for NICS in `ls -1 /sys/class/net`
do
[ "$NICS" = "dummy0" ] && continue
[ "$NICS" = "lo" ] && continue
let "NicCount += 1"
/bin/rm -f /tmp/$NICS.found
done

while [ $Timeout -gt 0 ]
do
for NICS in `ls -1 /sys/class/net`
do
[ "$NICS" = "dummy0" ] && continue
[ "$NICS" = "lo" ] && continue
if [ ! -e /tmp/$NICS.found ]
then
if [ `/bin/grep 1 /sys/class/net/$NICS/carrier` ]
then
touch /tmp/$NICS.found
let "NicCount -= 1"
fi
fi
done
[ $NicCount -le 0 ] && break
sleep 1
let "Timeout -= 1"
done

echo $NicCount
The script will continue to execute until all the network interfaces are brought up. Should any interfaces fail
to come up, there is a 10 second timeout to terminate the loop. If all the interfaces were brought up, it
returns 0, otherwise it returns the number of interfaces that failed to come up.

Offline curaga

  • Administrator
  • Hero Member
  • *****
  • Posts: 11044
Re: I would like DHCP finished running before I run ghost
« Reply #6 on: March 12, 2012, 11:41:13 AM »
It's considered bad practise to have more than one NIC use dhcp, you know ;)
The only barriers that can stop you are the ones you create yourself.

Offline funguy

  • Newbie
  • *
  • Posts: 29
Re: I would like DHCP finished running before I run ghost
« Reply #7 on: March 19, 2012, 02:42:16 PM »
Tested and minor issues.  Eth0 unplugged and eth1 ready leads to 10 second pause during testing.  Can the script be modded to skip no-link ethernets?

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11594
Re: I would like DHCP finished running before I run ghost
« Reply #8 on: March 19, 2012, 03:19:42 PM »
Hi funguy
I don't know how you would tell the difference between a link that has not yet been brought up and one that
is not connected. You can modify   Timeout=10  to  Timeout=5  or to whatever you want, I just pulled that
number out of thin air.

Offline bmarkus

  • Administrator
  • Hero Member
  • *****
  • Posts: 7183
    • My Community Forum
Re: I would like DHCP finished running before I run ghost
« Reply #9 on: March 19, 2012, 03:25:20 PM »

I don't know how you would tell the difference between a link that has not yet been brought up and one that
is not connected.


You can detect whether line is connected or not with ethtool:

Quote
[root@zero ~]# ethtool eth0
Settings for eth0:
        Supported ports: [ MII ]
        Supported link modes:   10baseT/Half 10baseT/Full
                                100baseT/Half 100baseT/Full
        Supports auto-negotiation: Yes
        Advertised link modes:  10baseT/Half 10baseT/Full
                                100baseT/Half 100baseT/Full
        Advertised pause frame use: No
        Advertised auto-negotiation: Yes
        Speed: 100Mb/s
        Duplex: Full
        Port: MII
        PHYAD: 1
        Transceiver: external
        Auto-negotiation: on
        Supports Wake-on: g
        Wake-on: d
        Link detected: yes
[root@zero ~]#
Béla
Ham Radio callsign: HA5DI

"Amateur Radio: The First Technology-Based Social Network."

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11594
Re: I would like DHCP finished running before I run ghost
« Reply #10 on: March 19, 2012, 04:36:42 PM »
Hi bmarkus
All I get is this:
Quote
[tc@box:~$ sudo ethtool eth0
Settings for eth0:
        Supports Wake-on: d
        Wake-on: d
tc@box:~$ /quote]