diff mbox

[v4] Allow a single DHCP configuration via the system configuration submenu

Message ID 1421413719-10383-1-git-send-email-jeremy.rosen@openwide.fr
State Accepted
Headers show

Commit Message

Jeremy Rosen Jan. 16, 2015, 1:08 p.m. UTC
This patch allows the setup of simple a single interface to be
automatically brought up and configured via DHCP on system startup.

The interface name can be set via a configuration option. This patch
does not support systemd-networkd, any complex network configuration should
be done via overlay of /etc/network/interfaces or the relevant networkd
configuration file

Signed-off-by: Jérémy Rosen <jeremy.rosen@openwide.fr>

---
v4 : - changed condition to also depend on ifupdown or busybox
       suggestion by Gustavo Zacarias

v3 : - move the condition from shell logic to make logic
       (reusing the code proposed by Thomas Petazzoni)

v2 : - inline shell section instead of providing a script
     - add one Config.in option instead of two
---
 system/Config.in                       | 21 +++++++++++++++++++++
 system/skeleton/etc/network/interfaces |  4 ----
 system/system.mk                       | 25 +++++++++++++++++++++++++
 3 files changed, 46 insertions(+), 4 deletions(-)
 delete mode 100644 system/skeleton/etc/network/interfaces

Comments

Thomas Petazzoni Jan. 16, 2015, 3:19 p.m. UTC | #1
Dear Jérémy Rosen,

On Fri, 16 Jan 2015 14:08:39 +0100, Jérémy Rosen wrote:

> +config BR2_SIMPLE_DHCP
> +	string "Name of the physical network interface to run DHCP on"
> +	default ""
> +	depends on !BR2_PACKAGE_SYSTEMD_NETWORKD && (BR2_PACKAGE_BUSYBOX || BR2_PACKAGE_IFUPDOWN)
> +	help
> +	  The name of the network interface to configure automatically.
> +	  A DHCP request will automatically happen on startup on the selected
> +	  interface.
> +
> +	  If left empty, no automatic DHCP requests will take place.
> +
> +	  For more complicated network setups use an overlay to overwrite
> +	  /etc/network/interfaces or add a networkd configuration file.
> +
> +comment "automatic network configuration via DHCP is not compatible with networkd"
> +	depends on BR2_PACKAGE_SYSTEMD_NETWORKD
> +
> +comment "automatic network configuration via DHCP needs ifupdown or busybox"
> +	depends on !(BR2_PACKAGE_BUSYBOX || BR2_PACKAGE_IFUPDOWN)

I'm still not really happy with this. It's
really /etc/network/interfaces as a whole that doesn't make sense in a
context where you don't have Busybox ifupdown, or the full blown
ifupdown, no? Not just the DHCP part of it.

Thomas
Jeremy Rosen Jan. 16, 2015, 3:30 p.m. UTC | #2
> Dear Jérémy Rosen,
> 
> On Fri, 16 Jan 2015 14:08:39 +0100, Jérémy Rosen wrote:
> 
> > +config BR2_SIMPLE_DHCP
> > +	string "Name of the physical network interface to run DHCP on"
> > +	default ""
> > +	depends on !BR2_PACKAGE_SYSTEMD_NETWORKD && (BR2_PACKAGE_BUSYBOX
> > || BR2_PACKAGE_IFUPDOWN)
> > +	help
> > +	  The name of the network interface to configure automatically.
> > +	  A DHCP request will automatically happen on startup on the
> > selected
> > +	  interface.
> > +
> > +	  If left empty, no automatic DHCP requests will take place.
> > +
> > +	  For more complicated network setups use an overlay to overwrite
> > +	  /etc/network/interfaces or add a networkd configuration file.
> > +
> > +comment "automatic network configuration via DHCP is not
> > compatible with networkd"
> > +	depends on BR2_PACKAGE_SYSTEMD_NETWORKD
> > +
> > +comment "automatic network configuration via DHCP needs ifupdown
> > or busybox"
> > +	depends on !(BR2_PACKAGE_BUSYBOX || BR2_PACKAGE_IFUPDOWN)
> 
> I'm still not really happy with this. It's
> really /etc/network/interfaces as a whole that doesn't make sense in
> a
> context where you don't have Busybox ifupdown, or the full blown
> ifupdown, no? Not just the DHCP part of it.
> 


