TITLE:          DHCP client daemon
LFS VERSION:    ALL (tested on 11-05-2001)
AUTHOR:         D.J. Lucas <dj_me@swbell.net>

SYNOPSIS:       How to setup a DHCP client daemon. This is used with most 
                networking gateways for cable modems and xDSL modems.  Also
                used, nowadays, on almost all corporate networks to set up
                non mission critical servers and most workstations.

HINT:  After reading the dhcpd hints by Simon Perreault <nomis80@videotron.ca>
and Thinus Pollard <thinusp@olienhout.org.za>, I had an excelent grasp on the
configuration and use of dhcpcd.  I, however, felt the need to expand on their 
ideas and incorporate the use of the DHCP client into the ethnet init script.
This will both reduce the amount of scripts in the /etc/init.d directory and 
symlinks in /etc/init.d/rc?.d, and also allow for easy change back to a static
network should the need arise.  Also, this could be easily added to the current
LFS book.  

NOTE:  Just a basic reminder....because this works on my system, dosen't mean
that it will work on yours, tho it should.  Even though these are just script 
changes, IT IS ALWAYS A GOOD IDEA TO BACKUP any existing files that will be 
changed, deleted, modified, moved, etc....

How to install and configure the DHCP client daemon according to the LFS
standards.

1. Get the current version of dhcpcd at ftp://ftp.phystech.com/pub/ 

2. Unpack the archive and install. 

tar -zxf dhcpcd-1.?.?.pl?  (maybe overstating the obvious...replace the "?"s)
cd dhcpcd-1.?.?.pl?
./configure --prefix=/usr
make
make install

NOTE:  Although not recomended, if you are using a version older than 
dhcpcd-1.3.21-pl1, see the previous dhcp hints (dhcpd.txt and dhcpd2.txt) at: 
http://hints.linuxfromscratch.org/hints/old/

3. Rename the old ethnet script and create the new one.  As you can see I 
have included the new variable explanations for the nic-config files 
in the script itself for easier administration after the initial setup. 
These can be omitted if you like.
Run the following commands to backup the old file and create the new one.

______________________________________________________________________________
mv /etc/init.d/ethnet /etc/init.d/ethnet-backup
cat > /etc/init.d/ethnet << "EOF"
#!/bin/sh
# Begin /etc/init.d/ethnet
#
# Main script by Gerard Beekmans - gerard@linuxfromscratch.org
# GATEWAY check by Jean-François Le Ray - jfleray@club-internet.fr
# "Specify which IF to use to reach default GATEWAY" by
# Graham Cantin - gcantin@pacbell.net
# DHCP sections added by D.J. Lucas - dj_me@swbell.net
#
# This file has been updated from it's original version to include the use
# of dhcpcd to autoconfigure network cards.  Therefore, the structure of the
# $DEVICE-config and sysconfig/network files has changed slightly.
# The new variable set now uses the following variables:
#
# /etc/$DEVICE-config:
# ONBOOT - yes,no - wether to set up the interface at boot
# DHCP - yes,no - wether or not to use dhcp
# DEVICE - device name (eth0,eth1...etc.) - the device name
# GW - yes,no - set this interface as the default gateway - only used with DHCP
# IP - IP address of the interface - not used with DHCP
# NETMASK - subnet mask of the interface - not used with DHCP
# BROADCAST - broadcast address of the interface - not used with DHCP
#
# /etc/sysconfig/network:
# HOSTNAME - host name of the machine - must be set
# GATEWAY - default gateway of the interface - not used with DHCP
# GATEWAY_IF - Interface - sets interface as default gateway for the system
#                          not used with DHCP
#
# Hope this makes things a bit more clear as my if/then/else statments are a
# little hard to follow.  Note:  the script will fail if both the GATEWAY_IF
# and the GW variables are set, or if both the GATEWAY and the GW variables
# are set.
#
# End of my additions...send any questions to DJ_Me@swbell.net.
#
# Include the functions declared in the /etc/init.d/functions file
# and the variables from the /etc/sysconfig/network file.
#

source /etc/init.d/functions
source /etc/sysconfig/network

case "$1" in
        start)

#
# Obtain all the network card configuration files
#

        for interface in $(/bin/ls /etc/sysconfig/nic-config/ifcfg* | \
            grep -v ifcfg-lo)
        do
#
# Load the variables from that file
#

            source $interface
#
# If the ONBOOT variable is set to yes, process this file and bring the
# interface up.
#
# Added for DHCP using dhcpcd:
# If the DHCP variable is set to yes, the if/then/else statements below
# will force the use of dhcpcd instead of ifconfig.
# End Addition
#

            if [ "$ONBOOT" == yes ]
            then
               if [ "$DHCP" == yes ]
               then
                  echo -n "Bringing up the $DEVICE interface using DHCP..."
                  /sbin/dhcpcd $DEVICE
                  evaluate_retval
                else
                   echo -n "Bringing up the $DEVICE interface..."
                   /sbin/ifconfig $DEVICE $IP broadcast $BROADCAST \
                       netmask $NETMASK
                   evaluate_retval
               fi
            fi
        done

