On 05/17/2011 08:54 AM, Jeremy Huntwork wrote:
On 5/16/11 1:49 AM, Bryan Kadzban wrote:
I'm not sure what the goal *should* be.  :-)  Does it make sense to try
to clean up completely in this kind of setup?  Maybe or maybe not.

I do think it's least *surprising* to only undo the effects of the start
script, though.  For whatever that's worth.
I do think we're getting a little overcomplicated here. Let's try to
simplify expectations. Here's what I expect (and I *think* this is
reasonable - you tell me :-) )

When I run the equivalent of /etc/init.d/network stop:

All devices configured to start ONBOOT are shut down, any addresses
assigned to them are removed (this of course means that if there is a
running dhcp service in the background, it won't add any addresses to
the device until I restart the network service).

When I run the equivalent of /etc/init.d/network start:

Any devices configured to start ONBOOT are brought up according to the
settings in their associated config file.

This behavior implies that I should be able to modify network devices on
the fly. If the service is restarted, the devices will only be activated
with configuration explicitly stated in the config files.

JH
With ifup and ifdown public, they needed to have predictable behavior with invalid options, and provide a help text. Having been edited a thousand times, those scripts had become kind of ugly to look at. At very least, they are more linear, but IMO they are much cleaner and easy to follow now.

Scripts attached for review.

-- DJ Lucas


--
This message has been scanned for viruses and
dangerous content, and is believed to be clean.

#!/bin/sh
# Begin /etc/init.d/network

### BEGIN INIT INFO
# Provides:            $network
# Required-Start:      $local_fs swap localnet
# Should-Start:        $syslog
# Required-Stop:       $local_fs swap localnet 
# Should-Stop:         $syslog
# Default-Start:       3 4 5
# Default-Stop:        0 1 2 6
# Short-Description:   Starts and configures network interfaces.
# Description:         Starts and configures network interfaces.
# X-LFS-Provided-By:   LFS
### END INIT INFO

. /lib/lsb/init-functions

case "${1}" in
    start)
        # Start all network interfaces
        for dir in ${NETWORK_DEVICES}/ifconfig.*
        do
            interface=${dir##*/ifconfig.}
            # skip if $dir is * (because nothing was found)
            if [ "${interface}" = "*" ]; then
                continue
            fi
            # Process individual configuration files
            for file in `ls "${dir}"`; do
                ONBOOT=`grep "ONBOOT" "${dir}/${file}" | sed 's@^ONBOOT=@@'`
                case "${ONBOOT}" in
                    Y* | y* | 0)
                        /sbin/ifup -c "${dir}/${file}" "${interface}"
                    ;;
                esac
            done
        done
    ;;

    stop)
        # Reverse list
        DIRS=""
        for dir in /run/network/ifconfig.*
        do
            DIRS="${dir} ${DIRS}"
        done

        # Stop all network interfaces
        for dir in ${DIRS}; do
            interface=${dir##*/ifconfig.}
            # skip if $dir is * (because nothing was found)
            if [ "${interface}" = "*" ]; then
                continue
            fi
            # Process individual configuration files
            for file in `ls "${dir}"`; do
                # No checking necessary if it is in /run/network
                /sbin/ifdown -c "${dir}/${file}" "${interface}"
            done
            link_status=`/sbin/ip link show "${interface}" | \
                grep -o "state DOWN"`
            if [ "${link_status}" != "state DOWN" ]; then
                message="Shutting down the ${interface} interface..."
                /sbin/ip addr flush "${interface}" &&
                /sbin/ip link set "${interface}" down
                evaluate_retval standard
            fi
        done
    ;;

    restart)
        ${0} stop
        sleep 1
        ${0} start
    ;;

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

# End /etc/init.d/network
#!/bin/sh
########################################################################
# Begin /sbin/ifdown
#
# Description : Interface Down
#
# Authors     : DJ Lucas - [email protected]
#
# Version     : 00.02
#
########################################################################

. /lib/lsb/init-functions

function get_args()
    {
        if test -z "${1}" ; then
            showhelp
            exit 1
        fi

        while test -n "${1}" ; do
            case "${1}" in
                -c | --configfile)
                    check_arg $1 $2
                    CONFIGFILE="${2}"
                    shift 2
                ;;
                -f | --force)
                    FORCE="1"
                    shift 1
                ;;
                eth* | iw* | wlan*)
                     INTERFACE="${1}"
                     shift 1
                ;;
                -h | --help)
                     showhelp
                     exit 0
                ;;
                *)
                   showhelp
                   echo "ERROR: '${1}' unknown argument"
                   echo ""
                   exit 2
                ;;
            esac
        done
    }

function check_arg()
    {
        echo "${2}" | grep -v "^-" > /dev/null
        if [ -z "$?" -o ! -n "$2" ]; then
            echo "Error:  $1 requires a valid argument."
            exit 2
        fi
    }

