3 # Dynamic Link Aggregation Setup
8 echo -e "\n---- Link Aggregation Version 1.5 Help ----"
9 echo -e "\nDynamically enable Link Aggregation using 802.3ad"
10 echo "802.3ad requires a Switch/PC/NAS which..."
11 echo "also supports 802.3ad to function correctly"
12 echo -e "\nUsage: /path/to/LinkAgg <port> <port>"
13 echo "Example: /path/to/LinkAgg 3 4"
14 echo -e "Only 2 ports are currently supported\n"
15 echo -e "\n--- Special Flags ---"
16 echo -e "\nHelp: -h or --help"
17 echo "Status: -s or --status"
18 echo -e "Delete: -d or --delete"
19 echo -e "Version: -v or --version\n"
23 #Set all needed bonding parameters
26 # Insert bonding module and set parameters (802.3ad, 100ms MII link monitoring)
29 echo 802.3ad
> /sys
/class
/net
/${1}/bonding
/mode
31 echo fast
> /sys
/class
/net
/${1}/bonding
/lacp_rate
34 # 100msec MII link monitoring
35 echo 100 > /sys
/class
/net
/${1}/bonding
/miimon
36 # enslave vlans to bond
37 echo +${2} > /sys
/class
/net
/${1}/bonding
/slaves
38 echo +${3} > /sys
/class
/net
/${1}/bonding
/slaves
39 # Bridge the bond allowing AP access
41 # We allow these VLANs to access the AP
42 iptables
-I INPUT
1 -i ${2} -j ACCEPT
43 iptables
-I INPUT
1 -i ${3} -j ACCEPT
44 iptables
-I INPUT
1 -i ${1} -j ACCEPT
47 Check_Bond_Status BondCreated
48 echo -e "\nBond Created Successfully\n"
51 # Delete Bond Function
54 # Get vlan's used in current bond
55 DELETEVLAN1
=$
(cat /sys
/class
/net
/bond
0/bonding
/slaves | cut
-d ' ' -f 1)
56 DELETEVLAN2
=$
(cat /sys
/class
/net
/bond
0/bonding
/slaves | cut
-d ' ' -f 2)
57 # Remove bonding module, this also deletes bond0
60 if [[ $DELETEVLAN1 != "" -o $DELETEVLAN1 != NULL
]]; then
61 vconfig rem
$DELETEVLAN1
62 DeadVLAN1
="Removed Dead $DELETEVLAN1"
64 if [[ $DELETEVLAN2 != "" -o $DELETEVLAN2 != NULL
]]; then
65 vconfig rem
$DELETEVLAN2
66 DeadVLAN2
="Removed Dead $DELETEVLAN2"
68 #Second check for -d flag allows delete function to be used in new bond creation as well as old bond removal
69 if [[ "$1" = "-d" -o "$1" = "--delete" ]]; then
70 # Set vlan1 back to default
71 robocfg vlan
1 ports
"1 2 3 4 8t"
72 ReturnDefaultPorts
="vlan1 default ports restored to - 1 2 3 4 8t"
73 echo -e "\nbond0 Deleted\n"
74 echo "$ReturnDefaultPorts"
76 echo -e "$DeadVLAN2\n"
86 Check1
=$
(ip link show |
grep bond0
: | cut
-d '<' -f 2 | cut
-d ',' -f 4)
87 if [ "$Check1" != "UP" ]; then
89 Error1
="Status : bond0 not UP"
91 CHECKVLAN1
=$
(cat /sys
/class
/net
/bond
0/bonding
/slaves | cut
-d ' ' -f 1)
92 CHECKVLAN2
=$
(cat /sys
/class
/net
/bond
0/bonding
/slaves | cut
-d ' ' -f 2)
93 if [[ "$CHECKVLAN1" = "" -o "$CHECKVLAN1" = NULL
]]; then
97 Error2
="Status : Slave 1 does not exist"
99 Check2
=$
(ip link show |
grep "$CHECKVLAN1" | cut
-d '<' -f 2 |
grep -o ,UP
, |
grep -o UP
)
100 if [ "$Check2" != "UP" ]; then
103 Error3
="Status : Slave 1 not UP"
104 elif [ "$Check2" = "UP" ]; then
105 PORT1
=$
(robocfg show |
grep "$CHECKVLAN1" | cut
-d ':' -f 3 |
sed -e "s/^ //" | cut
-d ' ' -f 1)
108 if [[ "$CHECKVLAN2" = "" -o "$CHECKVLAN2" = NULL
]]; then
112 Error4
="Status : Slave 2 does not exist"
114 Check3
=$
(ip link show |
grep "$CHECKVLAN2" | cut
-d '<' -f 2 |
grep -o ,UP
, |
grep -o UP
)
115 if [ "$Check3" != "UP" ]; then
118 Error5
="Status : Slave 2 not UP"
119 elif [ "$Check3" = "UP" ]; then
120 PORT2
=$
(robocfg show |
grep "$CHECKVLAN2" | cut
-d ':' -f 3 |
sed -e "s/^ //" | cut
-d ' ' -f 1)
123 WANACCESS
=$
(brctl show |
grep bond0 |
awk '{$1=$1}{ print }')
124 if [ "$WANACCESS" != "bond0" ]; then
126 Error6
="Status : bond0 not part of br0 - no WAN Access"
128 # check if status call was done by Create bond function
129 if [ "$1" = "BondCreated" ]; then
130 # if -d flag is set and any of these status checks fail, delete bond0, this is part of error recovery after new bond creation
131 if [[ "$WANACCESS" = "NONE" -o "$Check1" = "NONE" -o "$CHECKVLAN1" = "NONE" -o "$PORT1" = "NONE" -o "$Check2" = "NONE" -o "$Check2" = "Down" -o "$CHECKVLAN2" = "NONE" -o "$PORT2" = "NONE" -o "$Check3" = "NONE" -o "$Check3" = "Down" ]]; then
137 echo -e "\n--- Bond Errors ---"
144 echo -e "\n--- Bond Status ---\n"
145 echo -e "Bond Status: bond0 $Check1"
146 echo -e "Bridge to WAN Status: Member of br0=$WANACCESS"
147 echo -e "Slave 1 Status: vlan=$CHECKVLAN1 Link=$Check2 Port=$PORT1"
148 echo -e "Slave 2 Status: vlan=$CHECKVLAN2 Link=$Check3 Port=$PORT2\n"
153 if [[ "$1" = "-v" -o "$1" = "--version" ]]; then
158 if [[ "$1" = "-s" -o "$1" = "--status" ]]; then
163 if [[ "$1" = "-d" -o "$1" = "--delete" ]]; then
168 if [[ "$1" = "-h" -o "$1" = "--help" ]]; then
172 # Number of ports must equal 2
173 if [[ "$#" -lt "2" -o "$#" -gt "2" ]]; then
174 echo -e "\nError : Incorrect Number of Ports\n"
178 # The Ports Must not be the same
179 if [ "$1" = "$2" ]; then
180 echo -e "\nError : Port Entries Must Be Unique\n"
184 # Valid port1 entries are 1,2,3,4
185 if [[ "$1" != "1" && "$1" != "2" && "$1" != "3" && "$1" != "4" ]]; then
186 echo -e "\nError : Port1 : Not A Valid Port\n"
190 # Valid port2 entries are 1,2,3,4
191 if [[ "$2" != "1" && "$2" != "2" && "$2" != "3" && "$2" != "4" ]]; then
192 echo -e "\nError : Port2 : Not A Valid Port\n"
196 #Find how many vlans exist
197 VLANS
=$
(ip link show |
grep vlan
* | cut
-d ' ' -f 2 | cut
-d '@' -f 1)
198 #If only 1 vlan exist
199 if [ ${#VLANS} = "5" ]; then
200 #Check if only 1 vlan exist it should be vlan1
201 if [ "$VLANS" = "vlan1" ]; then
203 TMP
=$
(echo $VLAN1 |
sed -e "s/$1 //g")
204 TMP1
=$
(echo $TMP |
sed -e "s/$2 //g")
205 robocfg vlan
1 ports
"$TMP1"
206 robocfg vlan
3 ports
"$1 8t"
207 robocfg vlan
4 ports
"$2 8t"
208 # Create the interfaces
211 BONDS
=$
(ip link show |
grep bond0
: | cut
-d ' ' -f 2 | cut
-d ':' -f 1)
212 #Check that bond0 does not exist
213 if [ ${#BONDS} = "0" ]; then
214 Create_Bond bond0 vlan3 vlan4
215 #If bond0 already exist remove it
218 Create_Bond bond0 vlan3 vlan4
220 #Only 1 vlan exist but it's not vlan1
222 echo -e "\nBonding Failed WTF!!\n"
225 # if more than 1 vlan exist
227 #Find first available vlan
229 tmp
=$
(echo "$VLANS" |
grep "$VLAN2")
231 # vlan2 is used by system internals and does not show up in ip link, useage of vlan2 is not allowed
232 if [ $VLAN2 = "vlan2" ]; then
235 tmp
=$
(echo "$VLANS" |
grep "$VLAN2")
237 while [[ "$tmp" = "$VLAN2" ]];
241 tmp
=$
(echo "$VLANS" |
grep "$VLAN2")
242 # vlan2 is used by system internals and does not show up in ip link, useage of vlan2 is not allowed
243 if [ $VLAN2 = "vlan2" ]; then
246 tmp
=$
(echo "$VLANS" |
grep "$VLAN2")
249 # Create First vlan for bond
250 # Remove port from vlan1 so we can use it in bond
251 VLAN2
=$
( echo -e "$VLAN2" |
sed -e "s/vlan//g" )
253 TMP
=$
(echo "$VLAN1" |
sed -e "s/$1 //g")
254 robocfg vlan
1 ports
"$TMP"
255 robocfg vlan
$VLAN2 ports
"$1 8t"
256 vconfig add eth0
$VLAN2
257 #Find second available vlan
259 VLANS
=$
(ip link show |
grep vlan
* | cut
-d ' ' -f 2 | cut
-d '@' -f 1)
260 tmp1
=$
(echo "$VLANS" |
grep "$VLAN3")
262 # vlan2 is used by system internals and does not show up in ip link, useage of vlan2 is not allowed
263 if [ $VLAN3 = "vlan2" ]; then
266 tmp1
=$
(echo "$VLANS" |
grep "$VLAN3")
268 while [[ "$tmp1" = "$VLAN3" ]];
272 tmp1
=$
(echo "$VLANS" |
grep "$VLAN3")
273 # vlan2 is used by system internals and does not show up in ip link, useage of vlan2 is not allowed
274 if [ $VLAN3 = "vlan2" ]; then
277 tmp1
=$
(echo "$VLANS" |
grep "$VLAN3")
280 # Create Second vlan for bond
281 # Remove port from vlan1 so we can use it in bond
282 VLAN3
=$
( echo "$VLAN3" |
sed -e "s/vlan//g" )
283 TMP1
=$
(echo "$TMP" |
sed -e "s/$2 //g")
284 robocfg vlan
1 ports
"$TMP1"
285 robocfg vlan
$VLAN3 ports
"$2 8t"
286 vconfig add eth0
$VLAN3
287 BONDS
=$
(ip link show |
grep bond0
: | cut
-d ' ' -f 2 | cut
-d ':' -f 1)
288 #Check that bond0 does not exist
289 if [ ${#BONDS} = "0" ]; then
290 Create_Bond bond0 vlan
$VLAN2 vlan
$VLAN3
292 #If bond0 already exist remove it
294 Create_Bond bond0 vlan
$VLAN2 vlan
$VLAN3