-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_ip_bonding.sh
235 lines (192 loc) · 6.68 KB
/
setup_ip_bonding.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/bin/bash
#############################################
# Purpose: Reads current network information and
# creates a bonded interface from two
# NICs
# Author: re-written 20120605 SDW
# Revision: $Rev$
# Updated by: $Author$
# Last change date: $LastChangedDate$
# SVN URL: $HeadURL$
#############################################
##################SETTINGS ###############################
# Change script behavior by modifying these settings
IF1=eth0
IF2=eth1
BOND=bond0
NETMASK=255.255.255.0
##################COMMAND LINE OVERRIDES#################
# If 3 arguments are passed to the script, it will use
# those instead of the settings listed above
if [[ $# -eq 3 ]]; then
IF1=$1
IF2=$2
BOND=$3
elif [[ $# -ne 0 ]] && [[ $# -ne 3 ]]; then
echo "Invalid arguments $@"
f_Usage
exit
fi
##################GENERATE VARIABLES #####################
export TS=`date +%Y%m%d%H%M%S`
CONFIG_DIR=/etc/sysconfig/network-scripts
#CONFIG_DIR=/tmp/sysconfig/network-scripts
IF1_FILE=ifcfg-${IF1}
IF2_FILE=ifcfg-${IF2}
BOND_FILE=ifcfg-${BOND}
##################FUNCTION DEFINITIONS####################
#-----------------
# Function: f_Usage
#-----------------
# Prints Usage Information
f_Usage () {
echo "$0"
echo " Usage: $0"
echo " OR"
echo " $0 <slave1> <slave2> <bond>"
echo ""
echo " ex: $0 eth0 eth1 bond0"
echo ""
}
# Include common_functions.h
SCRIPTDIR1=/maint/scripts
# Locate and source common_functions.h
if [[ -s "${SCRIPTDIR1}/common_functions.h" ]]; then
source "${SCRIPTDIR1}/common_functions.h"
elif [[ -s common_functions.h ]]; then
source common_functions.h
else
echo "Critical dependency failure: unable to locate common_functions.h"
exit
fi
#########################MAIN EXECUTION START##############
#Don't attempt bonding if we're on a virtual machine
if [[ `f_DetectVM` == TRUE ]]; then
echo "Detected virtual machine. Bonding will be skipped."
exit 4
fi
# echo some description
echo "Attempting to make $IF1 and $IF2 slaves of $BOND."
# Check our interfaces to make sure they exist
echo " checking interfaces..."
for j in $IF1 $IF2; do
RESULT=`f_CheckIF $j`
case $RESULT in
FAILURE ) echo " FAILURE: The device $j does not exist, aborting."
exit 255
;;
SUCCESS ) echo " The device $j appears to be valid."
;;
NOLINK ) echo " WARNING: The device $j does not have a link and may not work."
;;
*) echo " FAILURE: the function f_CheckIF returned the unexpected result $RESULT."
exit 90
;;
esac
done
# Compare our interfaces to see if they both have IP addresses
# (In which case we don't want to set up bonding because it won't work
# and it'll break the server)
echo " ensuring there is only one IP between the slaves"
if [[ `f_IPforIF $IF1` != NONE ]] && [[ `f_IPforIF $IF2` != NONE ]]; then
echo " FAILURE: $IF1 has `f_IPforIF $IF1` and $IF2 has `f_IPforIF $IF2`."
echo " Bonding would kill one or both network connections."
echo " Aborting."
exit
fi
# Find our modprobe file
if [[ -f /etc/modprobe.conf ]]; then
MP_FILE=/etc/modprobe.conf
elif [[ -d /etc/modprobe.d ]]; then
MP_FILE=/etc/modprobe.d/bonding.conf
fi
# Get the IP
echo " getting the IP for this machine..."
IP=`f_FindPubIP`
if [[ $IP == FAILURE ]]; then
echo " FAILURE: unable to determine the IP for this machine."
exit
else
echo " the IPv4 address that will be used for this machine is $IP."
fi
# Find our default gateway for arping purposes
echo " getting the gateway for this machine..."
GW=`f_FindDefaultGW`
if [[ $GW == FAILURE ]]; then
echo " FAILURE: There is no default gateway set up for this machine!"
echo " please set up the gateway and try again."
exit
else
echo " the IPv4 gateway that will be used for this machine is $GW."
fi
#Usage: f_CreateBondMaster <IP> <DEVNAME> <NETMASK> <FILE>
#Usage: f_CreateBondSlave <DEV> <MASTER> <FILE>
# Create the master config file
#echo " creating master config for $BOND at ${CONFIG_DIR}/${BOND_FILE}"
echo " creating master config for $BOND"
if [[ `f_CreateBondMaster $IP $BOND $NETMASK ${CONFIG_DIR}/${BOND_FILE}` != FAILURE ]]; then
echo " success."
else
echo " failure, aborting operation."
exit
fi
# Create the slaves
#echo " creating slave config for $IF1 at ${CONFIG_DIR}/${IF1_FILE}"
echo " creating slave config for $IF1"
if [[ `f_CreateBondSlave $IF1 $BOND ${CONFIG_DIR}/${IF1_FILE}` != FAILURE ]]; then
echo " success."
else
echo " failure, aborting operation."
exit
fi
#echo " creating slave config for $IF2 at ${CONFIG_DIR}/${IF2_FILE}"
echo " creating slave config for $IF2"
if [[ `f_CreateBondSlave $IF2 $BOND ${CONFIG_DIR}/${IF2_FILE}` != FAILURE ]]; then
echo " success."
else
echo " failure, aborting operation."
exit
fi
# Add an alias for our bond interface
#echo " adding an alias for the bonding driver to $MP_FILE"
echo " adding an alias for the bonding driver to modprobe"
if [[ -f $MP_FILE ]]; then
cp ${MP_FILE} /etc/modprobe_backup.${TS}
else
touch $MP_FILE
fi
if [[ -z `grep "alias $BOND bonding" $MP_FILE` ]]; then
echo "alias $BOND bonding" >> $MP_FILE
fi
# Add bonding options based on release
# If we're dealing with RHEL 5 or newer these get added to the
# ifcfg bond files, otherwise they're added to modprobe
DISTRO=`f_GetRelease | awk '{print $1}'`
RELEASE=`f_GetRelease | awk '{print $2}'`
if [[ $DISTRO == RHEL ]] && [[ $RELEASE -ge 5 ]]; then
echo " detected $DISTRO ${RELEASE}.X,"
echo " adding bonding opts to ${BOND_FILE}"
#echo "BONDING_OPTS=\"mode=1 primary=${IF1} arp_interval=1000 arp_ip_target=${GW}\"" >> ${CONFIG_DIR}/${BOND_FILE}
echo "BONDING_OPTS=\"mode=1 miimon=100\"" >> ${CONFIG_DIR}/${BOND_FILE}
else
echo " detected $DISTRO ${RELEASE}.X,"
echo " adding bonding opts to modprobe."
if [[ -n `grep "options $BOND" $MP_FILE` ]]; then
grep -v "options $BOND" $MP_FILE > ${MP_FILE}.tmp
/bin/mv ${MP_FILE}.tmp $MP_FILE
fi
#echo "options $BOND primary=${IF1} mode=1 arp_interval=1000 arp_ip_target=${GW}" >> $MP_FILE
echo "options $BOND primary=${IF1} mode=1 miimon=100" >> $MP_FILE
fi
# load the bonding module, so the next time the network is restarted, it can be envoked
if [[ -z `lsmod | grep bonding` ]]; then
modprobe bonding 2>&1 >/dev/null
fi
echo ""
echo "Bonding configuration has been completed."
echo "The network subsystem needs to be restarted to"
echo "complete the changes."
echo ""
echo "If the network restart is unsuccessful, "
echo "you can either reload the nic drivers or reboot."
exit