自己写的第一个shell脚本 HD_init.sh

本脚本功能,初始化新添加的硬盘,分区格式化成我们机器需要的状态。

包括单个硬盘普通格式,和多磁盘阵列方式。并添加卷标(label)“/data”。

#!/bin/sh
targetdisk1=
targetdisk2=
dopartition=
isnewdisk=
newdisks=
disknum=
disks=`fdisk -l 2>/dev/null |grep Disk |cut -d : -f1 |cut -d ' ' -f2 2>/dev/null |grep /`

for disk in $disks ;do

        if [ "$disk" = "/dev/md0" ]; then
                 echo mdadm raid exist!So exit.
                 exit
        fi

done

for disk in $disks ; do
        label=`e2label ${disk}1 2>/dev/null`
        if [ "$label" = "/root" ]; then
                continue
        fi
        if [ "$label" = "/data" ]; then
               continue
        else
                echo "new disk $disk found!"
                targetdisk1=$disk
                isnewdisk="yes"
                disknum=`expr $disknum + 1`
                newdisks="$newdisks $disk"
        fi

done

if [ "$isnewdisk" = "yes" ]; then
        if [ "$disknum" -eq "1"  ]; then
                dd if=/dev/zero of=$targetdisk1 bs=512 count=1 >/dev/null 2>&1
                echo -e "m/nn/np/n1/n/n/nt/n83/na/n1/nwq/n" |fdisk $targetdisk1 >/dev/null 2>&1
                mkfs.ext3 ${targetdisk1}1 >/dev/null 2>&1
                e2label ${targetdisk1}1 /data >/dev/null 2>&1
                echo ${targetdisk1}1
        elif [ "$disknum" -gt "1" ]; then
                echo "more than 1 new disk found,so use raid mode."
                echo "formating new disks..."
                for disk2 in $newdisks ; do
                        targetdisk2=$disk2
                        echo -e "m/nn/np/n1/n/n/nt/nfd/nwq/n" |fdisk $targetdisk2  >/dev/null 2>&1
                done
                echo "doing mdadm..."

                mdadm --create /dev/md0 --level=1 --raid-devices=$disknum -c128 $newdisks

                mkfs.ext3 /dev/md0
                e2label /dev/md0 /data >/dev/null 2>&1
                mkdir /mnt/test
                mount /dev/md0 /mnt/test

        fi
        echo "list of new disks: $newdisks"
        echo "$disknum new disk(s) has been initialized!!!"
else
        echo "no new disk(s) found!"
fi

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值