#!/bin/bash # Mak1=0 Mak2=0 Mak3=0 CenTos=`cat /etc/centos-release | sed 's/.*release \([0-9]\).*/\1/'` while [ $Mak1 -eq 0 ]; do read -p "Input share dir's path: (if clients,just input:'client'!) " SHARe if [[ -d $SHARe ]]; then chmod 777 $SHARe let Mak1=1 elif [[ $SHARe == "client" ]]; then while [ $Mak2 -eq 0 ]; do read -p "Input the path you will mount: (mount with server's shares) " MounT if [[ -d $MounT ]]; then Contents=`du -s $MounT | awk '{print $1}'` if [ $Contents -eq 0 ]; then let Mak2=1 elif [ $Contents -eq 4 ]; then let Mak2=1 else echo -e "\t ${R}$MounT is not empty, Please input a right path${N}" let Mak2=0 fi else echo "Doesn't exist such dir." let Mak2=0 fi done let Mak1=1 else echo "Doesn't exist such dir." let Mak1=0 fi done ############################################################# ##################beautiful################################## ############################################################# Screen=`stty -F /dev/console size` &> /dev/null Lenth_T=${Screen#* } [ -z $Lenth_T ] && Lenth_T=80 Lenth_1=$[Lenth_T-14] R="\033[31m" G="\033[32m" Y="\033[33m" B="\033[34m" P="\033[35m" N="\033[0m" success() { Str=$1 S_pace=$[$Lenth_1-${#Str}] echo -n $Str for i in `seq $S_pace`; do echo -n " " done echo -e "[ ${G}OK${N} ]" } failure() { Str=$1 S_pace=$[$Lenth_1-${#Str}] echo -n $Str for i in `seq $S_pace`; do echo -n " " done echo -e "[ ${R}failure${N} ]" } warning() { Str=$1 S_pace=$[$Lenth_1-${#Str}] echo -n $Str for i in `seq $S_pace`; do echo -n " " done echo -e "[ ${Y}warning${N} ]" } ################################################################## ##1. check environment # ##2. install softwares rpm -q nfs-utils &> /dev/null || yum install -y nfs-utils if [ $? -eq 0 ]; then success "nfs" else failure "nfs" exit 2 fi if [ $CenTos -eq 5 ]; then rpm -q portmap &> /dev/null || yum install -y portmap if [ $? -eq 0 ]; then success "portmap" else failure "portmap" exit 2 fi elif [ $CenTos -eq 6 ] || [ $CenTos -eq 7 ]; then rpm -q rpcbind &> /dev/null || yum install -y rpcbind if [ $? -eq 0 ]; then success "rpcbind" else failure "rpcbind" exit 2 fi else warning "Your system is too old, update it please!" exit 2 fi # ##3. config share dir if [[ $SHARe != "client" ]]; then cat > /etc/exports << EOF $SHARe *(rw,sync,no_root_squash,fsid=0) EOF cat >> /etc/sysconfig/nfs << EOF RQUOTAD_PORT=875 LOCKD_TCPPORT=32803 LOCKD_UDPPORT=32769 MOUNTD_PORT=892 EOF fi # ##4. start nfs serice if [[ $CenTos -eq 5 ]]; then chkconfig --add nfs chkconfig --add portmap chkconfig nfs on chkconfig portmap on service portmap start service nfs start [ $? -eq 0 ] && success "nfs" || failure "nfs" elif [[ $CenTos -eq 6 ]]; then chkconfig --add nfs chkconfig --add rpcbind chkconfig nfs on chkconfig rpcbind on service rpcbind start service nfs start [ $? -eq 0 ] && success "nfs" || failure "nfs" elif [[ $CenTos -eq 7 ]]; then systemctl enable rpcbind &> /dev/null systemctl enable nfs &> /dev/null systemctl start rpcbind systemctl start nfs [ $? -eq 0 ] && success "nfs" || failure "nfs" else warning "Your system is too old, update it please!" exit 4 fi #####################FINISH WORDS####################### MOUNt() { while [ $Mak3 -eq 0 ]; do read -p "Please input nfs server's IP: " Ip if showmount -e $Ip &> /dev/null; then shareS=(`showmount -e $Ip | sed 1d | awk '{print $1}'`) for servi in ${shareS[@]} do [[ "$servi" == "$MounT" ]] && \ mount -t nfs -o soft $Ip:$shareS $MounT &> /dev/null && \ break done [ $? -eq 0 ] && success "client config finish." || failure "client config failure." let Mak3=1 echo -e "$Ip:$shareS\t$MounT\tnfs\tdefaults,soft\t 0 0" >> /etc/fstab mount -a && df -h | grep -q $Ip && success "mount $MounT success" || failure "mount $MounT failure" else echo "$Ip is not nfs server or your input is wrong." sleep 1 let Mak3=0 fi done } if [[ -d $SHARe ]]; then DiR=`exportfs | awk '{print $1}'` &> /dev/null echo -e "\tThe share directories are : \n\t${P}$DiR${N}" elif [[ $SHARe == "client" ]]; then MOUNt fi #client: /etc/fstab: serverIP:/dir /client-dir nfs defaults,_rnetdev 0 0 #如果有重启主机的情况,重启顺序:先关掉nfs,再关掉rpcbind.然后先重启rpcbind,再重启nfs