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 |
alshabib | e16ef4c | 2016-05-27 17:13:23 -0700 | [diff] [blame] | 52 | mlx4_en) |
| 53 | RESULT="MLX4_EN" |
| 54 | ;; |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 55 | i40e) |
| 56 | RESULT="I40G" |
| 57 | ;; |
David K. Bainbridge | 4ec841c | 2016-05-11 22:10:15 -0700 | [diff] [blame] | 58 | igb|e1000) |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 59 | RESULT="ETH" |
| 60 | ;; |
| 61 | *) ;; |
| 62 | esac |
| 63 | echo $RESULT |
| 64 | } |
| 65 | |
| 66 | function get_mac { |
| 67 | echo $(ifconfig $1 | grep HWaddr | awk '{print $5}') |
| 68 | } |
| 69 | |
| 70 | function generate_persistent_names { |
| 71 | local OUT=$NAMES_FILE |
| 72 | #"70-persistent-net.rules" |
| 73 | rm -rf $OUT |
| 74 | |
| 75 | IDX=0 |
| 76 | for i in $(cat $1 | sort); do |
David K. Bainbridge | 55a2385 | 2016-05-20 08:34:40 -0700 | [diff] [blame] | 77 | echo "SUBSYSTEM==\"net\", ACTION==\"add\", DRIVERS==\"?*\", ATTR{address}==\"$i\", ATTR{dev_id}==\"0x0\", ATTR{type}==\"1\", KERNEL==\"*\", NAME=\"eth$IDX\"" >> $OUT |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 78 | IDX=$(expr $IDX + 1) |
| 79 | done |
| 80 | |
| 81 | for i in $(cat $2 | sort); do |
| 82 | echo "SUBSYSTEM==\"net\", ACTION==\"add\", DRIVERS==\"?*\", ATTR{address}==\"$i\", NAME=\"eth$IDX\"" >> $OUT |
| 83 | IDX=$(expr $IDX + 1) |
| 84 | done |
| 85 | } |
| 86 | |
David K. Bainbridge | 8db2f30 | 2016-05-19 23:41:13 -0700 | [diff] [blame] | 87 | # 40G_LIST ETH_LIST FAB_IFACE EXT_IFACE MGT_IFACE |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 88 | function generate_interfaces { |
| 89 | OUT=$IFACES_FILE |
| 90 | rm -rf $OUT |
| 91 | echo "# This file describes the network interfaces available on your system" >> $OUT |
| 92 | echo "# and how to activate them. For more information, see interfaces(5)." >> $OUT |
| 93 | echo "" >> $OUT |
| 94 | echo "# The loopback network interface" >> $OUT |
| 95 | echo "auto lo" >> $OUT |
| 96 | echo "iface lo inet loopback" >> $OUT |
| 97 | echo "" >> $OUT |
| 98 | |
| 99 | IDX=0 |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 100 | for i in $(cat $1); do |
David K. Bainbridge | 8db2f30 | 2016-05-19 23:41:13 -0700 | [diff] [blame] | 101 | if [ "eth$IDX" == "$3" ]; then |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 102 | echo "auto eth$IDX" >> $OUT |
| 103 | echo "iface eth$IDX inet static" >> $OUT |
David K. Bainbridge | 8db2f30 | 2016-05-19 23:41:13 -0700 | [diff] [blame] | 104 | echo " address $FAB_IP" >> $OUT |
| 105 | echo " network $FAB_NETWORK" >> $OUT |
| 106 | echo " netmask $FAB_NETMASK" >> $OUT |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 107 | FIRST=0 |
| 108 | else |
| 109 | echo "iface eth$IDX inet manual" >> $OUT |
| 110 | fi |
| 111 | echo "" >> $OUT |
| 112 | IDX=$(expr $IDX + 1) |
| 113 | done |
| 114 | |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 115 | for i in $(cat $2); do |
David K. Bainbridge | 8db2f30 | 2016-05-19 23:41:13 -0700 | [diff] [blame] | 116 | if [ "eth$IDX" == "$4" ]; then |
| 117 | if [ "$EXT_IP" == "dhcp" ]; then |
| 118 | echo "auto eth$IDX" >> $OUT |
| 119 | echo "iface eth$IDX inet dhcp" >> $OUT |
| 120 | elif [ "$EXT_IP" == "manual" ]; then |
| 121 | echo "iface eth$IDX inet manual" >> $OUT |
| 122 | else |
| 123 | echo "auto eth$IDX" >> $OUT |
| 124 | echo "iface eth$IDX inet static" >> $OUT |
| 125 | echo " address $EXT_IP" >> $OUT |
| 126 | echo " network $EXT_NETWORK" >> $OUT |
| 127 | echo " netmask $EXT_NETMASK" >> $OUT |
| 128 | echo " broadcast $EXT_BROADCAST" >> $OUT |
| 129 | echo " gateway $EXT_GW" >> $OUT |
| 130 | echo " dns-nameservers 8.8.8.8 8.8.4.4" >> $OUT |
| 131 | echo " dns-search cord.lab" >> $OUT |
| 132 | fi |
| 133 | elif [ "eth$IDX" == "$5" ]; then |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 134 | echo "auto eth$IDX" >> $OUT |
| 135 | echo "iface eth$IDX inet dhcp" >> $OUT |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 136 | else |
| 137 | echo "iface eth$IDX inet manual" >> $OUT |
| 138 | fi |
| 139 | echo "" >> $OUT |
| 140 | IDX=$(expr $IDX + 1) |
| 141 | done |
| 142 | } |
| 143 | |
David K. Bainbridge | 8db2f30 | 2016-05-19 23:41:13 -0700 | [diff] [blame] | 144 | FAB_IFACE=$1 |
| 145 | FAB_ADDR=$2 |
| 146 | FAB_IP=$(echo $FAB_ADDR | cut -d/ -f1) |
| 147 | FAB_MASKBITS=$(echo $FAB_ADDR | cut -d/ -f2) |
| 148 | FAB_NETWORK=$(network $FAB_IP $FAB_MASKBITS) |
| 149 | FAB_NETMASK=$(netmask $FAB_MASKBITS) |
| 150 | |
| 151 | EXT_IFACE=$3 |
| 152 | EXT_ADDR=$4 |
| 153 | if [ "$EXT_ADDR" != "dhcp" ]; then |
| 154 | EXT_IP=$(echo $EXT_ADDR | cut -d/ -f1) |
| 155 | EXT_MASKBITS=$(echo $EXT_ADDR | cut -d/ -f2) |
| 156 | EXT_NETWORK=$(network $EXT_IP $EXT_MASKBITS) |
| 157 | EXT_NETMASK=$(netmask $EXT_MASKBITS) |
| 158 | EXT_BROADCAST=$(broadcast $EXT_IP $EXT_MASKBITS) |
| 159 | EXT_GW=$(first $EXT_ADDR) |
| 160 | fi |
| 161 | MGT_IFACE=$5 |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 162 | |
| 163 | LIST_ETH=$(mktemp -u) |
| 164 | LIST_40G=$(mktemp -u) |
| 165 | IFACES_FILE=$(mktemp -u) |
| 166 | NAMES_FILE=$(mktemp -u) |
| 167 | |
| 168 | IFACES=$(ifconfig -a | grep "^[a-z]" | awk '{print $1}') |
| 169 | |
| 170 | for i in $IFACES; do |
| 171 | TYPE=$(guess_type $i) |
| 172 | case $TYPE in |
| 173 | ETH) |
| 174 | echo "$(get_mac $i)" >> $LIST_ETH |
| 175 | ;; |
alshabib | e16ef4c | 2016-05-27 17:13:23 -0700 | [diff] [blame] | 176 | I40G|MLX4_EN) |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 177 | echo "$(get_mac $i)" >> $LIST_40G |
| 178 | ;; |
| 179 | *) ;; |
| 180 | esac |
| 181 | done |
| 182 | |
David K. Bainbridge | 8db2f30 | 2016-05-19 23:41:13 -0700 | [diff] [blame] | 183 | CHANGED="false" |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 184 | |
David K. Bainbridge | 8db2f30 | 2016-05-19 23:41:13 -0700 | [diff] [blame] | 185 | generate_interfaces $LIST_40G $LIST_ETH $FAB_IFACE $EXT_IFACE $MGT_IFACE |
| 186 | |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 187 | diff /etc/network/interfaces $IFACES_FILE 2>&1 > /dev/null |
| 188 | if [ $? -ne 0 ]; then |
David K. Bainbridge | 8db2f30 | 2016-05-19 23:41:13 -0700 | [diff] [blame] | 189 | CHANGED="true" |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 190 | cp /etc/network/interfaces /etc/network/interfaces.1 |
| 191 | cp $IFACES_FILE /etc/network/interfaces |
| 192 | fi |
| 193 | |
David K. Bainbridge | 8db2f30 | 2016-05-19 23:41:13 -0700 | [diff] [blame] | 194 | generate_persistent_names $LIST_40G $LIST_ETH $FAB_IFACE $EXT_IFACE |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 195 | if [ -r /etc/udev/rules.d/70-persistent-net.rules ]; then |
| 196 | diff /etc/udev/rules.d/70-persistent-net.rules $NAMES_FILE 2>&1 > /dev/null |
| 197 | if [ $? -ne 0 ]; then |
David K. Bainbridge | 8db2f30 | 2016-05-19 23:41:13 -0700 | [diff] [blame] | 198 | CHANGED="true" |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 199 | cp /etc/udev/rules.d/70-persistent-net.rules /etc/udev/rules.d/70-persistent-net.rules.1 |
| 200 | cp $NAMES_FILE /etc/udev/rules.d/70-persistent-net.rules |
| 201 | fi |
| 202 | else |
David K. Bainbridge | 8db2f30 | 2016-05-19 23:41:13 -0700 | [diff] [blame] | 203 | CHANGED="true" |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 204 | cp $NAMES_FILE /etc/udev/rules.d/70-persistent-net.rules |
| 205 | fi |
| 206 | |
| 207 | rm -rf $IFACES_FILE |
| 208 | rm -rf $NAMES_FILE |
| 209 | rm -rf $LIST_ETH |
| 210 | rm -rf $LIST_40G |
| 211 | |
David K. Bainbridge | 8db2f30 | 2016-05-19 23:41:13 -0700 | [diff] [blame] | 212 | echo -n $CHANGED |