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