summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@tele2.at>2018-04-26 20:34:14 +0200
committerDavid Oberhollenzer <david.oberhollenzer@tele2.at>2018-04-27 11:28:46 +0200
commit0ded4765344b6360cf4f030909d5bbe736a1a584 (patch)
treea9f56a14fa1b278f3c58bfe11ef6ed9394253459
parent59731dd69bb4be66c721b3de66fb2ab80bec3e7e (diff)
Add optional script for persistent network interface renaming
Add a new service that runs a small helper script that applies a consistent, deterministic naming pattern to all interfaces, based on their mac address. A configuration file with wild card pattern matching can be used for determining names. By default, this is disabled. Also, this script is Linux specific. Signed-off-by: David Oberhollenzer <david.oberhollenzer@tele2.at>
-rw-r--r--.gitignore2
-rw-r--r--Makefile.am3
-rw-r--r--configure.ac2
-rw-r--r--netcfg/ifrename13
-rw-r--r--scripts/Makemodule.am2
-rwxr-xr-xscripts/ifrename.sh.in65
-rw-r--r--services/Makemodule.am2
-rw-r--r--services/ifrename.in6
8 files changed, 92 insertions, 3 deletions
diff --git a/.gitignore b/.gitignore
index 6c13aba..3a291c4 100644
--- a/.gitignore
+++ b/.gitignore
@@ -26,7 +26,9 @@ services/sigterm
services/devfs
services/procfs
services/sysfs
+services/ifrename
scripts/devfs.sh
+scripts/ifrename.sh
etc/initd.env
diff --git a/Makefile.am b/Makefile.am
index ad977d4..01e21a2 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -5,8 +5,9 @@ AM_CFLAGS = $(WARN_CFLAGS)
sbin_PROGRAMS =
noinst_LIBRARIES =
+nobase_sysconf_DATA = netcfg/ifrename
sysconf_DATA = etc/initd.env
-EXTRA_DIST = README.md LICENSE docs
+EXTRA_DIST = README.md LICENSE docs netcfg
helperdir = @SCRIPTDIR@
helper_PROGRAMS =
diff --git a/configure.ac b/configure.ac
index c7a870b..13b11eb 100644
--- a/configure.ac
+++ b/configure.ac
@@ -52,7 +52,9 @@ AC_CONFIG_FILES([services/sigterm])
AC_CONFIG_FILES([services/sysfs])
AC_CONFIG_FILES([services/devfs])
AC_CONFIG_FILES([services/procfs])
+AC_CONFIG_FILES([services/ifrename])
AC_CONFIG_FILES([scripts/devfs.sh])
+AC_CONFIG_FILES([scripts/ifrename.sh])
AC_CONFIG_FILES([etc/initd.env])
AC_OUTPUT([Makefile])
diff --git a/netcfg/ifrename b/netcfg/ifrename
new file mode 100644
index 0000000..08909fa
--- /dev/null
+++ b/netcfg/ifrename
@@ -0,0 +1,13 @@
+#
+# Interface renaming rules
+#
+# Format: NAME,MAC,NEWNAME
+#
+# NAME and MAC are shell glob patterns. Both must match for a rule to apply.
+# The first matching rule is chosen (top to bottom).
+#
+# Interfaces with the same NEWNAME are sorted by MAC and have a running
+# index appended to their new name.
+#
+# Example: rename all ethernet interfaces to "port<X>"
+# eth*,*,port
diff --git a/scripts/Makemodule.am b/scripts/Makemodule.am
index 3326860..5759ff0 100644
--- a/scripts/Makemodule.am
+++ b/scripts/Makemodule.am
@@ -1,3 +1,3 @@
-helper_SCRIPTS += scripts/devfs.sh scripts/trymount.sh
+helper_SCRIPTS += scripts/devfs.sh scripts/trymount.sh scripts/ifrename.sh
EXTRA_DIST += scripts/trymount.sh
diff --git a/scripts/ifrename.sh.in b/scripts/ifrename.sh.in
new file mode 100755
index 0000000..fee5444
--- /dev/null
+++ b/scripts/ifrename.sh.in
@@ -0,0 +1,65 @@
+#!/bin/sh
+#
+# SPDX-License-Identifier: GPL-3.0-or-later
+#
+# Copyright (C) 2018 - David Oberhollenzer
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+#
+NAMERULES="@ETCPATH@/netcfg/ifrename"
+TMPPATH="/tmp/ifrename"
+
+[ -f "$NAMERULES" ] || exit 0
+
+mkdir -p "$TMPPATH"
+
+for IFPATH in /sys/class/net/*; do
+ [ "$IFPATH" == "/sys/class/net/lo" ] && continue
+
+ IF=`basename $IFPATH`
+ MAC=`cat $IFPATH/address`
+
+ grep "^[^,]\+,[^,]\+,[a-zA-Z0-9]\+$" $NAMERULES | while read LINE;
+ do
+ NAMECMP=$(echo $LINE | cut -d',' -f1)
+ ADDRCMP=$(echo $LINE | cut -d',' -f2)
+ RULE=$(echo $LINE | cut -d',' -f3)
+
+ case $IF in ($NAMECMP) ;; *) continue;; esac
+ case $MAC in ($ADDRCMP) ;; *) continue;; esac
+
+ echo "$MAC,$IF" >> "$TMPPATH/$RULE"
+ break
+ done
+done
+
+for FNAME in $TMPPATH/*; do
+ [ ! -f "$FNAME" ] && break
+
+ IDX=0
+ PREFIX=$(basename $FNAME)
+
+ sort -t',' -k1 -u $FNAME | while read LINE;
+ do
+ OLDNAME=$(echo $LINE | cut -d',' -f2)
+ NEWNAME="$PREFIX$IDX"
+ IDX=`expr $IDX + 1`
+
+ ip link set "$OLDNAME" name "$NEWNAME"
+ done
+
+ rm "$FNAME"
+done
+
+rmdir "$TMPPATH"
diff --git a/services/Makemodule.am b/services/Makemodule.am
index 7187aab..5dc8c4c 100644
--- a/services/Makemodule.am
+++ b/services/Makemodule.am
@@ -4,7 +4,7 @@ init_DATA += services/sysctl services/hwclock services/sysinit
init_DATA += services/reboot services/shutdown services/sigkill
init_DATA += services/sigterm services/sync services/devfs
init_DATA += services/sysfs services/procfs services/tmpfs
-init_DATA += services/vfs
+init_DATA += services/vfs services/ifrename
EXTRA_DIST += services/sysinit services/vfs services/agetty services/hostname
EXTRA_DIST += services/hwclock services/loopback services/reboot
diff --git a/services/ifrename.in b/services/ifrename.in
new file mode 100644
index 0000000..7c6e346
--- /dev/null
+++ b/services/ifrename.in
@@ -0,0 +1,6 @@
+description "rename network interfaces"
+type wait
+target boot
+after sysinit
+
+exec "@SCRIPTDIR@/ifrename.sh" \ No newline at end of file