diff mbox

Allow a single DHCP configuration via the system configuration submenu

Message ID 1418832946-28337-1-git-send-email-jeremy.rosen@openwide.fr
State Superseded
Headers show

Commit Message

Jeremy Rosen Dec. 17, 2014, 4:15 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>
---
 support/scripts/generate-network-config.sh | 24 ++++++++++++++++++++++++
 system/Config.in                           | 18 ++++++++++++++++++
 system/skeleton/etc/network/interfaces     |  4 ----
 system/system.mk                           |  5 +++++
 4 files changed, 47 insertions(+), 4 deletions(-)
 create mode 100755 support/scripts/generate-network-config.sh
 delete mode 100644 system/skeleton/etc/network/interfaces

Comments

Samuel Martin Dec. 17, 2014, 4:30 p.m. UTC | #1
Hi Jeremy,

On Wed, Dec 17, 2014 at 5:15 PM, Jérémy Rosen <jeremy.rosen@openwide.fr> wrote:
> 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>
> ---
>  support/scripts/generate-network-config.sh | 24 ++++++++++++++++++++++++
>  system/Config.in                           | 18 ++++++++++++++++++
>  system/skeleton/etc/network/interfaces     |  4 ----
>  system/system.mk                           |  5 +++++
>  4 files changed, 47 insertions(+), 4 deletions(-)
>  create mode 100755 support/scripts/generate-network-config.sh
>  delete mode 100644 system/skeleton/etc/network/interfaces
>
> diff --git a/support/scripts/generate-network-config.sh b/support/scripts/generate-network-config.sh
> new file mode 100755
> index 0000000..92f8d16
> --- /dev/null
> +++ b/support/scripts/generate-network-config.sh
> @@ -0,0 +1,24 @@
> +#!/bin/sh
> +
> +export BR2_SIMPLE_DHCP=$(sed -r -e "/^BR2_SIMPLE_DHCP=(.*)$/!d;" -e 's//\1/;' $BR2_CONFIG)
> +
> +export BR2_SIMPLE_DHCP_INTERFACE="$(sed -r -e "/^BR2_SIMPLE_DHCP_INTERFACE=\"(.*)\"$/!d;" -e 's//\1/;' $BR2_CONFIG)"
> +
> +do_generate_interfaces ()
> +{
> +       echo "# interface file auto-generated by buildroot"
> +       echo
> +       echo "auto lo"
> +       echo "iface lo inet loopback"
> +       echo
> +
> +       if [ -z "$BR2_SIMPLE_DHCP" ] ; then
> +               return
> +        fi
> +
> +       echo "auto $BR2_SIMPLE_DHCP_INTERFACE"
> +       echo "iface $BR2_SIMPLE_DHCP_INTERFACE inet dhcp"
> +}
> +
> +mkdir -p $TARGET_DIR/etc/network/
> +do_generate_interfaces > $TARGET_DIR/etc/network/interfaces
> diff --git a/system/Config.in b/system/Config.in
> index a3b7aff..10a81e9 100644
> --- a/system/Config.in
> +++ b/system/Config.in
> @@ -324,6 +324,24 @@ config BR2_TARGET_GENERIC_REMOUNT_ROOTFS_RW
>
>  endif # BR2_ROOTFS_SKELETON_DEFAULT
>
> +config BR2_SIMPLE_DHCP
> +       bool "automatic network configuration via DHCP"
> +       default n
> +       depends on !BR2_PACKAGE_SYSTEMD_NETWORKD
> +       help
> +         Automatically do a DHCP request on startup on selected interface at
> +         startup. For more complicated setups use an overlay.
> +
> +comment "automatic network configuration via DHCP is not compatible with networkd"
> +       depends on BR2_PACKAGE_SYSTEMD_NETWORKD

IIRC, comment does not support help message, but it could be
worthwhile to document somewhere how to do this in the systemd case,
without needing reading the git log ;-)

> +
> +config BR2_SIMPLE_DHCP_INTERFACE
> +       string "name of the physical network interface to run DHCP on"
> +       depends on BR2_SIMPLE_DHCP
> +       default eth0
> +       help
> +         The name of the network interface to configure automatically
> +
>  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..bb933f6 100644
> --- a/system/system.mk
> +++ b/system/system.mk
> @@ -38,6 +38,11 @@ ifneq ($(TARGET_GENERIC_ROOT_PASSWD),)
>  TARGETS += host-mkpasswd
>  endif
>
> +define SIMPLE_NETWORK
> +       $(TOPDIR)/support/scripts/generate-network-config.sh $(TARGET_DIR)
> +endef
> +TARGET_FINALIZE_HOOKS += SIMPLE_NETWORK
> +
>  ifeq ($(BR2_ROOTFS_SKELETON_DEFAULT),y)
>
>  define SYSTEM_ROOT_PASSWD
> --
> 2.1.3
>
> _______________________________________________
> buildroot mailing list
> buildroot@busybox.net
> http://lists.busybox.net/mailman/listinfo/buildroot

