0.原理说明
目前网卡绑定mode共有七种(0~6)bond0、bond1、bond2、bond3、bond4、bond5、bond6
常用的有三种:
mode=0:平衡负载模式,有自动备援,但需要”Switch”支援及设定。
mode=1:自动备援模式,其中一条线若断线,其他线路将会自动备援。
mode=6:平衡负载模式,有自动备援,不必”Switch”支援及设定。
海斯3536 启用bond模块命令
1)停止bond模块:关闭网卡后停止模块
ifconfig bond0 down
ifconfig eth0 down
ifconfig eth1 down
modprobe -r bonding
2)启动bond模块:
modprobe bonding mode=0 miimon=100
ifconfig bond0 192.168.2.143 netmask 192.168.240.1 mtu 1500 up
ifenslave bond0 eth0 eth1
route add default gw 192.168.2.1
modprobe bonding mode=0 miimon=100
mode模式,0表示平衡负载,miimon用来进行链路监测的时间间隔。100即每100ms监测一次链路状态。bonding只监测主机与交换机之间链路。如果交换机出去的链路出问题而本身没有问题,那么bonding认为链路没有问题而继续使用。
ifconfig bond0 192.168.2.143 netmask 192.168.240.1 mtu 1500 up
配置IP地址,子网掩码
ifenslave bond0 eth0 eth1
绑定网卡及主网卡,写前面的(eth0)为主网卡
route add default gw 192.168.2.1设置网关
1.shell脚本
#!/bin/sh
echo "bond reading network configure..."
. /mnt/nand/etc/network.conf
restart() {
if [ $MODE = 0 ] || [ $MODE = 1 ]; then
ifconfig $BONDIFNAME down
ifconfig $IF0 down
ifconfig $IF1 down
modprobe -r bonding
modprobe bonding mode=$MODE miimon=$BONDIFMIIMON
# dhcp
if [ $BONDIFTYPE = 1 ];then
ifconfig $BONDIFNAME up
if [ $PRIMARY = 0 ];then
ifenslave $BONDIFNAME $IF0 $IF1
else
ifenslave $BONDIFNAME $IF1 $IF0
fi
dhcpcd $BONDIFNAME -t 30
# static
else
ifconfig $BONDIFNAME $BONDIFADDR netmask $BONDIFMASK mtu $BONDIFMTU up
if [ $PRIMARY = 0 ];then
ifenslave $BONDIFNAME $IF0 $IF1
else
ifenslave $BONDIFNAME $IF1 $IF0
fi
route add default gw $BONDIFGATEWAY
fi
fi
}
case "$1" in
stop)
modprobe -r bonding
;;
restart)
restart
;;
*)
restart
;;
esac
exit $?
echo "bond networking configure success."
2.配置文件
MODE=1
BONDIFNAME=bond0
BONDIFTYPE=0
BONDIFADDR=192.168.2.143
BONDIFMASK=255.255.240.0
BONDIFGATEWAY=192.168.2.1
BONDIFMTU=1500
BONDIFMIIMON=100
PRIMARY=0
IF0=eth0
IF1=eth1
3.代码实现
#define DEVICE_NAME_BOND "bond0"
#define DEVICE_NAME_ETH0 "eth0"
#define DEVICE_NAME_ETH1 "eth1"
#define NETWORK_CFG_PATH "/mnt/nand/etc/network.conf"
#define NETWORK_CFG_FMT "\
MODE=%d\n\
BONDIFNAME=%s\n\
BONDIFTYPE=%d\n\
BONDIFADDR=%s\n\
BONDIFMASK=%s\n\
BONDIFGATEWAY=%s\n\
BONDIFMTU=%d\n\
BONDIFMIIMON=100\n\
PRIMARY=%d\n\
IF0=%s\n\
IF1=%s\n"
static int write_network_cfg(void)
{
FILE *fp = NULL;
if (netcfg == NULL)
return -1;
if ((fp = fopen(NETWORK_CFG_PATH, "w")) == NULL)
return -1;
lockf(fileno(fp), F_LOCK, 0);
fprintf(fp, NETWORK_CFG_FMT, 0, DEVICE_NAME_BOND, 0,
"192.168.2.143", "192.168.240.0",
"192.168.2.1", 1500, 0, DEVICE_NAME_ETH0, DEVICE_NAME_ETH1);
fflush(fp);
lockf(fileno(fp), F_ULOCK, 0);
fclose(fp);
return 0;
}
最后在命令行上运行shell脚本即可