首页 > 代码库 > 磁盘自动分区
磁盘自动分区
#!/bin/bash
#########################################
#Function: auto fdisk
#Usage: bash auto_fdisk.sh
#Author: Customer service department
#Company: Alibaba Cloud Computing
#Version: 2.0
#########################################
count=0
tmp1=/tmp/.tmp1
tmp2=/tmp/.tmp2
>$tmp1
如果没有/tmp/.tmp1 这个文件,则新建
如果有,并且大小不为0,则清空文件
[root@lvmraid lianxi]# aa=meng
[root@lvmraid lianxi]# >$aa >后面跟一个变量
[root@lvmraid lianxi]# ls
meng
>$tmp2
fstab_file=/etc/fstab 将文件的绝对路径复制给fstab_file
#check lock file ,one time only let the script run one time
LOCKfile=/tmp/.$(basename $0) $0是执行该脚本时的第一个参数./auto_fdisk02.sh
basename ./auto_fdisk02.sh
auto_fdisk02.sh
if [ -f "$LOCKfile" ]
then
echo -e "\033[1;40;31mThe script is runing,please next time to run this script.\033[0m"
exit
else
echo -e "\033[40;32mStep 1.No lock file,begin to create lock file and continue.\033[40;37m"
touch $LOCKfile
fi
#check user
if [ $(id -u) != "0" ] 查看当前用户的id
then
echo -e "\033[1;40;31mError: You must be root to run this script, please use root to install this script.\033[0m"
rm -rf $LOCKfile
exit 1 0表示成功(Zero - Success)
非0表示失败(Non-Zero - Failure)
2表示用法不当(Incorrect Usage)
127表示命令没有找到(Command Not Found)
126表示不是可执行的(Not an executable)
>=128 信号产生
fi
#check disk partition
check_disk()
{
>$LOCKfile 新建LOCKfile 文件
device_list=$(fdisk -l|grep "Disk"|grep "/dev"|awk ‘{print $2}‘|awk -F: ‘{print $1}‘|grep "xv") xxx=$(xx) 将xx赋值给xxx
/dev/sda
/dev/sdb
/dev/mapper/vg_lvmraid-lv_root
/dev/mapper/vg_lvmraid-lv_swap
for i in `echo $device_list`
do
device_count=$(fdisk -l $i|grep "$i"|awk ‘{print $2}‘|awk -F: ‘{print $1}‘|wc -l)
/dev/sda
*
64
echo
if [ $device_count -lt 2 ] 小于,此条件的含义是该磁盘没有分区
then
now_mount=$(df -h)
if echo $now_mount|grep -w "$i" >/dev/null 2>&1
then
echo -e "\033[40;32mThe $i disk is mounted.\033[40;37m"
else
echo $i >>$LOCKfile
echo "You have a free disk,Now will fdisk it and mount it."
fi
fi
done
disk_list=$(cat $LOCKfile)
if [ "X$disk_list" == "X" ] 此变量为空字符串
then
echo -e "\033[1;40;31mNo free disk need to be fdisk.Exit script.\033[0m"
rm -rf $LOCKfile
exit 0
else
echo -e "\033[40;32mThis system have free disk :\033[40;37m"
for i in `echo $disk_list`
do
echo "$i"
count=$((count+1)) [root@lvmraid ~]# count=$(count+1)
-bash: count+1: command not found
[root@lvmraid ~]# count=$((count+1))
done
fi
}
#fdisk ,formating and create the file system
fdisk_fun()
{ $1 位置参数 指磁盘
fdisk -S 56 $1 << EOF -S 指定每磁道的扇区数
n
p
1
wq
EOF
sleep 5
mkfs.ext4 ${1}1 第一个“1”是变量,第二个“1”作为字符串输出
}
#make directory
make_dir()
{
echo -e "\033[40;32mStep 4.Begin to make directory\033[40;37m"
for j in `seq $count`
do
if [ -d "/alidata$j" ]
then
echo -e "\033[1;40;31m/alidata$j is exists.This script will exit,you must to choose a directory for mount.\033[0m"
rm -rf $LOCKfile $tmp2
exit
else
echo "/alidata$j" >>$tmp1
mkdir /alidata$j
fi
done
}
#config /etc/fstab and mount device
main()
{
for i in `echo $disk_list` 未分区磁盘
do
echo -e "\033[40;32mStep 3.Begin to fdisk free disk.\033[40;37m"
fdisk_fun $i
echo "${i}1" >>$tmp2
done
make_dir
>$LOCKfile
paste $tmp2 $tmp1 >$LOCKfile
echo -e "\033[40;32mStep 5.Begin to write configuration to /etc/fstab and mount device.\033[40;37m"
while read a b
do
if grep -v ^# $fstab_file|grep ${a} >/dev/null
then
sed -i "s=${a}*=#&=" $fstab_file
fi
echo "${a} $b ext3 defaults 0 0" >>$fstab_file
done <$LOCKfile
mount -a
}
#=========start script===========
echo -e "\033[40;32mStep 2.Begin to check free disk.\033[40;37m"
check_disk
main
df -h
rm -rf $LOCKfile $tmp1 $tmp2
====
disk_list 尚没有分区的磁盘
LOCKfile 存放尚未分区且没有被挂载的磁盘
count 指尚未分区的磁盘的数量
/tmp2 存放新建的分区名字
/tmp1 存放新建目录路径
device_list 磁盘设备赋值给它
make_dir 创建挂载目录
本文出自 “爱武装一辈子” 博客,请务必保留此出处http://menglinux.blog.51cto.com/8733806/1437320