Regards,
Jeremy Rosen Dec. 17, 2014, 4:37 p.m. UTC | #2
Hey Samuel


----- Mail original -----
> Hi Jeremy,
> 
> On Wed, Dec 17, 2014 at 5:15 PM, Jérémy Rosen
> <jeremy.rosen@openwide.fr> wrote:
> > 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>
> > ---
> >  support/scripts/generate-network-config.sh | 24
> >  ++++++++++++++++++++++++
> >  system/Config.in                           | 18 ++++++++++++++++++
> >  system/skeleton/etc/network/interfaces     |  4 ----
> >  system/system.mk                           |  5 +++++
> >  4 files changed, 47 insertions(+), 4 deletions(-)
> >  create mode 100755 support/scripts/generate-network-config.sh
> >  delete mode 100644 system/skeleton/etc/network/interfaces
> >
> > diff --git a/support/scripts/generate-network-config.sh
> > b/support/scripts/generate-network-config.sh
> > new file mode 100755
> > index 0000000..92f8d16
> > --- /dev/null
> > +++ b/support/scripts/generate-network-config.sh
> > @@ -0,0 +1,24 @@
> > +#!/bin/sh
> > +
> > +export BR2_SIMPLE_DHCP=$(sed -r -e "/^BR2_SIMPLE_DHCP=(.*)$/!d;"
> > -e 's//\1/;' $BR2_CONFIG)
> > +
> > +export BR2_SIMPLE_DHCP_INTERFACE="$(sed -r -e
> > "/^BR2_SIMPLE_DHCP_INTERFACE=\"(.*)\"$/!d;" -e 's//\1/;'
> > $BR2_CONFIG)"
> > +
> > +do_generate_interfaces ()
> > +{
> > +       echo "# interface file auto-generated by buildroot"
> > +       echo
> > +       echo "auto lo"
> > +       echo "iface lo inet loopback"
> > +       echo
> > +
> > +       if [ -z "$BR2_SIMPLE_DHCP" ] ; then
> > +               return
> > +        fi
> > +
> > +       echo "auto $BR2_SIMPLE_DHCP_INTERFACE"
> > +       echo "iface $BR2_SIMPLE_DHCP_INTERFACE inet dhcp"
> > +}
> > +
> > +mkdir -p $TARGET_DIR/etc/network/
> > +do_generate_interfaces > $TARGET_DIR/etc/network/interfaces
> > diff --git a/system/Config.in b/system/Config.in
> > index a3b7aff..10a81e9 100644
> > --- a/system/Config.in
> > +++ b/system/Config.in
> > @@ -324,6 +324,24 @@ config BR2_TARGET_GENERIC_REMOUNT_ROOTFS_RW
> >
> >  endif # BR2_ROOTFS_SKELETON_DEFAULT
> >
> > +config BR2_SIMPLE_DHCP
> > +       bool "automatic network configuration via DHCP"
> > +       default n
> > +       depends on !BR2_PACKAGE_SYSTEMD_NETWORKD
> > +       help
> > +         Automatically do a DHCP request on startup on selected
> > interface at
> > +         startup. For more complicated setups use an overlay.
> > +
> > +comment "automatic network configuration via DHCP is not
> > compatible with networkd"
> > +       depends on BR2_PACKAGE_SYSTEMD_NETWORKD
> 
> IIRC, comment does not support help message, but it could be
> worthwhile to document somewhere how to do this in the systemd case,
> without needing reading the git log ;-)
> 

well, the answer is "the usual way, use overlays" and "read the man
page for networkd".

