#!/bin/bash
##filename:cleardisk.sh
##function:把硬盘置为 空盘

DISK_NAME="$1"

###################################################
# 显示帮助
###################################################
function show_help()
{
    echo "Usage: $0 /dev/sdx"
}

###################################################
# 参数检查
###################################################
function chk_params()
{
    for cmd in lsscsi parted
    do
        if [ -z "$cmd" ]; then
            continue
        fi

        #检查命令是否存在
        which $cmd > /dev/null 2>&1
        if [ $? -ne 0 ]; then
            echo "${cmd} not exist"
            return 1
        fi
    done

    #参数不能为空
    if [ -z "${DISK_NAME}" ]; then
        show_help
        return 1
    fi

    #参数必须为块设备
    if [ ! -b "${DISK_NAME}" ]; then
        echo "${DISK_NAME} is not a block device."
        show_help
        return 1
    fi

    #入参必须在硬盘列表中
    lsscsi | grep "\/dev" | grep -v DVD | grep -v -i Floppy | grep " ${DISK_NAME} " > /dev/null
    if [ $? -ne 0 ]; then
        echo "Error: ${DISK_NAME} is not disk."
        show_help
        return 1
    fi

    return 0
}

###################################################
# 判断硬盘是否正在使用
###################################################
function disk_is_used()
{
    #mount 中不能包含 该设备
    mount | grep "^/dev/" | awk '{print $1}' | grep "${DISK_NAME}" > /dev/null
    if [ $? -eq 0 ]; then
        echo "Error: ${DISK_NAME} is used by mount."
        return 1
    fi

    #df 中不能包含 该设备
    df -P -h | grep "^/dev" | awk '{print $1}' | grep "${DISK_NAME}" > /dev/null
    if [ $? -eq 0 ]; then
        echo "Error: ${DISK_NAME} is used by df."
        return 1
    fi

    #swap 中不能包含 该设备
    swapon -s | grep "^/dev" | awk '{print $1}' | grep "${DISK_NAME}" > /dev/null
    if [ $? -eq 0 ]; then
        echo "Error: ${DISK_NAME} is used by swap."
        return 1
    fi

    #正在格式化的硬盘
    ps -wwef | grep -E "\ mkfs\.|\/mkfs\." | grep "${DISK_NAME}" > /dev/null
    if [ $? -eq 0 ]; then
        echo "Error: ${DISK_NAME} is used by mkfs."
        return 1
    fi

    #lvm 中不能包含 该设备
    which pvs > /dev/null 2>&1
    if [ $? -eq 0 ]; then
        pvs | grep "${DISK_NAME}" > /dev/null
        if [ $? -eq 0 ]; then
            echo "Error: ${DISK_NAME} is used by lvm."
            return 1
        fi
    fi

    return 0
}

###################################################
# 把硬盘置为 空盘
###################################################
function clear_disk()
{
    #删除硬盘分区
    parted -s ${DISK_NAME} mklabel gpt
    if [ $? -ne 0 ]; then
        echo "parted -s ${DISK_NAME} mklabel gpt failed."
        return 1
    fi

    sleep 1

    #刷新系统信息
    partprobe ${DISK_NAME}
    if [ $? -ne 0 ]; then
        echo "partprobe ${DISK_NAME} failed."
        return 1
    fi

    sleep 1

    #删除硬盘分区表
    dd if=/dev/zero of=${DISK_NAME} count=10 > /dev/null
    if [ $? -ne 0 ]; then
        echo "dd if=/dev/zero of=${DISK_NAME} count=10 failed."
        return 1
    fi

    return 0
}

###################################################
# 主函数
###################################################
function main()
{
    #参数检查
    chk_params
    if [ $? -ne 0 ]; then
        return 1
    fi

    #判断硬盘是否正在使用
    disk_is_used
    if [ $? -ne 0 ]; then
        return 1
    fi

    #把硬盘置为 空盘
    clear_disk
    if [ $? -ne 0 ]; then
        return 1
    fi

    echo "Set ${DISK_NAME} to blank successfully."

    return 0
}

main "$@"
exit $?
03-24 17:54