David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | BASE="10.4" |
| 4 | LEAF="1" |
| 5 | SERVER="1" |
| 6 | |
| 7 | function guess_type { |
| 8 | local CNT=$(echo "$1" | sed -e 's/[:.]/ /g' | wc -w) |
| 9 | if [ $CNT -ne 1 ]; then |
| 10 | # drop all sub and vlan interfaces |
| 11 | echo "DNC" |
| 12 | return |
| 13 | fi |
| 14 | local DRIVER=$(ethtool -i $1 2>/dev/null | grep driver | awk '{print $2}') |
| 15 | local RESULT="DNC" |
| 16 | case $DRIVER in |
| 17 | i40e) |
| 18 | RESULT="I40G" |
| 19 | ;; |
| 20 | igb) |
| 21 | RESULT="ETH" |
| 22 | ;; |
| 23 | *) ;; |
| 24 | esac |
| 25 | echo $RESULT |
| 26 | } |
| 27 | |
| 28 | function get_mac { |
| 29 | echo $(ifconfig $1 | grep HWaddr | awk '{print $5}') |
| 30 | } |
| 31 | |
| 32 | function generate_persistent_names { |
| 33 | local OUT="70-persistent-net.rules" |
| 34 | rm -rf $OUT |
| 35 | |
| 36 | IDX=0 |
| 37 | for i in $(cat $1 | sort); do |
| 38 | echo "SUBSYSTEM==\"net\", ACTION==\"add\", DRIVERS==\"?*\", ATTR{address}==\"$i\", ATTR{dev_id}==\"0x0\", ATTR{type}==\"1\", KERNEL==\"eth*\", NAME=\"eth$IDX\"" >> $OUT |
| 39 | IDX=$(expr $IDX + 1) |
| 40 | done |
| 41 | |
| 42 | for i in $(cat $2 | sort); do |
| 43 | echo "SUBSYSTEM==\"net\", ACTION==\"add\", DRIVERS==\"?*\", ATTR{address}==\"$i\", NAME=\"eth$IDX\"" >> $OUT |
| 44 | IDX=$(expr $IDX + 1) |
| 45 | done |
| 46 | } |
| 47 | |
| 48 | function generate_interfaces { |
| 49 | OUT="interfaces" |
| 50 | rm -rf $OUT |
| 51 | echo "# This file describes the network interfaces available on your system" >> $OUT |
| 52 | echo "# and how to activate them. For more information, see interfaces(5)." >> $OUT |
| 53 | echo "" >> $OUT |
| 54 | echo "# The loopback network interface" >> $OUT |
| 55 | echo "auto lo" >> $OUT |
| 56 | echo "iface lo inet loopback" >> $OUT |
| 57 | echo "" >> $OUT |
| 58 | |
| 59 | IDX=0 |
| 60 | FIRST=1 |
| 61 | for i in $(cat $1); do |
| 62 | if [ $FIRST -eq 1 ]; then |
| 63 | echo "auto eth$IDX" >> $OUT |
| 64 | echo "iface eth$IDX inet static" >> $OUT |
| 65 | echo " address $BASE.$LEAF.$SERVER" >> $OUT |
| 66 | echo " network $BASE.$LEAF.0" >> $OUT |
| 67 | echo " netmask 255.255.255.0" >> $OUT |
| 68 | FIRST=0 |
| 69 | else |
| 70 | echo "iface eth$IDX inet manual" >> $OUT |
| 71 | fi |
| 72 | echo "" >> $OUT |
| 73 | IDX=$(expr $IDX + 1) |
| 74 | done |
| 75 | |
| 76 | FIRST=1 |
| 77 | for i in $(cat $2); do |
| 78 | if [ $FIRST -eq 1 ]; then |
| 79 | echo "auto eth$IDX" >> $OUT |
| 80 | echo "iface eth$IDX inet dhcp" >> $OUT |
| 81 | FIRST=0 |
| 82 | else |
| 83 | echo "iface eth$IDX inet manual" >> $OUT |
| 84 | fi |
| 85 | echo "" >> $OUT |
| 86 | IDX=$(expr $IDX + 1) |
| 87 | done |
| 88 | } |
| 89 | |
| 90 | LIST_ETH=$(mktemp -u) |
| 91 | LIST_40G=$(mktemp -u) |
| 92 | IFACES=$(ifconfig -a | grep "^[a-z]" | awk '{print $1}') |
| 93 | |
| 94 | for i in $IFACES; do |
| 95 | TYPE=$(guess_type $i) |
| 96 | case $TYPE in |
| 97 | ETH) |
| 98 | echo "$(get_mac $i)" >> $LIST_ETH |
| 99 | ;; |
| 100 | I40G) |
| 101 | echo "$(get_mac $i)" >> $LIST_40G |
| 102 | ;; |
| 103 | *) ;; |
| 104 | esac |
| 105 | done |
| 106 | |
| 107 | generate_persistent_names $LIST_40G $LIST_ETH |
| 108 | generate_interfaces $LIST_40G $LIST_ETH |
| 109 | rm -rf $LIST_ETH |
| 110 | rm -rf $LIST_40G |