blob: 89b49aa1495d10f7ad5f12fe98c1aa854d9bd802 [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
alshabibe16ef4c2016-05-27 17:13:23 -070052 mlx4_en)
53 RESULT="MLX4_EN"
54 ;;
David K. Bainbridge317e7d72016-05-11 08:31:44 -070055 i40e)
56 RESULT="I40G"
57 ;;
David K. Bainbridge9d1e02d2016-06-22 09:22:16 -070058 *)
59 IS_PHY=$(ls -l /sys/class/net/$1 | grep -v virtual | wc -l)
60 if [ $IS_PHY -eq 1 ]; then
61 RESULT="ETH"
62 fi
David K. Bainbridge317e7d72016-05-11 08:31:44 -070063 ;;
64 *) ;;
65 esac
66 echo $RESULT
67}
68
69function get_mac {
70 echo $(ifconfig $1 | grep HWaddr | awk '{print $5}')
71}
72
73function generate_persistent_names {
74 local OUT=$NAMES_FILE
75#"70-persistent-net.rules"
76 rm -rf $OUT
77
78 IDX=0
alshabib54cdbb22016-06-03 16:37:01 -070079 # this will not support more than 10 fabric nics... should be ok. (Famous last words)
David K. Bainbridge317e7d72016-05-11 08:31:44 -070080 for i in $(cat $1 | sort); do
alshabib54cdbb22016-06-03 16:37:01 -070081 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 -070082 IDX=$(expr $IDX + 1)
83 done
84
85 for i in $(cat $2 | sort); do
86 echo "SUBSYSTEM==\"net\", ACTION==\"add\", DRIVERS==\"?*\", ATTR{address}==\"$i\", NAME=\"eth$IDX\"" >> $OUT
87 IDX=$(expr $IDX + 1)
88 done
89}
90
David K. Bainbridge8db2f302016-05-19 23:41:13 -070091# 40G_LIST ETH_LIST FAB_IFACE EXT_IFACE MGT_IFACE
David K. Bainbridge317e7d72016-05-11 08:31:44 -070092function generate_interfaces {
93 OUT=$IFACES_FILE
94 rm -rf $OUT
95 echo "# This file describes the network interfaces available on your system" >> $OUT
96 echo "# and how to activate them. For more information, see interfaces(5)." >> $OUT
97 echo "" >> $OUT
98 echo "# The loopback network interface" >> $OUT
99 echo "auto lo" >> $OUT
100 echo "iface lo inet loopback" >> $OUT
101 echo "" >> $OUT
102
103 IDX=0
David K. Bainbridgeb56bd5a2016-06-15 15:42:48 -0700104 local CNT40=$(cat $1 | wc -w)
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700105 for i in $(cat $1); do
alshabib54cdbb22016-06-03 16:37:01 -0700106 echo "auto eth$IDX" >> $OUT
107 echo "iface eth$IDX inet manual" >> $OUT
108 echo " bond-master $3" >> $OUT
109 [ -z $FIRST ] && echo " bond-primary eth$IDX" >> $OUT
alshabib54cdbb22016-06-03 16:37:01 -0700110 FIRST="done"
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700111 echo "" >> $OUT
112 IDX=$(expr $IDX + 1)
113 done
114
David K. Bainbridgeb56bd5a2016-06-15 15:42:48 -0700115 if [ $CNT40 -ne 0 ]; then
116 echo "auto $3" >> $OUT
117 echo "iface $3 inet static" >> $OUT
118 echo " address $FAB_IP" >> $OUT
119 echo " network $FAB_NETWORK" >> $OUT
120 echo " netmask $FAB_NETMASK" >> $OUT
121 # Make bond-mode configurable
122 echo " bond-mode active-backup" >> $OUT
123 echo " bond-miimon 100" >> $OUT
124 echo " bond-slaves none" >> $OUT
125 echo "" >> $OUT
126 fi
alshabib54cdbb22016-06-03 16:37:01 -0700127
David K. Bainbridge9d1e02d2016-06-22 09:22:16 -0700128 BRIDGE_PORTS=
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700129 for i in $(cat $2); do
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700130 if [ "eth$IDX" == "$4" ]; then
David K. Bainbridge9d1e02d2016-06-22 09:22:16 -0700131 echo "auto eth$IDX" >> $OUT
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700132 if [ "$EXT_IP" == "dhcp" ]; then
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700133 echo "iface eth$IDX inet dhcp" >> $OUT
134 elif [ "$EXT_IP" == "manual" ]; then
135 echo "iface eth$IDX inet manual" >> $OUT
136 else
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700137 echo "iface eth$IDX inet static" >> $OUT
138 echo " address $EXT_IP" >> $OUT
139 echo " network $EXT_NETWORK" >> $OUT
140 echo " netmask $EXT_NETMASK" >> $OUT
141 echo " broadcast $EXT_BROADCAST" >> $OUT
142 echo " gateway $EXT_GW" >> $OUT
143 echo " dns-nameservers 8.8.8.8 8.8.4.4" >> $OUT
144 echo " dns-search cord.lab" >> $OUT
145 fi
David K. Bainbridge9d1e02d2016-06-22 09:22:16 -0700146 elif [ "eth$IDX" == "$5" ]; then
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700147 echo "auto eth$IDX" >> $OUT
David K. Bainbridge9d1e02d2016-06-22 09:22:16 -0700148 if [ "$MGT_IP" == "dhcp" ]; then
149 echo "iface eth$IDX inet dhcp" >> $OUT
150 elif [ "$MGT_IP" == "manual" ]; then
151 echo "iface eth$IDX inet manual" >> $OUT
152 else
153 echo "iface eth$IDX inet static" >> $OUT
154 echo " address $MGT_IP" >> $OUT
155 echo " network $MGT_NETWORK" >> $OUT
156 echo " netmask $MGT_NETMASK" >> $OUT
157 echo " broadcast $MGT_BROADCAST" >> $OUT
158 echo " gateway $MGT_GW" >> $OUT
159 fi
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700160 else
David K. Bainbridge9d1e02d2016-06-22 09:22:16 -0700161 echo "auto eth$IDX" >> $OUT
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700162 echo "iface eth$IDX inet manual" >> $OUT
David K. Bainbridge9d1e02d2016-06-22 09:22:16 -0700163 BRIDGE_PORTS="$BRIDGE_PORTS eth$IDX"
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700164 fi
165 echo "" >> $OUT
166 IDX=$(expr $IDX + 1)
167 done
David K. Bainbridge9d1e02d2016-06-22 09:22:16 -0700168
169 local BRNAME=$5
170 local F=$(echo $5 | grep "^eth[0-9]*$" | wc -l)
171 if [ $F -ne 0 ]; then
172 BRNAME="br-mgmt"
173 fi
174 local BPCNT=$(echo $BRIDGE_PORTS | wc -w)
175 if [ $BPCNT -ne 0 ]; then
176 echo "auto $BRNAME" >> $OUT
177 echo "iface $BRNAME inet dhcp" >> $OUT
178 echo " bridge_ports $BRIDGE_PORTS" >> $OUT
179 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
191if [ "$EXT_ADDR" != "dhcp" ]; then
192 EXT_IP=$(echo $EXT_ADDR | cut -d/ -f1)
193 EXT_MASKBITS=$(echo $EXT_ADDR | cut -d/ -f2)
194 EXT_NETWORK=$(network $EXT_IP $EXT_MASKBITS)
195 EXT_NETMASK=$(netmask $EXT_MASKBITS)
196 EXT_BROADCAST=$(broadcast $EXT_IP $EXT_MASKBITS)
197 EXT_GW=$(first $EXT_ADDR)
198fi
199MGT_IFACE=$5
David K. Bainbridge9d1e02d2016-06-22 09:22:16 -0700200MGT_ADDR=$6
201if [ "$MGT_ADDR" != "dhcp" ]; then
202 MGT_IP=$(echo $MGT_ADDR | cut -d/ -f1)
203 MGT_MASKBITS=$(echo $MGT_ADDR | cut -d/ -f2)
204 MGT_NETWORK=$(network $MGT_IP $MGT_MASKBITS)
205 MGT_NETMASK=$(netmask $MGT_MASKBITS)
206 MGT_BROADCAST=$(broadcast $MGT_IP $MGT_MASKBITS)
207 MGT_GW=$(first $MGT_ADDR)
208fi
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700209
210LIST_ETH=$(mktemp -u)
211LIST_40G=$(mktemp -u)
212IFACES_FILE=$(mktemp -u)
213NAMES_FILE=$(mktemp -u)
214
215IFACES=$(ifconfig -a | grep "^[a-z]" | awk '{print $1}')
216
217for i in $IFACES; do
218 TYPE=$(guess_type $i)
219 case $TYPE in
220 ETH)
221 echo "$(get_mac $i)" >> $LIST_ETH
222 ;;
alshabibe16ef4c2016-05-27 17:13:23 -0700223 I40G|MLX4_EN)
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700224 echo "$(get_mac $i)" >> $LIST_40G
225 ;;
226 *) ;;
227 esac
228done
229
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700230CHANGED="false"
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700231
David K. Bainbridge9d1e02d2016-06-22 09:22:16 -0700232generate_interfaces $LIST_40G $LIST_ETH "$FAB_IFACE" "$EXT_IFACE" "$MGT_IFACE"
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700233
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700234diff /etc/network/interfaces $IFACES_FILE 2>&1 > /dev/null
235if [ $? -ne 0 ]; then
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700236 CHANGED="true"
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700237 cp /etc/network/interfaces /etc/network/interfaces.1
238 cp $IFACES_FILE /etc/network/interfaces
239fi
240
David K. Bainbridge9d1e02d2016-06-22 09:22:16 -0700241generate_persistent_names $LIST_40G $LIST_ETH "$FAB_IFACE" "$EXT_IFACE"
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700242if [ -r /etc/udev/rules.d/70-persistent-net.rules ]; then
243 diff /etc/udev/rules.d/70-persistent-net.rules $NAMES_FILE 2>&1 > /dev/null
244 if [ $? -ne 0 ]; then
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700245 CHANGED="true"
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700246 cp /etc/udev/rules.d/70-persistent-net.rules /etc/udev/rules.d/70-persistent-net.rules.1
247 cp $NAMES_FILE /etc/udev/rules.d/70-persistent-net.rules
248 fi
249else
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700250 CHANGED="true"
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700251 cp $NAMES_FILE /etc/udev/rules.d/70-persistent-net.rules
252fi
253
254rm -rf $IFACES_FILE
255rm -rf $NAMES_FILE
256rm -rf $LIST_ETH
257rm -rf $LIST_40G
258
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700259echo -n $CHANGED