blob: a490717a55adf49862b58fb84af569e4e117e0ab [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. Bainbridgee80fd392016-08-19 15:46:19 -0700135 if [ "$EXT_ADDR" == "dhcp" ]; then
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700136 echo "iface eth$IDX inet dhcp" >> $OUT
David K. Bainbridgee80fd392016-08-19 15:46:19 -0700137 elif [ "$EXT_ADDR" == "manual" ]; then
138 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
David K. Bainbridgee80fd392016-08-19 15:46:19 -0700146 echo " dns-nameservers 8.8.8.8 8.8.4.4" >> $OUT
147 echo " dns-search cord.lab" >> $OUT
Yi Tseng8a12cb12016-08-19 01:45:21 +0800148 fi
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700149 else
David K. Bainbridge9d1e02d2016-06-22 09:22:16 -0700150 echo "auto eth$IDX" >> $OUT
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700151 echo "iface eth$IDX inet manual" >> $OUT
Yi Tseng8a12cb12016-08-19 01:45:21 +0800152 BRIDGE_PORTS="$BRIDGE_PORTS eth$IDX"
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700153 fi
154 echo "" >> $OUT
155 IDX=$(expr $IDX + 1)
156 done
David K. Bainbridge9d1e02d2016-06-22 09:22:16 -0700157
158 local BRNAME=$5
159 local F=$(echo $5 | grep "^eth[0-9]*$" | wc -l)
160 if [ $F -ne 0 ]; then
David K. Bainbridgee80fd392016-08-19 15:46:19 -0700161 BRNAME="mgmtbr"
David K. Bainbridge9d1e02d2016-06-22 09:22:16 -0700162 fi
163 local BPCNT=$(echo $BRIDGE_PORTS | wc -w)
164 if [ $BPCNT -ne 0 ]; then
Yi Tseng8a12cb12016-08-19 01:45:21 +0800165 echo "auto $BRNAME" >> $OUT
David K. Bainbridgee80fd392016-08-19 15:46:19 -0700166 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 Tseng8a12cb12016-08-19 01:45:21 +0800178 echo " bridge_ports $BRIDGE_PORTS" >> $OUT
David K. Bainbridge9d1e02d2016-06-22 09:22:16 -0700179 fi
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700180}
181
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700182FAB_IFACE=$1
183FAB_ADDR=$2
184FAB_IP=$(echo $FAB_ADDR | cut -d/ -f1)
185FAB_MASKBITS=$(echo $FAB_ADDR | cut -d/ -f2)
186FAB_NETWORK=$(network $FAB_IP $FAB_MASKBITS)
187FAB_NETMASK=$(netmask $FAB_MASKBITS)
188
189EXT_IFACE=$3
190EXT_ADDR=$4
David K. Bainbridgee80fd392016-08-19 15:46:19 -0700191EXT_GW=$5
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700192if [ "$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. Bainbridgee80fd392016-08-19 15:46:19 -0700198 test -z $EXT_GW && EXT_GW=$(first $EXT_ADDR)
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700199fi
David K. Bainbridgee80fd392016-08-19 15:46:19 -0700200MGT_IFACE=$6
201MGT_ADDR=$7
202MGT_GW=$8
David K. Bainbridge9d1e02d2016-06-22 09:22:16 -0700203if [ "$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. Bainbridgee80fd392016-08-19 15:46:19 -0700209 test -z $MGT_GW && MGT_GW=$(first $MGT_ADDR)
David K. Bainbridge9d1e02d2016-06-22 09:22:16 -0700210fi
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700211
212LIST_ETH=$(mktemp -u)
Yi Tseng8a12cb12016-08-19 01:45:21 +0800213LIST_FABRIC=$(mktemp -u)
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700214IFACES_FILE=$(mktemp -u)
215NAMES_FILE=$(mktemp -u)
216
217IFACES=$(ifconfig -a | grep "^[a-z]" | awk '{print $1}')
218
219for i in $IFACES; do
220 TYPE=$(guess_type $i)
221 case $TYPE in
222 ETH)
223 echo "$(get_mac $i)" >> $LIST_ETH
224 ;;
Yi Tseng8a12cb12016-08-19 01:45:21 +0800225 I40G|MLX4_EN|10G)
226 echo "$(get_mac $i)" >> $LIST_FABRIC
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700227 ;;
228 *) ;;
229 esac
230done
231
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700232CHANGED="false"
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700233
Yi Tseng8a12cb12016-08-19 01:45:21 +0800234generate_interfaces $LIST_FABRIC $LIST_ETH "$FAB_IFACE" "$EXT_IFACE" "$MGT_IFACE"
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700235
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700236diff /etc/network/interfaces $IFACES_FILE 2>&1 > /dev/null
237if [ $? -ne 0 ]; then
David K. Bainbridgee80fd392016-08-19 15:46:19 -0700238 CHANGED="true"
239 cp /etc/network/interfaces /etc/network/interfaces.1
240 cp $IFACES_FILE /etc/network/interfaces
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700241fi
242
Yi Tseng8a12cb12016-08-19 01:45:21 +0800243generate_persistent_names $LIST_FABRIC $LIST_ETH "$FAB_IFACE" "$EXT_IFACE"
244
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700245if [ -r /etc/udev/rules.d/70-persistent-net.rules ]; then
David K. Bainbridgee80fd392016-08-19 15:46:19 -0700246 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. Bainbridge317e7d72016-05-11 08:31:44 -0700252else
David K. Bainbridgee80fd392016-08-19 15:46:19 -0700253 CHANGED="true"
254 cp $NAMES_FILE /etc/udev/rules.d/70-persistent-net.rules
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700255fi
256
257rm -rf $IFACES_FILE
258rm -rf $NAMES_FILE
259rm -rf $LIST_ETH
Yi Tseng8a12cb12016-08-19 01:45:21 +0800260rm -rf $LIST_FABRIC
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700261
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700262echo -n $CHANGED