chiark / gitweb /
rules: fix md "change"/"remove" handling
[elogind.git] / extras / rule_generator / write_net_rules
1 #!/bin/sh -e
2 #
3 # Copyright (C) 2006 Marco d'Itri <md@Linux.IT>
4 # Copyright (C) 2007 Kay Sievers <kay.sievers@vrfy.org>
5 #
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.
9 #
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.
14
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
24
25 # debug, if UDEV_LOG=<debug>
26 if [ -n "$UDEV_LOG" ]; then
27         if [ "$UDEV_LOG" -ge 7 ]; then
28                 set -x
29         fi
30 fi
31
32 RULES_FILE='/etc/udev/rules.d/70-persistent-net.rules'
33
34 . /lib/udev/rule_generator.functions
35
36 interface_name_taken() {
37         local value="$(find_all_rules 'NAME=' $INTERFACE)"
38         if [ "$value" ]; then
39                 return 0
40         else
41                 return 1
42         fi
43 }
44
45 find_next_available() {
46         raw_find_next_available "$(find_all_rules 'NAME=' "$1")"
47 }
48
49 write_rule() {
50         local match="$1"
51         local name="$2"
52         local comment="$3"
53
54         {
55         if [ "$PRINT_HEADER" ]; then
56                 PRINT_HEADER=
57                 echo "# This file was automatically generated by the $0"
58                 echo "# program, run by the persistent-net-generator.rules rules file."
59                 echo "#"
60                 echo "# You can modify it, as long as you keep each rule on a single"
61                 echo "# line, and change only the value of the NAME= key."
62         fi
63
64         echo ""
65         [ "$comment" ] && echo "# $comment"
66         echo "SUBSYSTEM==\"net\", ACTION==\"add\"$match, NAME=\"$name\""
67         } >> $RULES_FILE
68 }
69
70 if [ -z "$INTERFACE" ]; then
71         echo "missing \$INTERFACE" >&2
72         exit 1
73 fi
74
75 # Prevent concurrent processes from modifying the file at the same time.
76 lock_rules_file
77
78 # Check if the rules file is writeable.
79 choose_rules_file
80
81 # the DRIVERS key is needed to not match bridges and VLAN sub-interfaces
82 if [ "$MATCHADDR" ]; then
83         match="$match, DRIVERS==\"?*\", ATTR{address}==\"$MATCHADDR\""
84 fi
85
86 if [ "$MATCHDRV" ]; then
87         match="$match, DRIVERS==\"$MATCHDRV\""
88 fi
89
90 if [ "$MATCHDEVID" ]; then
91         match="$match, ATTR{dev_id}==\"$MATCHDEVID\""
92 fi
93
94 if [ "$MATCHID" ]; then
95         match="$match, KERNELS==\"$MATCHID\""
96 fi
97
98 if [ "$MATCHIFTYPE" ]; then
99         match="$match, ATTR{type}==\"$MATCHIFTYPE\""
100 fi
101
102 if [ -z "$match" ]; then
103         echo "missing valid match" >&2
104         unlock_rules_file
105         exit 1
106 fi
107
108 basename=${INTERFACE%%[0-9]*}
109 match="$match, KERNEL==\"$basename*\""
110
111 if [ "$INTERFACE_NAME" ]; then
112         # external tools may request a custom name
113         COMMENT="$COMMENT (custom name provided by external tool)"
114         if [ "$INTERFACE_NAME" != "$INTERFACE" ]; then
115                 INTERFACE=$INTERFACE_NAME;
116                 echo "INTERFACE_NEW=$INTERFACE"
117         fi
118 else
119         # if a rule using the current name already exists, find a new name
120         if interface_name_taken; then
121                 INTERFACE="$basename$(find_next_available "$basename[0-9]*")"
122                 echo "INTERFACE_NEW=$INTERFACE"
123         fi
124 fi
125
126 write_rule "$match" "$INTERFACE" "$COMMENT"
127
128 unlock_rules_file
129
130 exit 0