Discussion:
Looking for simple ifplugd example script
Bryan Evenson
2014-02-20 16:14:12 UTC
Permalink
I am using busybox-1.20.2 on an embedded Debian Linux machine, and I have configured Busybox to include ifplugd. I would like ifplugd to do the following:

1. When the Ethernet cable is plugged in, attempt DHCP on eth0
2. If DHCP fails, use a set static IP address
3. When the Ethernet cable is unplugged, unconfigure eth0

At this point, ifplugd is not running. After some poking around, I see that there is no default daemon script for ifplugd included with Busybox. I'm assuming a script to do the above isn't that difficult, but I can't seem to find any examples on how to do this. Does anyone have a simple example that I can go by? I did see the example in the Busybox source under examples/var_service/ifplugd_if, but I couldn't make any sense of what that script did and how to make use of it.

On a related note, I have eth0 setup in /etc/network/interfaces as:
auto eth0
iface eth0 inet dhcp

If I want to use a static IP address if DHCP fails, do I need to add something to /etc/network/interfaces, or is that something that would belong in the ifplugd script?

Thanks,
Bryan
ibid.ag
2014-02-20 22:05:11 UTC
Permalink
Post by Bryan Evenson
1. When the Ethernet cable is plugged in, attempt DHCP on eth0
2. If DHCP fails, use a set static IP address
3. When the Ethernet cable is unplugged, unconfigure eth0
At this point, ifplugd is not running. After some poking around,
I see that there is no default daemon script for ifplugd included
with Busybox. I'm assuming a script to do the above isn't that
difficult, but I can't seem to find any examples on how to do this.
Does anyone have a simple example that I can go by? I did see the
example in the Busybox source under examples/var_service/ifplugd_if,
but I couldn't make any sense of what that script did and how to make use of it.
DISCLAIMER: I haven't tried any of this, it's based on my reading of the
documentation.

I'm not sure what init system it's aimed at (runit?), but here's what I
can make out:

examples/var_service/ifplugd_if/run: starts ifplugd with ifplugd_handler
as the script.

examples/var_service/ifplugd_if/ifplugd_handler: ifplugd script; called
as
$SCRIPT <if0> up
$SCRIPT <if0> down

A script for that would be something like:
===begin /lib/busybox/ifplugd_script===
#!/bin/sh
IP="192.268.2.1"

case $2 in
up)
dhclient $1 || ifconfig $1 $IP
;;
down)
ifconfig $1 down
esac
===end /lib/busybox/ifplugd_script===

You may need a good bit more magic in there, and may want to change
the commands used.
And you'd start it with something like this commandline:

ifplugd -IfM -t 5 -i eth0 -r /lib/busybox/ifplugd_script
Post by Bryan Evenson
auto eth0
iface eth0 inet dhcp
If I want to use a static IP address if DHCP fails, do I need to add
something to /etc/network/interfaces, or is that something that would belong in the ifplugd script?
Thanks,
Bryan
I'm not sure what's the proper approach. I see notes that a static IP can be
configured via the DHCP client...

HTH,
Isaac Dunham
Denys Vlasenko
2014-02-20 22:28:18 UTC
Permalink
Post by Bryan Evenson
1. When the Ethernet cable is plugged in, attempt DHCP on eth0
2. If DHCP fails, use a set static IP address
3. When the Ethernet cable is unplugged, unconfigure eth0
At this point, ifplugd is not running. After some poking around, I see that there is no default daemon script for ifplugd included with Busybox. I'm assuming a script to do the above isn't that difficult, but I can't seem to find any examples on how to do this. Does anyone have a simple example that I can go by? I did see the example in the Busybox source under examples/var_service/ifplugd_if, but I couldn't make any sense of what that script did and how to make use of it.
It means that busybox/examples/var_service/README is badly located,
or badly written, or both.

