From: Michael Tremer Date: Wed, 3 Aug 2011 14:52:58 +0000 (+0000) Subject: Add automatic renaming of hotplugged devices. X-Git-Tag: 002~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a1a8f0f439cfeb542ec21683f2bab2b1767eb0e5;p=network.git Add automatic renaming of hotplugged devices. Fixes #183. --- diff --git a/Makefile b/Makefile index 6f11b79c..0d86ce6d 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ all: install: -mkdir -pv $(DESTDIR)/etc/{network,ppp} - -mkdir -pv $(DESTDIR)/lib/network + -mkdir -pv $(DESTDIR)/lib/{network,udev} -mkdir -pv $(DESTDIR)/sbin -mkdir -pv $(DESTDIR)/usr/lib/sysctl.d -mkdir -pv $(DESTDIR)/var/log/network @@ -21,6 +21,7 @@ install: cp -rfv {hooks,header*,functions*} $(DESTDIR)/lib/network/ cp -fv sysctl.d/* $(DESTDIR)/usr/lib/sysctl.d/ + cp -rfv udev/* $(DESTDIR)/lib/udev install -m 755 -v ppp/ip-updown $(DESTDIR)/etc/ppp ln -svf ip-updown $(DESTDIR)/etc/ppp/ip-pre-up diff --git a/functions.constants b/functions.constants index bd2a41e8..4f47c54a 100644 --- a/functions.constants +++ b/functions.constants @@ -72,3 +72,5 @@ SYS_CLASS_NET="/sys/class/net" TIMEOUT_RESTART=2 DEVICE_PRINT_LINE1=" %-24s %s\n" + +PORT_PATTERN=portN diff --git a/functions.device b/functions.device index 7367f642..bb47baed 100644 --- a/functions.device +++ b/functions.device @@ -558,18 +558,18 @@ function device_hotplug() { #assert device_exists ${device} if ! device_is_free ${device}; then - log ERROR "The device '${device}' does not look to be unused." - log ERROR "Don't proceed." - # return ${EXIT_ERROR} + log ERROR "The device '${device}' is in use." + return ${EXIT_ERROR} fi for port in $(ports_get_all); do port_cmd hotplug ${port} ${device} if [ $? -eq ${EXIT_OK} ]; then echo "${port}" - exit ${EXIT_OK} + return ${EXIT_OK} fi done - exit ${EXIT_ERROR} + port_find_free ${PORT_PATTERN} + return $? } diff --git a/functions.ports b/functions.ports index 8aa4cac1..6460ab19 100644 --- a/functions.ports +++ b/functions.ports @@ -227,10 +227,12 @@ function port_find_free() { port=${pattern//N/${i}} if ! port_exists ${port} && ! device_exists ${port}; then echo "${port}" - break + return ${EXIT_OK} fi i=$(( ${i} + 1 )) done + + return ${EXIT_ERROR} } function port_get_info() { diff --git a/udev/network-hotplug-rename b/udev/network-hotplug-rename new file mode 100755 index 00000000..3879d029 --- /dev/null +++ b/udev/network-hotplug-rename @@ -0,0 +1,31 @@ +#!/bin/bash +############################################################################### +# # +# IPFire.org - A linux based firewall # +# Copyright (C) 2011 Michael Tremer & Christian Schmidt # +# # +# 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 . # +# # +############################################################################### + +. /lib/network/functions + +# Check if the INTERFACE variable is properly set. +assert isset INTERFACE + +# Get the name of that device from the configuration +# or return a new one if it is unknown. +device_hotplug ${INTERFACE} + +exit $? diff --git a/udev/rules.d/60-net.rules b/udev/rules.d/60-net.rules new file mode 100644 index 00000000..7c89ccf1 --- /dev/null +++ b/udev/rules.d/60-net.rules @@ -0,0 +1,8 @@ + +# Call a script that checks for the right name of the new device. +# It is either renamed to match an existing configuration or gets +# a new name. +ACTION=="add", SUBSYSTEM=="net", PROGRAM="/lib/udev/network-hotplug-rename", RESULT=="?*", ENV{INTERFACE_NAME}="$result" + +# Handle all plugged-in devices. +#SUBSYSTEM=="net", RUN+="/lib/udev/network-hotlug"