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
|
#!/usr/bin/perl
#
# **** License ****
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# A copy of the GNU General Public License is available as
# `/usr/share/common-licenses/GPL' in the Debian GNU/Linux distribution
# or on the World Wide Web at `http://www.gnu.org/copyleft/gpl.html'.
# You can also obtain it by writing to the Free Software Foundation,
# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
# This code was originally developed by Vyatta, Inc.
# Portions created by Vyatta are Copyright (C) 2007 Vyatta, Inc.
# All Rights Reserved.
#
# Author: Stephen Hemminger
# Date: September 2008
# Description: Script to setup bonding interfaces
#
# **** End License ****
#
use lib "/opt/vyatta/share/perl5/";
use VyattaConfig;
use Getopt::Long;
use strict;
use warnings;
my %modes = (
"round-robin" => 0,
"active-backup" => 1,
"xor-hash" => 2,
"broadcast" => 3,
"802.3ad" => 4,
"transmit-load-balance" => 5,
"adaptive-load-balance" => 6,
);
sub create_bond {
my $bond = shift;
my $config = new VyattaConfig;
$config->setLevel("interfaces bonding $bond");
my $mode = $modes{$config->returnValue("mode")};
defined $mode or die "bonding mode not defined";
system("sudo modprobe -o \"$bond\" bonding mode=$mode") == 0
or die "modprobe of bonding failed: $!\n";
system("sudo ip link set \"$bond\" up") == 0
or die "enabling $bond failed: $!\n";
$config->setLevel("interfaces ethernet");
for my $intf ( $config->listNodes() ) {
my $group = $config->returnValue("bond-group");
if (defined $group && $group eq $bond ) {
system("sudo ifenslave $bond $intf") == 0
or die "Adding $intf to $bond failed\n";
}
}
}
sub delete_bond {
my $bond = shift;
system("sudo rmmod \"$bond\"") == 0
or die "removal of bonding module failed: $!\n";
}
# See if bonding device exists and the mode has changed
sub change_bond {
my $bond = shift;
my $config = new VyattaConfig;
$config->setLevel("interfaces bonding");
if ( !( $config->isAdded($bond) || $config->isDeleted($bond) )
&& $config->isChanged("$bond mode") )
{
delete_bond($bond);
create_bond($bond);
}
exit 0;
}
sub usage {
print "Usage: $0 --create bondX\n";
print " --delete bondX\n";
print " --mode-change bondX\n";
exit 1;
}
GetOptions(
'create=s' => sub { create_bond( $_[1] ); },
'delete=s' => sub { delete_bond( $_[1] ); },
'mode-change=s' => sub { change_bond( $_[1] ); },
) or usage();
|