Did you find it? If no, how this can e improved?
Please read it, and tell me where it is not clear.
Post by Bryan Evenson
auto eth0
iface eth0 inet dhcp
If I want to use a static IP address if DHCP fails, do I need to add something to /etc/network/interfaces, or is that something that would belong in the ifplugd script?
If you want sane networking in a non-trivial situation,
I think ifup isn't the tool.
Bryan Evenson
2014-02-21 10:39:58 UTC
Permalink
Denys,
-----Original Message-----
From: Denys Vlasenko [mailto:vda.linux at googlemail.com]
Sent: Thursday, February 20, 2014 5:28 PM
To: Bryan Evenson
Cc: busybox at busybox.net
Subject: Re: Looking for simple ifplugd example script
On Thu, Feb 20, 2014 at 5:14 PM, Bryan Evenson
Post by Bryan Evenson
I am using busybox-1.20.2 on an embedded Debian Linux machine, and I
have configured Busybox to include ifplugd. I would like ifplugd to do the
Post by Bryan Evenson
1. When the Ethernet cable is plugged in, attempt DHCP on eth0 2. If
DHCP fails, use a set static IP address 3. When the Ethernet cable is
unplugged, unconfigure eth0
At this point, ifplugd is not running. After some poking around, I see that
there is no default daemon script for ifplugd included with Busybox. I'm
assuming a script to do the above isn't that difficult, but I can't seem to find
any examples on how to do this. Does anyone have a simple example that I
can go by? I did see the example in the Busybox source under
examples/var_service/ifplugd_if, but I couldn't make any sense of what that
script did and how to make use of it.
It means that busybox/examples/var_service/README is badly located, or
badly written, or both.
I found the README you were talking about. I have never used runsvdir before and I don't have it included in my version of Busybox. I didn't realize that Busybox was assuming you'd use runsvdir to run ifplugd, which was the start of my confusion. And since I didn't know that's what was assumed, I didn't think to look a couple directories up for a related README.

A little more background on my system. It's an ARM family processor and I'm using the Yocto Project's Poky distribution on my system. The default image for that build uses sysVinit and is setup for ifup/ifdown. So if I wanted to use ifplugd, is the suggestion that I include runsvdir and runsv in my Busybox configuration and use it to run ifplugd? I'm unclear what runsvdir is doing that sysVinit can't.
Did you find it? If no, how this can e improved?
Please read it, and tell me where it is not clear.
Post by Bryan Evenson
auto eth0
iface eth0 inet dhcp
If I want to use a static IP address if DHCP fails, do I need to add something
to /etc/network/interfaces, or is that something that would belong in the
ifplugd script?
If you want sane networking in a non-trivial situation, I think ifup isn't the
tool.
If not ifup, what would you suggest? I'm open to ideas, just looking for the simplest path to get to where I want to go.

Thanks,
Bryan
Michael Conrad
2014-02-21 11:30:22 UTC
Permalink
Post by Bryan Evenson
It means that busybox/examples/var_service/README is badly located, or
badly written, or both.
I found the README you were talking about. I have never used runsvdir before and I don't have it included in my version of Busybox. I didn't realize that Busybox was assuming you'd use runsvdir to run ifplugd, which was the start of my confusion. And since I didn't know that's what was assumed, I didn't think to look a couple directories up for a related README.
A little more background on my system. It's an ARM family processor and I'm using the Yocto Project's Poky distribution on my system. The default image for that build uses sysVinit and is setup for ifup/ifdown.
Every distro has a different take on how networking should be handled.
It isn't practical for busybox to have networking configuration examples
for every distro. If you want to make use of ifplugd, you first need to
understand the design and layout of your distro. The example configs
are only meant to help you understand how the tool works. That said,
Denys was asking what could be done to the docs to help you on this path.
Post by Bryan Evenson
So if I wanted to use ifplugd, is the suggestion that I include runsvdir and runsv in my Busybox configuration and use it to run ifplugd? I'm unclear what runsvdir is doing that sysVinit can't.
If you want sane networking in a non-trivial situation, I think ifup isn't the
tool.
If not ifup, what would you suggest? I'm open to ideas, just looking for the simplest path to get to where I want to go.
While busybox clones many classic tools, there is a sentiment among many
developers and users that some of these classic tools suffer from design
flaws and shouldn't be used.

IMHO your best bet for a quick solution is to stick with your distro's
way of doing things. But if you have the time for the learning curve,
the best way to get rock-solid reliability from your system is to toss
anything having to do with ifup/ifdown or sysvinit or init scripts,
learn the actual commands and daemons needed for your system to
function, and put these bare essentials into runit-style scripts. But
this is an opinion.
Denys Vlasenko
2014-02-21 11:56:07 UTC
Permalink
Post by Bryan Evenson
Post by Bryan Evenson
Post by Bryan Evenson
At this point, ifplugd is not running. After some poking around, I see that
there is no default daemon script for ifplugd included with Busybox. I'm
assuming a script to do the above isn't that difficult, but I can't seem to find
any examples on how to do this. Does anyone have a simple example that I
can go by? I did see the example in the Busybox source under
examples/var_service/ifplugd_if, but I couldn't make any sense of what that
script did and how to make use of it.
It means that busybox/examples/var_service/README is badly located, or
badly written, or both.
I found the README you were talking about. I have never used runsvdir before and I don't have it included in my version of Busybox. I didn't realize that Busybox was assuming you'd use runsvdir to run ifplugd,
We don't pull Lenart Pottering here.
Busybox does not assume or require you to use runsvdir with ifplugd.
You can use ifplugd with or without runsvdir.

