David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | function 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 | |
| 9 | function 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 | |
| 19 | function netmask { |
| 20 | local mask=$((0xffffffff << (32 - $1))); shift |
| 21 | int2ip $mask |
| 22 | } |
| 23 | |
| 24 | function broadcast { |
| 25 | local addr=$(ip2int $1); shift |
| 26 | local mask=$((0xffffffff << (32 -$1))); shift |
| 27 | int2ip $((addr | ~mask)) |
| 28 | } |
| 29 | |
| 30 | function network { |
| 31 | local addr=$(ip2int $1); shift |
| 32 | local mask=$((0xffffffff << (32 -$1))); shift |
| 33 | int2ip $((addr & mask)) |
| 34 | } |
| 35 | |
| 36 | function first { |
| 37 | local addr=$(ip2int $1) |
| 38 | addr=`expr $addr + 1` |
| 39 | int2ip $addr |
| 40 | } |
| 41 | |
| 42 | function 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. Bainbridge | 4ec841c | 2016-05-11 22:10:15 -0700 | [diff] [blame] | 55 | igb|e1000) |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 56 | RESULT="ETH" |
| 57 | ;; |
| 58 | *) ;; |
| 59 | esac |
| 60 | echo $RESULT |
| 61 | } |
| 62 | |
| 63 | function get_mac { |
| 64 | echo $(ifconfig $1 | grep HWaddr | awk '{print $5}') |
| 65 | } |
| 66 | |
| 67 | function 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 | |
David K. Bainbridge | 8db2f30 | 2016-05-19 23:41:13 -0700 | [diff] [blame^] | 84 | # 40G_LIST ETH_LIST FAB_IFACE EXT_IFACE MGT_IFACE |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 85 | function generate_interfaces { |
| 86 | OUT=$IFACES_FILE |
| 87 | rm -rf $OUT |
| 88 | echo "# This file describes the network interfaces available on your system" >> $OUT |
| 89 | echo "# and how to activate them. For more information, see interfaces(5)." >> $OUT |
| 90 | echo "" >> $OUT |
| 91 | echo "# The loopback network interface" >> $OUT |
| 92 | echo "auto lo" >> $OUT |
| 93 | echo "iface lo inet loopback" >> $OUT |
| 94 | echo "" >> $OUT |
| 95 | |
| 96 | IDX=0 |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 97 | for i in $(cat $1); do |
David K. Bainbridge | 8db2f30 | 2016-05-19 23:41:13 -0700 | [diff] [blame^] | 98 | if [ "eth$IDX" == "$3" ]; then |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 99 | echo "auto eth$IDX" >> $OUT |
| 100 | echo "iface eth$IDX inet static" >> $OUT |
David K. Bainbridge | 8db2f30 | 2016-05-19 23:41:13 -0700 | [diff] [blame^] | 101 | echo " address $FAB_IP" >> $OUT |
| 102 | echo " network $FAB_NETWORK" >> $OUT |
| 103 | echo " netmask $FAB_NETMASK" >> $OUT |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 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 | |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 112 | for i in $(cat $2); do |
David K. Bainbridge | 8db2f30 | 2016-05-19 23:41:13 -0700 | [diff] [blame^] | 113 | if [ "eth$IDX" == "$4" ]; then |
| 114 | if [ "$EXT_IP" == "dhcp" ]; then |
| 115 | echo "auto eth$IDX" >> $OUT |
| 116 | echo "iface eth$IDX inet dhcp" >> $OUT |
| 117 | elif [ "$EXT_IP" == "manual" ]; then |
| 118 | echo "iface eth$IDX inet manual" >> $OUT |
| 119 | else |
| 120 | echo "auto eth$IDX" >> $OUT |
| 121 | echo "iface eth$IDX inet static" >> $OUT |
| 122 | echo " address $EXT_IP" >> $OUT |
| 123 | echo " network $EXT_NETWORK" >> $OUT |
| 124 | echo " netmask $EXT_NETMASK" >> $OUT |
| 125 | echo " broadcast $EXT_BROADCAST" >> $OUT |
| 126 | echo " gateway $EXT_GW" >> $OUT |
| 127 | echo " dns-nameservers 8.8.8.8 8.8.4.4" >> $OUT |
| 128 | echo " dns-search cord.lab" >> $OUT |
| 129 | fi |
| 130 | elif [ "eth$IDX" == "$5" ]; then |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 131 | echo "auto eth$IDX" >> $OUT |
| 132 | echo "iface eth$IDX inet dhcp" >> $OUT |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 133 | else |
| 134 | echo "iface eth$IDX inet manual" >> $OUT |
| 135 | fi |
| 136 | echo "" >> $OUT |
| 137 | IDX=$(expr $IDX + 1) |
| 138 | done |
| 139 | } |
| 140 | |
David K. Bainbridge | 8db2f30 | 2016-05-19 23:41:13 -0700 | [diff] [blame^] | 141 | FAB_IFACE=$1 |
| 142 | FAB_ADDR=$2 |
| 143 | FAB_IP=$(echo $FAB_ADDR | cut -d/ -f1) |
| 144 | FAB_MASKBITS=$(echo $FAB_ADDR | cut -d/ -f2) |
| 145 | FAB_NETWORK=$(network $FAB_IP $FAB_MASKBITS) |
| 146 | FAB_NETMASK=$(netmask $FAB_MASKBITS) |
| 147 | |
| 148 | EXT_IFACE=$3 |
| 149 | EXT_ADDR=$4 |
| 150 | if [ "$EXT_ADDR" != "dhcp" ]; then |
| 151 | EXT_IP=$(echo $EXT_ADDR | cut -d/ -f1) |
| 152 | EXT_MASKBITS=$(echo $EXT_ADDR | cut -d/ -f2) |
| 153 | EXT_NETWORK=$(network $EXT_IP $EXT_MASKBITS) |
| 154 | EXT_NETMASK=$(netmask $EXT_MASKBITS) |
| 155 | EXT_BROADCAST=$(broadcast $EXT_IP $EXT_MASKBITS) |
| 156 | EXT_GW=$(first $EXT_ADDR) |
| 157 | fi |
| 158 | MGT_IFACE=$5 |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 159 | |
| 160 | LIST_ETH=$(mktemp -u) |
| 161 | LIST_40G=$(mktemp -u) |
| 162 | IFACES_FILE=$(mktemp -u) |
| 163 | NAMES_FILE=$(mktemp -u) |
| 164 | |
| 165 | IFACES=$(ifconfig -a | grep "^[a-z]" | awk '{print $1}') |
| 166 | |
| 167 | for i in $IFACES; do |
| 168 | TYPE=$(guess_type $i) |
| 169 | case $TYPE in |
| 170 | ETH) |
| 171 | echo "$(get_mac $i)" >> $LIST_ETH |
| 172 | ;; |
| 173 | I40G) |
| 174 | echo "$(get_mac $i)" >> $LIST_40G |
| 175 | ;; |
| 176 | *) ;; |
| 177 | esac |
| 178 | done |
| 179 | |
David K. Bainbridge | 8db2f30 | 2016-05-19 23:41:13 -0700 | [diff] [blame^] | 180 | CHANGED="false" |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 181 | |
David K. Bainbridge | 8db2f30 | 2016-05-19 23:41:13 -0700 | [diff] [blame^] | 182 | generate_interfaces $LIST_40G $LIST_ETH $FAB_IFACE $EXT_IFACE $MGT_IFACE |
| 183 | |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 184 | diff /etc/network/interfaces $IFACES_FILE 2>&1 > /dev/null |
| 185 | if [ $? -ne 0 ]; then |
David K. Bainbridge | 8db2f30 | 2016-05-19 23:41:13 -0700 | [diff] [blame^] | 186 | CHANGED="true" |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 187 | cp /etc/network/interfaces /etc/network/interfaces.1 |
| 188 | cp $IFACES_FILE /etc/network/interfaces |
| 189 | fi |
| 190 | |
David K. Bainbridge | 8db2f30 | 2016-05-19 23:41:13 -0700 | [diff] [blame^] | 191 | generate_persistent_names $LIST_40G $LIST_ETH $FAB_IFACE $EXT_IFACE |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 192 | if [ -r /etc/udev/rules.d/70-persistent-net.rules ]; then |
| 193 | diff /etc/udev/rules.d/70-persistent-net.rules $NAMES_FILE 2>&1 > /dev/null |
| 194 | if [ $? -ne 0 ]; then |
David K. Bainbridge | 8db2f30 | 2016-05-19 23:41:13 -0700 | [diff] [blame^] | 195 | CHANGED="true" |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 196 | cp /etc/udev/rules.d/70-persistent-net.rules /etc/udev/rules.d/70-persistent-net.rules.1 |
| 197 | cp $NAMES_FILE /etc/udev/rules.d/70-persistent-net.rules |
| 198 | fi |
| 199 | else |
David K. Bainbridge | 8db2f30 | 2016-05-19 23:41:13 -0700 | [diff] [blame^] | 200 | CHANGED="true" |
David K. Bainbridge | 317e7d7 | 2016-05-11 08:31:44 -0700 | [diff] [blame] | 201 | cp $NAMES_FILE /etc/udev/rules.d/70-persistent-net.rules |
| 202 | fi |
| 203 | |
| 204 | rm -rf $IFACES_FILE |
| 205 | rm -rf $NAMES_FILE |
| 206 | rm -rf $LIST_ETH |
| 207 | rm -rf $LIST_40G |
| 208 | |
David K. Bainbridge | 8db2f30 | 2016-05-19 23:41:13 -0700 | [diff] [blame^] | 209 | echo -n $CHANGED |