i'm not sure how you want me to correct that...

* not generate network/interfaces at all if ifupdown is not available
* correct the comment sections "generation of /etc/network/interfaces needs  ifupdown"
  (but that does not reflect what the option is when it is available)
* something else

please tell me and i'll send a new patch


> Thomas
> --
> Thomas Petazzoni, CTO, Free Electrons
> Embedded Linux, Kernel and Android engineering
> http://free-electrons.com
>
Peter Korsgaard Feb. 2, 2015, 4:36 p.m. UTC | #3
>>>>> "Jérémy" == Jérémy Rosen <jeremy.rosen@openwide.fr> writes:

Hi,

 > This patch allows the setup of simple a single interface to be
 > automatically brought up and configured via DHCP on system startup.

 > The interface name can be set via a configuration option. This patch
 > does not support systemd-networkd, any complex network configuration should
 > be done via overlay of /etc/network/interfaces or the relevant networkd
 > configuration file

 > Signed-off-by: Jérémy Rosen <jeremy.rosen@openwide.fr>

 > ---
 > v4 : - changed condition to also depend on ifupdown or busybox
 >        suggestion by Gustavo Zacarias

Sorry for the slow response. We have discussed this during the dev days
and agreed to include it. Committed with some changes, see below.

Thanks!

> +config BR2_SIMPLE_DHCP

We're not completely consist with this, but the new system options are
called BR2_SYSTEM_*, so I've renamed to the option.

> +	string "Name of the physical network interface to run DHCP on"

This is quite long, so I reworded it to:

"Network interface to configure through DHCP"

> +	default ""
 > +	depends on !BR2_PACKAGE_SYSTEMD_NETWORKD && (BR2_PACKAGE_BUSYBOX || BR2_PACKAGE_IFUPDOWN)
 > +	help
 > +	  The name of the network interface to configure automatically.
 > +	  A DHCP request will automatically happen on startup on the selected
 > +	  interface.

And here I reworded it a bit and mentioned an example (eth0).

 
 > +define SET_NETWORK_LOCALHOST
 > +        echo "# interface file auto-generated by buildroot" >  $(TARGET_DIR)/etc/network/interfaces
 > +        echo                                                >> $(TARGET_DIR)/etc/network/interfaces
 > +        echo "auto lo"                                      >> $(TARGET_DIR)/etc/network/interfaces
 > +        echo "iface lo inet loopback"                       >> $(TARGET_DIR)/etc/network/interfaces
 > +        echo                                                >> $(TARGET_DIR)/etc/network/interfaces
 > +endef

Elsewhere in BR we use syntax like:

( \
  echo foo ; \
  echo bar ; \
) > file

Which is a bit less verbose, so I've changed it to that.
Jeremy Rosen Feb. 2, 2015, 4:38 p.m. UTC | #4
> 
> Sorry for the slow response. We have discussed this during the dev
> days
> and agreed to include it. Committed with some changes, see below.
> 
> Thanks!


Thanks a lot, really appreciated :)

> 
> > +config BR2_SIMPLE_DHCP
> 
> We're not completely consist with this, but the new system options
> are
> called BR2_SYSTEM_*, so I've renamed to the option.
> 
> > +	string "Name of the physical network interface to run DHCP on"
> 
> This is quite long, so I reworded it to:
> 
> "Network interface to configure through DHCP"
> 
> > +	default ""
>  > +	depends on !BR2_PACKAGE_SYSTEMD_NETWORKD && (BR2_PACKAGE_BUSYBOX
>  > || BR2_PACKAGE_IFUPDOWN)
>  > +	help
>  > +	  The name of the network interface to configure automatically.
>  > +	  A DHCP request will automatically happen on startup on the
>  > selected
>  > +	  interface.
> 
> And here I reworded it a bit and mentioned an example (eth0).
> 
>  
>  > +define SET_NETWORK_LOCALHOST
>  > +        echo "# interface file auto-generated by buildroot" >
>  >  $(TARGET_DIR)/etc/network/interfaces
>  > +        echo                                                >>
>  > $(TARGET_DIR)/etc/network/interfaces
>  > +        echo "auto lo"                                      >>
>  > $(TARGET_DIR)/etc/network/interfaces
>  > +        echo "iface lo inet loopback"                       >>
>  > $(TARGET_DIR)/etc/network/interfaces
>  > +        echo                                                >>
>  > $(TARGET_DIR)/etc/network/interfaces
>  > +endef
> 
> Elsewhere in BR we use syntax like:
> 
> ( \
>   echo foo ; \
>   echo bar ; \
> ) > file
> 
> Which is a bit less verbose, so I've changed it to that.
> 
> --
> Bye, Peter Korsgaard
>
Kenton, Stephen M. Feb. 14, 2015, 4:18 p.m. UTC | #5
>>
>>> +config BR2_SIMPLE_DHCP
>>
>> We're not completely consist with this, but the new system options
>> are
>> called BR2_SYSTEM_*, so I've renamed to the option.
>>
On this topic, what about the following idea?