ifplugd is written in a way that it's *you* who decide what to do
when link goes up or down (by the way of writing desired
actions in your script).
Post by Bryan Evenson
which was the start of my confusion. And since I didn't know that's what was assumed, I didn't think to look a couple directories up for a related README.
How about this fix (adding another README)?

$ cat examples/var_service/ifplugd_if/README
The real README file is one directory up.
Post by Bryan Evenson
A little more background on my system. It's an ARM family processor and I'm using the Yocto Project's Poky distribution on my system. The default image for that build uses sysVinit and is setup for ifup/ifdown.
So if I wanted to use ifplugd, is the suggestion that I include runsvdir and runsv in my Busybox configuration and use it to run ifplugd?
If you want to use ifplugd, use it.
Namely, decide where to start it, and what to put into
its helper script.
Post by Bryan Evenson
I'm unclear what runsvdir is doing that sysVinit can't.
There are plenty of stuff about daemontools and runsvdir on Google.

A very short synopsis: runsvdir babysits processes just like
sysVinit does for getties / X / whatever, but in a better way:
you can add/remove/start/stop these babysitted processes
without sending signals or editing text config file.
Post by Bryan Evenson
If not ifup, what would you suggest? I'm open to ideas,
just looking for the simplest path to get to where I want to go.
The very simplest path wouldn't work: you can't design your networking
for a single wired ethernet with static address or DHCP, and then start
bolting on hacks to make it work with wifi, more than one interface,
vpn's, dynamic firewalling etc. It would be a horrifying mess.
I went through that path already.

Here is my current "best idea":

http://busybox.net/~vda/no_ifup.txt
Kevyn-Alexandre Paré
2014-02-21 14:59:17 UTC
Permalink
Denys,
Post by Denys Vlasenko
http://busybox.net/~vda/no_ifup.txt
thx for the link I'm also searching/learning something similar...

When you are saying:

"Treat appearance and disappearance of network interfaces
and links as asynchronous events. Whenever an interface
or link goes up or down, reconfigure network-related settings."

With busybox how do you do this? with mdev events?

thx,

-KA
Denys Vlasenko
2014-02-21 20:09:37 UTC
Permalink
On Fri, Feb 21, 2014 at 3:59 PM, Kevyn-Alexandre Par?
Post by Kevyn-Alexandre Paré
Post by Denys Vlasenko
http://busybox.net/~vda/no_ifup.txt
thx for the link I'm also searching/learning something similar...
"Treat appearance and disappearance of network interfaces
and links as asynchronous events. Whenever an interface
or link goes up or down, reconfigure network-related settings."
With busybox how do you do this? with mdev events?
It's device- and program-specific. For example, ethernet
controlled by udhcpc: when udhcpc gets the lease, it runs
its -s SCRIPT.

The act of running that script *is* a notification
that something has changed. According to no_ifup.txt "ideology",
the script should trigger network reconfiguration.

