#!/bin/bash

mount_disk_to_var(){

    echo ""
    echo ""
    echo ""
    echo "-----------------------------------------------------------------------"

    local VG_NAME=datavg
    local disk_name=/dev/vdb
    local mount_dir=/var/lib/docker

    echo "n
p



t

8e
w
" | fdisk ${disk_name}
    partprobe

    # 如果已经存在卷组，直接进行添加
    # vgextend /dev/mapper/centos /dev/vda3
    vgcreate ${VG_NAME} ${disk_name}1
    export selfpesize=$(vgdisplay ${VG_NAME} | grep 'Total PE' | awk '{print $3}')
    # 大小根据实际情况调整
    lvcreate -l ${selfpesize} -n lvdata ${VG_NAME}
#     resize2fs /dev/mapper/${VG_NAME}-lvdata
    mkfs.xfs /dev/mapper/${VG_NAME}-lvdata
    mkdir -p /data
    mkdir -p /var/lib/docker
    local selffstab="/dev/mapper/${VG_NAME}-lvdata      ${mount_dir}         xfs    defaults        0 0"
    echo "${selffstab}" >> /etc/fstab
    mount -a
    xfs_growfs /dev/mapper/${VG_NAME}-lvdata
    echo ""
    echo ""
    echo ""
    df -TH
    echo "-----------------------------------------------------------------------"
}


mount_disk_to_var