init-net-rules.sh 3.5 KB

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