#!/bin/bash

# **************************************************************************** #
# File Name: os_upgrade_hotpacth.sh
# Description: 热补丁升级操作接口:安装、确认、回退、删除、查询版本以及系统重启
#              后恢复热补丁功能等。
# Author: 
# Created: 2009.11.24
# Function List：
# History
# 1.Date:
#   Author:
#   Modification: 2011.12.14
# **************************************************************************** #
source /OSM/script/os_hotpatch.sh

#R2 P11G-4082 modified2012/02/05 begin
#根据热补丁新需求重新实现热补丁管理功能

#安装/回退/删除前检查的错误码(与om协商的公共错误码,新增或修改以下错误码需要知会到om)
G_OS_HP_RET_SUCC=0               #操作成功
G_OS_HP_RET_PACKET_ERR=1            #一致性检测失败
G_OS_HP_RET_VERSION_ERR=2           #基础版本不一致
G_OS_HP_RET_REINSTALL_ERR=3         #热补丁包重复安装
G_OS_HP_RET_MEMORY_ERR=4            #热补丁区内存不够
G_OS_HP_RET_SYS_DISK_ERR=5          #系统盘错误
G_OS_HP_RET_SYS_BUSY=6              #当前系统忙无法安装热补丁
G_OS_HP_RET_NO_INSTALL=7            #当前系统没有热补丁包
G_OS_HP_RET_CUR_HP_ERROR=8          #当前热补丁包状态错误
G_OS_HP_RET_REROLLBACK_ERR=9        #重复回退热补丁包,即当前没有可回退的热补丁包
G_OS_HP_RET_SPECIAL_PATCH=10        #特殊补丁包,解决安全红线进入DEBU模式问题
G_OS_HP_RET_RELATIONSHIP_ERR=11     #补丁包含关系错误

#补丁包解压的错误码与升级保持一致
G_ERROR_PKG_INVALIDE=3              
G_ERROR_PKG_INEXISTENCE=4           
#需要使用的全局变量
G_PROCESS_RET=
G_CHECK_MEM_SIZE=
G_PREPARE_TYPE=
G_PREPARE_PKG=
G_HOTPATH_PKG_NAME=
G_IS_MOUNT=
G_MIN_CPU_USING=75
G_CHECK_CPU_INTERVAL=3
#R2R5&R5C01 P12I-3843 modified2013/1/11 begin
G_HOTPATCH_STATUS=/tmp/hotpatch_status_tmp
#R2R5&R5C01 P12I-3843 modified2013/1/11 begin
G_OS_CP_HOOK="${G_OS_HP_DIR_TEMP}/os_hotpatch_hook.sh"
G_OS_HP_OSP_DIR=/startup_disk/image/boot_osp/hotpatch
BACKUP_DIR=/OSM/ccdb/store/soft_backup
G_BOOT_LINK=/startup_disk/image/boot

G_UPD_HP_RESUME_FLAG_FILE="/tmp/upd_hotpatch_resume.log"   #升级过程补丁恢复流程记录文件
G_UPDATE_TMP_DIR=/startup_disk/image/boot_osp_tmp      #升级包解压后存放目录
G_BACKUP_PATCH_VER=SPH030

# **************************************************************************** #
# Function Name: OS_UPG_HP_HELP
# Description:使用帮助信息
# Parameter: none
# Return: none
# **************************************************************************** #
OS_UPG_HP_HELP()
{
    echo "usage: os_upgrade_hotpatch.sh [-m size] [-w install|roll|delete] [-p package name [mini] ] [-g package name] [-c] [-r] [-k] [-d] [-s [1|2] ] [-v all|local|peertolocal] [-h]"
    echo "-m : check memory size"
    echo "-w : prepare to install,delete,rollback hotpatch"
    echo "-p : install hotpatch"
    echo "-c : confirm hotpatch"
    echo "-r : rollback hotpatch"
    echo "-d : delete all hotpatch"
    echo "-s : when system restart, resume the hotpatch"
    echo "-v : get the hotpatch version"
    echo "-k : delete the all files in update dir"
    echo "-g : check special patch"
    echo "-h : help"
    return 0
}

# **************************************************************************** #
# Function Name: OS_SetStartUpDiskProperty
# Description: set thd disk writable or readonly
# Parameter: $1 is the block device name of startup disk
#            $2 is the mount property of image
# Return: none
# **************************************************************************** #
OS_SetStartUpDiskProperty()
{
    #检查是否被mount,如果被mount,就把它remount成ro或rw ,否则重新mount
    L_DISK_BOOT=$1
    L_DISK_PROPERTY=$2
    L_PARTITION_BOOT=`grep -w "$L_DISK_BOOT" /etc/fstab | awk '{print $1}'`

    if [ -z "$L_PARTITION_BOOT" ]; then
        OS_echo "Can not find $L_DISK_BOOT partition in fstab"
        return 1
    fi
    if [ "$L_DISK_PROPERTY" != "rw" -a "$L_DISK_PROPERTY" != "ro" ] ; then
        OS_echo "Arg error :$2"
        return 1
    fi
    # add for rw,async
    L_SYNC_ASYNC=async
    if [ "$L_DISK_PROPERTY" = "ro" ] ; then 
        L_SYNC_ASYNC=sync
    fi 

    if [ "$L_DISK_PROPERTY" = "rw" ] ; then 
        L_SYNC_ASYNC=async
    fi 
    # add for rw,async

    L_IS_MOUNT=`cat /proc/mounts |grep "$L_DISK_BOOT" `
    if [ -z "$L_IS_MOUNT" ] ; then
        # mount 指定的属性
    # add for rw,async
        mount -o $L_DISK_PROPERTY,$L_SYNC_ASYNC,noatime,nodiratime ${L_PARTITION_BOOT} $L_DISK_BOOT 2>/dev/null
    # add for rw,async
        if [ $? != 0 ] ; then
            OS_echo "Can not mount disk:$1, Error!"
            return 1
        fi
    else
        L_RW=`cat /proc/mounts |grep "$L_DISK_BOOT"|awk '{print $4}'|awk -F ,  '{print $1}'`
        if [ "$L_RW" != "$L_DISK_PROPERTY" ];then
            L_MOUNT_ERROR=`mount -o remount,$L_DISK_PROPERTY,$L_SYNC_ASYNC $L_DISK_BOOT 2>/dev/stdout`
            L_MOUNT_RET=$?
            if [ "$L_MOUNT_RET" != "0" ];then
                OS_echo "$L_MOUNT_ERROR"
                OS_echo "Error[$L_MOUNT_RET], can not remount and try to recover it"
                ##umount
                L_UMOUNT_ERROR=`umount $L_DISK_BOOT 2>/dev/stdout`
                L_UMOUNT_RET=$?
                if [ "$L_UMOUNT_RET" != "0" ];then
                    OS_echo "$L_UMOUNT_ERROR"
                    OS_echo "Error [$L_UMOUNT_RET], umount again..."
                    L_PARTITION=`cat /proc/mounts |grep "$L_DISK_BOOT"|awk '{print $1}'`
                    OS_echo "fuser -kvm $L_PARTITION"
                    fuser -kvm $L_PARTITION &>/dev/kmsg
                    sleep 2
                    L_UMOUNT_ERROR=`umount $L_DISK_BOOT 2>/dev/stdout`
                    L_UMOUNT_RET=$?
                    if [ "$L_UMOUNT_RET" != "0" ];then
                        OS_echo "$L_UMOUNT_ERROR"
                        OS_echo "Error[$L_UMOUNT_RET],fail again, please check"
                    fi
                fi
                ##if umount success,then recover mounting
                if [ "$L_UMOUNT_RET" = "0" ];then
                    L_MOUNT_ERROR=`mount -o $L_DISK_PROPERTY,$L_SYNC_ASYNC,noatime,nodiratime ${L_PARTITION_BOOT} $L_DISK_BOOT 2>/dev/stdout`
                    L_MOUNT_RET=$?
                fi
            fi
            ##recovering fail
            if [ "$L_MOUNT_RET" != "0" ];then
                OS_echo "$L_MOUNT_ERROR"
                OS_echo "Error[$L_MOUNT_RET], recovering fail and can not mount disk:$L_PARTITION_BOOT"
                return 1
            fi
        fi
    fi

    return 0
}
# **************************************************************************** #
# Function Name: OS_UPG_HP_CHECK_MEM
# Description:系统剩余内存检查
# Parameter: 期望的内存大小
# Return: 0表示当前内存满足升级要求
#             1表示当前内存不够,系统不能升级热补丁
# **************************************************************************** #
OS_UPG_HP_CHECK_MEM()
{
    local L_MIN_MEM=$1
    local L_CUR_FREE=
    local L_PID=
    local L_NUM=
    local L_STATE=
    local L_RET=0
    
    OS_echo "begin to check system memory[${L_MIN_MEM}]."
    expr ${L_MIN_MEM} + 0 &>/dev/null
    if [ $? -ne 0 ];then
        OS_echo "Parameter illegality."
        return 1
    fi
    echo 3 >/proc/sys/vm/drop_caches
    L_CUR_FREE=`free -m| grep ^Mem| awk '{print $4}'`
    if [ ${L_CUR_FREE} -lt ${L_MIN_MEM} ];then
        OS_echo "current system memory[${L_CUR_FREE}] do not reach ${L_MIN_MEM}."
        L_RET=1
        
        L_PID=`ps -ef|grep -w "os_upgrade_hotpatch.sh -m"|grep -v grep|awk '{print $2}'`
        L_NUM=`echo "${L_PID}" |wc -w`
        L_STATE=`ps -ef |grep "os_upgrade_hotpatch.sh -m ${L_MIN_MEM}"|grep -v grep`
            
        if [ ${L_NUM} -gt 1 ];then
            OS_echo "release memory has already run."
            OS_echo "process hotpatch pid=${L_PID}"
            OS_echo "process hotpatch state:${L_STATE}"
        else
            OS_echo "need to release memory."
            OS_FREEMEM_TO_SPECIFY_SIZE "${L_MIN_MEM}" &
        fi
    fi
    
    OS_echo "end to check system memory[cur:${L_CUR_FREE}]."
    return ${L_RET}
}

# **************************************************************************** #
# Function Name: OS_UPG_HP_CHECK_DIR_ROLLBACK
# Description:回退前校验相关目录是否正确
# Parameter: 
# Return:  G_OS_HP_RET_SUCC=0                           操作成功
#              G_OS_HP_RET_PACKET_ERR=1               一致性检测失败
#              G_OS_HP_RET_NO_INSTALL=7            当前系统没有热补丁包
#              G_OS_HP_RET_CUR_HP_ERROR=8        当前热补丁包状态错误
# **************************************************************************** #
OS_UPG_HP_CHECK_DIR_ROLLBACK()
{
    local L_RET=
    
    OS_echo "begin to check rollback hotpatch directory."
    
    #检查链接是否正常
    OS_HP_CHECK_LINK
    L_RET=$?
    if [ 0 -ne ${L_RET} ];then
        OS_echo "check link failed, do not rollback."
        return ${G_OS_HP_RET_CUR_HP_ERROR}
    fi
    
    #是否允许回退:系统没有安装热补丁不能回退,且不允许热补丁连续回退
    OS_HP_IS_PERMIT_ROLLBACK
    L_RET=$?
    if [ 0 -ne ${L_RET} ];then
        if [ ${G_OS_HP_RESULT_NO_HOTPATCH} -eq ${L_RET} ];then
            L_RET=${G_OS_HP_RET_NO_INSTALL}
            OS_echo "there is no hotpatch, do not rollback."
        else
            L_RET=${G_OS_HP_RET_REROLLBACK_ERR}
            OS_echo "system has already been rollback, do not rollback hotpatch."
        fi
        return ${L_RET}    
    fi

    #校验 G_OS_HP_DIR_EMERGENCY
    if [ -d "${G_OS_HP_DIR_EMERGENCY}" ];then
        OS_HP_CHECK_MD5 "${G_OS_HP_DIR_EMERGENCY}"
        L_RET=$?
        #紧急热补丁中肯定要有sha256校验文件,校验失败返回检查失败
        if [ 0 -ne ${L_RET} ];then
            OS_echo "check hotpatch[${G_OS_HP_DIR_EMERGENCY}] sha256 failed, do not rollback."
            return ${G_OS_HP_RET_CUR_HP_ERROR}
        fi
    fi    
    
    #校验G_OS_HP_DIR_CUR
    if [ -d ${G_OS_HP_DIR_CUR} ];then
        OS_HP_CHECK_MD5 "${G_OS_HP_DIR_CUR}"
        L_RET=$?
        #当前的热补丁中肯定要有sha256校验文件,校验失败返回检查失败
        if [ 0 -ne ${L_RET} ];then
            OS_echo "check hotpatch[${G_OS_HP_DIR_CUR}] sha256 failed, do not rollback."
            return ${G_OS_HP_RET_CUR_HP_ERROR}
        fi
    fi
    
    #校验G_OS_HP_DIR_BAK
    OS_HP_CHECK_MD5 "${G_OS_HP_DIR_BAK}"
    L_RET=$?
    #首次安装bak目录中没有sha256文件,因此返回G_OS_HP_RESULT_FAIL才表示校验失败
    if [ ${G_OS_HP_RESULT_FAIL} -eq ${L_RET} ];then
        OS_echo "check hotpatch[${G_OS_HP_DIR_BAK}] sha256 failed, do not rollback."
        return ${G_OS_HP_RET_PACKET_ERR}
    fi
    
    OS_echo "check rollback hotpatch directory complete."
    return 0
}