If the interface name is blank do not perform DHCP as currently
If the interface name is "*" then run something like this script
to automatically find and use the first network interface for DHCP
I'd like to be able to use the same BR config on slightly different
boards that may sometimes have different network interface names/orders
The kernel handles that OK with modules so maybe buildroot could to?

Steve


#!/bin/bash
interfaces="/tmp/interfaces" # The on-the-fly generated interfaces file
# This is only used for the BR2_SYSTEM_DHCP "simple single interface dhcp" option.
# Many systems only have a single network interface, so auto-find/use it by default.
# If there are in fact multiple network interfaces the first non-loop interface
# listed in /sys/class/net will be used as the default DHCP configured interface.
# As an aid to debugging on new systems all other interfaces found are added to the
# generated $interfaces file with DHCP enabled but only the first one is brought up.
# Run "ifup -a -i $interfaces" as an alternate to the default /etc/network/interfaces
#
if [ $# -gt 0 ]; then # be our own mapping script
	echo "default" # will always be the first interface
	exit 0
fi

first=""
echo "# Auto-generated by $(readlink -f $0)" >		$interfaces
echo "auto lo" >>					$interfaces
echo "iface lo inet loopback" >>			$interfaces
echo "" >>						$interfaces
for interface in $(ls --hide=lo* /sys/class/net); do
	if [ "$first" == "" ]; then
		first=$interface
		echo "auto $first" >>			$interfaces
	else
# This is only used to help troubleshoot any additional network interfaces later on
		echo "iface $interface inet dhcp" >>	$interfaces
	fi
	echo "" >>					$interfaces
done
# This is the only network interface that needs to be brought up for the simple case
# The mapping to default instead of using the physical inteface name is intentional
# so that all modifications and documentation will refer to the constant name "default"
echo "iface default inet dhcp" >>			$interfaces
echo "" >>						$interfaces
# Map the first physical network interface to the logical default interface
echo "mapping $first" >>				$interfaces
echo "	script $(readlink -f $0)" >>			$interfaces
# We don't need any map statements since it's always going to be "default"
Peter Korsgaard Feb. 15, 2015, 8:12 p.m. UTC | #6
>>>>> "Steve" == Steve Kenton <skenton@ou.edu> writes:

 >>> 
 >>>> +config BR2_SIMPLE_DHCP
 >>> 
 >>> We're not completely consist with this, but the new system options
 >>> are
 >>> called BR2_SYSTEM_*, so I've renamed to the option.
 >>> 
 > On this topic, what about the following idea?

 > If the interface name is blank do not perform DHCP as currently
 > If the interface name is "*" then run something like this script
 > to automatically find and use the first network interface for DHCP
 > I'd like to be able to use the same BR config on slightly different
 > boards that may sometimes have different network interface names/orders
 > The kernel handles that OK with modules so maybe buildroot could to?

So those boards have network interfaces not using eth%d names? That
sounds pretty uncommon to me. Another approach would simply be add an
init script to rename those to eth0 with nameif.
Kenton, Stephen M. Feb. 15, 2015, 10:04 p.m. UTC | #7
On 2/15/2015 2:12 PM, Peter Korsgaard wrote:
>>>>>> "Steve" == Steve Kenton<skenton@ou.edu>  writes:
>   >>>
>   >>>>  +config BR2_SIMPLE_DHCP
>   >>>
>   >>>  We're not completely consist with this, but the new system options
>   >>>  are
>   >>>  called BR2_SYSTEM_*, so I've renamed to the option.
>   >>>
>   >  On this topic, what about the following idea?
>
>   >  If the interface name is blank do not perform DHCP as currently
>   >  If the interface name is "*" then run something like this script
>   >  to automatically find and use the first network interface for DHCP
>   >  I'd like to be able to use the same BR config on slightly different
>   >  boards that may sometimes have different network interface names/orders
>   >  The kernel handles that OK with modules so maybe buildroot could to?
>
> So those boards have network interfaces not using eth%d names? That
> sounds pretty uncommon to me. Another approach would simply be add an
> init script to rename those to eth0 with nameif.
Yes, two I have to test right now are an Asrock and an ECS mini-ITX and 
on both
the kernel built with buildroot 2015.02-rc1 calls the network interface 
"sit0"

Given that the name(s) are available why not just use it/them instead of 
requiring the
entering of a configuration value for an interface name to use  
BR2_SIMPLE_DHCP?

Steve
>
Peter Korsgaard Feb. 15, 2015, 10:11 p.m. UTC | #8
>>>>> "Steve" == Steve Kenton <skenton@ou.edu> writes:

Hi,

>> So those boards have network interfaces not using eth%d names? That
 >> sounds pretty uncommon to me. Another approach would simply be add an
 >> init script to rename those to eth0 with nameif.

> Yes, two I have to test right now are an Asrock and an ECS mini-ITX
 > and on both
 > the kernel built with buildroot 2015.02-rc1 calls the network
 > interface "sit0"

Really? sit%d is normally for point-to-point tunnels, not for real hw
devices:

http://www.deepspace6.net/docs/iproute2tunnel-en.html
Kenton, Stephen M. Feb. 16, 2015, 3:06 a.m. UTC | #9
On 02/15/2015 04:11 PM, Peter Korsgaard wrote:
>>>>>> "Steve" == Steve Kenton <skenton@ou.edu> writes:
> 
> Hi,
> 
>>> So those boards have network interfaces not using eth%d names? That
>  >> sounds pretty uncommon to me. Another approach would simply be add an
>  >> init script to rename those to eth0 with nameif.
> 
>> Yes, two I have to test right now are an Asrock and an ECS mini-ITX
>  > and on both
>  > the kernel built with buildroot 2015.02-rc1 calls the network
>  > interface "sit0"
> 
> Really? sit%d is normally for point-to-point tunnels, not for real hw
> devices:
> 
> http://www.deepspace6.net/docs/iproute2tunnel-en.html
> 
I was not aware of that, but it certainly set off alarm bells in my mind.
I'm just now getting to the networking and I thought my problem was the names
being wrong. I guess I wrote a clever script to solve a problem that does not exist :-)

