blob: be08ba54fdf144c03b7500398d70243a86b70add [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
52 i40e)
53 RESULT="I40G"
54 ;;
David K. Bainbridge4ec841c2016-05-11 22:10:15 -070055 igb|e1000)
David K. Bainbridge317e7d72016-05-11 08:31:44 -070056 RESULT="ETH"
57 ;;
58 *) ;;
59 esac
60 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
73 for i in $(cat $1 | sort); do
74 echo "SUBSYSTEM==\"net\", ACTION==\"add\", DRIVERS==\"?*\", ATTR{address}==\"$i\", ATTR{dev_id}==\"0x0\", ATTR{type}==\"1\", KERNEL==\"eth*\", NAME=\"eth$IDX\"" >> $OUT
75 IDX=$(expr $IDX + 1)
76 done
77
78 for i in $(cat $2 | sort); do
79 echo "SUBSYSTEM==\"net\", ACTION==\"add\", DRIVERS==\"?*\", ATTR{address}==\"$i\", NAME=\"eth$IDX\"" >> $OUT
80 IDX=$(expr $IDX + 1)
81 done
82}
83
84function generate_interfaces {
85 OUT=$IFACES_FILE
86 rm -rf $OUT
87 echo "# This file describes the network interfaces available on your system" >> $OUT
88 echo "# and how to activate them. For more information, see interfaces(5)." >> $OUT
89 echo "" >> $OUT
90 echo "# The loopback network interface" >> $OUT
91 echo "auto lo" >> $OUT
92 echo "iface lo inet loopback" >> $OUT
93 echo "" >> $OUT
94
95 IDX=0
96 FIRST=1
97 for i in $(cat $1); do
98 if [ $FIRST -eq 1 ]; then
99 echo "auto eth$IDX" >> $OUT
100 echo "iface eth$IDX inet static" >> $OUT
101 echo " address $IP" >> $OUT
102 echo " network $NETWORK" >> $OUT
103 echo " netmask $NETMASK" >> $OUT
104 FIRST=0
105 else
106 echo "iface eth$IDX inet manual" >> $OUT
107 fi
108 echo "" >> $OUT
109 IDX=$(expr $IDX + 1)
110 done
111
112 FIRST=1
113 for i in $(cat $2); do
114 if [ $FIRST -eq 1 ]; then
115 echo "auto eth$IDX" >> $OUT
116 echo "iface eth$IDX inet dhcp" >> $OUT
117 FIRST=0
118 else
119 echo "iface eth$IDX inet manual" >> $OUT
120 fi
121 echo "" >> $OUT
122 IDX=$(expr $IDX + 1)
123 done
124}
125
126ADDR=$1
127IP=$(echo $ADDR | cut -d/ -f1)
128MASKBITS=$(echo $ADDR | cut -d/ -f2)
129NETWORK=$(network $IP $MASKBITS)
130NETMASK=$(netmask $MASKBITS)
131
132LIST_ETH=$(mktemp -u)
133LIST_40G=$(mktemp -u)
134IFACES_FILE=$(mktemp -u)
135NAMES_FILE=$(mktemp -u)
136
137IFACES=$(ifconfig -a | grep "^[a-z]" | awk '{print $1}')
138
139for i in $IFACES; do
140 TYPE=$(guess_type $i)
141 case $TYPE in
142 ETH)
143 echo "$(get_mac $i)" >> $LIST_ETH
144 ;;
145 I40G)
146 echo "$(get_mac $i)" >> $LIST_40G
147 ;;
148 *) ;;
149 esac
150done
151
152RESULT="false"
153
154generate_interfaces $LIST_40G $LIST_ETH
155diff /etc/network/interfaces $IFACES_FILE 2>&1 > /dev/null
156if [ $? -ne 0 ]; then
157 RESULT="true"
158 cp /etc/network/interfaces /etc/network/interfaces.1
159 cp $IFACES_FILE /etc/network/interfaces
160fi
161
162generate_persistent_names $LIST_40G $LIST_ETH
163if [ -r /etc/udev/rules.d/70-persistent-net.rules ]; then
164 diff /etc/udev/rules.d/70-persistent-net.rules $NAMES_FILE 2>&1 > /dev/null
165 if [ $? -ne 0 ]; then
166 RESULT="true"
167 cp /etc/udev/rules.d/70-persistent-net.rules /etc/udev/rules.d/70-persistent-net.rules.1
168 cp $NAMES_FILE /etc/udev/rules.d/70-persistent-net.rules
169 fi
170else
171 RESULT="true"
172 cp $NAMES_FILE /etc/udev/rules.d/70-persistent-net.rules
173fi
174
175rm -rf $IFACES_FILE
176rm -rf $NAMES_FILE
177rm -rf $LIST_ETH
178rm -rf $LIST_40G
179
180echo -n $RESULT