aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoy Marples <roy@marples.name>2009-04-16 23:47:23 +0000
committerRoy Marples <roy@marples.name>2009-04-16 23:47:23 +0000
commit6027b0ed7d1791a363004ec5b77ec7e866fc8bc9 (patch)
treef4d8a33beb871f2f90a16de1ec742cd96bcb2232
parent95d954e7b58e55534f393a1249db2becf74ee9f9 (diff)
Add a new init script - network
This simply assigns static addresses and an optional default route. It's possible to add external commands as well, so to create a bonded interface. Hopefully we can add a few examples to satisfy most of the old net.lo, which is no longer installed into boot by default.
-rw-r--r--conf.d/Makefile4
-rw-r--r--conf.d/Makefile.FreeBSD3
-rw-r--r--conf.d/Makefile.Linux4
-rw-r--r--conf.d/Makefile.NetBSD3
-rw-r--r--conf.d/network.Linux10
-rw-r--r--conf.d/network.in10
-rw-r--r--init.d/Makefile4
-rw-r--r--init.d/network.in226
-rw-r--r--runlevels/Makefile3
-rw-r--r--runlevels/Makefile.BSD2
-rw-r--r--runlevels/Makefile.Linux2
11 files changed, 264 insertions, 7 deletions
diff --git a/conf.d/Makefile b/conf.d/Makefile
index 5040ba27..9d0fc0d0 100644
--- a/conf.d/Makefile
+++ b/conf.d/Makefile
@@ -1,5 +1,7 @@
DIR= ${CONFDIR}
-CONF= bootmisc fsck hostname local localmount net urandom
+CONF= bootmisc fsck hostname local localmount network urandom
+
+CLEANFILES+= network
MK= ../mk
include ${MK}/os.mk
diff --git a/conf.d/Makefile.FreeBSD b/conf.d/Makefile.FreeBSD
index c43a1d4f..fb531e2f 100644
--- a/conf.d/Makefile.FreeBSD
+++ b/conf.d/Makefile.FreeBSD
@@ -1 +1,4 @@
CONF+= ipfw moused powerd rarpd savecore syscons
+
+network:
+ cp network.in network
diff --git a/conf.d/Makefile.Linux b/conf.d/Makefile.Linux
index 74679400..9e1ce9d8 100644
--- a/conf.d/Makefile.Linux
+++ b/conf.d/Makefile.Linux
@@ -1 +1,5 @@
CONF+= consolefont dmesg hwclock keymaps modules
+
+network:
+ cp network.in network
+ cat network.Linux >> network
diff --git a/conf.d/Makefile.NetBSD b/conf.d/Makefile.NetBSD
index 18a52cce..fd0be1ec 100644
--- a/conf.d/Makefile.NetBSD
+++ b/conf.d/Makefile.NetBSD
@@ -1 +1,4 @@
CONF+= moused rarpd savecore
+
+network:
+ cp network.in network
diff --git a/conf.d/network.Linux b/conf.d/network.Linux
new file mode 100644
index 00000000..9967301d
--- /dev/null
+++ b/conf.d/network.Linux
@@ -0,0 +1,10 @@
+# ifconfig under Linux is not that powerful and doesn't easily handle
+# multiple addresses
+# On the other hand, ip (iproute2) is quite powerful and is also supported
+# ip_eth0="192.168.0.10/24; 192.168.10.10/24"
+
+# Create a bonded interface
+# interfaces="bond0"
+# ifup_bond0="modprobe bonding; ifconfig bond0 up; ifenslave bond0 bge0"
+# ifconfig_bond0="192.168.0.10/24"
+# ifdown_bond0="rmmod bonding"
diff --git a/conf.d/network.in b/conf.d/network.in
new file mode 100644
index 00000000..2bb2b071
--- /dev/null
+++ b/conf.d/network.in
@@ -0,0 +1,10 @@
+# Assign static IP addresses and run custom scripts per interface
+# Seperate commands with ;
+# Prefix with ! to run a shell script
+# ifconfig_eth0="up; 192.168.0.10 netmask 255.255.255.0; ! echo up"
+
+# You also have ifup_eth0 and ifdown_eth0 to run other commands when
+# eth0 is started and stopped.
+# Lastly, the interfaces variable pulls in virtual interfaces that cannot
+# be automatically detected.
+
diff --git a/init.d/Makefile b/init.d/Makefile
index fd2c87d1..d3c7cf1b 100644
--- a/init.d/Makefile
+++ b/init.d/Makefile
@@ -1,6 +1,6 @@
DIR= ${INITDIR}
-SRCS= bootmisc.in fsck.in hostname.in local.in localmount.in \
- netmount.in root.in savecache.in swap.in sysctl.in urandom.in
+SRCS= bootmisc.in fsck.in hostname.in local.in localmount.in netmount.in \
+ network.in root.in savecache.in swap.in sysctl.in urandom.in
BIN= ${OBJS}
INSTALLAFTER= _installafter
diff --git a/init.d/network.in b/init.d/network.in
new file mode 100644
index 00000000..9b3fc285
--- /dev/null
+++ b/init.d/network.in
@@ -0,0 +1,226 @@
+#!@PREFIX@/sbin/runscript
+# Copyright 2009 Roy Marples <roy@marples.name>
+# All rights reserved. Released under the 2-clause BSD license.
+
+description="Configures network interfaces."
+__nl="
+"
+
+depend()
+{
+ need localmount
+ after bootmisc
+ provide net
+ keyword nojail noprefix novserver
+}
+
+interfaces()
+{
+ case "${RC_UNAME}" in
+ Linux)
+ local w= rest= i= cmd=$1
+ while read w rest; do
+ i=${w%%:*}
+ [ "$i" != "$w" ] || continue
+ if [ "$cmd" = u ]; then
+ ifconfig "$i" | grep -q "[ ]*UP" || continue
+ fi
+ printf "%s " "$i"
+ done </proc/net/dev
+ ;;
+ *)
+ ifconfig -l$1
+ ;;
+ esac
+}
+
+uniqify()
+{
+ local result=
+ while [ -n "$1" ]; do
+ case " ${result} " in
+ *" $1 "*);;
+ *) result="${result} $1";;
+ esac
+ shift
+ done
+ echo "${result# *}"
+}
+
+dumpargs()
+{
+ local f="$1"
+
+ shift
+ case "$@" in
+ '') cat "$f";;
+ *"$__nl"*) echo "$@";;
+ *)
+ (
+ set -o noglob
+ IFS=';'; set -- $@
+ IFS="$__nl"; echo "$*"
+ );;
+ esac
+}
+
+runip()
+{
+ local iface="$1" err=
+
+ shift
+ err=$(LC_ALL=C ip address add "$@" dev "$iface" 2>&1)
+ [ -z "$err" ] && return 0
+ if [ "$err" = "RTNETLINK answers: File exists" ]; then
+ ip address del "$@" dev "${iface}" 2>/dev/null
+ fi
+ # Localise the error
+ ip address add "$@" dev "$iface"
+}
+
+routeflush()
+{
+ if [ "${RC_UNAME}" = Linux ]; then
+ if [ -x /sbin/ip ]; then
+ ip route flush scope global
+ else
+ # Sadly we also delete some link routes, but
+ # this cannot be helped
+ local dest= gate= net= flags= rest=
+ route -n | while read dest gate net flags rest; do
+ [ -z "$net" ] && continue
+ case "$dest" in
+ [0-9]*) ;;
+ *) continue;;
+ esac
+ local xtra= netmask="netmask $net"
+ case "$flags" in
+ U) continue;;
+ *H*) flags=-host; netmask=;;
+ *!*) flags=-net; xtra=reject;;
+ *) flags=-net;;
+ esac
+ route del $flags $dest $netmask $xtra
+ done
+ fi
+ else
+ route -qn flush
+ fi
+}
+
+runargs()
+{
+ dumpargs "$@" | while read -r args; do
+ case "$args" in
+ ''|"#"*) ;;
+ *)
+ (
+ vebegin "${args#*!}"
+ eval "${args#*!}"
+ veend $?
+ );;
+ esac
+ done
+}
+
+start()
+{
+ local cr=0 r= iface= cnf= cmd= args= upcmd=
+ einfo "Starting network"
+ routeflush
+ if [ "${RC_UNAME}" = "Linux" ]; then
+ ifconfig lo 127.0.0.1 netmask 255.0.0.0 || cr=1
+ route add -net 127.0.0.0 netmask 255.0.0.0 \
+ gw 127.0.0.1 2>/dev/null
+ route add -net 127.0.0.0 netmask 255.0.0.0 \
+ gw 127.0.0.1 reject 2>/dev/null
+ else
+ ifconfig lo0 127.0.0.1 netmask 255.0.0.0 || cr=1
+ route -q add -inet 127.0.0.0 -netmask 255.0.0.0 \
+ 127.0.0.1 -reject || cr=1
+ fi
+ eindent
+ for iface in $(uniqify $(interfaces) $interfaces); do
+ local func= cf=
+ eval upcmd=\$ifup_$iface
+ for func in ip ifconfig; do
+ eval cmd=\$${func}_${iface}
+ if [ -n "$cmd" -o -f /etc/"$func.$iface" ]; then
+ cf=/etc/"$func.$iface"
+ break
+ fi
+ done
+ [ -n "$cf" -o -n "$upcmd" -o \
+ -f /etc/ifup."$iface" -o -f "$cf" ] || continue
+ vebegin "$iface"
+ case "$func" in
+ ip) func=runip;;
+ esac
+ eindent
+ if [ -n "$upcmd" -o -f /etc/ifup."$iface" ]; then
+ runargs /etc/ifup."$iface" "$upcmd"
+ fi
+ r=0
+ dumpargs "$cf" "$cmd" | while read -r args; do
+ case "$args" in
+ ''|"#"*) ;;
+ "!"*)
+ (
+ vebegin "${args#*!}"
+ eval "${args#*!}"
+ veend $?
+ );;
+ *)
+ (
+ set -o noglob
+ eval set -- "$args"
+ vebegin "$@"
+ $func "$iface" "$@"
+ veend $?
+ );;
+ esac
+ done
+ eoutdent
+ veend $? || cr=1
+ done
+ eoutdent
+ eend $cr
+
+ if [ -n "$defaultroute" ]; then
+ ebegin "Setting default route $defaultroute"
+ if [ "${RC_UNAME}" = Linux ]; then
+ route add default gw $defaultroute
+ else
+ route add default $defaultroute
+ fi
+ eend $?
+ fi
+
+ return 0
+}
+
+stop()
+{
+ local cr=0 r= iface= cnf= cmd= args= downcmd=
+ einfo "Stopping network"
+ routeflush
+ eindent
+ for iface in $(uniqify $(interfaces u) $interfaces); do
+ eval downcmd=\$ifdown_$iface
+ eval cmd=\$ip_$iface
+ [ -z "$cmd" ] && eval cmd=\$ifconfig_$iface
+ if [ -n "$cmd" -o -f /etc/ip."$iface" -o \
+ -f /etc/ifconfig."$iface" -o \
+ -n "$downcmd" -o -f /etc/ifdown."$iface" ];
+ then
+ vebegin "$iface"
+ if [ -n "$downcmd" -o -f /etc/ifdown."$iface" ]; then
+ runargs /etc/ifdown."$iface" "$downcmd"
+ fi
+ ifconfig "$iface" down 2>/dev/null
+ veend $?
+ fi
+ done
+ eoutdent
+ eend 0
+}
diff --git a/runlevels/Makefile b/runlevels/Makefile
index fb882d94..766a2cf3 100644
--- a/runlevels/Makefile
+++ b/runlevels/Makefile
@@ -1,4 +1,4 @@
-BOOT= bootmisc fsck hostname localmount \
+BOOT= bootmisc fsck hostname localmount network \
root swap sysctl urandom
DEFAULT= local netmount
SHUTDOWN= savecache
@@ -32,7 +32,6 @@ install:
${INSTALL} -d ${BOOTDIR} || exit $$?; \
for x in ${BOOT}; do \
if test -n "${PREFIX}"; then \
- test "$$x" = "net.lo" -o "$$x" = "net.lo0" && continue; \
grep -q "keyword .*noprefix" ${INITDIR}/"$$x" && continue; \
fi; \
ln -snf ${PREFIX}/etc/init.d/"$$x" ${BOOTDIR}/"$$x" || exit $$?; \
diff --git a/runlevels/Makefile.BSD b/runlevels/Makefile.BSD
index c750e4f3..ffa758c1 100644
--- a/runlevels/Makefile.BSD
+++ b/runlevels/Makefile.BSD
@@ -1 +1 @@
-BOOT+= hostid net.lo0 newsyslog savecore syslogd swap-blk
+BOOT+= hostid newsyslog savecore syslogd swap-blk
diff --git a/runlevels/Makefile.Linux b/runlevels/Makefile.Linux
index 9876cd2b..251c6b8b 100644
--- a/runlevels/Makefile.Linux
+++ b/runlevels/Makefile.Linux
@@ -1,3 +1,3 @@
SYSINIT+= devfs dmesg
-BOOT+= hwclock keymaps modules mtab net.lo procfs termencoding
+BOOT+= hwclock keymaps modules mtab procfs termencoding
SHUTDOWN+= killprocs mount-ro