#
# If the /etc/sysconfig/network file contains a GATEWAY variable, set
# the default gateway and the interface through which the default
# gateway can be reached.
#
# Added for DHCP using dhcpcd:
# If the default gateway device is to be set up using DHCP, then the
# GW variable should be used and set to yes as opposed to setting a
# GATEWAY varialbe with an IP address and GATEWAY_IF variable.
# The GATEWAY variable will be set by the /etc/dhcpc/dhcpcd-$DEVICE.info
# file when it is sourced in the following set of if/then/else statements.
# End Addition
#

            if [ "$GATEWAY" != "" ]
            then
                 if [ "$GATEWAY" == yes ]
                 then
                      echo -n "Setting up routing for $GATEWAY_IF interface..."
                      /sbin/route add default gateway $GATEWAY \
                              metric 1 dev $GATEWAY_IF
                      evaluate_retval
                 fi
            else
                 if [ "$GW" == yes ]
                 then
                      echo -n "Setting up routing for $DEVICE interface..."
                      source /etc/dhcpc/dhcpcd-$DEVICE.info
                      /sbin/route add default gateway $GATEWAY \
                              metric 1 dev $DEVICE
                      evaluate_retval
                 fi
            fi
                ;;

        stop)

#
# Obtain all the network card configuration files
#

        for interface in $(/bin/ls /etc/sysconfig/nic-config/ifcfg* | \
            grep -v ifcfg-lo)
        do
#
# Load the variables from that file
#

            source $interface
#
# If the ONBOOT variable is set, process the file and bring the
# interface down
# If the DHCP variable is set to yes, then use dhcpcd to bring the
# interface down instead of ifconfig.

            if [ $ONBOOT == yes ]
            then
                if [ $DHCP == yes ]
                then
                    echo -n "Bringing down the $DEVICE interface..."
                    /sbin/dhcpcd -k
                    evaluate_retval
                else
                    echo -n "Bringing down the $DEVICE interface..."
                    /sbin/ifconfig $DEVICE down
                    evaluate_retval
                fi
            fi
        done
                ;;

        restart)
                $0 stop
                sleep 1
                $0 start
                ;;
        *)
                echo "Usage: $0 {start|stop|restart}"
                exit 1
                ;;
esac

# End /etc/init.d/ethnet
EOF
chmod +x /etc/init.d/ethnet
______________________________________________________________________________

4.  Create the updated nic-config files.  Decide which interface(s) will now
use DHCP and create the files.  On devices that will use static information,
you will use the old config files as created in the LFS book.  
MAKE BACKUP COPIES of the config files that are going to be replaced.
In the new files there will only be 4 variables set.  Here is a copy of my 
/etc/sysconfig/nic-config/ifcfg-eth0 file.
______________________________________________________________________________
ONBOOT=yes
DEVICE=eth0
DHCP=yes
GW=yes
______________________________________________________________________________
The ONBOOT variale tells the ethnet script wether or not to set up the card 
(at boot time).  The options for this variable are "yes" or "no"

The DEVICE variable tells the ethnet script the device name.  This should be
the same as whatever is after "ifcfg-" in the filename.

The DHCP variable tells the ethnet script to run the DCHP client.
The options for this variable are "yes" or "no"

The GW variable tells the ethnet script wether or not this interface should
be set as the default gateway for the system. The options are "yes" or "no"

Note:  You must also remove the GATEWAY and GATEWAY_IF variables from the
/etc/sysconfig/network file.

5.  What to do with your hostname and the /etc/hosts file. 
On most systems it should not be necessary to do the following.  The only 
case where it is required would be if you are using server software.
THIS IS NOT RECOMENDED UNLESS YOU KNOW EXACTLY WHAT YOU ARE DOING.
I'll leave it up to the reader to edit the scripts to do this.  I have done
it on my system, but I know, quite well, how to fix it if the machine should 
die without shuting down properly.  You can source the variables in
/etc/dhcpc/dhcpcd-$DEVICE.info file after the the interface is set up, and 
then dynamicly write (appended) to the hosts file.  IF THIS METHOD IS USED,
AND THE MACHINE IS IMPROPERLY SHUT DOWN, OR IF YOU DON'T WRITE A SCRIPT TO 
RESET THE ORIGINAL FILE, YOUR /etc/hosts FILE WILL GROW AND HAVE A BUNCH OF
INVALID ENTRIES. It might be a good Idea to reset these values with defaults
on every boot.....hmmmm?  The next part of my little projet. :)  I'll create
a new hint on this one in time.  Look for something along the lines of 
hostfile_dhcp.txt.

6.  Before you reboot you need to test the system.  Start by running ifconfig
with no options to see which devices are enabled.  Disable all devices except
lo by running ifconfig <device name> down.  Now run the ethnet script using 
the following command:  /etc/init.d/ethnet start
Look for any failures and try to track them down.  If you have no failures,
run ifconfig and make sure everything looks okay.  Also check that your 
routing is set up correctly by running route with no argurments.  If 
everything looks good, then stop ethnet.  /etc/init.d/ethnet stop
Again look for any failures and track them down.  Again run route and ifconfig,
and look for any interfaces that aren't supposed to be there.  If there are no
interfaces, besides lo, returned by ifconfig and there are no routes set,
congratulations.  Reboot your system and continue on your path with LFS.
If you do have failures that you cannot track down.  Please feel free to
e-mail me <dj_me@swbell.net>  be sure to include the full problem description
(as much as you understand), the exact error message, and the text of all
your config files.  If you have problems, you've made a backup of everything, 
so put the backups back where you got them from.

7.  Conclusion:  "I hope this helps somebody out there ;)" -- Thinus Pollard
I'd like to thank Thinus Pollard and Simon Perreault for their versions of
this hint....which had my LFS system up and running in under an hour.  
Please send any tips or suggestions to dj_me@swbell.net or just edit this one.  
lol. Oh one more thing, please forgive me if I've made any of this hint too 
terribly obvious..it's my first.  

-- D.J. Lucas <dj_me@swbell.net>