blob: b35557992985426076e0c248b5b2f07bd9cba96e [file] [log] [blame]
David K. Bainbridge317e7d72016-05-11 08:31:44 -07001#!/bin/bash
2
3function 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
9function 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
19function netmask {
20 local mask=$((0xffffffff << (32 - $1))); shift
21 int2ip $mask
22}
23
24function broadcast {
25 local addr=$(ip2int $1); shift
26 local mask=$((0xffffffff << (32 -$1))); shift
27 int2ip $((addr | ~mask))
28}
29
30function network {
31 local addr=$(ip2int $1); shift
32 local mask=$((0xffffffff << (32 -$1))); shift
33 int2ip $((addr & mask))
34}
35
36function first {
37 local addr=$(ip2int $1)
38 addr=`expr $addr + 1`
39 int2ip $addr
40}
41
42function 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 Tseng8a12cb12016-08-19 01:45:21 +080052 mlx4_en)
alshabibe16ef4c2016-05-27 17:13:23 -070053 RESULT="MLX4_EN"
54 ;;
David K. Bainbridge317e7d72016-05-11 08:31:44 -070055 i40e)
56 RESULT="I40G"
57 ;;
Yi Tseng8a12cb12016-08-19 01:45:21 +080058 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. Bainbridge317e7d72016-05-11 08:31:44 -070066 ;;
67 *) ;;
68 esac
69 echo $RESULT
70}
71
72function get_mac {
73 echo $(ifconfig $1 | grep HWaddr | awk '{print $5}')
74}
75
76function generate_persistent_names {
77 local OUT=$NAMES_FILE
78#"70-persistent-net.rules"
79 rm -rf $OUT
80
81 IDX=0
alshabib54cdbb22016-06-03 16:37:01 -070082 # this will not support more than 10 fabric nics... should be ok. (Famous last words)
David K. Bainbridge317e7d72016-05-11 08:31:44 -070083 for i in $(cat $1 | sort); do
alshabib54cdbb22016-06-03 16:37:01 -070084 echo "SUBSYSTEM==\"net\", ACTION==\"add\", DRIVERS==\"?*\", ATTR{address}==\"$i\", ATTR{dev_id}==\"0x$IDX\", ATTR{type}==\"1\", KERNEL==\"*\", NAME=\"eth$IDX\"" >> $OUT
David K. Bainbridge317e7d72016-05-11 08:31:44 -070085 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 Tseng8a12cb12016-08-19 01:45:21 +080094# FABRIC_LIST ETH_LIST FAB_IFACE EXT_IFACE MGT_IFACE
David K. Bainbridge317e7d72016-05-11 08:31:44 -070095function 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. Bainbridgeb56bd5a2016-06-15 15:42:48 -0700107 local CNT40=$(cat $1 | wc -w)
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700108 for i in $(cat $1); do
alshabib54cdbb22016-06-03 16:37:01 -0700109 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
alshabib54cdbb22016-06-03 16:37:01 -0700113 FIRST="done"
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700114 echo "" >> $OUT
115 IDX=$(expr $IDX + 1)
116 done
117
David K. Bainbridgeb56bd5a2016-06-15 15:42:48 -0700118 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
alshabib54cdbb22016-06-03 16:37:01 -0700130
David K. Bainbridge9d1e02d2016-06-22 09:22:16 -0700131 BRIDGE_PORTS=
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700132 for i in $(cat $2); do
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700133 if [ "eth$IDX" == "$4" ]; then
Yi Tseng8a12cb12016-08-19 01:45:21 +0800134 echo "auto eth$IDX" >> $OUT
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700135 if [ "$EXT_IP" == "dhcp" ]; then
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700136 echo "iface eth$IDX inet dhcp" >> $OUT
137 elif [ "$EXT_IP" == "manual" ]; then
Yi Tseng8a12cb12016-08-19 01:45:21 +0800138 echo "iface eth$IDX inet manual" >> $OUT
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700139 else
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700140 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
Yi Tseng8a12cb12016-08-19 01:45:21 +0800146 echo " dns-nameservers 8.8.8.8 8.8.4.4" >> $OUT
147 echo " dns-search cord.lab" >> $OUT
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700148 fi
David K. Bainbridge9d1e02d2016-06-22 09:22:16 -0700149 elif [ "eth$IDX" == "$5" ]; then
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700150 echo "auto eth$IDX" >> $OUT
Yi Tseng8a12cb12016-08-19 01:45:21 +0800151 if [ "$MGT_IP" == "dhcp" ]; then
152 echo "iface eth$IDX inet dhcp" >> $OUT
153 elif [ "$MGT_IP" == "manual" ]; then
154 echo "iface eth$IDX inet manual" >> $OUT
155 else
David K. Bainbridge9d1e02d2016-06-22 09:22:16 -0700156 echo "iface eth$IDX inet static" >> $OUT
157 echo " address $MGT_IP" >> $OUT
158 echo " network $MGT_NETWORK" >> $OUT
159 echo " netmask $MGT_NETMASK" >> $OUT
160 echo " broadcast $MGT_BROADCAST" >> $OUT
161 echo " gateway $MGT_GW" >> $OUT
Yi Tseng8a12cb12016-08-19 01:45:21 +0800162 fi
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700163 else
David K. Bainbridge9d1e02d2016-06-22 09:22:16 -0700164 echo "auto eth$IDX" >> $OUT
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700165 echo "iface eth$IDX inet manual" >> $OUT
Yi Tseng8a12cb12016-08-19 01:45:21 +0800166 BRIDGE_PORTS="$BRIDGE_PORTS eth$IDX"
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700167 fi
168 echo "" >> $OUT
169 IDX=$(expr $IDX + 1)
170 done
David K. Bainbridge9d1e02d2016-06-22 09:22:16 -0700171
172 local BRNAME=$5
173 local F=$(echo $5 | grep "^eth[0-9]*$" | wc -l)
174 if [ $F -ne 0 ]; then
Yi Tseng8a12cb12016-08-19 01:45:21 +0800175 BRNAME="br-mgmt"
David K. Bainbridge9d1e02d2016-06-22 09:22:16 -0700176 fi
177 local BPCNT=$(echo $BRIDGE_PORTS | wc -w)
178 if [ $BPCNT -ne 0 ]; then
Yi Tseng8a12cb12016-08-19 01:45:21 +0800179 echo "auto $BRNAME" >> $OUT
180 echo "iface $BRNAME inet dhcp" >> $OUT
181 echo " bridge_ports $BRIDGE_PORTS" >> $OUT
David K. Bainbridge9d1e02d2016-06-22 09:22:16 -0700182 fi
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700183}
184
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700185FAB_IFACE=$1
186FAB_ADDR=$2
187FAB_IP=$(echo $FAB_ADDR | cut -d/ -f1)
188FAB_MASKBITS=$(echo $FAB_ADDR | cut -d/ -f2)
189FAB_NETWORK=$(network $FAB_IP $FAB_MASKBITS)
190FAB_NETMASK=$(netmask $FAB_MASKBITS)
191
192EXT_IFACE=$3
193EXT_ADDR=$4
194if [ "$EXT_ADDR" != "dhcp" ]; then
195 EXT_IP=$(echo $EXT_ADDR | cut -d/ -f1)
196 EXT_MASKBITS=$(echo $EXT_ADDR | cut -d/ -f2)
197 EXT_NETWORK=$(network $EXT_IP $EXT_MASKBITS)
198 EXT_NETMASK=$(netmask $EXT_MASKBITS)
199 EXT_BROADCAST=$(broadcast $EXT_IP $EXT_MASKBITS)
200 EXT_GW=$(first $EXT_ADDR)
201fi
202MGT_IFACE=$5
David K. Bainbridge9d1e02d2016-06-22 09:22:16 -0700203MGT_ADDR=$6
204if [ "$MGT_ADDR" != "dhcp" ]; then
205 MGT_IP=$(echo $MGT_ADDR | cut -d/ -f1)
206 MGT_MASKBITS=$(echo $MGT_ADDR | cut -d/ -f2)
207 MGT_NETWORK=$(network $MGT_IP $MGT_MASKBITS)
208 MGT_NETMASK=$(netmask $MGT_MASKBITS)
209 MGT_BROADCAST=$(broadcast $MGT_IP $MGT_MASKBITS)
210 MGT_GW=$(first $MGT_ADDR)
211fi
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700212
213LIST_ETH=$(mktemp -u)
Yi Tseng8a12cb12016-08-19 01:45:21 +0800214LIST_FABRIC=$(mktemp -u)
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700215IFACES_FILE=$(mktemp -u)
216NAMES_FILE=$(mktemp -u)
217
218IFACES=$(ifconfig -a | grep "^[a-z]" | awk '{print $1}')
219
220for i in $IFACES; do
221 TYPE=$(guess_type $i)
222 case $TYPE in
223 ETH)
224 echo "$(get_mac $i)" >> $LIST_ETH
225 ;;
Yi Tseng8a12cb12016-08-19 01:45:21 +0800226 I40G|MLX4_EN|10G)
227 echo "$(get_mac $i)" >> $LIST_FABRIC
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700228 ;;
229 *) ;;
230 esac
231done
232
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700233CHANGED="false"
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700234
Yi Tseng8a12cb12016-08-19 01:45:21 +0800235generate_interfaces $LIST_FABRIC $LIST_ETH "$FAB_IFACE" "$EXT_IFACE" "$MGT_IFACE"
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700236
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700237diff /etc/network/interfaces $IFACES_FILE 2>&1 > /dev/null
238if [ $? -ne 0 ]; then
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700239 CHANGED="true"
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700240 cp /etc/network/interfaces /etc/network/interfaces.1
241 cp $IFACES_FILE /etc/network/interfaces
242fi
243
Yi Tseng8a12cb12016-08-19 01:45:21 +0800244generate_persistent_names $LIST_FABRIC $LIST_ETH "$FAB_IFACE" "$EXT_IFACE"
245
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700246if [ -r /etc/udev/rules.d/70-persistent-net.rules ]; then
247 diff /etc/udev/rules.d/70-persistent-net.rules $NAMES_FILE 2>&1 > /dev/null
248 if [ $? -ne 0 ]; then
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700249 CHANGED="true"
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700250 cp /etc/udev/rules.d/70-persistent-net.rules /etc/udev/rules.d/70-persistent-net.rules.1
251 cp $NAMES_FILE /etc/udev/rules.d/70-persistent-net.rules
252 fi
253else
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700254 CHANGED="true"
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700255 cp $NAMES_FILE /etc/udev/rules.d/70-persistent-net.rules
256fi
257
258rm -rf $IFACES_FILE
259rm -rf $NAMES_FILE
260rm -rf $LIST_ETH
Yi Tseng8a12cb12016-08-19 01:45:21 +0800261rm -rf $LIST_FABRIC
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700262
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700263echo -n $CHANGED