blob: 74d522e6f15968c91076c0ee100e189e60dda13c [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. Bainbridge4ec841c2016-05-11 22:10:15 -070058 igb|e1000)
David K. Bainbridge317e7d72016-05-11 08:31:44 -070059 RESULT="ETH"
60 ;;
61 *) ;;
62 esac
63 echo $RESULT
64}
65
66function get_mac {
67 echo $(ifconfig $1 | grep HWaddr | awk '{print $5}')
68}
69
70function generate_persistent_names {
71 local OUT=$NAMES_FILE
72#"70-persistent-net.rules"
73 rm -rf $OUT
74
75 IDX=0
alshabib54cdbb22016-06-03 16:37:01 -070076 # this will not support more than 10 fabric nics... should be ok. (Famous last words)
David K. Bainbridge317e7d72016-05-11 08:31:44 -070077 for i in $(cat $1 | sort); do
alshabib54cdbb22016-06-03 16:37:01 -070078 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 -070079 IDX=$(expr $IDX + 1)
80 done
81
alshabib54cdbb22016-06-03 16:37:01 -070082
David K. Bainbridge317e7d72016-05-11 08:31:44 -070083 for i in $(cat $2 | sort); do
84 echo "SUBSYSTEM==\"net\", ACTION==\"add\", DRIVERS==\"?*\", ATTR{address}==\"$i\", NAME=\"eth$IDX\"" >> $OUT
85 IDX=$(expr $IDX + 1)
86 done
87}
88
David K. Bainbridge8db2f302016-05-19 23:41:13 -070089# 40G_LIST ETH_LIST FAB_IFACE EXT_IFACE MGT_IFACE
David K. Bainbridge317e7d72016-05-11 08:31:44 -070090function generate_interfaces {
91 OUT=$IFACES_FILE
92 rm -rf $OUT
93 echo "# This file describes the network interfaces available on your system" >> $OUT
94 echo "# and how to activate them. For more information, see interfaces(5)." >> $OUT
95 echo "" >> $OUT
96 echo "# The loopback network interface" >> $OUT
97 echo "auto lo" >> $OUT
98 echo "iface lo inet loopback" >> $OUT
99 echo "" >> $OUT
100
101 IDX=0
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700102 for i in $(cat $1); do
alshabib54cdbb22016-06-03 16:37:01 -0700103 echo "auto eth$IDX" >> $OUT
104 echo "iface eth$IDX inet manual" >> $OUT
105 echo " bond-master $3" >> $OUT
106 [ -z $FIRST ] && echo " bond-primary eth$IDX" >> $OUT
alshabib54cdbb22016-06-03 16:37:01 -0700107 FIRST="done"
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700108 echo "" >> $OUT
109 IDX=$(expr $IDX + 1)
110 done
111
alshabib54cdbb22016-06-03 16:37:01 -0700112 echo "auto $3" >> $OUT
113 echo "iface $3 inet static" >> $OUT
114 echo " address $FAB_IP" >> $OUT
115 echo " network $FAB_NETWORK" >> $OUT
116 echo " netmask $FAB_NETMASK" >> $OUT
117 # Make bond-mode configurable
alshabib823f1ab2016-06-15 11:36:57 -0700118 echo " bond-mode active-backup" >> $OUT
119 echo " bond-miimon 100" >> $OUT
120 echo " bond-slaves none" >> $OUT
alshabib54cdbb22016-06-03 16:37:01 -0700121 echo "" >> $OUT
122
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
125 if [ "$EXT_IP" == "dhcp" ]; then
126 echo "auto eth$IDX" >> $OUT
127 echo "iface eth$IDX inet dhcp" >> $OUT
128 elif [ "$EXT_IP" == "manual" ]; then
129 echo "iface eth$IDX inet manual" >> $OUT
130 else
131 echo "auto eth$IDX" >> $OUT
132 echo "iface eth$IDX inet static" >> $OUT
133 echo " address $EXT_IP" >> $OUT
134 echo " network $EXT_NETWORK" >> $OUT
135 echo " netmask $EXT_NETMASK" >> $OUT
136 echo " broadcast $EXT_BROADCAST" >> $OUT
137 echo " gateway $EXT_GW" >> $OUT
138 echo " dns-nameservers 8.8.8.8 8.8.4.4" >> $OUT
139 echo " dns-search cord.lab" >> $OUT
140 fi
141 elif [ "eth$IDX" == "$5" ]; then
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700142 echo "auto eth$IDX" >> $OUT
143 echo "iface eth$IDX inet dhcp" >> $OUT
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700144 else
145 echo "iface eth$IDX inet manual" >> $OUT
146 fi
147 echo "" >> $OUT
148 IDX=$(expr $IDX + 1)
149 done
150}
151
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700152FAB_IFACE=$1
153FAB_ADDR=$2
154FAB_IP=$(echo $FAB_ADDR | cut -d/ -f1)
155FAB_MASKBITS=$(echo $FAB_ADDR | cut -d/ -f2)
156FAB_NETWORK=$(network $FAB_IP $FAB_MASKBITS)
157FAB_NETMASK=$(netmask $FAB_MASKBITS)
158
159EXT_IFACE=$3
160EXT_ADDR=$4
161if [ "$EXT_ADDR" != "dhcp" ]; then
162 EXT_IP=$(echo $EXT_ADDR | cut -d/ -f1)
163 EXT_MASKBITS=$(echo $EXT_ADDR | cut -d/ -f2)
164 EXT_NETWORK=$(network $EXT_IP $EXT_MASKBITS)
165 EXT_NETMASK=$(netmask $EXT_MASKBITS)
166 EXT_BROADCAST=$(broadcast $EXT_IP $EXT_MASKBITS)
167 EXT_GW=$(first $EXT_ADDR)
168fi
169MGT_IFACE=$5
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700170
171LIST_ETH=$(mktemp -u)
172LIST_40G=$(mktemp -u)
173IFACES_FILE=$(mktemp -u)
174NAMES_FILE=$(mktemp -u)
175
176IFACES=$(ifconfig -a | grep "^[a-z]" | awk '{print $1}')
177
178for i in $IFACES; do
179 TYPE=$(guess_type $i)
180 case $TYPE in
181 ETH)
182 echo "$(get_mac $i)" >> $LIST_ETH
183 ;;
alshabibe16ef4c2016-05-27 17:13:23 -0700184 I40G|MLX4_EN)
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700185 echo "$(get_mac $i)" >> $LIST_40G
186 ;;
187 *) ;;
188 esac
189done
190
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700191CHANGED="false"
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700192
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700193generate_interfaces $LIST_40G $LIST_ETH $FAB_IFACE $EXT_IFACE $MGT_IFACE
194
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700195diff /etc/network/interfaces $IFACES_FILE 2>&1 > /dev/null
196if [ $? -ne 0 ]; then
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700197 CHANGED="true"
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700198 cp /etc/network/interfaces /etc/network/interfaces.1
199 cp $IFACES_FILE /etc/network/interfaces
200fi
201
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700202generate_persistent_names $LIST_40G $LIST_ETH $FAB_IFACE $EXT_IFACE
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700203if [ -r /etc/udev/rules.d/70-persistent-net.rules ]; then
204 diff /etc/udev/rules.d/70-persistent-net.rules $NAMES_FILE 2>&1 > /dev/null
205 if [ $? -ne 0 ]; then
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700206 CHANGED="true"
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700207 cp /etc/udev/rules.d/70-persistent-net.rules /etc/udev/rules.d/70-persistent-net.rules.1
208 cp $NAMES_FILE /etc/udev/rules.d/70-persistent-net.rules
209 fi
210else
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700211 CHANGED="true"
David K. Bainbridge317e7d72016-05-11 08:31:44 -0700212 cp $NAMES_FILE /etc/udev/rules.d/70-persistent-net.rules
213fi
214
215rm -rf $IFACES_FILE
216rm -rf $NAMES_FILE
217rm -rf $LIST_ETH
218rm -rf $LIST_40G
219
David K. Bainbridge8db2f302016-05-19 23:41:13 -0700220echo -n $CHANGED