#!/bin/bash
: <Program:
NFS Server installbr/>[email protected]
History:
20171129 v1.0
20171130 v1.1 修改内容如下:
1、取消grep "样式" 文件 &>/dev/null if [ $? -ne 0 ];then的使用方法,修改为 if grep -q "样式" 文件;then方法
2、全都改为函数,用户可以根据需要调用,如不需要判断是否能连接到Internet,可以在main主函数中注释不然其生效
DESC
export LANG="zh_CN.UTF-8"
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
[ -e /etc/init.d/functions ] && . /etc/init.d/functions

1 判断是否为root

root_user(){
[ $UID -ne 0 ] && { echo "请使用root用户";exit 1; }
}

2 判断是否能连接到Internet

con_internet(){
ping -c2 mirrors.aliyun.com &>/dev/null
[ $? -ne 0 ] && { echo "无法连接到Internet";exit 1; }
}

3 SELINUX是否关闭

selinux_cls(){
if [ getenforce != "Disabled" ];then
setenforce 0
sed -ir 's/^(SELINUX=).*/\1disabled/' /etc/sysconfig/selinux
fi
}

4 IPTABLES是否关闭

iptables_cls(){
if ! /etc/init.d/iptables status|grep -q 'not running';then
/etc/init.d/iptables stop
chkconfig iptables off
fi
}

5 yum修改

yum_mod(){
if ! grep -q aliyun /etc/yum.repos.d/CentOS-Base.repo;then
\cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.reop.bak
wget -O /etc/yum.repos./CentOS-Base.reop http://mirrors.aliyun.com/repo/Centos-6.repo
fi
}

6 install NFS Server

ins_nfs(){
if [ rpm -aq rpcbind nfs-utils|wc -l -lt 2 ];then
yum install -y rpcbind nfs-utils &>/dev/null
if [ $? -ne 0 ];then
echo "NFS Server install fail"
exit 1
fi
/etc/init.d/rpcbind start &>/dev/null
/etc/init.d/nfs start &>/dev/null
fi
if [ netstat -lntup|egrep "rpcbind|nfs"|wc -l -lt 6 ];then
action "NFS Server install..." /bin/fasle
else
action "NFS Server install..." /bin/true
fi
}

7 配置 /etc/exports 文档,NFS Server默认目录设置为 /nfsnobody

nfs_conf(){
[ -d /nfsnobody ] || mkdir /nfsnobody
chown -R nfsnobody:nfsnobody /nfsnobody
if [ -s /etc/exports ];then
echo '/nfsnobody 192.168.0.0/24(rw,all_squash,sync)' >>/etc/exports
else
echo '/nfsnobody 192.168.0.0/24(rw,all_squash,sync)' >/etc/exports
fi
exportfs -r &>/dev/null
}

8 优化NFS,设置 /etc/sysctl.conf 文档

sysctl_opt(){
default=$(sysctl -a|grep "[rw]mem_default"|awk '{print $NF}')
max=$(sysctl -a|grep "[rw]mem_max"|awk '{print $NF}')
if [ "$default" != "8388608 8388608" -a "$max" != "16777216 16777216" ];then
cat >>/etc/sysctl.conf<net.core.rmem_default = 8388608
net.core.wmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
EOF
sysctl -p &>/dev/null
fi
}

9 把NFS Server开机启动放置到 /etc/rc.local

nfs_boot_start(){
if [ egrep "(rpcbind|nfs) start" /etc/rc.local|wc -l -lt 2 ];then
echo -e "\n#nfs server 20171129\n/etc/init.d/rpcbind start\n/etc/init.d/nfs start" >>/etc/rc.local
fi
}

main(){
root_user
con_internet
selinux_cls
iptables_cls
yum_mod
ins_nfs
nfs_conf
sysctl_opt
nfs_boot_start
}
main