nfs自动化配置(开机自动挂载,关机自动卸载,状态检查)


nfs自动化配置(开机自动挂载,关机自动卸载,状态检查)

如果nfs服务端状态不可用,nfs客户端挂载目录不可用,会导致客户端挂载目录父目录下无法执行ll、ls等命令。出现卡死的现象(这种问题,通过umount -f -l <挂载目录> 即可解决)。

nfs自动化配置要求:

  1. 实现开机自动挂载nfs;(关键点:/etc/rc.d/rc.local)
  2. 关机自动卸载;(关键点:systemd系统服务配置)
  3. nfs挂载状态检查,包括网络检查、服务端共享目录检查、挂载状态检查,层层深入。(关键点:网络检查、挂载状态检查)
  4. (optional)如果是故障修复场景,比如网络中断后的恢复,客户端检测到网络中断后已经卸载挂载点,当网络恢复后,客户端需要自动挂载的必要性?(目前自动化配置化脚本中实现自动检查及挂载)

实现:

#!/bin/bash
#########################################################################
#
# nfs config script
#
# output:
#       -0 When the nfs configuration is successfully finished.
#       -1 something wrong occurs.
#########################################################################
source ../common/profile.sh
source ../common/common.sh
 
CURRENT_PATH=$(cd $(dirname "${BASH_SOURCE[0]}");pwd)
 
NFS_SERVER=172.16.0.105
CLIENT_MOUNT_POINT=/root/share_dir
NFS_SERVER_SHARED_DIR=/root/share_dir
CUSTOM_SERVICE_PATH=/usr/lib/systemd/system/my-powerdown.service
CUSTOM_SERVICE_NAME=my-powerdown
 
NFS_LOG_PATH=/var/log/nfs.log
 
# set log path
set_log_path $NFS_LOG_PATH
 
if [ ! -e $NFS_LOG_PATH ]; then
    $ECHO "log file path: $NFS_LOG_PATH is not exist, will touch it..."
    touch $NFS_LOG_PATH
fi
 
