How to detect the number of network adapters installed from kickstart file for network config?

peter Sjoberg lpaseen at gmail.com
Sun Feb 20 02:57:44 UTC 2011


On Sat, 2011-02-19 at 04:09 +0100, Patrick Lists wrote:
> On 02/19/2011 02:37 AM, Pat wrote:
> > My kickstart file is usually used on systems with 1 or 2 network interfaces installed. If my kickstart file has the following lines:
> >
> > network --device eth0 --bootproto dhcp
> > network --device eth1 --onboot no --bootproto dhcp
> >
> >
> > It will fail if I use it on a system with only 1 network interface since eth1 doesn't exist.
> >
> >> From a kicksstart file how do I detect the number of network interfaces installed on the system and then only assign eth1 as above if that number is 2 or more? If it matters, it's possible the network interfaces are not plugged into the network when the kickstart is run. Thanks.
> 
> You could test for the number of interfaces. Since I'm a total n00b in 
> this area hopefully the experienced folks who monitor this list will 
> have a good laugh at my "scripting" and come up with something better 
> that actually works :)
Well, dunno about works but a simpler version at least
Task: if eth1 exist - configure it
Solution:

#Top part:
#Always config eth0
network --device eth0 --bootproto dhcp
%include /tmp/optionalnetwork.inc

%pre
#make sure the file is there to not cause any errors
touch /tmp/optionalnetwork.inc

/sbin/ifconfig eth1 &>/dev/null && \
  echo "network --device eth1 --onboot no --bootproto dhcp"\
    >/tmp/optionalnetwork.inc

Or if ksbootenv ifconfig is different and doesn't fail if not found

ifconfig -a|grep ^eth1 &>/dev/null && \
  echo "network --device eth1 --onboot no --bootproto dhcp"\
    >/tmp/optionalnetwork.inc


No need to do some fancy interface count, if it is eth1 you want to
configure, just check if it's there or not.
One issue is if eth1 use a different driver then eth0 and that one isn't
loaded, then you must first modprobe the driver before checking if the
interface is there.

> Here's how I would try to solve it. Totally untested, use at own risk. 
I second that for my code

> Mind the linewrap. It's all one line. My box has lo, br0, eth0 and 
> virbr0 so the script should give a total of 4 interfaces.
> 
> $ ifconfig -a | grep -vE 'inet|UP|RX|TX|collisions|Interrupt' | awk 
> 'BEGIN {no_of_interfaces=0} /^[a-z]/ {print $1}' | awk 'END 
> {no_of_interfaces=NR; print "Number of interfaces: " no_of_interfaces}'
> 
> Number of interfaces: 4 (correct so apparently this works)
> 
> You could add this to the %pre section in your kickstart file:
> 
> %pre
> NO_OF_INTERFACES=`ifconfig -a | grep -vE 
> 'inet|UP|RX|TX|collisions|Interrupt' | awk 'BEGIN {no_of_interfaces=0} 
> /^[a-z]/ {print $1}' | awk 'END {no_of_interfaces=NR; print 
> no_of_interfaces}'`
> echo "INTERFACE_COUNT=$NO_OF_INTERFACES" > /tmp/interface_count.txt
> 
> Then in the %post section test if the value is greater than or equal to 
> 2 in which case you configure both interfaces. Or else configure one 
> interface:
> 
> %post
> # first get the nr of interfaces from the interface_count.txt file
> # afaik usually at the top of %post
> source /tmp/interface_count.txt
> 
> # test the value. not sure if I need to quote INTERFACES and 2
> if [ "\$INTERFACE_COUNT" -ge "2" ]; then
>    # configure 2 nics
>    network --device eth0 --bootproto dhcp
>    network --device eth1 --onboot no --bootproto dhcp
> else
>    # configure 1 nic
>    network --device eth0 --bootproto dhcp
> fi
> 
> Since I don't know awk I got the syntax from the excellent n00b-friendly 
> awk tutorial at 
> http://bashshell.net/stream-filtering-utilities/exercise-1-learning-awk-basics/
> 
> Hope this helps.
> 
> Regards,
> Patrick
> 
> _______________________________________________
> Kickstart-list mailing list
> Kickstart-list at redhat.com
> https://www.redhat.com/mailman/listinfo/kickstart-list






More information about the Kickstart-list mailing list