自動檢測硬盤,格式化及掛載

雲主機常常開機器,格式化,掛硬盤。之前是手動,一條一條命令敲的,如今參考批量生成腳本。shell

shell#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin
export PATH

if [ ! -f "/etc/fstab.bak" ]; then
        # 備份分區表
        echo "cp /etc/fstab /etc/fstab.bak"
fi

echo "echo Start finding disk drives ...... !"

# TODO: 參數列表化,分區數及掛盤名字
i=1
# 大於50G的磁盤掛載到/home/disk{1,2,3,...}
for disk in `fdisk -l |grep Disk |grep dev |perl -e '{while(<>){ my @a=split(/[\s+\:\,]/,$_); print $a[1]."\n" if($a[4] eq "GB" && $a[3] >= 50);}}'`; do
    # fdisk 分盤
    echo "fdisk $disk <<End
n
p
1


w
End"
    # 格式化
    #echo "mkfs.ext4 -T largefile ${disk}1"
    echo "mkfs.ext4 ${disk}1"
    # 建立掛載目錄
    if [  ! -d  "/home/data${i}" ]; then
        echo "mkdir /home/data${i}"
    fi
    # 寫入盤符
    echo "e2label ${disk}1 /home/data${i}1"
    mount="\"LABEL=/home/data${i}1       /home/data${i}  ext4    defaults        0       0\""
    echo "echo $mount >>/etc/fstab"
    #echo "mount ${disk}1 /home/data${i}" # 若是不重啓的話
    echo ""
    i=$(($i+1))
done
相關文章
相關標籤/搜索