init-net-rules.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #! /bin/bash
  2. # This script generates rules for persistent network device naming
  3. # Data from udev-182 75-persistent-net-generator.rules
  4. # Updated fof udev-197 (DEVICES=en*)
  5. RULES=/etc/udev/rules.d/70-persistent-net.rules
  6. DEVICES=$(eval echo /sys/class/net/{en*,eth*,ath*,wlan*[0-9],msh*,ra*,sta*,ctc*,lcs*,hsi*})
  7. function usage
  8. {
  9. echo $msg
  10. echo "init-net-rules.sh is an LFS-specific script to initialize"
  11. echo "$RULES"
  12. exit 1
  13. }
  14. declare -A VENDORS_IGNORED
  15. VENDORS_IGNORED['52:54:00:']="kvm"
  16. VENDORS_IGNORED['00:0c:29:']="vmware"
  17. VENDORS_IGNORED['00:50:56:']="vmware"
  18. VENDORS_IGNORED['00:15:5d:']="hyper-v"
  19. VENDORS_IGNORED['00:00:00:']="invalid"
  20. declare -A VENDORS
  21. VENDORS['02:07:01:']="Interlan, DEC, etc"
  22. VENDORS['02:60:60:']="3com"
  23. VENDORS['02:60:8c:']="3Com IBM PC; Imagen. etc"
  24. VENDORS['02:a0:c9:']="intel"
  25. VENDORS['02:aa:3c:']="Olivetti"
  26. VENDORS['02:cf:1f:']="Masscomp, Silicon Graphics, etc"
  27. VENDORS['02:e0:3b:']="Gigabit"
  28. VENDORS['02:e6:d3:']="BTI"
  29. VENDORS['52:54:00:']="Realtek"
  30. VENDORS['52:54:4c:']="Novell"
  31. VENDORS['52:54:ab:']="Realtek"
  32. VENDORS['e2:0c:0f:']="Kingston"
  33. VENDORS['00:16:3e:']="Xensource"
  34. function ignore_if
  35. {
  36. if [[ "${VENDORS_IGNORED[$VENDOR]}" != "" ]]; then return 0; fi
  37. if [[ "${VENDORS[$VENDOR]}" != "" ]]; then return 1; fi
  38. byte2=$(echo $VENDOR | cut -c2)
  39. if echo $byte2 | grep -q "[2367abef]"; then return 0; fi
  40. return 1 # Default is to not ignore
  41. }
  42. function comment
  43. {
  44. # Not implemented
  45. # SUBSYSTEMS=="pci"
  46. # export COMMENT="PCI device $attr{vendor}:$attr{device} ($driver)"
  47. # SUBSYSTEMS=="usb", ATTRS{idVendor}=="?*"
  48. # export COMMENT="USB device 0x$attr{idVendor}:0x$attr{idProduct} ($driver)"
  49. # SUBSYSTEMS=="pcmcia",
  50. # export COMMENT="PCMCIA device $attr{card_id}:$attr{manf_id} ($driver)"
  51. # SUBSYSTEMS=="ieee1394",
  52. # export COMMENT="Firewire device $attr{host_id})"
  53. # ibmveth likes to use "locally administered" MAC addresses
  54. # DRIVERS=="ibmveth",
  55. # export COMMENT="ibmveth ($id)"
  56. # S/390 uses id matches only, do not use MAC address match
  57. # SUBSYSTEMS=="ccwgroup",
  58. # export COMMENT="S/390 $driver device at $id",
  59. # export MATCHID="$id"
  60. # export MATCHDRV="$driver"
  61. # export MATCHADDR=""
  62. # Default
  63. driver=$(basename $(readlink -f $NIC/device/driver/module))
  64. export COMMENT="net device ${driver}"
  65. }
  66. if ! mountpoint -q /sys; then
  67. msg="/sys must be mounted"
  68. usage
  69. fi
  70. if ! mountpoint -q /proc; then
  71. msg="/proc must be mounted"
  72. usage
  73. fi
  74. if [ -e $RULES ]; then
  75. msg="The rules file already exists"
  76. usage
  77. fi
  78. # Ignore Xen virtual interfaces
  79. if [ -e /proc/xen ]; then
  80. msg="The rules file should not be created in the Xen environment"
  81. usage
  82. fi
  83. # Variables used to communicate with write_net_rules:
  84. # INTERFACE simple interface name
  85. # MATCHADDR MAC address used for the match
  86. # MATCHID bus_id used for the match
  87. # MATCHDRV driver name used for the match
  88. # MATCHIFTYPE interface type match
  89. # COMMENT comment to add to the generated rule
  90. # INTERFACE_NAME requested name supplied by external tool
  91. # INTERFACE_NEW new interface name returned by rule writer
  92. for NIC in $DEVICES; do
  93. IF=${NIC##*/}
  94. if echo $NIC | grep -q '*' ; then continue; fi
  95. export INTERFACE=${NIC##*/} # Simple interface name
  96. export MATCHADDR="$(cat $NIC/address)" # Read MAC address
  97. VENDOR=$(echo $MATCHADDR | cut -c-9)
  98. if ignore_if; then continue; fi
  99. export MATCHDEVID="$(cat $NIC/dev_id)"
  100. export MATCHIFTYPE="$(cat $NIC/type)" # Read interface type
  101. comment
  102. /lib/udev/write_net_rules
  103. done