source: udev-lfs/write_net_rules@ c74edb3

ml-11.0 multilib
Last change on this file since c74edb3 was f8fe8970, checked in by Bruce Dubbs <bdubbs@…>, 10 years ago

Add groups and a user for systemd.
Correct files so udev rule for persistent network cards works properly.

git-svn-id: http://svn.linuxfromscratch.org/LFS/trunk/BOOK@10525 4aa44e1e-78dd-0310-a6d2-fbcd4c07a689

  • Property mode set to 100755
File size: 4.2 KB
Line 
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>
35if [ -n "$UDEV_LOG" ]; then
36 if [ "$UDEV_LOG" -ge 7 ]; then
37 set -x
38 fi
39fi
40
41RULES_FILE='/etc/udev/rules.d/70-persistent-net.rules'
42
43. /lib/udev/rule_generator.functions
44
45interface_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
54find_next_available() {
55 raw_find_next_available "$(find_all_rules 'NAME=' "$1")"
56}
57
58write_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
79if [ -z "$INTERFACE" ]; then
80 echo "missing \$INTERFACE" >&2
81 exit 1
82fi
83
84# Prevent concurrent processes from modifying the file at the same time.
85lock_rules_file
86
87# Check if the rules file is writeable.
88choose_rules_file
89
90# the DRIVERS key is needed to not match bridges and VLAN sub-interfaces
91if [ "$MATCHADDR" ]; then
92 match="$match, DRIVERS==\"?*\", ATTR{address}==\"$MATCHADDR\""
93fi
94
95if [ "$MATCHDRV" ]; then
96 match="$match, DRIVERS==\"$MATCHDRV\""
97fi
98
99if [ "$MATCHDEVID" ]; then
100 match="$match, ATTR{dev_id}==\"$MATCHDEVID\""
101fi
102
103if [ "$MATCHID" ]; then
104 match="$match, KERNELS==\"$MATCHID\""
105fi
106
107if [ "$MATCHIFTYPE" ]; then
108 match="$match, ATTR{type}==\"$MATCHIFTYPE\""
109fi
110
111if [ -z "$match" ]; then
112 echo "missing valid match" >&2
113 unlock_rules_file
114 exit 1
115fi
116
117basename=${INTERFACE%%[0-9]*}
118
119if [ "$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
126else
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
134fi
135
136write_rule "$match" "$INTERFACE" "$COMMENT"
137
138unlock_rules_file
139
140exit 0
Note: See TracBrowser for help on using the repository browser.