systemd in general has pretty good set of docs, and we don't document
most of the ways to customize your image. BR users are supposed to
know system configuration (we don't document /etc/network/interfaces)

so I personally think it's not really needed, but if you think it's
really necessary and you tell me where to add a note, I can write
something... I have a good understanding of networkd with my previous
attempt at network configuration


> > +
> > +config BR2_SIMPLE_DHCP_INTERFACE
> > +       string "name of the physical network interface to run DHCP
> > on"
> > +       depends on BR2_SIMPLE_DHCP
> > +       default eth0
> > +       help
> > +         The name of the network interface to configure
> > automatically
> > +
> >  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..bb933f6 100644
> > --- a/system/system.mk
> > +++ b/system/system.mk
> > @@ -38,6 +38,11 @@ ifneq ($(TARGET_GENERIC_ROOT_PASSWD),)
> >  TARGETS += host-mkpasswd
> >  endif
> >
> > +define SIMPLE_NETWORK
> > +       $(TOPDIR)/support/scripts/generate-network-config.sh
> > $(TARGET_DIR)
> > +endef
> > +TARGET_FINALIZE_HOOKS += SIMPLE_NETWORK
> > +
> >  ifeq ($(BR2_ROOTFS_SKELETON_DEFAULT),y)
> >
> >  define SYSTEM_ROOT_PASSWD
> > --
> > 2.1.3
> >
> > _______________________________________________
> > buildroot mailing list
> > buildroot@busybox.net
> > http://lists.busybox.net/mailman/listinfo/buildroot
> 
> Regards,
> 
> --
> Samuel
>
Thomas Petazzoni Dec. 17, 2014, 8:29 p.m. UTC | #3
Dear Jérémy Rosen,

On Wed, 17 Dec 2014 17:15:46 +0100, Jérémy Rosen wrote:

>  support/scripts/generate-network-config.sh | 24 ++++++++++++++++++++++++

Do we really need a helper script to do this?

NETWORK_IFACES = $(TARGET_DIR)/etc/network/interfaces
NETWORK_DHCP_IFACE = $(call qstrip,$(BR2_SIMPLE_DHCP_INTERFACE))

define NETWORK_ADD_DHCP_INTERFACE
	echo "auto $(NETWORK_DHCP_IFACE)" >> $(NETWORK_IFACES)
	echo "iface $(NETWORK_DHCP_IFACE) inet dhcp" >> $(NETWORK_IFACES)
endef

define NETWORK_CREATE_IFACES_FILE
	rm -f $(NETWORK_IFACES)
	echo "auto lo" >> $(NETWORK_IFACES)
	echo "iface lo inet loopback" >> $(NETWORK_IFACES)
	$(NETWORK_ADD_DHCP_INTERFACE)
endef

TARGET_FINALIZE_HOOKS += NETWORK_CREATE_IFACES_FILE

> +config BR2_SIMPLE_DHCP
> +	bool "automatic network configuration via DHCP"
> +	default n
> +	depends on !BR2_PACKAGE_SYSTEMD_NETWORKD
> +	help
> +	  Automatically do a DHCP request on startup on selected interface at 
> +	  startup. For more complicated setups use an overlay.
> +
> +comment "automatic network configuration via DHCP is not compatible with networkd"
> +	depends on BR2_PACKAGE_SYSTEMD_NETWORKD
> +
> +config BR2_SIMPLE_DHCP_INTERFACE
> +	string "name of the physical network interface to run DHCP on"
> +	depends on BR2_SIMPLE_DHCP
> +	default eth0
> +	help
> +	  The name of the network interface to configure automatically

Why two options? Just make the BR2_SIMPLE_DHCP_INTERFACE empty by
default (does nothing), and when non-empty, used as the DHCP network
interface.

Best regards,

Thomas
Jeremy Rosen Dec. 18, 2014, 8:04 a.m. UTC | #4
----- Mail original -----
> Dear Jérémy Rosen,
> 
> On Wed, 17 Dec 2014 17:15:46 +0100, Jérémy Rosen wrote:
> 
> >  support/scripts/generate-network-config.sh | 24
> >  ++++++++++++++++++++++++
> 
> Do we really need a helper script to do this?
>

That's... debatable. I think it's a bit long for inline, but it's 
a matter of taste. I'll inline in simplify it (do everythin without
functions to shorten a bit)

 
> NETWORK_IFACES = $(TARGET_DIR)/etc/network/interfaces
> NETWORK_DHCP_IFACE = $(call qstrip,$(BR2_SIMPLE_DHCP_INTERFACE))
> 
> define NETWORK_ADD_DHCP_INTERFACE
> 	echo "auto $(NETWORK_DHCP_IFACE)" >> $(NETWORK_IFACES)
> 	echo "iface $(NETWORK_DHCP_IFACE) inet dhcp" >> $(NETWORK_IFACES)
> endef
> 
> define NETWORK_CREATE_IFACES_FILE
> 	rm -f $(NETWORK_IFACES)
> 	echo "auto lo" >> $(NETWORK_IFACES)
> 	echo "iface lo inet loopback" >> $(NETWORK_IFACES)
> 	$(NETWORK_ADD_DHCP_INTERFACE)
> endef
> 
> TARGET_FINALIZE_HOOKS += NETWORK_CREATE_IFACES_FILE
> 
> > +config BR2_SIMPLE_DHCP
> > +	bool "automatic network configuration via DHCP"
> > +	default n
> > +	depends on !BR2_PACKAGE_SYSTEMD_NETWORKD
> > +	help
> > +	  Automatically do a DHCP request on startup on selected
> > interface at
> > +	  startup. For more complicated setups use an overlay.
> > +
> > +comment "automatic network configuration via DHCP is not
> > compatible with networkd"
> > +	depends on BR2_PACKAGE_SYSTEMD_NETWORKD
> > +
> > +config BR2_SIMPLE_DHCP_INTERFACE
> > +	string "name of the physical network interface to run DHCP on"
> > +	depends on BR2_SIMPLE_DHCP
> > +	default eth0
> > +	help
> > +	  The name of the network interface to configure automatically
> 
> Why two options? Just make the BR2_SIMPLE_DHCP_INTERFACE empty by
> default (does nothing), and when non-empty, used as the DHCP network
> interface.
> 

