Execute command *before* interface is brought up

I wonder if there is a way to specify a command to be executed before an interface is brought up to do some work necessary for the bringing up of the interface to be successful.

I know about /etc/hotplug.d/iface/ but AFAIU, those scripts are executed after interface events, not before.

I also know about /etc/rc.d/ and /etc/rc.local but I am not certain that interface start ups are done serially, after initscripts or if interface start ups can happen in parallel with initscripts -- like some other init implementations.

Moreover, this is a command that I would want executed on every bring up of an interface, not just the one at boot time. I.e. on every ifup <interface>, so it needs a way to happen other than just with an initscript.

Is this possible?

Disable "bring up on boot", and do ifup when you're ready?

1 Like

But I don't really want this to be a manual operation and I don't want to have to (remember to) manually run the desired command prior to running ifup when I'm ready.

In that case, how about

  • removing the automatic start from the interface X, and
  • creating a hotplug that reacts to "wan" getting up and sets the needed parameters for interface X and does "ifup" for the interface X

Oh poo. I didn't get my edit in before another reply. :slightly_frowning_face:

Except that I would want the command executed on just a simple CLI ifup <interface> -- without having to remember the command and having to remember to execute it.

  1. Create directory /root/scripts

    mkdir -p /root/scripts
    
  2. Create script /root/scripts/ifup with:

    #!/bin/sh
    do_stuff_before_ifup $1
    /sbin/ifup $1
    
  3. Make script executable with chmod +x /root/scripts/ifup

Now you just have to make it so invoking ifup calls the one in /root/scripts instead of the one in /sbin. One way is to add /root/scripts to PATH before the default entries:

  1. Create directory /etc/profile.d

    mkdir -p /etc/profile.d
    
  2. Create script /etc/profile.d/setpath with:

    export PATH="/root/scripts:$PATH"
    
  3. Log out and log in again

Another way is to move /sbin/ifup to /sbin/ifup.real or something like that and make /sbin/ifup a symlink to /root/scripts/ifup. Then have your script call /sbin/ifup.real instead. This is a rather invasive change, so I prefer the PATH method.

Except that I would want the command executed on just a simple CLI ifup <interface> -- without having to remember the command and having to remember to execute it.

Edit /sbin/ifup, catch the interface with if, do your stuff and let it run. This method is very invasiv and do not work for upgrades.

One option is to define a special protocol for that interface type, and place a suitable netifd init script for that protocol into /lib/netifd/proto/ . That script should then make the necessary modifications at the ifup action when netifd calls that script.

(I haven't done that myself, but it should be doable)

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.