[lfs-dev] How to create an alias interface with LFS networt bootscripts

Bruce Dubbs bruce.dubbs at gmail.com
Fri Feb 6 22:05:39 PST 2015


Bruce Dubbs wrote:
> Bruce Dubbs wrote:
>> Tim Tassonis wrote:
>>> Hi all
>>>
>>> I would like to create an alias interface in the form of eth0:0 to set a
>>> a second ip address on the interface. I tried the (obvious):
>>>
>>> cat > /etc/sysconfig/ifconfig.eth0:0  << EOF
>>> ONBOOT="yes"
>>> IFACE="eth0:0"
>>> SERVICE="ipv4-static"
>>> IP="192.168.219.221"
>>> GATEWAY="192.168.219.1"
>>> PREFIX="24"
>>> EOF

> I'll post a revised script that you can use tomorrow.

It was too easy.  Try the included script.  It goes in /lib/lsb/

Use:

cat > /etc/sysconfig/ifconfig.eth0:0  << EOF
ONBOOT=yes
IFACE=eth0
LABEL=eth0:0
SERVICE=ipv4-static
IP=192.168.219.221
PREFIX=24
BROADCAST=192.168.219.255
EOF

Note1: No GATEWAY, but you can set up a static route if you desire.

Note2: Quotes are optional around the values.  The only time they would 
be needed is if there is a space needed like in:

DHCP_START="-b -q"

I need to update BLFS to be consistent.

   -- Bruce


-------------- next part --------------
#!/bin/sh
########################################################################
# Begin /lib/services/ipv4-static
#
# Description : IPV4 Static Boot Script
#
# Authors     : Nathan Coulson - nathan at linuxfromscratch.org
#               Kevin P. Fleming - kpfleming at linuxfromscratch.org
# Update      : Bruce Dubbs - bdubbs at linuxfromscratch.org
#
# Version     : LFS 7.0
#
########################################################################

. /lib/lsb/init-functions
. ${IFCONFIG}

if [ -z "${IP}" ]; then
   log_failure_msg "\nIP variable missing from ${IFCONFIG}, cannot continue."
   exit 1
fi

if [ -z "${PREFIX}" -a -z "${PEER}" ]; then
   log_warning_msg "\nPREFIX variable missing from ${IFCONFIG}, assuming 24."
   PREFIX=24
   args="${args} ${IP}/${PREFIX}"

elif [ -n "${PREFIX}" -a -n "${PEER}" ]; then
   log_failure_msg "\nPREFIX and PEER both specified in ${IFCONFIG}, cannot continue."
   exit 1

elif [ -n "${PREFIX}" ]; then
   args="${args} ${IP}/${PREFIX}"

elif [ -n "${PEER}" ]; then
   args="${args} ${IP} peer ${PEER}"
fi

if [ -n "${LABEL}" ]; then
   args="${args} label ${LABEL}"
fi

if [ -n "${BROADCAST}" ]; then
   args="${args} broadcast ${BROADCAST}"
fi

case "${2}" in
   up)
      if [ "$(ip addr show ${1} 2>/dev/null | grep ${IP}/)" == "" ]; then
         
         # Cosmetic output not needed for multiple services
         if ! $(echo ${SERVICE} | grep -q " "); then 
           log_info_msg2 "\n" # Terminate the previous message
         fi
         
         log_info_msg "Adding IPv4 address ${IP} to the ${1} interface..."
         ip addr add ${args} dev ${1}
         evaluate_retval
      else
         log_warning_msg "Cannot add IPv4 address ${IP} to ${1}.  Already present."
      fi
   ;;

   down)
      if [ "$(ip addr show ${1} 2>/dev/null | grep ${IP}/)" != "" ]; then
         log_info_msg "Removing IPv4 address ${IP} from the ${1} interface..."
         ip addr del ${args} dev ${1}
         evaluate_retval
      fi

      if [ -n "${GATEWAY}" ]; then
         # Only remove the gateway if ther are no remaining ipv4 addresses
         if [ "$(ip addr show ${1} 2>/dev/null | grep 'inet ')" != "" ]; then
            log_info_msg "Removing default gateway..."
            ip route del default
            evaluate_retval
         fi
      fi
   ;;

   *)
      echo "Usage: ${0} [interface] {up|down}"
      exit 1
   ;;
esac

# End /lib/services/ipv4-static


More information about the lfs-dev mailing list