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