Monday, 26 August 2013

RHEL. Network post up scripts

Issue

  • How do I run a script or program immediately after my network interface goes up?

Environment

  • Red Hat Enterprise Linux 4, 5, 6

Resolution

  • Create file /sbin/ifup-local and add the commands to run after the network is up.
An example for one physical interface:
#!/bin/bash
if [ "$1" == "eth0" ]; then
  /sbin/ethtool -G eth0 rx 4096 tx 4096
fi
For bonding and bridging, you need to reference the virtual device being started, not the underlying physical devices:
#!/bin/bash
if [ "$1" == "br0" ]; then
  /sbin/ethtool -G eth0 rx 4096 tx 4096
fi
#!/bin/bash
if [ "$1" == "bond0" ]; then
  /sbin/ethtool -G eth0 rx 4096 tx 4096
  /sbin/ethtool -G eth1 rx 4096 tx 4096
fi
  • Make /sbin/ifup-local executable
chmod +x /sbin/ifup-local

Root Cause

The network control scripts in /etc/sysconfig/network-scripts allows for adding scripts that will run after the device is up.
For example, looking at the /etc/sysconfig/network-scripts/ifup-eth file, the last few lines are:
...
        echo ${dhcp6_pid[0]} > /var/run/dhcp6c_${DEVICE}.pid
    fi;
fi

if [ "${IPX}" = yes ]; then
       /etc/sysconfig/network-scripts/ifup-ipx ${DEVICE}
fi

exec /etc/sysconfig/network-scripts/ifup-post ${CONFIG} ${2}
A script /etc/sysconfig/network-scripts/ifup-post is the last item to be executed. Looking at that script:
...
if [ -x /sbin/ifup-local ]; then
    /sbin/ifup-local ${DEVICE}
fi

exit 0
Notice the last few lines search for a file called /sbin/ifup-local. If that file is found, it is executed.
The file /sbin/ifup-local is the one that is available to run programs immediately after the interface is up.

No comments:

Post a Comment