1 | #!/bin/sh -e
|
---|
2 |
|
---|
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.
|
---|
7 |
|
---|
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
|
---|
17 |
|
---|
18 | # Copyright (C) 2006 Marco d'Itri <md@Linux.IT>
|
---|
19 | # Copyright (C) 2007 Kay Sievers <kay.sievers@vrfy.org>
|
---|
20 | #
|
---|
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.
|
---|
25 | #
|
---|
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.
|
---|
30 | #
|
---|
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/>.
|
---|
33 |
|
---|
34 | # debug, if UDEV_LOG=<debug>
|
---|
35 | if [ -n "$UDEV_LOG" ]; then
|
---|
36 | if [ "$UDEV_LOG" -ge 7 ]; then
|
---|
37 | set -x
|
---|
38 | fi
|
---|
39 | fi
|
---|
40 |
|
---|
41 | RULES_FILE='/etc/udev/rules.d/70-persistent-net.rules'
|
---|
42 |
|
---|
43 | . /lib/udev/rule_generator.functions
|
---|
44 |
|
---|
45 | interface_name_taken() {
|
---|
46 | local value="$(find_all_rules 'NAME=' $INTERFACE)"
|
---|
47 | if [ "$value" ]; then
|
---|
48 | return 0
|
---|
49 | else
|
---|
50 | return 1
|
---|
51 | fi
|
---|
52 | }
|
---|
53 |
|
---|
54 | find_next_available() {
|
---|
55 | raw_find_next_available "$(find_all_rules 'NAME=' "$1")"
|
---|
56 | }
|
---|
57 |
|
---|
58 | write_rule() {
|
---|
59 | local match="$1"
|
---|
60 | local name="$2"
|
---|
61 | local comment="$3"
|
---|
62 |
|
---|
63 | {
|
---|
64 | if [ "$PRINT_HEADER" ]; then
|
---|
65 | PRINT_HEADER=
|
---|
66 | echo "# This file was automatically generated by the $0"
|
---|
67 | echo "# program, run by the persistent-net-generator.rules rules file."
|
---|
68 | echo "#"
|
---|
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."
|
---|
71 | fi
|
---|
72 |
|
---|
73 | echo ""
|
---|
74 | [ "$comment" ] && echo "# $comment"
|
---|
75 | echo "SUBSYSTEM==\"net\", ACTION==\"add\"$match, NAME=\"$name\""
|
---|
76 | } >> $RULES_FILE
|
---|
77 | }
|
---|
78 |
|
---|
79 | if [ -z "$INTERFACE" ]; then
|
---|
80 | echo "missing \$INTERFACE" >&2
|
---|
81 | exit 1
|
---|
82 | fi
|
---|
83 |
|
---|
84 | # Prevent concurrent processes from modifying the file at the same time.
|
---|
85 | lock_rules_file
|
---|
86 |
|
---|
87 | # Check if the rules file is writeable.
|
---|
88 | choose_rules_file
|
---|
89 |
|
---|
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\""
|
---|
93 | fi
|
---|
94 |
|
---|
95 | if [ "$MATCHDRV" ]; then
|
---|
96 | match="$match, DRIVERS==\"$MATCHDRV\""
|
---|
97 | fi
|
---|
98 |
|
---|
99 | if [ "$MATCHDEVID" ]; then
|
---|
100 | match="$match, ATTR{dev_id}==\"$MATCHDEVID\""
|
---|
101 | fi
|
---|
102 |
|
---|
103 | if [ "$MATCHID" ]; then
|
---|
104 | match="$match, KERNELS==\"$MATCHID\""
|
---|
105 | fi
|
---|
106 |
|
---|
107 | if [ "$MATCHIFTYPE" ]; then
|
---|
108 | match="$match, ATTR{type}==\"$MATCHIFTYPE\""
|
---|
109 | fi
|
---|
110 |
|
---|
111 | if [ -z "$match" ]; then
|
---|
112 | echo "missing valid match" >&2
|
---|
113 | unlock_rules_file
|
---|
114 | exit 1
|
---|
115 | fi
|
---|
116 |
|
---|
117 | basename=${INTERFACE%%[0-9]*}
|
---|
118 |
|
---|
119 | if [ "$INTERFACE_NAME" ]; then
|
---|
120 | # external tools may request a custom name
|
---|
121 | COMMENT="$COMMENT (custom name provided by external tool)"
|
---|
122 | if [ "$INTERFACE_NAME" != "$INTERFACE" ]; then
|
---|
123 | INTERFACE=$INTERFACE_NAME;
|
---|
124 | echo "INTERFACE_NEW=$INTERFACE"
|
---|
125 | fi
|
---|
126 | else
|
---|
127 | # if a rule using the current name already exists, find a new name
|
---|
128 | if interface_name_taken; then
|
---|
129 | INTERFACE="$basename$(find_next_available "$basename[0-9]*")"
|
---|
130 | # prevent INTERFACE from being "eth" instead of "eth0"
|
---|
131 | [ "$INTERFACE" = "${INTERFACE%%[ \[\]0-9]*}" ] && INTERFACE=${INTERFACE}0
|
---|
132 | echo "INTERFACE_NEW=$INTERFACE"
|
---|
133 | fi
|
---|
134 | fi
|
---|
135 |
|
---|
136 | write_rule "$match" "$INTERFACE" "$COMMENT"
|
---|
137 |
|
---|
138 | unlock_rules_file
|
---|
139 |
|
---|
140 | exit 0
|
---|