3 # Copyright (C) 2006 Marco d'Itri <md@Linux.IT>
4 # Copyright (C) 2007 Kay Sievers <kay.sievers@vrfy.org>
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by the
8 # Free Software Foundation version 2 of the License.
10 # This script is run to create persistent network device naming rules
11 # based on properties of the device.
12 # If the interface needs to be renamed, INTERFACE_NEW=<name> will be printed
13 # on stdout to allow udev to IMPORT it.
15 # variables used to communicate:
16 # MATCHADDR MAC address used for the match
17 # MATCHID bus_id used for the match
18 # MATCHDEVID dev_id used for the match
19 # MATCHDRV driver name used for the match
20 # MATCHIFTYPE interface type match
21 # COMMENT comment to add to the generated rule
22 # INTERFACE_NAME requested name supplied by external tool
23 # INTERFACE_NEW new interface name returned by rule writer
25 RULES_FILE='/etc/udev/rules.d/70-persistent-net.rules'
27 . /lib/udev/rule_generator.functions
29 interface_name_taken() {
30 local value="$(find_all_rules 'NAME=' $INTERFACE)"
38 find_next_available() {
39 raw_find_next_available "$(find_all_rules 'NAME=' "$1")"
48 if [ "$PRINT_HEADER" ]; then
50 echo "# This file was automatically generated by the $0"
51 echo "# program run by the persistent-net-generator.rules rules file."
53 echo "# You can modify it, as long as you keep each rule on a single line."
57 [ "$comment" ] && echo "# $comment"
58 echo "SUBSYSTEM==\"net\", ACTION==\"add\"$match, NAME=\"$name\""
62 if [ -z "$INTERFACE" ]; then
63 echo "missing \$INTERFACE" >&2
67 # Prevent concurrent processes from modifying the file at the same time.
70 # Check if the rules file is writeable.
73 # the DRIVERS key is needed to not match bridges and VLAN sub-interfaces
74 if [ "$MATCHADDR" ]; then
75 match="$match, DRIVERS==\"?*\", ATTR{address}==\"$MATCHADDR\""
78 if [ "$MATCHDRV" ]; then
79 match="$match, DRIVERS==\"$MATCHDRV\""
82 if [ "$MATCHDEVID" ]; then
83 match="$match, ATTR{dev_id}==\"$MATCHDEVID\""
86 if [ "$MATCHID" ]; then
87 match="$match, KERNELS==\"$MATCHID\""
90 if [ "$MATCHIFTYPE" ]; then
91 match="$match, ATTR{type}==\"$MATCHIFTYPE\""
94 if [ -z "$match" ]; then
95 echo "missing valid match" >&2
100 basename=${INTERFACE%%[0-9]*}
101 match="$match, KERNEL==\"$basename*\""
103 if [ "$INTERFACE_NAME" ]; then
104 # external tools may request a custom name
105 COMMENT="$COMMENT (custom name provided by external tool)"
106 if [ "$INTERFACE_NAME" != "$INTERFACE" ]; then
107 INTERFACE=$INTERFACE_NAME;
108 echo "INTERFACE_NEW=$INTERFACE"
111 # if a rule using the current name already exists, find a new name
112 if interface_name_taken; then
113 INTERFACE="$basename$(find_next_available "$basename[0-9]*")"
114 echo "INTERFACE_NEW=$INTERFACE"
118 write_rule "$match" "$INTERFACE" "$COMMENT"