I did it for better presentation in the menuconfig menu, I thought
one option to activate and then another one to set, rather than
an empty-by-default one was cleaner... but if you think a single
one is better it's easy to change...


I'll wait for more comments and submit a v2 when I have more feedback


> Best regards,
> 
> Thomas
> --
> Thomas Petazzoni, CTO, Free Electrons
> Embedded Linux, Kernel and Android engineering
> http://free-electrons.com
>
diff mbox

Patch

diff --git a/support/scripts/generate-network-config.sh b/support/scripts/generate-network-config.sh
new file mode 100755
index 0000000..92f8d16
--- /dev/null
+++ b/support/scripts/generate-network-config.sh
@@ -0,0 +1,24 @@ 
+#!/bin/sh
+
+export BR2_SIMPLE_DHCP=$(sed -r -e "/^BR2_SIMPLE_DHCP=(.*)$/!d;" -e 's//\1/;' $BR2_CONFIG)
+
+export BR2_SIMPLE_DHCP_INTERFACE="$(sed -r -e "/^BR2_SIMPLE_DHCP_INTERFACE=\"(.*)\"$/!d;" -e 's//\1/;' $BR2_CONFIG)"
+
+do_generate_interfaces ()
+{
+	echo "# interface file auto-generated by buildroot"
+	echo
+	echo "auto lo"
+	echo "iface lo inet loopback"
+	echo
+
+	if [ -z "$BR2_SIMPLE_DHCP" ] ; then
+		return
+        fi
+
+	echo "auto $BR2_SIMPLE_DHCP_INTERFACE"
+	echo "iface $BR2_SIMPLE_DHCP_INTERFACE inet dhcp"
+}
+
+mkdir -p $TARGET_DIR/etc/network/
+do_generate_interfaces > $TARGET_DIR/etc/network/interfaces
diff --git a/system/Config.in b/system/Config.in
index a3b7aff..10a81e9 100644
--- a/system/Config.in
+++ b/system/Config.in
@@ -324,6 +324,24 @@  config BR2_TARGET_GENERIC_REMOUNT_ROOTFS_RW
 
 endif # BR2_ROOTFS_SKELETON_DEFAULT
 
+config BR2_SIMPLE_DHCP
+	bool "automatic network configuration via DHCP"
+	default n
+	depends on !BR2_PACKAGE_SYSTEMD_NETWORKD
+	help
+	  Automatically do a DHCP request on startup on selected interface at 
+	  startup. For more complicated setups use an overlay.
+
+comment "automatic network configuration via DHCP is not compatible with networkd"
+	depends on BR2_PACKAGE_SYSTEMD_NETWORKD
+
+config BR2_SIMPLE_DHCP_INTERFACE
+	string "name of the physical network interface to run DHCP on"
+	depends on BR2_SIMPLE_DHCP
+	default eth0
+	help
+	  The name of the network interface to configure automatically
+
 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..bb933f6 100644
--- a/system/system.mk
+++ b/system/system.mk
@@ -38,6 +38,11 @@  ifneq ($(TARGET_GENERIC_ROOT_PASSWD),)
 TARGETS += host-mkpasswd
 endif
 
+define SIMPLE_NETWORK
+	$(TOPDIR)/support/scripts/generate-network-config.sh $(TARGET_DIR)
+endef
+TARGET_FINALIZE_HOOKS += SIMPLE_NETWORK
+
 ifeq ($(BR2_ROOTFS_SKELETON_DEFAULT),y)
 
 define SYSTEM_ROOT_PASSWD