3
# This script is run to create persistent network device naming rules
4
# based on properties of the device.
5
# If the interface needs to be renamed, INTERFACE_NEW=<name> will be printed
6
# on stdout to allow udev to IMPORT it.
8
# variables used to communicate:
9
# MATCHADDR MAC address used for the match
10
# MATCHID bus_id used for the match
11
# MATCHDEVID dev_id used for the match
12
# MATCHDRV driver name used for the match
13
# MATCHIFTYPE interface type match
14
# COMMENT comment to add to the generated rule
15
# INTERFACE_NAME requested name supplied by external tool
16
# INTERFACE_NEW new interface name returned by rule writer
18
# Copyright (C) 2006 Marco d'Itri <md@Linux.IT>
19
# Copyright (C) 2007 Kay Sievers <kay.sievers@vrfy.org>
21
# This program is free software: you can redistribute it and/or modify
22
# it under the terms of the GNU General Public License as published by
23
# the Free Software Foundation, either version 2 of the License, or
24
# (at your option) any later version.
26
# This program is distributed in the hope that it will be useful,
27
# but WITHOUT ANY WARRANTY; without even the implied warranty of
28
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29
# GNU General Public License for more details.
31
# You should have received a copy of the GNU General Public License
32
# along with this program. If not, see <http://www.gnu.org/licenses/>.
34
# debug, if UDEV_LOG=<debug>
35
if [ -n "$UDEV_LOG" ]; then
36
if [ "$UDEV_LOG" -ge 7 ]; then
41
RULES_FILE='/etc/udev/rules.d/70-persistent-net.rules'
43
. /lib/udev/rule_generator.functions
45
interface_name_taken() {
46
local value="$(find_all_rules 'NAME=' $INTERFACE)"
54
find_next_available() {
55
raw_find_next_available "$(find_all_rules 'NAME=' "$1")"
64
if [ "$PRINT_HEADER" ]; then
66
echo "# This file was automatically generated by the $0"
67
echo "# program, run by the persistent-net-generator.rules rules file."
69
echo "# You can modify it, as long as you keep each rule on a single"
70
echo "# line, and change only the value of the NAME= key."
74
[ "$comment" ] && echo "# $comment"
75
echo "SUBSYSTEM==\"net\", ACTION==\"add\"$match, NAME=\"$name\""
79
if [ -z "$INTERFACE" ]; then
80
echo "missing \$INTERFACE" >&2
84
# Prevent concurrent processes from modifying the file at the same time.
87
# Check if the rules file is writeable.
90
# the DRIVERS key is needed to not match bridges and VLAN sub-interfaces
91
if [ "$MATCHADDR" ]; then
92
match="$match, DRIVERS==\"?*\", ATTR{address}==\"$MATCHADDR\""
95
if [ "$MATCHDRV" ]; then
96
match="$match, DRIVERS==\"$MATCHDRV\""
99
if [ "$MATCHDEVID" ]; then
100
match="$match, ATTR{dev_id}==\"$MATCHDEVID\""
103
if [ "$MATCHID" ]; then
104
match="$match, KERNELS==\"$MATCHID\""
107
if [ "$MATCHIFTYPE" ]; then
108
match="$match, ATTR{type}==\"$MATCHIFTYPE\""
111
if [ -z "$match" ]; then
112
echo "missing valid match" >&2
117
basename=${INTERFACE%%[0-9]*}
118
match="$match, KERNEL==\"$basename*\""
120
if [ "$INTERFACE_NAME" ]; then
121
# external tools may request a custom name
122
COMMENT="$COMMENT (custom name provided by external tool)"
123
if [ "$INTERFACE_NAME" != "$INTERFACE" ]; then
124
INTERFACE=$INTERFACE_NAME;
125
echo "INTERFACE_NEW=$INTERFACE"
128
# if a rule using the current name already exists, find a new name
129
if interface_name_taken; then
130
INTERFACE="$basename$(find_next_available "$basename[0-9]*")"
131
# prevent INTERFACE from being "eth" instead of "eth0"
132
[ "$INTERFACE" = "${INTERFACE%%[ \[\]0-9]*}" ] && INTERFACE=${INTERFACE}0
133
echo "INTERFACE_NEW=$INTERFACE"
137
write_rule "$match" "$INTERFACE" "$COMMENT"