2020年4月

注意事项:修改字符集前先将数据库进行备份

此处演示将ZHS16GBK字符集修改为AL32UTF8

1、修改server端字符集
登录sqlpus查看字符集设置

[oracle@localhost ~]$ sqlplus /nolog
SQL*Plus: Release 11.2.0.1.0 Production on Wed Jan 24 13:55:51 2018
Copyright (c) 1982, 2009, Oracle.  All rights reserved.
SQL> conn /as sysdba
Connected to an idle instance.                  #数据库未启动,先启动数据库。最好将数据库设未开机启动
SQL> startup
SQL> conn /as sysdba
Connected.                                      #连接成功
SQL> select userenv('language') from dual;      #server端字符集查询

USERENV('LANGUAGE')
----------------------------------------------------
AMERICAN_AMERICA.ZHS16GBK

依次执行如下命令

SQL>SHUTDOWN IMMEDIATE;
SQL>STARTUP MOUNT;
SQL>ALTER SYSTEM ENABLE RESTRICTED SESSION;
SQL>ALTER SYSTEM SET JOB_QUEUE_PROCESSES=0;
SQL>ALTER SYSTEM SET AQ_TM_PROCESSES=0;
SQL>ALTER DATABASE OPEN;
SQL>ALTER DATABASE CHARACTER SET INTERNAL_USE AL32UTF8;
SQL>SHUTDOWN IMMEDIATE;
SQL>STARTUP;
SQL> select userenv('language') from dual;
USERENV('LANGUAGE')
----------------------------------------------------
AMERICAN_AMERICA.AL32UTF8
SQL> 

2、修改client端字符集
查看系统环境变量设置的字符集(client端字符集)

[oracle@localhost ~]$ cat /home/oracle/.bash_profile
...
PATH=$PATH:$HOME/.local/bin:$HOME/bin
export PATH
export ORACLE_BASE=/usr/oracle
export ORACLE_HOME=$ORACLE_BASE/product/11.2.0/db_1
export ORACLE_SID=orcl
export ORACLE_TERM=xterm
export PATH=$ORACLE_HOME/bin:/usr/sbin:$PATH
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib
export LANG=C
export NLS_LANG=AMERICAN_AMERICA.ZHS16GBK                #客户端字符集

进入编辑界面,将ZHS16GBK改为AL32UTF8,保存退出

[oracle@localhost ~]$ vim /home/oracle/.bash_profile

使配置生效

[oracle@localhost ~]$ source /home/oracle/.bash_profile

修改完成

方法一:

1、安装好Oracle数据库后: 执行 dbstart和dbshut

[oracle@localhost ~]$ dbstart
ORACLE_HOME_LISTNER is not SET, unable to auto-start Oracle Net Listener
Usage: /usr/oracle/product/11.2.0/db_1/bin/dbstart ORACLE_HOME

错误原因:dbstart和dbshut脚本文件中ORACLE_HOME_LISTNER的设置有问题

[oracle@localhost ~]$ vim $ORACLE_HOME/bin/dbstart
[oracle@localhost ~]$ vim $ORACLE_HOME/bin/dbshut

分别打开两个文件修改(50行左右)

ORACLE_HOME_LISTNER=$1
修改为:
ORACLE_HOME_LISTNER=$ORACLE_HOME
修改后保存退出,问题解决

2、Linux启动时自动启动Oracle监听和实例
第一步:修改/etc/oratab文件

[oracle@localhost ~]$ vim /etc/oratab
找到:   orcl:/usr/oracle/product/11.2.0/db_1:N   
修改为: orcl:/usr/oracle/product/11.2.0/db_1:Y
(这个路径跟安装路径有关,$ORACLE_SID:$ORACLE_HOME:<N|Y>)
第二步:把lsnrctl start和dbstart添加到rc.local文件中:

root权限执行

[root@localhost oracle]# vim /etc/rc.d/rc.local

添加:

su - oracle -lc "/usr/oracle/product/11.2.0/db_1/bin/lsnrctl start"
su - oracle -lc "/usr/oracle/product/11.2.0/db_1/bin/dbstart"

说明:

第一行为开机启动数据库监听服务,第二行为开机启动数据库。(路径跟安装路径相关)。

注意:CentOs7中/etc/rc.d/rc.local不会开机执行,需添加执行权限。

查看/etc/rc.d/rc.local的权限

[root@localhost oracle]# ll /etc/rc.d/rc.local

添加执行权限

[root@localhost oracle]# chmod +x /etc/rc.d/rc.local

3、重启服务器后查看是否自启动成功
查看监听是否自启动成功

[oracle@localhost ~]$ lsnrctl status LISTENER

查看Oracle服务状态

[oracle@localhost ~]$ ps –ef | grep oracle

4、oracle的启动或关闭管理

[root@localhost oracle]# dbstart                   #启动
[root@localhost oracle]# dbshut                    #停止

方法二:

1、修改/etc/oratab文件
root权限执行

[root@localhost oracle]# vim /etc/oratab
找到:    orcl:/usr/oracle/product/11.2.0/db_1:N   
修改为:  orcl:/usr/oracle/product/11.2.0/db_1:Y

(这个路径跟安装路径有关,$ORACLE_SID:$ORACLE_HOME:<N|Y>)
2、新建Oracle服务自启动脚本

[root@localhost oracle]# vim /etc/init.d/oracle

将以下脚本复制到文件中,保存退出(:wq)

#!/bin/sh
# chkconfig: 2345 61 61
# description: Oracle 11g R2 AutoRun Servimces
# /etc/init.d/oracle
#
# Run-level Startup script for the Oracle Instance, Listener, and
# Web Interface
export ORACLE_BASE=/usr/oracle                                          #oracle安装位置
export ORACLE_HOME=$ORACLE_BASE/product/11.2.0/db_1                     #Oracle安装路径
export ORACLE_SID=orcl
export PATH=$PATH:$ORACLE_HOME/bin
ORA_OWNR="oracle"
# if the executables do not exist -- display error
if [ ! -f $ORACLE_HOME/bin/dbstart -o ! -d $ORACLE_HOME ]
then
echo "Oracle startup: cannot start"
exit 1
fi
# depending on parameter -- startup, shutdown, restart
# of the instance and listener or usage display
case "$1" in
start)
# Oracle listener and instance startup
su $ORA_OWNR -lc $ORACLE_HOME/bin/dbstart
echo "Oracle Start Succesful!OK."
;;
stop)
# Oracle listener and instance shutdown
su $ORA_OWNR -lc $ORACLE_HOME/bin/dbshut
echo "Oracle Stop Succesful!OK."
;;
reload|restart)
$0 stop
$0 start
;;
*)
echo $"Usage: `basename $0` {start|stop|reload|reload}"
exit 1
esac
exit 0

3、检查脚本能否执行

[root@localhost oracle]# /etc/init.d/oracle start            #启动oracle脚本
[root@localhost oracle]# /etc/init.d/oracle stop             #关闭oracle脚本
[root@localhost oracle]# /etc/init.d/oracle restart          #重启oracle脚本

4、添加执行权限并建立链接
更改oracle脚本的执行权限

[root@localhost oracle]# chmod a+x /etc/init.d/oracle

建立链接

将启动脚本添加到系统服务并设置自启动

[root@localhost oracle]# chkconfig --add oracle

当这个命令被执行的时候,会去脚本文件oracle中寻找# chkconfig: 2345 61 61这行注释,并解析这行注释,根据解析结果分别在

/etc/rc.d/rc2.d
/etc/rc.d/rc3.d
/etc/rc.d/rc4.d
/etc/rc.d/rc5.d

中创建符号连接文件S61oracle,此文件在系统启动时根据运行级别执行,此文件是指向/etc/init.d/oracle文件。启动时系统向此文件发送一个start参数,执行oracle文件中的start分支。另外还会在

/etc/rc.d/rc0.d
/etc/rc.d/rc1.d
/etc/rc.d/rc6.d

中创建符号连接文件K61oracle,此文件在系统关闭时执行,此文件也指向/etc/init.d/oracle文件,关闭时系统向此文件发送一个stop参数,执行oracle文件中的stop分支。

chkconfig: 2345 61 61

表明脚本应该在运行级 2, 3, 4, 5 启动,启动优先权为61,停止优先权为 61。

修改服务运行等级(虽然脚本里写过,但还是重新设置一下),可以自行设置oracle脚本的运行级别

[root@localhost oracle]# chkconfig --level 2345 oracle on

说明:设置oracle脚本在运行级别为2、3、4、5时,都是on(开启)状态,off为关闭

查看oracle自动启动设置

[root@localhost oracle]# chkconfig –list oracle
Oracle   0:off 1:off 2:on 3:on 4:on 5:on 6:off
复制代码
等级0表示:表示关机
等级1表示:单用户模式
等级2表示:无网络连接的多用户命令行模式
等级3表示:有网络连接的多用户命令行模式
等级4表示:不可用
等级5表示:带图形界面的多用户模式
等级6表示:重新启动
复制代码
手动创建符号链接文件(执行效果和执行chkconfig --add oracle是一样,作为知识笔记记录,可以不执行)
[root@localhost oracle]# ln –s /etc/rc.d/init.d/oracle /etc/rc0.d/K61oracle
[root@localhost oracle]# ln –s /etc/rc.d/init.d/oracle /etc/rc1.d/K61oracle
[root@localhost oracle]# ln –s /etc/rc.d/init.d/oracle /etc/rc2.d/S61oracle
[root@localhost oracle]# ln –s /etc/rc.d/init.d/oracle /etc/rc3.d/S61oracle
[root@localhost oracle]# ln –s /etc/rc.d/init.d/oracle /etc/rc4.d/S61oracle
[root@localhost oracle]# ln –s /etc/rc.d/init.d/oracle /etc/rc5.d/S61oracle
[root@localhost oracle]# ln –s /etc/rc.d/init.d/oracle /etc/rc6.d/K61oracle

5、oracle的启动或关闭管理

启动
[root@localhost oracle]# service oracle start
停止
[root@localhost oracle]# service oracle stop
重启
[root@localhost oracle]# service oracle restart

VGPU官方资料合集

https://www.nvidia.cn/data-center/virtualization/resources/

VGPU 90天测试license申请地址

https://enterpriseproductregistration.nvidia.com/?LicType=EVAL&ProductFamily=vGPU

第三方站点

https://www.techpowerup.com/gpu-specs/

VGPU文档库

https://docs.nvidia.com/grid/latest/

VGPU许可门户

https://nvid.nvidia.com/dashboard/

VGPU许可门户

https://nvidia.flexnetoperations.com/

VGPU许可门户

https://nvid.nvidia.com/dashboard/

GPU服务器认证查询

https://www.nvidia.com/object/vgpu-certified-servers.html

支持VGPU的物理GPU

https://docs.nvidia.com/grid/gpus-supported-by-vgpu.html

VGPU软件版本集合

https://docs.nvidia.com/grid/get-grid-version.html

VGPU切割指导

https://docs.nvidia.com/grid/latest/grid-vgpu-user-guide/index.html#architecture-grid-vgpu

#!/bin/bash
if [ $UID -ne 0 ]; then
echo "The current user is not root,please execute this script as the root user!"
exit
fi
rsync -h > /dev/null 2>&1
if [ $? != 0 ];then
echo "rsync命令未找到!请先安装!"
exit
fi

while getopts "s:d:p:z:" opt; do
  case $opt in
    s)
        source=$OPTARG
    ;;
    d)
        target=$OPTARG
    ;;
    p)
        nfsip=$OPTARG
    ;;
    z)
        del=--delete
    ;;
    \?)
echo -e "\e[94m使用方法:./scriptname.sh -s |源绝对路径| -d |目的绝对路径| -p |NFS IP地址|(可选) -z sync|两边数据一致|(可选)\e[0m"
echo -e "\e[94m----------------------------------------------------------------------------------------------------------------------\e[0m"
echo "示例:./scriptname.sh -s /data/oracle -d /nfs 192.168.200.200"
echo -e "\e[94m----------------------------------------------------------------------------------------------------------\e[0m"
echo -e "\e[94m如果要保证源文件和目标文件完全一致则加 -z sync\e[0m"
echo -e "\e[94m----------------------------------------------------------------------------------------------------------\e[0m"
echo "示例:./scriptname.sh -s /data/oracle -d /nfs 192.168.200.200 -z sync"
echo -e "\e[94m----------------------------------------------------------------------------------------------------------\e[0m"
echo -e "\e[94m如有多个NFS地址,则用 - 分隔\e[0m"
echo -e "\e[94m----------------------------------------------------------------------------------------------------------\e[0m"
echo "示例:./scriptname.sh -s /data/oracle -d /nfs -p 192.168.200.200-192.168.200.201-192.168.200.203"
echo -e "\e[94m----------------------------------------------------------------------------------------------------------\e[0m"
  esac
done

if [[ -z "$source" ]]||[[ -z "$target" ]];then
echo -e "\e[94m使用方法:./scriptname.sh -s |源绝对路径| -d |目的绝对路径| -p |NFS IP地址|(可选) -z sync|两边数据一致|(可选)\e[0m"
echo -e "\e[94m----------------------------------------------------------------------------------------------------------\e[0m"
echo "示例:./scriptname.sh -s /data/oracle -d /nfs 192.168.200.200"
echo -e "\e[94m----------------------------------------------------------------------------------------------------------\e[0m"
echo -e "\e[94m如果要保证源文件和目标文件完全一致则加 -z sync\e[0m"
echo -e "\e[94m----------------------------------------------------------------------------------------------------------\e[0m"
echo "示例:./scriptname.sh -s /data/oracle -d /nfs 192.168.200.200 -z sync"
echo -e "\e[94m----------------------------------------------------------------------------------------------------------\e[0m"
echo -e "\e[94m如有多个NFS地址,则用 - 分隔\e[0m"
echo -e "\e[94m----------------------------------------------------------------------------------------------------------\e[0m"
echo "示例:./scriptname.sh -s /data/oracle -d /nfs -p 192.168.200.200-192.168.200.201-192.168.200.203"
echo -e "\e[94m----------------------------------------------------------------------------------------------------------\e[0m"
exit
fi
if [[ -d "$source" ]]||[[ -d "$target" ]];then
echo
else
echo -e "输入的路径不存在!" 
exit
fi
if [[ -n $nfsip ]];then
ip=`echo $nfsip |sed s/-/\ /g`
for tip in $ip
do
ping=`ping -c 5 $tip |awk 'NR==9 {print $4}'`
if [[ $ping -ne 0 ]];then
dect=`/usr/sbin/showmount -e $tip`
if [[ $? != 0 ]];then
echo -e "\e[94mNFS组件不正常!\e[0m"
fi
path=`/usr/sbin/showmount -e $tip |grep -v Export |awk -F " " '{print$1}'|awk -F "/" '{print$2}' |sed '/^$/d'`
mount=`df -h |grep $tip: |awk -F " " '{print$1}' |awk -F "/" '{print$2}'`
if [[ "$path" != "$mount" ]];then
echo -e "$tip NFS挂载不正常,请检查!\e[0m"
exit
fi
else
echo -e "\e[94m当前主机无法ping通NFS $tip 请检查网络配置!\e[0m"
exit
fi
done
fi
mkdir -p /script/logs
mkdir -p /script/scriptlogs
folder=`echo $source |sed s#/#\#g`
rsync -avz $del $source $target > /script/logs/$folder.`date +"%Y-%m-%d-%H-%M-%S"`.log
if [ $? == 0 ];then
echo "已在`date +"%Y-%m-%d-%H-%M-%S"`从$source同步到$target成功,在/script/logs下查看备份信息。"  > /script/scriptlogs/$folder.`date +"%Y-%m-%d-%H-%M-%S"`.log
else
echo "当前备份不成功!"  > /script/scriptlogs/$folder.`date +"%Y-%m-%d-%H-%M-%S"`.log
fi

