自动初始化磁盘

#!/bin/bash

cloud_disk_list=$(lsblk -r --output NAME,MOUNTPOINT | awk -F \/ '/vd/ { dsk=substr($1,1,3);dsks[dsk]+=1 } END { for ( i in dsks ) { if (dsks[i]==1) print i } }' | head -1)
local_disk_list=$(lsblk -r --output NAME,MOUNTPOINT | awk -F \/ '/sd/ { dsk=substr($1,1,3);dsks[dsk]+=1 } END { for ( i in dsks ) { if (dsks[i]==1) print i } }' | head -1)

function init_disk() {
disk_name=$1

fdisk ${disk_name} <<EOF
n
p
1


w
q
EOF

mkfs.ext4 ${disk_name}1

mkdir -p ${mount_point}
mount ${disk_name}1 ${mount_point}
echo "${disk_name}1 ${mount_point} ext4 defaults 0 0" >> /etc/fstab
}

if [ -n "${cloud_disk_list}" ];then
    disk_tag="/dev/"${cloud_disk_list}
    mount_point="/data"
    check_result=$(df -h | grep ${disk_tag}1)

    if [ ${#check_result} -gt 0 ];then
        echo ${disk_tag}'磁盘已初始化'
    else
        echo ${disk_tag}'未初始化,可以处理'
        init_disk ${disk_tag}
    fi
fi

if [ -n "${local_disk_list}" ];then
    disk_tag="/dev/"${local_disk_list}
    mount_point="/data"
    check_result=$(df -h | grep ${disk_tag}1)

    if [ ${#check_result} -gt 0 ];then
        echo ${disk_tag}'磁盘已初始化'
    else
        echo ${disk_tag}'未初始化,可以处理'
        init_disk ${disk_tag}
    fi
fi

 

该脚本可以自动检测未初始化磁盘,并进行初始化,最后进行挂载。

 

posted @ 2022-12-13 16:33  道霖  阅读(37)  评论(0编辑  收藏  举报