examples/var_service/* files demonstrate it.

The examples/var_service/fw/run file is a script which is
the network reconfiguration thing.

In examples/var_service/dhcp_if/run you see how udhcpc is run:
udhcpc -vv -s "$pwd/dhcp_handler"

In examples/var_service/dhcp_if/dhcp_handler you see how
the "fw" service gets restarted every time lease is obtained
or lost. You see how before doing that, dhcp_handler saves
network data (IP/router/etc) so that "fw" can get at it.

examples/var_service/ifplugd_if is another variation of the same:
it's a tool which detects changes in network topology -
in this case, link disappearing because cable was unplugged -
and acting on it. (In this case, though, it proved better
to stop/start udhcpc rather than kicking fw - udhcpc
will do that for us, but it also will get IP first).

Questions?
Bryan Evenson
2014-02-21 16:43:30 UTC
Permalink
Denys,
-----Original Message-----
From: Denys Vlasenko [mailto:vda.linux at googlemail.com]
Sent: Friday, February 21, 2014 6:56 AM
To: Bryan Evenson
Cc: busybox at busybox.net
Subject: Re: Looking for simple ifplugd example script
On Fri, Feb 21, 2014 at 11:39 AM, Bryan Evenson
Post by Bryan Evenson
Post by Bryan Evenson
Post by Bryan Evenson
At this point, ifplugd is not running. After some poking around, I see that
there is no default daemon script for ifplugd included with Busybox.
I'm assuming a script to do the above isn't that difficult, but I
can't seem to find any examples on how to do this. Does anyone have
a simple example that I can go by? I did see the example in the
Busybox source under examples/var_service/ifplugd_if, but I couldn't
make any sense of what that script did and how to make use of it.
It means that busybox/examples/var_service/README is badly located,
or badly written, or both.
I found the README you were talking about. I have never used runsvdir
before and I don't have it included in my version of Busybox. I
didn't realize that Busybox was assuming you'd use runsvdir to run
ifplugd,
We don't pull Lenart Pottering here.
Busybox does not assume or require you to use runsvdir with ifplugd.
You can use ifplugd with or without runsvdir.
ifplugd is written in a way that it's *you* who decide what to do when link
goes up or down (by the way of writing desired actions in your script).
Post by Bryan Evenson
which was the start of my confusion. And since I didn't know that's what
was assumed, I didn't think to look a couple directories up for a related
README.
How about this fix (adding another README)?
$ cat examples/var_service/ifplugd_if/README
The real README file is one directory up.
A copy of examples/var_service/nmeter/README in examples/var_service/ifplugd_if/ would be helpful. In my searches for examples on how to use ifplugd, everything I found said "see /etc/default/ifplugd for example usage", which I didn't have. I'm unfamiliar with runsvdir, so I didn't know "run" was the file that called ifplugd. So the example in run is as follows:

ifplugd -apqns -t3 -u8 -d8 -i "$if" -r "$pwd/ifplugd_handler"

I'm trying to get a better understanding as to what this command does and how it'd apply to my setup if I wanted to use it in an init script with sysVinit. Following the setup from the other init scripts in my system, I would be using start-stop-daemon to start ifplugd. And as far as right now, forget what I'd mentioned before about the static IP address; first step is just getting the system to re-attempt DHCP when someone plugs in the Ethernet cable.

Based on how I'm assuming I would need to call ifplugd from a sysVinit script, here is the script I have as a starting point:

#! /bin/sh
#
# ifplugd init.d script
test -x /usr/bin/ifplugd || exit 0

#Functions to do individual actions
startdaemon(){
# Start the application
echo -n "Starting ifplugd: "
start-stop-daemon --start --quiet --background --exec /usr/bin/ifplugd -- "-apq -t3 -u8 -d8 -i eth0 -r /etc/udhcpc.d/50default"
echo "done"
}
stopdaemon(){
echo -n "Stopping ifplugd: "
start-stop-daemon --stop --quiet --exec /usr/bin/ifplugd
echo "done"
}

case "$1" in
start)
startdaemon
;;
stop)
stopdaemon
;;
restart|force-reload)
stopdaemon
sleep 2
startdaemon
;;
*)
echo "Usage: /etc/init.d/ifplugd { start | stop | restart | force-reload }" >&2
exit 1
;;
esac

exit 0

Basically, start ifplugd and let it call udhcpc if ifplugd detects a change in the link. My startup says that its calling this script, and I can call "/etc/init.d/ifplugd start" on the command line and I don't see any complaints. However, I don't see ifplugd in my running process list; for some reason the daemon isn't running (or, maybe it starts but fails for some reason). I also don't see any message related to ifplugd in my system log. Are there some arguments I got wrong in the call to ifplugd?
Post by Bryan Evenson
A little more background on my system. It's an ARM family processor and
I'm using the Yocto Project's Poky distribution on my system. The default
image for that build uses sysVinit and is setup for ifup/ifdown.
Post by Bryan Evenson
So if I wanted to use ifplugd, is the suggestion that I include runsvdir and
runsv in my Busybox configuration and use it to run ifplugd?
If you want to use ifplugd, use it.
Namely, decide where to start it, and what to put into its helper script.
Post by Bryan Evenson
I'm unclear what runsvdir is doing that sysVinit can't.
There are plenty of stuff about daemontools and runsvdir on Google.
A very short synopsis: runsvdir babysits processes just like sysVinit does for
you can add/remove/start/stop these babysitted processes without sending
signals or editing text config file.
Post by Bryan Evenson
If not ifup, what would you suggest? I'm open to ideas, just looking
for the simplest path to get to where I want to go.
The very simplest path wouldn't work: you can't design your networking for a
single wired ethernet with static address or DHCP, and then start bolting on
hacks to make it work with wifi, more than one interface, vpn's, dynamic
firewalling etc. It would be a horrifying mess.
I went through that path already.
Agreed, trying to do all that with either a static IP address or with DHCP would be a horrifying mess. I'm not looking for something that complicated. We have one Ethernet port on our board, and there are instances that we would want to direct connect a laptop to the board for pulling data off of the board, etc. My thought would be the easiest way to do that would be to just have it use a known static IP address if DHCP failed. We do have a secondary static IP address right now that is live all the time at eth0:0, but we want to get away from that for some of the reasons you mentioned.

So back to my original question: assuming I use ifplugd in a sysVinit for automatically starting DHCP when someone plugs in the Ethernet cable (and I get that working), is there a good way with ifplugd (or udhcpc for that matter) to set a static IP address if DHCP fails?

Thanks,
Bryan
http://busybox.net/~vda/no_ifup.txt
Denys Vlasenko
2014-02-21 20:20:56 UTC
Permalink
Post by Bryan Evenson
ifplugd -apqns -t3 -u8 -d8 -i "$if" -r "$pwd/ifplugd_handler"
I'm trying to get a better understanding as to what this command does and how it'd apply to my setup if I wanted to use it in an init script with sysVinit. Following the setup from the other init scripts in my system, I would be using start-stop-daemon to start ifplugd. And as far as right now, forget what I'd mentioned before about the static IP address; first step is just getting the system to re-attempt DHCP when someone plugs in the Ethernet cable.
#! /bin/sh
#
# ifplugd init.d script
test -x /usr/bin/ifplugd || exit 0
#Functions to do individual actions
startdaemon(){
# Start the application
echo -n "Starting ifplugd: "
start-stop-daemon --start --quiet --background --exec /usr/bin/ifplugd -- "-apq -t3 -u8 -d8 -i eth0 -r /etc/udhcpc.d/50default"
Not that direct. /etc/udhcpc.d/50default is probably not expecting
to be used like that.

See examples/var_service/ifplugd_if/ifplugd_handler. It says:

#!/bin/sh
# parameters:
# $1: interface
# $2: state
if test -d "/var/service/dhcp_$1"; then
if test x"$2" = x"down"; then
...what to do if link disappeared...
fi
if test x"$2" = x"up"; then
...what to do if link appeared (or was 'on' at start)...
fi
fi

Decide what needs to be in those two if blocks for your system.
Do you need to run /etc/udhcpc.d/50default? In both cases?
With some params? (E.g. how do you tell your system
"restart udhcpc for interface $1"? Your current script
clearly fails to take into account interface name).
Post by Bryan Evenson
Basically, start ifplugd and let it call udhcpc if ifplugd detects a change in the link. My startup says that its calling this script, and I can call "/etc/init.d/ifplugd start" on the command line and I don't see any complaints.
ifplugd won't call its script with $1="start".
It runs its script with $1="ethN" and $2="up"/"down".
You need a shim to translate that into "/etc/init.d/ifplugd start"
and (I'm guessing) "/etc/init.d/ifplugd stop"
Post by Bryan Evenson
However, I don't see ifplugd in my running process list; for some reason the daemon isn't running (or, maybe it starts but fails for some reason). I also don't see any message related to ifplugd in my system log. Are there some arguments I got wrong in the call to ifplugd?
Try to run in from command line, and see what it says.
Post by Bryan Evenson
Post by Bryan Evenson
If not ifup, what would you suggest? I'm open to ideas, just looking
for the simplest path to get to where I want to go.
The very simplest path wouldn't work: you can't design your networking for a
single wired ethernet with static address or DHCP, and then start bolting on
hacks to make it work with wifi, more than one interface, vpn's, dynamic
firewalling etc. It would be a horrifying mess.
I went through that path already.
Agreed, trying to do all that with either a static IP address or with DHCP would be a horrifying mess. I'm not looking for something that complicated. We have one Ethernet port on our board, and there are instances that we would want to direct connect a laptop to the board for pulling data off of the board, etc. My thought would be the easiest way to do that would be to just have it use a known static IP address if DHCP failed. We do have a secondary static IP address right now that is live all the time at eth0:0, but we want to get away from that for some of the reasons you mentioned.
So back to my original question: assuming I use ifplugd in a sysVinit for automatically starting DHCP when someone plugs in the Ethernet cable (and I get that working), is there a good way with ifplugd (or udhcpc for that matter) to set a static IP address if DHCP fails?
udhcpc runs its -s SCRIPT with $1="leasefail" when it fails to get a lease.
You can hook on that.

Loading...