function showhelp()
    {
        echo ""
        echo "`/usr/bin/basename ${0}` brings down a valid network interface."
        echo ""
        echo "Options:"
        echo "          -c  --configfile    The path to an interface 
configuration file"
        echo "                              If no configuration file is given, 
all files"
        echo "                              listed in 
/etc/network/ifconfig.<int> will"
        echo "                              be processed, regarless of the 
value of ONBOOT"
        echo "          -f  --force         Flush all IPs and force the 
interface down."
        echo "          -h  --help          Show this help message and exit."
        echo ""
        echo "Examples:"
        echo "          `/usr/bin/basename ${0}` eth0 -c 
/run/network/ifconfig.eth0/ipv4"
        echo "          `/usr/bin/basename ${0}` eth0 --force -c 
/run/network/ifconfig.eth0/ipv4"
        echo "          `/usr/bin/basename ${0}` eth0 --force"
        echo "          `/usr/bin/basename ${0}` eth0"
        echo ""
        echo ""
    }

# Intialize empty variables so that the shell does not polute the script
CONFIGFILE=""
CONFIGDIR=""
INTERFACE=""
FORCE=""
failed=0

# Process command line arguments
get_args ${@}

# Handle common errors - No need to account for bootscripts, this should not
# happen during boot or shutdown.
if [ "${CONFIGFILE}x" != "x" -a ! -f "${CONFIGFILE}" ]; then
    echo "ERROR: ${CONFIGFILE} is not a valid network configuration file."
    echo ""
    exit 2
fi

if [ "${INTERFACE}x" == "x" ]; then
    echo "ERROR: No interface was given"
    echo ""
    exit 2
else
    grep "${INTERFACE}" /proc/net/dev 2>&1 > /dev/null
    if [ "${?}" != "0" ]; then
        echo "ERROR: ${INTERFACE} is not a valid network interface."
        echo ""
        exit2
    fi
fi

# If a configuration file is present, use it
if [ "${CONFIGFILE}x" != "x" ]; then
    . "${CONFIGFILE}"
    if [ -x "/lib/network-services/${SERVICE}" ]; then
        # do the work
        IFCONFIG=${CONFIGFILE} \
            /lib/network-services/${SERVICE} ${INTERFACE} down
        if [ "${?}" == "0" ]; then
            rm "${CONFIGFILE}"
        fi
    else
        echo "ERROR: Service '${SERVICE}' is not a valid service."
        echo ""
        exit 2
    fi
# No interface configuration file was given
else
    # Process all running interface configuration files
    CONFIGDIR="/run/network/ifconfig.${INTERFACE}"
    if [ -d "${CONFIGDIR}" ]; then
        FILES=`ls "${CONFIGDIR}"`
        for CONFIGFILE in ${FILES}
        do
            (
                . "${CONFIGDIR}/${CONFIGFILE}"
                # No error checking necessary if they are in /run
                IFCONFIG="${CONFIGDIR}/${CONFIGFILE}" \
                    /lib/network-services/${SERVICE} ${INTERFACE} down
                if [ "${?}" == "0" ]; then
                    rm "${CONFIGDIR}/${CONFIGFILE}"
                fi
            )
        done
        # all running config files processes, set the link down
        message="Setting interface ${INTERFACE} down..."
        /sbin/ip link set "${INTERFACE}" down
        evaluate_retval standard
    else
        if [ "${FORCE}" != "1" ]; then
            echo "ERROR: No configuration files found for ${INTERFACE}."
            echo ""
            exit 2
        fi
    fi
fi
    
if [ "${FORCE}" == "1" ]; then
    /sbin/ip addr flush dev "${INTERFACE}" 2>&1 > /dev/null || failed=1
    if [ "${failed}" == "1" ]; then
        log_failure_msg "Flushing IP addresses from interface ${INTERFACE}..."
        echo ""
        exit 1
    else
        log_success_msg "Flushing IP addresses from interface ${INTERFACE}..."
    fi
    /sbin/ip link set dev "${INTERFACE}" down 2>&1 > /dev/null || failed=1
    if [ "${failed}" == "1" ]; then
        log_failure_msg "Setting link down for interface ${INTERFACE}..."
        echo ""
        exit 1
    else
        log_success_msg "Setting link down for interface ${INTERFACE}..."
    fi
fi

exit "${failed}"

#!/bin/sh
########################################################################
# Begin /sbin/ifdown
#
# Description : Interface Up
#
# Authors     : DJ Lucas - [email protected]
#
# Version     : 00.02
#
########################################################################

. /lib/lsb/init-functions

