From: "André Erdmann" <dywi@mailerd.de>
To: buildroot@busybox.net
Subject: [Buildroot] [PATCH v3] set simple network setup via the system configuration submenu
Date: Fri, 24 Oct 2014 16:37:35 +0200 [thread overview]
Message-ID: <544A642F.4020604@mailerd.de> (raw)
In-Reply-To: <1414071600-28318-1-git-send-email-jeremy.rosen@openwide.fr>
Hi,
2014/10/24 Jeremy Rosen <jeremy.rosen@openwide.fr>:
> diff --git a/support/scripts/generate-network-config.sh b/support/scripts/generate-network-config.sh
> new file mode 100755
> index 0000000..c09d165
> --- /dev/null
> +++ b/support/scripts/generate-network-config.sh
> @@ -0,0 +1,136 @@
> +#!/bin/sh
> +
> +#extract our parameters from the config file
> +# see comment in support/scripts/mkusers at to why we can't simply source
> +for PARAM in \
> + BR2_PACKAGE_SYSTEMD_NETWORKD \
> + BR2_SIMPLE_NETWORK_NONE \
> + BR2_SIMPLE_NETWORK_IPV4_DHCP \
> + BR2_SIMPLE_NETWORK_IPV4_MANUAL \
> + ; do
> +TMP=$(sed -r -e "/^$PARAM=(.*)$/!d;" -e 's//\1/;' $BR2_CONFIG)
> +export $PARAM=$TMP
> +done
> +
> +for PARAM in \
> + BR2_SIMPLE_NETWORK_NAME \
> + BR2_SIMPLE_NETWORK_IPV4_ADDRESS \
> + BR2_SIMPLE_NETWORK_IPV4_NETMASK \
> + BR2_SIMPLE_NETWORK_IPV4_BROADCAST \
> + BR2_SIMPLE_NETWORK_IPV4_GATEWAY \
> + ; do
> +TMP=$(sed -r -e "/^$PARAM=\"(.*)\"$/!d;" -e 's//\1/;' $BR2_CONFIG)
> +export $PARAM="$TMP"
> +done
> +
> +check_configuration ()
> +{
> + if [ -z "$BR2_SIMPLE_NETWORK_NONE" ] ; then
> + if [ -z "$BR2_SIMPLE_NETWORK_NAME" ] ; then
> + echo ERROR no name specified for first network interface
> + exit 1
> + fi
> + if [ "$BR2_SIMPLE_NETWORK_IPV4_MANUAL" ] ; then
> + if [ -z "$BR2_SIMPLE_NETWORK_IPV4_ADDRESS" ] ; then
> + echo ERROR BR2_SIMPLE_NETWORK_IPV4_ADDRESS not set 1>&2
> + exit 1
> + fi
> + if [ -z "$BR2_SIMPLE_NETWORK_IPV4_NETMASK" ] ; then
> + echo ERROR BR2_SIMPLE_NETWORK_IPV4_NETMASK not set 1>&2
> + exit 1
> + fi
Not strictly necessary(*), but it's safer to back up the old field separator and
restore it later on:
OLDIFS="$IFS"; IFS=.
(*) otherwise, the script could misbehave if TARGET_DIR contains dot "." chars:
TARGET_DIR="/tmp/br.foo/..."
echo > $TARGET_DIR/x =expands-to=> echo > /tmp/br foo/.../x
> + IFS=.
> + for dec in $BR2_SIMPLE_NETWORK_IPV4_NETMASK ; do
> + case $dec in
> + 255 | 254 | 252 |248 | 240 |224 | 192 | 128 | 0 );;
> + *) echo "Error: $BR2_SIMPLE_NETWORK_IPV4_NETMASK is not a correct NETMASK" 1>&2; exit 1 ;;
> + esac
> + done
IFS="$OLDIFS"
> + fi
> + fi
> +}
> +
> +do_generate_interfaces ()
> +{
==START
> + if [ "$BR2_PACKAGE_SYSTEMD_NETWORKD" ] ; then
> + echo
> + return
> + fi
==END
Can be dropped, because you already check BR2_PACKAGE_SYSTEMD_NETWORKD
before calling do_generate_interfaces().
> + echo "# interface file auto-generated by buildroot"
> + echo
> + echo "auto lo"
> + echo "iface lo inet loopback"
> + echo
> +
> + if [ -z "$BR2_SIMPLE_NETWORK_NONE" ] ; then
> + echo "auto $BR2_SIMPLE_NETWORK_NAME"
> + if [ "$BR2_SIMPLE_NETWORK_IPV4_DHCP" ] ; then
> + echo "iface $BR2_SIMPLE_NETWORK_NAME inet dhcp"
> + elif [ "$BR2_SIMPLE_NETWORK_IPV4_MANUAL" ] ; then
> + echo "iface $BR2_SIMPLE_NETWORK_NAME inet static"
> + echo " address $BR2_SIMPLE_NETWORK_IPV4_ADDRESS"
> + echo " netmask $BR2_SIMPLE_NETWORK_IPV4_NETMASK"
> +
> + if [ "$BR2_SIMPLE_NETWORK_IPV4_BROADCAST" ] ; then
> + echo " broadcast $BR2_SIMPLE_NETWORK_IPV4_BROADCAST"
> + fi
> + if [ "$BR2_SIMPLE_NETWORK_IPV4_GATEWAY" ] ; then
> + echo " gateway $BR2_SIMPLE_NETWORK_IPV4_GATEWAY"
> + fi
> + fi
> + fi
> +}
> +
> +do_generate_network_service ()
> +{
> + if [ "$BR2_SIMPLE_NETWORK_NONE" ] ; then
> + return
> + fi
> + echo "[Match]"
> + echo "Name=$BR2_SIMPLE_NETWORK_NAME"
> + echo
==START
> + if [ $BR2_SIMPLE_NETWORK_IPV4_DHCP ] ; then
> + echo "[Network]"
> + echo "DHCP=v4"
> + elif [ $BR2_SIMPLE_NETWORK_IPV4_MANUAL ] ; then
> + nbits=0
> + IFS=.
> + for dec in $BR2_SIMPLE_NETWORK_IPV4_NETMASK ; do
> + case $dec in
> + 255) let nbits+=8;;
> + 254) let nbits+=7;;
> + 252) let nbits+=6;;
> + 248) let nbits+=5;;
> + 240) let nbits+=4;;
> + 224) let nbits+=3;;
> + 192) let nbits+=2;;
> + 128) let nbits+=1;;
> + 0);;
> + esac
> + done
> +
> + if [ "$BR2_SIMPLE_NETWORK_IPV4_ADDRESS" != "0.0.0.0" ] ; then
> + if [ "$BR2_SIMPLE_NETWORK_IPV4_GATEWAY" ] ; then
> + echo "[Network]"
> + echo "Gateway=$BR2_SIMPLE_NETWORK_IPV4_GATEWAY"
> + echo "Description=buildroot-configured interface"
> + echo
> + fi
> + echo "[Address]"
> + echo "Address=$BR2_SIMPLE_NETWORK_IPV4_ADDRESS/$nbits"
> + if [ "$BR2_SIMPLE_NETWORK_IPV4_BROADCAST" ] ; then
> + echo "Broadcast=$BR2_SIMPLE_NETWORK_IPV4_BROADCAST"
> + fi
> + fi
> + fi
==END
What happens here is that "Description=" is only printed if a gateway is configured,
plus you can optimize the control flow a bit:
echo "[Network]"
echo "Description=buildroot-configured interface"
if [ $BR2_SIMPLE_NETWORK_IPV4_DHCP ] ; then
echo "DHCP=v4"
elif \
[ $BR2_SIMPLE_NETWORK_IPV4_MANUAL ] && \
[ "$BR2_SIMPLE_NETWORK_IPV4_ADDRESS" != "0.0.0.0" ]
then
OLDIFS="$IFS"; IFS=. ## save IFS
for ... case ... esac ... done ## nbits
IFS="$OLDIFS" ## restore IFS
if [ "$BR2_SIMPLE_NETWORK_IPV4_GATEWAY" ]; then
echo "Gateway=$BR2_SIMPLE_NETWORK_IPV4_GATEWAY"
fi
echo
echo "[Address]"
echo "Address=$BR2_SIMPLE_NETWORK_IPV4_ADDRESS/$nbits"
if [ "$BR2_SIMPLE_NETWORK_IPV4_BROADCAST" ] ; then
echo "Broadcast=$BR2_SIMPLE_NETWORK_IPV4_BROADCAST"
fi
fi
> +}
> +
> +check_configuration
> +mkdir -p $TARGET_DIR/etc/network/
Need an "mkdir -p $TARGET_DIR/etc/systemd/network" here.
However, this would also create it for non-systemd systems, which is useless,
so rather remove the systemd file if it exists:
rm -f -- $TARGET_DIR/etc/systemd/network/80-buildroot.network
> +if [ "$BR2_PACKAGE_SYSTEMD_NETWORKD" ] ; then
and create the directory here:
mkdir -p $TARGET_DIR/etc/systemd/network
> + echo > $TARGET_DIR/etc/network/interfaces
> + do_generate_network_service > $TARGET_DIR/etc/systemd/network/80-buildroot.network
> +else
> + do_generate_interfaces > $TARGET_DIR/etc/network/interfaces
and drop this echo:
> + echo > $TARGET_DIR/etc/systemd/network/80-buildroot.network
> +fi
> diff --git a/system/Config.in b/system/Config.in
> index 2465f79..3683edf 100644
> --- a/system/Config.in
[snip]
--
Andr?
prev parent reply other threads:[~2014-10-24 14:37 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-10-23 13:40 [Buildroot] [PATCH v3] set simple network setup via the system configuration submenu Jérémy Rosen
2014-10-24 14:37 ` André Erdmann [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=544A642F.4020604@mailerd.de \
--to=dywi@mailerd.de \
--cc=buildroot@busybox.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox