#!/bin/bash
which ethtool &> /dev/null || yum install ethtool -y 2>&1
which bc &> /dev/null || yum install bc -y 2>&1
#2.
#if lsblk -l | egrep "/$|boot|swap" | grep luks ;then                                                                                                                       
#  sys_dev=$(lsblk -l| egrep -B 1 "/$|boot|swap"|grep -v luks|awk '{print $1}'|sed -e '/sd/s/[0-9]//' -e '/nvme/s/p[0-9]//' -e '/vd/s/[0-9]//'|sort|uniq)                   
#elif  lsblk -l|egrep "/$|boot|swap"|grep lvm ;then                                                                                                                         
#  sys_dev=$(lsblk -l|egrep -B 1 "/$|boot|swap"|grep -v lvm|awk '{print $1}'|sed -e '/sd/s/[0-9]//' -e '/nvme/s/p[0-9]//' -e '/vd/s/[0-9]//'|sort|uniq)                     
#else                                                                                                                                                                       
#  sys_dev=$(lsblk -l|egrep "/$|boot|swap"|awk '{print $1}'|sed -e '/sd/s/[0-9]//' -e '/nvme/s/p[0-9]//' -e '/vd/s/[0-9]//'|sort|uniq)                                      
#fi                                                                                                                                                                         
sys_dev=$(lsblk -l| egrep -B 1 "/$|boot|swap"|egrep -v "lvm|luks|--"|awk '{print $1}'|sed -e '/sd/s/[0-9]//' -e '/nvme/s/p[0-9]//' -e '/vd/s/[0-9]//'|sort|uniq)
echo "system disk: ${sys_dev}"                                                                                                                                             
#获取所有硬盘                                                                                                                                                                          
lsblk |grep "disk" |egrep -v "sr|fd|$sys_dev" |awk '{print $1}' |sort |uniq > /tmp/fdisk_txt                                                                               
#去掉系统盘
for m in ${sys_dev};do                                                                                                                                                     
        sed -i "/$m/d" /tmp/fdisk_txt                                                                                                                                      
done 
#
                                                                                                                                                                 
###/tmp/fdisk_txt 文件为空则退出执行                                                                                                                             
[ `cat /tmp/fdisk_txt | wc -l` -eq 0 ]  && echo -e  "\n####\n\n/tmp/fdisk_txt is null , exit.....\n\n####\n" && echo "OK" && exit 999                            
                                                                                                                                                                 
#清除磁盘信息                                                                                                                                                    
if lsblk -l|egrep -v "/$|boot|swap"|grep lvm ;then                                                                                                               
        lvs |grep -v LV|awk '{print "lvremove -f /dev/"$2"/"$1}'|sh                                                                                              
        vgs |grep -v VG |awk '{print "vgremove -f "$1}'|sh                                                                                                       
        pvs |grep -v PV|awk '{print "pvremove -f "$1}'| sh                                                                                                       
elif lsblk -l|egrep -v "/$|boot|swap"|grep raid;then                                                                                                             
        dmsetup remove_all                                                                                                                                       
fi 
#
ssds=()
hdds=()
n=0
m=0
[ -f /tmp/fio_disktype ] && rm -f /tmp/fio_disktype
for dev in $(cat /tmp/fdisk_txt);do
    Dev_disk=/dev/$dev
    df -hP | grep "^/dev" | grep -E "$dev" |awk '{print "umount " $1}'|sh
    Iodepth=32
    Bs=4K
    fio -filename=${Dev_disk} -direct=1 -ioengine=libaio -runtime=5 -iodepth=${Iodepth} -rw=randrw -rwmixread=80 -bs=${Bs} -name=${dev}_libaio_randrw_${Bs} -output=/tmp/fio.log
    sleep 1
    iops_randrw_read=$(grep read /tmp/fio.log |egrep -io "iops=[0-9.kK]+" |cut -d"=" -f2)
    iops_randrw_write=$(grep write /tmp/fio.log |egrep -io "iops=[0-9.kK]+" |cut -d"=" -f2)
    if [[ $iops_randrw_read =~ k || $iops_randrw_read =~ K ]]; then                                                                                                        
        iops_randrw_read=$(echo $iops_randrw_read|awk -F "k|K" '{print $1*1000}')                                                                                          
    fi                                                                                                                                                                     
    if [[ $iops_randrw_write =~ k || $iops_randrw_write =~ K ]]; then                                                                                                      
        iops_randrw_write=$(echo $iops_randrw_write|awk -F "k|K" '{print $1*1000}')                                                                                        
    fi                                                                                                                                                                     
    #  
    disk_size=$(lsblk -b |grep -w $dev |awk '{print int($4/1000/1000/1000)}')
    iops_randrw_read=${iops_randrw_read:=0}                                                                                                                                
    if [ ${iops_randrw_read} -lt 5000 ];then                                                                                                                              
        disk_type=HDD                                                                                                                                         
	hdds[m]=$dev
	let m=m+1
    else
        disk_type=SSD
	ssds[n]=$dev
	let n=n+1                                                                                                                                             
    fi                                                                                                                                                                     
    printf "%-5s %-15s %-10s %-25s %-15s\n" ${dev} "${disk_size}G" "${disk_type}" ${iops_randrw_read} ${iops_randrw_write} >> /tmp/fio_disktype          
    wait 
done
#
format_ssd(){
    n=1
    for d in ${ssds[*]};do
        dd if=/dev/zero of=/dev/$d bs=4K count=64
        mkfs.xfs -f /dev/$d -L /ssd$n
        [ -d "/ssd$n" ] || mkdir -p /ssd$n
        mount -o noatime,discard LABEL=/ssd$n /ssd$n
        sed -i "/LABEL=\/ssd${n}[ ]\{1,\}\/ssd${n}/d" /etc/rc.d/rc.local
        echo "mount -o noatime,discard LABEL=/ssd$n /ssd$n" >> /etc/rc.d/rc.local
        let n=n+1
    done
}
format_hdd(){
    n=1
    for d in ${hdds[*]};do
        dd if=/dev/zero of=/dev/$d bs=4K count=64
        mkfs.xfs -f /dev/$d -L /hdd$n
        mkdir -p /mgtv/disks/hdd$n
        mount -o noatime,discard LABEL=/hdd$n /mgtv/disks/hdd$n
        sed -i "/LABEL=\/hdd${n}[ ]\{1,\}\/mgtv\/disks\/ssd${n}/d" /etc/rc.d/rc.local
        echo "mount -o noatime,discard LABEL=/hdd$n /mgtv/disks/hdd$n" >> /etc/rc.d/rc.local
        let n=n+1
    done
}

main(){
    format_ssd
}
main
