changeset 630:58211c615a8c

core/rc: add basic rc.networking support
author David Demelier <markand@malikania.fr>
date Tue, 23 Jul 2019 23:00:00 +0200
parents dbd752e285f6
children 76aea39af966
files core/rc/rc.7 core/rc/rc.conf core/rc/rc.conf.5 core/rc/rc.init core/rc/rc.networking core/rc/rc.sh core/rc/rc.shutdown
diffstat 7 files changed, 148 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/core/rc/rc.7	Tue Jul 23 22:56:00 2019 +0200
+++ b/core/rc/rc.7	Tue Jul 23 23:00:00 2019 +0200
@@ -20,7 +20,7 @@
 .Nm rc
 .Nd Vanilla Linux init scripts
 .Sh DESCRIPTION
-Vanilla runs either
+Vanilla Linux runs either
 .Ar busybox
 or
 .Ar sysvinit
@@ -31,8 +31,9 @@
 .Nm vpk .
 Therefore, you can safely edit them for your own purposes.
 .Pp
-The following files and directories are understood by Vanilla's init process:
-.Bl -tag -width indent-xxxxxxxxxx
+The following files and directories are understood by Vanilla Linux init
+process:
+.Bl -tag -width 18n
 .It Pa /etc/rc.conf
 The global configuration file for system initialization and basic configuration.
 See
@@ -43,6 +44,9 @@
 mounts filesystem, checks devices sets configuration from
 .Pa /etc/rc.conf
 file.
+.It Pa /etc/rc.networking
+Enable network interfaces and configure them. Called just before starting user
+services and mostly used for static address scenarios.
 .It Pa /etc/rc.start
 This script is executed after the initialization and start users services. It
 takes a runlevel number as argument which is in the range 0-6.
--- a/core/rc/rc.conf	Tue Jul 23 22:56:00 2019 +0200
+++ b/core/rc/rc.conf	Tue Jul 23 23:00:00 2019 +0200
@@ -14,3 +14,6 @@
 # Console font and keymap
 FONT=""
 KEYMAP=""
+
+# Networking.
+INTERFACES="lo"
--- a/core/rc/rc.conf.5	Tue Jul 23 22:56:00 2019 +0200
+++ b/core/rc/rc.conf.5	Tue Jul 23 23:00:00 2019 +0200
@@ -39,11 +39,15 @@
 directory. The variable must be in the form
 .Ar Area/Region .
 (Default: empty)
+.Pp
+Note: this requires package
+.Ar tzdata
+to be installed.
 .It Va SERVICES
-This variable contains services to be started at boot. In Vanilla, services have
-no dependencies and therefore user is responsible of starting them is a specific
-order if needed. Thus, services marked in this list separated by spaces are
-executed in order.
+This variable contains services to be started at boot. (Default: empty).
+In Vanilla Linux, services have no dependencies and therefore user is
+responsible of starting them is a specific order if needed. Thus, services
+marked in this list separated by spaces are executed in order.
 .Pp
 Services names are in the form
 .Ar name[:N]
@@ -59,7 +63,7 @@
 service file header for more information.
 .Pp
 Note: don't forget to mark the service file as executable or it won't be
-executed by init process. (Default: empty)
+executed by init process.
 .It Va FONT
 Sets the console font. A list is available in /share/kbd/consolefonts. (Default:
 empty).
@@ -74,6 +78,17 @@
 Note: this requires package
 .Ar kbd
 to be installed.
+.It Va INTERFACES
+Interfaces to pass to
+.Pa /etc/rc.networking
+script during bootup and shutdown. Mostly used for static IP addresses. It's
+strongly advised to keep
+.Ar lo
+interface in this variable. (Default: lo)
+.Pp
+See the documentation in the
+.Pa /etc/rc.networking
+file.
 .El
 .Sh EXAMPLES
 .Ss Starting sysklogd package
--- a/core/rc/rc.init	Tue Jul 23 22:56:00 2019 +0200
+++ b/core/rc/rc.init	Tue Jul 23 23:00:00 2019 +0200
@@ -1,6 +1,6 @@
 #!/bin/sh
 #
-# /etc/rc.init: system initialization script
+# /etc/rc.init -- system initialization script
 #
 # Copyright (c) 2019 David Demelier <markand@malikania.fr>
 #