usage()
{
        cat <<EOF
Usage:
    bash ${0##*/}
 
Options:
    -h, --help                Show help message
 
Output:
    - 0 when the script is executed successfully.
    - 1 something wrong occurs.
EOF
}
 
# 检查ip connect
function check_ip_connect() {
    ping -c 3 -i 0.2 -W 3 $1 &> /dev/null
    if [ $? -eq 0 ]; then
        return 0
    else
        return -1
    fi
}
 
 
# 自动挂载nfs,重启时
function auto_mount_when_onOS() {
    print_log INFO "start to config auto mount shared directory when OS is started ..."
    check_ip_connect $NFS_SERVER
    if [ $? -ne 0 ]; then
        echo "Can not connect $NFS_SERVER" >> $NFS_LOG_PATH
        print_log ERROR "local can not connect $NFS_SERVER, please check the network..."
        return
    fi
 
    print_log INFO "start to config /etc/rc.local ..."
    chmod +x /etc/rc.d/rc.local
    grep -E '^mount.*share' /etc/rc.local &>/dev/null
    if [ $? != 0 ]; then
        cat >> /etc/rc.local <<eof
#mount nfs
mount $NFS_SERVER:$NFS_SERVER_SHARED_DIR $CLIENT_MOUNT_POINT
echo "starting os, init operation ---------" >> /var/log/nfs.log
#end
eof
    else
        print_log INFO "current /etc/rc.local has contained the auto mount configuration, will skip ..."
    fi
    print_log INFO "fininsh config /etc/rc.local..."
}
 
# 关机时自动卸载nfs
function auto_umount_when_offOS() {
    print_log INFO "start to config auto umount shared directory when OS is closed ..."
    touch /etc/init.d/unmountnfs
    print_log INFO "touched /etc/init.d/unmountnfs file ..."
    chmod 777 /etc/init.d/unmountnfs
    cat > /etc/init.d/unmountnfs <<"eof"
#!/bin/bash
DATE_FORMAT=`date "+%Y-%m-%d %H:%M:%S"`
echo $DATE_FORMAT >> /var/log/nfs.log
message="[`hostname`]-[$DATE_FORMAT]-[INFO]"
echo "$message: OS is shutting down, start unmout nfs mount point.. " >> /var/log/nfs.log
umount /root/share_dir
if [ $? -ne 0 ]; then
     umount -l -f /root/share_dir
fi
echo "$message: OS is shutting down, finish umout nfs mount point.. " >> /var/log/nfs.log
eof
    print_log INFO "appended umount operation to /etc/init.d/unmountnfs file ..."
 
    touch $CUSTOM_SERVICE_PATH
    # create systemd service
    cat > $CUSTOM_SERVICE_PATH <<"eof"
[Unit]
Description=run my scripts before poweroff and reboot
Before=poweroff.target halt.target reboot.target
DefaultDependencies=no
 
[Service]
Type=oneshot
ExecStart=/etc/init.d/unmountnfs
 
[Install]
WantedBy=poweroff.target halt.target reboot.target
eof
    print_log INFO "created systemd service $CUSTOM_SERVICE_PATH ..."
    systemctl enable $CUSTOM_SERVICE_NAME
    print_log INFO "finish auto umount nfs configuration when reboot/halt/poweroff ..."
}
 
# 设置定时任务监控挂载状态
function set_cron_monitor_mount() {
    # 创建定时任务脚本
    print_log INFO "start to create cron task file..."
    touch $CURRENT_PATH/nfs_check_1min.sh
    chmod 755 $CURRENT_PATH/nfs_check_1min.sh
    # 定时任务脚本业务逻辑处理
    cat > $CURRENT_PATH/nfs_check_1min.sh << "eof"
#!/bin/bash
DATE_FORMAT=`date "+%Y-%m-%d %H:%M:%S"`
DEFAULT_LOG_PATH=/var/log/nfs.log
print_log()
{
    log_level="INFO"
    if [[ ! $1 =~ 'INFO' ]] && [[ ! $1 =~ 'WARNING' ]] && [[ ! $1 =~ 'ERROR' ]] ; then
        echo "print log function wrong called, print_log first para must be log level(INFO、ERROR、WARNING)"
        exit 1
    fi
    if [ "$#" == "0" ] || [ "$#" == "1" ]; then
        echo "print log function wrong called"
        exit 1
    elif [ "$#" == "2" ]; then
        message="[`hostname`]-[$DATE_FORMAT]-[$1]:$2"
        echo $message >> $DEFAULT_LOG_PATH
        return 0
    fi
 
    if [ ! -e $3 ]; then
        temp_path=$(dirname $3)
        touch $3
        echo "print log function wrong called, the 3nd para must be a exist file path when the params length is 3"
    fi
 
    message="[`hostname`]-[$DATE_FORMAT]-[$1]:$2"
    echo $message
    echo $message >> $3
    return 0
}
 
# 网络连通性检查,五次检查都失败,直接卸载
failure_count=0
for (( i = 0; i < 5; i++ )); do
    ping -c 3 -i 0.2 -W 3 172.16.0.105 &> /dev/null
    if [ $? -eq 0 ]; then
        print_log INFO "net connection is normal,from local to nfs server..."
    else
        print_log INFO "net connection is unnormal,please check the network..."
        failure_count=`expr $failure_count + 1`
    fi
done
 
if [ "$failure_count" == "5" ]; then
    print_log INFO "net connection is unnormal,start to unmount ..."
    umount -f -l /root/share_dir
    print_log INFO "umount /root/share_dir successfully..."
    exit 1
fi
 
# showmount -e挂载点检查
count=`showmount -e 172.16.0.105 | wc -l`
if [ $count -eq 0 ]; then
    print_log INFO "There exists no shared dir on nfs server ..., start umount mountpoint"
    exit 1
else
    print_log INFO "shared directory normally..."
fi
 
# 挂载状态检查
read -t1 < <(stat -t "/root/share_dir")
if [ $? -eq 1 ]; then
   print_log EEROR "current nfs mount status is not normal, start to umount /root/share_dir..."
   umount -f -l /root/share_dir
   print_log INFO "umount /root/share_dir successfully..."
   exit 1
fi
 
# 所有状态均正常,如果没有挂在的情况下,执行挂载操作
filecount=$(ls /root/share_dir | wc -l)
if [ "$filecount" == "0" ]; then
   print_log INFO "current nfs is not mounted, remount 172.16.0.105:/root/share_dir /root/share_dir -o nolock..."
   mount 172.16.0.105:/root/share_dir /root/share_dir -o nolock
   if [ $? -ne 0 ]; then
      print_log ERROR "remount /root/share_dir failed..."
   fi
   print_log INFO "remount /root/share_dir successfully..."
fi
print_log INFO "nfs server is normal, mount status is NORMAL..."
eof
 
    # 配置定时任务
    print_log INFO "start to config crontab, check nfs / 1min..."
    grep "nfs_check_1min" /etc/crontab
    if [ $? -eq 0 ]; then
        exit 1
    fi
    echo "*/1 * * * * root $CURRENT_PATH/nfs_check_1min.sh" >> /etc/crontab
}
 
main()
{
    while [ ! -z "$1" ]
    do
        case "$1" in
                -h | *help)
                usage
                exit 0
                ;;
        esac
        shift
    done
    mount $NFS_SERVER:$NFS_SERVER_SHARED_DIR $CLIENT_MOUNT_POINT -o nolock
    if [ $? -ne 0 ]; then
      print_log ERROR "mount $NFS_SERVER:$NFS_SERVER_SHARED_DIR $CLIENT_MOUNT_POINT -o nolock failed..."
    fi
    auto_mount_when_onOS
    auto_umount_when_offOS
    set_cron_monitor_mount
}
 
main $*
exit 0
posted @ 2020-10-11 23:11  一介草民李八千  阅读(1166)  评论(0)    收藏  举报