CentOS新增加磁盘自动创建LVM卷组


exit_script(){
    exit 1
}
input_error_exit(){
    if [ -z  $1 ];then 
        echo -e "\033[31m "Incorrect input!!!" \033[0m"
        exit_script
    fi
}

#从现有物理卷扩展逻辑卷
extend_lv_from_pv(){
    PVS_NAME=`pvs | grep -v "PFree" | awk '{if($6>0){print $1;}}'`
    VG_NAME=`vgdisplay | awk '/VG Name/ {print $3}'`
    LVS_NAME=`lvdisplay | awk '/LV Name/ {print $3}'`
    if [ ${#PVS_NAME} == 0 ];then
        echo -e "\033[31m "Don\'t available physics volume!" \033[0m"
        exit_script
    fi
    echo -e "\033[34m Please select physics volume to operate: \033[0m"
    select OPERATE_DISK in $PVS_NAME
    do
    break
    done
    input_error_exit $OPERATE_DISK    

    echo -e "\033[34m Please select logical volume to extend: \033[0m"
    echo -e "\n"
    select LV_NAME in $LVS_NAME
    do
        break
    done
    input_error_exit $LV_NAME

    echo -e "\n"
    #判断逻辑卷是根分区还是swap分区
    unset TEMP
    TEMP=`cat /etc/fstab  | grep $LV_NAME | awk '{print $2}'`
    if [ $TEMP = "/" ];then
        lvextend /dev/$VG_NAME/$LV_NAME $OPERATE_DISK
    unset TEST
        TEST=`cat /etc/fstab  | grep $LV_NAME | awk '{print $3}'`
        if [ "$TEST" = "ext4" ] || [ "$TEST" = "ext3" ];then
            resize2fs /dev/$VG_NAME/$LV_NAME
        elif [ "$TEST" = "xfs" ];then
            xfs_growfs /dev/$VG_NAME/$LV_NAME
        fi
    elif [ $TEMP = "swap" ] || [ $TEMP = "none" ];then
    view_vg_size
    read -p "Please input size to extend(G,g,M,m):" SIZE
        swapoff /dev/$VG_NAME/$LV_NAME
        lvextend -L +${SIZE} /dev/$VG_NAME/$LV_NAME
        mkswap /dev/$VG_NAME/$LV_NAME
        swapon /dev/$VG_NAME/$LV_NAME
    fi
}

#从未分区的硬盘扩展逻辑卷
extend_lv_from_disk(){
    #搜索所有/dev/sd开头的硬盘
    DISKS=`fdisk -l | awk '/Disk/ {print $2}' | cut -d: -f1 | grep '\/dev\/sd*'`
    I=0
    #筛选未分区的硬盘,并将未分区的硬盘存入数组中
    for J in $DISKS;do
        TEST1=`fdisk -l $J | grep ^$J.`
        FLAG1=$?
        if [ $FLAG1 != 0 ];then
        DISK[$I]=$J
        I=`expr $I + 1` 
        fi
    done
    #如果没有可用硬盘,则退出脚本
    if [ ${#DISK[*]}  == 0 ];then
        echo -e "\033[31m "Don\'t available disk!" \033[0m"
        exit_script
    fi
    echo -e "\n"
    echo -e "\033[34m "The following are unused hard drives." \033[0m"
    #从未分区数组中选择要操作的硬盘
    select OPERATION_DISK in ${DISK[*]} 
    do
        break
    done
    input_error_exit $OPERATION_DISK

    #创建分区
    fdisk $OPERATION_DISK<

你可能感兴趣的:(CentOS新增加磁盘自动创建LVM卷组)