根据工作需要,需要一个能自动分区的脚本,折腾了半天终于弄好。现在进行总结一下:
#/bin/bash
#Step 1. 判断系统有2块以上磁盘同则开始循环
DISK_COUNTS=`fdisk -l|grep ^Disk|wc -l`
while [ $DISK_COUNTS -gt 1 ];do
DISK_LISTS=`fdisk -l|grep ^Disk|cut -c6-13`
echo -ne "\033[30;32m \n\nThese disks are available for use:\n$DISK_LISTS\n\n \033[0m"
#Step 2.交互式确认是否要对第二块磁盘处理,输入不合法则自动要求重新输入
read -p " Do you want to partion /dev/sdb and mount it ? [Y/y/N/n] " ANSWER
case $ANSWER in
[Yy])
#确认sdb未被分区
if [ `fdisk -l /dev/sdb|grep /dev/sdb1|wc -l` -eq 0 ] && [ `fdisk -l /dev/sdb|wc -l` -gt 0 ];then
mkdir -p /directory/
#因为测试了很久,将fdisk集成在一个shell文件会提示错误," invalid end of file.",所以生成一个临时文件/tmp/mount2disk_arg.sh,这个小脚本用于将/dev/sdbd磁盘划分成一个分区
SIZE_CYLINDERS=`fdisk -l /dev/sdb |grep 'cylinders$'|awk '{print $5}'`
echo -ne '#!/bin/bash\nfdisk /dev/sdb << EOF > /dev/null\n'> /tmp/mount2disk_arg.sh
echo -ne "n\np\n1\n1\n$SIZE_CYLINDERS\nw\nq\nEOF\n" >> /tmp/mount2disk_arg.sh
chmod u+x /tmp/mount2disk_arg.sh && /tmp/mount2disk_arg.sh
sleep 1
mkfs.ext3 /dev/sdb1 > /dev/null
mount /dev/sdb1 /directory && echo -ne "/dev/sdb1\t\t/data\t\t\text3\tdefaults,noatime\t0 0\n" >> /etc/fstab
echo -ne "\033[30;32m \nIt's done,See The output for command 'mount':\n\n \033[0m" && mount |grep /data
else
echo -ne "\033[30;31m do not have second disk or already had a partion.quit without proceed.\n \033[0m"
fi
break
;;
[Nn])
echo -ne "\033[30;32m As you wish,did not proceed /dev/sdb.\n \033[0m"
break
;;
*)
echo -ne "\033[30;31m Invalied input. only accept [Y/y/N/n].\n \033[0m"
;;
esac
done
rm -f /tmp/mount2disk_arg.sh
-----------------------------------------------------------------------------------------------------
1.如果改良一下,可以做为自动分区多个盘方便做raid
2.为了方便,设置了输出的颜色
阅读(3496) | 评论(0) | 转发(0) |