G_DIAG_RESULT=/tmp/upd_hot_diag_result
# **************************************************************************** #
# Function Name: OM_ExecuteMMLCommand
# Description: 执行diagnose命令
# Parameter:
# Return:
# **************************************************************************** #
OM_ExecuteDiagnoseCommand()
{ 
    rm -rf $G_DIAG_RESULT
    OS_echo "exec diagnose "
(
diagnose --auto <<EOF > $G_DIAG_RESULT
$1
quit
EOF
) 
return 0
}
# **************************************************************************** #
# Function Name: UPG_CHECK_MODULE_CAN_ROLLBACK_REPHV
# Description:检查业务模块是否可以卸载，当前给复制检查
# Parameter: 
# Return:  0  成功 1   失败
# **************************************************************************** #
UPG_CHECK_MODULE_CAN_ROLLBACK_REPHV()
{
    local bDel=$1
    
    #回滚到的补丁是否包含复制新功能
    if [ "$bDel" == "roll" ] && [ -f $G_OS_HP_DIR_BAK/klp_DTS202101230L3M20P0D00.tar.gz ];then
        OS_echo "no need check rephv" >/dev/null
        return 0
    fi
    
    OS_echo "Begin to check whether can rollback rephv" >/dev/null
    OM_ExecuteDiagnoseCommand "rephv vault getMaxCopySwitch"
    local ret=`cat $G_DIAG_RESULT |grep "connect to server fail"`
    if [ -n "$ret" ];then
        OS_echo "cannot exec rephv diagnose" >/dev/null
        rm -rf $G_DIAG_RESULT
        return 1
    fi
    
    ret=`cat $G_DIAG_RESULT |grep "isSetMaxCopySwitch   :1"`
    if [ -n "$ret" ];then
        OS_echo "rephv cannot del" >/dev/null
        rm -rf $G_DIAG_RESULT
        return 1
    fi
    
    rm -rf $G_DIAG_RESULT
    return 0
}

# **************************************************************************** #
# Function Name: OS_UPG_HP_CHECK_DIR_DEL
# Description:删除前校验相关目录是否正确
# Parameter: 
# Return:  G_OS_HP_RET_SUCC=0                           操作成功
# **************************************************************************** #
OS_UPG_HP_CHECK_DIR_DEL()
{
    local L_TEMP=
    local L_LAST_VER=
    local L_RET=0
    
    #链接文件不存在时再检查版本是否正确
    if [ ! -L "${G_OS_HP_DIR_LINK}" ];then
        OS_echo "hotpatch[${G_OS_HP_DIR_LINK}] is not exist, maybe not delete."
        L_LAST_VER=`OS_GetHPVer`
        if [ -z "${L_LAST_VER}" ];then
            OS_echo "link is not exist and no hotpatch version, do not delete."
        else
            OS_echo "link is not exist, but hotpatch version is ${L_LAST_VER}, must delete."
        fi
    fi
    
    OS_echo "check delete hotpatch directory complete."
    return 0
}

# **************************************************************************** #
# Function Name: OS_UPG_HP_CHECK_DIR_UPGRADE
# Description:安装前校验相关目录是否正确
# Parameter: 
# Return:  G_OS_HP_RET_SUCC=0                           操作成功
#             G_OS_HP_RET_CUR_HP_ERROR=8          当前热补丁包状态错误
# **************************************************************************** #
OS_UPG_HP_CHECK_DIR_UPGRADE()
{
    local L_RET=
    
    #检查链接是否正常
    OS_HP_CHECK_LINK
    L_RET=$?
    if [ 0 -ne ${L_RET} ];then
        OS_echo "check hotpatch link failed, do not upgrade."
        return ${G_OS_HP_RET_CUR_HP_ERROR}
    fi
 
    #校验当前版本
    OS_HP_CHECK_MD5 "${G_OS_HP_DIR_LINK}"
    L_RET=$?
    #首次安装没有链接文件,因此不存在sha256文件,因此返回G_OS_HP_RESULT_FAIL才表示校验失败,而返回G_OS_HP_RESULT_MD5_NOT_EXIST正常
    if [ ${G_OS_HP_RESULT_FAIL} -eq ${L_RET} ];then
        OS_echo "check hotpatch[${G_OS_HP_DIR_LINK}] sha256 failed, do not upgrade."
        return ${G_OS_HP_RET_CUR_HP_ERROR}
    fi
    
    OS_echo "check upgrade hotpatch directory complete."
    return 0
}

# **************************************************************************** #
# Function Name: UPG_CHECK_MODULE_CAN_ROLLBACK
# Description:检查业务模块是否可以回滚，当前检查BIOS是否已升级
# Parameter: 
# Return:  0  成功 1   是吧
# **************************************************************************** #
UPG_CHECK_MODULE_CAN_ROLLBACK()
{
    local ltype=$1
    local biosflag=0
    local L_PRODUCT_TYPE=$(grep "Product Name is" /proc/osp/bsp | cut -d' ' -f4 | tr 'a-z' 'A-Z')
    if [ -f /startup_disk/image/boot/update_pkg/${L_PRODUCT_TYPE}/bios/bios_bak.ROM ]; then 
        biosflag=1
    fi
    
    if [ "$ltype" == "del" ] && [ $biosflag == 1 ];then
        OS_echo "please resumebios before delete hotpatch"
        return 1    
    fi
    
    #需要判断回滚的补丁版本和当前生效的bios版本，低则说明bios生效了需要限制回退，DMI文件格式V500R007C61SPH018BiosFlag取出018，一个SPC版本标记只会有一个
    #可能共存这两个文件V500R007C61SPH018ResumeBiosFlag  V500R007C61SPH018BiosFlag
    if [ "$ltype" == "roll" ] && [ $biosflag == 1 ];then
        #spc版本号中剔除SPC，V500R007C30SCP100取出V500R007C30
        local spc_version=`cat /OSM/conf/versions.conf |grep -m 1 SpcVersion |awk -F"=" '{print $2}'|awk -F"SPC" '{print $1}'`
        local bios_version=`ls /startup_disk/conf/conf_local/ | grep "BiosFlag" |grep "$spc_version" | grep -v "Resume" | awk -F'SPH' '{print $2}'| awk -F'Bios' '{print $1}'`
        if [ -z "$bios_version" ];then
            OS_echo "no bios flag, may be wrong"
            return 1 
        fi
        local sph_version=`cat $G_OS_HP_DIR_BAK/$G_OS_HP_CONF | grep ^Version | awk -F'SPH' '{print $2}'`
        if [ -z "$sph_version" ];then
            OS_echo "no roll version, may be wrong"
            return 1 
        fi
        OS_echo "bios version is $bios_version, roll version is $sph_version"
        if [ "$bios_version" \> "$sph_version" ];then
            OS_echo "please resumebios before roll hotpatch"
            return 1    
        fi
    fi
    
    if [ -f /OSM/script/numa_check_hotpatch_can_rollback.sh ];then
        local sph_version=""
        if [ -f $G_OS_HP_DIR_BAK/$G_OS_HP_CONF ];then
            sph_version=`cat $G_OS_HP_DIR_BAK/$G_OS_HP_CONF | grep ^Version | awk -F'SPH' '{print $2}'`
        fi
        #del不用判版本号。roll需要判版本号，版本号为空也报错。 
        /OSM/script/numa_check_hotpatch_can_rollback.sh $ltype $sph_version
        if [ 0 -ne $? ];then
            OS_echo "check numa failed, do not rollback hotpatch."
            return 1
        fi        
    fi

    if [ -f /OSM/script/readdirplus_switch_check_SPH033_DTS2022032900900.sh ];then
        local sph_version=""
        if [ -f $G_OS_HP_DIR_BAK/$G_OS_HP_CONF ];then
            sph_version=`cat $G_OS_HP_DIR_BAK/$G_OS_HP_CONF | grep ^Version | awk -F'SPH' '{print $2}'`
        fi 
        #del不用判版本号。roll需要判版本号，版本号为空也报错。 
        /OSM/script/readdirplus_switch_check_SPH033_DTS2022032900900.sh $ltype $sph_version
        if [ 0 -ne $? ];then
            OS_echo "check readdirplus switch failed, do not rollback hotpatch."
            return 1
        fi 
    fi

    if [ -f /OSM/script/cpld_check_hotpatch_can_rollback.sh ];then
        local sph_version=""
        if [ -f $G_OS_HP_DIR_BAK/$G_OS_HP_CONF ];then
            sph_version=`cat $G_OS_HP_DIR_BAK/$G_OS_HP_CONF | grep ^Version | awk -F'SPH' '{print $2}'`
        fi 
        #del不用判版本号。roll需要判版本号，版本号为空也报错。 
        /OSM/script/cpld_check_hotpatch_can_rollback.sh $ltype $sph_version
        if [ 0 -ne $? ];then
            OS_echo "check cpld failed, do not rollback hotpatch."
            return 1
        fi 
    fi
    return 0
}
# **************************************************************************** #
# Function Name: OS_UPG_HP_ROLLBACK_CHECK
# Description:回退前的检查工作
# Parameter: 
# Return:  G_OS_HP_RET_SUCC=0                           操作成功
#              G_OS_HP_RET_PACKET_ERR=1               一致性检测失败
#              G_OS_HP_RET_SYS_BUSY=6                 当前系统忙无法安装热补丁
#              G_OS_HP_RET_NO_INSTALL=7            当前系统没有热补丁包
#              G_OS_HP_RET_CUR_HP_ERROR=8        当前热补丁包状态错误
# **************************************************************************** #
OS_UPG_HP_ROLLBACK_CHECK()
{
    local L_BOOT_DISK=
    local L_RET=
    
    OS_CHECK_BACKUP_DIR_BEFORE_ROLLBACK
    L_RET=$?
    if [ 0 -ne ${L_RET} ];then
        OS_echo "check coffer_data failed, do not rollback hotpatch."
        return ${L_RET}
    fi
    
    #系统cpu利用率是否正常
    OS_HP_CHECK_CPU
    L_RET=$?
    if [ 0 -ne ${L_RET} ];then
        OS_echo "check cpu using failed, do not rollback hotpatch."
        return ${G_OS_HP_RET_SYS_BUSY}
    fi   
    
    #检查热补丁目录是否正常
    OS_UPG_HP_CHECK_DIR_ROLLBACK
    L_RET=$?
    if [ 0 -ne ${L_RET} ];then
        OS_echo "check hotpatch directory for rollback failed[${L_RET}], do not rollback."
        return ${L_RET}
    fi
    
    UPG_CHECK_MODULE_CAN_ROLLBACK_REPHV roll
    L_RET=$?
    if [ 0 -ne ${L_RET} ];then
        return ${G_OS_HP_RET_CUR_HP_ERROR}
    fi
    
    #检查业务模块是否可以回滚
    UPG_CHECK_MODULE_CAN_ROLLBACK roll
    L_RET=$?
    if [ 0 -ne ${L_RET} ];then
        OS_echo "check module rollback failed, do not rollback hotpatch."
        return ${G_OS_HP_RET_CUR_HP_ERROR}
    fi
    OS_echo "check to rollback hotpatch complete."
    return 0
}

# **************************************************************************** #
# Function Name: OS_UPG_HP_DEL_CHECK
# Description:删除前的检查工作
# Parameter: 
# Return:  G_OS_HP_RET_SUCC=0                           操作成功
#              G_OS_HP_RET_SYS_BUSY=6                 当前系统忙无法安装热补丁
#              G_OS_HP_RET_NO_INSTALL=7            当前系统没有热补丁包        
# **************************************************************************** #
OS_UPG_HP_DEL_CHECK()
{
    local L_RET=
    L_RET=`mount | grep ${G_HOT_PATCH_BACKUP_ROOT_DIR} | grep "rw,"`
    if [ -z "$L_RET" ];then
        OS_echo "The coffer_data dir is not rw."
        return ${G_OS_HP_RET_SYS_DISK_ERR}
    fi
    
    #检查业务模块是否可以回滚
    UPG_CHECK_MODULE_CAN_ROLLBACK del
    L_RET=$?
    if [ 0 -ne ${L_RET} ];then
        OS_echo "check module rollback failed, do not del hotpatch."
        return ${G_OS_HP_RET_CUR_HP_ERROR}
    fi
    
    #系统cpu利用率是否正常
    OS_HP_CHECK_CPU
    L_RET=$?
    if [ 0 -ne ${L_RET} ];then
        OS_echo "check cpu using failed, do not delete hotpatch."
        return ${G_OS_HP_RET_SYS_BUSY}
    fi

    #检查系统当前是否已经安装了热补丁
    OS_UPG_HP_CHECK_DIR_DEL
    
    UPG_CHECK_MODULE_CAN_ROLLBACK_REPHV del
    L_RET=$?
    if [ 0 -ne ${L_RET} ];then
        return ${G_OS_HP_RET_CUR_HP_ERROR}
    fi
    
    OS_echo "check to delete hotpatch complete." 
    return 0
}

