# Read network settings
network_settings_read
-# Setup the locking.
-LOCKFILE="${RUN_DIR}/.rename_lock"
-function cleanup() {
- lock_release ${LOCKFILE}
-}
-trap cleanup EXIT TERM KILL
-
# Check if the INTERFACE variable is properly set.
assert isset INTERFACE
# Log what we are doing here.
-log DEBUG "Called for interface '${INTERFACE}'."
-
-# Just check if the device has already vanished.
-device_exists ${INTERFACE} || exit ${EXIT_ERROR}
-
-# Acquiring lock for this operation.
-lock_acquire ${LOCKFILE}
+log DEBUG "Called for INTERFACE='${INTERFACE}'"
# Check if the device is already in use and
# prevent the script to touch it in any way.
if ! device_is_free ${INTERFACE}; then
- log ERROR "The device '${INTERFACE}' is in use."
+ log ERROR "The device '${INTERFACE}' is in use"
exit ${EXIT_ERROR}
fi
-# Determine the type of the device and then see what
-# we need to do with it.
-type=$(device_get_type ${INTERFACE})
-log DEBUG "Interface '${INTERFACE}' is of type '${type}'."
+# Everything is wrapped into a function so that we can use locking on it
+main() {
+ # Check if the device has a unique MAC address (which is
+ # what are using to uniquely identify an interface)
+ local address="$(device_get_address "${INTERFACE}")"
+ if isset address && [ "${address}" = "00:00:00:00:00:00" ]; then
+ log DEBUG "Ignoring interface ${INTERFACE} with invalid MAC address ${address}"
+ return ${EXIT_OK}
+ fi
+
+ # Determine the type of the device and then see what
+ # we need to do with it.
+ local type="$(device_get_type "${INTERFACE}")"
-case "${type}" in
- ethernet|real)
- # Search within all the port configurations
- # if this port has already been configured.
- for port in $(ports_get_all); do
- port_cmd hotplug_rename ${port} ${INTERFACE} &>/dev/null
- if [ $? -eq ${EXIT_TRUE} ]; then
- echo "${port}"
- exit ${EXIT_OK}
+ log DEBUG "Interface '${INTERFACE}' is of type '${type}'"
+
+ case "${type}" in
+ ethernet)
+ # Search within all the port configurations
+ # if this port has already been configured.
+ local port
+ for port in $(ports_get_all); do
+ port_cmd hotplug_rename "${port}" "${INTERFACE}" &>/dev/null
+ if [ $? -eq ${EXIT_TRUE} ]; then
+ print "${port}"
+ exit ${EXIT_OK}
+ fi
+ done
+
+ # Could not find a valid port configuration, creating a new one
+ port="$(port_find_free "${PORT_PATTERN}")"
+ assert isset port
+
+ # Create a new port configuration with the new name
+ if ! port_new "ethernet" "${port}" "${INTERFACE}"; then
+ log ERROR "Could not create new port configuration for ${INTERFACE}"
+ return ${EXIT_ERROR}
fi
- done
- # If no port configuration could be found,
- # we search for the next unused name and return that.
- port=$(port_find_free ${PORT_PATTERN})
- echo "${port}"
+ print "${port}"
+ ;;
+ esac
- log DEBUG "Could not find an existing port configuration for '${INTERFACE}'."
- log DEBUG "${INTERFACE} --> ${port}"
- ;;
-esac
+ return ${EXIT_OK}
+}
-exit ${EXIT_OK}
+# Run perform rename function exclusively
+lock "${RUN_DIR}/.network-rename-lock" main || exit $?