#!/bin/bash
sttya=`tty`
if [[ "$sttya" =~ "pts" ]];then
stty erase ^?
fi
if [ $UID -ne 0 ]; then
echo "当前用户不是root用户,请以root用户身份执行此脚本!"
echo "The current user is not root,please execute this script as the root user!"
exit
fi
echo -e "\e[94m##############################################################\e[0m"
echo -e "\e[94m#                           1.中文                           #\e[0m"
echo -e "\e[94m#                           2.English                        #\e[0m"
echo -e "\e[94m##############################################################\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
echo -e "\e[94m请选择运行脚本所用的语言: \e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
echo -e "\e[94mPlease select a language to run the script: \e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
while true
do
read -p "Enter the number: " language
if [[ $language == 1 ]];then
echo -e "\e[94m--------------------------------------------------------------\e[0m"
echo -e "\e[94m已选择中文!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
language=1
export LANG="zh_CN.UTF-8"
sleep 1
break;
elif [[ $language == 2 ]];then
echo -e "\e[94m--------------------------------------------------------------\e[0m"
echo -e "\e[94mEnglish selected!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
language=2
export LANG="en_US.UTF-8"
sleep 1
break;
else
echo -e "\e[94m请选择语言!\e[0m"
echo -e "\e[94mPlease select a language!\e[0m"
fi
done 
if [ $language == 1 ];then
echo -e "\e[94m##############################################################\e[0m"
echo -e "\e[94m#         1.Linux网卡绑定脚本,支持bond和team绑定方式        #\e[0m"
echo -e "\e[94m#         2.支持CentOS 6.x/7.x/8.x RedHat 6.x/7.x/8.x        #\e[0m"
echo -e "\e[94m#         3.作者:猫先生                                     #\e[0m"
echo -e "\e[94m#         4.编写时间:2020年3月14日                          #\e[0m"
echo -e "\e[94m#         5.更多内容访问 https://www.mr-mao.cn               #\e[0m"
echo -e "\e[94m##############################################################\e[0m"
echo -e "\033[1;m提示:如按Backspace键无法正常删除内容时,可按 Ctrl+Backspace\e[0m"
oskernel=`uname -r | awk -F"-" '{print $1}'`
osrealease=`cat /etc/redhat-release`
echo -e "\e[94m--------------------------------------------------------------\e[0m"
echo -e "\e[94m当前操作系统为$osrealease\e[0m"
if [ "$oskernel" = "2.6.32" ]; then
echo -e "\e[94m内核版本为 2.6.32 系列\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
echo -e "\e[94m当前系统只支持bond绑定模式!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
selectmode=1
service NetworkManager stop > /dev/null 2>&1
chkconfig NetworkManager off > /dev/null 2>&1
elif [ "$oskernel" = "3.10.0" ]; then
systemctl status network > /dev/null 2>&1
if [ $? -ne 0 ];then
echo -e "\e[94m当前系统网络服务运行不正常,请先检查网络服务!\e[0m"
systemctl status network
exit
fi
echo -e "\e[94m内核版本为 3.10.0 系列\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
echo -e "\e[94m##############################################################\e[0m"
echo -e "\e[94m#                           1.Bond模式                       #\e[0m"
echo -e "\e[94m#                           2.Team模式                       #\e[0m"
echo -e "\e[94m##############################################################\e[0m"
echo -e "\033[1;m提示:bond和team二选一,不推荐两种模式共存!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
echo -e "\e[94m当前系统支持team和bond两种绑定模式,请选择:\e[0m"
while true
do
read -p "选择绑定模式,输入数字即可: " selectmode
if [[ $selectmode == 1 ]];then
systemctl stop NetworkManager > /dev/null 2>&1
systemctl disable NetworkManager > /dev/null 2>&1
echo -e "\e[94m已选择Bond绑定模式:\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
elif [[ $selectmode == 2 ]];then
systemctl enable NetworkManager > /dev/null 2>&1
systemctl start NetworkManager > /dev/null 2>&1
systemctl status NetworkManager > /dev/null 2>&1
if [ $? -ne 0 ];then
echo -e "\e[94m#当前系统无法支持team绑定命令!请检查系统网络管理组件!\e[0m"
exit
fi
echo -e "\e[94m已选择Team绑定模式:\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
else
echo -e "\e[94m请选择正确的绑定模式!\e[0m"
fi
done
elif [ "$oskernel" = "4.18.0" ]; then
echo -e "\e[94m内核版本为 4.18.0 系列\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
while true
do
systemctl start network > /dev/null 2>&1
systemctl status network > /dev/null 2>&1
if [ $? -ne 0 ];then
echo -e "\e[94m当前系统只支持Team模式\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
selectmode=2
systemctl enable NetworkManager > /dev/null 2>&1
systemctl start NetworkManager > /dev/null 2>&1
systemctl status NetworkManager > /dev/null 2>&1
if [ $? -ne 0 ];then
echo -e "\e[94m当前系统无法支持team绑定命令!请检查系统网络管理组件!#\e[0m"
exit
fi
sleep 1
break;
else
echo -e "\e[94m#####################################################\e[0m"
echo -e "\e[94m#                    1.Bond模式                     #\e[0m"
echo -e "\e[94m#                    2.Team模式                     #\e[0m"
echo -e "\e[94m#####################################################\e[0m"
echo -e "\e[94m提示:bond和team二选一,不推荐两种模式共存!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
echo -e "\e[94m当前系统支持team和bond两种绑定模式,推荐选择Team模式:\e[0m"
read -p "选择绑定模式,输入数字即可: " selectmode
if [[ $selectmode == 1 ]];then
systemctl stop NetworkManager > /dev/null 2>&1
systemctl disable NetworkManager > /dev/null 2>&1
echo -e "\e[94m已选择Bond绑定模式!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
selectmode=1
break;
elif [[ $selectmode == 2 ]];then
systemctl enable NetworkManager > /dev/null 2>&1
systemctl start NetworkManager > /dev/null 2>&1
systemctl status NetworkManager > /dev/null 2>&1
if [ $? -ne 0 ];then
echo -e "\e[94m#当前系统无法支持team绑定命令!请检查系统网络管理组件!#\e[0m"
exit
fi
echo -e "\e[94m已选择Team绑定模式!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
selectmode=2
break;
else
echo -e "\e[94m请选择正确的绑定模式!\e[0m"
fi
fi
done
else
echo -e "\e[94m此脚本不支持当前操作系统!\e[0m"
exit
fi
rm -f /tmp/nic
rm -f /tmp/nicnew
rm -f /tmp/presentbondall
rm -f /tmp/nicall
rm -f /tmp/teams
mkdir -p /etc/sysconfig/network-scripts/nicsbak
ip a | grep -E "^[0-9]" | grep -vE ": lo|: virbr" |awk -F ": <" '{print$1}' |awk -F ": " '{print$2}' > /tmp/nicall
for selnic in `ip a | grep -E "^[0-9]" | grep -vE ": lo|: virbr|: team|: bond" |awk -F ": <" '{print$1}' |awk -F ": " '{print$2}';`
do
selnicip=`ip a s $selnic | grep -o '[0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+' | head -n1`
selnicgateway=`ip route |grep default |grep $selnic |head -n1 |awk -F "via " '{print$2}' |awk -F " dev" '{print$1}'`
echo $selnic  $selnicip  $selnicgateway >> /tmp/nic
done
ip a | grep -E "^[0-9]" | grep -vE ": lo|: virbr" |awk -F ": <" '{print$1}' |awk -F ": " '{print$2}' > /tmp/nicnew
for selbond in `ip a | grep -E "^[0-9]" | grep -vE ": lo|: virbr" |awk -F ": <" '{print$1}' |awk -F ": " '{print$2}' |grep -E "team|bond"`;
do
selbondip=`ip a s $selbond | grep -o '[0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+' | head -n1`
selbondgateway=`ip route |grep default |grep $selbond |head -n1 |awk -F "via " '{print$2}' |awk -F " dev" '{print$1}'`
echo $selbond  $selbondip  $selbondgateway >> /tmp/presentbondall
done
nicall=`cat /tmp/nicall`
nicallnum=`cat /tmp/nic |wc -l`
if [ "$nicallnum" == 1 ];then
echo -e "\e[94m当前主机只有一块网卡,无需做绑定!\e[0m"
exit
fi
if [[ "$nicall" =~ "bond" ]];then
echo -e "\e[94m当前系统已存在bond!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
for detectnic in `ip a | grep -E "^[0-9]" | grep -vE ": lo|: bond|: virbr" |awk -F ": <" '{print$1}' |awk -F ": " '{print$2}'`;
do
if [ -f "/etc/sysconfig/network-scripts/ifcfg-$detectnic" ];then
bondsubniccontent=`cat /etc/sysconfig/network-scripts/ifcfg-$detectnic`
if [[ "$bondsubniccontent" =~ "bond" ]];then
presentbond=`cat /etc/sysconfig/network-scripts/ifcfg-$detectnic |grep MASTER |awk -F "=" '{print$2}'`
echo -e "\e[94m$detectnic 已是 $presentbond 成员!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
sleep 1
fi
fi
done
fi
if [[ "$nicall" =~ "team" ]];then
echo -e "\e[94m当前系统已存在team!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
for detectnic in `ls /etc/sysconfig/network-scripts/ifcfg-team*-* | xargs -i basename {\}`;
do
teamsubniccontent=`cat /etc/sysconfig/network-scripts/$detectnic`
if [[ "$teamsubniccontent" =~ "team" ]];then
presentteam=`cat /etc/sysconfig/network-scripts/$detectnic |grep TEAM_MASTER |awk -F "=" '{print$2}'`
teamport=`cat /etc/sysconfig/network-scripts/$detectnic |grep '\<DEVICE\>' |awk -F "=" '{print$2}'`
echo $teamport $presentteam >> /tmp/teams
echo -e "\e[94m$teamport 已是 $presentteam 成员!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
sleep 1
fi
done
fi
if [[ -f "/tmp/presentbondall" ]];then
echo -e "\e[94m当前主机已有网卡聚合组:\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
cat /tmp/presentbondall
echo -e "\e[94m--------------------------------------------------------------\e[0m"
fi
echo -e "\e[94m当前主机所有网卡如下:\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
cat /tmp/nic |awk '{print NR,$0}'
echo -e "\e[94m--------------------------------------------------------------\e[0m"
while true
do
read -p "选择用来绑定的网卡,输入数字即可,多个网卡,用空格隔开: " nicnum
selectnum=`echo $nicnum |awk -F " " '{print NF}'`
if [ $selectnum -lt 2 ];then
echo -e "\e[94m最少选择两块网卡,请重新选择!\e[0m"
else
break;
fi
done
for num in $nicnum;
do
nicx=`cat /tmp/nic |awk "NR==$num" |awk -F " " '{print$1}'`
if [[ "$nicx" == "" ]];then
echo -e "\e[94m请选择正确的网卡!\e[0m"
exit
fi
done
for num in $nicnum;
do
selectnic=`cat /tmp/nic |awk "NR==$num" |awk -F " " '{print$1}'`
selectnicip=`ip a s $selectnic | grep -o '[0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+' | head -n1`
if [ -n "$selectnicip" ];then
echo -e "\e[94m网卡 $selectnic 已有IP地址 $selectnicip \e[0m"
read -p "是否继续?如果继续,$selectnic 的IP配置会被删除,主机网络可能会中断!按n退出,按其他按键继续:" answer0
answer0lower=`echo $answer0 | tr [A-Z] [a-z]`
if [[ "$answer0lower" == "n" ]]; then
echo -e "\e[94m如需绑定,请重新执行脚本,选择其他网卡!\e[0m"
exit
fi
fi
if [ -f "/etc/sysconfig/network-scripts/ifcfg-$selectnic" ];then
sed -i '/ONBOOT/s/yes/no/' /etc/sysconfig/network-scripts/ifcfg-$selectnic
selectniccontent=`cat /etc/sysconfig/network-scripts/ifcfg-$selectnic`
selectnicinpresentbond=`cat /etc/sysconfig/network-scripts/ifcfg-$selectnic |grep MASTER |awk -F "=" '{print$2}'`
echo -e "\e[94m已选择网卡 $selectnic \e[0m"
if [[ "$selectniccontent" =~ "bond" ]];then
echo -e "\e[94m网卡 $selectnic 已是$selectnicinpresentbond成员!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
read -p "是否继续?如果继续,$selectnicinpresentbond 会被删除!按n退出,按其他按键继续:" answer1
answer1lower=`echo $answer1 | tr [A-Z] [a-z]`
if [[ "$answer1lower" == "n" ]]; then
echo -e "\e[94m如需绑定,请重新执行脚本,选择其他网卡!\e[0m"
exit
else
echo -e "\e[94m网卡 $selectnic 已选定!\e[0m"
ip a | grep -E "^[0-9]" | grep -vE ": lo|: virbr" |awk -F ": <" '{print$1}' |awk -F ": " '{print$2}' > /tmp/nicnew
nicnewall=`cat /tmp/nicnew`
if [[ "$nicnewall" =~ "$selectnicinpresentbond" ]];then
ifdown $selectnicinpresentbond > /dev/null 2>&1
echo -e "\e[94m已将现有 $selectnicinpresentbond 禁用!\e[0m"
cp -f /etc/sysconfig/network-scripts/ifcfg-$selectnicinpresentbond /etc/sysconfig/network-scripts/ifcfg-$selectnicinpresentbond-`date +"%Y-%m-%d-%H-%M"`.bak > /dev/null 2>&1
rm -f /etc/sysconfig/network-scripts/ifcfg-$selectnicinpresentbond
echo -$selectnicinpresentbond > /sys/class/net/bonding_masters
echo -e "\e[94m已将现有 $selectnicinpresentbond 配置文件备份!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
fi
fi
fi
cp -f /etc/sysconfig/network-scripts/ifcfg-$selectnic /etc/sysconfig/network-scripts/ifcfg-$selectnic-`date +"%Y-%m-%d-%H-%M"`.bak > /dev/null 2>&1
rm -f /etc/sysconfig/network-scripts/ifcfg-$selectnic
echo -e "\e[94m网卡 $selectnic 配置文件已备份!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
ls /etc/sysconfig/network-scripts/*.bak > /dev/null 2>&1
if [ $? -eq 0 ];then
cp -f /etc/sysconfig/network-scripts/*.bak /etc/sysconfig/network-scripts/nicsbak/
fi
rm -f /etc/sysconfig/network-scripts/*.bak
sleep 1
else
echo -e "\e[94m网卡 $selectnic 已选定!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
sleep 1
fi
done
if [ -f "/tmp/teams" ];then
for num in $nicnum;
do
selectteamnic=`cat /tmp/nic |awk "NR==$num" |awk -F " " '{print$1}'`
selectnicinpresentteam=`cat /tmp/teams |grep $selectteamnic |awk -F " " '{print$2}'`
echo -e "\e[94m已选择网卡 $selectteamnic \e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
if [[ "$selectnicinpresentteam" =~ "team" ]];then
echo -e "\e[94m网卡 $selectteamnic 已是 $selectnicinpresentteam 成员!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
read -p "是否继续?如果继续,$selectnicinpresentteam 会被删除!按n退出,按其他按键继续:" answer2
answer2lower=`echo $answer2 | tr [A-Z] [a-z]`
if [[ "$answer2lower" == "n" ]]; then
echo -e "\e[94m如需绑定,请重新执行脚本,选择其他网卡!\e[0m"
exit
else
echo -e "\e[94m网卡 $selectteamnic 已选定!\e[0m"
ip a | grep -E "^[0-9]" | grep -vE ": lo|: virbr" |awk -F ": <" '{print$1}' |awk -F ": " '{print$2}' > /tmp/nicnew
nicnewall=`cat /tmp/nicnew`
if [[ "$nicnewall" =~ "$selectnicinpresentteam" ]];then
cp -f /etc/sysconfig/network-scripts/ifcfg-$selectnicinpresentteam* /etc/sysconfig/network-scripts/nicsbak/
if [ "$oskernel" = "3.10.0" ]; then
systemctl start NetworkManager > /dev/null 2>&1
fi
if [ "$oskernel" = "4.18.0" ]; then
systemctl start NetworkManager > /dev/null 2>&1
fi
for disteam in `ls /etc/sysconfig/network-scripts/ifcfg-$selectnicinpresentteam* | xargs -i basename {\} |awk -F "ifcfg-" '{print$2}'`
do
cp -f /etc/sysconfig/network-scripts/ifcfg-$disteam /etc/sysconfig/network-scripts/ifcfg-$disteam-`date +"%Y-%m-%d-%H-%M"`.bak > /dev/null 2>&1
nmcli con delete $disteam > /dev/null 2>&1
done 
echo -e "\e[94m已将现有 $selectnicinpresentteam 禁用!\e[0m"
echo -e "\e[94m已将现有 $selectnicinpresentteam 配置文件备份!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
fi
fi
cp -f /etc/sysconfig/network-scripts/ifcfg-$selectteamnic /etc/sysconfig/network-scripts/ifcfg-$selectteamnic-`date +"%Y-%m-%d-%H-%M"`.bak > /dev/null 2>&1
ls /etc/sysconfig/network-scripts/*.bak > /dev/null 2>&1
if [ $? -eq 0 ];then
cp -f /etc/sysconfig/network-scripts/*.bak /etc/sysconfig/network-scripts/nicsbak/
fi
rm -f /etc/sysconfig/network-scripts/*.bak
echo -e "\e[94m网卡 $selectteamnic 配置文件已备份!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
sleep 1
else
echo -e "\e[94m网卡 $selectteamnic 已选定!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
if [ "$oskernel" = "3.10.0" ]; then
systemctl start NetworkManager > /dev/null 2>&1
fi
if [ "$oskernel" = "4.18.0" ]; then
systemctl start NetworkManager > /dev/null 2>&1
fi
sleep 1
fi
done
fi
if [[ $selectmode == 1 ]];then
systemctl stop NetworkManager > /dev/null 2>&1
systemctl disable NetworkManager > /dev/null 2>&1
while true
do
read -p "请输入bond名称,如 bond0 : " bondname1
bondname=`echo ${bondname1,,}`
for ifpresentbond in `cat /tmp/nicnew`
do
if [[ "$ifpresentbond" == "$bondname" ]];then
echo -e "\e[94m当前已存在 $bondname 请重新输入!\e[0m"
read -p "请输入bond名称,如 bond0 : " bondname
echo -e "\e[94m输入的bond名称为:$bondname \e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
fi
done
if [ -z $bondname ];then
echo -e "\e[94mbond名称不能为空,请重新输入\e[0m"
else
echo -e "\e[94m输入的bond名称为:$bondname \e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
fi
done
while true
do
read -p "请输入 $bondname IP地址: " bondip
ipcalc -cs $bondip
if [ $? -ne 0  ];then
echo -e "\e[94m请输入有效的IP地址\e[0m"
else
echo -e "\e[94m输入的 $bondname IP地址为 $bondip \e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
fi
done
while true
do
read -p "请输入 $bondname IP地址掩码或者掩码前缀: " bondnetmask
if [[ -n "$bondnetmask" && "$bondnetmask" =~ "." ]];then
ipcalc -p $bondip $bondnetmask > /dev/null 2>&1
if [ $? -ne 0 ];then
echo -e "\e[94m请输入有效的掩码地址!\e[0m"
else
bondmask=`ipcalc -p $bondip $bondnetmask |awk -F "=" '{print$2}'`
echo -e "\e[94m输入的掩码前缀为 $bondmask\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
fi
fi
if [ -z $bondnetmask ];then
echo -e "\e[94m请输入有效的掩码地址!\e[0m"
fi
if [ -n "$bondnetmask" ];then
if [[ "$bondnetmask" -gt 1 && "$bondnetmask" -lt 32 ]] > /dev/null 2>&1;then 
echo -e "\e[94m输入的掩码前缀为 $bondnetmask\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
bondmask=$bondnetmask
break;
else 
echo -e "\e[94m输入的子网掩码有误,请重新输入!\e[0m"
fi 
fi
done
defaultgateway=`ip route |grep default |awk -F "via " '{print$2}' |awk -F " dev" '{print$1}' |head -n1`
if [ -n $defaultgateway ];then
echo
echo -e "\e[94m提示:当前系统已存在默认网关 $defaultgateway 如果配置bond网关,可能会导致网络不通,如需配置,请尝试添加静态路由!\e[0m"
sleep 3
fi
while true
do
read -p "请输入 $bondname 网关地址,如无网关,则直接回车: " bondgateway
if [ -n "$bondgateway" ];then
ipcalc -cs $bondgateway
if [ $? -ne 0  ];then
echo -e "\e[94m请输入有效的网关地址\e[0m"
else
echo -e "\e[94m输入的为 $bondname 的网关为 $bondgateway \e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
gateway=$bondgateway
break;
fi
else
echo -e "\e[94m已配置 $bondname 网关为空!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
gateway=$bondgateway
break;
fi
done
while true
do
read -p "请输入 $bondname 第一个DNS地址,如无,则直接回车: " bonddns1
if [ -n "$bonddns1" ];then
ipcalc -cs $bonddns1
if [ $? -ne 0  ];then
echo -e "\e[94m请输入有效的DNS地址\e[0m"
else
echo -e "\e[94m输入的第一个DNS地址为 $bonddns1 \e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
dns1=$bonddns1
break;
fi
else
echo -e "\e[94m已配置DNS地址为空!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
fi
done
if [ -n "$dns1" ];then
while true
do
read -p "请输入 $bondname 第二个DNS地址,如无,则直接回车: " bonddns2
if [ -n "$bonddns2" ];then
ipcalc -cs $bonddns2
if [ $? -ne 0  ];then
echo -e "\e[94m请输入有效的DNS地址\e[0m"
else
echo -e "\e[94m输入的第二个DNS地址为 $bonddns2 \e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
dns2=$bonddns2
break;
fi
else
echo -e "\e[94m已配置DNS地址为空!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
fi
done
fi
while true
do
echo -e "\e[94m##############################################################\e[0m"
echo -e "\e[94m#                      0,平衡轮循环策略                     #\e[0m"
echo -e "\e[94m#                      1,主备策略                           #\e[0m"
echo -e "\e[94m#                      2,平衡策略                           #\e[0m"
echo -e "\e[94m#                      3,广播策略                           #\e[0m"
echo -e "\e[94m#                      4,802.3ad动态链接聚合                #\e[0m"
echo -e "\e[94m#                      5,适配器传输负载均衡                 #\e[0m"
echo -e "\e[94m#                      6,适配器适应性负载均衡               #\e[0m"
echo -e "\e[94m##############################################################\e[0m"
read -p "请选择绑定模式,输入数字即可: " bondmode
if [ "$bondmode" == 0 ];then
echo -e "\e[94m已选择bond模式为 0 即:(balance-rr0)(平衡轮循环策略)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
elif [ "$bondmode" == 1 ];then
echo -e "\e[94m已选择bond模式为 1 即:(active-backup)(主备策略)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
elif [ "$bondmode" == 2 ];then
echo -e "\e[94m已选择bond模式为 2 即:(balance-xor)(平衡策略)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
elif [ "$bondmode" == 3 ];then
echo -e "\e[94m已选择bond模式为 3 即:(broadcast)(广播策略)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
elif [ "$bondmode" == 4 ];then
echo -e "\e[94m已选择bond模式为 4 即:(802.3ad)(IEEE 802.3ad 动态链接聚合)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
elif [ "$bondmode" == 5 ];then
echo -e "\e[94m已选择bond模式为 5 即:(balance-tlb)(适配器传输负载均衡)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
elif [ "$bondmode" == 6 ];then
echo -e "\e[94m已选择bond模式为 6 即:(balance-alb)(适配器适应性负载均衡)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
else
echo -e "\e[94m请选择正确的bond模式! \e[0m"
fi
done
for num in $nicnum;
do
selectnic=`cat /tmp/nic |awk "NR==$num" |awk -F " " '{print$1}'`
cat > /etc/sysconfig/network-scripts/ifcfg-$selectnic << EOF
DEVICE=$selectnic
BOOTPROTO=none
ONBOOT=yes
MASTER=$bondname
SLAVE=yes
USERCTL=no
NM_CONTROLLED=no
EOF
echo -e "\e[94m已配置 $bondname 成员 $selectnic \e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
sleep 1
done
cat > /etc/sysconfig/network-scripts/ifcfg-$bondname << EOF  
DEVICE=$bondname
ONBOOT=yes
BOOTPROTO=static
USERCTL=no
NM_CONTROLLED=no
IPADDR=$bondip
PREFIX=$bondmask
EOF
if [ -n "$gateway" ];then
cat >> /etc/sysconfig/network-scripts/ifcfg-$bondname << EOF
GATEWAY=$gateway
EOF
fi
if [ -n "$dns1" ];then
cat >> /etc/sysconfig/network-scripts/ifcfg-$bondname << EOF
DNS1=$dns1
EOF
fi
if [ -n "$dns2" ];then
cat >> /etc/sysconfig/network-scripts/ifcfg-$bondname << EOF
DNS2=$dns2
EOF
fi
if [ "$bondmode" == 4 ];then 
cat >> /etc/sysconfig/network-scripts/ifcfg-$bondname << EOF
BONDING_OPTS="mode=4 miimon=100 xmit_hash_policy=layer2+3"
EOF
else
cat >> /etc/sysconfig/network-scripts/ifcfg-$bondname << EOF
BONDING_OPTS="mode=$bondmode miimon=100"
EOF
fi
if [ "$oskernel" = "2.6.32" ]; then
service network restart > /dev/null 2>&1
elif [ "$oskernel" = "3.10.0" ]; then
systemctl restart network > /dev/null 2>&1
elif [ "$oskernel" = "4.18.0" ]; then
systemctl restart network > /dev/null 2>&1
fi
echo -e "\e[94m$bondname信息\e[0m"
ip a s $bondname
bondinfo=`cat /proc/net/bonding/$bondname |grep Mode |awk -F ": " '{print$2}'`
echo -e "\e[94m已配置的 $bondname 模式为 $bondinfo \e[0m"
echo
cat /proc/net/bonding/$bondname
ls /etc/sysconfig/network-scripts/ifcfg-bond0 > /dev/null 2>&1
if [ $? -ne 0 ];then
for niclist in `ip a | grep -E "^[0-9]" | grep -vE ": lo|: virbr" |awk -F ": <" '{print$1}' |awk -F ": " '{print$2}'`;
do
if [[ "$niclist" == "bond0" && "$bondname" != "bond0" ]];then
echo -bond0 > /sys/class/net/bonding_masters
fi
done
fi
rm -f /tmp/nic
rm -f /tmp/nicnew
rm -f /tmp/presentbondall
rm -f /tmp/nicall
rm -f /tmp/teams
echo -e "\e[94m--------------------------------------------------------------\e[0m"
echo -e "\e[94m网卡聚合 $bondname 配置已完成!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
fi
if [[ $selectmode == 2 ]];then
systemctl start NetworkManager > /dev/null 2>&1
systemctl enable NetworkManager > /dev/null 2>&1
while true
do
read -p "请输入team名称,如 team0 : " teamname1
teamname=`echo ${teamname1,,}`
for ifpresentteam in `cat /tmp/nicnew`
do
if [[ "$ifpresentteam" == "$teamname" ]];then
echo -e "\e[94m当前已存在 $teamname 请重新输入!\e[0m"
read -p "请输入team名称,如 team0 : " teamname
echo -e "\e[94m输入的team名称为:$teamname \e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
fi
done
if [ -z $teamname ];then
echo -e "\e[94mteam名称不能为空,请重新输入\e[0m"
else
echo -e "\e[94m输入的team名称为:$teamname \e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
fi
done
while true
do
read -p "请输入 $teamname IP地址: " teamip
ipcalc -cs $teamip
if [ $? -ne 0  ];then
echo -e "\e[94m请输入有效的IP地址\e[0m"
else
echo -e "\e[94m输入的 $teamname IP地址为 $teamip \e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
fi
done
while true
do
read -p "请输入 $teamname IP地址掩码或者掩码前缀: " teamnetmask
if [[ -n "$teamnetmask" && "$teamnetmask" =~ "." ]];then
ipcalc -c $teamip $teamnetmask > /dev/null 2>&1
if [ $? -ne 0 ];then
echo -e "\e[94m请输入有效的掩码地址!\e[0m"
else
teammask=`ipcalc -p $teamip $teamnetmask |awk -F "=" '{print$2}'`
echo -e "\e[94m输入的掩码前缀为 $teammask\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
fi
fi
if [ -z $teamnetmask ];then
echo -e "\e[94m请输入有效的掩码地址!\e[0m"
fi
if [ -n "$teamnetmask" ];then
if [[ "$teamnetmask" -gt 1 && "$teamnetmask" -lt 32 ]] > /dev/null 2>&1;then 
echo -e "\e[94m输入的掩码前缀为 $teamnetmask\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
teammask=$teamnetmask
break;
else 
echo -e "\e[94m输入的子网掩码有误,请重新输入!\e[0m"
fi 
fi
done
defaultgateway=`ip route |grep default |awk -F "via " '{print$2}' |awk -F " dev" '{print$1}' |head -n1`
if [ -n $defaultgateway ];then
echo
echo -e "\e[94m提示:当前系统已存在默认网关 $defaultgateway 如果配置team网关,可能会导致网络不通,如需配置,请尝试添加静态路由!\e[0m"
sleep 3
fi
while true
do
read -p "请输入 $teamname 网关地址,如无网关,则直接回车: " teamgateway
if [ -n "$teamgateway" ];then
ipcalc -cs $teamgateway
if [ $? -ne 0  ];then
echo -e "\e[94m请输入有效的网关地址\e[0m"
else
echo -e "\e[94m输入的为 $teamname 的网关为 $teamgateway \e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
gateway=$teamgateway
break;
fi
else
echo -e "\e[94m已配置 $teamname 网关为空!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
gateway=$teamgateway
break;
fi
done
while true
do
read -p "请输入 $teamname 第一个DNS地址,如无,则直接回车: " teamdns1
if [ -n "$teamdns1" ];then
ipcalc -cs $teamdns1
if [ $? -ne 0  ];then
echo -e "\e[94m请输入有效的DNS地址\e[0m"
else
echo -e "\e[94m输入的第一个DNS地址为 $teamdns1 \e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
dns1=$teamdns1
break;
fi
else
echo -e "\e[94m已配置DNS地址为空!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
fi
done
if [ -n "$dns1" ];then
while true
do
read -p "请输入 $teamname 第二个DNS地址,如无,则直接回车: " teamdns2
if [ -n "$teamdns2" ];then
ipcalc -cs $teamdns2
if [ $? -ne 0  ];then
echo -e "\e[94m请输入有效的DNS地址\e[0m"
else
echo -e "\e[94m输入的第二个DNS地址为 $teamdns2 \e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
dns2=$teamdns2
break;
fi
else
echo -e "\e[94m已配置DNS地址为空!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
fi
done
fi
while true
do
echo -e "\e[94m##############################################################\e[0m"
echo -e "\e[94m#                           1,广播容错                      #\e[0m"
echo -e "\e[94m#                           2,负载轮询                      #\e[0m"
echo -e "\e[94m#                           3,主备模式                      #\e[0m"
echo -e "\e[94m#                           4,负载均衡                      #\e[0m"
echo -e "\e[94m#                           5,LACP                          #\e[0m"
echo -e "\e[94m##############################################################\e[0m"
read -p "请选择绑定模式,输入数字即可: " teammode
if [ "$teammode" == 1 ];then
echo -e "\e[94m已选择team模式为:(broadcast)(广播容错)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
teammode=broadcast
break;
elif [ "$teammode" == 2 ];then
echo -e "\e[94m已选择team模式为:(roundrobin)(负载轮询)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
teammode=roundrobin
break;
elif [ "$teammode" == 3 ];then
echo -e "\e[94m已选择team模式为:(activebackup)(主备模式)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
teammode=activebackup
break;
elif [ "$teammode" == 4 ];then
echo -e "\e[94m已选择team模式为:(loadbalance)(负载均衡)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
teammode=loadbalance
break;
elif [ "$teammode" == 5 ];then
echo -e "\e[94m已选择team模式为:(lacp)(LACP)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
teammode=lacp
break;
else
echo -e "\e[94m请选择正确的team模式! \e[0m"
fi
done
nmcli con add con-name $teamname type team ifname $teamname config '{"runner":{"name":"'$teammode'"}}' > /dev/null 2>&1
if [[ -n "$gateway" && -n "dns1" ]];then
nmcli con mod $teamname ipv4.addresses $teamip/$teammask ipv4.gateway $gateway ipv4.dns $dns1 ipv4.method manual connectio.autoconnect yes > /dev/null 2>&1
fi
if [[ -n "$gateway" && -n "dns1" && -n "dns2" ]];then
nmcli con mod $teamname ipv4.addresses $teamip/$teammask ipv4.gateway $gateway ipv4.dns $dns1,$dns2 ipv4.method manual connectio.autoconnect yes > /dev/null 2>&1
fi
if [[ -n "$gateway" ]];then
nmcli con mod $teamname ipv4.addresses $teamip/$teammask ipv4.gateway $gateway ipv4.method manual connectio.autoconnect yes > /dev/null 2>&1
fi
if [[ -z "$gateway" ]];then
nmcli con mod $teamname ipv4.addresses $teamip/$teammask ipv4.method manual connectio.autoconnect yes > /dev/null 2>&1
fi
if [[ -z "$gateway" && -n "dns1" ]];then
nmcli con mod $teamname ipv4.addresses $teamip/$teammask ipv4.dns $dns1 ipv4.method manual connectio.autoconnect yes > /dev/null 2>&1
fi
if [[ -z "$gateway" && -n "dns1" && -n "dns2" ]];then
nmcli con mod $teamname ipv4.addresses $teamip/$teammask ipv4.dns $dns1,$dns2 ipv4.method manual connectio.autoconnect yes > /dev/null 2>&1
fi
for num in $nicnum;
do
selectnic=`cat /tmp/nic |awk "NR==$num" |awk -F " " '{print$1}'`
nmcli con down $selectnic > /dev/null 2>&1
nmcli con add type team-slave con-name $teamname-port-$selectnic ifname $selectnic master $teamname > /dev/null 2>&1
done
nmcli con reload > /dev/null 2>&1
nmcli con up $teamname > /dev/null 2>&1
teamdctl $teamname state
rm -f /tmp/nic
rm -f /tmp/nicnew
rm -f /tmp/presentbondall
rm -f /tmp/nicall
rm -f /tmp/teams
systemctl restart network > /dev/null 2>&1
echo -e "\e[94m网卡聚合 $teamname 配置已完成!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
fi
fi
 
if [ $language == 2 ];then
echo -e "\e[94m##############################################################\e[0m"
echo -e "\e[94m#   1.Linux NIC binding script,support bond and team mode    #\e[0m"
echo -e "\e[94m#   2.Support CentOS 6.x/7.x/8.x RedHat 6.x/7.x/8.x          #\e[0m"
echo -e "\e[94m#   3.Author: Mr.Mao                                         #\e[0m"
echo -e "\e[94m#   4.Compiled: March 14,2020                                #\e[0m"
echo -e "\e[94m#   5.More view https://www.mr-mao.cn                        #\e[0m"
echo -e "\e[94m##############################################################\e[0m"
echo -e "\033[1;mTip:If the Backspace key does not work,press Ctrl+Backspace\e[0m"
oskernel=`uname -r | awk -F"-" '{print $1}'`
osrealease=`cat /etc/redhat-release`
echo -e "\e[94m--------------------------------------------------------------\e[0m"
echo -e "\e[94mThe current operating system is $osrealease\e[0m"
if [ "$oskernel" = "2.6.32" ]; then
echo -e "\e[94mKernel version is 2.6.32 Series\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
echo -e "\e[94mThe current OS only supports the bond binding mode!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
selectmode=1
service NetworkManager stop > /dev/null 2>&1
chkconfig NetworkManager off > /dev/null 2>&1
elif [ "$oskernel" = "3.10.0" ]; then
systemctl status network > /dev/null 2>&1
if [ $? -ne 0 ];then
echo -e "\e[94mThe network service is not running normally. Please check the network service first!\e[0m"
systemctl status network
exit
fi
echo -e "\e[94mKernel version is 3.10.0 Series\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
echo -e "\e[94m##############################################################\e[0m"
echo -e "\e[94m#                           1.Bond Mode                      #\e[0m"
echo -e "\e[94m#                           2.Team Mode                      #\e[0m"
echo -e "\e[94m##############################################################\e[0m"
echo -e "\033[1;mTip:choose one of them.It is not recommended to coexist in both modes!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
echo -e "\e[94mThe current OS supports two binding modes:team and bond.Choose one: \e[0m"
while true
do
read -p "Select the mode and enter the number: " selectmode
if [[ $selectmode == 1 ]];then
systemctl stop NetworkManager > /dev/null 2>&1
systemctl disable NetworkManager > /dev/null 2>&1
echo -e "\e[94mBond binding mode has been selected!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
elif [[ $selectmode == 2 ]];then
systemctl enable NetworkManager > /dev/null 2>&1
systemctl start NetworkManager > /dev/null 2>&1
systemctl status NetworkManager > /dev/null 2>&1
if [ $? -ne 0 ];then
echo -e "\e[94m#Please check the NetworkManager!\e[0m"
exit
fi
echo -e "\e[94mTeam binding mode has been selected!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
else
echo -e "\e[94mPlease select the correct binding mode!\e[0m"
fi
done
elif [ "$oskernel" = "4.18.0" ]; then
echo -e "\e[94mKernel version is 4.18.0 Series\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
while true
do
systemctl start network > /dev/null 2>&1
systemctl status network > /dev/null 2>&1
if [ $? -ne 0 ];then
echo -e "\e[94mThe current OS only supports the team binding mode!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
selectmode=2
systemctl enable NetworkManager > /dev/null 2>&1
systemctl start NetworkManager > /dev/null 2>&1
systemctl status NetworkManager > /dev/null 2>&1
if [ $? -ne 0 ];then
echo -e "\e[94mPlease check the NetworkManager!\e[0m"
exit
fi
sleep 1
break;
else
echo -e "\e[94m##############################################################\e[0m"
echo -e "\e[94m#                          1.Bond Mode                       #\e[0m"
echo -e "\e[94m#                          2.Team Mode                       #\e[0m"
echo -e "\e[94m##############################################################\e[0m"
echo -e "\e[94mTip:choose one of them. It is not recommended to coexist in both modes!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
echo -e "\e[94mThe current OS supports two binding modes:team and bond.Choose one: \e[0m"
read -p "Select the mode and enter the number: " selectmode
if [[ $selectmode == 1 ]];then
systemctl stop NetworkManager > /dev/null 2>&1
systemctl disable NetworkManager > /dev/null 2>&1
echo -e "\e[94mBond binding mode has been selected!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
selectmode=1
break;
elif [[ $selectmode == 2 ]];then
systemctl enable NetworkManager > /dev/null 2>&1
systemctl start NetworkManager > /dev/null 2>&1
systemctl status NetworkManager > /dev/null 2>&1
if [ $? -ne 0 ];then
echo -e "\e[94m#Please check the NetworkManager!#\e[0m"
exit
fi
echo -e "\e[94mTeam binding mode has been selected!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
selectmode=2
break;
else
echo -e "\e[94mPlease select the correct binding mode!\e[0m"
fi
fi
done
else
echo -e "\e[94mThis script does not support the current OS!\e[0m"
exit
fi
rm -f /tmp/nic
rm -f /tmp/nicnew
rm -f /tmp/presentbondall
rm -f /tmp/nicall
rm -f /tmp/teams
mkdir -p /etc/sysconfig/network-scripts/nicsbak
ip a | grep -E "^[0-9]" | grep -vE ": lo|: virbr" |awk -F ": <" '{print$1}' |awk -F ": " '{print$2}' > /tmp/nicall
for selnic in `ip a | grep -E "^[0-9]" | grep -vE ": lo|: virbr|: team|: bond" |awk -F ": <" '{print$1}' |awk -F ": " '{print$2}';`
do
selnicip=`ip a s $selnic | grep -o '[0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+' | head -n1`
selnicgateway=`ip route |grep default |grep $selnic |head -n1 |awk -F "via " '{print$2}' |awk -F " dev" '{print$1}'`
echo $selnic  $selnicip  $selnicgateway >> /tmp/nic
done
ip a | grep -E "^[0-9]" | grep -vE ": lo|: virbr" |awk -F ": <" '{print$1}' |awk -F ": " '{print$2}' > /tmp/nicnew
for selbond in `ip a | grep -E "^[0-9]" | grep -vE ": lo|: virbr" |awk -F ": <" '{print$1}' |awk -F ": " '{print$2}' |grep -E "team|bond"`;
do
selbondip=`ip a s $selbond | grep -o '[0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+' | head -n1`
selbondgateway=`ip route |grep default |grep $selbond |head -n1 |awk -F "via " '{print$2}' |awk -F " dev" '{print$1}'`
echo $selbond  $selbondip  $selbondgateway >> /tmp/presentbondall
done
nicall=`cat /tmp/nicall`
nicallnum=`cat /tmp/nic |wc -l`
if [ "$nicallnum" == 1 ];then
echo -e "\e[94mThe current host has only one NIC, no binding is required!\e[0m"
exit
fi
if [[ "$nicall" =~ "bond" ]];then
echo -e "\e[94mBond already exists in the current system!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
for detectnic in `ip a | grep -E "^[0-9]" | grep -vE ": lo|: bond|: virbr" |awk -F ": <" '{print$1}' |awk -F ": " '{print$2}'`;
do
if [ -f "/etc/sysconfig/network-scripts/ifcfg-$detectnic" ];then
bondsubniccontent=`cat /etc/sysconfig/network-scripts/ifcfg-$detectnic`
if [[ "$bondsubniccontent" =~ "bond" ]];then
presentbond=`cat /etc/sysconfig/network-scripts/ifcfg-$detectnic |grep MASTER |awk -F "=" '{print$2}'`
echo -e "\e[94m$detectnic is already a member of $presentbond!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
sleep 1
fi
fi
done
fi
if [[ "$nicall" =~ "team" ]];then
echo -e "\e[94mteam already exists in the current system!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
for detectnic in `ls /etc/sysconfig/network-scripts/ifcfg-team*-* | xargs -i basename {\}`;
do
teamsubniccontent=`cat /etc/sysconfig/network-scripts/$detectnic`
if [[ "$teamsubniccontent" =~ "team" ]];then
presentteam=`cat /etc/sysconfig/network-scripts/$detectnic |grep TEAM_MASTER |awk -F "=" '{print$2}'`
teamport=`cat /etc/sysconfig/network-scripts/$detectnic |grep '\<DEVICE\>' |awk -F "=" '{print$2}'`
echo $teamport $presentteam >> /tmp/teams
echo -e "\e[94m$teamport is already a member of $presentteam!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
sleep 1
fi
done
fi
if [[ -f "/tmp/presentbondall" ]];then
echo -e "\e[94mThe current host already has a NIC aggregation group:\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
cat /tmp/presentbondall
echo -e "\e[94m--------------------------------------------------------------\e[0m"
fi
echo -e "\e[94mAll NICs of the current host are as follows:\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
cat /tmp/nic |awk '{print NR,$0}'
echo -e "\e[94m--------------------------------------------------------------\e[0m"
while true
do
read -p "Select the NIC used for binding, enter the number, multiple NIC, separated by spaces: " nicnum
selectnum=`echo $nicnum |awk -F " " '{print NF}'`
if [ $selectnum -lt 2 ];then
echo -e "\e[94mSelect at least two NICs, please choose again!\e[0m"
else
break;
fi
done
for num in $nicnum;
do
nicx=`cat /tmp/nic |awk "NR==$num" |awk -F " " '{print$1}'`
if [[ "$nicx" == "" ]];then
echo -e "\e[94mPlease select correct NIC!\e[0m"
exit
fi
done
for num in $nicnum;
do
selectnic=`cat /tmp/nic |awk "NR==$num" |awk -F " " '{print$1}'`
selectnicip=`ip a s $selectnic | grep -o '[0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+' | head -n1`
if [ -n "$selectnicip" ];then
echo -e "\e[94mNIC $selectnic already has an IP address $selectnicip\e[0m"
read -p "Whether to continue? If you continue, the IP configuration of $selectnic will be deleted and the host communication may be interrupted! Press n to exit, press other keys to continue: " answer0
answer0lower=`echo $answer0 | tr [A-Z] [a-z]`
if [[ "$answer0lower" == "n" ]]; then
echo -e "\e[94mIf you need to bind, please re-execute the script and select another NICs!\e[0m"
exit
fi
fi
if [ -f "/etc/sysconfig/network-scripts/ifcfg-$selectnic" ];then
sed -i '/ONBOOT/s/yes/no/' /etc/sysconfig/network-scripts/ifcfg-$selectnic
selectniccontent=`cat /etc/sysconfig/network-scripts/ifcfg-$selectnic`
selectnicinpresentbond=`cat /etc/sysconfig/network-scripts/ifcfg-$selectnic |grep MASTER |awk -F "=" '{print$2}'`
echo -e "\e[94mChosen NIC $selectnic\e[0m"
if [[ "$selectniccontent" =~ "bond" ]];then
echo -e "\e[94mNIC $selectnic is already a member of $selectnicinpresentbond!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
read -p "Whether to continue? If you continue, $selectnicinpresentbond will be deleted! Press n to exit, press other keys to continue: " answer1
answer1lower=`echo $answer1 | tr [A-Z] [a-z]`
if [[ "$answer1lower" == "n" ]]; then
echo -e "\e[94mIf you need to bind, please re-execute the script and select another NICs!\e[0m"
exit
else
echo -e "\e[94mNIC $selectnic has been selected!\e[0m"
ip a | grep -E "^[0-9]" | grep -vE ": lo|: virbr" |awk -F ": <" '{print$1}' |awk -F ": " '{print$2}' > /tmp/nicnew
nicnewall=`cat /tmp/nicnew`
if [[ "$nicnewall" =~ "$selectnicinpresentbond" ]];then
ifdown $selectnicinpresentbond > /dev/null 2>&1
echo -e "\e[94mNIC $selectnicinpresentbond has been disabled!\e[0m"
cp -f /etc/sysconfig/network-scripts/ifcfg-$selectnicinpresentbond /etc/sysconfig/network-scripts/ifcfg-$selectnicinpresentbond-`date +"%Y-%m-%d-%H-%M"`.bak > /dev/null 2>&1
rm -f /etc/sysconfig/network-scripts/ifcfg-$selectnicinpresentbond
echo -$selectnicinpresentbond > /sys/class/net/bonding_masters
echo -e "\e[94mNIC $selectnicinpresentbond configuration is backed up!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
fi
fi
fi
cp -f /etc/sysconfig/network-scripts/ifcfg-$selectnic /etc/sysconfig/network-scripts/ifcfg-$selectnic-`date +"%Y-%m-%d-%H-%M"`.bak > /dev/null 2>&1
rm -f /etc/sysconfig/network-scripts/ifcfg-$selectnic
echo -e "\e[94mNIC $selectnic configuration is backed up!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
ls /etc/sysconfig/network-scripts/*.bak > /dev/null 2>&1
if [ $? -eq 0 ];then
cp -f /etc/sysconfig/network-scripts/*.bak /etc/sysconfig/network-scripts/nicsbak/
fi
rm -f /etc/sysconfig/network-scripts/*.bak
sleep 1
else
echo -e "\e[94mNIC $selectnic has been selected!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
sleep 1
fi
done
if [ -f "/tmp/teams" ];then
for num in $nicnum;
do
selectteamnic=`cat /tmp/nic |awk "NR==$num" |awk -F " " '{print$1}'`
selectnicinpresentteam=`cat /tmp/teams |grep $selectteamnic |awk -F " " '{print$2}'`
echo -e "\e[94mChosen NIC $selectteamnic\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
if [[ "$selectnicinpresentteam" =~ "team" ]];then
echo -e "\e[94mNIC $selectteamnic is already a member of $selectnicinpresentteam!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
read -p "Whether to continue? If you continue, $selectnicinpresentteam will be deleted! Press n to exit, press other keys to continue:" answer2
answer2lower=`echo $answer2 | tr [A-Z] [a-z]`
if [[ "$answer2lower" == "n" ]]; then
echo -e "\e[94mIf you need to bind, please re-execute the script and select another NICs!\e[0m"
exit
else
echo -e "\e[94mNIC $selectteamnic has been selected!\e[0m"
ip a | grep -E "^[0-9]" | grep -vE ": lo|: virbr" |awk -F ": <" '{print$1}' |awk -F ": " '{print$2}' > /tmp/nicnew
nicnewall=`cat /tmp/nicnew`
if [[ "$nicnewall" =~ "$selectnicinpresentteam" ]];then
cp -f /etc/sysconfig/network-scripts/ifcfg-$selectnicinpresentteam* /etc/sysconfig/network-scripts/nicsbak/
if [ "$oskernel" = "3.10.0" ]; then
systemctl start NetworkManager > /dev/null 2>&1
fi
if [ "$oskernel" = "4.18.0" ]; then
systemctl start NetworkManager > /dev/null 2>&1
fi
for disteam in `ls /etc/sysconfig/network-scripts/ifcfg-$selectnicinpresentteam* | xargs -i basename {\} |awk -F "ifcfg-" '{print$2}'`
do
cp -f /etc/sysconfig/network-scripts/ifcfg-$disteam /etc/sysconfig/network-scripts/ifcfg-$disteam-`date +"%Y-%m-%d-%H-%M"`.bak > /dev/null 2>&1
nmcli con delete $disteam > /dev/null 2>&1
done 
echo -e "\e[94mNIC $selectnicinpresentteam has been disabled!\e[0m"
echo -e "\e[94mNIC $selectnicinpresentteam configuration is backed up!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
fi
fi
cp -f /etc/sysconfig/network-scripts/ifcfg-$selectteamnic /etc/sysconfig/network-scripts/ifcfg-$selectteamnic-`date +"%Y-%m-%d-%H-%M"`.bak > /dev/null 2>&1
ls /etc/sysconfig/network-scripts/*.bak > /dev/null 2>&1
if [ $? -eq 0 ];then
cp -f /etc/sysconfig/network-scripts/*.bak /etc/sysconfig/network-scripts/nicsbak/
fi
rm -f /etc/sysconfig/network-scripts/*.bak
echo -e "\e[94mNIC $selectteamnic configuration is backed up!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
sleep 1
else
echo -e "\e[94mNIC $selectteamnic has been selected!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
if [ "$oskernel" = "3.10.0" ]; then
systemctl start NetworkManager > /dev/null 2>&1
fi
if [ "$oskernel" = "4.18.0" ]; then
systemctl start NetworkManager > /dev/null 2>&1
fi
sleep 1
fi
done
fi
if [[ $selectmode == 1 ]];then
systemctl stop NetworkManager > /dev/null 2>&1
systemctl disable NetworkManager > /dev/null 2>&1
while true
do
read -p "Please enter a bond name, such as bond0: " bondname1
bondname=`echo ${bondname1,,}`
for ifpresentbond in `cat /tmp/nicnew`
do
if [[ "$ifpresentbond" == "$bondname" ]];then
echo -e "\e[94m$bondname currently exists Please re-enter!\e[0m"
read -p "Please enter a bond name, such as bond0: " bondname
echo -e "\e[94mThe bond name entered is: $bondname\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
fi
done
if [ -z $bondname ];then
echo -e "\e[94mbond name cannot be empty, please re-enter!\e[0m"
else
echo -e "\e[94mThe bond name entered is: $bondname\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
fi
done
while true
do
read -p "Please enter $bondname IP address: " bondip
ipcalc -cs $bondip
if [ $? -ne 0  ];then
echo -e "\e[94mPlease enter a valid IP address!\e[0m"
else
echo -e "\e[94mThe entered IP address of $bondname is $bondip\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
fi
done
while true
do
read -p "Please enter $bondname IP netmask address or netmask prefix: " bondnetmask
if [[ -n "$bondnetmask" && "$bondnetmask" =~ "." ]];then
ipcalc -p $bondip $bondnetmask > /dev/null 2>&1
if [ $? -ne 0 ];then
echo -e "\e[94mPlease enter a valid netmask address!\e[0m"
else
bondmask=`ipcalc -p $bondip $bondnetmask |awk -F "=" '{print$2}'`
echo -e "\e[94mThe entered netmask prefix is $bondmask\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
fi
fi
if [ -z $bondnetmask ];then
echo -e "\e[94mPlease enter a valid netmask address!\e[0m"
fi
if [ -n "$bondnetmask" ];then
if [[ "$bondnetmask" -gt 1 && "$bondnetmask" -lt 32 ]] > /dev/null 2>&1;then 
echo -e "\e[94mThe entered netmask prefix is $bondmask\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
bondmask=$bondnetmask
break;
else 
echo -e "\e[94mPlease enter a valid netmask address!\e[0m"
fi 
fi
done
defaultgateway=`ip route |grep default |awk -F "via " '{print$2}' |awk -F " dev" '{print$1}' |head -n1`
if [ -n $defaultgateway ];then
echo
echo -e "\e[94mTip: A default gateway already exists in the current system:$defaultgateway,If the bond gateway is configured, it may cause network disconnection. If you need to configure it, please try to add a static route!\e[0m"
sleep 3
fi
while true
do
read -p "Please enter the $bondname gateway address, if there is no gateway, press Enter: " bondgateway
if [ -n "$bondgateway" ];then
ipcalc -cs $bondgateway
if [ $? -ne 0  ];then
echo -e "\e[94mPlease enter a valid gateway address!\e[0m"
else
echo -e "\e[94mThe gateway for $bondname entered is $bondgateway\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
gateway=$bondgateway
break;
fi
else
echo -e "\e[94mThe configured $bondname gateway is none!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
gateway=$bondgateway
break;
fi
done
while true
do
read -p "Please enter the first DNS address of $bondname, if there is no DNS, press Enter: " bonddns1
if [ -n "$bonddns1" ];then
ipcalc -cs $bonddns1
if [ $? -ne 0  ];then
echo -e "\e[94mPlease enter a valid DNS address!\e[0m"
else
echo -e "\e[94mThe first DNS address entered is $bonddns1\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
dns1=$bonddns1
break;
fi
else
echo -e "\e[94mThe configured $bondname DNS is none!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
fi
done
if [ -n "$dns1" ];then
while true
do
read -p "Please enter the second DNS address of $bondname, if there is no DNS, press Enter: " bonddns2
if [ -n "$bonddns2" ];then
ipcalc -cs $bonddns2
if [ $? -ne 0  ];then
echo -e "\e[94mPlease enter a valid DNS address!\e[0m"
else
echo -e "\e[94mThe second DNS address entered is $bonddns1\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
dns2=$bonddns2
break;
fi
else
echo -e "\e[94mThe configured $bondname DNS is none!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
fi
done
fi
while true
do
echo -e "\e[94m##############################################################\e[0m"
echo -e "\e[94m#         0,Round-robin policy                              #\e[0m"
echo -e "\e[94m#         1,Active-backup policy                            #\e[0m"
echo -e "\e[94m#         2,XOR policy                                      #\e[0m"
echo -e "\e[94m#         3,Broadcast policy                                #\e[0m"
echo -e "\e[94m#         4,802.3ad Dynamic link aggregation(LACP)          #\e[0m"
echo -e "\e[94m#         5,Adaptive transmit load balancing policy         #\e[0m"
echo -e "\e[94m#         6,Adaptive load balancing policy                  #\e[0m"
echo -e "\e[94m##############################################################\e[0m"
read -p "Please select the binding mode and enter the number: " bondmode
if [ "$bondmode" == 0 ];then
echo -e "\e[94mThe selected bond mode is (balance-rr0)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
elif [ "$bondmode" == 1 ];then
echo -e "\e[94mThe selected bond mode is (active-backup)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
elif [ "$bondmode" == 2 ];then
echo -e "\e[94mThe selected bond mode is (balance-xor)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
elif [ "$bondmode" == 3 ];then
echo -e "\e[94mThe selected bond mode is (broadcast)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
elif [ "$bondmode" == 4 ];then
echo -e "\e[94mThe selected bond mode is (LACP)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
elif [ "$bondmode" == 5 ];then
echo -e "\e[94mThe selected bond mode is (balance-tlb)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
elif [ "$bondmode" == 6 ];then
echo -e "\e[94mThe selected bond mode is (balance-alb)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
else
echo -e "\e[94mPlease select the correct bond mode!\e[0m"
fi
done
for num in $nicnum;
do
selectnic=`cat /tmp/nic |awk "NR==$num" |awk -F " " '{print$1}'`
cat > /etc/sysconfig/network-scripts/ifcfg-$selectnic << EOF
DEVICE=$selectnic
BOOTPROTO=none
ONBOOT=yes
MASTER=$bondname
SLAVE=yes
USERCTL=no
NM_CONTROLLED=no
EOF
echo -e "\e[94m$bondname member $selectnic has been configured\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
sleep 1
done
cat > /etc/sysconfig/network-scripts/ifcfg-$bondname << EOF  
DEVICE=$bondname
ONBOOT=yes
BOOTPROTO=static
USERCTL=no
NM_CONTROLLED=no
IPADDR=$bondip
PREFIX=$bondmask
EOF
if [ -n "$gateway" ];then
cat >> /etc/sysconfig/network-scripts/ifcfg-$bondname << EOF
GATEWAY=$gateway
EOF
fi
if [ -n "$dns1" ];then
cat >> /etc/sysconfig/network-scripts/ifcfg-$bondname << EOF
DNS1=$dns1
EOF
fi
if [ -n "$dns2" ];then
cat >> /etc/sysconfig/network-scripts/ifcfg-$bondname << EOF
DNS2=$dns2
EOF
fi
if [ "$bondmode" == 4 ];then 
cat >> /etc/sysconfig/network-scripts/ifcfg-$bondname << EOF
BONDING_OPTS="mode=4 miimon=100 xmit_hash_policy=layer2+3"
EOF
else
cat >> /etc/sysconfig/network-scripts/ifcfg-$bondname << EOF
BONDING_OPTS="mode=$bondmode miimon=100"
EOF
fi
if [ "$oskernel" = "2.6.32" ]; then
service network restart > /dev/null 2>&1
elif [ "$oskernel" = "3.10.0" ]; then
systemctl restart network > /dev/null 2>&1
elif [ "$oskernel" = "4.18.0" ]; then
systemctl restart network > /dev/null 2>&1
fi
echo -e "\e[94m$bondname information\e[0m"
ip a s $bondname
bondinfo=`cat /proc/net/bonding/$bondname |grep Mode |awk -F ": " '{print$2}'`
echo -e "\e[94mThe configured $bondname mode is $bondinfo\e[0m"
echo
cat /proc/net/bonding/$bondname
ls /etc/sysconfig/network-scripts/ifcfg-bond0 > /dev/null 2>&1
if [ $? -ne 0 ];then
for niclist in `ip a | grep -E "^[0-9]" | grep -vE ": lo|: virbr" |awk -F ": <" '{print$1}' |awk -F ": " '{print$2}'`;
do
if [[ "$niclist" == "bond0" && "$bondname" != "bond0" ]];then
echo -bond0 > /sys/class/net/bonding_masters
fi
done
fi
rm -f /tmp/nic
rm -f /tmp/nicnew
rm -f /tmp/presentbondall
rm -f /tmp/nicall
rm -f /tmp/teams
echo -e "\e[94m--------------------------------------------------------------\e[0m"
echo -e "\e[94mNIC aggregation $bondname configuration is completed!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
fi
if [[ $selectmode == 2 ]];then
systemctl start NetworkManager > /dev/null 2>&1
systemctl enable NetworkManager > /dev/null 2>&1
while true
do
read -p "Please enter a team name, such as team0: " teamname1
teamname=`echo ${teamname1,,}`
for ifpresentteam in `cat /tmp/nicnew`
do
if [[ "$ifpresentteam" == "$teamname" ]];then
echo -e "\e[94m$teamname currently exists Please re-enter!\e[0m"
read -p "Please enter a team name, such as team0: " teamname
echo -e "\e[94mThe team name entered is: $teamname\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
fi
done
if [ -z $teamname ];then
echo -e "\e[94mteamname cannot be empty, please re-enter!\e[0m"
else
echo -e "\e[94mThe teamname entered is: $teamname\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
fi
done
while true
do
read -p "Please enter $teamname IP address: " teamip
ipcalc -cs $teamip
if [ $? -ne 0  ];then
echo -e "\e[94mPlease enter a valid IP address!\e[0m"
else
echo -e "\e[94mThe entered IP address of $teamname is $teamip \e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
fi
done
while true
do
read -p "Please enter $teamname netmask or netmask prefix: " teamnetmask
if [[ -n "$teamnetmask" && "$teamnetmask" =~ "." ]];then
ipcalc -c $teamip $teamnetmask > /dev/null 2>&1
if [ $? -ne 0 ];then
echo -e "\e[94mPlease enter a valid netmask address!\e[0m"
else
teammask=`ipcalc -p $teamip $teamnetmask |awk -F "=" '{print$2}'`
echo -e "\e[94mThe entered netmask prefix is $teammask\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
fi
fi
if [ -z $teamnetmask ];then
echo -e "\e[94mPlease enter a valid netmask address!\e[0m"
fi
if [ -n "$teamnetmask" ];then
if [[ "$teamnetmask" -gt 1 && "$teamnetmask" -lt 32 ]] > /dev/null 2>&1;then 
echo -e "\e[94mThe entered netmask prefix is $teamnetmask\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
teammask=$teamnetmask
break;
else 
echo -e "\e[94mPlease enter a valid netmask address!\e[0m"
fi 
fi
done
defaultgateway=`ip route |grep default |awk -F "via " '{print$2}' |awk -F " dev" '{print$1}' |head -n1`
if [ -n $defaultgateway ];then
echo
echo -e "\e[94mTip: A default gateway already exists in the current system:$defaultgateway,If the team gateway is configured, it may cause network disconnection. If you need to configure it, please try to add a static route!\e[0m"
sleep 3
fi
while true
do
read -p "Please enter the $teamname gateway address, if there is no gateway, press Enter: " teamgateway
if [ -n "$teamgateway" ];then
ipcalc -cs $teamgateway
if [ $? -ne 0  ];then
echo -e "\e[94mPlease enter a valid gateway address!\e[0m"
else
echo -e "\e[94mThe gateway for $teamname entered is $teamgateway\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
gateway=$teamgateway
break;
fi
else
echo -e "\e[94mThe configured $teamname gateway is none!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
gateway=$teamgateway
break;
fi
done
while true
do
read -p "Please enter the first DNS address of $teamname, if there is no DNS, press Enter: " teamdns1
if [ -n "$teamdns1" ];then
ipcalc -cs $teamdns1
if [ $? -ne 0  ];then
echo -e "\e[94mPlease enter a valid DNS address!\e[0m"
else
echo -e "\e[94mThe first DNS address entered is $teamdns1\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
dns1=$teamdns1
break;
fi
else
echo -e "\e[94mThe configured $bondname DNS is none!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
fi
done
if [ -n "$dns1" ];then
while true
do
read -p "Please enter the second DNS address of $teamname, if there is no DNS, press Enter: " teamdns2
if [ -n "$teamdns2" ];then
ipcalc -cs $teamdns2
if [ $? -ne 0  ];then
echo -e "\e[94mPlease enter a valid DNS address!\e[0m"
else
echo -e "\e[94mThe second DNS address entered is $teamdns2 \e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
dns2=$teamdns2
break;
fi
else
echo -e "\e[94mThe configured $teamname DNS is none!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
break;
fi
done
fi
while true
do
echo -e "\e[94m##############################################################\e[0m"
echo -e "\e[94m#                    1,broadcast policy                     #\e[0m"
echo -e "\e[94m#                    2,roundrobin policy                    #\e[0m"
echo -e "\e[94m#                    3,activebackup policy                  #\e[0m"
echo -e "\e[94m#                    4,loadbalance policy                   #\e[0m"
echo -e "\e[94m#                    5,lacp policy                          #\e[0m"
echo -e "\e[94m##############################################################\e[0m"
read -p "Please select the binding mode and enter the number: " teammode
if [ "$teammode" == 1 ];then
echo -e "\e[94mThe selected team mode is (broadcast)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
teammode=broadcast
break;
elif [ "$teammode" == 2 ];then
echo -e "\e[94mThe selected team mode is (roundrobin)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
teammode=roundrobin
break;
elif [ "$teammode" == 3 ];then
echo -e "\e[94mThe selected team mode is (activebackup)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
teammode=activebackup
break;
elif [ "$teammode" == 4 ];then
echo -e "\e[94mThe selected team mode is (loadbalance)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
teammode=loadbalance
break;
elif [ "$teammode" == 5 ];then
echo -e "\e[94mThe selected team mode is (lacp)\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
teammode=lacp
break;
else
echo -e "\e[94mPlease select the correct team mode!\e[0m"
fi
done
nmcli con add con-name $teamname type team ifname $teamname config '{"runner":{"name":"'$teammode'"}}' > /dev/null 2>&1
if [[ -n "$gateway" && -n "dns1" ]];then
nmcli con mod $teamname ipv4.addresses $teamip/$teammask ipv4.gateway $gateway ipv4.dns $dns1 ipv4.method manual connectio.autoconnect yes > /dev/null 2>&1
fi
if [[ -n "$gateway" && -n "dns1" && -n "dns2" ]];then
nmcli con mod $teamname ipv4.addresses $teamip/$teammask ipv4.gateway $gateway ipv4.dns $dns1,$dns2 ipv4.method manual connectio.autoconnect yes > /dev/null 2>&1
fi
if [[ -n "$gateway" ]];then
nmcli con mod $teamname ipv4.addresses $teamip/$teammask ipv4.gateway $gateway ipv4.method manual connectio.autoconnect yes > /dev/null 2>&1
fi
if [[ -z "$gateway" ]];then
nmcli con mod $teamname ipv4.addresses $teamip/$teammask ipv4.method manual connectio.autoconnect yes > /dev/null 2>&1
fi
if [[ -z "$gateway" && -n "dns1" ]];then
nmcli con mod $teamname ipv4.addresses $teamip/$teammask ipv4.dns $dns1 ipv4.method manual connectio.autoconnect yes > /dev/null 2>&1
fi
if [[ -z "$gateway" && -n "dns1" && -n "dns2" ]];then
nmcli con mod $teamname ipv4.addresses $teamip/$teammask ipv4.dns $dns1,$dns2 ipv4.method manual connectio.autoconnect yes > /dev/null 2>&1
fi
for num in $nicnum;
do
selectnic=`cat /tmp/nic |awk "NR==$num" |awk -F " " '{print$1}'`
nmcli con down $selectnic > /dev/null 2>&1
nmcli con add type team-slave con-name $teamname-port-$selectnic ifname $selectnic master $teamname > /dev/null 2>&1
done
nmcli con reload > /dev/null 2>&1
nmcli con up $teamname > /dev/null 2>&1
teamdctl $teamname state
rm -f /tmp/nic
rm -f /tmp/nicnew
rm -f /tmp/presentbondall
rm -f /tmp/nicall
rm -f /tmp/teams
systemctl restart network > /dev/null 2>&1
echo -e "\e[94mNIC aggregation $teamname configuration is completed!\e[0m"
echo -e "\e[94m--------------------------------------------------------------\e[0m"
fi
fi

#!/bin/bash
sttya=`tty`
if [[ "$sttya" =~ "pts" ]];then
stty erase ^?
fi
if [ $UID -ne 0 ]; then
echo "当前用户不是root用户,请以root用户身份执行此脚本!"
echo "The current user is not root,please execute this script as the root user!"
exit
fi
multipah -l > /dev/null 2>&1
if [ $? == 0 ];then 
ifuse=`multipath -F`
if [[ "$ifuse" =~ "use" ]];then
echo -e "\e[94m当前主机已有存储正在使用!请先卸载存储卷!\e[0m"
echo -e "\e[94mCurrent host already has storage in use!Please unmount the storage volume first!\e[0m"
exit
fi
fi
echo -e "\e[94m###################################################################\e[0m"
echo -e "\e[94m#                               1.中文                            #\e[0m"
echo -e "\e[94m#                               2.English                         #\e[0m"
echo -e "\e[94m###################################################################\e[0m"
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
echo -e "\e[94m请选择运行脚本所用的语言: \e[0m"
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
echo -e "\e[94mPlease select a language to run the script: \e[0m"
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
while true
do
read -p "Enter the number: " language
if [[ $language == 1 ]];then
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
echo -e "\e[94m已选择中文!\e[0m"
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
language=1
export LANG="zh_CN.UTF-8"
sleep 1
break;
elif [[ $language == 2 ]];then
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
echo -e "\e[94mEnglish selected!\e[0m"
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
language=2
export LANG="en_US.UTF-8"
sleep 1
break;
else
echo -e "\e[94m请选择语言!\e[0m"
echo -e "\e[94mPlease select a language!\e[0m"
fi
done 
if [ $language == 1 ];then
echo -e "\e[94m###################################################################\e[0m"
echo -e "\e[94m#        1.多路径辅助配置脚本适用于FC-SAN和IP-SAN                 #\e[0m"
echo -e "\e[94m#        2.支持联想 DM、DE系列、DS系列、Storwize V系列            #\e[0m"
echo -e "\e[94m#        3.支持NetApp FAS、E系列,IBM Storwize V系列、FS系列      #\e[0m"
echo -e "\e[94m#        4.支持华为OceanStor系列,浪潮G2系列                      #\e[0m"
echo -e "\e[94m#        5.支持戴尔 MD系列、SC系列,宏杉MS系列                    #\e[0m"
echo -e "\e[94m#        6.支持惠普 3PAR系列、MSA 1040、1050、2040、2050系列      #\e[0m"
echo -e "\e[94m#        7.其他品牌和型号,使用操作系统生成的默认多路径参数       #\e[0m"
echo -e "\e[94m#        8.支持CentOS 6.x/7.x/8.x RedHat 6.x/7.x/8.x              #\e[0m"
echo -e "\e[94m#        9.作者:猫先生                                           #\e[0m"
echo -e "\e[94m#        10.编写时间:2020年4月16日                               #\e[0m"
echo -e "\e[94m#        11.更多内容访问 https://www.mr-mao.cn                    #\e[0m"
echo -e "\e[94m###################################################################\e[0m"
echo -e "\033[1;m提示:如按Backspace键无法正常删除内容时,可按 Ctrl+Backspace\e[0m"
sleep 1
oskernel=`uname -r | awk -F"-" '{print $1}'`
osrealease=`cat /etc/redhat-release`
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
echo -e "\e[94m当前操作系统为$osrealease\e[0m"
if [ "$oskernel" = "2.6.32" ]; then
echo -e "\e[94m内核版本为2.6.32系列\e[0m"
elif [ "$oskernel" = "3.10.0" ]; then
echo -e "\e[94m内核版本为3.10.0系列\e[0m"
elif [ "$oskernel" = "4.18.0" ]; then
echo -e "\e[94m内核版本为4.18.0系列\e[0m"
echo -e "\033[1;m因存储厂家目前未做适配,当前系统默认使用系统生成的多路径参数!\e[0m"
else
echo -e "\e[94m此脚本不支持当前操作系统!\e[0m"
exit
fi
multipathpack=`rpm -qa | grep device-mapper-multipath |wc -l`
if [ "$multipathpack" -ge 2 ]; 
then
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
echo -e "\e[94m操作系统多路径软件已安装\e[0m"
elif [ "$multipathpack" -lt 2 ]; 
then
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
echo -e "\e[94m系统多路径软件安装不完整,如yum正常,执行下面的命令安装\e[0m"
echo yum install -y device-mapper-multipath
echo -e "\e[94m如果yum未配置,则进入系统盘/Packages文件夹下执行\e[0m"
echo rpm -ivh device-mapper-multipath-*.rpm
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
exit
else
exit
fi
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
initpack=`rpm -qa |grep iscsi-initiator-utils |wc -l`
lspci > /dev/null 2>&1
if [[ $? == 0 ]];then
hba=`lspci |grep -i fibre`
fi
oslabel=`df -h |grep boot |head -n1 |awk -F " " '{print$1}' |grep [a-z]d[a-z] -o`
echo -e "\e[94m###################################################################\e[0m"
echo -e "\e[94m#                            1.FC                                 #\e[0m"
echo -e "\e[94m#                            2.ISCSI                              #\e[0m"
echo -e "\e[94m###################################################################\e[0m"
while true
do
read -p "选择存储连接方式,输入数字即可: " mode
if [ "$mode" == 1 ];then
echo -e "\e[94m当前选择的连接方式为:FC\e[0m"
mode=1
break;
elif [ "$mode" == 2 ]; then
echo -e "\e[94m当前选择的连接方式为:ISCSI\e[0m"
mode=2
break;
else
echo -e "\e[94m选择正确的连接方式!\e[0m"
fi
done
if [ "$mode" == 1 ]; then
if [ -z "$hba" ]; then
lspci > /dev/null 2>&1
if [[ $? != 0 ]];then
echo -e "\e[94m当前操作系统缺少查看硬件的组件,请自行确定HBA卡是否已识别
或输入下面的命令安装。\e[0m"
echo -e "\e[94m如yum正常,则可以执行下面的命令安装\e[0m"
echo yum -y install pciutils
echo -e "\e[94m如果未配置yum,则进入系统盘/Packages文件夹下执行\e[0m"
echo rpm -ivh pciutils-*.rpm
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
fi
echo -e "\e[94m未检测到HBA卡,请检查驱动是否安装合适!\e[0m"
exit
fi
echo -e "\e[94m已检测到当前系统盘为 $oslabel \e[0m"
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
sleep 1
echo -e "\e[94m当前主机HBA卡的WWPN如下:\e[0m"
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
cat /sys/class/fc_host/host*/port_name |awk -F "0x" '{print $2}'
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
sleep 1
fi
if [ "$mode" == 2 ]; then
if [ "$initpack" == 0 ]; then
echo -e "\e[94m当前系统未安装iSCSI,请先安装!\e[0m"
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
echo -e "\e[94m如yum正常,则可以执行下面的命令安装\e[0m"
echo yum -y install iscsi-initiator-utils 
echo -e "\e[94m如果未配置yum,则进入系统盘/Packages文件夹下执行\e[0m"
echo rpm -ivh iscsi-initiator-utils-*.rpm
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
exit
fi
echo -e "\e[94m当前主机的iSCSI发起名称是:\e[0m"
echo
iscsiadm -m node --logoutall=all > /dev/null 2>&1
cat /etc/iscsi/initiatorname.iscsi |awk -F "=" '{print$2}'
echo
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
sleep 1
ip a | grep -E "^[0-9]" | grep -vE ": lo|: virbr" |awk -F ": <" '{print$1}' |awk -F ": " '{print$2}' > /tmp/nic
echo -e "\e[94m当前主机所有网卡如下:\e[0m"
echo -e "\e[94m-------------------------\e[0m"
cat /tmp/nic |awk '{print NR,$1}'
echo -e "\e[94m-------------------------\e[0m"
while true
do
read -p "选择用来连接存储的网卡,输入数字即可,如使用多个网卡,用空格隔开: " nicnum
selectnum=`echo $nicnum |awk -F " " '{print NF}'`
if [ $selectnum -lt 1 ];then
echo -e "\e[94m最少选择1块网卡,请重新选择!\e[0m"
else
break;
fi
done
for num in $nicnum;
do
nicx=`cat /tmp/nic |awk "NR==$num" |awk -F " " '{print$1}'`
if [[ "$nicx" == "" ]];then
echo -e "\e[94m选择正确的网卡!\e[0m"
exit
fi
done
for num in $nicnum;
do
selectnic=`cat /tmp/nic |awk "NR==$num"`
selectnicip=`ip a s $selectnic | grep -o '[0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+' | head -n1`
if [ -z "$selectnicip" ];then
echo -e "\e[94m当前网卡 $selectnic 无IP地址,请先配置!\e[0m"
exit
else
echo -e "\e[94m已选择 $selectnic\e[0m"
echo -e "\e[94m-------------------------\e[0m"
echo -e "\e[94m网卡 $selectnic IP地址:
$selectnicip\e[0m"
echo -e "\e[94m-------------------------\e[0m"
sleep 1
fi
done
read -p "请输入存储target IP地址,如有多个IP,使用空格隔开: " targetip
for tip in $targetip;
do
echo -e "\e[94m正在检查网络连通性,请稍候!\e[0m"
ping=`ping -c 5 $tip |awk 'NR==9 {print $4}'`
if [[ $ping -eq 0 ]];then
echo -e "\e[94m当前主机无法ping通存储 $tip,请检查网络配置!\e[0m"
exit
else 
echo -e "\e[94m当前主机与存储 $tip 通信正常,正在配置iSCSI连接。。。\e[0m"
iscsiadm -m discovery -t st -p $targetip > /dev/null 2>&1
if [[ "$?" == 0 ]]; then
iscsiadm -m node -p $tip -l > /dev/null 2>&1
echo -e "\e[94miSCSI已连接,请在存储端映射卷。\e[0m"
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
sleep 1
else 
echo -e "\e[94m当前主机无法登录iSCSI存储!请检查配置!\e[0m"
exit
fi
fi
done
fi
read -p "存储端是否完成映射?按n退出,按其他按键表示已映射:" answer1
answer1lower=`echo $answer1 | tr [A-Z] [a-z]`
if [ "$answer1lower" == "n" ]; then
echo -e "\e[94m请在存储端完成映射后,重新执行此脚本!\e[0m"
else 
sleep 1
fi
if [[ -z "$oslabel" ]]; then
while true
do
read -p "输入操作系统所在磁盘,比如 sda: " osdisk
if [[ -z "$osdisk" ]]; then
echo -e "\e[94m系统盘不能为空!\e[0m"
else
echo -e "\e[94m当前系统所在磁盘为 $osdisk\e[0m"
break;
sleep 1
fi
done
else
osdisk=$oslabel
fi
if [ "$oskernel" = "2.6.32" ]; then
wwid=`/sbin/scsi_id -g -u /dev/$osdisk`
fi
if [[ "$oskernel" = "3.10.0" ]]; then
wwid=`/usr/lib/udev/scsi_id -g -u /dev/$osdisk`
fi
if [[ "$oskernel" = "4.18.0" ]]; then
wwid=`/usr/lib/udev/scsi_id -g -u /dev/$osdisk`
fi
sleep 1
if [ "$mode" == 1 ]; then
for x in `ls /sys/class/fc_host`; do echo "- - -" > /sys/class/scsi_host/$x/scan; done
fi
if [ "$mode" == 2 ]; then
for x in `ls /sys/class/iscsi_host`; do echo "- - -" > /sys/class/scsi_host/$x/scan; done
fi
echo -e "\e[94m正在检测存储.....\e[0m"
sleep 1
rm -f /etc/multipath.conf
if [ "$oskernel" = "2.6.32" ]; then
mpathconf --enable --find_multipaths y --user_friendly_names y --with_module y --with_chkconfig y
sleep 1
service multipathd restart > /dev/null 2>&1
sleep 3
multipath -F
multipath -v2 > /dev/null 2>&1
multipath -l
fi
if [[ "$oskernel" = "3.10.0" ]]; then
mpathconf --enable --find_multipaths y --with_module y --with_multipathd y --user_friendly_names y
sleep 1
systemctl restart multipathd > /dev/null 2>&1
sleep 3
multipath -F
multipath -v2 > /dev/null 2>&1
multipath -l
fi
if [[ "$oskernel" = "4.18.0" ]]; then
mpathconf --enable --find_multipaths y --with_module y --with_multipathd y --user_friendly_names y
sleep 1
systemctl restart multipathd > /dev/null 2>&1
sleep 3
multipath -F
multipath -v2 > /dev/null 2>&1
multipath -l
fi
dev=`multipath -l`
if [[ -z "$dev" ]]; then
echo -e "\e[94m未检测到存储!请检查是否已映射或链路是否为多链路!\e[0m"
exit
fi
echo
if [ "$wwid" == "" ]; then
echo -e "\e[94m未检测到系统盘 $osdisk 的wwid,当前主机可能是虚拟机,已在多路径配置文件中使用 $osdisk 做为黑名单设备\e[0m"
backlist=$osdisk
cat > /tmp/multipath.conf << EOF
blacklist{
devnode "$backlist"
}
defaults {
     user_friendly_names yes
     find_multipaths yes
}
multipaths {
EOF
else 
backlist=$wwid
echo -e "\e[94m已检测到系统盘WWID为 $wwid,已在多路径配置文件中使用wwid为黑名单设备\e[0m"
echo
cat > /tmp/multipath.conf << EOF
blacklist{
wwid "$backlist"
}
defaults {
     user_friendly_names yes
     find_multipaths yes
}
multipaths {
EOF
fi
multipath=`multipath -l |grep dm- |grep [0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z] -o`
if [[ "$multipath" == "" ]];then
echo -e "\e[94m此脚本不支持当前存储!\e[0m"
exit
fi
for dev in $multipath;
do
size=`multipath -l $dev |grep size= |awk -F " " '{print$1}'`
dm=`multipath -l |grep $dev |grep dm-"[0-9]\{1,3\}" -o`
vendor=`multipath -l |grep $dev |awk -F "$dm" '{print$2}' |sed s/[[:space:]]//g`
multipath -l |grep $dev |awk -F "$dm" '{print$2}' |sed s/[[:space:]]//g > /tmp/vendor
multipath -l |grep $dev |awk -F "$dm" '{print$2}' |sed s/[[:space:]]//g >> /tmp/vendors
if [[ "$vendor" =~ "NETAPP,LUNC-Mode" ]]; then
echo -e "\e[94m已检测到NETAPP品牌FAS系列存储\e[0m"
elif [[ "$vendor" =~ "LENOVO,DE_Series" ]]; then
echo -e "\e[94m已检测到联想品牌DE系列存储\e[0m"
elif [[ "$vendor" =~ "NETAPP,INF-01-00" ]]; then
echo -e "\e[94m已检测到NETAPP品牌E系列存储\e[0m"
elif [[ "$vendor" =~ "LSI,INF-01-00" ]]; then
echo -e "\e[94m已检测到NETAPP品牌E系列存储\e[0m"
elif [[ "$vendor" =~ "Lenovo,DS2200" ]]; then
echo -e "\e[94m已检测到联想品牌DS2200存储\e[0m"
elif [[ "$vendor" =~ "Lenovo,DS4200" ]]; then
echo -e "\e[94m已检测到联想品牌DS4200存储\e[0m"
elif [[ "$vendor" =~ "Lenovo,DS6200" ]]; then
echo -e "\e[94m已检测到联想品牌DS6200存储\e[0m"
elif [[ "$vendor" =~ "IBM,2145" ]]; then
echo -e "\e[94m已检测到IBM品牌V或者FS系列存储\e[0m"
elif [[ "$vendor" =~ "HUAWEI,XSG1" ]]; then
echo -e "\e[94m已检测到华为品牌OceanStor系列存储\e[0m"
elif [[ "$vendor" =~ "INSPUR,MCS" ]]; then
echo -e "\e[94m已检测到浪潮品牌G2系列存储\e[0m"
elif [[ "$vendor" =~ "COMPELNT,CompellentVol" ]]; then
echo -e "\e[94m已检测到戴尔品牌SC系列存储\e[0m"
elif [[ "$vendor" =~ "DELL,MD34xx" ]]; then
echo -e "\e[94m已检测到戴尔品牌MD34系列存储\e[0m"
elif [[ "$vendor" =~ "DELL,MD32xx" ]]; then
echo -e "\e[94m已检测到戴尔品牌MD32系列存储\e[0m"
elif [[ "$vendor" =~ "DELL,MD36xx" ]]; then
echo -e "\e[94m已检测到戴尔品牌MD36系列存储\e[0m"
elif [[ "$vendor" =~ "DELL,MD38xx" ]]; then
echo -e "\e[94m已检测到戴尔品牌MD38系列存储\e[0m"
elif [[ "$vendor" =~ "HP,MSA1040SAN" ]]; then
echo -e "\e[94m已检测到惠普品牌MSA 1040系列存储\e[0m"
elif [[ "$vendor" =~ "HP,MSA1050SAN" ]]; then
echo -e "\e[94m已检测到惠普品牌MSA 1050系列存储\e[0m"
elif [[ "$vendor" =~ "HP,MSA2040SAN" ]]; then
echo -e "\e[94m已检测到惠普品牌MSA 2040系列存储\e[0m"
elif [[ "$vendor" =~ "HP,MSA2050SAN" ]]; then
echo -e "\e[94m已检测到惠普品牌MSA 2050系列存储\e[0m"
elif [[ "$vendor" =~ "MacroSAN,LU" ]]; then
echo -e "\e[94m已检测到宏杉品牌MS系列存储\e[0m"
elif [[ "$vendor" =~ "3PARdata,VV" ]]; then
echo -e "\e[94m已检测到惠普品牌3PAR系列存储\e[0m"
elif [[ "$vendor" =~ "UniversalXport" ]]; then
echo -e "\e[94m已检测到存储带内管理分区\e[0m"
else
echo -e "\e[94m已检测到$vendor存储,当前存储将使用操作系统默认多路径参数!\e[0m"
fi
echo  WWID为 $dev
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
while true
do
if [[ "$vendor" =~ "UniversalXport" ]]; then
echo -e "\e[94m此分区为存储厂商实现带内管理而自动分配,已将其屏蔽!\e[0m"
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
break;
else
echo -e "\e[94m为 [ wwid为 $dev $size DM为 $dm ]  的存储卷定义别名\e[0m"
read -p "输入存储卷的别名: " dmname1
dmname=`echo ${dmname1,,}`
if [ "$dmname" == "" ]; then
echo -e "\e[94m别名不能为空,请重新输入!\e[0m"
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
else
echo -e "\e[94m已配置别名为 $dmname \e[0m"
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
break;
fi
fi
done
if [ "$dmname" != "" ];then
cat >> /tmp/multipath.conf  << EOF
        multipath {
        wwid                  $dev
        alias                 $dmname
       }
EOF
fi
done
sleep 1
echo > /tmp/vendor
if [[ "$oskernel" != "4.18.0" ]]; then
cat >> /tmp/multipath.conf << EOF
}
devices{
EOF
echo -e "\e[94m根据存储品牌,正在写入当前存储厂商推荐的多路径参数.................\e[0m"
for dev in $multipath;
do
dm=`multipath -l |grep $dev |grep dm-"[0-9]\{1,3\}" -o`
vendor1=`multipath -l |grep $dev |awk -F "$dm" '{print$2}' |sed s/[[:space:]]//g`
vendor2=`cat /tmp/vendor`
if [[ "$vendor1" == "$vendor2" ]];then
echo .
else
vendor=$vendor1
multipath -l |grep $dev |awk -F "$dm" '{print$2}' |sed s/[[:space:]]//g > /tmp/vendor
if [[ "$vendor" =~ "NETAPP,LUNC-Mode" ]]; then
cat >> /tmp/multipath.conf << EOF
device {
        vendor                  "NETAPP"
        product                 "LUN"
        flush_on_last_del       yes
        dev_loss_tmo            infinity
        fast_io_fail_tmo        5
        failback                immediate
        features                "3 queue_if_no_path pg_init_retries 50"
        hardware_handler        "1 alua"
        path_checker            tur
        path_grouping_policy    group_by_prio
        path_selector           "round-robin 0"
        prio                    alua
        rr_min_io               1000
        rr_weight               uniform
           }
EOF
elif [[ "$vendor" =~ "NETAPP,INF-01-00" ]]; then
cat >> /tmp/multipath.conf << EOF
device {
        vendor                  "NETAPP"
        product                 "INF-01-00"
        path_grouping_policy    "group_by_prio"
        features                "2 pg_init_retries 50"
        path_checker            "rdac"
        path_selector           "round-robin 0"
        hardware_handler        "1 rdac"
        prio                    "rdac"
        failback                "immediate"
        no_path_retry           30
        detect_prio             yes
        rr_weight               priorities
        }
EOF
elif [[ "$vendor" =~ "LSI,INF-01-00" ]]; then
cat >> /tmp/multipath.conf << EOF
device {
        vendor                  "LSI"
        product                 "INF-01-00"
        path_grouping_policy    "group_by_prio"
        features                "2 pg_init_retries 50"
        path_checker            "rdac"
        path_selector           "round-robin 0"
        hardware_handler        "1 rdac"
        prio                    "rdac"
        failback                "immediate"
        no_path_retry           30
        detect_prio             yes
        rr_weight               priorities
        }
EOF
elif [[ "$vendor" =~ "LENOVO,DE_Series" ]]; then
cat >> /tmp/multipath.conf << EOF
device {
        vendor                  "LENOVO"
        product                 "DE_Series"
        path_grouping_policy    "group_by_prio"
        features                "2 pg_init_retries 50"
        path_checker            "rdac"
        path_selector           "round-robin 0"
        hardware_handler        "1 rdac"
        prio                    "rdac"
        failback                "immediate"
        no_path_retry           30
        detect_prio             yes
        rr_weight               priorities
        }
EOF
elif [[ "$vendor" =~ "Lenovo,DS2200" ]]; then
cat >> /tmp/multipath.conf << EOF
device {
        vendor          "Lenovo" 
        product         "DS2200" 
        failback        immediate    
        prio            alua 
        } 
EOF
elif [[ "$vendor" =~ "Lenovo,DS4200" ]]; then
cat >> /tmp/multipath.conf << EOF
device {
        vendor          "Lenovo" 
        product         "DS4200" 
        failback        immediate    
        prio            alua 
        } 
EOF
elif [[ "$vendor" =~ "Lenovo,DS6200" ]]; then
cat >> /tmp/multipath.conf << EOF
device {
        vendor          "Lenovo" 
        product         "DS6200" 
        failback        immediate    
        prio            alua 
        } 
EOF
elif [[ "$vendor" =~ "IBM,2145" ]]; then
cat >> /tmp/multipath.conf << EOF
device {
       vendor                  "IBM"
       product                 "2145"
       path_grouping_policy    "group_by_prio"
       path_selector           "service-time 0" 
       prio                    "alua"
       path_checker            "tur"
       failback                "immediate"
       no_path_retry           5
       rr_weight               uniform
       rr_min_io_rq            "1"
       dev_loss_tmo            120
       } 
EOF
elif [[ "$vendor" =~ "HUAWEI,XSG1" ]]; then
cat >> /tmp/multipath.conf << EOF
device {
       vendor                  "HUAWEI"
       product                 "XSG1"
       path_grouping_policy    "group_by_prio"
       path_checker            "tur"
       path_selector           "round-robin 0"
       prio                    alua
       failback                "immediate"
       dev_loss_tmo            30
       fast_io_fail_tmo        5
    }
EOF
elif [[ "$vendor" =~ "INSPUR,MCS" ]]; then
cat >> /tmp/multipath.conf << EOF
device {
        vendor                  "INSPUR"
        product                 "MCS"
        path_grouping_policy    group_by_prio
        path_selector           "service-time 0"
        features                "1 queue_if_no_path" 
        path_checker            tur
        prio                    alua 
        failback                immediate 
    } 
EOF
elif [[ "$vendor" =~ "COMPELNT,CompellentVol" ]]; then
cat >> /tmp/multipath.conf << EOF
device {
        vendor                  "COMPELNT" 
        product                 "Compellent Vol" 
        features                0 
        no_path_retry           fail 
    } 
EOF
elif [[ "$vendor" =~ "DELL,MD34xx" ]]; then
cat >> /tmp/multipath.conf << EOF
device {
        vendor                  "DELL"
        product                 "MD34xx"
        path_grouping_policy    "group_by_prio"
        features                "2 pg_init_retries 50"
        path_checker            "rdac"
        path_selector           "round-robin 0"
        hardware_handler        "1 rdac"
        prio                    "rdac"
        failback                "immediate"
        no_path_retry           30
        detect_prio             yes
        rr_weight               priorities
        }
EOF
elif [[ "$vendor" =~ "DELL,MD32xxi" ]]; then
cat >> /tmp/multipath.conf << EOF
device {
        vendor                  "DELL"
        product                 "MD32xxi"
        path_grouping_policy    "group_by_prio"
        features                "2 pg_init_retries 50"
        path_checker            "rdac"
        path_selector           "round-robin 0"
        hardware_handler        "1 rdac"
        prio                    "rdac"
        polling_interval        5
        failback                "immediate"
        no_path_retry           30
        detect_prio             yes
        rr_weight               priorities
        }
EOF
elif [[ "$vendor" =~ "DELL,MD32xx" ]]; then
cat >> /tmp/multipath.conf << EOF
device {
        vendor                  "DELL"
        product                 "MD32xx"
        path_grouping_policy    "group_by_prio"
        features                "2 pg_init_retries 50"
        path_checker            "rdac"
        path_selector           "round-robin 0"
        hardware_handler        "1 rdac"
        prio                    "rdac"
        polling_interval        5
        failback                "immediate"
        no_path_retry           30
        detect_prio             yes
        rr_min_io               100
        }
EOF
elif [[ "$vendor" =~ "DELL,MD36xxi" ]]; then
cat >> /tmp/multipath.conf << EOF
device {
        vendor                  "DELL"
        product                 "MD36xxi"
        path_grouping_policy    "group_by_prio"
        features                "2 pg_init_retries 50"
        path_checker            "rdac"
        path_selector           "round-robin 0"
        hardware_handler        "1 rdac"
        prio                    "rdac"
        polling_interval        5
        failback                "immediate"
        no_path_retry           30
        detect_prio             yes
        rr_min_io               100
        }
EOF
elif [[ "$vendor" =~ "DELL,MD36xxf" ]]; then
cat >> /tmp/multipath.conf << EOF
device {
        vendor                  "DELL"
        product                 "MD36xxf"
        path_grouping_policy    "group_by_prio"
        features                "2 pg_init_retries 50"
        path_checker            "rdac"
        path_selector           "round-robin 0"
        hardware_handler        "1 rdac"
        prio                    "rdac"
        polling_interval        5
        failback                "immediate"
        no_path_retry           30
        detect_prio             yes
        rr_min_io               100
        }
EOF
elif [[ "$vendor" =~ "DELL,MD38xx" ]]; then
cat >> /tmp/multipath.conf << EOF
device {
        vendor                  "DELL"
        product                 "MD38xx"
        path_grouping_policy    "group_by_prio"
        features                "2 pg_init_retries 50"
        path_checker            "rdac"
        path_selector           "round-robin 0"
        hardware_handler        "1 rdac"
        prio                    "rdac"
        failback                "immediate"
        no_path_retry           30
        detect_prio             yes
        rr_weight               priorities
        }
EOF
elif [[ "$vendor" =~ "HP,MSA1040SAN" ]]; then
cat >> /tmp/multipath.conf << EOF
device{
       vendor                      "HP”
       product                     "MSA 1040 SAN"
       path_grouping_policy        "group_by_prio"
       path_selector               "round-robin 0"
       rr_weight                   uniform
       path_checker                tur
       hardware_handler            "0"
       failback                    immediate
       no_path_retry               18
       rr_min_io                   100
       }
EOF
elif [[ "$vendor" =~ "HP,MSA1050SAN" ]]; then
cat >> /tmp/multipath.conf << EOF
device{
       vendor                      "HP”
       product                     "MSA 1050 SAN"
       path_grouping_policy        "group_by_prio"
       path_selector               "round-robin 0"
       rr_weight                   uniform
       path_checker                tur
       hardware_handler            "0"
       failback                    immediate
       no_path_retry               18
       rr_min_io                   100
       }
EOF
elif [[ "$vendor" =~ "HP,MSA2040SAN" ]]; then
cat >> /tmp/multipath.conf << EOF
device{
       vendor                      "HP”
       product                     "MSA 2040 SAN"
       path_grouping_policy        "group_by_prio"
       path_selector               "round-robin 0"
       rr_weight                   uniform
       path_checker                tur
       hardware_handler            "0"
       failback                    immediate
       no_path_retry               18
       rr_min_io                   100
       }
EOF
elif [[ "$vendor" =~ "HP,MSA2050SAN" ]]; then
cat >> /tmp/multipath.conf << EOF
device{
       vendor                      "HP”
       product                     "MSA 2050 SAN"
       path_grouping_policy        "group_by_prio"
       path_selector               "round-robin 0"
       rr_weight                   uniform
       path_checker                tur
       hardware_handler            "0"
       failback                    immediate
       no_path_retry               18
       rr_min_io                   100
       }
EOF
elif [[ "$vendor" =~ "MacroSAN,LU" ]]; then
cat >> /tmp/multipath.conf << EOF
device{ 
      vendor                        "MacroSAN"
      product                       "LU"
      path_grouping_policy          group_by_prio
      path_selector                 "round-robin 0"
      path_checker                  tur
      prio                          alua
      no_path_retry                 30
      hardware_handler              "0"
      failback                      15
      }
EOF
elif [[ "$vendor" =~ "3PARdata,VV" ]]; then
cat >> /tmp/multipath.conf << EOF
device{
      vendor                   "3PARdata"
      product                  "VV"
      path_grouping_policy     group_by_prio
      path_selector            "round-robin 0"
      path_checker             tur
      features                 "0"
      hardware_handler         "1 alua"
      prio                     alua
      failback                 immediate
      rr_weight                uniform
      no_path_retry            18
      rr_min_io_rq             1
      detect_prio              yes
      fast_io_fail_tmo         10
      dev_loss_tmo             14
      }
EOF
fi
fi
done
fi
cat >> /tmp/multipath.conf  << EOF
}
EOF
xport=`cat /tmp/vendors`
if [[ "$xport" =~ "UniversalXport" ]];then
sed -i 's/blacklist{/&\ndevice{\nvendor     "*"\nproduct    "Universal Xport"\n}/' /tmp/multipath.conf
fi
if [[ "$vendor" =~ "IBM,2145" && "$oskernel" = "2.6.32" ]];then
sed -i '/path_selector/s/service-time 0/round-robin 0/' /tmp/multipath.conf
fi
hpe3par=`cat /tmp/vendors`
if [[ "$hpe3par" =~ "3PARdata,VV" && "$oskernel" = "2.6.32" ]];then
echo -e "\e[94m当前存储为HPE 3PAR系列,官方推荐使用【Generic-ALUA 角色2】,linux7.x
系统默认使用【Generic-ALUA 角色2】,鉴于当前系统是6.x,请选择你创建主机的角色定义\e[0m"
echo -e "\e[94m###################################################################\e[0m"
echo -e "\e[94m#                    1.【Generic 角色 1】                         #\e[0m"
echo -e "\e[94m#                    2.【Generic-ALUA 角色 2】                    #\e[0m"
echo -e "\e[94m###################################################################\e[0m"
while true
do
read -p "选择主机角色定义,输入数字即可:" hostnum
if [[ "$hostnum" == 1 ]];then
echo -e "\e[94m已选择【Generic 角色 1】\e[0m"
break;
elif [[ "$hostnum" == 2 ]];then
echo -e "\e[94m已选择【Generic 角色 1】\e[0m"
break;
else
echo -e "\e[94m选择正确的主机定义!\e[0m"
fi
done
fi
if [[ "$hostnum" == 1 ]];then
sed -i '/path_grouping_policy/s/group_by_prio/multibus/' /tmp/multipath.conf
sed -i '/prio/d' /tmp/multipath.conf
echo -e "\e[94m已适配【Generic 角色 1】的多路径参数!\e[0m"
fi
cat /tmp/multipath.conf > /etc/multipath.conf
sleep 1
echo -e "\e[94m正在配置多路径.............................\e[0m"
if [ "$oskernel" = "2.6.32" ]; then
service multipathd restart > /dev/null 2>&1
sleep 3
multipath -F
multipath -v2 > /dev/null 2>&1
multipath -ll
fi
if [[ "$oskernel" = "3.10.0" ]]; then
systemctl restart multipathd
sleep 3
multipath -F
multipath -v2 > /dev/null 2>&1
multipath -ll
fi
if [[ "$oskernel" = "4.18.0" ]]; then
sleep 1
systemctl restart multipathd > /dev/null 2>&1
sleep 3
multipath -F
multipath -v2 > /dev/null 2>&1
multipath -ll
fi
rm -f /tmp/multipath.conf
rm -f /tmp/nic
rm -f /tmp/vendor
rm -f /tmp/vendors
echo -e "\e[94m多路径配置已完成!\e[0m"
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
fi
if [[ "$language" == 2 ]];then
echo -e "\e[94m###################################################################\e[0m"
echo -e "\e[94m#  1.Multipath configuration script for FC-SAN and IP-SAN         #\e[0m"
echo -e "\e[94m#  2.Support Lenovo DM/DE Series,DS Series,Storwize V Series      #\e[0m"
echo -e "\e[94m#  3.Support NetApp FAS/E Series,IBM Storwize V Series,FS Series  #\e[0m"
echo -e "\e[94m#  4.Support HuaWei OceanStor Series,Inspur G2 Series             #\e[0m"
echo -e "\e[94m#  5.Support DELL MD/SC Series, MacroSAN MS Series                #\e[0m"
echo -e "\e[94m#  6.Support HPE 3PAR Series,MSA 1040/1050/2040/2050 Series       #\e[0m"
echo -e "\e[94m#  7.Others,use default multipath parameters generated by OS      #\e[0m"
echo -e "\e[94m#  8.Support CentOS 6.x/7.x/8.x RedHat 6.x/7.x/8.x                #\e[0m"
echo -e "\e[94m#  9.Author: Mr.Mao                                               #\e[0m"
echo -e "\e[94m#  10.Compiled: April 16,2020                                     #\e[0m"
echo -e "\e[94m#  11.More View https://www.mr-mao.cn                             #\e[0m"
echo -e "\e[94m###################################################################\e[0m"
echo -e "\033[1;mTip:If the Backspace key does not work,press Ctrl+Backspace\e[0m"
sleep 1
oskernel=`uname -r | awk -F"-" '{print $1}'`
osrealease=`cat /etc/redhat-release`
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
echo -e "\e[94mThe current OS is $osrealease\e[0m"
if [ "$oskernel" = "2.6.32" ]; then
echo -e "\e[94mThe kernel version is 2.6.32 Series\e[0m"
elif [ "$oskernel" = "3.10.0" ]; then
echo -e "\e[94mThe kernel version is 3.10.0 Series\e[0m"
elif [ "$oskernel" = "4.18.0" ]; then
echo -e "\e[94mThe kernel version is4.18.0 Series\e[0m"
echo -e "\033[1;mBecause the storage manufacturer has not made adaptations, the current system uses the multipath parameters generated by the system by default!\e[0m"
else
echo -e "\e[94mThis script does not support the current OS\e[0m"
exit
fi
multipathpack=`rpm -qa | grep device-mapper-multipath |wc -l`
if [ "$multipathpack" -ge 2 ]; 
then
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
echo -e "\e[94mMultipath software is installed!\e[0m"
elif [ "$multipathpack" -lt 2 ]; 
then
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
echo -e "\e[94mMultipath software is not installed,if you have yum,then execute\e[0m"
echo yum install -y device-mapper-multipath
echo -e "\e[94melse,Go to the /Packages folder of the installation source,execute\e[0m"
echo rpm -ivh device-mapper-multipath-*.rpm
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
exit
else
exit
fi
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
initpack=`rpm -qa |grep iscsi-initiator-utils |wc -l`
lspci > /dev/null 2>&1
if [[ $? == 0 ]];then
hba=`lspci |grep -i fibre`
fi
oslabel=`df -h |grep boot |head -n1 |awk -F " " '{print$1}' |grep [a-z]d[a-z] -o`
echo -e "\e[94m###################################################################\e[0m"
echo -e "\e[94m#                            1.FC                                 #\e[0m"
echo -e "\e[94m#                            2.ISCSI                              #\e[0m"
echo -e "\e[94m###################################################################\e[0m"
while true
do
read -p "Select the storage connection method and enter the number: " mode
if [ "$mode" == 1 ];then
echo -e "\e[94mThe selected connection method is: FC\e[0m"
mode=1
break;
elif [ "$mode" == 2 ]; then
echo -e "\e[94mThe selected connection method is: ISCSI\e[0m"
mode=2
break;
else
echo -e "\e[94m选择正确的连接方式!\e[0m"
fi
done
if [ "$mode" == 1 ]; then
if [ -z "$hba" ]; then
lspci > /dev/null 2>&1
if [[ $? != 0 ]];then
echo -e "\e[94mThe current operating system is missing a component to view the hardware, please determine whether the HBA card has been recognized by yourself,Or enter the following command to install.\e[0m"
echo -e "\e[94mif you have yum,then execute\e[0m"
echo yum -y install pciutils
echo -e "\e[94melse,Go to the /Packages folder of the installation source,execute\e[0m"
echo rpm -ivh pciutils-*.rpm
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
fi
echo -e "\e[94mNo HBA card detected,please check if the driver is installed properly!\e[0m"
exit
fi
echo -e "\e[94mThe current OS disk has been detected as $oslabel\e[0m"
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
sleep 1
echo -e "\e[94mThe WWPN of the current host HBA card is as follows:\e[0m"
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
cat /sys/class/fc_host/host*/port_name |awk -F "0x" '{print $2}'
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
sleep 1
fi
if [ "$mode" == 2 ]; 
then
if [ "$initpack" == 0 ]; then
echo -e "\e[94mISCSI software is not installed,please install it first!\e[0m"
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
echo -e "\e[94mif you have yum,then execute\e[0m"
echo yum -y install iscsi-initiator-utils
echo -e "\e[94melse,Go to the /Packages folder of the installation source,execute\e[0m"
echo rpm -ivh iscsi-initiator-utils-*.rpm
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
exit
fi
echo -e "\e[94mThe iSCSI initiator name of the current host is: \e[0m"
echo
iscsiadm -m node --logoutall=all > /dev/null 2>&1
cat /etc/iscsi/initiatorname.iscsi |awk -F "=" '{print$2}'
echo
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
sleep 1
ip a | grep -E "^[0-9]" | grep -vE ": lo|: virbr" |awk -F ": <" '{print$1}' |awk -F ": " '{print$2}' > /tmp/nic
echo -e "\e[94mAll NICs of the current host are as follows: \e[0m"
echo -e "\e[94m-------------------------\e[0m"
cat /tmp/nic |awk '{print NR,$1}'
echo -e "\e[94m-------------------------\e[0m"
while true
do
read -p "Select the NIC used for storage,enter the number.multiple NICs,separated by spaces: " nicnum
selectnum=`echo $nicnum |awk -F " " '{print NF}'`
if [ $selectnum -lt 1 ];then
echo -e "\e[94mSelect at least one NIC,please choose again!\e[0m"
else
break;
fi
done
for num in $nicnum;
do
nicx=`cat /tmp/nic |awk "NR==$num" |awk -F " " '{print$1}'`
if [[ "$nicx" == "" ]];then
echo -e "\e[94mPlease select correct NIC!\e[0m"
exit
fi
done
for num in $nicnum;
do
selectnic=`cat /tmp/nic |awk "NR==$num"`
selectnicip=`ip a s $selectnic | grep -o '[0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+' | head -n1`
if [ -z "$selectnicip" ];then
echo -e "\e[94mThe NIC $selectnic has no IP address,please configure it first!\e[0m"
exit
else
echo -e "\e[94mChosen $selectnic\e[0m"
echo -e "\e[94m-------------------------\e[0m"
echo -e "\e[94mNIC $selectnic IP address:
$selectnicip\e[0m"
echo -e "\e[94m-------------------------\e[0m"
sleep 1
fi
done
read -p "Please enter the storage target IP address.If there are multiple IPs,separate them with spaces: " targetip
for tip in $targetip;
do
echo -e "\e[94mChecking network connectivity,please wait!\e[0m"
ping=`ping -c 5 $tip |awk 'NR==9 {print $4}'`
if [[ $ping -eq 0 ]];then
echo -e "\e[94mCurrent host and storage cannot communicate,please check the network configuration!\e[0m"
exit
else 
echo -e "\e[94mCurrent host and storage $tip communication is normal,iSCSI connection is being configuring......\e[0m"
iscsiadm -m discovery -t st -p $targetip > /dev/null 2>&1
if [[ "$?" == 0 ]]; then
iscsiadm -m node -p $tip -l > /dev/null 2>&1
echo -e "\e[94miSCSI is connected,please map the volume on the storage side!\e[0m"
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
sleep 1
else 
echo -e "\e[94mCurrent host cannot login to the iSCSI storage! Please check the configuration!\e[0m"
exit
fi
fi
done
fi
read -p "Is the mapping done on the storage side? Press n to exit,press other keys to continue: " answer1
answer1lower=`echo $answer1 | tr [A-Z] [a-z]`
if [ "$answer1lower" == "n" ]; then
echo -e "\e[94mPlease re-execute this script after the mapping is done on the storage side!\e[0m"
else 
sleep 1
fi
if [[ -z "$oslabel" ]]; then
while true
do
read -p "Enter the disk where the OS is located,such as sda: " osdisk
if [[ -z "$osdisk" ]]; then
echo -e "\e[94mThe OS disk cannot be empty!\e[0m"
else
echo -e "\e[94mThe current OS disk is $osdisk\e[0m"
break;
sleep 1
fi
done
else
osdisk=$oslabel
fi
if [ "$oskernel" = "2.6.32" ]; then
wwid=`/sbin/scsi_id -g -u /dev/$osdisk`
fi
if [[ "$oskernel" = "3.10.0" ]]; then
wwid=`/usr/lib/udev/scsi_id -g -u /dev/$osdisk`
fi
if [[ "$oskernel" = "4.18.0" ]]; then
wwid=`/usr/lib/udev/scsi_id -g -u /dev/$osdisk`
fi
sleep 1
if [ "$mode" == 1 ]; 
then
for x in `ls /sys/class/fc_host`; do echo "- - -" > /sys/class/scsi_host/$x/scan; done
elif [ "$mode" == 2 ]; 
then
for x in `ls /sys/class/iscsi_host`; do echo "- - -" > /sys/class/scsi_host/$x/scan; done
else
exit
fi
echo -e "\e[94mDetecting the storage....\e[0m"
sleep 1
rm -f /etc/multipath.conf
if [ "$oskernel" = "2.6.32" ]; then
mpathconf --enable --find_multipaths y --user_friendly_names y --with_module y --with_chkconfig y
sleep 2
service multipathd restart > /dev/null 2>&1
sleep 2
multipath -F
multipath -v2 > /dev/null 2>&1
multipath -l
fi
if [[ "$oskernel" = "3.10.0" ]]; then
mpathconf --enable --find_multipaths y --with_module y --with_multipathd y --user_friendly_names y
sleep 2
systemctl restart multipathd
sleep 2
multipath -F
multipath -v2 > /dev/null 2>&1
multipath -l
fi
if [[ "$oskernel" = "4.18.0" ]]; then
mpathconf --enable --find_multipaths y --with_module y --with_multipathd y --user_friendly_names y
sleep 1
systemctl restart multipathd > /dev/null 2>&1
sleep 3
multipath -F
multipath -v2 > /dev/null 2>&1
multipath -l
fi
dev=`multipath -l`
if [[ -z "$dev" ]]; then
echo -e "\e[94mNo storage detected!Please check if it is mapped or if the link is multi-link!\e[0m"
exit
fi
echo
if [ "$wwid" == "" ]; then
echo -e "\e[94mThe wwid of the system disk $osdisk is not detected,the current host may be a virtual machine,and $osdisk has been used as a blacklisted device in the multipath configuration file!\e[0m"
backlist=$osdisk
cat > /tmp/multipath.conf << EOF
blacklist{
devnode "$backlist"
}
defaults {
     user_friendly_names yes
     find_multipaths yes
}
multipaths {
EOF
else 
backlist=$wwid
echo -e "\e[94mThe WWID of OS disk is detected as $wwid,and wwid has been used as a blacklisted device in the multipath configuration file!\e[0m"
echo
cat > /tmp/multipath.conf << EOF
blacklist{
wwid "$backlist"
}
defaults {
     user_friendly_names yes
     find_multipaths yes
}
multipaths {
EOF
fi
multipath=`multipath -l |grep dm- |grep [0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z] -o`
if [[ "$multipath" == "" ]];then
echo -e "\e[94mThis script does not support current storage!\e[0m"
exit
fi
for dev in $multipath;
do
size=`multipath -l $dev |grep size= |awk -F " " '{print$1}'`
dm=`multipath -l |grep $dev |grep dm-"[0-9]\{1,3\}" -o`
vendor=`multipath -l |grep $dev |awk -F "$dm" '{print$2}' |sed s/[[:space:]]//g`
multipath -l |grep $dev |awk -F "$dm" '{print$2}' |sed s/[[:space:]]//g > /tmp/vendor
multipath -l |grep $dev |awk -F "$dm" '{print$2}' |sed s/[[:space:]]//g >> /tmp/vendors
if [[ "$vendor" =~ "NETAPP,LUNC-Mode" ]]; then
echo -e "\e[94mDetected NETAPP brand FAS series storage!\e[0m"
elif [[ "$vendor" =~ "NETAPP,INF-01-00" ]]; then
echo -e "\e[94mDetected NETAPP brand E series storage!\e[0m"
elif [[ "$vendor" =~ "LSI,INF-01-00" ]]; then
echo -e "\e[94metected NETAPP brand E series storage!\e[0m"
elif [[ "$vendor" =~ "LENOVO,DE_Series" ]]; then
echo -e "\e[94mDetected LENOVO brand DE series storage!\e[0m"
elif [[ "$vendor" =~ "Lenovo,DS2200" ]]; then
echo -e "\e[94mDetected LENOVO brand DS2200 storage!\e[0m"
elif [[ "$vendor" =~ "Lenovo,DS4200" ]]; then
echo -e "\e[94mDetected LENOVO brand DS4200 storage!\e[0m"
elif [[ "$vendor" =~ "Lenovo,DS6200" ]]; then
echo -e "\e[94mDetected LENOVO brand DS6200 storage!\e[0m"
elif [[ "$vendor" =~ "IBM,2145" ]]; then
echo -e "\e[94mDetected IBM brand V or FS series storage!\e[0m"
elif [[ "$vendor" =~ "HUAWEI,XSG1" ]]; then
echo -e "\e[94mDetected HUAWEI brand OceanStor series storage!\e[0m"
elif [[ "$vendor" =~ "INSPUR,MCS" ]]; then
echo -e "\e[94mDetected INSPUR brand G2 series storage!\e[0m"
elif [[ "$vendor" =~ "COMPELNT,CompellentVol" ]]; then
echo -e "\e[94mDetected DELL brand SC series storage!\e[0m"
elif [[ "$vendor" =~ "DELL,MD34xx" ]]; then
echo -e "\e[94mDetected DELL brand MD34xx series storage!\e[0m"
elif [[ "$vendor" =~ "DELL,MD32xx" ]]; then
echo -e "\e[94mDetected DELL brand MD32xx series storage!\e[0m"
elif [[ "$vendor" =~ "DELL,MD36xx" ]]; then
echo -e "\e[94mDetected DELL brand MD36xx series storage!\e[0m"
elif [[ "$vendor" =~ "DELL,MD38xx" ]]; then
echo -e "\e[94mDetected DELL brand MD38xx series storage!\e[0m"
elif [[ "$vendor" =~ "HP,MSA1040SAN" ]]; then
echo -e "\e[94mDetected HPE brand MSA 1040 series storage!\e[0m"
elif [[ "$vendor" =~ "HP,MSA1050SAN" ]]; then
echo -e "\e[94mDetected HPE brand MSA 1050 series storage!\e[0m"
elif [[ "$vendor" =~ "HP,MSA2040SAN" ]]; then
echo -e "\e[94mDetected HPE brand MSA 2040 series storage!\e[0m"
elif [[ "$vendor" =~ "HP,MSA2050SAN" ]]; then
echo -e "\e[94mDetected HPE brand MSA 2050 series storage!\e[0m"
elif [[ "$vendor" =~ "MacroSAN,LU" ]]; then
echo -e "\e[94mDetected MacroSAN brand MS series storage!\e[0m"
elif [[ "$vendor" =~ "3PARdata,VV" ]]; then
echo -e "\e[94mDetected HPE brand 3PAR series storage!\e[0m"
elif [[ "$vendor" =~ "UniversalXport" ]]; then
echo -e "\e[94mIn-storage management partition detected\e[0m"
else
echo -e "\e[94mDetected $vendor brand storage!Current storage will use OS default multipath parameters!\e[0m"
fi
echo  WWID is $dev
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
while true
do
if [[ "$vendor" =~ "UniversalXport" ]]; then
echo -e "\e[94mThis partition is automatically mapped for storage vendors to implement in-band management and has been blocked!\e[0m"
break;
else
echo -e "\e[94mDefine alias for storage volume [wwid is $dev $size DM is $dm]\e[0m"
read -p "Enter alias for storage volume: " dmname1
dmname=`echo ${dmname1,,}`
if [ "$dmname" == "" ]; then
echo -e "\e[94mAlias cannot be empty,please re-enter!\e[0m"
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
else
echo -e "\e[94mConfigured volume alias as: $dmname\e[0m"
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
break;
fi
fi
done
if [ "$dmname" != "" ];then
cat >> /tmp/multipath.conf  << EOF
        multipath {
        wwid                  $dev
        alias                 $dmname
       }
EOF
fi
done
sleep 1
echo > /tmp/vendor
if [[ "$oskernel" != "4.18.0" ]]; then
cat >> /tmp/multipath.conf << EOF
}
devices{
EOF
echo -e "\e[94mAccording to storage brand,multipath parameters recommended by storage vendors are being written.................\e[0m"
for dev in $multipath;
do
dm=`multipath -l |grep $dev |grep dm-"[0-9]\{1,3\}" -o`
vendor1=`multipath -l |grep $dev |awk -F "$dm" '{print$2}' |sed s/[[:space:]]//g`
vendor2=`cat /tmp/vendor`
if [[ "$vendor1" == "$vendor2" ]];then
echo .
else
vendor=$vendor1
multipath -l |grep $dev |awk -F "$dm" '{print$2}' |sed s/[[:space:]]//g > /tmp/vendor
if [[ "$vendor" =~ "NETAPP,LUNC-Mode" ]]; then
cat >> /tmp/multipath.conf << EOF
device {
        vendor                  "NETAPP"
        product                 "LUN"
        flush_on_last_del       yes
        dev_loss_tmo            infinity
        fast_io_fail_tmo        5
        failback                immediate
        features                "3 queue_if_no_path pg_init_retries 50"
        hardware_handler        "1 alua"
        path_checker            tur
        path_grouping_policy    group_by_prio
        path_selector           "round-robin 0"
        prio                    alua
        rr_min_io               1000
        rr_weight               uniform
           }
EOF
elif [[ "$vendor" =~ "NETAPP,INF-01-00" ]]; then
cat >> /tmp/multipath.conf << EOF
device {
        vendor                  "NETAPP"
        product                 "INF-01-00"
        path_grouping_policy    "group_by_prio"
        features                "2 pg_init_retries 50"
        path_checker            "rdac"
        path_selector           "round-robin 0"
        hardware_handler        "1 rdac"
        prio                    "rdac"
        failback                "immediate"
        no_path_retry           30
        detect_prio             yes
        rr_weight               priorities
        }
EOF
elif [[ "$vendor" =~ "LSI,INF-01-00" ]]; then
cat >> /tmp/multipath.conf << EOF
device {
        vendor                  "LSI"
        product                 "INF-01-00"
        path_grouping_policy    "group_by_prio"
        features                "2 pg_init_retries 50"
        path_checker            "rdac"
        path_selector           "round-robin 0"
        hardware_handler        "1 rdac"
        prio                    "rdac"
        failback                "immediate"
        no_path_retry           30
        detect_prio             yes
        rr_weight               priorities
        }
EOF
elif [[ "$vendor" =~ "LENOVO,DE_Series" ]]; then
cat >> /tmp/multipath.conf << EOF
device {
        vendor                  "LENOVO"
        product                 "DE_Series"
        path_grouping_policy    "group_by_prio"
        features                "2 pg_init_retries 50"
        path_checker            "rdac"
        path_selector           "round-robin 0"
        hardware_handler        "1 rdac"
        prio                    "rdac"
        failback                "immediate"
        no_path_retry           30
        detect_prio             yes
        rr_weight               priorities
        }
EOF
elif [[ "$vendor" =~ "Lenovo,DS2200" ]]; then
cat >> /tmp/multipath.conf << EOF
device {
        vendor          "Lenovo" 
        product         "DS2200" 
        failback        immediate    
        prio            alua 
        } 
EOF
elif [[ "$vendor" =~ "Lenovo,DS4200" ]]; then
cat >> /tmp/multipath.conf << EOF
device {
        vendor          "Lenovo" 
        product         "DS4200" 
        failback        immediate    
        prio            alua 
        } 
EOF
elif [[ "$vendor" =~ "Lenovo,DS6200" ]]; then
cat >> /tmp/multipath.conf << EOF
device {
        vendor          "Lenovo" 
        product         "DS6200" 
        failback        immediate    
        prio            alua 
        } 
EOF
elif [[ "$vendor" =~ "IBM,2145" ]]; then
cat >> /tmp/multipath.conf << EOF
device {
       vendor                  "IBM"
       product                 "2145"
       path_grouping_policy    "group_by_prio"
       path_selector           "service-time 0" 
       prio                    "alua"
       path_checker            "tur"
       failback                "immediate"
       no_path_retry           5
       rr_weight               uniform
       rr_min_io_rq            "1"
       dev_loss_tmo            120
       } 
EOF
elif [[ "$vendor" =~ "HUAWEI,XSG1" ]]; then
cat >> /tmp/multipath.conf << EOF
device {
       vendor                  "HUAWEI"
       product                 "XSG1"
       path_grouping_policy    "group_by_prio"
       path_checker            "tur"
       path_selector           "round-robin 0"
       prio                    alua
       failback                "immediate"
       dev_loss_tmo            30
       fast_io_fail_tmo        5
    }
EOF
elif [[ "$vendor" =~ "INSPUR,MCS" ]]; then
cat >> /tmp/multipath.conf << EOF
device {
        vendor                  "INSPUR"
        product                 "MCS"
        path_grouping_policy    group_by_prio
        path_selector           "service-time 0"
        features                "1 queue_if_no_path" 
        path_checker            tur
        prio                    alua 
        failback                immediate 
    } 
EOF
elif [[ "$vendor" =~ "COMPELNT,CompellentVol" ]]; then
cat >> /tmp/multipath.conf << EOF
device {
        vendor                  "COMPELNT" 
        product                 "Compellent Vol" 
        features                0 
        no_path_retry           fail 
    } 
EOF
elif [[ "$vendor" =~ "DELL,MD34xx" ]]; then
cat >> /tmp/multipath.conf << EOF
device {
        vendor                  "DELL"
        product                 "MD34xx"
        path_grouping_policy    "group_by_prio"
        features                "2 pg_init_retries 50"
        path_checker            "rdac"
        path_selector           "round-robin 0"
        hardware_handler        "1 rdac"
        prio                    "rdac"
        failback                "immediate"
        no_path_retry           30
        detect_prio             yes
        rr_weight               priorities
        }
EOF
elif [[ "$vendor" =~ "DELL,MD32xxi" ]]; then
cat >> /tmp/multipath.conf << EOF
device {
        vendor                  "DELL"
        product                 "MD32xxi"
        path_grouping_policy    "group_by_prio"
        features                "2 pg_init_retries 50"
        path_checker            "rdac"
        path_selector           "round-robin 0"
        hardware_handler        "1 rdac"
        prio                    "rdac"
        polling_interval        5
        failback                "immediate"
        no_path_retry           30
        detect_prio             yes
        rr_weight               priorities
        }
EOF
elif [[ "$vendor" =~ "DELL,MD32xx" ]]; then
cat >> /tmp/multipath.conf << EOF
device {
        vendor                  "DELL"
        product                 "MD32xx"
        path_grouping_policy    "group_by_prio"
        features                "2 pg_init_retries 50"
        path_checker            "rdac"
        path_selector           "round-robin 0"
        hardware_handler        "1 rdac"
        prio                    "rdac"
        polling_interval        5
        failback                "immediate"
        no_path_retry           30
        detect_prio             yes
        rr_min_io               100
        }
EOF
elif [[ "$vendor" =~ "DELL,MD36xxi" ]]; then
cat >> /tmp/multipath.conf << EOF
device {
        vendor                  "DELL"
        product                 "MD36xxi"
        path_grouping_policy    "group_by_prio"
        features                "2 pg_init_retries 50"
        path_checker            "rdac"
        path_selector           "round-robin 0"
        hardware_handler        "1 rdac"
        prio                    "rdac"
        polling_interval        5
        failback                "immediate"
        no_path_retry           30
        detect_prio             yes
        rr_min_io               100
        }
EOF
elif [[ "$vendor" =~ "DELL,MD36xxf" ]]; then
cat >> /tmp/multipath.conf << EOF
device {
        vendor                  "DELL"
        product                 "MD36xxf"
        path_grouping_policy    "group_by_prio"
        features                "2 pg_init_retries 50"
        path_checker            "rdac"
        path_selector           "round-robin 0"
        hardware_handler        "1 rdac"
        prio                    "rdac"
        polling_interval        5
        failback                "immediate"
        no_path_retry           30
        detect_prio             yes
        rr_min_io               100
        }
EOF
elif [[ "$vendor" =~ "DELL,MD38xx" ]]; then
cat >> /tmp/multipath.conf << EOF
device {
        vendor                  "DELL"
        product                 "MD38xx"
        path_grouping_policy    "group_by_prio"
        features                "2 pg_init_retries 50"
        path_checker            "rdac"
        path_selector           "round-robin 0"
        hardware_handler        "1 rdac"
        prio                    "rdac"
        failback                "immediate"
        no_path_retry           30
        detect_prio             yes
        rr_weight               priorities
        }
EOF
elif [[ "$vendor" =~ "HP,MSA1040SAN" ]]; then
cat >> /tmp/multipath.conf << EOF
device{
       vendor                      "HP”
       product                     "MSA 1040 SAN"
       path_grouping_policy        "group_by_prio"
       path_selector               "round-robin 0"
       rr_weight                   uniform
       path_checker                tur
       hardware_handler            "0"
       failback                    immediate
       no_path_retry               18
       rr_min_io                   100
       }
EOF
elif [[ "$vendor" =~ "HP,MSA1050SAN" ]]; then
cat >> /tmp/multipath.conf << EOF
device{
       vendor                      "HP”
       product                     "MSA 1050 SAN"
       path_grouping_policy        "group_by_prio"
       path_selector               "round-robin 0"
       rr_weight                   uniform
       path_checker                tur
       hardware_handler            "0"
       failback                    immediate
       no_path_retry               18
       rr_min_io                   100
       }
EOF
elif [[ "$vendor" =~ "HP,MSA2040SAN" ]]; then
cat >> /tmp/multipath.conf << EOF
device{
       vendor                      "HP”
       product                     "MSA 2040 SAN"
       path_grouping_policy        "group_by_prio"
       path_selector               "round-robin 0"
       rr_weight                   uniform
       path_checker                tur
       hardware_handler            "0"
       failback                    immediate
       no_path_retry               18
       rr_min_io                   100
       }
EOF
elif [[ "$vendor" =~ "HP,MSA2050SAN" ]]; then
cat >> /tmp/multipath.conf << EOF
device{
       vendor                      "HP”
       product                     "MSA 2050 SAN"
       path_grouping_policy        "group_by_prio"
       path_selector               "round-robin 0"
       rr_weight                   uniform
       path_checker                tur
       hardware_handler            "0"
       failback                    immediate
       no_path_retry               18
       rr_min_io                   100
       }
EOF
elif [[ "$vendor" =~ "HP,MSA2050SAN" ]]; then
cat >> /tmp/multipath.conf << EOF
device{
       vendor                      "HP”
       product                     "MSA 2050 SAN"
       path_grouping_policy        "group_by_prio"
       path_selector               "round-robin 0"
       rr_weight                   uniform
       path_checker                tur
       hardware_handler            "0"
       failback                    immediate
       no_path_retry               18
       rr_min_io                   100
       }
EOF
elif [[ "$vendor" =~ "MacroSAN,LU" ]]; then
cat >> /tmp/multipath.conf << EOF
device{ 
      vendor                        "MacroSAN"
      product                       "LU"
      path_grouping_policy          group_by_prio
      path_selector                 "round-robin 0"
      path_checker                  tur
      prio                          alua
      no_path_retry                 30
      hardware_handler              "0"
      failback                      15
      }
EOF
elif [[ "$vendor" =~ "3PARdata,VV" ]]; then
cat >> /tmp/multipath.conf << EOF
device{
      vendor                   "3PARdata"
      product                  "VV"
      path_grouping_policy     group_by_prio
      path_selector            "round-robin 0"
      path_checker             tur
      features                 "0"
      hardware_handler         "1 alua"
      prio                     alua
      failback                 immediate
      rr_weight                uniform
      no_path_retry            18
      rr_min_io_rq             1
      detect_prio              yes
      fast_io_fail_tmo         10
      dev_loss_tmo             14
      }
EOF
fi
fi
done
fi
cat >> /tmp/multipath.conf  << EOF
}
EOF
xport=`cat /tmp/vendors`
if [[ "$xport" =~ "UniversalXport" ]];then
sed -i 's/blacklist{/&\ndevice{\nvendor     "*"\nproduct    "Universal Xport"\n}/' /tmp/multipath.conf
fi
if [[ "$vendor" =~ "IBM,2145" && "$oskernel" = "2.6.32" ]];then
sed -i '/path_selector/s/service-time 0/round-robin 0/' /tmp/multipath.conf
fi
hpe3par=`cat /tmp/vendors`
if [[ "$hpe3par" =~ "3PARdata,VV" && "$oskernel" = "2.6.32" ]];then
echo -e "\e[94mThe Detected storage is HPE 3PAR series,officially recommended to use
[Generic-ALUA role 2], linux7.x series use [Generic-ALUA role 2] by default.In view of
the fact that OS is 6.x series,please select the role definition of the host you created\e[0m"
echo -e "\e[94m###################################################################\e[0m"
echo -e "\e[94m#                    1.[Generic Persona 1]                        #\e[0m"
echo -e "\e[94m#                    2.[Generic-ALUA Persona 2]                   #\e[0m"
echo -e "\e[94m###################################################################\e[0m"
while true
do
read -p "Select the host role definition and enter the numbers: " hostnum
if [[ "$hostnum" == 1 ]];then
echo -e "\e[94mChosen [Generic Persona 1]\e[0m"
break;
elif [[ "$hostnum" == 2 ]];then
echo -e "\e[94mChosen [Generic-ALUA Persona 2]\e[0m"
break;
else
echo -e "\e[94mChoose the correct host definition!\e[0m"
fi
done
fi
if [[ "$hostnum" == 1 ]];then
sed -i '/path_grouping_policy/s/group_by_prio/multibus/' /tmp/multipath.conf
sed -i '/prio/d' /tmp/multipath.conf
echo -e "\e[94mThe multipath parameters of [Generic Persona 1] have been adapted!\e[0m"
fi
cat /tmp/multipath.conf > /etc/multipath.conf
sleep 1
echo -e "\e[94mConfiguring multipath..........................\e[0m"
if [ "$oskernel" = "2.6.32" ]; then
service multipathd restart > /dev/null 2>&1
sleep 3
multipath -F
multipath -v2 > /dev/null 2>&1
multipath -ll
fi
if [[ "$oskernel" = "3.10.0" ]]; then
systemctl restart multipathd
sleep 3
multipath -F
multipath -v2 > /dev/null 2>&1
multipath -ll
fi
if [[ "$oskernel" = "4.18.0" ]]; then
sleep 1
systemctl restart multipathd > /dev/null 2>&1
sleep 3
multipath -F
multipath -v2 > /dev/null 2>&1
multipath -ll
fi
rm -f /tmp/multipath.conf
rm -f /tmp/nic
rm -f /tmp/vendor
rm -f /tmp/vendors
echo -e "\e[94mMultipath configuration is completed!\e[0m"
echo -e "\e[94m-------------------------------------------------------------------\e[0m"
fi

 为存储配置多路径,各位工程师都不陌生。在Linux下配置multipath,更不在话下,mpathconf --enable 一条命令走天下,更是老手的惯例,因为mpatha、mpathb、mpathc、这种名称也不会影响使用。但是,给存储卷定义别名就显得不那么粗狂,使用厂商推荐的多路径参数,更显得专业,现在配置多路径的自动化交互式脚本横空出世,极大的方便了各位工程师。

Linux下配置网卡聚合交互式自动配置脚本

之前写了手动配置多路径的文档Linux操作系统配置多路径通用教程
[scode type="red"]仅适用于初次配置![/scode] 

脚本功能:

1.多路径辅助配置脚本适用于FC-SAN和IP-SAN
2.支持联想 DM、DE系列、DS系列、Storwize V系列
3.支持NetApp FAS、E系列,IBM Storwize V系列、FS系列
4.支持华为OceanStor系列,浪潮G2系列
5.支持戴尔 MD系列、SC系列,宏杉MS系列
6.支持惠普 3PAR系列、MSA 1040、1050、2040、2050系列
7.其他品牌和型号,使用操作系统生成的默认多路径参数
8.支持CentOS 6.x/7.x/8.x RedHat 6.x/7.x
9.支持中英选择

5月14日更新:

修复Lenovo DE系列存储新版本微码下20m管理分区的识别
优化判断逻辑

4月16日更新:

1,新增支持CentOS 8.x/RedHat 8.x
2,优化多个判断逻辑
备注:因为存储厂商目前并未适配8系列系统,我多次测试使用7系列的官方参数,会有问题。
所以会默认使用8系列系统生成的多路径参数,不加入任何官方参数。

[scode type="blue"]已付费用户,联系我获取最新版本,或者有其他需求,如增加功能,或者修改直接联系我即可[/scode]

linux 8系列下部分截图

39882-38g6si1hxuf.png

00426-7ajrlw1qvlc.png

[scode type="blue"]上述列出的品牌及型号,会使用厂商推荐的最佳多路径参数,没有列出的品牌和型号,则使用操作系统生成的默认多路径参数,如你使用的存储不被支持,则会有相应的提示![/scode]

在使用的过程中如果遇到什么问题,或有什么建议或者需要增加品牌或者型号的参数,加我微信交流,微信二维码在文章底部。

部分截图:

72245-ulijo5dydc.png

69257-s6gehd62tcb.png

04657-pysozkg51r.png

41654-hwm6xkglsiu.png

59342-oz6m3r7btq.png

38546-7k1sjoa1ugj.png

屏蔽20M管理分区截图:

02765-zez5w5pyw3.png

惠普3PAR存储 选择主机定义截图:

04779-ijslxwx30qp.png

[scode type="blue"]创作脚本不易,打赏后获取脚本下载链接!获取后加我微信,后续更新版本,会通过微信传递[/scode]

  4月初VMware发布了VSphere7.0的版本,基友91严先生马不停蹄测试了VCSA7.0无DNS的安装,经多次测试,总结出如下安装过程。

6.7的版本看这里 无DNS解析环境下部署Vcenter6.7

1,第一阶段,FQDN,IP地址,DNS服务器填写同一个IP地址

41341-1d3slc52118.png

2,待第一阶段提示部署成功后,在vsphere web client里,按F2进入VCSA的配置界面,选择Troubleshooting Mode Options

28331-vyglqtk75v9.png

回车进入,选择Enable SSH,然后回车。确保SSH状态开启。

11773-vs6njsppyzi.png

ssh登录VCSA的管理地址,输入shell,编辑/etc/hosts文件,添加一行如下内容:

192.168.80.110    localhost
#格式说明:VCSA管理地址   主机名

59134-tpqs9dv0k2p.png

VCSA7.0默认的主机名是localhost,VCSA6.7默认的主机名是photon-machine
如果你进入5480安装,并且更改了默认的主机名localhost,则/etc/hosts文件中的主机名和修改后的主机名一致

48048-rz9es4pqwce.png

3,安装到63%的时候,会提示如下错误,点击关闭即可。其实已经安装成功。

43051-j27pqsmu4i8.png

4,登录VCSA的管理地址

83574-x0bo3bectbp.png

个人建议,还是安装AD,部署DNS服务器稳妥。

  大多数服务器厂商都随着VMware发布新版的VSphere,从而发布集成自家硬件驱动的ISO镜像,但是还是有少数厂商没有,或者服务器使用偏门阵列卡和网卡以及有最新驱动的需求,这就需要手动集成驱动到ESXi镜像中。集成驱动主要以集成阵列卡和网卡驱动为主,其他驱动则可通过ssh安装。镜像中没有阵列卡驱动没法安装,没有网络驱动,没法使用虚拟化平台。网上关于集成 ESXI6.x 驱动的文章其实比较多,大部分都是通过VMware PowerShell在线下载镜像集成驱动的教程,由于网络原因,成功的次数少之又少,就这样,网络门槛劝退了不少人。而使用本地bundle来集成驱动的教程也有,但是关于如何下载缺少的驱动都是一笔带过,我今天主要以如何下载驱动为主。

一,准备环境

1,Windows 10

下载链接在底部

下载 VMware-PowerCLI-11.5.0-14912921.zip,并解压,把解压出来的模块文件夹放到C:\Program Files\WindowsPowerShell\Modules,如下图:

42722-5pa3oix8hrb.png

2,Windows 7

下载 VMware-PowerCLI-6.5.0-4624819.exe 并安装,6.5.R1这是VMware PowerShell 里目前最新的exe安装包,如果安装过程中提示安装其他依赖包,大家自行解决即可。VMware PowerShell 10系列版本及最新版本都是通过在线安装模式,门槛有点高。

3,测试环境

下载 ESXi-Customizer-PS-v2.6.0.ps1,这是集成驱动用到的脚本,由国外的社区团队维护。把脚本放到你操作顺手的文件夹,使用管理员打开PowerShell程序,定位到文件夹,输入

Set-ExecutionPolicy Unrestricted

输入Y,然后回车,这一步是为了让PowerShell执行各种程序脚本没有限制。

97525-enyal86a4ne.png

继续输入

.\ESXi-Customizer-PS-v2.6.0.ps1 -help

如果有下面的输出则说明VMware PowerShell模块安装成功,这是脚本的使用说明。

31470-qpo27ia295.png

如果提示:FATAL ERROR: Failed to import the VMware.VimAutomation.Core module! 则说明未安装成功!

32287-dettt3idh74.png

二,下载驱动

1,下载ESXi离线包
登录VMware官网,下载VSphere的离线bundle包

https://my.vmware.com/cn/group/vmware/info?slug=datacenter_cloud_infrastructure/vmware_vsphere/6_7

此处我以6.7U3 VMware vSphere Hypervisor (ESXi) Offline Bundle为例

64243-t0q42vd3ia.png

2,下载驱动
我以浪潮为例,下载阵列卡PM8222-SHBA 驱动和自研万兆网卡INSPUR_XL710_10G_LC_PCIEX8_2驱动。

打开VMware网站

https://www.vmware.com/resources/compatibility/search.php

在查找内容中选择IO Devices,产品发行版本选择6.7U3,品牌名称选择Inspur,关键词输入pm8222-shba,点击更新并查看结果,在搜索过程中,如果未出现你所要结果,则扩大搜索范围,更换关键词,还是找不到,则到厂商官网下载。

75600-63s03mdbkb5.png

根据阵列卡微码版本下载相应版本的驱动

61347-y5mkzmrfiog.png

下载相应驱动

30591-i4omgxa2xg.png

打开浪潮官网,找到XL710网卡驱动并下载

29290-ujlw8l6ryp.png

如果服务器加了其他网卡,则可以到这里找找看

https://vibsdepot.v-front.de/wiki/index.php/List_of_currently_available_ESXi_packages

关于查找驱动的说明:一般在VMware官网可以下载到大部分驱动,除非硬件很特别,则需联系厂商。

驱动说明:集成的驱动可以是vib格式文件,也可以是offline_bundle.zip文件。我们下载的驱动是压缩包,里面包含vib文件和offline_bundle.zip文件,需要提取二者之一来使用,不能直接使用下载的zip文件。vib文件很好辨认,如何确认下载的zip是offline_bundle.zip 还是二者的一个压缩包。首先,离线bundle驱动包,一般会以offline_bundle命名,其次,离线包包含下面内容

29066-bi6a3kaft5.png

三,集成驱动

把ESXi-Customizer-PS-v2.6.0.ps1 和下载好的VSphere离线bundle包放一起,然后新建驱动文件夹drivers

a,以集成vib格式的驱动为例

drivers文件夹放vib格式驱动

90119-vlvivby1toi.png

.\ESXi-Customizer-PS-v2.6.0.ps1 -izip .\ESXi670-201912001.zip -pkgDir .\drivers

01366-lvof9nub6a.png

成功

26271-kkh7c93kc5f.png

b,以集成offline_bundle格式的驱动为例

drivers文件夹放offline_bundle格式驱动
47433-s4sfaqczrl.png

成功

17807-id20zj5jdr.png

87824-gdnvi1i6wik.png