此篇文章主要针对于DELL服务器racadm环境的BIOS配置,以F1/F2 Error提示和硬盘启动顺序更改为例。racadm基础用法可以参考前面文章。

#!/bin/bash
# Sly Chen
# date : 2016-09-14
IDRAC_IP=$1
USER='root'
PASSWD='calvin'
# racadm通用语法定义成变量
COMMAND="racadm -r ${IDRAC_IP} -u $USER -p $PASSWD"
# 获取控制器
CONTROLLER=$($COMMAND storage get controllers --nocertwarn | sed 's/\r/\n/g' | egrep "RAID")
# 当前作业数量统计
JOBS=$($COMMAND jobqueue view --nocertwarn | sed -n '/---/,$p' | wc -l)

# 以红色显示
function red() {
    echo -e "\033[31;40m$*\033[0m"
}

# 以绿色显示
function green() {
    echo -e "\033[32;40m$*\033[0m"
}

function sys_reboot {
        # 定义物理机重启函数
    $COMMAND serveraction powercycle --nocertwarn &> /dev/null
    if [ $? -eq 0 ]; then
        echo "${IDRAC_IP}: server powercycle success"
    else
        red "${IDRAC_IP}: server powercycle false"
        exit
    fi
}

function jobdel () {
    # 删除所有作业记录
    echo "${IDRAC_IP}: start jobqueue delete."
    while [ ${JOBS} -ne 2 ]; do
        racadm -r ${IDRAC_IP} -u ${USER} -p ${PASSWD} jobqueue delete --all --nocertwarn &> /dev/null
        local JOBS=$($COMMAND jobqueue view --nocertwarn | sed -n '/---/,$p' | wc -l)
    done
    echo "${IDRAC_IP}: jobqueue delete success."
}

function get_job_id (){
    # 此函数用于判断任务作业百分比,达到100%时确认作业完成
    # 启用LC
    $COMMAND set LifeCycleController.LCAttributes.LifecycleControllerState 1 &> /dev/null
    # 作业进度100%则退出死循环
    while true; do
        # 获取作业进度百分比
        JOB_PERCENT=$(${COMMAND} jobqueue view --nocertwarn | egrep -A7 "$JOBID" | awk -F "=" '/Percent Complete/ {print $2}' | sed 's/\r//g')
        if [ -z "$JOB_PERCENT" ]; then
            red "${IDRAC_IP}: Jobqueue is empty"
            exit
        elif echo $JOB_PERCENT | egrep '100' &> /dev/null; then
            break
        fi
        sleep 10
    done
    echo "${IDRAC_IP}: Jobqueue create success."
}

function jobsub_bios (){
    # 提交BIOS任务
    echo "${IDRAC_IP}: Jobqueue create BIOS.Setup, May be need 15min. Start time: $(date +%Y-%m-%d-%H:%M:%S)"
    # 提交BIOS作业
    local JOBID=$($COMMAND jobqueue create BIOS.Setup.1-1 --nocertwarn | awk '/Commit JID/ {print $4}' | sed 's/\r//g')
    [ -z "$JOBID" ] && red "${IDRAC_IP}: Did not get the JID" && exit
    ${COMMAND} serveraction powercycle --nocertwarn &> /dev/null
    # 判断作业百分比
    get_job_id ${IDRAC_IP}
    echo "${IDRAC_IP}: BIOS.Setup task jobsub done"
}

# job submit
function jobsub (){
    echo "${IDRAC_IP}: Jobqueue create Hard disk task, May be need 15min. Start time: $(date +%Y-%m-%d-%H:%M:%S)"
    # 提交阵列作业,获取作业JID
    local JOBID=$($COMMAND jobqueue create ${CONTROLLER} -s TIME_NOW -r forced --nocertwarn | awk '/Commit JID/ {print $4}' | sed 's/\r//g')
    # JID为空则退出
    [ -z "$JOBID" ] && red "${IDRAC_IP}: Did not get the JID" && exit
    ${COMMAND} serveraction powercycle --nocertwarn &> /dev/null
    # 判断作业百分比
    get_job_id ${IDRAC_IP}
    echo "${IDRAC_IP}: BIOS Controller task jobsub done"
}

