source: udev-lfs/write_net_rules@ b2b1e1a

10.0 10.0-rc1 10.1 10.1-rc1 11.0 11.0-rc1 11.0-rc2 11.0-rc3 11.1 11.1-rc1 11.2 11.2-rc1 11.3 11.3-rc1 12.0 12.0-rc1 12.1 12.1-rc1 7.3 7.4 7.5 7.5-systemd 7.6 7.6-systemd 7.7 7.7-systemd 7.8 7.8-systemd 7.9 7.9-systemd 8.0 8.1 8.2 8.3 8.4 9.0 9.1 arm bdubbs/gcc13 ml-11.0 multilib renodr/libudev-from-systemd s6-init trunk xry111/arm64 xry111/arm64-12.0 xry111/clfs-ng xry111/lfs-next xry111/loongarch xry111/loongarch-12.0 xry111/loongarch-12.1 xry111/mips64el xry111/pip3 xry111/rust-wip-20221008 xry111/update-glibc
Last change on this file since b2b1e1a was b2b1e1a, checked in by Bruce Dubbs <bdubbs@…>, 12 years ago

Move directory to make build easier

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

  • Property mode set to 100755
File size: 4.3 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]*}
118match="$match, KERNEL==\"$basename*\""
119
120if [ "$INTERFACE_NAME" ]; then
121 # external tools may request a custom name
122 COMMENT="$COMMENT (custom name provided by external tool)"
123 if [ "$INTERFACE_NAME" != "$INTERFACE" ]; then
124 INTERFACE=$INTERFACE_NAME;
125 echo "INTERFACE_NEW=$INTERFACE"
126 fi
127else
128 # if a rule using the current name already exists, find a new name
129 if interface_name_taken; then
130 INTERFACE="$basename$(find_next_available "$basename[0-9]*")"
131 # prevent INTERFACE from being "eth" instead of "eth0"
132 [ "$INTERFACE" = "${INTERFACE%%[ \[\]0-9]*}" ] && INTERFACE=${INTERFACE}0
133 echo "INTERFACE_NEW=$INTERFACE"
134 fi
135fi
136
137write_rule "$match" "$INTERFACE" "$COMMENT"
138
139unlock_rules_file
140
141exit 0
Note: See TracBrowser for help on using the repository browser.