-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkbond.sh
executable file
·138 lines (122 loc) · 2.87 KB
/
mkbond.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/bin/bash
if [[ $(id -u) -ne 0 ]]; then
echo "Must have root privileges to execute."
exit 1
fi
# all of our boxes should only have 2 active physical devices but this logic
# should still work if we have more than that
NICS=$(ls -d /sys/class/net/*/device | awk -F/ '{print $5}')
FirstNIC=$(echo $NICS | awk '{print $1}')
SecondNIC=$(echo $NICS | awk '{print $2}')
# need this because UCS blades require additional bond options
Platform=$(dmidecode -s system-manufacturer)
if [[ $Platform =~ 'Cisco' ]]; then
BondOpts='mode=active-backup miimon=10'
else
BondOpts='mode=active-backup'
fi
if [[ -z "$FirstNIC" || -z "$SecondNIC" ]]; then
echo "Unable to detect 2 active physical NICs. Exiting."
exit 1
fi
FirstNICMAC=$(cat /sys/class/net/$FirstNIC/address)
SecondNICMAC=$(cat /sys/class/net/$SecondNIC/address)
Ipaddr=''
Gateway=''
Netmask=''
Vlan=''
usage () {
echo "Usage: $(basename $0) -I [IP address] -G [gateway IP] -N [netmask] -V [vlan #]"
echo "All flags are required."
echo
echo "This script makes several assumptions:"
echo "1. There are 2 active network cards."
echo "2. You put in the correct IP, gateway, netmask and Vlan."
echo "3. The existing config will be replaced."
}
# show usage and exit if no options are provided
if ( ! getopts "I:G:N:V:" OPT ); then
usage
exit 1
fi
while getopts "I:G:N:V:" OPT; do
case $OPT in
I)
Ipaddr=$OPTARG
;;
G)
Gateway=$OPTARG
;;
N)
Netmask=$OPTARG
;;
V)
Vlan=$OPTARG
;;
\?)
echo ""
;;
esac
done
# all options with args are required. exit otherwise.
if [[ -z "$Ipaddr" || -z "$Gateway" || -z "$Netmask" || -z "$Vlan" ]]; then
usage
exit 1
fi
cd /etc/sysconfig/network-scripts
mv ifcfg-$FirstNIC /tmp 2>&1 >/dev/null
mv ifcfg-$SecondNIC /tmp 2>&1 >/dev/null
mv ifcfg-bond0 /tmp 2>&1 >/dev/null
mv ifcfg-bond0.$Vlan /tmp 2>&1 >/dev/null
echo "Writing out ifcfg-$FirstNIC..."
cat << EOF > ifcfg-$FirstNIC
BOOTPROTO="none"
DEVICE="$FirstNIC"
HWADDR="$FirstNICMAC"
ONBOOT=yes
PEERDNS=no
PEERROUTES=no
NM_CONTROLLED=no
MASTER=bond0
SLAVE=yes
EOF
echo "Writing out ifcfg-$SecondNIC..."
cat << EOF > ifcfg-$SecondNIC
BOOTPROTO="none"
DEVICE="$SecondNIC"
HWADDR="$SecondNICMAC"
ONBOOT=yes
PEERDNS=no
PEERROUTES=no
NM_CONTROLLED=no
MASTER=bond0
SLAVE=yes
EOF
echo "Writing out ifcfg-bond0..."
cat << EOF > ifcfg-bond0
BOOTPROTO="none"
DEVICE="bond0"
ONBOOT=yes
PEERDNS=no
PEERROUTES=no
DEFROUTE=no
TYPE=Bond
BONDING_OPTS="$BondOpts"
BONDING_MASTER=yes
NM_CONTROLLED=no
EOF
echo "Writing out ifcfg.bond0.$Vlan..."
cat << EOF > ifcfg-bond0.$Vlan
BOOTPROTO="none"
IPADDR="$Ipaddr"
NETMASK="$Netmask"
GATEWAY="$Gateway"
DEVICE="bond0.$Vlan"
ONBOOT=yes
PEERDNS=no
PEERROUTES=no
VLAN=yes
NM_CONTROLLED=no
EOF
echo "Please make sure to restart network services for the new config to take place."
echo "Original files (if there were any) should be in /tmp."