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 |
Yi Tseng | 8a12cb1 | 2016-08-19 01:45:21 +0800 | [diff] [blame] | 52 | mlx4_en) |
alshabib | e16ef4c | 2016-05-27 17:13:23 -0700 | [diff] [blame] | 53 | RESULT="MLX4_EN" |
| 54 | ;; |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 55 | i40e) |
| 56 | RESULT="I40G" |
| 57 | ;; |
Yi Tseng | 8a12cb1 | 2016-08-19 01:45:21 +0800 | [diff] [blame] | 58 | ixgbe) |
| 59 | RESULT="10G" |
| 60 | ;; |
| 61 | *) |
| 62 | IS_PHY=$(ls -l /sys/class/net/$1 | grep -v virtual | wc -l) |
| 63 | if [ $IS_PHY -eq 1 ]; then |
| 64 | RESULT="ETH" |
| 65 | fi |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 66 | ;; |
| 67 | *) ;; |
| 68 | esac |
| 69 | echo $RESULT |
| 70 | } |
| 71 | |
| 72 | function get_mac { |
| 73 | echo $(ifconfig $1 | grep HWaddr | awk '{print $5}') |
| 74 | } |
| 75 | |
| 76 | function generate_persistent_names { |
| 77 | local OUT=$NAMES_FILE |
| 78 | #"70-persistent-net.rules" |
| 79 | rm -rf $OUT |
| 80 | |
| 81 | IDX=0 |
alshabib | 54cdbb2 | 2016-06-03 16:37:01 -0700 | [diff] [blame] | 82 | # 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] | 83 | for i in $(cat $1 | sort); do |
alshabib | 54cdbb2 | 2016-06-03 16:37:01 -0700 | [diff] [blame] | 84 | 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] | 85 | IDX=$(expr $IDX + 1) |
| 86 | done |
| 87 | |
| 88 | for i in $(cat $2 | sort); do |
| 89 | echo "SUBSYSTEM==\"net\", ACTION==\"add\", DRIVERS==\"?*\", ATTR{address}==\"$i\", NAME=\"eth$IDX\"" >> $OUT |
| 90 | IDX=$(expr $IDX + 1) |
| 91 | done |
| 92 | } |
| 93 | |
Yi Tseng | 8a12cb1 | 2016-08-19 01:45:21 +0800 | [diff] [blame] | 94 | # FABRIC_LIST ETH_LIST FAB_IFACE EXT_IFACE MGT_IFACE |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 95 | function generate_interfaces { |
| 96 | OUT=$IFACES_FILE |
| 97 | rm -rf $OUT |
| 98 | echo "# This file describes the network interfaces available on your system" >> $OUT |
| 99 | echo "# and how to activate them. For more information, see interfaces(5)." >> $OUT |
| 100 | echo "" >> $OUT |
| 101 | echo "# The loopback network interface" >> $OUT |
| 102 | echo "auto lo" >> $OUT |
| 103 | echo "iface lo inet loopback" >> $OUT |
| 104 | echo "" >> $OUT |
| 105 | |
| 106 | IDX=0 |
David K. Bainbridge | b56bd5a | 2016-06-15 15:42:48 -0700 | [diff] [blame] | 107 | local CNT40=$(cat $1 | wc -w) |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 108 | for i in $(cat $1); do |
alshabib | 54cdbb2 | 2016-06-03 16:37:01 -0700 | [diff] [blame] | 109 | echo "auto eth$IDX" >> $OUT |
| 110 | echo "iface eth$IDX inet manual" >> $OUT |
| 111 | echo " bond-master $3" >> $OUT |
| 112 | [ -z $FIRST ] && echo " bond-primary eth$IDX" >> $OUT |
alshabib | 54cdbb2 | 2016-06-03 16:37:01 -0700 | [diff] [blame] | 113 | FIRST="done" |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 114 | echo "" >> $OUT |
| 115 | IDX=$(expr $IDX + 1) |
| 116 | done |
| 117 | |
David K. Bainbridge | b56bd5a | 2016-06-15 15:42:48 -0700 | [diff] [blame] | 118 | if [ $CNT40 -ne 0 ]; then |
| 119 | echo "auto $3" >> $OUT |
| 120 | echo "iface $3 inet static" >> $OUT |
| 121 | echo " address $FAB_IP" >> $OUT |
| 122 | echo " network $FAB_NETWORK" >> $OUT |
| 123 | echo " netmask $FAB_NETMASK" >> $OUT |
| 124 | # Make bond-mode configurable |
| 125 | echo " bond-mode active-backup" >> $OUT |
| 126 | echo " bond-miimon 100" >> $OUT |
| 127 | echo " bond-slaves none" >> $OUT |
| 128 | echo "" >> $OUT |
| 129 | fi |
alshabib | 54cdbb2 | 2016-06-03 16:37:01 -0700 | [diff] [blame] | 130 | |
David K. Bainbridge | 9d1e02d | 2016-06-22 09:22:16 -0700 | [diff] [blame] | 131 | BRIDGE_PORTS= |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 132 | for i in $(cat $2); do |
David K. Bainbridge | 8db2f30 | 2016-05-19 23:41:13 -0700 | [diff] [blame] | 133 | if [ "eth$IDX" == "$4" ]; then |
Yi Tseng | 8a12cb1 | 2016-08-19 01:45:21 +0800 | [diff] [blame] | 134 | echo "auto eth$IDX" >> $OUT |
David K. Bainbridge | e80fd39 | 2016-08-19 15:46:19 -0700 | [diff] [blame^] | 135 | if [ "$EXT_ADDR" == "dhcp" ]; then |
David K. Bainbridge | 8db2f30 | 2016-05-19 23:41:13 -0700 | [diff] [blame] | 136 | echo "iface eth$IDX inet dhcp" >> $OUT |
David K. Bainbridge | e80fd39 | 2016-08-19 15:46:19 -0700 | [diff] [blame^] | 137 | elif [ "$EXT_ADDR" == "manual" ]; then |
| 138 | echo "iface eth$IDX inet manual" >> $OUT |
David K. Bainbridge | 8db2f30 | 2016-05-19 23:41:13 -0700 | [diff] [blame] | 139 | else |
David K. Bainbridge | 8db2f30 | 2016-05-19 23:41:13 -0700 | [diff] [blame] | 140 | echo "iface eth$IDX inet static" >> $OUT |
| 141 | echo " address $EXT_IP" >> $OUT |
| 142 | echo " network $EXT_NETWORK" >> $OUT |
| 143 | echo " netmask $EXT_NETMASK" >> $OUT |
| 144 | echo " broadcast $EXT_BROADCAST" >> $OUT |
| 145 | echo " gateway $EXT_GW" >> $OUT |
David K. Bainbridge | e80fd39 | 2016-08-19 15:46:19 -0700 | [diff] [blame^] | 146 | echo " dns-nameservers 8.8.8.8 8.8.4.4" >> $OUT |
| 147 | echo " dns-search cord.lab" >> $OUT |
Yi Tseng | 8a12cb1 | 2016-08-19 01:45:21 +0800 | [diff] [blame] | 148 | fi |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 149 | else |
David K. Bainbridge | 9d1e02d | 2016-06-22 09:22:16 -0700 | [diff] [blame] | 150 | echo "auto eth$IDX" >> $OUT |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 151 | echo "iface eth$IDX inet manual" >> $OUT |
Yi Tseng | 8a12cb1 | 2016-08-19 01:45:21 +0800 | [diff] [blame] | 152 | BRIDGE_PORTS="$BRIDGE_PORTS eth$IDX" |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 153 | fi |
| 154 | echo "" >> $OUT |
| 155 | IDX=$(expr $IDX + 1) |
| 156 | done |
David K. Bainbridge | 9d1e02d | 2016-06-22 09:22:16 -0700 | [diff] [blame] | 157 | |
| 158 | local BRNAME=$5 |
| 159 | local F=$(echo $5 | grep "^eth[0-9]*$" | wc -l) |
| 160 | if [ $F -ne 0 ]; then |
David K. Bainbridge | e80fd39 | 2016-08-19 15:46:19 -0700 | [diff] [blame^] | 161 | BRNAME="mgmtbr" |
David K. Bainbridge | 9d1e02d | 2016-06-22 09:22:16 -0700 | [diff] [blame] | 162 | fi |
| 163 | local BPCNT=$(echo $BRIDGE_PORTS | wc -w) |
| 164 | if [ $BPCNT -ne 0 ]; then |
Yi Tseng | 8a12cb1 | 2016-08-19 01:45:21 +0800 | [diff] [blame] | 165 | echo "auto $BRNAME" >> $OUT |
David K. Bainbridge | e80fd39 | 2016-08-19 15:46:19 -0700 | [diff] [blame^] | 166 | if [ "$MGT_ADDR" == "dhcp" ]; then |
| 167 | echo "iface $BRNAME inet dhcp" >> $OUT |
| 168 | elif [ "$MGT_ADDR" == "manual" ]; then |
| 169 | echo "iface $BRNAME inet manual" >> $OUT |
| 170 | else |
| 171 | echo "iface $BRNAME inet static" >> $OUT |
| 172 | echo " address $MGT_IP" >> $OUT |
| 173 | echo " network $MGT_NETWORK" >> $OUT |
| 174 | echo " netmask $MGT_NETMASK" >> $OUT |
| 175 | echo " broadcast $MGT_BROADCAST" >> $OUT |
| 176 | echo " gateway $MGT_GW" >> $OUT |
| 177 | fi |
Yi Tseng | 8a12cb1 | 2016-08-19 01:45:21 +0800 | [diff] [blame] | 178 | echo " bridge_ports $BRIDGE_PORTS" >> $OUT |
David K. Bainbridge | 9d1e02d | 2016-06-22 09:22:16 -0700 | [diff] [blame] | 179 | fi |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 180 | } |
| 181 | |
David K. Bainbridge | 8db2f30 | 2016-05-19 23:41:13 -0700 | [diff] [blame] | 182 | FAB_IFACE=$1 |
| 183 | FAB_ADDR=$2 |
| 184 | FAB_IP=$(echo $FAB_ADDR | cut -d/ -f1) |
| 185 | FAB_MASKBITS=$(echo $FAB_ADDR | cut -d/ -f2) |
| 186 | FAB_NETWORK=$(network $FAB_IP $FAB_MASKBITS) |
| 187 | FAB_NETMASK=$(netmask $FAB_MASKBITS) |
| 188 | |
| 189 | EXT_IFACE=$3 |
| 190 | EXT_ADDR=$4 |
David K. Bainbridge | e80fd39 | 2016-08-19 15:46:19 -0700 | [diff] [blame^] | 191 | EXT_GW=$5 |
David K. Bainbridge | 8db2f30 | 2016-05-19 23:41:13 -0700 | [diff] [blame] | 192 | if [ "$EXT_ADDR" != "dhcp" ]; then |
| 193 | EXT_IP=$(echo $EXT_ADDR | cut -d/ -f1) |
| 194 | EXT_MASKBITS=$(echo $EXT_ADDR | cut -d/ -f2) |
| 195 | EXT_NETWORK=$(network $EXT_IP $EXT_MASKBITS) |
| 196 | EXT_NETMASK=$(netmask $EXT_MASKBITS) |
| 197 | EXT_BROADCAST=$(broadcast $EXT_IP $EXT_MASKBITS) |
David K. Bainbridge | e80fd39 | 2016-08-19 15:46:19 -0700 | [diff] [blame^] | 198 | test -z $EXT_GW && EXT_GW=$(first $EXT_ADDR) |
David K. Bainbridge | 8db2f30 | 2016-05-19 23:41:13 -0700 | [diff] [blame] | 199 | fi |
David K. Bainbridge | e80fd39 | 2016-08-19 15:46:19 -0700 | [diff] [blame^] | 200 | MGT_IFACE=$6 |
| 201 | MGT_ADDR=$7 |
| 202 | MGT_GW=$8 |
David K. Bainbridge | 9d1e02d | 2016-06-22 09:22:16 -0700 | [diff] [blame] | 203 | if [ "$MGT_ADDR" != "dhcp" ]; then |
| 204 | MGT_IP=$(echo $MGT_ADDR | cut -d/ -f1) |
| 205 | MGT_MASKBITS=$(echo $MGT_ADDR | cut -d/ -f2) |
| 206 | MGT_NETWORK=$(network $MGT_IP $MGT_MASKBITS) |
| 207 | MGT_NETMASK=$(netmask $MGT_MASKBITS) |
| 208 | MGT_BROADCAST=$(broadcast $MGT_IP $MGT_MASKBITS) |
David K. Bainbridge | e80fd39 | 2016-08-19 15:46:19 -0700 | [diff] [blame^] | 209 | test -z $MGT_GW && MGT_GW=$(first $MGT_ADDR) |
David K. Bainbridge | 9d1e02d | 2016-06-22 09:22:16 -0700 | [diff] [blame] | 210 | fi |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 211 | |
| 212 | LIST_ETH=$(mktemp -u) |
Yi Tseng | 8a12cb1 | 2016-08-19 01:45:21 +0800 | [diff] [blame] | 213 | LIST_FABRIC=$(mktemp -u) |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 214 | IFACES_FILE=$(mktemp -u) |
| 215 | NAMES_FILE=$(mktemp -u) |
| 216 | |
| 217 | IFACES=$(ifconfig -a | grep "^[a-z]" | awk '{print $1}') |
| 218 | |
| 219 | for i in $IFACES; do |
| 220 | TYPE=$(guess_type $i) |
| 221 | case $TYPE in |
| 222 | ETH) |
| 223 | echo "$(get_mac $i)" >> $LIST_ETH |
| 224 | ;; |
Yi Tseng | 8a12cb1 | 2016-08-19 01:45:21 +0800 | [diff] [blame] | 225 | I40G|MLX4_EN|10G) |
| 226 | echo "$(get_mac $i)" >> $LIST_FABRIC |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 227 | ;; |
| 228 | *) ;; |
| 229 | esac |
| 230 | done |
| 231 | |
David K. Bainbridge | 8db2f30 | 2016-05-19 23:41:13 -0700 | [diff] [blame] | 232 | CHANGED="false" |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 233 | |
Yi Tseng | 8a12cb1 | 2016-08-19 01:45:21 +0800 | [diff] [blame] | 234 | generate_interfaces $LIST_FABRIC $LIST_ETH "$FAB_IFACE" "$EXT_IFACE" "$MGT_IFACE" |
David K. Bainbridge | 8db2f30 | 2016-05-19 23:41:13 -0700 | [diff] [blame] | 235 | |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 236 | diff /etc/network/interfaces $IFACES_FILE 2>&1 > /dev/null |
| 237 | if [ $? -ne 0 ]; then |
David K. Bainbridge | e80fd39 | 2016-08-19 15:46:19 -0700 | [diff] [blame^] | 238 | CHANGED="true" |
| 239 | cp /etc/network/interfaces /etc/network/interfaces.1 |
| 240 | cp $IFACES_FILE /etc/network/interfaces |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 241 | fi |
| 242 | |
Yi Tseng | 8a12cb1 | 2016-08-19 01:45:21 +0800 | [diff] [blame] | 243 | generate_persistent_names $LIST_FABRIC $LIST_ETH "$FAB_IFACE" "$EXT_IFACE" |
| 244 | |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 245 | if [ -r /etc/udev/rules.d/70-persistent-net.rules ]; then |
David K. Bainbridge | e80fd39 | 2016-08-19 15:46:19 -0700 | [diff] [blame^] | 246 | diff /etc/udev/rules.d/70-persistent-net.rules $NAMES_FILE 2>&1 > /dev/null |
| 247 | if [ $? -ne 0 ]; then |
| 248 | CHANGED="true" |
| 249 | cp /etc/udev/rules.d/70-persistent-net.rules /etc/udev/rules.d/70-persistent-net.rules.1 |
| 250 | cp $NAMES_FILE /etc/udev/rules.d/70-persistent-net.rules |
| 251 | fi |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 252 | else |
David K. Bainbridge | e80fd39 | 2016-08-19 15:46:19 -0700 | [diff] [blame^] | 253 | CHANGED="true" |
| 254 | cp $NAMES_FILE /etc/udev/rules.d/70-persistent-net.rules |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 255 | fi |
| 256 | |
| 257 | rm -rf $IFACES_FILE |
| 258 | rm -rf $NAMES_FILE |
| 259 | rm -rf $LIST_ETH |
Yi Tseng | 8a12cb1 | 2016-08-19 01:45:21 +0800 | [diff] [blame] | 260 | rm -rf $LIST_FABRIC |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 261 | |
David K. Bainbridge | 8db2f30 | 2016-05-19 23:41:13 -0700 | [diff] [blame] | 262 | echo -n $CHANGED |