blob: aa8e07c67b3b4ed3e1137e85019d2dd05486b934 [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"
David K. Bainbridgede51ce52016-08-22 13:54:24 -070051 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 Tseng8a12cb12016-08-19 01:45:21 +080057 RESULT="ETH"
David K. Bainbridgede51ce52016-08-22 13:54:24 -070058 fi
59 fi
David K. Bainbridge317e7d72016-05-11 08:31:44 -070060 echo $RESULT
61}
62
63function get_mac {
64 echo $(ifconfig $1 | grep HWaddr | awk '{print $5}')
65}
66
67function generate_persistent_names {
68 local OUT=$NAMES_FILE
69#"70-persistent-net.rules"
70 rm -rf $OUT
71
72 IDX=0
alshabib54cdbb22016-06-03 16:37:01 -070073 # this will not support more than 10 fabric nics... should be ok. (Famous last words)
David K. Bainbridge317e7d72016-05-11 08:31:44 -070074 for i in $(cat $1 | sort); do
alshabib54cdbb22016-06-03 16:37:01 -070075 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 -070076 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 Tseng8a12cb12016-08-19 01:45:21 +080085# FABRIC_LIST ETH_LIST FAB_IFACE EXT_IFACE MGT_IFACE
David K. Bainbridge317e7d72016-05-11 08:31:44 -070086function 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. Bainbridgeb56bd5a2016-06-15 15:42:48 -070098 local CNT40=$(cat $1 | wc -w)
David K. Bainbridge317e7d72016-05-11 08:31:44 -070099 for i in $(cat $1); do
alshabib54cdbb22016-06-03 16:37:01 -0700100 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
alshabib54cdbb22016-06-03 16:37:01 -0700104 FIRST="done"
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700105 echo "" >> $OUT
106 IDX=$(expr $IDX + 1)
107 done
108
David K. Bainbridgeb56bd5a2016-06-15 15:42:48 -0700109 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
alshabib54cdbb22016-06-03 16:37:01 -0700121
David K. Bainbridge9d1e02d2016-06-22 09:22:16 -0700122 BRIDGE_PORTS=
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700123 for i in $(cat $2); do
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700124 if [ "eth$IDX" == "$4" ]; then
Yi Tseng8a12cb12016-08-19 01:45:21 +0800125 echo "auto eth$IDX" >> $OUT
David K. Bainbridgee80fd392016-08-19 15:46:19 -0700126 if [ "$EXT_ADDR" == "dhcp" ]; then
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700127 echo "iface eth$IDX inet dhcp" >> $OUT
David K. Bainbridgee80fd392016-08-19 15:46:19 -0700128 elif [ "$EXT_ADDR" == "manual" ]; then
129 echo "iface eth$IDX inet manual" >> $OUT
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700130 else
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700131 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. Bainbridgee80fd392016-08-19 15:46:19 -0700137 echo " dns-nameservers 8.8.8.8 8.8.4.4" >> $OUT
138 echo " dns-search cord.lab" >> $OUT
Yi Tseng8a12cb12016-08-19 01:45:21 +0800139 fi
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700140 else
David K. Bainbridge9d1e02d2016-06-22 09:22:16 -0700141 echo "auto eth$IDX" >> $OUT
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700142 echo "iface eth$IDX inet manual" >> $OUT
Yi Tseng8a12cb12016-08-19 01:45:21 +0800143 BRIDGE_PORTS="$BRIDGE_PORTS eth$IDX"
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700144 fi
145 echo "" >> $OUT
146 IDX=$(expr $IDX + 1)
147 done
David K. Bainbridge9d1e02d2016-06-22 09:22:16 -0700148
149 local BRNAME=$5
150 local F=$(echo $5 | grep "^eth[0-9]*$" | wc -l)
151 if [ $F -ne 0 ]; then
David K. Bainbridgee80fd392016-08-19 15:46:19 -0700152 BRNAME="mgmtbr"
David K. Bainbridge9d1e02d2016-06-22 09:22:16 -0700153 fi
154 local BPCNT=$(echo $BRIDGE_PORTS | wc -w)
155 if [ $BPCNT -ne 0 ]; then
Yi Tseng8a12cb12016-08-19 01:45:21 +0800156 echo "auto $BRNAME" >> $OUT
David K. Bainbridgee80fd392016-08-19 15:46:19 -0700157 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 Tseng8a12cb12016-08-19 01:45:21 +0800169 echo " bridge_ports $BRIDGE_PORTS" >> $OUT
David K. Bainbridge9d1e02d2016-06-22 09:22:16 -0700170 fi
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700171}
172
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700173FAB_IFACE=$1
174FAB_ADDR=$2
175FAB_IP=$(echo $FAB_ADDR | cut -d/ -f1)
176FAB_MASKBITS=$(echo $FAB_ADDR | cut -d/ -f2)
177FAB_NETWORK=$(network $FAB_IP $FAB_MASKBITS)
178FAB_NETMASK=$(netmask $FAB_MASKBITS)
179
180EXT_IFACE=$3
181EXT_ADDR=$4
David K. Bainbridgee80fd392016-08-19 15:46:19 -0700182EXT_GW=$5
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700183if [ "$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. Bainbridgee80fd392016-08-19 15:46:19 -0700189 test -z $EXT_GW && EXT_GW=$(first $EXT_ADDR)
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700190fi
David K. Bainbridgee80fd392016-08-19 15:46:19 -0700191MGT_IFACE=$6
192MGT_ADDR=$7
193MGT_GW=$8
David K. Bainbridge9d1e02d2016-06-22 09:22:16 -0700194if [ "$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. Bainbridgee80fd392016-08-19 15:46:19 -0700200 test -z $MGT_GW && MGT_GW=$(first $MGT_ADDR)
David K. Bainbridge9d1e02d2016-06-22 09:22:16 -0700201fi
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700202
David K. Bainbridgede51ce52016-08-22 13:54:24 -0700203FABRIC_IFACE_SPEC=$9
204test -z $FABRIC_IFACE_SPEC && FABRIC_IFACE_SPEC="i40e|mlx4_en"
205
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700206LIST_ETH=$(mktemp -u)
Yi Tseng8a12cb12016-08-19 01:45:21 +0800207LIST_FABRIC=$(mktemp -u)
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700208IFACES_FILE=$(mktemp -u)
209NAMES_FILE=$(mktemp -u)
210
211IFACES=$(ifconfig -a | grep "^[a-z]" | awk '{print $1}')
212
213for 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. Bainbridgede51ce52016-08-22 13:54:24 -0700219 FABRIC)
Yi Tseng8a12cb12016-08-19 01:45:21 +0800220 echo "$(get_mac $i)" >> $LIST_FABRIC
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700221 ;;
222 *) ;;
223 esac
224done
225
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700226CHANGED="false"
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700227
Yi Tseng8a12cb12016-08-19 01:45:21 +0800228generate_interfaces $LIST_FABRIC $LIST_ETH "$FAB_IFACE" "$EXT_IFACE" "$MGT_IFACE"
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700229
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700230diff /etc/network/interfaces $IFACES_FILE 2>&1 > /dev/null
231if [ $? -ne 0 ]; then
David K. Bainbridgee80fd392016-08-19 15:46:19 -0700232 CHANGED="true"
233 cp /etc/network/interfaces /etc/network/interfaces.1
234 cp $IFACES_FILE /etc/network/interfaces
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700235fi
236
Yi Tseng8a12cb12016-08-19 01:45:21 +0800237generate_persistent_names $LIST_FABRIC $LIST_ETH "$FAB_IFACE" "$EXT_IFACE"
238
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700239if [ -r /etc/udev/rules.d/70-persistent-net.rules ]; then
David K. Bainbridgee80fd392016-08-19 15:46:19 -0700240 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. Bainbridge317e7d72016-05-11 08:31:44 -0700246else
David K. Bainbridgee80fd392016-08-19 15:46:19 -0700247 CHANGED="true"
248 cp $NAMES_FILE /etc/udev/rules.d/70-persistent-net.rules
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700249fi
250
251rm -rf $IFACES_FILE
252rm -rf $NAMES_FILE
253rm -rf $LIST_ETH
Yi Tseng8a12cb12016-08-19 01:45:21 +0800254rm -rf $LIST_FABRIC
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700255
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700256echo -n $CHANGED