shell脚本自动分区、创建逻辑卷

2020/10/20 12:12
阅读数 228
#!/bin/bash
# vim:et:ts=4:sw=4:
<<INFO
SCRIPYT:autodiskmount.sh
AUTHOR:anqixiang
DATE:2020-10-18
DESCRIBE:把磁盘分为一个区,并做逻辑卷
SYSTEM:CentOS 7、RedHat 7、Ubuntu16/18
WARNING:LV_NAME、DISK_CAP、MOUNT_DIR三个数组的元素个数需一致
VERSION:1.1.0
MODIFY:
INFO

set -e

cecho(){
echo -e "\033[$1m$2\033[0m"
}

#定义逻辑卷的相关参数
VG_NAME="data"          #卷组名
LV_NAME=(${VG_NAME}_app ${VG_NAME}_mysql)   #以数组的形式存储逻辑卷名字
DISK_CAP=(30% 70%)                          #代表第一个逻辑卷占30%磁盘空间,第二个占70%
MOUNT_DIR=(/data/app /data/mysql)           #逻辑卷的挂载路径

######################功能函数######################
#检测磁盘
Check_Disk()
{
    local disk_num=$(fdisk -l |grep "${DISK_NMAE}" |wc -l)
    if [ ${disk_num} -eq 0 ];then
        cecho 31 "${DISK_NMAE}不存在" && exit 1
    elif [ ${disk_num} -gt 1 ];then
        cecho 31 "${DISK_NMAE}已分区,请先格式化" && exit 1
    fi
    cecho 32 "#分区磁盘为:${DISK_NMAE}"
}

#磁盘分区
Divi_Disk()
{
    fdisk ${DISK_NMAE} << EOF
n
p
1


wq
EOF
    fdisk -l |grep "${DISK_NMAE}"
}

#创建逻辑卷
Create_Lvm()
{
    #创建卷组
    if ! vgs | grep -w "${VG_NAME}" >/dev/null;then  
        vgcreate "${VG_NAME}" "${DISK_NMAE}1"
    fi
    #创建逻辑卷
    for((i=0;i<${#LV_NAME[@]};i++))
    do
        if ! lvs | grep -w "${LV_NAME[i]}" >/dev/null;then
            lvcreate -n ${LV_NAME[i]} -l ${DISK_CAP[i]}VG ${VG_NAME} -y
            mkfs.ext4 /dev/${VG_NAME}/${LV_NAME[i]} >/dev/null           
        fi
    done
    lvs
}

#挂载逻辑卷
Mount_Lvm()
{
    for((i=0;i<${#MOUNT_DIR[@]};i++))
    do
        [ ! -d ${MOUNT_DIR[i]} ] && mkdir -pv ${MOUNT_DIR[i]}
        local uuid=$(blkid | grep "${LV_NAME[i]}" |awk '{print $2}')
        [ ! -f /etc/fstab.bak ] && cp /etc/fstab{,.bak}
        sed -ri "s#(.*)${MOUNT_DIR[i]}(.*)##" /etc/fstab        #防止多次写入相同的挂载内容
        echo "${uuid} ${MOUNT_DIR[i]} ext4 defaults 0 0" >> /etc/fstab
    done
    sed -i '/^$/d' /etc/fstab
    mount -a
    cecho 96 "逻辑卷创建成功"
}

#卸载逻辑卷
Umount_Lvm()
{
    for((i=0;i<${#MOUNT_DIR[@]};i++))
    do
        df -h |grep -w "${MOUNT_DIR[i]}" && umount ${MOUNT_DIR[i]}
        [ -d ${MOUNT_DIR[i]} ] && rm -rf ${MOUNT_DIR[i]}
        if lvs | grep -w "${LV_NAME[i]}" >/dev/null;then
            lvremove -f /dev/${VG_NAME}/${LV_NAME[i]}
        fi
        sed -ri "s#(.*)${MOUNT_DIR[i]}(.*)##" /etc/fstab
    done
    if vgs | grep -w "${VG_NAME}" >/dev/null;then
        vgremove ${VG_NAME}
    fi
    sed -i '/^$/d' /etc/fstab
    mount -a
    #删除磁盘分区
    fdisk ${DISK_NMAE} << EOF
d
w
EOF
    lsblk
    cecho 96 "逻辑卷卸载成功"
}

#帮助信息
Help(){
	cat << EOF
Usage: 
=======================================================================
EXAMPLE:
    bash autodiskmount.sh mount     #创建逻辑卷
    bash autodiskmount.sh umount    #卸载逻辑卷
EOF
}

######################主函数######################
[ "$#" -ne 2 ] && Help && exit 1
DISK_NMAE=$1
if [ "x$2" == "xmount" ];then
    Check_Disk
    Divi_Disk
    Create_Lvm
    Mount_Lvm
elif [ "x$2" == "xumount" ];then
    Umount_Lvm
else
    Help && exit 1
fi
set +e
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部