# **************************************************************************** #
# Function Name: OS_UPG_HP_UPGRADE_CHECK
# Description:安装前的检查工作
# Parameter: 热补丁包名( 传入参数为/OSM/update/hotpatch.tgz)
# Return: G_OS_HP_RET_SUCC=0                           操作成功
#             G_OS_HP_RET_PACKET_ERR=1               一致性检测失败
#             G_OS_HP_RET_VERSION_ERR=2           基础版本不一致
#             G_OS_HP_RET_REINSTALL_ERR=3        热补丁包重复安装
#             G_OS_HP_RET_MEMORY_ERR=4            热补丁区内存不够
#             G_OS_HP_RET_SYS_DISK_ERR=5          系统盘错误
#             G_OS_HP_RET_SYS_BUSY=6                 当前系统忙无法安装热补丁
#             G_OS_HP_RET_CUR_HP_ERROR=8          当前热补丁包状态错误
# **************************************************************************** #
OS_UPG_HP_UPGRADE_CHECK()
{
    local L_UPG_HP_NAME=$1
    local L_KER_CUR_MEM=0
    local L_KER_PKG_MEM=0
    local L_TMP=0
    local L_RET=
    
    OS_CHECK_BACKUP_DIR
    if [ "$?" != "0" ];then
        OS_echo "check coffer_data failed, do not upgrade hotpatch."
        return ${G_OS_HP_RET_SYS_DISK_ERR}
    fi
    
    ###如果当前系统存在临时补丁，则不允许升级
    OS_CheckEHPFlag ${G_OS_HP_DIR_LINK_CONF}
    if [ $? -eq 0 ];then        
        OS_echo "the current hotpatch is emergency hotpatch, please rollback to upgrade."
        return ${G_OS_HP_RET_REINSTALL_ERR}    
    fi
 
    #系统cpu利用率是否正常
    OS_HP_CHECK_CPU
    L_RET=$?
    if [ 0 -ne ${L_RET} ];then
        OS_echo "check cpu using failed, do not upgrade hotpatch."
        return ${G_OS_HP_RET_SYS_BUSY}
    fi

    #检查热补丁包是否存在
    if [ ! -e ${L_UPG_HP_NAME} ];then
        OS_echo "the hotpatch package[${L_UPG_HP_NAME}] is not exist, do not upgrade."
        return ${G_OS_HP_RET_NO_INSTALL}
    fi
    OS_echo "check hotpatch package complete."
    
    #检查当前正在运行的热补丁是否正常
    OS_UPG_HP_CHECK_DIR_UPGRADE
    L_RET=$?
    if [ 0 -ne ${L_RET} ];then
        OS_echo "check hotpatch directory failed[${L_RET}], do not upgrade."
        return ${L_RET}
    fi
    OS_HP_CHECK_CUR_VER   
    L_RET=$?
    if [ ${G_OS_HP_RESULT_CUR_HP_ERROR} -eq ${L_RET} ];then
        OS_echo "check the current hotpatch error, do not upgrade hotpatch."
        return ${G_OS_HP_RET_CUR_HP_ERROR}
    fi
    #临时解压目录不存在则创建,否则删除下面所有的文件    
    if [ ! -d "${G_OS_HP_DIR_TEMP}" ];then
        mkdir -p ${G_OS_HP_DIR_TEMP} &>/dev/kmsg
        L_RET=$?
        if [ 0 -ne ${L_RET} ];then
            OS_echo "create hotpatch directory[${G_OS_HP_DIR_TEMP}] failed[${L_RET}], do not upgrade."
            return ${G_OS_HP_RET_SYS_DISK_ERR}
        fi
    else
        rm -rf ${G_OS_HP_DIR_TEMP}/* &>/dev/kmsg
        L_RET=$?
        if [ 0 -ne ${L_RET} ];then
            OS_echo "remove hotpatch files[${G_OS_HP_DIR_TEMP}] failed[${L_RET}], do not upgrade."
            return ${G_OS_HP_RET_SYS_DISK_ERR}
        fi
    fi  
    OS_echo "check hotpatch temp diretory[${G_OS_HP_DIR_TEMP}] complete."
    
    #解压缩热补丁包文件到目录下
    tar -xzvf ${L_UPG_HP_NAME} -C ${G_OS_HP_DIR_TEMP} &>/dev/kmsg
    L_RET=$?
    if [ 2 -eq ${L_RET} ];then
        OS_echo "decompress hotpatch[${L_UPG_HP_NAME}] to ${G_OS_HP_DIR_TEMP} failed, do not upgrade."
        OS_echo "perhaps the hotpatch is wrong, do not upgrade."
        rm -rf ${G_OS_HP_DIR_TEMP} &>/dev/kmsg
        return ${G_OS_HP_RET_PACKET_ERR}
    elif [ 0 -ne ${L_RET} ];then
        OS_echo "decompress hotpatch[${L_UPG_HP_NAME}] to ${G_OS_HP_DIR_TEMP} failed, do not upgrade."
        rm -rf ${G_OS_HP_DIR_TEMP} &>/dev/kmsg
        return ${G_OS_HP_RET_PACKET_ERR}
    fi
    OS_echo "decompress hotpatch package to ${G_OS_HP_DIR_TEMP} complete."
    
    #sha256文件校验
    OS_HP_CHECK_MD5 "${G_OS_HP_DIR_TEMP}"
    L_RET=$?
    if [ 0 -ne ${L_RET} ];then
        OS_echo "check hotpatch[${G_OS_HP_DIR_TEMP}] sha256sum failed, do not upgrade."
        rm -rf ${G_OS_HP_DIR_TEMP} &>/dev/kmsg
        return ${G_OS_HP_RET_PACKET_ERR}
    fi
    
    #比较版本. 0表示操作成功
    OS_HP_CHECK_PKG_VER
    L_RET=$?
    if [ ${G_OS_HP_RESULT_VERSION_ERR} -eq ${L_RET} ];then
        OS_echo "check upgrade package version failed[${L_RET}], do not upgrade hotpatch."
        rm -rf ${G_OS_HP_DIR_TEMP} &>/dev/kmsg
        return ${G_OS_HP_RET_VERSION_ERR}
    elif [ ${G_OS_HP_RESULT_REINSTALL_ERR} -eq ${L_RET} ];then
        OS_echo "check upgrade package version failed[${L_RET}], do not upgrade hotpatch."
        rm -rf ${G_OS_HP_DIR_TEMP} &>/dev/kmsg
        return ${G_OS_HP_RET_REINSTALL_ERR}
    elif [ ${G_OS_HP_RESULT_CUR_HP_ERROR} -eq ${L_RET} ];then
        OS_echo "check the current hotpatch error, do not upgrade hotpatch."
        rm -rf ${G_OS_HP_DIR_TEMP} &>/dev/kmsg
        return ${G_OS_HP_RET_CUR_HP_ERROR}
    elif [ ${G_OS_HP_RESULT_NO_HOTPATCH} -eq ${L_RET} ];then
        OS_echo "there is no hotpatch, do not upgrade hotpatch."
        rm -rf ${G_OS_HP_DIR_TEMP} &>/dev/kmsg
        return ${G_OS_HP_RET_NO_INSTALL}
    fi
    
    OS_CheckEHPFlag ${G_OS_HP_DIR_TEMP_CONF}
    if [ $? -ne 0 ];then    
        #如果即将升级的热补丁包不是临时补丁，则检查是否包含当前运行的热补丁文件
        OS_HP_CHECK_PKG_FILE
        L_RET=$?
        if [ 0 -ne ${L_RET} ];then
            OS_echo "check upgrade hotpatch file failed, do not upgrade."
            rm -rf ${G_OS_HP_DIR_TEMP} &>/dev/kmsg
            if [ ${L_RET} -eq 2 ];then
                return ${G_OS_HP_RET_REINSTALL_ERR}
            else
                return ${G_OS_HP_RET_RELATIONSHIP_ERR}
            fi
        fi
    fi
    
    #检查补丁包里的用户态文件在系统中是否存在
    OS_HP_CHECK_PKG_USR_FILE ${G_OS_HP_DIR_TEMP_CONF}
    L_RET=$?
    if [ 0 -ne ${L_RET} ];then
        OS_echo "check upgrade usr hotpatch file failed, do not upgrade."
        rm -rf ${G_OS_HP_DIR_TEMP} &>/dev/kmsg
        return ${G_OS_HP_RET_PACKET_ERR}
    fi    
 
    OS_echo "check to upgrade hotpatch complete."
    return 0
}

# **************************************************************************** #
# Function Name: OS_UPG_HP_CHECK
# Description:安装/回退/删除前的检查工作
# Parameter: 热补丁检查类型
# Return: 回退/删除前的检查的返回值,0表示成功,1表示失败
#             安装前检查的返回值如下:
#             G_OS_HP_RET_SUCC=0                           操作成功
#             G_OS_HP_RET_PACKET_ERR=1               一致性检测失败
#             G_OS_HP_RET_VERSION_ERR=2           基础版本不一致
#             G_OS_HP_RET_REINSTALL_ERR=3        热补丁包重复安装
#             G_OS_HP_RET_MEMORY_ERR=4            热补丁区内存不够
#             G_OS_HP_RET_SYS_DISK_ERR=5          系统盘错误
#             G_OS_HP_RET_SYS_BUSY=6                 当前系统忙无法安装热补丁
#             G_OS_HP_RET_NO_INSTALL=7            当前系统没有热补丁包
#             G_OS_HP_RET_CUR_HP_ERROR=8          当前热补丁包状态错误
# **************************************************************************** #
OS_UPG_HP_CHECK()
{
    local L_TYPE=$1
    local L_PKG=$2
    local L_RET=
    local L_RET_TEMP=

    OS_echo "begin to check hotpatch ${L_TYPE} ${L_PKG}."
    
    #把补丁目录分区设置为可写
    OS_echo "begin to set $G_HOT_PATCH_BOOT_DIR for write when check ${L_TYPE} hotpatch."
    OS_SetStartUpDiskProperty $G_HOT_PATCH_BOOT_DIR rw
    L_RET_TEMP=$?
    if [ 0 -ne ${L_RET_TEMP} ] ;then
        OS_echo "can not set [$G_HOT_PATCH_BOOT_DIR] for write when check ${L_TYPE} hotpatch."
        return ${G_OS_HP_RET_SYS_DISK_ERR}
    fi  
    OS_echo "end to set $G_HOT_PATCH_BOOT_DIR for write when check ${L_TYPE} hotpatch."
    
    #把系统目录设置为可写
    OS_echo "begin to set $G_DISK_MOUNT_IMAGE for write when check ${L_TYPE} hotpatch." 
    OS_SetStartUpDiskProperty $G_DISK_MOUNT_IMAGE rw
    L_RET_TEMP=$?
    if [ 0 -ne ${L_RET_TEMP} ] ;then
        OS_echo "can not set [$G_DISK_MOUNT_IMAGE] for write when check ${L_TYPE} hotpatch."
    fi  
    OS_echo "end to set $G_DISK_MOUNT_IMAGE for write when check ${L_TYPE} hotpatch."
    
    #安装/回退/删除前的检查工作
    if [ "roll" = "${L_TYPE}" ];then
        #热补丁回退前的检查
        OS_UPG_HP_ROLLBACK_CHECK
        L_RET=$?
    elif [ "delete" = "${L_TYPE}" ];then
        #热补丁删除前的检查 
        OS_UPG_HP_DEL_CHECK
        L_RET=$?
    elif [ "install" = "${L_TYPE}" ];then
        #热补丁升级前的检查
        OS_UPG_HP_UPGRADE_CHECK "${L_PKG}"
        L_RET=$?    
    else
        #参数错误
        OS_echo "check hotpatch type[${L_TYPE}] is error."
        L_RET=1
    fi
    OS_echo "check hotpatch ${L_TYPE} ret[${L_RET}]."

    #把补丁目录分区设置为只读
    OS_echo "begin to set $G_HOT_PATCH_BOOT_DIR for read-only when check ${L_TYPE} hotpatch."
    sync
    OS_SetStartUpDiskProperty $G_HOT_PATCH_BOOT_DIR ro
    L_RET_TEMP=$?
    if [ 0 -ne ${L_RET_TEMP} ] ;then
        OS_echo "can not set [$G_HOT_PATCH_BOOT_DIR] for read-only when check ${L_TYPE} hotpatch."
        return ${G_OS_HP_RET_SYS_DISK_ERR}
    fi      
    OS_echo "end to set $G_HOT_PATCH_BOOT_DIR for read-only when check ${L_TYPE} hotpatch."
    
    #把系统目录分区设置为只读
    OS_echo "begin to set $G_DISK_MOUNT_IMAGE for read-only when check ${L_TYPE} hotpatch."
    sync
    OS_SetStartUpDiskProperty $G_DISK_MOUNT_IMAGE ro
    L_RET_TEMP=$?
    if [ 0 -ne ${L_RET_TEMP} ] ;then
        OS_echo "can not set [$G_DISK_MOUNT_IMAGE] for read-only when check ${L_TYPE} hotpatch."
    fi      
    OS_echo "end to set $G_DISK_MOUNT_IMAGE for read-only when check ${L_TYPE} hotpatch."
    
    OS_echo "end to check hotpatch ${L_TYPE} ${L_PKG}[ret:${L_RET}]."
    return ${L_RET}
}

# **************************************************************************** #
# Function Name: 
#   OS_UPG_HP_INSTALL
# Description
#   热补丁包安装。
# Parameter:
#   $1:待被安装的补丁包名。
#   $2:补丁激活阶段 
#   $3:是否是升级过程中安装补丁
# Retrun: 0表示成功,1表示失败
# **************************************************************************** #
OS_UPG_HP_INSTALL()
{
    local L_PKG_NAME=$1
    local L_PKG_STAGE=$2
    local L_IS_INSTALL_IN_UPGRADING=$3  
    local L_HP_VER=
    local L_RET=
    local L_RET_TEMP=
    
    OS_echo "begin to install hotpatch ${L_PKG_NAME}."
    rm -f $L_PKG_NAME  &>/dev/kmsg
    #把热补丁目录分区设置为可读写
    OS_echo "begin to set $G_HOT_PATCH_BOOT_DIR for write when install ${L_PKG_NAME} hotpatch."
    OS_SetStartUpDiskProperty $G_HOT_PATCH_BOOT_DIR rw
    L_RET_TEMP=$?
    if [ 0 -ne ${L_RET_TEMP} ] ;then
        OS_echo "can not set [$G_HOT_PATCH_BOOT_DIR] for write when install ${L_PKG_NAME} hotpatch."
        return 1
    fi
    OS_echo "end to set $G_HOT_PATCH_BOOT_DIR for write when install ${L_PKG_NAME} hotpatch."
    
    #把系统目录分区设置为可读写
    OS_echo "begin to set $G_DISK_MOUNT_IMAGE for write when install ${L_PKG_NAME} hotpatch."
    OS_SetStartUpDiskProperty $G_DISK_MOUNT_IMAGE rw
    L_RET_TEMP=$?
    if [ 0 -ne ${L_RET_TEMP} ] ;then
        OS_echo "can not set [$G_DISK_MOUNT_IMAGE] for write when install ${L_PKG_NAME} hotpatch."
    fi
    OS_echo "end to set $G_DISK_MOUNT_IMAGE for write when install ${L_PKG_NAME} hotpatch."

    if [ ! -d $G_OS_HP_DIR ];then
         mkdir $G_OS_HP_DIR
         OS_echo "create hot patch package patch succeed."
    fi  
    #安装热补丁
    if [ "${L_PKG_STAGE}" == "mini" ]; then 
        OS_HP_INSTALL "${G_OS_HP_DIR_TEMP}" "${G_OS_HP_DIR_LINK}" "first"
    elif [ "${L_IS_INSTALL_IN_UPGRADING}" == "true" ];then
        OS_HP_INSTALL "${G_OS_HP_DIR_TEMP}" "${G_OS_HP_DIR_LINK}" "all" "upgrade_left"
    else
        OS_HP_INSTALL "${G_OS_HP_DIR_TEMP}" "${G_OS_HP_DIR_LINK}" "all"
    fi
 
    L_RET=$?
    OS_echo "install hotpatch ${L_PKG_NAME} ret:${L_RET}."
    
    # 在热补丁中打入冷不丁
    # 运行冷补丁hook脚本
    if [ ${L_RET} = 0 -a -f ${G_OS_CP_HOOK} ] ;then
         OS_CP_PACKAGE=`ls ${G_OS_HP_DIR_TEMP} | grep ".tgz"`
         if [ -z ${OS_CP_PACKAGE} ] ;then
             OS_echo "cold_patch_package is not exist,just execute os_hotpatch_hook.sh!! "
             dos2unix ${G_OS_CP_HOOK}
             chmod 755 ${G_OS_CP_HOOK}
             ${G_OS_CP_HOOK}
         else
            OS_CP_PACKAGE=${G_OS_HP_DIR_TEMP}/${OS_CP_PACKAGE}
            dos2unix ${G_OS_CP_HOOK}
            chmod 755 ${G_OS_CP_HOOK}
            ${G_OS_CP_HOOK} ${OS_CP_PACKAGE}
         fi
    fi
    #设置补丁目录分区为只读
    OS_echo "begin to set $G_HOT_PATCH_BOOT_DIR for read-only when install ${L_PKG_NAME} hotpatch."
    sync    
    OS_SetStartUpDiskProperty $G_HOT_PATCH_BOOT_DIR ro
    L_RET_TEMP=$?
    if [ 0 -ne ${L_RET_TEMP} ] ;then
        OS_echo "can not set [$G_HOT_PATCH_BOOT_DIR] for read-only when install ${L_PKG_NAME} hotpatch."
        return 1
    fi
    OS_echo "end to set $G_HOT_PATCH_BOOT_DIR for read-only when install ${L_PKG_NAME} hotpatch."
    #设置系统目录分区为只读
    OS_echo "begin to set $G_DISK_MOUNT_IMAGE for read-only when install ${L_PKG_NAME} hotpatch."
    sync 
    OS_SetStartUpDiskProperty $G_DISK_MOUNT_IMAGE ro
    L_RET_TEMP=$?
    if [ 0 -ne ${L_RET_TEMP} ] ;then
        OS_echo "can not set [$G_DISK_MOUNT_IMAGE] for read-only when install ${L_PKG_NAME} hotpatch."
    fi
    OS_echo "end to set $G_DISK_MOUNT_IMAGE for read-only when install ${L_PKG_NAME} hotpatch."
    
    OS_echo "end to install hotpatch[${L_PKG_NAME}] [ret:${L_RET}]."

    return ${L_RET}
}

# **************************************************************************** #
# Function Name: OS_UPG_HP_ROLLBACK
# Description: 热补丁包回退(ism主动下发回退热补丁)
# Parameter:
# Retrun: 0表示成功,1表示失败
# **************************************************************************** #
OS_UPG_HP_ROLLBACK()
{
    local L_TYPE=$1
    
    OS_echo "begin to rollback hotpatch."
    
    #把补丁目录分区设置为可读写
    OS_echo "begin to set $G_HOT_PATCH_BOOT_DIR for write when rollback hotpatch."
    OS_SetStartUpDiskProperty $G_HOT_PATCH_BOOT_DIR rw
    L_RET_TEMP=$?
    if [ 0 -ne ${L_RET_TEMP} ] ;then
        OS_echo "can not set [$G_HOT_PATCH_BOOT_DIR] for write when rollback hotpatch."
        return 1
    fi
    OS_echo "end to set $G_HOT_PATCH_BOOT_DIR for write when rollback hotpatch."
    #设置系统目录分区为可读写
    OS_echo "begin to set $G_DISK_MOUNT_IMAGE for while when rollback hotpatch."
    OS_SetStartUpDiskProperty $G_DISK_MOUNT_IMAGE rw
    L_RET_TEMP=$?
    if [ 0 -ne ${L_RET_TEMP} ] ;then
        OS_echo "can not set [$G_DISK_MOUNT_IMAGE] for write when rollback hotpatch."
    fi
    OS_echo "end to set [$G_DISK_MOUNT_IMAGE ] for write when rollback hotpatch."
    
    OS_CP_BACKUP_TO_MEMORY_BEFORE_ROLLBACK
    L_RET_TEMP=$?
    if [ 0 -ne ${L_RET_TEMP} ] ;then
        return 1
    fi
    
    #回退热补丁
    OS_HP_ROLLBACK_NORMAL
    L_RET=$?    
    #设置补丁目录分区为只读
    OS_echo "begin to set $G_HOT_PATCH_BOOT_DIR for read-only when rollback hotpatch."
    sync    
    OS_SetStartUpDiskProperty $G_HOT_PATCH_BOOT_DIR ro
    L_RET_TEMP=$?
    if [ 0 -ne ${L_RET_TEMP} ] ;then
        OS_echo "can not set [$G_HOT_PATCH_BOOT_DIR] for read-only when rollback hotpatch."
        return 1
    fi
    OS_echo "end to set $G_HOT_PATCH_BOOT_DIR for read-only when rollback hotpatch."  
    #设置系统目录分区为只读
    OS_echo "begin to set $G_DISK_MOUNT_IMAGE for read-only when rollback hotpatch."
    sync
    OS_SetStartUpDiskProperty $G_DISK_MOUNT_IMAGE ro
    L_RET_TEMP=$?
    if [ 0 -ne ${L_RET_TEMP} ] ;then
        OS_echo "can not set [$G_DISK_MOUNT_IMAGE] for read-only when rollback hotpatch."
    fi
    OS_echo "end to set $G_DISK_MOUNT_IMAGE for read-only when rollback hotpatch."  
    
    OS_echo "end to rollback hotpatch[ret:${L_RET}]."
    return ${L_RET}
}

# **************************************************************************** #
# Function Name: 
#   OS_UPG_HP_DEL_ALL
# Description
#    #删除系统中的所有热补丁。
# Parameter:
#   none
# Retrun: 
#   G_OS_HP_RET_SUCC
#   G_OS_HP_RET_INNER_ERR
#   G_OS_HP_RET_FUNC_IN_USE
# **************************************************************************** #
OS_UPG_HP_DEL_ALL()
{
    local L_RET=

    OS_echo "begin to delete all hotpatch."
    #把补丁目录分区设置为可读写
    OS_echo "begin to set $G_HOT_PATCH_BOOT_DIR for write when delete all hotpatch."
    OS_SetStartUpDiskProperty $G_HOT_PATCH_BOOT_DIR rw
    L_RET_TEMP=$?
    if [ 0 -ne ${L_RET_TEMP} ] ;then
        OS_echo "can not set [$G_HOT_PATCH_BOOT_DIR] for write when delete all hotpatch."
        return 1
    fi
    OS_echo "end to set $G_HOT_PATCH_BOOT_DIR for write when delete all hotpatch."
    
    #设置系统目录分区为可读写
    OS_echo "begin to set $G_DISK_MOUNT_IMAGE for write when delete all hotpatch."
    OS_SetStartUpDiskProperty $G_DISK_MOUNT_IMAGE rw
    L_RET_TEMP=$?
    if [ 0 -ne ${L_RET_TEMP} ] ;then
        OS_echo "can not set [$G_DISK_MOUNT_IMAGE] for write when delete all hotpatch."
    fi
    OS_echo "end to set $G_DISK_MOUNT_IMAGE for write when delete all hotpatch."
    
    #删除热补丁
    OS_HP_DEL_ALL
    L_RET=$?
    OS_echo "delete all hotpatch ret:${L_RET}."
    
    #设置补丁目录分区为只读
    OS_echo "begin to set $G_HOT_PATCH_BOOT_DIR for read-only when delete all hotpatch."
    sync    
    OS_SetStartUpDiskProperty $G_HOT_PATCH_BOOT_DIR ro
    L_RET_TEMP=$?
    if [ 0 -ne ${L_RET_TEMP} ] ;then
        OS_echo "can not set [$G_HOT_PATCH_BOOT_DIR] for read-only when delete all hotpatch."
        return 1
    fi
    OS_echo "end to set $G_HOT_PATCH_BOOT_DIR for read-only when delete all hotpatch."
    #设置系统目录分区为可读写
    OS_echo "begin to set $G_DISK_MOUNT_IMAGE for read-only when delete all hotpatch."
    sync
    OS_SetStartUpDiskProperty $G_DISK_MOUNT_IMAGE ro
    L_RET_TEMP=$?
    if [ 0 -ne ${L_RET_TEMP} ] ;then
        OS_echo "can not set [$G_DISK_MOUNT_IMAGE] for read-only when delete all hotpatch."
    fi
    OS_echo "end to set $G_DISK_MOUNT_IMAGE for read-only when delete all hotpatch."
    
    OS_echo "end to delete all hotpatch[ret:${L_RET}]."
    return ${L_RET}
}

# **************************************************************************** #
# Function Name: OS_RollBackSpecialPatch
# Description: 回退特殊补丁
# Parameter: none
# Return: none
# **************************************************************************** #
OS_RollBackSpecialPatch()
{
    local L_UPG_DIR="."
    if [ ! -f /startup_disk/image/boot/coldpatch/patch.conf ] ;then
        OS_echo "Rollback special patch failed,/startup_disk/image/boot/coldpatch/patch.conf not exist."
        return 1
    fi
    
    OS_echo "Rollback special patch begin."
    
    cd /startup_disk/image/boot/coldpatch 
    
    cat patch.conf |grep '^Item' |while read L_ITEM
    do
        L_FILE=`echo $L_ITEM |awk '{print $2}'`
        L_DIR=`echo $L_ITEM |awk '{print $3}'`
        L_SCRIPT=`echo $L_ITEM |awk '{print $4}'`
        ##如果没有补丁文件，补丁文件补丁不替换文件，只执行脚本。
        if [ "${L_DIR}" == "script_only" -a "${L_FILE}" == "script_only" ]; then
                if [ "${L_SCRIPT}" != "0" -a -n "${L_SCRIPT}" ];then
                        OS_HP_SCRIPT "${L_SCRIPT}" "rollstart" "${L_UPG_DIR}"
                        L_RET=$?
                        if [ $L_RET -ne 0 ];then
                                OS_echo "run ${L_FILE} script start failed"
                                return 1
                        fi
                fi
                continue
        fi

        if [ "${L_SCRIPT}" != "0" -a -n "${L_SCRIPT}" ];then
                OS_HP_SCRIPT "${L_SCRIPT}" "rollstart" "${L_UPG_DIR}"
                L_RET=$?
                if [ $L_RET -ne 0 ];then
                        #由于实际上补丁文件还没有替换，因此补丁流程针对该补丁不做回滚操作，需要由用户在start参数中进行异常处理
                        #后面只要start执行成功了，都需要调用回滚脚本
                        OS_echo "run ${L_FILE} script start failed"
                        return 1
                fi
        fi
  

        if [ -f ${L_DIR}/${L_FILE}_orig ];then
            mv -f ${L_DIR}/${L_FILE}_orig ${L_DIR}/${L_FILE} &>/dev/kmsg
        else
            rm -f ${L_DIR}/${L_FILE} &>/dev/kmsg
        fi

        if [ "${L_SCRIPT}" != "0" -a -n "${L_SCRIPT}" ];then
                OS_HP_SCRIPT "${L_SCRIPT}" "rollend" "${L_UPG_DIR}"
                L_RET=$?
                if [ $L_RET -ne 0 ];then
                        OS_echo "run ${L_FILE} script end  failed"
                        return 1
                fi
        fi

    done
    
    cd - &>/dev/null
    OS_echo "Rollback special patch finish."
    return 0
}

# **************************************************************************** #
# Function Name: 
#   OS_UPG_HP_DEL_SPECIAL_PATCH
# Description
#    #删除特殊补丁
# **************************************************************************** #
OS_UPG_HP_DEL_SPECIAL_PATCH()
{
    OS_echo "begin to delete special hotpatch."
    #删除安全红线的特殊补丁
    if [ -f /startup_disk/image/boot/coldpatch/patch.conf ] ;then
        #把启动盘分区设置为可读写
        OS_echo "begin to set $G_DISK_MOUNT_IMAGE for write when delete special hotpatch."
        OS_SetStartUpDiskProperty $G_DISK_MOUNT_IMAGE rw
        L_RET_TEMP=$?
        if [ 0 -ne ${L_RET_TEMP} ] ;then
            OS_echo "can not set [$G_DISK_MOUNT_IMAGE] for write when delete special hotpatch."
            return 1
        fi
        OS_echo "end to set $G_DISK_MOUNT_IMAGE for write when delete special hotpatch."
        OS_RollBackSpecialPatch
        rm -rf /startup_disk/image/boot/coldpatch/* &>/dev/kmsg
        sync
        OS_echo "begin to set $G_DISK_MOUNT_IMAGE for read-only when delete special hotpatch."        
        OS_SetStartUpDiskProperty $G_DISK_MOUNT_IMAGE ro
        L_RET_TEMP=$?
        if [ 0 -ne ${L_RET_TEMP} ] ;then
            OS_echo "can not set [$G_DISK_MOUNT_IMAGE] for read-only when delete special hotpatch."
            return 1
        fi
        OS_echo "end to set $G_DISK_MOUNT_IMAGE for read-only when delete special patch."
    else
        OS_echo "special patch not exist."
    fi
    OS_echo "end to delete special hotpatch."
    return ${G_OS_HP_RET_SUCC}
}
    
# **************************************************************************** #
# Function Name: OS_CP_BACKUP_TO_MEMORY_BEFORE_ROLLBACK
# Description: 回滚到不支持此功能的补丁版本前，把备份文件从系统盘拷贝回内存。
# Parameter:
# Retrun: 0表示成功,1表示失败
# **************************************************************************** #
OS_CP_BACKUP_TO_MEMORY_BEFORE_ROLLBACK()
{
    local cur_patch=`cat "${G_OS_HP_DIR_BAK}/${G_OS_HP_CONF}" 2>/dev/kmsg |grep "^Version" |awk '{print $2}'`
    if [ -n "$cur_patch" ] && [ "$cur_patch" \< "$G_BACKUP_PATCH_VER" ];then
        OS_echo "rollback to $cur_patch, need mv patch file to memory."
        if [ -d "${G_HOT_PATCH_BACKUP_DIR}" ];then   
            local num=`ls ${G_HOT_PATCH_BACKUP_DIR}/ | wc -l`
            if [ "$num" == "0" ];then
                OS_echo "no patch file, no need mv."
                return 0
            fi
            mv ${G_HOT_PATCH_BACKUP_DIR}/*_${G_OS_USR_HP_BAK_FIELD} /OSM/bin/
            if [ "$?" != "0" ];then
                OS_echo "rollback mv patch file to memory failed."
                return 1
            fi 
        fi
    fi    
    return 0
}  
  
# **************************************************************************** #
# Function Name: OS_CHECK_BACKUP_DIR_BEFORE_ROLLBACK
# Description: 回滚前检查coffer_data是rw，检查内存。
# Parameter:
# Retrun: 0表示成功,其他表示失败
# **************************************************************************** #
OS_CHECK_BACKUP_DIR_BEFORE_ROLLBACK()
{
    L_RET=`mount | grep ${G_HOT_PATCH_BACKUP_ROOT_DIR} | grep "rw,"`
    if [ -z "$L_RET" ];then
        OS_echo "The coffer_data dir is not rw."
        return ${G_OS_HP_RET_SYS_DISK_ERR}
    fi
    
    if [ ! -f "$G_OS_HP_DIR_BAK/$G_OS_HP_MD5SUM_FILE" ];then
        OS_echo "The last patch is null, no need check."
        return 0
    fi
    
    local cur_patch=`cat "${G_OS_HP_DIR_BAK}/${G_OS_HP_CONF}" 2>/dev/kmsg |grep "^Version" |awk '{print $2}'`
    if [ -n "$cur_patch" ] && [ "$cur_patch" \> "$G_BACKUP_PATCH_VER" -o "$cur_patch" == "$G_BACKUP_PATCH_VER" ];then
        OS_echo "The last patch is $cur_patch, no need check."
        return 0
    fi
    
    #回退后的补丁不支持备份在系统盘的功能，则需把系统盘备份文件还原回去，所以要检查下内存
    if [ -d "${G_HOT_PATCH_BACKUP_DIR}" ];then   
        local dirsize=`du -m ${G_HOT_PATCH_BACKUP_DIR} | tail -n 1 | awk '{print $1}'`
        let dirsize=$dirsize+100
        OS_UPG_HP_CHECK_MEM $dirsize
        if [ "$?" != "0" ];then
            OS_echo "The free momory is not enough."
            return ${G_OS_HP_RET_MEMORY_ERR}
        fi
    fi
    
    return 0
}

# **************************************************************************** #
# Function Name: OS_CHECK_BACKUP_DIR
# Description: 检查coffer_data是rw，检查coffer_data剩余空间是否大于200M。
# Parameter:
# Retrun: 0表示成功,其他表示失败
# **************************************************************************** #
OS_CHECK_BACKUP_DIR()
{
    local ret=`mount | grep ${G_HOT_PATCH_BACKUP_ROOT_DIR} | grep "rw,"`
    if [ -z "$ret" ];then
        OS_echo "The coffer_data dir is not rw."
        return 1
    fi
    
    local size=`df -m ${G_HOT_PATCH_BACKUP_ROOT_DIR} | grep ${G_HOT_PATCH_BACKUP_ROOT_DIR} |awk '{print $4}'`
    if [ $size -lt 200 ];then
        OS_echo "The coffer_data available size is $size."
        rm -rf ${G_HOT_PATCH_BACKUP_ROOT_DIR}/ftds/tracefile/*
        size=`df -m ${G_HOT_PATCH_BACKUP_ROOT_DIR} | grep ${G_HOT_PATCH_BACKUP_ROOT_DIR} |awk '{print $4}'`
        if [ $size -lt 200 ];then
            OS_echo "The coffer_data available size is $size."
            return 1
        fi
    fi
    
    OS_echo "check backup dir succeed."
    return 0
}

# **************************************************************************** #
# Function Name: OS_CREATE_BACKUP_DIR
# Description: 创建备份目录。
# Parameter:
# Retrun: 0表示成功,1表示失败
# **************************************************************************** #
OS_CREATE_BACKUP_DIR()
{
    local delflag=$1
    
    local ret=`mount | grep ${G_HOT_PATCH_BACKUP_ROOT_DIR} | grep "rw,"`
    if [ -z "$ret" ];then
        OS_echo "The coffer_data dir is not rw."
        return 1
    fi
    
    #上电恢复内存和系统盘中都有备份,不能删除已有的目录
    if [ "$delflag" == "delall" ];then 
        rm -rf ${G_HOT_PATCH_BACKUP_ROOT_DIR}/patch_backup
    else    
        #删除不属于本版本的备份目录
        if [ -d ${G_HOT_PATCH_BACKUP_ROOT_DIR}/patch_backup ];then
            for dir in `ls ${G_HOT_PATCH_BACKUP_ROOT_DIR}/patch_backup`
            do
                if [ "$dir" != "${G_HOT_PATCH_BACKUP_VERSION}" ];then
                    OS_echo "del the other dir $dir."
                    rm -rf ${G_HOT_PATCH_BACKUP_ROOT_DIR}/patch_backup/$dir
                fi
            done
        fi
    fi
    
    if [ ! -d ${G_HOT_PATCH_BACKUP_DIR} ];then
        mkdir -p ${G_HOT_PATCH_BACKUP_DIR}
    fi
    
    OS_echo "create backup dir succeed."
    return 0
}

# **************************************************************************** #
# Function Name: OS_CP_BACKUP_FILE_AFTER_POWERON
# Description: om_load_ok上电恢复normal后，把内存备份文件拷贝到系统盘。
# Parameter:
# Retrun: 0表示成功,1表示失败
# **************************************************************************** #
OS_CP_BACKUP_FILE_AFTER_POWERON()
{
    OS_CREATE_BACKUP_DIR
    if [ "$?" != "0" ];then
        return 1
    fi
    
    mv /OSM/bin/*_${G_OS_USR_HP_BAK_FIELD} ${G_HOT_PATCH_BACKUP_DIR}/
    if [ "$?" != "0" ];then
        OS_echo "Poweron mv patch file failed."
        return 1
    fi
    
    OS_echo "Poweron mv patch file succeed."
    return 0
}

# **************************************************************************** #
# Function Name: OS_CP_BACKUP_FILE_AFTER_INSTALL
# Description: 首次安装此功能补丁，把内存备份文件拷贝到系统盘。
# Parameter:
# Retrun: 0表示成功,1表示失败
# **************************************************************************** #
OS_CP_BACKUP_FILE_AFTER_INSTALL()
{
    local cur_patch=`cat "${G_OS_HP_DIR_BAK}/${G_OS_HP_CONF}" 2>/dev/kmsg |grep "^Version" |awk '{print $2}'`
    if [ -n "$cur_patch" ] && [ "$cur_patch" \> "$G_BACKUP_PATCH_VER" -o "$cur_patch" == "$G_BACKUP_PATCH_VER" ];then
        OS_echo "The last patch is $cur_patch, no need cp."
        return 0
    fi
    
    OS_CREATE_BACKUP_DIR delall
    if [ "$?" != "0" ];then
        return 1
    fi
    
    OS_CHECK_BACKUP_DIR
    if [ "$?" != "0" ];then
        return 1
    fi
    
    mv /OSM/bin/*_${G_OS_USR_HP_BAK_FIELD} ${G_HOT_PATCH_BACKUP_DIR}/
    if [ "$?" != "0" ];then
        OS_echo "mv patch file failed."
        mv ${G_HOT_PATCH_BACKUP_DIR}/*_${G_OS_USR_HP_BAK_FIELD} /OSM/bin/
        return 1
    fi
    
    OS_echo "mv patch file succeed."
    return 0
}

# **************************************************************************** #
# Function Name: OS_UPG_HP_CONFIRM
# Description: 热补丁包安装后的确认。
# Parameter:
# Retrun: 0表示成功,1表示失败
# **************************************************************************** #
OS_UPG_HP_CONFIRM()
{   
    local L_RET=
    OS_echo "begin to confirm."
    if [ ! -f "${G_OS_HP_DIR_TEMP_CONF}" ] ;then 
        OS_echo "no conf file ${G_OS_HP_DIR_TEMP_CONF}. confirm fail."
        return 1
    fi

    local L_TEMP_VER=`cat "${G_OS_HP_DIR_TEMP_CONF}" 2>/dev/kmsg |grep "^Version" |awk '{print $2}'`    
    if [ "`OS_GetHPVer`" != ${L_TEMP_VER} ];then
            OS_echo "no successfully installed package found, confirm abort!"
            return 1
    fi

    #把启动盘分区设置为可读写
    OS_echo "begin to set $G_HOT_PATCH_BOOT_DIR for write when confirm hotpatch."
    OS_SetStartUpDiskProperty $G_HOT_PATCH_BOOT_DIR rw
    L_RET_TEMP=$?
    if [ 0 -ne ${L_RET_TEMP} ] ;then
        OS_echo "can not set [$G_HOT_PATCH_BOOT_DIR] for write when confirm hotpatch."
        return 1
    fi  
    OS_echo "end to set $G_HOT_PATCH_BOOT_DIR for write when confirm hotpatch."
 
    #修改软链接
    OS_HP_CHANGE_DIR "install"
    L_RET=$?

    #升级过程中安装补丁成功，删除补丁恢复标志
    if [ -f /tmp/upd_hotpatch_resume.log ];then
        OS_echo "This is upgrade hotpatch in upgrading,delete hotpatch resume file."
        rm -f /tmp/upd_hotpatch_resume.log
    fi
    #把启动盘分区设置为可读写
    OS_echo "begin to set $G_HOT_PATCH_BOOT_DIR for read-only when confirm hotpatch."
    sync
    OS_SetStartUpDiskProperty $G_HOT_PATCH_BOOT_DIR ro
    L_RET_TEMP=$?
    if [ 0 -ne ${L_RET_TEMP} ] ;then
        OS_echo "can not set [$G_HOT_PATCH_BOOT_DIR] for read-only when confirm hotpatch."
        return 1
    fi
    OS_echo "end to set $G_HOT_PATCH_BOOT_DIR for read-only when confirm hotpatch."   
    
    if [ "${L_RET}" != "0" ];then
        OS_echo "end to confirm hotpatch[ret:${L_RET}]."
        return ${L_RET}
    fi
    
    OS_CP_BACKUP_FILE_AFTER_INSTALL
    if [ "$?" != "0" ];then
        return 1
    fi
    
    OS_echo "end to confirm hotpatch."
    return 0    
}

# **************************************************************************** #
# Function Name: 
#   OS_UPG_HP_GET_VER
# Description
#   获取热补丁版本，包括:当前版本、可回退的版本、非活动版本；
#   #配置文件格式:
#       [RUNNING]
#       Version=
#       CreateTime=
#       [ROLLBACK]
#       Version=
#       CreateTime=
#       [INACTIVE]
#       Version=
#       CreateTime=
#   说明:
#       [RUNNING]：当前运行的补丁包信息，如果没有补丁，该域不存在；
#       [ROLLBACK]：可回退的补丁包信息，如果没有补丁，或者没有可回退的补丁包，
#                   该域不存在；如果系统只打过一次补丁，该域中的Version是控制器版本.
#                   CreateTime是控制器启动时的Cmos时间。
#       [INACTIVE]:如果未指定非活动补丁包，该域将不存在。
# Parameter:
#   $1:非活动补丁包名
# Retrun: 0表示成功,1表示失败
# **************************************************************************** #
OS_UPG_HP_GET_VER()
{
    local L_INACTIVE_PKG=$1
    local L_CUR_VER=
    local L_RB_VER=
    local L_RET=
    
    if [ "all" = "${L_INACTIVE_PKG}" ];then
        #获取双端热补丁配置信息
        OS_HP_GET_VER_ALL &> /dev/kmsg
        L_RET=$?
        if [ "$L_RET" != "$G_OS_HP_RET_SUCC" ] ;then
            echo "get hotpatch version fail." 
        fi 
    elif [ "peertolocal" = "${L_INACTIVE_PKG}" ];then
        #专门提供给对端来获取本端配置时使用
        OS_HP_GET_VER_PEER_TO_LOCAL
        L_RET=$? 
    elif [ "local" = "${L_INACTIVE_PKG}" ];then
        #获取包括非激活热补丁配置信息
        OS_HP_GET_VER_LOCAL "${L_INACTIVE_PKG}" &> /dev/null
        L_RET=$?
        if [ "$L_RET" != "$G_OS_HP_RET_SUCC" ] ;then
            echo "get hotpatch version fail." 
        else
            cat ${G_OS_HP_VER_TEMP_CONF}
        fi
    else
        #获取包括非激活热补丁配置信息
        OS_HP_GET_VER_LOCAL "${L_INACTIVE_PKG}" &> /dev/null
        L_RET=$?
        if [ "$L_RET" != "$G_OS_HP_RET_SUCC" ] ;then
            echo "get hotpatch version fail." 
        else
            cat ${G_OS_HP_VER_TEMP_CONF}
        fi
    fi

    if [[ ${L_RET} -ne 0 ]]; then
            OS_echo "end to get hotpatch version[type:${L_INACTIVE_PKG}] ret[${L_RET}]."
    fi
    return ${L_RET}
}

# **************************************************************************** #
# Function Name: 
#   OS_UPG_HP_RECOVER_HOTPACTH_FILE
# Description
#   系统启动，单控模式下将热补丁文件从/startup_disk/image/boot_osp/hotpatch恢复到/OSM/coffer_bin/hotpatch
# Parameter:
#   none
# Retrun: 
#   0--OK
#   1--ERR
# **************************************************************************** #
OS_UPG_HP_RECOVER_HOTPACTH_FILE()
{
    if [ ! -d $G_OS_HP_OSP_DIR ] ; then
        return 0
    fi
    #保险箱盘没有备份系统目录，非装备模式不需要恢复
    if [ ! -d $BACKUP_DIR ] ; then
        return 0
    fi
    #升级场景或多控换备件场景不需要恢复
    local l_bootLink=$(ls -l $G_BOOT_LINK | awk '{print $11}')
    if [ "$l_bootLink" == "boot_osp_bak" ]; then
        return 0
    fi
    
    if [ "`ls -A $G_OS_HP_OSP_DIR`" != "" ] ; then
        OS_echo "$G_OS_HP_OSP_DIR is not empty, need to recover." >/dev/null
        rm -rf $G_OS_HP_DIR
        if [ 0 != $? ] ; then
            OS_echo "remove hotpatch dir failed.">/dev/null
            return 1
        fi
        OS_echo "succeed to remove hotpatch dir.">/dev/null
        cp -rf $G_OS_HP_OSP_DIR $G_HOT_PATCH_BOOT_DIR
        if [ 0 != $? ] ; then
            OS_echo "Recover hotpatch to $G_HOT_PATCH_BOOT_DIR failed, try again.">/dev/null
            #第一次拷贝失败重试一次，重试也失败，返回失败
            cp -rf $G_OS_HP_OSP_DIR $G_HOT_PATCH_BOOT_DIR
            if [ 0 != $? ] ; then
                OS_echo "Recover hotpatch to $G_HOT_PATCH_BOOT_DIR failed again, return error.">/dev/null
                return 1
            fi        
        fi
        OS_SetStartUpDiskProperty $G_DISK_MOUNT_IMAGE rw
        rm -rf $G_OS_HP_OSP_DIR/*
        OS_SetStartUpDiskProperty $G_DISK_MOUNT_IMAGE ro
    fi
    return 0
}

# **************************************************************************** #
# Function Name: 
#   OS_UPG_HP_RESUME_RUN
# Description
#   系统启动，使系统中的热补丁恢复运行。
# Parameter:
#   none
# Retrun: 
#   G_OS_HP_RET_SUCC
#   G_OS_HP_RET_INNER_ERR
# **************************************************************************** #
OS_UPG_HP_RESUME_RUN()
{
    local L_HP_RESUME_STAGE=$1
    local L_IS_RESUME_IN_UPGRADING=$2
    OS_echo "begin to resume all hotpatch."
    #把热补丁目录分区设置为可读写
    OS_echo "begin to set $G_HOT_PATCH_BOOT_DIR for write when resume all hotpatch."
    OS_SetStartUpDiskProperty $G_HOT_PATCH_BOOT_DIR rw
    L_RET_TEMP=$?
    if [ 0 -ne ${L_RET_TEMP} ] ;then
        OS_echo "can not set [$G_HOT_PATCH_BOOT_DIR] for write when resume all hotpatch."
        return 1
    fi
    OS_echo "end to set $G_HOT_PATCH_BOOT_DIR for write when resume all hotpatch."
    #设置系统目录分区为可读写
    OS_echo "begin to set $G_DISK_MOUNT_IMAGE for write when resume all hotpatch."
    OS_SetStartUpDiskProperty $G_DISK_MOUNT_IMAGE rw
    L_RET_TEMP=$?
    if [ 0 -ne ${L_RET_TEMP} ] ;then
        OS_echo "can not set [$G_DISK_MOUNT_IMAGE] for write when resume all hotpatch."
    fi
    OS_echo "end to set $G_DISK_MOUNT_IMAGE for write when resume all hotpatch."    
    #恢复热补丁
    OS_HP_RESUME_RUN "$L_HP_RESUME_STAGE" "$L_IS_RESUME_IN_UPGRADING"
    L_RET=$?
    OS_echo "resume ${L_HP_RESUME_STAGE}  hotpatch ret:${L_RET}."
    # P12I-3095 modified2012-04-23 begin    
    if [ 2 == ${L_RET} ];then
        # hotpatch 目录或hotpatch/patch软链接不存在
    #P11G-5140 modify2012-06-13 begin
        #P12V-1275 modify2012/06/07 begin
        OS_SetStartUpDiskProperty $G_HOT_PATCH_BOOT_DIR ro
        L_RET_TEMP=$?
        if [ 0 -ne ${L_RET_TEMP} ] ;then
            OS_echo "can not set [$G_HOT_PATCH_BOOT_DIR] for read-only when hotpatch is not exist."
        fi
        #P12V-1275 modify2012/06/07 end
    #P11G-5140 modify2012-06-13 end
        return 0
    fi
    # P12I-3095 modified2012-04-23 end
    #设置补丁目录分区为只读
    OS_echo "begin to set $G_HOT_PATCH_BOOT_DIR for read-only when resume all hotpatch."
    sync    
    OS_SetStartUpDiskProperty $G_HOT_PATCH_BOOT_DIR ro
    L_RET_TEMP=$?
    if [ 0 -ne ${L_RET_TEMP} ] ;then
        OS_echo "can not set [$G_HOT_PATCH_BOOT_DIR] for read-only when resume all hotpatch."
        return 1
    fi
    OS_echo "end to set $G_HOT_PATCH_BOOT_DIR for read-only when resume all hotpatch."
    #设置系统目录分区为可读写
    OS_echo "begin to set $G_DISK_MOUNT_IMAGE for read-only when resume all hotpacth."
    sync
    OS_SetStartUpDiskProperty $G_DISK_MOUNT_IMAGE ro
    L_RET_TEMP=$?
    if [ 0 -ne ${L_RET_TEMP} ];then
        OS_echo "can not set [$G_DISK_MOUNT_IMAGE] for read-only when resume all hotpacth."
    fi
    OS_echo "end to set [$G_DISK_MOUNT_IMAGE] for read-only when resume all hotpatch."
    
    OS_echo "end to resume all hotpatch."
    return ${L_RET}         
}

#/* XVE V2R1 P12N-4681 modified 2012/10/08 begin */
# **************************************************************************** #
# Function Name: OS_UPG_HP_SPECIAL_PACKAGE_CHECK
# Description:查询安全红线整改特殊补丁包信息
# Parameter: 补丁包名(传入参数为/OSM/update/hotpatch.tgz)
# Return: G_OS_HP_RET_SUCC=0                  操作成功
#         G_OS_HP_RET_PACKET_ERR=1            一致性检测失败
#         G_OS_HP_RET_VERSION_ERR=2           基础版本不一致
#         G_OS_HP_RET_NO_INSTALL=7            当前系统没有热补丁包
#         G_OS_HP_RET_SPECIAL_PATCH=10        特殊补丁包,解决安全红线进入DEBU模式问题 
# **************************************************************************** #
OS_UPG_HP_SPECIAL_PACKAGE_CHECK()
{
    local L_UPG_HP_SPECIAL_NAME=$1
    local L_RET=0

    #检查补丁包是否存在
    if [ ! -e "${L_UPG_HP_SPECIAL_NAME}" ];then
        OS_echo "the hotpatch package[${L_UPG_HP_SPECIAL_NAME}] is not exist, do not upgrade."
        return ${G_OS_HP_RET_NO_INSTALL}
    fi
    OS_echo "check whether hotpatch package is special for entering debug mode."
    
    #解压补丁包版本文件到临时目录下
    tar -xzvf ${L_UPG_HP_SPECIAL_NAME} -C /tmp ${G_OS_HP_CONF} &>/dev/kmsg
    L_RET=$?
    if [ 2 -eq ${L_RET} ];then
        OS_echo "decompress patch[${L_UPG_HP_SPECIAL_NAME}] to /tmp failed, do not upgrade."
        OS_echo "perhaps the patch is wrong, do not upgrade."
        return ${G_OS_HP_RET_PACKET_ERR}
    elif [ 0 -ne ${L_RET} ];then
        OS_echo "decompress patch[${L_UPG_HP_SPECIAL_NAME}] to /tmp failed, do not upgrade."
        return ${G_OS_HP_RET_PACKET_ERR}
    fi
    OS_echo "decompress patch version file to /tmp complete."

    #从补丁包版本文件中,读取特殊补丁包标志字符"SPH",判断是否热补丁包
    IS_SPH_FLAG=`cat "/tmp/${G_OS_HP_CONF}" 2>/dev/null|grep ^Version|grep "SPH"`
    if [ -z "$IS_SPH_FLAG" ];then
        #从补丁包版本文件中,读取特殊补丁包标志字符"SPC"
        IS_SPECIALFLAG=`cat "/tmp/${G_OS_HP_CONF}" 2>/dev/null|grep ^Version|grep "SPC"`
        if [ -z "$IS_SPECIALFLAG" ];then
            OS_echo "this patch is not special for entering debug mode."
            rm -rf /tmp/${G_OS_HP_CONF} &>/dev/kmsg
            return ${G_OS_HP_RET_VERSION_ERR}
        else
            #从补丁包版本文件中,读取特殊补丁包标志字符SysVersionSupported是否为通用标志"1"
            VER_SUPPORT_FLAG=`cat "/tmp/${G_OS_HP_CONF}" 2>/dev/null|grep ^SysVersionSupported|awk '{print $2}'`
            if [ "$VER_SUPPORT_FLAG" == "1" ];then
                OS_echo "this patch is special for entering debug mode."
                rm -rf /tmp/${G_OS_HP_CONF} &>/dev/kmsg
                return ${G_OS_HP_RET_SPECIAL_PATCH}
            else
                OS_echo "this patch is not special for entering debug mode."
                rm -rf /tmp/${G_OS_HP_CONF} &>/dev/kmsg
                return ${G_OS_HP_RET_VERSION_ERR}
            fi   
        fi
    fi
    
    OS_echo "this patch is general hot patch."
    
    rm -rf /tmp/${G_OS_HP_CONF} &>/dev/kmsg

    return ${G_OS_HP_RET_SUCC}
}
#/* XVE V2R1 P12N-4681 modified 2012/10/08 end */

# **************************************************************************** #
# Function Name: OS_DEL_FILES_IN_UPDATE_DIR
# Description: delete files in /OSM/coffer_bin
# Parameter:
# Return:
# **************************************************************************** #
OS_DEL_FILES_IN_UPDATE_DIR()
{    
    #把启动盘分区设置为可读写
    OS_echo "begin to set $G_HOT_PATCH_BOOT_DIR for write when del all hotpatch."
    OS_SetStartUpDiskProperty $G_HOT_PATCH_BOOT_DIR rw
    L_RET_TEMP=$?
    if [ 0 -ne ${L_RET_TEMP} ] ;then
        OS_echo "can not set [$G_HOT_PATCH_BOOT_DIR] for write when del all hotpatch."
        return 1
    fi
    OS_echo "end to set $G_HOT_PATCH_BOOT_DIR for write when del all hotpatch."
    
    #delete medium update dir content    
    #从C50及以后基线版本支持同时升级系统和热补丁，目标版本补丁放在patch_temp目录；
    #之前不支持的版本通过补丁合入的，目标版本补丁放在patch_bak或者patch_cur目录   
    if [ -f "$G_OS_HP_DIR_TEMP_CONF" ];then
        #删除软链接
        if [ -L "${G_OS_HP_DIR_LINK}" ];then
            rm -f ${G_OS_HP_DIR_LINK}
        fi
        #删除patch_cur目录
        if [ -d "$G_OS_HP_DIR_CUR" ] ;then
            rm -fr $G_OS_HP_DIR_CUR
            OS_echo "rm hotpatch directory:${G_OS_HP_DIR_CUR}."
        fi
        #删除patch_bar目录
        if [ -d "$G_OS_HP_DIR_BAK" ] ;then
            rm -fr $G_OS_HP_DIR_BAK
            OS_echo "rm hotpatch directory:${G_OS_HP_DIR_BAK}."
        fi
        l_temp_hp_version=`cat "${G_OS_HP_DIR_TEMP_CONF}" 2>/dev/kmsg| grep ^SysVersionSupported |awk '{print $2}'`
        l_system_version=`grep -A 1 "\[SYS\]" /OSM/conf/versions.conf | tail -1| awk -F'=' '{print $2}'`
        if [ "${l_temp_hp_version}" != "${l_system_version}" ] ;then
            rm -fr $G_OS_HP_DIR_TEMP
            OS_echo "rm hotpatch directory:${G_OS_HP_DIR_TEMP}."
        fi
    else
        local del_cur=0
        local del_bak=0
        #删除patch_cur目录
        if [ -d "$G_OS_HP_DIR_CUR" ];then
            if [ -f ${G_OS_HP_DIR_CUR}/${G_OS_HP_CONF} ];then
                l_temp_hp_version=`cat "${G_OS_HP_DIR_CUR}/${G_OS_HP_CONF}" 2>/dev/kmsg| grep ^SysVersionSupported |awk '{print $2}'`
                l_system_version=`grep -A 1 "\[SYS\]" /OSM/conf/versions.conf | tail -1| awk -F'=' '{print $2}'`
                if [ "${l_temp_hp_version}" != "${l_system_version}" ] ;then
                    rm -fr $G_OS_HP_DIR_CUR
                    del_cur=1
                    OS_echo "rm hotpatch directory:${G_OS_HP_DIR_CUR}."
                fi
            else
                rm -fr $G_OS_HP_DIR_CUR
                del_cur=1
            fi
        fi
        #删除patch_bar目录
        if [ -d "$G_OS_HP_DIR_BAK" ];then
            if [ -f ${G_OS_HP_DIR_BAK}/${G_OS_HP_CONF} ];then
                l_temp_hp_version=`cat "${G_OS_HP_DIR_BAK}/${G_OS_HP_CONF}" 2>/dev/kmsg| grep ^SysVersionSupported |awk '{print $2}'`
                l_system_version=`grep -A 1 "\[SYS\]" /OSM/conf/versions.conf | tail -1| awk -F'=' '{print $2}'`
                if [ "${l_temp_hp_version}" != "${l_system_version}" ] ;then
                    rm -fr $G_OS_HP_DIR_BAK
                    del_bak=1
                    OS_echo "rm hotpatch directory:${G_OS_HP_DIR_BAK}."
                fi
            else
                rm -fr $G_OS_HP_DIR_BAK
                del_bak=1
            fi      
        fi
        local L_LINK=`ls -l "${G_OS_HP_DIR_LINK}" | awk '{print $11}'`  
        if [ $del_cur == 1 ] && [ "${L_LINK}" == "${G_OS_HP_CUR}" ];then
            cd "${G_OS_HP_DIR}"
            rm -f "${G_OS_HP_LINK}"
            if [ -d "${G_OS_HP_BAK}" ];then
                ln -s "${G_OS_HP_BAK}" "${G_OS_HP_LINK}"
            fi
            OS_echo "rm hotpatch link:${G_OS_HP_CUR}."  
        fi
        if [ $del_bak == 1 ] && [ "${L_LINK}" == "${G_OS_HP_BAK}" ];then
            cd "${G_OS_HP_DIR}"
            rm -f "${G_OS_HP_LINK}"
            if [ -d "${G_OS_HP_CUR}" ];then
                ln -s "${G_OS_HP_CUR}" "${G_OS_HP_LINK}"
            fi
            OS_echo "rm hotpatch link:${G_OS_HP_BAK}."  
        fi
    fi
        
    OS_echo "begin to set $G_HOT_PATCH_BOOT_DIR for read-only when del all hotpatch."
    sync    
    OS_SetStartUpDiskProperty $G_HOT_PATCH_BOOT_DIR ro
    L_RET_TEMP=$?
    if [ 0 -ne ${L_RET_TEMP} ] ;then
        OS_echo "can not set [$G_HOT_PATCH_BOOT_DIR] for read-only when del all hotpatch."
        return 1
    fi
    OS_echo "end to set $G_HOT_PATCH_BOOT_DIR for read-only when del all hotpatch."
    
    return 0
}

# **************************************************************************** #
# Function Name: OS_CheckOrDelCurHotpatch
# Description: 
# Parameter:
# Return:
# **************************************************************************** #
function OS_CheckOrDelCurHotpatch()
{
    local pkgsize=$1
    #解压补丁按照补丁大小增加1半评估,120M是C代码加的UPD_XVE_EXTRA_MEM_FOR_UNZIP_PKG
    declare -i needsize=0
    needsize=$pkgsize-120+$(($pkgsize-120))/2
    
    OS_echo "check and del hotpatch dir" >/dev/null
    
    #把启动盘分区设置为可读写
    OS_echo "begin to set $G_HOT_PATCH_BOOT_DIR for write when check hotpatch."
    OS_SetStartUpDiskProperty $G_HOT_PATCH_BOOT_DIR rw
    L_RET_TEMP=$?
    if [ 0 -ne ${L_RET_TEMP} ] ;then
        OS_echo "can not set [$G_HOT_PATCH_BOOT_DIR] for write when check hotpatch."
        return 1
    fi
    OS_echo "end to set $G_HOT_PATCH_BOOT_DIR for write when check hotpatch."
    
    OS_DelCurHotpatch
    
    #如果coffer_bin目录不够，删除除hotpatch和系统lost目录外所有的文件，防止其他模块占用。
    #当前在V3上理论只有boot目录，欧拉debug包存放dump配置，release包没有
    L_AVAILABLE_SIZE=`df -m $G_HOT_PATCH_BOOT_DIR | grep $G_HOT_PATCH_BOOT_DIR |awk '{print $4}'`
    OS_echo "UPGRADE_SH: Cur coffer_bin Capacity(byte) = $L_AVAILABLE_SIZE,  Needed = $needsize" >/dev/null
    if [ $L_AVAILABLE_SIZE -lt $needsize ] ; then
        cd $G_HOT_PATCH_BOOT_DIR
        ls | grep -vw "\." | grep -vw "hotpatch" | grep -vw "lost" | xargs -d "\n" rm -rf
        cd - &>/dev/null
        L_AVAILABLE_SIZE=`df -m $G_HOT_PATCH_BOOT_DIR | grep $G_HOT_PATCH_BOOT_DIR |awk '{print $4}'`
        OS_echo "UPGRADE_SH: Cur coffer_bin Capacity(byte) = $L_AVAILABLE_SIZE,  Needed = $needsize" >/dev/null
        if [ $L_AVAILABLE_SIZE -lt $needsize ] ; then
            sync    
            OS_SetStartUpDiskProperty $G_HOT_PATCH_BOOT_DIR ro
            return 2
        fi
    fi
    
    OS_echo "begin to set $G_HOT_PATCH_BOOT_DIR for read-only when check hotpatch."
    sync    
    OS_SetStartUpDiskProperty $G_HOT_PATCH_BOOT_DIR ro
    L_RET_TEMP=$?
    if [ 0 -ne ${L_RET_TEMP} ] ;then
        OS_echo "can not set [$G_HOT_PATCH_BOOT_DIR] for read-only when check hotpatch."
        return 1
    fi
    OS_echo "end to set $G_HOT_PATCH_BOOT_DIR for read-only when check hotpatch."
    return 0
}
# **************************************************************************** #
# Function Name: OS_UPG_HP_IS_RESUME_IN_UPGRADING
# Description:判断是否是升级过程中补丁恢复
# Parameter: 
# Return: 0                  非升级中恢复补丁
#         1                  升级过程中恢复补丁
# **************************************************************************** #
OS_UPG_HP_IS_RESUME_IN_UPGRADING()
{
    local l_bootLink=$(ls -l $G_BOOT_LINK | awk '{print $11}')
    local upd_state_file=/startup_disk/conf/conf_local/upd_state_atoms.lua
    local sys_version_file=/OSM/conf/versions.conf
    #升级状态文件
    local l_state_upd=/startup_disk/conf/conf_local/state_upd.lua
    local pkg_version=""
    local real_version=""
    #没有升级状态文件，非升级状态恢复补丁
    if [ ! -f ${l_state_upd} ];then
        OS_echo "There is no $l_state_upd,no need resume hotpatch in patch_temp."
        return 0    
    fi
    flow_name=`cat $l_state_upd | grep -oE "OnlineUpgrade|OfflineUpgrade"`
    if [ -z "$flow_name" ];then
        OS_echo "The flow is not OnlineUpgrade or OfflineUpgrade,no need resume hotpatch in patch_temp."
        return 0
    fi
    pkg_version=$(grep -A20 "ParamList" $upd_state_file | grep -m 1 pkgversion | grep -oP "[0-9]+")
    real_version=`grep -A 1 "\[SYS\]" $sys_version_file | tail -1| awk -F'=' '{print $2}'`
    
    #当前运行版本与升级版本不一致，非升级状态恢复补丁
    if [ -n "$pkg_version" ] && [ "$pkg_version" != "$real_version" ];then
        OS_echo "The sys-version do not match the upgrade-version,no need resume hotpatch in patch_temp."
        return 0
    fi
    
    #G_OS_HP_DIR_TEMP_CONF不存存在，非升级状态下恢复补丁
    if [ ! -f $G_OS_HP_DIR_TEMP_CONF ];then
        OS_echo "There is no $G_OS_HP_DIR_TEMP_CONF,no need resume hotpatch in patch_temp."
        return 0
    fi
    return 1
}

# **************************************************************************** #
# Function Name: OS_TAR_HOTPATCH_PACKAGE
# Description:解压补丁包
# Parameter: 
# Return: 0                         解压成功
#         G_ERROR_PKG_INEXISTENCE   补丁包不存在
#         G_OS_HP_RET_SYS_DISK_ERR  系统盘故障
#         G_OS_HP_RET_PACKET_ERR    包错误
# **************************************************************************** #
OS_TAR_HOTPATCH_PACKAGE()
{
    local l_pkg_name=$1
    #判断补丁包是否存在
    if [ ! -e $l_pkg_name ];then
        OS_echo "The hotpatch package[${l_pkg_name}] is not exist, no need tar and check hotpatch."
        return ${G_ERROR_PKG_INEXISTENCE}
    fi   
    #把补丁目录分区设置为可写
    OS_echo "begin to set $G_HOT_PATCH_BOOT_DIR for write when tar hotpatch."
    OS_SetStartUpDiskProperty $G_HOT_PATCH_BOOT_DIR rw
    L_RET_TEMP=$?
    if [ 0 -ne ${L_RET_TEMP} ] ;then
        OS_echo "can not set [$G_HOT_PATCH_BOOT_DIR] for write when tar hotpatch."
        return ${G_OS_HP_RET_SYS_DISK_ERR}
    fi  
    OS_echo "end to set $G_HOT_PATCH_BOOT_DIR for write when tar hotpatch."
    #临时解压目录不存在则创建,否则删除下面所有的文件    
    if [ ! -d "${G_OS_HP_DIR_TEMP}" ];then
        mkdir -p ${G_OS_HP_DIR_TEMP} 
        L_RET=$?
        if [ 0 -ne ${L_RET} ];then
            OS_echo "Create hotpatch directory[${G_OS_HP_DIR_TEMP}] failed[${L_RET}]."
            rm -f $l_pkg_name
            OS_SetStartUpDiskProperty $G_HOT_PATCH_BOOT_DIR ro
            return ${G_OS_HP_RET_SYS_DISK_ERR}
        fi
    else
        rm -rf ${G_OS_HP_DIR_TEMP}/*
        L_RET=$?
        if [ 0 -ne ${L_RET} ];then
            OS_echo "Remove hotpatch files[${G_OS_HP_DIR_TEMP}] failed[${L_RET}]."
            rm -f $l_pkg_name
            OS_SetStartUpDiskProperty $G_HOT_PATCH_BOOT_DIR ro
            return ${G_OS_HP_RET_SYS_DISK_ERR}
        fi
    fi 
    OS_echo "Check hotpatch temp diretory[${G_OS_HP_DIR_TEMP}] complete."
    #解压缩热补丁包文件到目录下
    tar -xzvf ${l_pkg_name} -C ${G_OS_HP_DIR_TEMP} &>/dev/kmsg
    L_RET=$?
    if [ 2 -eq ${L_RET} ];then
        OS_echo "Decompress hotpatch to ${G_OS_HP_DIR_TEMP} failed."
        OS_echo "Perhaps the hotpatch is wrong."
        rm -rf ${G_OS_HP_DIR_TEMP} 
        OS_SetStartUpDiskProperty $G_HOT_PATCH_BOOT_DIR ro
        rm -f $l_pkg_name
        return ${G_ERROR_PKG_INVALIDE}
    elif [ 0 -ne ${L_RET} ];then
        OS_echo "Decompress hotpatch to ${G_OS_HP_DIR_TEMP} failed, do not upgrade."
        rm -rf ${G_OS_HP_DIR_TEMP} 
        OS_SetStartUpDiskProperty $G_HOT_PATCH_BOOT_DIR ro
        rm -f $l_pkg_name
        return ${G_ERROR_PKG_INVALIDE}
    fi
    OS_echo "Decompress hotpatch package to ${G_OS_HP_DIR_TEMP} complete."
    rm -f $l_pkg_name
    #sha256文件校验
    OS_HP_CHECK_MD5 "${G_OS_HP_DIR_TEMP}"
    L_RET=$?
    if [ 0 -ne ${L_RET} ];then
        OS_echo "Check hotpatch[${G_OS_HP_DIR_TEMP}] sha256sum failed."
        rm -rf ${G_OS_HP_DIR_TEMP}      
        OS_SetStartUpDiskProperty $G_HOT_PATCH_BOOT_DIR ro
        return ${G_OS_HP_RET_PACKET_ERR}
    fi
    #多次导包upgrade_sh -k -m会清除/OSM/update目录，影响后续升级前检查
    cp -f $G_UPDATE_TMP_DIR/versions.conf /OSM/update
    #把补丁目录分区设置为可写
    OS_echo "begin to set $G_HOT_PATCH_BOOT_DIR for read-only when tar hotpatch."
    OS_SetStartUpDiskProperty $G_HOT_PATCH_BOOT_DIR ro
    L_RET_TEMP=$?
    if [ 0 -ne ${L_RET_TEMP} ] ;then
        OS_echo "can not set [$G_HOT_PATCH_BOOT_DIR] for read-only when tar hotpatch."
        return ${G_OS_HP_RET_SYS_DISK_ERR}
    fi  
    OS_echo "end to set $G_HOT_PATCH_BOOT_DIR for read-only when tar hotpatch."
    return 0
}

# **************************************************************************** #
# **************************************************************************** #
# Function Name: OS_CHECK_HOTPATCH_VERSION
# Description:校验补丁包
# Parameter: 
# Return: 0                         解压成功
#         G_OS_HP_RET_SYS_DISK_ERR  系统盘故障
#         G_OS_HP_RET_PACKET_ERR    包错误
# **************************************************************************** #
OS_CHECK_HOTPATCH_VERSION()
{
    local L_PATCH_D_VERSION=""
    local L_UPD_SYS_D_VERSION=""    
    if [ -f ${G_OS_HP_DIR_TEMP_CONF} ];then
        L_PATCH_D_VERSION=`cat "${G_OS_HP_DIR_TEMP_CONF}" 2>/dev/kmsg| grep ^SysVersionSupported |awk '{print $2}'`
    else
        return ${G_OS_HP_RET_NO_INSTALL}
    fi

    L_UPD_SYS_D_VERSION=`grep -A 1 "\[SYS\]" $G_UPDATE_TMP_DIR/versions.conf | tail -1| awk -F'=' '{print $2}'`
    if [ "${L_PATCH_D_VERSION}" != "${L_UPD_SYS_D_VERSION}" ] ;then
        OS_echo "This hotpatch package[${L_PATCH_D_VERSION}] can not match with system version[${L_UPD_SYS_D_VERSION}]."
        rm -rf ${G_OS_HP_DIR_TEMP} 
        return ${G_ERROR_PKG_INVALIDE}
    fi      
    return 0
}
# **************************************************************************** #
# Function Name: OS_UPG_HP_RECORE_RESUME_STATUS
# Description:记录升级过程中补丁恢复情况
# Parameter: 
# Return: 0                  成功
#         1                  失败
# **************************************************************************** #
OS_UPG_HP_RECORE_RESUME_STATUS()
{
    local l_resume_stage=$1
    local l_is_upgrading=$2
    local l_resume_result=$3
    if [ $l_is_upgrading == "false" ];then
        OS_echo "Not resume hotpatch in upgrading."
        return 0
    fi
    if [ ! -f $G_UPD_HP_RESUME_FLAG_FILE ];then
        OS_echo "Touch the upd_hotpatch_resume.log file."
        touch $G_UPD_HP_RESUME_FLAG_FILE
    fi
    #判断补丁配置文件中是否存在升级补丁
    l_upgrade=`grep -w "upgrade" $G_OS_HP_DIR_TEMP_CONF`
    if [ -z "${l_upgrade}" ];then
        OS_echo "There is no upgrade hotpatch."
        echo "The $l_resume_stage resume succeed." >> "$G_UPD_HP_RESUME_FLAG_FILE"
        return 0
    fi
    #日志不能动，升级后校验会根据字符串判断补丁恢复是否成功
    if [ "$l_resume_result" != "$G_OS_HP_RET_SUCC" ] ;then
        echo "The $l_resume_stage resume fail." >> "$G_UPD_HP_RESUME_FLAG_FILE"
    else
        echo "The $l_resume_stage resume succeed." >> "$G_UPD_HP_RESUME_FLAG_FILE"    
    fi
    return 0
    
}
# **************************************************************************** #
# Function Name: OS_CHECK_PACKAGE_EXIST
# Description:判断包的存在性
# Parameter: 
# Return: 0                  成功
#         1                  失败
# **************************************************************************** #
OS_CHECK_PACKAGE_EXIST()
{
    local l_pkg_name=$1
    if [ ! -f "${l_pkg_name}" ]; then 
        OS_echo "The hotpatch package is not exist."
        return 1
    fi
    return 0
}
# ----------------------------------main-------------------------------------- #
#检查启动分区是否mount
G_IS_MOUNT=`mount |grep "${G_HOT_PATCH_BOOT_DIR}"`
if [ -z "${G_IS_MOUNT}" ];then
    OS_echo "${G_HOT_PATCH_BOOT_DIR} is not mount when processing hotpatch."
    exit 1
fi

if [ $# -eq 0 ];then
        OS_UPG_HP_HELP
        exit 0
fi

G_PROCESS_RET=${G_OS_HP_RET_SUCC}
while getopts hm:w:p:rcdstv:kg:e:f:ua options 2>/dev/null
do
    case $options in
        h)  #显示帮助信息
            OS_UPG_HP_HELP
            exit 0
        ;;
        m)  #检查系统剩余内存(单位为M)
            G_CHECK_MEM_SIZE=$OPTARG
            OS_UPG_HP_CHECK_MEM "${G_CHECK_MEM_SIZE}"
            G_PROCESS_RET=$?
            if [ "${G_PROCESS_RET}" != "0" ]; then
                echo "check free memory fail."
            else
                echo "check free memory pass."
            fi
            exit ${G_PROCESS_RET}
        ;;
        w)  #热补丁安装/回退/删除前检查
            G_PREPARE_TYPE=$OPTARG
            G_PREPARE_PKG=$3
            OS_UPG_HP_CHECK "${G_PREPARE_TYPE}" "${G_PREPARE_PKG}" &>/dev/kmsg
            G_PROCESS_RET=$?
            if [ "${G_PROCESS_RET}" != "${G_OS_HP_RET_SUCC}" ]; then
                echo "check hotpatch action fail."
            else
                echo "check hotpatch action success."
            fi
            exit ${G_PROCESS_RET}
        ;;
        p)  #安装热补丁包
            G_HOTPATH_PKG_NAME=$OPTARG
            G_HOTPATH_PKG_STAGE=$3  
            L_IS_INSTALL_IN_UPGRADING=$4  
            if [ "${G_HOTPATH_PKG_STAGE}" != "mini" ] ;then
                /OSM/script/notify_xnet_patch.sh begin
                G_PROCESS_RET=$?
                if [ "$G_PROCESS_RET" != "0" ]; then
                    OS_echo "Xnet prepare for patch fail, return $G_PROCESS_RET"
                    echo "Xnet prepare for patch fail, return $G_PROCESS_RET"
                    exit 1
                fi
            else
                OS_echo "mini mode, no need to call xnet begin." 
            fi
            OS_UPG_HP_INSTALL "${G_HOTPATH_PKG_NAME}" "${G_HOTPATH_PKG_STAGE}" "${L_IS_INSTALL_IN_UPGRADING}" &>/dev/kmsg
            G_PROCESS_RET=$?
            if [ "${G_HOTPATH_PKG_STAGE}" != "mini" ] ;then
                /OSM/script/notify_xnet_patch.sh end
            else
                OS_echo "mini mode, no need to call xnet end." 
            fi
            if [ "${G_PROCESS_RET}" != "${G_OS_HP_RET_SUCC}" ] ;then
                OS_echo "install hotpatch fail."
                echo "install hotpatch fail."
            else
                echo "install hotpatch success."
            fi                
            exit ${G_PROCESS_RET}
        ;;
        c)  #确认热补丁包，使其处于运行态
            OS_UPG_HP_CONFIRM &>/dev/kmsg
            G_PROCESS_RET=$?
            if [ "$G_PROCESS_RET" != "$G_OS_HP_RET_SUCC" ] ;then
                OS_echo "confirm hotpatch fail."
                echo "confirm hotpatch fail."
            else
                echo "confirm hotpatch success."
            fi
            exit ${G_PROCESS_RET}
        ;;      
        r)  #回退热补丁包
            /OSM/script/notify_xnet_patch.sh begin
            G_PROCESS_RET=$?
            if [ "$G_PROCESS_RET" != "0" ]; then
                OS_echo "Xnet prepare for patch fail, return $G_PROCESS_RET"
                exit 1
            fi
            OS_UPG_HP_ROLLBACK &>/dev/kmsg
            G_PROCESS_RET=$?
            /OSM/script/notify_xnet_patch.sh end
            if [ "$G_PROCESS_RET" != "$G_OS_HP_RET_SUCC" ] ;then
                OS_echo "rollback hotpatch fail."
                echo "rollback hotpatch fail."
            else
                echo "rollback hotpatch success."
            fi
            exit ${G_PROCESS_RET}
        ;;  
        d)  #删除系统中的所有热补丁
            L_LINK_FLAG="true"
            #删除特殊补丁
            OS_UPG_HP_DEL_SPECIAL_PATCH

            if [ ! -L "${G_OS_HP_DIR_LINK}" ];then
                L_LINK_FLAG="false"
            fi
            /OSM/script/notify_xnet_patch.sh begin
            G_PROCESS_RET=$?
            if [ "$G_PROCESS_RET" != "0" ]; then
                OS_echo "Xnet prepare for patch fail, return $G_PROCESS_RET"
                exit 1
            fi
            OS_UPG_HP_DEL_ALL &>/dev/kmsg 
            G_PROCESS_RET=$?
            /OSM/script/notify_xnet_patch.sh end
            if [ "$G_PROCESS_RET" != "$G_OS_HP_RET_SUCC" ] ;then
                OS_echo "delete all hotpatch fail."
                echo "delete all  hotpatch fail."
            #R2R5&R5C01 P12I-3843 modified2013/1/11 begin
            else
                echo "delete all hotpatch success."
                if [ "$L_LINK_FLAG" = "true" ];then
                    echo "hotpatch del">$G_HOTPATCH_STATUS
                fi
             #R2R5&R5C01 P12I-3843 modified2013/1/11 end
            fi
            exit ${G_PROCESS_RET}
           
        ;;
        s)  #系统启动，恢复热补丁运行(在os_load_ok.sh中会调用)
            L_RESUME_STAGE=$2
            L_IS_RESUME_IN_UPGRADING="false"
            OS_UPG_HP_IS_RESUME_IN_UPGRADING
            if [ $? == 1 ];then
                OS_echo "Resume hotpatch in upgrading status."    
                L_IS_RESUME_IN_UPGRADING="true"
            fi       
            if [ "$L_RESUME_STAGE" == "1" ]; then
                OS_UPG_HP_RECOVER_HOTPACTH_FILE
                OS_UPG_HP_RESUME_RUN "first" "$L_IS_RESUME_IN_UPGRADING" &>/dev/kmsg
                G_PROCESS_RET=$?
                OS_UPG_HP_RECORE_RESUME_STATUS  "first" "$L_IS_RESUME_IN_UPGRADING" "$G_PROCESS_RET"
            elif [ "$L_RESUME_STAGE" == "2" ]; then
                /OSM/script/notify_xnet_patch.sh begin
                G_PROCESS_RET=$?
                if [ "$G_PROCESS_RET" != "0" ]; then
                    OS_echo "Xnet prepare for patch fail, return $G_PROCESS_RET"
                    exit 1
                fi
                OS_UPG_HP_RESUME_RUN "second" "$L_IS_RESUME_IN_UPGRADING"&>/dev/kmsg
                G_PROCESS_RET=$? 
                OS_UPG_HP_RECORE_RESUME_STATUS  "second" "$L_IS_RESUME_IN_UPGRADING" "$G_PROCESS_RET"
                /OSM/script/notify_xnet_patch.sh end
            else
                /OSM/script/notify_xnet_patch.sh begin
                G_PROCESS_RET=$?
                if [ "$G_PROCESS_RET" != "0" ]; then
                    OS_echo "Xnet prepare for patch fail, return $G_PROCESS_RET"
                    exit 1
                fi
                OS_UPG_HP_RESUME_RUN "all" "$L_IS_RESUME_IN_UPGRADING" &>/dev/kmsg
                G_PROCESS_RET=$? 
                OS_UPG_HP_RECORE_RESUME_STATUS  "all" "$L_IS_RESUME_IN_UPGRADING" "$G_PROCESS_RET"
                /OSM/script/notify_xnet_patch.sh end   
            fi
            if [ "$G_PROCESS_RET" != "$G_OS_HP_RET_SUCC" ] ;then
                OS_echo "resume hotpatch fail."
                echo "resume hotpatch fail."
            else
                OS_echo "resume hotpatch success."
                echo "resume hotpatch success."
            fi
            exit ${G_PROCESS_RET}
        ;;
        t)  #系统启动，恢复热补丁运行(系统控制调用，不需要调用XNET脚本)
            L_IS_RESUME_IN_UPGRADING="false"
            OS_UPG_HP_IS_RESUME_IN_UPGRADING
            if [ $? == 1 ];then
                OS_echo "Resume hotpatch in upgrading status."    
                L_IS_RESUME_IN_UPGRADING="true"
            fi  
            OS_UPG_HP_RESUME_RUN "second" "$L_IS_RESUME_IN_UPGRADING" &>/dev/kmsg
            G_PROCESS_RET=$?         
            if [ "$G_PROCESS_RET" != "$G_OS_HP_RET_SUCC" ] ;then
                OS_echo "resume hotpatch fail."
            else
                OS_echo "resume hotpatch success."
            fi
            OS_UPG_HP_RECORE_RESUME_STATUS  "second" "$L_IS_RESUME_IN_UPGRADING" "$G_PROCESS_RET"
            exit ${G_PROCESS_RET}
        ;;
        v)  #查询热补丁版本
            L_INACTIVE_PKG=$OPTARG
            OS_UPG_HP_GET_VER "$L_INACTIVE_PKG"
            G_PROCESS_RET=$?
            if [ "$G_PROCESS_RET" != "$G_OS_HP_RET_SUCC" ] ;then
                OS_echo "get hotpatch version fail."
                echo "get hotpatch version fail."
            fi
            exit ${G_PROCESS_RET}
        ;;
        k)  #删除补丁目录中的热补丁
            OS_DEL_FILES_IN_UPDATE_DIR
            exit $?
        ;;
        #/* XVE V2R1 P12N-4681 modified 2012/10/08 begin */
        g)  #特殊热补丁检查
            OS_CHECK_PACKAGE_EXIST "$OPTARG"
            G_PROCESS_RET=$?
            if [ "${G_PROCESS_RET}" != "${G_OS_HP_RET_SUCC}" ]; then
                echo "The hotpatch package is not exist."
                exit 1
            fi
            OS_UPG_HP_SPECIAL_PACKAGE_CHECK "$OPTARG"
            G_PROCESS_RET=$?
            if [ "$G_PROCESS_RET" != "$G_OS_HP_RET_SPECIAL_PATCH" ] ;then
                OS_echo "No special hot package found." 
                echo "No special hot package found."
            else
                echo "It is special hotpatch."
            fi            
            exit ${G_PROCESS_RET}
        ;;
        e)  #检查补丁存放目录空间是否足够
            L_PKG_SIZE=$OPTARG
            OS_CheckOrDelCurHotpatch $L_PKG_SIZE
            exit $?
        ;;
        #/* XVE V2R1 P12N-4681 modified 2012/10/08 end */
        f) #解压补丁包
            OS_echo "UPGRADE_HOTPATCH_SH [-t]: Begin to tar the hotpatch." >/dev/kmsg
            L_HP_PACKAGE_NAME=$2
            #判断是否存在升级包
            if [ ! -d $G_UPDATE_TMP_DIR ];then
                OS_echo "The $G_UPDATE_TMP_DIR is not exist, no need tar hotpatch."
                exit 0
            fi
            #判断升级过程打补丁标志是否存在
            if [ ! -f /tmp/upgrade_and_hotpatch.txt ];then
                OS_echo "The upgrade_and_hotpatch file flag is not exist, no need tar hotpatch."
                exit 0
            fi
            OS_TAR_HOTPATCH_PACKAGE ${L_HP_PACKAGE_NAME}
            G_PROCESS_RET=$?
            if [ "$G_PROCESS_RET" != "$G_OS_HP_RET_SUCC" ] ;then
                rm -f /tmp/upgrade_and_hotpatch.txt
            fi
            OS_echo "UPGRADE_HOTPATCH_SH [-t]: End to tar the hotpatch." >/dev/kmsg         
            exit ${G_PROCESS_RET}
        ;;        
        u)  #校验补丁包
            #判断是否存在升级包
            OS_echo "UPGRADE_HOTPATCH_SH [-u]: Begin to verify the hotpatch." >/dev/kmsg
            if [ ! -d $G_UPDATE_TMP_DIR ];then
                OS_echo "The $G_UPDATE_TMP_DIR is not exist, no need verify hotpatch."
                exit 0
            fi
           #判断升级过程打补丁标志是否存在
            if [ ! -f /tmp/upgrade_and_hotpatch.txt ];then
                OS_echo "The upgrade_and_hotpatch file flag is not exist, no need verify hotpatch."
                exit 0
            fi
            rm -f /tmp/upgrade_and_hotpatch.txt
            OS_CHECK_HOTPATCH_VERSION ${L_HP_PACKAGE_NAME}   
            G_PROCESS_RET=$?            
            OS_echo "UPGRADE_HOTPATCH_SH [-u]: End to verify the hotpatch." >/dev/kmsg
            exit ${G_PROCESS_RET}
        ;;
        a)  #/OSM/bin/补丁备份到系统盘
            OS_CP_BACKUP_FILE_AFTER_POWERON 
            G_PROCESS_RET=$?
            exit ${G_PROCESS_RET}
        ;;
        *)
            OS_UPG_HP_HELP
            exit 0
        ;;
    esac
done

#R2 P11G-4082 modified2012/02/05 end
