write_net_rules 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!/bin/sh -e
  2. # This script is run to create persistent network device naming rules
  3. # based on properties of the device.
  4. # If the interface needs to be renamed, INTERFACE_NEW=<name> will be printed
  5. # on stdout to allow udev to IMPORT it.
  6. # variables used to communicate:
  7. # MATCHADDR MAC address used for the match
  8. # MATCHID bus_id used for the match
  9. # MATCHDEVID dev_id used for the match
  10. # MATCHDRV driver name used for the match
  11. # MATCHIFTYPE interface type match
  12. # COMMENT comment to add to the generated rule
  13. # INTERFACE_NAME requested name supplied by external tool
  14. # INTERFACE_NEW new interface name returned by rule writer
  15. # Copyright (C) 2006 Marco d'Itri <md@Linux.IT>
  16. # Copyright (C) 2007 Kay Sievers <kay.sievers@vrfy.org>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU General Public License as published by
  20. # the Free Software Foundation, either version 2 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. # debug, if UDEV_LOG=<debug>
  31. if [ -n "$UDEV_LOG" ]; then
  32. if [ "$UDEV_LOG" -ge 7 ]; then
  33. set -x
  34. fi
  35. fi
  36. RULES_FILE='/etc/udev/rules.d/70-persistent-net.rules'
  37. . /lib/udev/rule_generator.functions
  38. interface_name_taken() {
  39. local value="$(find_all_rules 'NAME=' $INTERFACE)"
  40. if [ "$value" ]; then
  41. return 0
  42. else
  43. return 1
  44. fi
  45. }
  46. find_next_available() {
  47. raw_find_next_available "$(find_all_rules 'NAME=' "$1")"
  48. }
  49. write_rule() {
  50. local match="$1"
  51. local name="$2"
  52. local comment="$3"
  53. {
  54. if [ "$PRINT_HEADER" ]; then
  55. PRINT_HEADER=
  56. echo "# This file was automatically generated by the $0"
  57. echo "# program, run by the persistent-net-generator.rules rules file."
  58. echo "#"
  59. echo "# You can modify it, as long as you keep each rule on a single"
  60. echo "# line, and change only the value of the NAME= key."
  61. fi
  62. echo ""
  63. [ "$comment" ] && echo "# $comment"
  64. echo "SUBSYSTEM==\"net\", ACTION==\"add\"$match, NAME=\"$name\""
  65. } >> $RULES_FILE
  66. }
  67. if [ -z "$INTERFACE" ]; then
  68. echo "missing \$INTERFACE" >&2
  69. exit 1
  70. fi
  71. # Prevent concurrent processes from modifying the file at the same time.
  72. lock_rules_file
  73. # Check if the rules file is writeable.
  74. choose_rules_file
  75. # the DRIVERS key is needed to not match bridges and VLAN sub-interfaces
  76. if [ "$MATCHADDR" ]; then
  77. match="$match, DRIVERS==\"?*\", ATTR{address}==\"$MATCHADDR\""
  78. fi
  79. if [ "$MATCHDRV" ]; then
  80. match="$match, DRIVERS==\"$MATCHDRV\""
  81. fi
  82. if [ "$MATCHDEVID" ]; then
  83. match="$match, ATTR{dev_id}==\"$MATCHDEVID\""
  84. fi
  85. if [ "$MATCHID" ]; then
  86. match="$match, KERNELS==\"$MATCHID\""
  87. fi
  88. if [ "$MATCHIFTYPE" ]; then
  89. match="$match, ATTR{type}==\"$MATCHIFTYPE\""
  90. fi
  91. if [ -z "$match" ]; then
  92. echo "missing valid match" >&2
  93. unlock_rules_file
  94. exit 1
  95. fi
  96. basename=${INTERFACE%%[0-9]*}
  97. match="$match, KERNEL==\"$basename*\""
  98. if [ "$INTERFACE_NAME" ]; then
  99. # external tools may request a custom name
  100. COMMENT="$COMMENT (custom name provided by external tool)"
  101. if [ "$INTERFACE_NAME" != "$INTERFACE" ]; then
  102. INTERFACE=$INTERFACE_NAME;
  103. echo "INTERFACE_NEW=$INTERFACE"
  104. fi
  105. else
  106. # if a rule using the current name already exists, find a new name
  107. if interface_name_taken; then
  108. INTERFACE="$basename$(find_next_available "$basename[0-9]*")"
  109. # prevent INTERFACE from being "eth" instead of "eth0"
  110. [ "$INTERFACE" = "${INTERFACE%%[ \[\]0-9]*}" ] && INTERFACE=${INTERFACE}0
  111. echo "INTERFACE_NEW=$INTERFACE"
  112. fi
  113. fi
  114. write_rule "$match" "$INTERFACE" "$COMMENT"
  115. unlock_rules_file
  116. exit 0