#!/bin/sh
source /OSM/script/os_load_base.sh
PATCH_START()
{
    # 该补丁已被收编，PATCH_START 不执行 set_expiry 命令：
	OS_echo "hotpatch PATCH_START nfs expiry 1800 seconds skipped."
	
	# 正式补丁请执行如下流程：
#    mount | grep pum >/dev/null
#    if [ $? -eq 0 ] ; then
#        OS_echo "ready to hotpatch nfs expiry 1800 seconds"
#        nas_proto_pum -m nfs -c set_expiry -v 1800
#    	if [ $? -eq 0 ] ; then	   
#	        OS_echo "hotpatch PATCH_START, nfs expiry 1800 seconds successful"
#	    else
#	        OS_echo "hotpatch PATCH_START nfs expiry 1800 seconds failed"
#	    fi
#    else
#        OS_echo "once hotpatch PATCH_START nfs expiry 1800 seconds, no need to execute any more after that, for example when reboot"
#    fi
}
PATCH_END()
{
	#新增判断 如果文件大小为0，则拷贝基线进程过来。解决小补丁大小超过1M限制方案。
	if [ -f "/OSM/bin/rpc.mountd" ]; then
		local FILE_SIZE=`du -sb /OSM/bin/rpc.mountd | awk '{print $1}'`
		if [ $FILE_SIZE -eq 0 ]; then
			cp /OSM/bin/rpc.mountd_origin /OSM/bin/rpc.mountd
			OS_echo "rpc.mountd: The rpc.mountd size is $FILE_SIZE."
			return 0
		fi
		OS_echo "rpc.mountd: The rpc.mountd size is $FILE_SIZE."
	fi
    chmod 775 /OSM/bin/rpc.mountd
    mv /OSM/bin/rpc.mountd /OSM/bin/rpc.mountd.tgz
    tar zxf /OSM/bin/rpc.mountd.tgz -C /OSM/bin
    chmod 500 /OSM/bin/rpc.mountd
    rm -f /OSM/bin/rpc.mountd.tgz

    checkproc rpc.mountd
    if [ $? -eq 0 ] ;then
		nas_proto_pum -m nfs -c get_slow_io_percent -v upgrade
		if [ $? -eq 0 ] ; then	
			OS_echo "rpc.mountd hotpatch PATCH_END, set nfs upgrade status successful"
	    else
	        OS_echo "rpc.mountd hotpatch PATCH_END set nfs upgrade status failed"
	    fi
        if [ -f /var/run/rpc.mountd.pid ];then
            sudo rm /var/run/rpc.mountd.pid 
        fi
        pid=`pidof rpc.mountd`
        sudo kill -9 $pid 
        cnt=0
        while [ $cnt -lt 3 ]
        do
            cnt=`expr $cnt + 1`
            checkproc rpc.mountd
            if [ $? -eq 0 ] ;then	
                OS_echo "kill rpc.mountd failed, cnt is $cnt."
                sleep 1
            else
                setsid /OSM/bin/rpc.mountd -p 2050 &
                OS_echo "hotpatch need kill rpc.mountd in SPH030"
                cnt=3
            fi
        done  
		sleep 30
		nas_proto_pum -m nfs -c get_slow_io_percent -v downgrade
		if [ $? -eq 0 ] ; then	
			OS_echo "rpc.mountd hotpatch PATCH_END, set nfs downgrade status successful"
	    else
	        OS_echo "rpc.mountd hotpatch PATCH_END set nfs downgrade status failed"
	    fi
    else
        OS_echo "hotpatch need not kill rpc.mountd in SPH030"
    fi
}
PATCH_ROLLBACK()
{
    mount | grep pum >/dev/null
    if [ $? -eq 0 ] ; then
        OS_echo "ready to hotpatch nfs expiry 1800 seconds"
        nas_proto_pum -m nfs -c set_expiry -v 1800
    	if [ $? -eq 0 ] ; then	   
	        OS_echo "rpc.mountd hotpatch PATCH_ROLLBACK nfs expiry 1800 seconds successful"
	    else
	        OS_echo "rpc.mountd hotpatch PATCH_ROLLBACK nfs expiry 1800 seconds failed"
	    fi
    else
        OS_echo "once hotpatch PATCH_ROLLBACK nfs expiry 1800 seconds, no need to execute any more after that, for example when reboot"
    fi
}
PATCH_ROLLEND()
{
	#新增判断 如果文件大小为0，则拷贝基线进程过来。解决小补丁大小超过1M限制方案。
	if [ -f "/OSM/bin/rpc.mountd" ]; then
		local FILE_SIZE=`du -sb /OSM/bin/rpc.mountd | awk '{print $1}'`
		if [ $FILE_SIZE -eq 0 ]; then
			cp /OSM/bin/rpc.mountd_origin /OSM/bin/rpc.mountd
			OS_echo "rpc.mountd: The rpc.mountd size is $FILE_SIZE."
			return 0
		fi
		OS_echo "rpc.mountd: The rpc.mountd size is $FILE_SIZE."
	fi

    chmod 775 /OSM/bin/rpc.mountd
	
    tar zxf /OSM/bin/rpc.mountd -C /OSM/bin 1> /dev/null 2>&1	
	if [ $? -eq 0 ] ;then
	    OS_echo "Tar zipped rpc.mountd success."
	fi	
    
    chmod 500 /OSM/bin/rpc.mountd
    
    checkproc rpc.mountd
    if [ $? -eq 0 ] ;then
		nas_proto_pum -m nfs -c get_slow_io_percent -v upgrade
		if [ $? -eq 0 ] ; then	
			OS_echo "rpc.mountd hotpatch PATCH_ROLLEND, set nfs upgrade status successful"
	    else
	        OS_echo "rpc.mountd hotpatch PATCH_ROLLEND set nfs upgrade status failed"
	    fi
        if [ -f /var/run/rpc.mountd.pid ];then
            sudo rm /var/run/rpc.mountd.pid 
        fi
        pid=`pidof rpc.mountd`
        sudo kill -9 $pid 
        cnt=0
        while [ $cnt -lt 3 ]
        do
            cnt=`expr $cnt + 1`
            checkproc rpc.mountd
            if [ $? -eq 0 ] ;then	
                OS_echo "kill rpc.mountd failed, cnt is $cnt."
                sleep 1
            else
                setsid /OSM/bin/rpc.mountd -p 2050 &
                OS_echo "hotpatch need kill rpc.mountd in SPH030"
                cnt=3
            fi
        done     
		sleep 30
		nas_proto_pum -m nfs -c get_slow_io_percent -v downgrade
		if [ $? -eq 0 ] ; then	
			OS_echo "rpc.mountd hotpatch PATCH_ROLLEND, set nfs downgrade status successful"
	    else
	        OS_echo "rpc.mountd hotpatch PATCH_ROLLEND set nfs downgrade status failed"
	    fi		
    else
        OS_echo "hotpatch need not kill rpc.mountd in SPH030"
    fi
}
L_CMD=$1
case $L_CMD in
    "start") PATCH_START
               ;;
    "end") PATCH_END
               ;;
    "rollstart") PATCH_ROLLBACK
               ;;
    "rollend") PATCH_ROLLEND
  ;;
    *)
        OS_echo "unsupported option."
esac
exit 0