本脚本功能,初始化新添加的硬盘,分区格式化成我们机器需要的状态。
包括单个硬盘普通格式,和多磁盘阵列方式。并添加卷标(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