#! /bin/bash
trap "" 1 2 3 15

source /OSM/script/os_load_base.sh

#问题单号:P12N-5195  描述:安全版本  修改人: 时间:20121113 begin
#*****************************************************************************#
#Function Name:HANDLE_RESCUE_DIR
#Description:处理boot_rescue目录中的系统文件
#Parameter: none
#Return: 0
#*****************************************************************************#
HANDLE_RESCUE_DIR()
{
    local L_PWD=`pwd`
    local L_BOOTLOADER=`ls -l /startup_disk/image|grep grub |awk '{print $9}'`

    cd /startup_disk/image
    
    #那么就使用系统版本备份boot_rescue目录
    rm -rf boot_rescue 2>/dev/null

    # modified on 2014-01-04, not copy update_pkg to boot_rescue for controling the boot_rescue's size, begin
    mkdir boot_rescue 
    for file_name in boot_osp/* 
    do 
        local L_DIR="boot_osp"
        if [ "${file_name}" != "${L_DIR}/update_pkg" ] && [ "${file_name}" != "${L_DIR}/initrd" ] && [ "${file_name}" != "${L_DIR}/bzImage" ] && [ "${file_name}" != "${L_DIR}/hotpatch" ] && [ "${file_name}" != "${L_DIR}/components" ]; then 
            cp -af ${file_name} boot_rescue 
        fi 
    done 
    # modified on 2014-01-04 end
    
    cd boot_rescue
    ln -s ../boot_osp/initrd initrd
    ln -s ../boot_osp/bzImage bzImage
    
    cd conf
    if [ "${L_BOOTLOADER}" == "grub2" ]; then
        sed 's/os_boot_osp/os_rescue/g' grub.cfg > tmp1
        sed 's/os_only/os_rescue/g' tmp1 > tmp2
        rm -f grub.cfg 2>/dev/kmsg
        rm -f tmp1 2>/dev/kmsg
        mv tmp2 grub.cfg 2>/dev/kmsg
        chmod +x grub.cfg
    else
        sed 's/os_boot_osp/os_rescue/g' menu.lst > tmp1
        sed 's/os_only/os_rescue/g' tmp1 > tmp2
        rm -f menu.lst 2>/dev/kmsg
        rm -f tmp1 2>/dev/kmsg
        mv tmp2 menu.lst 2>/dev/kmsg
        chmod +x menu.lst
    fi
    cd ${L_PWD}
    return 0
}
#问题单号:P12N-5195  描述:安全版本  修改人: 时间:20121113 end

#------------------------main---------------------------------------#

L_BOOT_DISK=`cat /proc/bootdisk |grep -a "boot disk is" |awk '{print $4}'`
#获取启动分区名
case "$L_BOOT_DISK" in
    manage_network|heartbeat_network) 
        exit 0
        ;;
    *)
        #检查是否存在raid
        OS_Check_Raid_Device
        if [ $? -eq 0 ];then
            L_BOOT_DISK=${G_DISK_PARTITION_RAID}
        fi
        ;;
esac

#step2. 处理升级指向
L_BOOT_LINK=`ls -l $G_DISK_MOUNT_IMAGE | grep -a "boot ->" |awk '{print $11}'`
if [ "$L_BOOT_LINK" = "boot_osp_bak" ];then
    #更改启动盘image分区可写
    OS_SetStartUpDiskProperty $L_BOOT_DISK rw
    
    cd $G_DISK_MOUNT_IMAGE    
    mv -f boot_osp os_tmp
    mv -f boot_osp_bak boot_osp
    mv -f os_tmp boot_osp_bak
    #给other组用户增加读权限，防止以该目录无法进行网络引导
    chmod 755 boot_osp_bak
    chmod 755 boot_osp
    rm -f boot
    ln -s boot_osp boot
    #问题单号:P12N-5195  描述:安全版本  修改人: 时间:20121113 begin
    HANDLE_RESCUE_DIR
    if [ "$?" != 0 ];then
        OS_echo "Upgrade Handling rescue abort"
    fi
    #问题单号:P12N-5195  描述:安全版本  修改人: 时间:20121113 end
    
    #重新给boot_osp目录生成完整的校验文件DTS2017090403656 
    /OSM/script/os_system_update.sh -Z $G_DISK_MOUNT_IMAGE/boot_osp 

    chattr -R +A boot/* &>/dev/null
    
    if [ ! -f $G_DISK_MOUNT_IMAGE/boot/versions.conf ];then
        OS_echo "[os_load_ok.sh]Can not find /boot/versions.conf!Mount Error!"
        exit 1
    fi

    if [ ! -d /boot/conf ] ;then
        mkdir -p /boot/conf
    fi
    cp -af $G_DISK_MOUNT_IMAGE/boot/versions.conf /boot/conf/
    
    #非工具升级场景，换备件、xve_up或者upgradesystem命令场景
    if [ ! -f /startup_disk/conf/conf_local/state_upd.lua ];then
        OS_echo "[os_load_ok.sh]It is not upgrade, del boot_osp_bak."
        cd /startup_disk/image/boot_osp_bak
        # 删除 boot_osp_bak 中除了 versions.conf 的所有内容;类似于os_system_update.sh -L
        ls | grep -vw "\." | grep -vw "versions.conf" | xargs rm -rf
        cd -
    fi
    # 设置成只读
    OS_SetStartUpDiskProperty $L_BOOT_DISK ro

    OS_echo "********* system update success! ***********"
fi

#/OSM/bin/补丁备份到系统盘
/OSM/script/os_upgrade_hotpatch.sh -a
exit 0