]> git.ipfire.org Git - people/stevee/network.git/blame - src/udev/network-hotplug-rename
Refactor network-hotplug-rename
[people/stevee/network.git] / src / udev / network-hotplug-rename
CommitLineData
a1a8f0f4
MT
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
8c63fa13
MT
22# Setup logging.
23LOG_FACILITY="network-hotplug-rename"
24LOG_DISABLE_STDOUT="true"
25
26. /usr/lib/network/functions
27
e9df08ad
MT
28# Read network settings
29network_settings_read
df468241 30
aa96e2b9
MT
31# Setup the locking
32LOCKFILE="${LOCK_DIR}/.network-rename-lock"
a1a8f0f4
MT
33
34# Check if the INTERFACE variable is properly set.
35assert isset INTERFACE
36
8c63fa13 37# Log what we are doing here.
aa96e2b9 38log DEBUG "Called for INTERFACE='${INTERFACE}'"
8c63fa13 39
2eaf16f3
MT
40# Check if the device is already in use and
41# prevent the script to touch it in any way.
42if ! device_is_free ${INTERFACE}; then
aa96e2b9 43 log ERROR "The device '${INTERFACE}' is in use"
2eaf16f3
MT
44 exit ${EXIT_ERROR}
45fi
46
aa96e2b9
MT
47# Everything is wrapped into a function so that we can use locking on it
48main() {
49 # Determine the type of the device and then see what
50 # we need to do with it.
51 local type="$(device_get_type "${INTERFACE}")"
2eaf16f3 52
aa96e2b9 53 log DEBUG "Interface '${INTERFACE}' is of type '${type}'"
2eaf16f3 54
aa96e2b9
MT
55 case "${type}" in
56 ethernet)
57 # Search within all the port configurations
58 # if this port has already been configured.
59 local port
60 for port in $(ports_get_all); do
61 port_cmd hotplug_rename "${port}" "${INTERFACE}" &>/dev/null
62 if [ $? -eq ${EXIT_TRUE} ]; then
63 echo "${port}"
64 exit ${EXIT_OK}
65 fi
66 done
2eaf16f3 67
aa96e2b9
MT
68 # If no port configuration could be found,
69 # we search for the next unused name and return that.
70 port=$(port_find_free ${PORT_PATTERN})
71 echo "${port}"
72
73 log DEBUG "Could not find an existing port configuration for '${INTERFACE}'"
74 log DEBUG "Renaming interface '${INTERFACE}' to '${port}'"
75 ;;
76 esac
77
78 return ${EXIT_OK}
79}
a1a8f0f4 80
aa96e2b9
MT
81# Run perform rename function exclusively
82lock "${RUN_DIR}/.network-rename-lock" main || exit $?