chiark / gitweb /
rule_generator: move all policy from write_net_rules to the rules file
[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 #   MATCHDRV              driver name used for the match
19 #   MATCHIFTYPE           interface type match
20 #   COMMENT               comment to add to the generated rule
21 #   INTERFACE_NAME        requested name supplied by external tool
22 #   INTERFACE_NEW         new interface name returned by rule writer
23
24 RULES_FILE='/etc/udev/rules.d/70-persistent-net.rules'
25
26 . /lib/udev/rule_generator.functions
27
28 interface_name_taken() {
29         local value="$(find_all_rules 'NAME=' $INTERFACE)"
30         if [ "$value" ]; then
31                 return 0
32         else
33                 return 1
34         fi
35 }
36
37 find_next_available() {
38         raw_find_next_available "$(find_all_rules 'NAME=' "$1")"
39 }
40
41 write_rule() {
42         local match="$1"
43         local name="$2"
44         local comment="$3"
45
46         {
47         if [ "$PRINT_HEADER" ]; then
48                 PRINT_HEADER=
49                 echo "# This file was automatically generated by the $0"
50                 echo "# program run by the persistent-net-generator.rules rules file."
51                 echo "#"
52                 echo "# You can modify it, as long as you keep each rule on a single line."
53         fi
54
55         echo ""
56         [ "$comment" ] && echo "# $comment"
57         echo "SUBSYSTEM==\"net\", ACTION==\"add\"$match, NAME=\"$name\""
58         } >> $RULES_FILE
59 }
60
61 if [ -z "$INTERFACE" ]; then
62         echo "missing \$INTERFACE" >&2
63         exit 1
64 fi
65
66 # Prevent concurrent processes from modifying the file at the same time.
67 lock_rules_file
68
69 # Check if the rules file is writeable.
70 choose_rules_file
71
72 # the DRIVERS key is needed to not match bridges and VLAN sub-interfaces
73 if [ "$MATCHADDR" ]; then
74         match="$match, DRIVERS==\"?*\", ATTR{address}==\"$MATCHADDR\""
75 fi
76
77 if [ "$MATCHDRV" ]; then
78         match="$match, DRIVERS==\"$MATCHDRV\""
79 fi
80
81 if [ "$MATCHID" ]; then
82         match="$match, KERNELS==\"$MATCHID\""
83 fi
84
85 if [ "$MATCHIFTYPE" ]; then
86         match="$match, ATTR{type}==\"$MATCHIFTYPE\""
87 fi
88
89 if [ -z "$match" ]; then
90         echo "missing valid match" >&2
91         exit 1
92 fi
93
94 if [ "$INTERFACE_NAME" ]; then
95         # external tools may request a custom name
96         COMMENT="$COMMENT (custom name provided by external tool)"
97         if [ "$INTERFACE_NAME" != "$INTERFACE" ]; then
98                 INTERFACE=$INTERFACE_NAME;
99                 echo "INTERFACE_NEW=$INTERFACE"
100         fi
101 else
102         # if a rule using the current name already exists, find a new name
103         basename=${INTERFACE%%[0-9]*}
104         if interface_name_taken; then
105                 INTERFACE="$basename$(find_next_available "$basename[0-9]*")"
106                 echo "INTERFACE_NEW=$INTERFACE"
107         fi
108 fi
109
110 write_rule "$match" "$INTERFACE" "$COMMENT"
111
112 unlock_rules_file
113
114 exit 0