David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | function ip2int { |
| 4 | local a b c d |
| 5 | { IFS=. read a b c d; } <<< $1 |
| 6 | echo $(((((((a << 8) | b) << 8) | c) << 8) | d)) |
| 7 | } |
| 8 | |
| 9 | function int2ip { |
| 10 | local ui32=$1; shift |
| 11 | local ip n |
| 12 | for n in 1 2 3 4; do |
| 13 | ip=$((ui32 & 0xff))${ip:+.}$ip |
| 14 | ui32=$((ui32 >> 8)) |
| 15 | done |
| 16 | echo $ip |
| 17 | } |
| 18 | |
| 19 | function netmask { |
| 20 | local mask=$((0xffffffff << (32 - $1))); shift |
| 21 | int2ip $mask |
| 22 | } |
| 23 | |
| 24 | function broadcast { |
| 25 | local addr=$(ip2int $1); shift |
| 26 | local mask=$((0xffffffff << (32 -$1))); shift |
| 27 | int2ip $((addr | ~mask)) |
| 28 | } |
| 29 | |
| 30 | function network { |
| 31 | local addr=$(ip2int $1); shift |
| 32 | local mask=$((0xffffffff << (32 -$1))); shift |
| 33 | int2ip $((addr & mask)) |
| 34 | } |
| 35 | |
| 36 | function first { |
| 37 | local addr=$(ip2int $1) |
| 38 | addr=`expr $addr + 1` |
| 39 | int2ip $addr |
| 40 | } |
| 41 | |
| 42 | function guess_type { |
| 43 | local CNT=$(echo "$1" | sed -e 's/[:.]/ /g' | wc -w) |
| 44 | if [ $CNT -ne 1 ]; then |
| 45 | # drop all sub and vlan interfaces |
| 46 | echo "DNC" |
| 47 | return |
| 48 | fi |
| 49 | local DRIVER=$(ethtool -i $1 2>/dev/null | grep driver | awk '{print $2}') |
| 50 | local RESULT="DNC" |
| 51 | case $DRIVER in |
| 52 | i40e) |
| 53 | RESULT="I40G" |
| 54 | ;; |
David K. Bainbridge | 4ec841c | 2016-05-11 22:10:15 -0700 | [diff] [blame^] | 55 | igb|e1000) |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 56 | RESULT="ETH" |
| 57 | ;; |
| 58 | *) ;; |
| 59 | esac |
| 60 | echo $RESULT |
| 61 | } |
| 62 | |
| 63 | function get_mac { |
| 64 | echo $(ifconfig $1 | grep HWaddr | awk '{print $5}') |
| 65 | } |
| 66 | |
| 67 | function generate_persistent_names { |
| 68 | local OUT=$NAMES_FILE |
| 69 | #"70-persistent-net.rules" |
| 70 | rm -rf $OUT |
| 71 | |
| 72 | IDX=0 |
| 73 | for i in $(cat $1 | sort); do |
| 74 | echo "SUBSYSTEM==\"net\", ACTION==\"add\", DRIVERS==\"?*\", ATTR{address}==\"$i\", ATTR{dev_id}==\"0x0\", ATTR{type}==\"1\", KERNEL==\"eth*\", NAME=\"eth$IDX\"" >> $OUT |
| 75 | IDX=$(expr $IDX + 1) |
| 76 | done |
| 77 | |
| 78 | for i in $(cat $2 | sort); do |
| 79 | echo "SUBSYSTEM==\"net\", ACTION==\"add\", DRIVERS==\"?*\", ATTR{address}==\"$i\", NAME=\"eth$IDX\"" >> $OUT |
| 80 | IDX=$(expr $IDX + 1) |
| 81 | done |
| 82 | } |
| 83 | |
| 84 | function generate_interfaces { |
| 85 | OUT=$IFACES_FILE |
| 86 | rm -rf $OUT |
| 87 | echo "# This file describes the network interfaces available on your system" >> $OUT |
| 88 | echo "# and how to activate them. For more information, see interfaces(5)." >> $OUT |
| 89 | echo "" >> $OUT |
| 90 | echo "# The loopback network interface" >> $OUT |
| 91 | echo "auto lo" >> $OUT |
| 92 | echo "iface lo inet loopback" >> $OUT |
| 93 | echo "" >> $OUT |
| 94 | |
| 95 | IDX=0 |
| 96 | FIRST=1 |
| 97 | for i in $(cat $1); do |
| 98 | if [ $FIRST -eq 1 ]; then |
| 99 | echo "auto eth$IDX" >> $OUT |
| 100 | echo "iface eth$IDX inet static" >> $OUT |
| 101 | echo " address $IP" >> $OUT |
| 102 | echo " network $NETWORK" >> $OUT |
| 103 | echo " netmask $NETMASK" >> $OUT |
| 104 | FIRST=0 |
| 105 | else |
| 106 | echo "iface eth$IDX inet manual" >> $OUT |
| 107 | fi |
| 108 | echo "" >> $OUT |
| 109 | IDX=$(expr $IDX + 1) |
| 110 | done |
| 111 | |
| 112 | FIRST=1 |
| 113 | for i in $(cat $2); do |
| 114 | if [ $FIRST -eq 1 ]; then |
| 115 | echo "auto eth$IDX" >> $OUT |
| 116 | echo "iface eth$IDX inet dhcp" >> $OUT |
| 117 | FIRST=0 |
| 118 | else |
| 119 | echo "iface eth$IDX inet manual" >> $OUT |
| 120 | fi |
| 121 | echo "" >> $OUT |
| 122 | IDX=$(expr $IDX + 1) |
| 123 | done |
| 124 | } |
| 125 | |
| 126 | ADDR=$1 |
| 127 | IP=$(echo $ADDR | cut -d/ -f1) |
| 128 | MASKBITS=$(echo $ADDR | cut -d/ -f2) |
| 129 | NETWORK=$(network $IP $MASKBITS) |
| 130 | NETMASK=$(netmask $MASKBITS) |
| 131 | |
| 132 | LIST_ETH=$(mktemp -u) |
| 133 | LIST_40G=$(mktemp -u) |
| 134 | IFACES_FILE=$(mktemp -u) |
| 135 | NAMES_FILE=$(mktemp -u) |
| 136 | |
| 137 | IFACES=$(ifconfig -a | grep "^[a-z]" | awk '{print $1}') |
| 138 | |
| 139 | for i in $IFACES; do |
| 140 | TYPE=$(guess_type $i) |
| 141 | case $TYPE in |
| 142 | ETH) |
| 143 | echo "$(get_mac $i)" >> $LIST_ETH |
| 144 | ;; |
| 145 | I40G) |
| 146 | echo "$(get_mac $i)" >> $LIST_40G |
| 147 | ;; |
| 148 | *) ;; |
| 149 | esac |
| 150 | done |
| 151 | |
| 152 | RESULT="false" |
| 153 | |
| 154 | generate_interfaces $LIST_40G $LIST_ETH |
| 155 | diff /etc/network/interfaces $IFACES_FILE 2>&1 > /dev/null |
| 156 | if [ $? -ne 0 ]; then |
| 157 | RESULT="true" |
| 158 | cp /etc/network/interfaces /etc/network/interfaces.1 |
| 159 | cp $IFACES_FILE /etc/network/interfaces |
| 160 | fi |
| 161 | |
| 162 | generate_persistent_names $LIST_40G $LIST_ETH |
| 163 | if [ -r /etc/udev/rules.d/70-persistent-net.rules ]; then |
| 164 | diff /etc/udev/rules.d/70-persistent-net.rules $NAMES_FILE 2>&1 > /dev/null |
| 165 | if [ $? -ne 0 ]; then |
| 166 | RESULT="true" |
| 167 | cp /etc/udev/rules.d/70-persistent-net.rules /etc/udev/rules.d/70-persistent-net.rules.1 |
| 168 | cp $NAMES_FILE /etc/udev/rules.d/70-persistent-net.rules |
| 169 | fi |
| 170 | else |
| 171 | RESULT="true" |
| 172 | cp $NAMES_FILE /etc/udev/rules.d/70-persistent-net.rules |
| 173 | fi |
| 174 | |
| 175 | rm -rf $IFACES_FILE |
| 176 | rm -rf $NAMES_FILE |
| 177 | rm -rf $LIST_ETH |
| 178 | rm -rf $LIST_40G |
| 179 | |
| 180 | echo -n $RESULT |