I don't know where "sit0" comes from but I see I'm missing a driver for my ethernet - oops!
I didn't look closely enough and just assumed they were the same two devices.

Sorry for the noise.

Steve


Here is what buildroot-2015.02-rc2 shows me with lspci -k and ifconfig -a:

03:00.0 Class 0200: 10ec:8168

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

sit0      Link encap:IPv6-in-IPv4
          NOARP  MTU:1480  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

And here is what Ubuntu 14.04 on the same hardware show me:

03:00.0 Ethernet controller: Realtek Semiconductor Co., Ltd. RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller (rev 06)
	Subsystem: ASRock Incorporation Motherboard (one of many)
	Kernel driver in use: r8169

eth1      Link encap:Ethernet  HWaddr bc:5f:f4:cb:1e:f6
          inet addr:192.168.1.2  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::be5f:f4ff:fecb:1ef6/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1288 errors:0 dropped:0 overruns:0 frame:0
          TX packets:783 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1472508 (1.4 MB)  TX bytes:67242 (67.2 KB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:286 errors:0 dropped:0 overruns:0 frame:0
          TX packets:286 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:23049 (23.0 KB)  TX bytes:23049 (23.0 KB)
Jeremy Rosen Feb. 16, 2015, 8:46 a.m. UTC | #10
----- Mail original -----
> >>>>> "Steve" == Steve Kenton <skenton@ou.edu> writes:
> 
>  >>> 
>  >>>> +config BR2_SIMPLE_DHCP
>  >>> 
>  >>> We're not completely consist with this, but the new system
>  >>> options
>  >>> are
>  >>> called BR2_SYSTEM_*, so I've renamed to the option.
>  >>> 
>  > On this topic, what about the following idea?
> 
>  > If the interface name is blank do not perform DHCP as currently
>  > If the interface name is "*" then run something like this script
>  > to automatically find and use the first network interface for DHCP
>  > I'd like to be able to use the same BR config on slightly
>  > different
>  > boards that may sometimes have different network interface
>  > names/orders
>  > The kernel handles that OK with modules so maybe buildroot could
>  > to?
> 
> So those boards have network interfaces not using eth%d names? That
> sounds pretty uncommon to me. Another approach would simply be add an
> init script to rename those to eth0 with nameif.
> 

there is the (still pretty uncommon) case of networkd that does interface
naming based on the physical layout and not the module loading order

interfaces are never named eth* in that case.



Regards

Jeremy Rosen
diff mbox

Patch

diff --git a/system/Config.in b/system/Config.in
index 4d7a4e3..71d8689 100644
--- a/system/Config.in
+++ b/system/Config.in
@@ -324,6 +324,27 @@  config BR2_TARGET_GENERIC_REMOUNT_ROOTFS_RW
 
 endif # BR2_ROOTFS_SKELETON_DEFAULT
 
+
+config BR2_SIMPLE_DHCP
+	string "Name of the physical network interface to run DHCP on"
+	default ""
+	depends on !BR2_PACKAGE_SYSTEMD_NETWORKD && (BR2_PACKAGE_BUSYBOX || BR2_PACKAGE_IFUPDOWN)
+	help
+	  The name of the network interface to configure automatically.
+	  A DHCP request will automatically happen on startup on the selected
+	  interface.
+
+	  If left empty, no automatic DHCP requests will take place.
+
+	  For more complicated network setups use an overlay to overwrite
+	  /etc/network/interfaces or add a networkd configuration file.
+
+comment "automatic network configuration via DHCP is not compatible with networkd"
+	depends on BR2_PACKAGE_SYSTEMD_NETWORKD
+
+comment "automatic network configuration via DHCP needs ifupdown or busybox"
+	depends on !(BR2_PACKAGE_BUSYBOX || BR2_PACKAGE_IFUPDOWN)
+
 config BR2_TARGET_TZ_INFO
 	bool "Install timezone info"
 	# No timezone for musl; only for uClibc or (e)glibc.
diff --git a/system/skeleton/etc/network/interfaces b/system/skeleton/etc/network/interfaces
deleted file mode 100644
index 218b82c..0000000
--- a/system/skeleton/etc/network/interfaces
+++ /dev/null
@@ -1,4 +0,0 @@ 
-# Configure Loopback
-auto lo
-iface lo inet loopback
-
diff --git a/system/system.mk b/system/system.mk
index e4a3160..96aa342 100644
--- a/system/system.mk
+++ b/system/system.mk
@@ -38,6 +38,31 @@  ifneq ($(TARGET_GENERIC_ROOT_PASSWD),)
 TARGETS += host-mkpasswd
 endif
 
+define SET_NETWORK_LOCALHOST
+        echo "# interface file auto-generated by buildroot" >  $(TARGET_DIR)/etc/network/interfaces
+        echo                                                >> $(TARGET_DIR)/etc/network/interfaces
+        echo "auto lo"                                      >> $(TARGET_DIR)/etc/network/interfaces
+        echo "iface lo inet loopback"                       >> $(TARGET_DIR)/etc/network/interfaces
+        echo                                                >> $(TARGET_DIR)/etc/network/interfaces
+endef
+
+NETWORK_DHCP_IFACE = $(call qstrip,$(BR2_SIMPLE_DHCP))
+
+ifneq ($(NETWORK_DHCP_IFACE),)
+define SET_NETWORK_DHCP
+        echo "auto $(NETWORK_DHCP_IFACE)"                   >> $(TARGET_DIR)/etc/network/interfaces
+        echo "iface $(NETWORK_DHCP_IFACE) inet dhcp"        >> $(TARGET_DIR)/etc/network/interfaces
+endef
+endif
+
+define SET_NETWORK
+        mkdir -p $(TARGET_DIR)/etc/network/
+        $(SET_NETWORK_LOCALHOST)
+        $(SET_NETWORK_DHCP)
+endef
+
+TARGET_FINALIZE_HOOKS += SET_NETWORK
+
 ifeq ($(BR2_ROOTFS_SKELETON_DEFAULT),y)
 
 define SYSTEM_ROOT_PASSWD