1

云主机经常开机器,格式化,挂硬盘。以前是手动,一条一条命令敲的,现在参考批量生成脚本。

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

Buttonwood
17 声望1 粉丝

Float like a butterfly! Stand like a buttonwood!