function get_args()
    {
        if test -z "${1}" ; then
            showhelp
            exit 1
        fi

        while test -n "${1}" ; do
            case "${1}" in
                -c | --configfile)
                    check_arg $1 $2
                    CONFIGFILE="${2}"
                    shift 2
                ;;
                eth* | iw* | wlan*)
                     INTERFACE="${1}"
                     shift 1
                ;;
                -h | --help)
                     showhelp
                     exit 0
                ;;
                *)
                   showhelp
                   echo "ERROR: '${1}' unknown argument"
                   echo ""
                   exit 2
                ;;
            esac
        done
    }

function check_arg()
    {
        echo "${2}" | grep -v "^-" > /dev/null
        if [ -z "$?" -o ! -n "$2" ]; then
            echo "Error:  $1 requires a valid argument."
            exit 2
        fi
    }

function showhelp()
    {
        echo "`/usr/bin/basename ${0}` brings up a valid network interface."
        echo ""
        echo "Options:"
        echo "          -c  --configfile    The path to an interface 
configuration file"
        echo "                              If no configuration file is given, 
all files"
        echo "                              listed in 
${NETWORK_DEVICES}/ifconfig.<int> will"
        echo "                              be processed, regarless of the 
value of ONBOOT"
        echo "          -h  --help          Show this help message and exit."
        echo ""
        echo "Examples:"
        echo "          `/usr/bin/basename ${0}` eth0 -c 
${NETWORK_DEVICES}/ifconfig.eth0/ipv4"
        echo "          `/usr/bin/basename ${0}` eth0"
        echo ""
        echo ""
    }

# Intialize empty variables so that the shell does not polute the script
CONFIGFILE=""
CONFIGDIR=""
INTERFACE=""

# Process command line arguments
get_args ${@}

# Handle common errors - No need to account for bootscripts, this should not
# happen during boot or shutdown.
if [ "${CONFIGFILE}x" != "x" -a ! -f "${CONFIGFILE}" ]; then
    echo "ERROR: ${CONFIGFILE} is not a valid network configuration file."
    echo ""
    exit 2
fi

if [ "${INTERFACE}x" == "x" ]; then
    echo "ERROR: No interface was given"
    echo ""
    exit 2
else
    grep "${INTERFACE}" /proc/net/dev 2>&1 > /dev/null
    if [ "${?}" != "0" ]; then
        echo "ERROR: ${INTERFACE} is not a valid network interface."
        echo ""
        exit2
    fi
fi

# If a configuration file is present, use it
if [ "${CONFIGFILE}x" != "x" ]; then
    . "${CONFIGFILE}"
    if [ -x "/lib/network-services/${SERVICE}" ]; then
        # do the work
        # Check to make sure the interface is up
        link_status=`/sbin/ip link show "${INTERFACE}" | \
            grep -o "state UP"`
        if [ "${link_status}" != "state UP" ]; then
            message="Bringing up the ${INTERFACE} interface..."
            /sbin/ip link set ${INTERFACE} up
            evaluate_retval standard
        fi
        IFCONFIG=${CONFIGFILE} \
            /lib/network-services/${SERVICE} ${INTERFACE} up
        if [ "$?" == "0" ]; then
            mkdir -p "/run/network/ifconfig.${INTERFACE}"
            cp "${CONFIGFILE}" "/run/network/ifconfig.${INTERFACE}/"
        fi
    else
        echo "ERROR: Service '${SERVICE}' is not a valid service."
        echo ""
        exit 2
    fi
# No interface configuration file was given
else
    # Process all available interface configuration files
    CONFIGDIR="/etc/network/ifconfig.${INTERFACE}"
    if [ -d "${CONFIGDIR}" ]; then
        FILES=`ls "${CONFIGDIR}"`
        for CONFIGFILE in ${FILES}
        do
            (
                . "${CONFIGDIR}/${CONFIGFILE}"
                if [ -x "/lib/network-services/${SERVICE}" ]; then
                    # Check to make sure the interface is up
                    link_status=`/sbin/ip link show "${INTERFACE}" | \
                        grep -o "state UP"`
                    if [ "${link_status}" != "state UP" ]; then
                        message="Bringing up the ${INTERFACE} interface..."
                        /sbin/ip link set ${INTERFACE} up
                        evaluate_retval standard
                    fi
                    IFCONFIG="${CONFIGDIR}/${CONFIGFILE}" \
                        /lib/network-services/${SERVICE} ${INTERFACE} up
                    if [ "$?" == "0" ]; then
                        mkdir -p "/run/network/ifconfig.${INTERFACE}"
                        cp "${CONFIGDIR}/${CONFIGFILE}" \
                            "/run/network/ifconfig.${INTERFACE}/"
                    fi
                else
                    echo "ERROR: Service '${SERVICE}' is not a valid service."
                    echo ""
                    exit 2
                fi

            )
        done
    fi
fi

-- 
http://linuxfromscratch.org/mailman/listinfo/lfs-dev
FAQ: http://www.linuxfromscratch.org/faq/
Unsubscribe: See the above information page

Reply via email to