blob: 76056ff48f167240b36360e3cabc6c429f28a584 [file] [log] [blame]
David K. Bainbridge317e7d72016-05-11 08:31:44 -07001#!/bin/bash
2
3BASE="10.4"
4LEAF="1"
5SERVER="1"
6
7function guess_type {
8 local CNT=$(echo "$1" | sed -e 's/[:.]/ /g' | wc -w)
9 if [ $CNT -ne 1 ]; then
10 # drop all sub and vlan interfaces
11 echo "DNC"
12 return
13 fi
14 local DRIVER=$(ethtool -i $1 2>/dev/null | grep driver | awk '{print $2}')
15 local RESULT="DNC"
16 case $DRIVER in
17 i40e)
18 RESULT="I40G"
19 ;;
20 igb)
21 RESULT="ETH"
22 ;;
23 *) ;;
24 esac
25 echo $RESULT
26}
27
28function get_mac {
29 echo $(ifconfig $1 | grep HWaddr | awk '{print $5}')
30}
31
32function generate_persistent_names {
33 local OUT="70-persistent-net.rules"
34 rm -rf $OUT
35
36 IDX=0
37 for i in $(cat $1 | sort); do
38 echo "SUBSYSTEM==\"net\", ACTION==\"add\", DRIVERS==\"?*\", ATTR{address}==\"$i\", ATTR{dev_id}==\"0x0\", ATTR{type}==\"1\", KERNEL==\"eth*\", NAME=\"eth$IDX\"" >> $OUT
39 IDX=$(expr $IDX + 1)
40 done
41
42 for i in $(cat $2 | sort); do
43 echo "SUBSYSTEM==\"net\", ACTION==\"add\", DRIVERS==\"?*\", ATTR{address}==\"$i\", NAME=\"eth$IDX\"" >> $OUT
44 IDX=$(expr $IDX + 1)
45 done
46}
47
48function generate_interfaces {
49 OUT="interfaces"
50 rm -rf $OUT
51 echo "# This file describes the network interfaces available on your system" >> $OUT
52 echo "# and how to activate them. For more information, see interfaces(5)." >> $OUT
53 echo "" >> $OUT
54 echo "# The loopback network interface" >> $OUT
55 echo "auto lo" >> $OUT
56 echo "iface lo inet loopback" >> $OUT
57 echo "" >> $OUT
58
59 IDX=0
60 FIRST=1
61 for i in $(cat $1); do
62 if [ $FIRST -eq 1 ]; then
63 echo "auto eth$IDX" >> $OUT
64 echo "iface eth$IDX inet static" >> $OUT
65 echo " address $BASE.$LEAF.$SERVER" >> $OUT
66 echo " network $BASE.$LEAF.0" >> $OUT
67 echo " netmask 255.255.255.0" >> $OUT
68 FIRST=0
69 else
70 echo "iface eth$IDX inet manual" >> $OUT
71 fi
72 echo "" >> $OUT
73 IDX=$(expr $IDX + 1)
74 done
75
76 FIRST=1
77 for i in $(cat $2); do
78 if [ $FIRST -eq 1 ]; then
79 echo "auto eth$IDX" >> $OUT
80 echo "iface eth$IDX inet dhcp" >> $OUT
81 FIRST=0
82 else
83 echo "iface eth$IDX inet manual" >> $OUT
84 fi
85 echo "" >> $OUT
86 IDX=$(expr $IDX + 1)
87 done
88}
89
90LIST_ETH=$(mktemp -u)
91LIST_40G=$(mktemp -u)
92IFACES=$(ifconfig -a | grep "^[a-z]" | awk '{print $1}')
93
94for i in $IFACES; do
95 TYPE=$(guess_type $i)
96 case $TYPE in
97 ETH)
98 echo "$(get_mac $i)" >> $LIST_ETH
99 ;;
100 I40G)
101 echo "$(get_mac $i)" >> $LIST_40G
102 ;;
103 *) ;;
104 esac
105done
106
107generate_persistent_names $LIST_40G $LIST_ETH
108generate_interfaces $LIST_40G $LIST_ETH
109rm -rf $LIST_ETH
110rm -rf $LIST_40G