@@ -122,3 +122,8 @@
 
 # Cleanup some files.
 rm -f /etc/forcefsck
+
+# Startup networking.
+if [ -x /etc/rc.networking ]; then
+	/etc/rc.networking start
+fi
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/core/rc/rc.networking	Tue Jul 23 23:00:00 2019 +0200
@@ -0,0 +1,105 @@
+#!/bin/sh
+#
+# /etc/rc.init -- basic networking script
+#
+# Copyright (c) 2019 David Demelier <markand@malikania.fr>
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+#
+
+#
+# This script is provided mostly for static configuration and/or more complex
+# scenarios where the system administrator requires full control over the
+# networking interfaces. Most people would prefer using dhcpcd for simple
+# configurations or NetworkManager on desktop systems.
+#
+# The file /etc/rc.networking allows you to define functions for each interface
+# and will automatically call "start" and "stop" at boot and shutdown
+# respectively.
+#
+# See below for basic examples and uncomment the appropriate one.
+#
+
+if [ -f /etc/rc.conf ]; then
+	. /etc/rc.conf
+fi
+
+lo()
+{
+	echo "Setting network interface: lo"
+
+	case $1 in
+	"start")
+		ip link set dev lo up
+		ip addr add 127.0.0.1/8 dev lo
+		ip -6 addr add ::1/128 dev lo
+		;;
+	"stop")
+		ip -6 addr del ::1/128 dev lo
+		ip addr del 127.0.0.1 dev lo
+		ip link set dev lo down
+		;;
+	esac
+}
+
+#
+# Example 1: static IP on eth0 interface.
+#
+# eth0()
+# {
+# 	echo "Setting network interface: eth0"
+#
+# 	case $1 in
+# 	"start")
+# 		ip link set eth0 up
+# 		ip addr add 192.168.0.1 dev eth0
+# 		;;
+# 	"stop")
+# 		ip addr del 192.168.0.1 dev eth0
+# 		ip link set eth0 down
+# 		;;
+# 	esac
+# }
+#
+
+usage()
+{
+	echo "usage: $(basename $0) start|stop [interface]" 1>&2
+	exit 1
+}
+
+if [ $# -eq 0 ]; then
+	usage
+	# NOTREACHED
+fi
+
+case $1 in
+"start"|"stop")
+	#
+	# If no interface is given, iterate over all defined in
+	# INTERFACES from /etc/rc.conf.
+	#
+	if [ $# -eq 1 ]; then
+		for iface in $INTERFACES; do
+			$iface $1
+		done
+	else
+		$2 $1
+	fi
+
+	;;
+*)
+	usage
+	# NOTREACHED
+	;;
+esac
--- a/core/rc/rc.sh	Tue Jul 23 22:56:00 2019 +0200
+++ b/core/rc/rc.sh	Tue Jul 23 23:00:00 2019 +0200
@@ -31,6 +31,7 @@
 	install -Dm0755 rc.init $DESTDIR/etc/rc.init
 	install -Dm0755 rc.shutdown $DESTDIR/etc/rc.shutdown
 	install -Dm0755 rc.start $DESTDIR/etc/rc.start
+	install -Dm0755 rc.networking $DESTDIR/etc/rc.networking
 	install -Dm0755 rc.conf.5 $DESTDIR/share/man/man5/rc.conf.5
 	install -Dm0755 rc.7 $DESTDIR/share/man/man7/rc.7
 }
--- a/core/rc/rc.shutdown	Tue Jul 23 22:56:00 2019 +0200
+++ b/core/rc/rc.shutdown	Tue Jul 23 23:00:00 2019 +0200
@@ -1,6 +1,6 @@
 #!/bin/sh
 #
-# /etc/rc.shutdown - reboot and halt script
+# /etc/rc.shutdown -- reboot and halt script
 #
 # Copyright (c) 2019 David Demelier <markand@malikania.fr>
 #
@@ -24,6 +24,11 @@
 echo "Terminating all processes."
 killall5 -15
 
+# Stop networking.
+if [ -x /etc/rc.networking ]; then
+	/etc/rc.networking stop
+fi
+
 echo "Unmounting all filesystems."
 halt -w
 swapoff -a