function disable_error_prompt {
    # BIOS F1/F2 ErrPrompt
    echo "${IDRAC_IP}: Start disable ErrPrompt"
    $COMMAND set BIOS.MiscSettings.ErrPrompt Disabled &> /dev/null
    echo "${IDRAC_IP}: ErrPrompt disabled done"
}

function set_hard_disk_first_boot () {
    # 取出所有启动设备重新排序,sort命令默认会比较首字符,磁盘启动会优先于网卡
    BOOT_DRIVES_LIST=$($COMMAND get BIOS.BiosBootSettings.BootSeq | awk -F '[=]' '/BootSeq/{print $2}' | sed 's/\r$//;s/,/ /g' | xargs -n 1 | sort | xargs -n 9 | sed 's/ /,/g')
    $COMMAND set BIOS.BiosBootSettings.BootSeq $BOOT_DRIVES_LIST &> /dev/null
}

function bios_setup_check () {
    # 定义BIOS检查函数
    ERR_PROMPT_STATU_CHK=$($COMMAND get BIOS.MiscSettings.ErrPrompt | awk -F '=' '/ErrPrompt/ {print $2}' | sed 's/\r$//')
    # ErrorPrompt状态不为Disabled则报错
        if [ "$ERR_PROMPT_STATU_CHK" == "Disabled" ]; then
            green "${IDRAC_IP}: BIOS F1/F2 error prompt is Disabled."
    else
            red "${IDRAC_IP}: BIOS F1/F2 error prompt is Enabled"
            exit
    fi

    FIRST_BOOT_NAME_CHK=$($COMMAND get BIOS.BiosBootSettings.BootSeq  | awk -F "[=.]" '/BootSeq/ {print $2}' | sed 's/\r$//')
    # 第一启动不为HardDisk则报错,如果有包含CD-ROM启动,排序需往后移 
        if [ "$FIRST_BOOT_NAME_CHK" == "HardDisk" ]; then
        green "${IDRAC_IP}: After testing,Hard disk first boot."
    else
        red "${IDRAC_IP}: The boot sequence: ${FIRST_BOOT_NAME_CHK}"
        exit
    fi
}

function main (){
    jobdel ${IDRAC_IP}
    # 获取第一启动项设备名称
    FIRST_BOOT_NAME=$($COMMAND get BIOS.BiosBootSettings.BootSeq  | awk -F "[=.]" '/BootSeq/ {print $2}' | sed 's/\r$//')
    # 获取ErrPrompt项状态
    ERR_PROMPT_STATU=$($COMMAND get BIOS.MiscSettings.ErrPrompt | awk -F '=' '/ErrPrompt/ {print $2}' | sed 's/\r$//')
    # 如果BIOS当前设置为硬盘第一启动,且F1/F2错误提示为Disabled,则退出,否则执行更改函数
    if [ "$FIRST_BOOT_NAME" == "HardDisk"  -a "$ERR_PROMPT_STATU" == "Disabled" ]; then
        echo "${IDRAC_IP}: BIOS F1/F2 errot prompt default is Disabled"
        echo "${IDRAC_IP}: Hard disk drive default is the first boot"
        exit
    elif [ "$FIRST_BOOT_NAME" != "HardDisk"  -a "$ERR_PROMPT_STATU" == "Disabled" ]; then
        set_hard_disk_first_boot
        echo "${IDRAC_IP}: BIOS F1/F2 errot prompt default is Disabled"
        jobsub_bios ${IDRAC_IP}
    elif [ "$FIRST_BOOT_NAME" == "HardDisk"  -a "$ERR_PROMPT_STATU" == "Enabled" ]; then
        echo "${IDRAC_IP}: Hard disk drive default is the first boot"
        disable_error_prompt
        jobsub_bios ${IDRAC_IP}
    elif [ "$FIRST_BOOT_NAME" != "HardDisk"  -a "$ERR_PROMPT_STATU" == "Enabled" ]; then
        set_hard_disk_first_boot
        disable_error_prompt
        jobsub_bios ${IDRAC_IP}
    else
        red "${IDRAC_IP}: Get Bios valus error"
        exit 3
    fi
    bios_setup_check ${IDRAC_IP}
}

main ${IDRAC_IP}