---------- Forwarded message ----------
From: "Victor Wren" <vw...@ponyhome.com>
Date: Mar 22, 2012 10:49 AM
Subject: Error in ipv4-static
To: <nat...@linuxfromscratch.org>
Cc: <kpflem...@linuxfromscratch.org>

I was having fits with trying to get secondary ethernet ports setup with
ipv4.  The scripts would neither bring up nor bring down an ip address of
172.16.24.2.  I finally tracked it down to this command in "ipv4-static,"
which shows up in two places (lines 45 & 67):

if [ "$(ip addr show ${1} | grep ${IP})" == "" ]; then

This fails in a couple of cases, if this IP address is not the first one
being set up on this interface.
Case 1: ip address begins with "2" in the last octet.
Case 2: ip address begins with "25" in the last octet.
It will fail on other cases, if the broadcast address has something other
than "255" in the last octet.

The reason for this is that the "ip addr show" command lists all primary
and secondary addresses for an interface.  In my own case, I have 6
addresses set up on eth0 (it's the lazy way to test many websites).  This
is what the first part of the test produces:

# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state
UP qlen 1000
   link/ether 00:1e:4f:f8:00:39 brd ff:ff:ff:ff:ff:ff
   inet 172.16.24.1/24 brd 172.16.24.255 scope global eth0
   inet 172.16.24.2/24 brd 172.16.24.255 scope global secondary eth0
   inet 172.16.24.3/24 brd 172.16.24.255 scope global secondary eth0
   inet 172.16.24.4/24 brd 172.16.24.255 scope global secondary eth0
   inet 172.16.24.6/24 brd 172.16.24.255 scope global secondary eth0
   inet 172.16.24.8/24 brd 172.16.24.255 scope global secondary eth0
....

All of them have a broadcast address of 172.16.24.255  That means that the
grep will match when the last octet of the address I'm trying to bring up
(or down) is 2 or 25, and the test will fall through to the message "Cannot
add IPv4 address ${IP} to ${1}.  Already present." even though the address
is NOT present in reality.

This is not good behavior.  It should only be looking at the second field,
not the whole line.  The ideal solution would probably be to filter it with
awk, but after a little perusal, I came up with a simpler solution.  This
is what the grep should look like:

if [ "$(ip addr show ${1} | grep ${IP}/)" == "" ]; then

Note the addition of a slash after the IP (the separator for the netmask.)
 That makes sure it's checking the correct IP address field.  I've tested
this, and it works.

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

Reply via email to