#/bin/bash#```ops#title:LINUX自动分区处理单块磁盘有未分区空间#descr:#```count=0tmp1=/tmp/tmp1.log
tmp2=/tmp/tmp2.log
>$tmp1>$tmp2fstab_file=/etc/fstab
#check lock file ,one time only let the script run one time LOCKfile=/tmp/.$(basename $0)if[-f"$LOCKfile"]thenecho-e"\033[1;40;31mThe script is already exist,please next time to run this script.\033[0m"exitelseecho-e"\033[40;32mStep 1.No lock file,begin to create lock file and continue.\033[40;37m"touch$LOCKfilefi#check userif[$(id-u)!="0"]thenecho-e"\033[1;40;31mError: You must be root to run this script, please use root to install this script.\033[0m"rm-rf$LOCKfileexit1fi#check disk partitioncheck_disk(){>$LOCKfiledevice_list=$(fdisk -l|grep"dev"|grep"sd"|awk-F[:]'{print $1}'|awk'{print $2}'|awk -F: '{print $1}'|head-n1)fdisk -l|grep"sda"|grep"dev"|grep Linux |grep"sd"|awk'{print $1}'>$tmp1}#check oscheck_os(){os_release=$(grep"CentOS" /etc/issue 2>/dev/null)os_release_2=$(grep"CentOS" /etc/redhat-release 2>/dev/null)if["$os_release"]&&["$os_release_2"]thenos_release=CentOS
modify_env
fi}#install ext4modify_env(){#yum install e4fsprogs parted -y#modprobe ext4echo ext4
}#fdisk ,formating and create the file systemfdisk_fun(){fdisk-S56$1<<EOF
n
e
n
p
wq
EOFsleep5#mkfs.ext4 ${1}1}#config /etc/fstab and mount devicemain(){
fdisk_fun $device_listfdisk -l|grep"sda"|grep"dev"|grep Linux |grep"sd"|awk'{print $1}'>$tmp2#partprobeforiin`grep-F-v-f $tmp1 $tmp2 |sort|uniq`do
partx -a$device_list
partx -a$i$device_listmkfs-t ext4 $iif[!-d /data ];thenmkdir-p /data
mount$i /data
echo"$i /data ext4 defaults 1 2">>$fstab_fileelseif[!-d /data1 ];thenmkdir-p /data1
mount$i /data1
echo"$i /data1 ext4 defaults 1 2">>$fstab_filefifidone}#=========start script===========echo-e"\033[40;32mStep 2.Begin to check free disk.\033[40;37m"
check_os
check_disk
main
df-h