From cb91ab1abbaeb94ddb69c9050640e48546e55068 Mon Sep 17 00:00:00 2001 From: Vincent Knecht Date: Tue, 16 Feb 2016 21:44:21 +0100 Subject: Add support for static network configuration. --- README.md | 27 +++++++++++++++++++++++++++ rpi2-gen-image.sh | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index fddf17d..0fb8a67 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,33 @@ Set default system locale and keyboard layout. This setting can also be changed ##### `TIMEZONE`="Europe/Berlin" Set default system timezone. All available timezones can be found in the `/usr/share/zoneinfo/` directory. This setting can also be changed inside the running OS using the `dpkg-reconfigure tzdata` command. +#### Networking settings +These settings are used to set up networking configuration in `/etc/systemd/network/eth.network`. + +#####`ENABLE_DHCP`=true +Set the system to use DHCP. When set to "true", the following `NET_*` settings (used for static configuration) are ignored. + +#####`NET_ADDRESS`="" +Set a static IPv4 or IPv6 address and its prefix, separated by "/", eg. "192.169.0.3/24". + +#####`NET_GATEWAY`="" +Set the IP address for the default gateway. + +#####`NET_DNS_1`="" +Set the IP address for the first DNS server. + +#####`NET_DNS_2`="" +Set the IP address for the second DNS server. + +#####`NET_DNS_DOMAINS`="" +Set the default DNS search domains to use for non fully qualified host names. + +#####`NET_NTP_1`="" +Set the IP address for the first NTP server. + +#####`NET_NTP_2`="" +Set the IP address for the second NTP server. + #### Basic system features: ##### `ENABLE_CONSOLE`=true Enable serial console interface. Recommended if no monitor or keyboard is connected to the RPi2. In case of problems fe. if the network (auto) configuration failed - the serial console can be used to access the system. diff --git a/rpi2-gen-image.sh b/rpi2-gen-image.sh index 3c80bf3..012d4ca 100755 --- a/rpi2-gen-image.sh +++ b/rpi2-gen-image.sh @@ -46,6 +46,18 @@ PASSWORD=${PASSWORD:=raspberry} DEFLOCAL=${DEFLOCAL:="en_US.UTF-8"} TIMEZONE=${TIMEZONE:="Europe/Berlin"} +# Network settings +ENABLE_DHCP=${ENABLE_DHCP:=true} +# NET_* settings are ignored when ENABLE_DHCP=true +# NET_ADDRESS is an IPv4 or IPv6 address and its prefix, separated by "/" +NET_ADDRESS=${NET_ADDRESS:=""} +NET_GATEWAY=${NET_GATEWAY:=""} +NET_DNS_1=${NET_DNS_1:=""} +NET_DNS_2=${NET_DNS_2:=""} +NET_DNS_DOMAINS=${NET_DNS_DOMAINS:=""} +NET_NTP_1=${NET_NTP_1:=""} +NET_NTP_2=${NET_NTP_2:=""} + # APT settings APT_PROXY=${APT_PROXY:=""} APT_SERVER=${APT_SERVER:="ftp.debian.org"} @@ -272,6 +284,10 @@ cat <$R/etc/hosts 127.0.0.1 localhost 127.0.1.1 ${HOSTNAME} EOM +if [ "$NET_ADDRESS" != "" ] ; then +NET_IP=$(echo ${NET_ADDRESS} | cut -f 1 -d'/') +sed -i "s/^127.0.1.1/${NET_IP}/" $R/etc/hosts +fi # Set up IPv6 hosts if [ "$ENABLE_IPV6" = true ] ; then @@ -289,6 +305,7 @@ cat <$R/etc/network/interfaces # please configure your networks in '/etc/systemd/network/' EOM +if [ "$ENABLE_DHCP" = true ] ; then # Enable systemd-networkd DHCP configuration for interface eth0 cat <$R/etc/systemd/network/eth.network [Match] @@ -300,7 +317,23 @@ EOM # Set DHCP configuration to IPv4 only if [ "$ENABLE_IPV6" = false ] ; then - sed -i "s/=yes/=v4/" $R/etc/systemd/network/eth.network + sed -i "s/^DHCP=yes/DHCP=v4/" $R/etc/systemd/network/eth.network +fi +else # ENABLE_DHCP=false +cat <$R/etc/systemd/network/eth.network +[Match] +Name=eth0 + +[Network] +DHCP=no +Address=${NET_ADDRESS} +Gateway=${NET_GATEWAY} +DNS=${NET_DNS_1} +DNS=${NET_DNS_2} +Domains=${NET_DNS_DOMAINS} +NTP=${NET_NTP_1} +NTP=${NET_NTP_2} +EOM fi # Enable systemd-networkd service -- cgit v1.2.3