1
0
Fork 0
mirror of https://github.com/pygos/init.git synced 2024-12-22 07:10:49 +01:00

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>
This commit is contained in:
David Oberhollenzer 2018-04-26 20:34:14 +02:00
parent 59731dd69b
commit 0ded476534
8 changed files with 92 additions and 3 deletions

2
.gitignore vendored
View file

@ -26,7 +26,9 @@ services/sigterm
services/devfs
services/procfs
services/sysfs
services/ifrename
scripts/devfs.sh
scripts/ifrename.sh
etc/initd.env

View file

@ -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 =

View file

@ -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])

13
netcfg/ifrename Normal file
View file

@ -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

View file

@ -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

65
scripts/ifrename.sh.in Executable file
View file

@ -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"

View file

@ -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

6
services/ifrename.in Normal file
View file

@ -0,0 +1,6 @@
description "rename network interfaces"
type wait
target boot
after sysinit
exec "@SCRIPTDIR@/ifrename.sh"