]> git.ipfire.org Git - network.git/blob - src/udev/network-hotplug-rename
903a07ca2725b04e00911ae2faa99243400c1f09
[network.git] / src / udev / network-hotplug-rename
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2011 Michael Tremer & Christian Schmidt #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 # Setup logging.
23 LOG_FACILITY="network-hotplug-rename"
24 LOG_DISABLE_STDOUT="true"
25
26 . /usr/lib/network/functions
27
28 # Read network settings
29 network_settings_read
30
31 # Setup the locking
32 LOCKFILE="${LOCK_DIR}/.network-rename-lock"
33
34 # Check if the INTERFACE variable is properly set.
35 assert isset INTERFACE
36
37 # Log what we are doing here.
38 log DEBUG "Called for INTERFACE='${INTERFACE}'"
39
40 # Check if the device is already in use and
41 # prevent the script to touch it in any way.
42 if ! device_is_free ${INTERFACE}; then
43 log ERROR "The device '${INTERFACE}' is in use"
44 exit ${EXIT_ERROR}
45 fi
46
47 # Everything is wrapped into a function so that we can use locking on it
48 main() {
49 # Check if the device has a unique MAC address (which is
50 # what are using to uniquely identify an interface)
51 local address="$(device_get_address "${INTERFACE}")"
52 if isset address && [ "${address}" = "00:00:00:00:00:00" ]; then
53 log DEBUG "Ignoring interface ${INTERFACE} with invalid MAC address ${address}"
54 return ${EXIT_OK}
55 fi
56
57 # Determine the type of the device and then see what
58 # we need to do with it.
59 local type="$(device_get_type "${INTERFACE}")"
60
61 log DEBUG "Interface '${INTERFACE}' is of type '${type}'"
62
63 case "${type}" in
64 ethernet)
65 # Search within all the port configurations
66 # if this port has already been configured.
67 local port
68 for port in $(ports_get_all); do
69 port_cmd hotplug_rename "${port}" "${INTERFACE}" &>/dev/null
70 if [ $? -eq ${EXIT_TRUE} ]; then
71 print "${port}"
72 exit ${EXIT_OK}
73 fi
74 done
75
76 # Could not find a valid port configuration, creating a new one
77 port="$(port_find_free "${PORT_PATTERN}")"
78 assert isset port
79
80 # Create a new port configuration with the new name
81 if ! port_new "ethernet" "${port}" "${INTERFACE}"; then
82 log ERROR "Could not create new port configuration for ${INTERFACE}"
83 return ${EXIT_ERROR}
84 fi
85
86 print "${port}"
87 ;;
88 esac
89
90 return ${EXIT_OK}
91 }
92
93 # Run perform rename function exclusively
94 lock "${RUN_DIR}/.network-rename-lock" main || exit $?