#!/bin/bash
#Author:Terrence.Zhou@ucloud.cn
#Date:9/3/2019
#Update Samung PM883/Intel S4500/Micron 5100/5200 SATA SSD firmware automatically
#Modified in 9/12/2019
#Add Samsung PM863a/Intel S4510 SSD firmware update
#Modified in 4/27/2020
#update Samsumg PM863a SSD FW GXT5404Q_NF
#Modified in 6/9/2020
#Add lsi_hba_card ssd update function
#Add lsi_raid_ssd_erase and pmc_raid_ssd_erase function
#Modified in 6/18/2020
#Delete VD in data disk before ssd erase action
#Move pmc_raid_erase function
#Modified in 7/27/2020
#re-format ssd fw update method which only maitains ssd_upd_list
#Moddified in 9/8/2020
#Update ssd fw update and erase tool with higher hdparm instead of strorcli and arcconf
#Modified in 13/10/2020
#Update hdparm v9.58 in yum that can support CentOS6.x and CentOS7.x update and erase funticon both
#Modified in 13/11/2020
#Update ssd firmware using acconf tool and abort os ssd erase of PM card

CPWD=`cd $(dirname $0);pwd`
cd $CPWD
rm -rf *_map
lsi_megaraid_ssd_upd(){
for i in `/opt/MegaRAID/storcli/storcli64 show all |grep -A 10 "Ctl Model" |grep -B 10 "Ctl=Controller Index" |sed -n '/^-/,/-$/p' |grep -v "^-" |awk '{print $1}' |sed '/^\s*$/d'`
do
	/opt/MegaRAID/storcli/storcli64 /c$i/eall/sall show |grep "SSD" |awk '{print $1,$(NF-2)}' > ${i}_lsi_ssd_map
	for j in `seq 1 $(cat ${i}_lsi_ssd_map |wc -l)`
	do
	Model=$(sed -n "$j"p ${i}_lsi_ssd_map |awk '{print $2}')
	EID=$(sed -n "$j"p ${i}_lsi_ssd_map |awk '{print $1}' |awk -F":" '{print $1}')
 	SID=$(sed -n "$j"p ${i}_lsi_ssd_map |awk '{print $1}' |awk -F":" '{print $2}')
	FW_Ver=$(/opt/MegaRAID/storcli/storcli64 /c$i/e${EID}/s${SID} show all  |grep "Firmware Revision" |awk '{print $NF}')
		for k in `seq 1 $(cat ssd_upd_list |wc -l)`
		do
		match_model=`sed -n "$k"p ssd_upd_list |awk '{print $1}'`
		match_fw_ver=`sed -n "$k"p ssd_upd_list |awk '{print $2}'`
		upd_fw=`sed -n "$k"p ssd_upd_list |awk '{print $3}'`
		if [[ "${Model}" =~ "${match_model}" ]] && [[ "${FW_Ver}" != "${match_fw_ver}" ]];then
			echo -e "\033[31m e${EID}:s${SID} start to upgrade ${Model} sata ssd firmware \033[0m"
			/opt/MegaRAID/storcli/storcli64 /c$i/e${EID}/s${SID} download src=${upd_fw}
		fi
		done
	done
done
}

dell_megaraid_ssd_upd(){
for i in `/opt/MegaRAID/perccli/perccli64 show all |grep -A 10 "Ctl Model" |grep -B 10 "Ctl=Controller Index" |sed -n '/^-/,/-$/p' |grep -v "^-" |awk '{print $1}' |sed '/^\s*$/d'`
do
        /opt/MegaRAID/perccli/perccli64 /c$i/eall/sall show |grep "SSD" |awk '{print $1,$(NF-2)}' > ${i}_dell_lsi_ssd_map
        for j in `seq 1 $(cat ${i}_dell_lsi_ssd_map |wc -l)`
        do
        Model=$(sed -n "$j"p ${i}_dell_lsi_ssd_map |awk '{print $2}')
        EID=$(sed -n "$j"p ${i}_dell_lsi_ssd_map |awk '{print $1}' |awk -F":" '{print $1}')
        SID=$(sed -n "$j"p ${i}_dell_lsi_ssd_map |awk '{print $1}' |awk -F":" '{print $2}')
        FW_Ver=$(/opt/MegaRAID/perccli/perccli64 /c$i/e${EID}/s${SID} show all  |grep "Firmware Revision" |awk '{print $NF}')
                for k in `seq 1 $(cat ssd_upd_list |wc -l)`
                do
                match_model=`sed -n "$k"p ssd_upd_list |awk '{print $1}'`
                match_fw_ver=`sed -n "$k"p ssd_upd_list |awk '{print $2}'`
                upd_fw=`sed -n "$k"p ssd_upd_list |awk '{print $3}'`
                if [[ "${Model}" =~ "${match_model}" ]] && [[ "${FW_Ver}" != "${match_fw_ver}" ]];then
                        echo -e "\033[31m e${EID}:s${SID} start to upgrade ${Model} sata ssd firmware \033[0m"
                        /opt/MegaRAID/perccli/perccli64 /c$i/e${EID}/s${SID} download src=${upd_fw}
                fi
                done
        done
done
}

lsi_mptsas_ssd_upd(){
which sg_write_buffer >/dev/null 2>&1
if [[ $? -ne 0 ]];then
	yum install sg3_utils -y
fi 
lsscsi -g |grep ATA > lsi_hba_ssd_map
for i in `awk '{print $NF}' lsi_hba_ssd_map`;do
	IS_SSD=`smartctl -i $i |grep "Rotation Rate" |awk -F":" '{print $NF}' |sed 's/^\s*//g'`
	Model=`smartctl -i $i |grep "Device Model" |awk -F":" '{print $NF}' |sed 's/\s*//g'`
	FW_Ver=`smartctl -i $i |grep "Firmware Version" |awk -F":" '{print $NF}' |sed 's/\s*//g'`
	if [[ "${IS_SSD}" =~ "Solid State Device" ]];then 
		for j in `seq 1 $(cat ssd_upd_list |wc -l)`
		do
		match_model=`sed -n "$j"p ssd_upd_list |awk '{print $1}'`
        	match_fw_ver=`sed -n "$j"p ssd_upd_list |awk '{print $2}'`
        	upd_fw=`sed -n "$j"p ssd_upd_list |awk '{print $3}'` 
		if [[ "${Model}" =~ "${match_model}" ]] && [[ "${FW_Ver}" != "${match_fw_ver}" ]];then
			echo -e "\033[31m $i ${Model} start to upgrade ${Model} sata ssd firmware \033[0m"
			hdparm --fwdownload ${upd_fw} --yes-i-know-what-i-am-doing --please-destroy-my-drive $i
			if [[ $? -ne 0 ]];then
				continue
			fi
		fi
		done
	fi
done
}

pmc_aacraid_ssd_upd(){
rm -rf *_pmc_vd_map pmc_aacraid_ssd_map_* *_os_disk_slot_map_*
for i in `arcconf list |grep "Controllers found" |awk '{print $NF}'`
do
	arcconf getconfig $i ld |grep "Logical Device number" |awk '{print $NF}' > ${i}_pmc_vd_map 
        for j in `grep -v "0" ${i}_pmc_vd_map`
        do
        arcconf delete $i logicaldrive $j noprompt >/dev/null 2>&1
        done
        controller_mode=`arcconf list $i |grep -Ew "Controller [0-9]*" |awk -F":" '{print $3}' |awk -F"," '{print $3}' |sed -e 's/(//' -e 's/)//' -e 's/\s*//g'`
        if [[ ${controller_mode} =~ "Hide RAW" ]];then
                arcconf setcontrollermode $i 0 >/dev/null 2>&1
                if [[ $? -ne 0 ]];then
                        echo "Please check the fucking PMC card and driver if any issue"
                        exit 1
                fi
        fi
done
sleep 5
#filter os ssd channel id and device id, then update firmware using arcconf tool
for controller_id in `arcconf list |grep "Controllers found" |awk '{print $NF}'`
do	
	HC=$((${controller_id}-1))
        arcconf list $i |grep -A 14 "Physical ID" |grep "Solid State Drive" > pmc_aacraid_ssd_map_${controller_id}
	for i in `seq 1 $(cat pmc_aacraid_all_map |wc -l)`
	do
        	Channel_ID=$(sed -n "$i"p pmc_aacraid_all_map_${controller_id} |awk '{print $1}' |awk -F"(" '{print $1}' |awk -F"," '{print $1}')
        	Device_ID=$(sed -n "$i"p pmc_aacraid_all_map_${controller_id} |awk '{print $1}' |awk -F"(" '{print $1}' |awk -F"," '{print $2}')
        	Disk_SN=`smartctl -i -d aacraid,$HC,${Channel_ID},${Device_ID} /dev/sda |awk '/Serial Number/{print $NF}' |sed 's/\s*//g'`
        	for os_sn in `arcconf getconfig 1 ld 0  |grep -E "Segment|Present" |awk '{print $NF}' |sed 's/\s*//g'`
        	do
                	if [[ ${Disk_SN} == ${os_sn} ]];then
                        	echo -e "${Channel_ID},${Device_ID}" >> os_disk_slot_map_${controller_id}
                	fi
        	done
	done
	cat os_disk_slot_map_${controller_id} |sort -u > real_os_disk_slot_map_${controller_id}
	for j in `seq 1 $(cat real_os_disk_slot_map_${controller_id})`
	do
		New_Channel_ID=$(sed -n "$j"p real_os_disk_slot_map_${controller_id} |awk -F"," '{print $1}')
		New_Device_ID=$(sed -n "$j"p real_os_disk_slot_map_${controller_id} |awk -F"," '{print $2')
		Model=$(smartctl -i -d aacraid,$HC,${New_Channel_ID},${New_Device_ID} /dev/sda |grep "Device Model" |awk '{print $NF}')
		FW_Ver=$(smartctl -i -d aacraid,$HC,${New_Channel_ID},${New_Device_ID} /dev/sda |grep "Firmware Version" |awk '{print $NF}')
		for k in `seq 1 $(cat ssd_upd_list |wc -l)`
                        do
                        match_model=`sed -n "$k"p ssd_upd_list |awk '{print $1}'`
                        match_fw_ver=`sed -n "$k"p ssd_upd_list |awk '{print $2}'`
                        upd_fw=`sed -n "$k"p ssd_upd_list |awk '{print $3}'`
			if [[ "${Model}" =~ "${match_model}" ]] && [[ "${FW_Ver}" != "${match_fw_ver}" ]];then
				echo -e "\033[31m ${New_Channel_ID},${New_Device_ID} start to upgrade ${Model} sata ssd firmware \033[0m"
				arcconf imageupdate ${controller_id} device ${New_Channel_ID} ${New_Device_ID} 32768 ${upd_fw} noprompt
			fi
		done
	done
done
#Update data ssd firmware using hdparm tool that can make them take effect immediately
lsscsi -g |grep ATA > pmc_data_disk_map
for k in `awk '{print $NF}' pmc_data_disk_map`
do
        IS_SSD=`smartctl -i $k |grep "Rotation Rate" |awk -F":" '{print $NF}' |sed 's/^\s*//g'`
        Model=`smartctl -i $k |grep "Device Model" |awk -F":" '{print $NF}' |sed 's/\s*//g'`
        FW_Ver=`smartctl -i $k |grep "Firmware Version" |awk -F":" '{print $NF}' |sed 's/\s*//g'`
        for m in `seq 1 $(cat ssd_upd_list |wc -l)`
        do
                match_model=`sed -n "$m"p ssd_upd_list |awk '{print $1}'`
                match_fw_ver=`sed -n "$m"p ssd_upd_list |awk '{print $2}'`
                upd_fw=`sed -n "$m"p ssd_upd_list |awk '{print $3}'`
                if [[ "${IS_SSD}" =~ "Solid State Device" ]];then
                        if [[ "${Model}" =~ "${match_model}" ]] && [[ "${FW_Ver}" != "${match_fw_ver}" ]];then
                                echo -e "\033[31m $k start to update ${Model} sata ssd \033[0m"
                                hdparm --fwdownload ${upd_fw} --yes-i-know-what-i-am-doing --please-destroy-my-drive $k
                                if [[ $? -ne 0 ]];then
                                        continue
                                fi
                                sleep 10
                        fi
                fi
        done
done
}

pmc_smartpqi_ssd_upd(){
rm -rf pmc_smartpqi_all_map os_disk_slot_map real_os_disk_slot_map 
for i in `arcconf list |grep "Controllers found" |awk '{print $NF}'`
do
        arcconf getconfig $i ld |grep "Logical Device number" |awk '{print $NF}' > ${i}_pmc_vd_map
        for j in `grep -v "0" ${i}_pmc_vd_map`
        do
        arcconf delete $i logicaldrive $j noprompt >/dev/null 2>&1
        done
        controller_mode=`arcconf list $i |grep -Ew "Controller [0-9]*" |awk -F":" '{print $3}' |awk -F"," '{print $3}' |sed 's/\s*//g'`
        if [[ ${controller_mode} != "Mixed" ]];then
                arcconf setcontrollermode $i 5 >/dev/null 2>&1
                if [[ $? -ne 0 ]];then
                        echo "Please check the fucking PMC card and driver if any issue"
                        exit 1
                fi
        fi
done
sleep 5

# Find the os ssd channel and device id arccording to SN 
for controller_id in `arcconf list |grep "Controllers found" |awk '{print $NF}'`
do
        arcconf getconfig ${controller_id} pd |grep -A10 "Device is a Hard drive" |grep -E "Reported Channel,Device|Model" |awk '{print $NF}' |sed 'N;s/\n/ /' >> pmc_smartpqi_all_map
done
for i in $(seq 0 `expr $(cat pmc_smartpqi_all_map |wc -l) - 1`)
do
        Channel_ID=$(sed -n "$(($i+1))"p pmc_smartpqi_all_map |awk '{print $1}' |awk -F"(" '{print $1}' |awk -F"," '{print $1}')
        Device_ID=$(sed -n "$(($i+1))"p pmc_smartpqi_all_map |awk '{print $1}' |awk -F"(" '{print $1}' |awk -F"," '{print $2}')
        Disk_SN=`smartctl -i -d cciss,$i /dev/sda |awk '/Serial Number/{print $NF}' |sed 's/\s*//g'`
        for os_sn in `arcconf getconfig 1 ld 0  |grep -E "Segment|Present" |awk '{print $NF}' |sed 's/\s*//g'`
        do
                if [[ ${Disk_SN} == ${os_sn} ]];then
                        echo -e "${Channel_ID},${Device_ID},$i" >> os_disk_slot_map
                fi
        done
done
sort -u os_disk_slot_map > real_os_disk_slot_map
# Update os ssd firmware using arcconf tool
if [ -s real_os_disk_slot_map ];then
        for controller_id in `arcconf list |grep "Controllers found" |awk '{print $NF}'`
        do
                for j in `seq 1 $(cat real_os_disk_slot_map |wc -l)`
                do
                        cciss_id=$(sed -n "$j"p real_os_disk_slot_map |awk -F"," '{print $3}')
                        IS_SSD=`smartctl -i -d cciss,${cciss_id} /dev/sda |grep "Rotation Rate" |awk -F":" '{print $NF}' |sed 's/^\s*//g'`
                        Model=`smartctl -i -d cciss,${cciss_id} /dev/sda |grep "Device Model" |awk -F":" '{print $NF}' |sed 's/\s*//g'`
                        FW_Ver=`smartctl -i -d cciss,${cciss_id} /dev/sda |grep "Firmware Version" |awk -F":" '{print $NF}' |sed 's/\s*//g'`
                        New_Channel_ID=$(sed -n "$j"p real_os_disk_slot_map |awk -F"," '{print $1}')
                        New_Device_ID=$(sed -n "$j"p real_os_disk_slot_map |awk -F"," '{print $2}')
                        for k in `seq 1 $(cat ssd_upd_list |wc -l)`
                        do
                                match_model=`sed -n "$k"p ssd_upd_list |awk '{print $1}'`
                                match_fw_ver=`sed -n "$k"p ssd_upd_list |awk '{print $2}'`
                                upd_fw=`sed -n "$k"p ssd_upd_list |awk '{print $3}'`
                                if [[ "${IS_SSD}" =~ "Solid State Device" ]];then
                                        if [[ "${Model}" =~ "${match_model}" ]] && [[ "${FW_Ver}" != "${match_fw_ver}" ]];then
                                                echo -e "\033[31m ${New_Channel_ID},${New_Device_ID} start to erase ${Model} sata ssd \033[0m"
                                                #H3C arcconf tool to update ssd connected in PMC smartpqi RAID/HBA card
                                                arcconf imageupdate ${controller_id} device ${New_Channel_ID} ${New_Device_ID} 32768 ${upd_fw} noprompt
                                                if [[ $? -ne 0 ]];then
                                                        continue
                                                fi
                                                sleep 10
                                        fi
                                fi
                        done
                done
        done
fi
#Update data ssd firmware using hadprm that can make ssd firmware take effect immediately
lsscsi -g |grep ATA > pmc_data_disk_map
for k in `awk '{print $NF}' pmc_data_disk_map`
do
        IS_SSD=`smartctl -i $k |grep "Rotation Rate" |awk -F":" '{print $NF}' |sed 's/^\s*//g'`
        Model=`smartctl -i $k |grep "Device Model" |awk -F":" '{print $NF}' |sed 's/\s*//g'`
        FW_Ver=`smartctl -i $k |grep "Firmware Version" |awk -F":" '{print $NF}' |sed 's/\s*//g'`
        for m in `seq 1 $(cat ssd_upd_list |wc -l)`
        do
                match_model=`sed -n "$m"p ssd_upd_list |awk '{print $1}'`
                match_fw_ver=`sed -n "$m"p ssd_upd_list |awk '{print $2}'`
                upd_fw=`sed -n "$m"p ssd_upd_list |awk '{print $3}'`
                if [[ "${IS_SSD}" =~ "Solid State Device" ]];then
                        if [[ "${Model}" =~ "${match_model}" ]] && [[ "${FW_Ver}" != "${match_fw_ver}" ]];then
                                echo -e "\033[31m $k start to update ${Model} sata ssd \033[0m"
                                hdparm --fwdownload ${upd_fw} --yes-i-know-what-i-am-doing --please-destroy-my-drive $k
                                if [[ $? -ne 0 ]];then
                                        continue
                                fi
                                sleep 10
                        fi
                fi
        done
done
}

direct_ssd_upd(){
lsscsi -g |grep ATA > direct_ssd_map
for i in `cat direct_ssd_map`;do
        IS_SSD=`smartctl -i $i |grep "Rotation Rate" |awk -F":" '{print $NF}' |sed 's/^\s*//g'`
        Model=`smartctl -i $i |grep "Device Model" |awk -F":" '{print $NF}' |sed 's/\s*//g'`
        FW_Ver=`smartctl -i $i |grep "Firmware Version" |awk -F":" '{print $NF}' |sed 's/\s*//g'`
        if [[ "${IS_SSD}" =~ "Solid State Device" ]];then
		for j in `seq 1 $(cat ssd_upd_list |wc -l)`
		do
		match_model=`sed -n "$j"p ssd_upd_list |awk '{print $1}'`
        	match_fw_ver=`sed -n "$j"p ssd_upd_list |awk '{print $2}'`
        	upd_fw=`sed -n "$j"p ssd_upd_list |awk '{print $3}'`
        	if [[ "${Model}" =~ "${match_model}" ]] && [[ "${FW_Ver}" != "${match_fw_ver}" ]];then
                        echo -e "\033[31m $i start to upgrade ${Model} sata ssd firmware \033[0m"
                        hdparm --fwdownload ${upd_fw} --yes-i-know-what-i-am-doing --please-destroy-my-drive $i
                        if [[ $? -ne 0 ]];then
                                continue
                        fi
                        sleep 10
		fi
                done
        else
                echo -e "LSI HBA card: $i ${Model} is not an ssd" 
        fi
done
}

lsi_megaraid_ssd_erase(){
for i in `/opt/MegaRAID/storcli/storcli64 show all |grep -A 10 "Ctl Model" |grep -B 10 "Ctl=Controller Index" |sed -n '/^-/,/-$/p' |grep -v "^-" |awk '{print $1}' |sed '/^\s*$/d'`
do
	/opt/MegaRAID/storcli/storcli64 /c$i/vall show |sed -n '/DG\/VD/,/Cac=CacheCade/p' |sed -n '/-/,/-$/p' |sed -e '1d' -e '$d' |awk '{print $1}' |awk -F"/" '{print $1}' > ${i}_lsi_vd_map
	for j in `grep -v "0" ${i}_lsi_vd_map`;do
		/opt/MegaRAID/storcli/storcli64 /c$i/v$j delete force >/dev/null 2>&1
	done
	/opt/MegaRAID/storcli/storcli64 /c$i set jbod=on >/dev/null 2>&1
	/opt/MegaRAID/storcli/storcli64 /c$i/eall/sall set jbod >/dev/null 2>&1
done
sleep 5
lsscsi -g |grep ATA > lsi_disk_map
for k in `awk '{print $NF}' lsi_disk_map`
do
	IS_SSD=`smartctl -i $k |grep "Rotation Rate" |awk -F":" '{print $NF}' |sed 's/^\s*//g'`	
        Model=`smartctl -i $k |grep "Device Model" |awk -F":" '{print $NF}' |sed 's/\s*//g'`
	FW_Ver=`smartctl -i $k |grep "Firmware Version" |awk -F":" '{print $NF}' |sed 's/\s*//g'`
	for m in `seq 1 $(cat ssd_upd_list |wc -l)`
        do
        	match_model=`sed -n "$m"p ssd_upd_list |awk '{print $1}'`
                match_fw_ver=`sed -n "$m"p ssd_upd_list |awk '{print $2}'`
		if [[ "${IS_SSD}" =~ "Solid State Device" ]];then
        		if [[ "${Model}" =~ "${match_model}" ]] && [[ "${FW_Ver}" == "${match_fw_ver}" ]];then
                		echo -e "\033[31m $k start to erase ${Model} sata ssd \033[0m"
				hdparm --yes-i-know-what-i-am-doing --sanitize-block-erase $k
				if [[ $? -ne 0 ]];then
					continue
				fi
			fi
		fi
	done
done
sleep 120
for n in `/opt/MegaRAID/storcli/storcli64 show all |grep -A 10 "Ctl Model" |grep -B 10 "Ctl=Controller Index" |sed -n '/^-/,/-$/p' |grep -v "^-" |awk '{print $1}' |sed '/^\s*$/d'`
do
	/opt/MegaRAID/storcli/storcli64 /c$n/eall/sall set good force >/dev/null 2>&1
	/opt/MegaRAID/storcli/storcli64 /c$n set jbod=off >/dev/null 2>&1
done
}

lsi_mptsas_ssd_erase(){
#lsscsi -g |grep ATA > lsi_disk_map
#/opt/op/sas2ircu list  |grep "UNKNOWN" > /dev/null 2>&1
#if [[ $? -eq 0 ]];then
#	/opt/op/sas3ircu 0 display  |grep -A13 "Device is a Hard disk" |grep -B11 "Drive Type" |grep -E "Optimal|Serial No" |grep -v "\(OPT\)" |awk '{print $NF}' > os_disk_sn
#else
#	/opt/op/sas2ircu 0 display  |grep -A13 "Device is a Hard disk" |grep -B11 "Drive Type" |grep -E "Optimal|Serial No" |grep -v "\(OPT\)" |awk '{print $NF}' > os_disk_sn
#fi
#for j in `awk '{print $NF}' lsi_disk_map`
#do
#	Disk_SN=`smartctl -i /dev/$j |awk '/Serial Number/{print $NF}' |sed 's/\s*//g'`
#	for os_sn in `cat os_disk_sn`
#	do
#		if [[ ${Disk_SN} == ${os_sn} ]];then
#			echo $j >> os_disk_map
#		fi
#	done
#done
ls /dev/sd* |grep -Ewv "sda[0-9*]|sda" >lsi_disk_map
if [ -s lsi_disk_map ];then
	for k in `cat lsi_disk_map`
	do
        IS_SSD=`smartctl -i $k |grep "Rotation Rate" |awk -F":" '{print $NF}' |sed 's/^\s*//g'`
        Model=`smartctl -i $k |grep "Device Model" |awk -F":" '{print $NF}' |sed 's/\s*//g'`
        FW_Ver=`smartctl -i $k |grep "Firmware Version" |awk -F":" '{print $NF}' |sed 's/\s*//g'`
        	for m in `seq 1 $(cat ssd_upd_list |wc -l)`
        	do
                match_model=`sed -n "$m"p ssd_upd_list |awk '{print $1}'`
                match_fw_ver=`sed -n "$m"p ssd_upd_list |awk '{print $2}'`
                if [[ "${IS_SSD}" =~ "Solid State Device" ]];then
                        if [[ "${Model}" =~ "${match_model}" ]] && [[ "${FW_Ver}" == "${match_fw_ver}" ]];then
                                echo -e "\033[31m $k start to erase ${Model} sata ssd \033[0m"
                                hdparm --yes-i-know-what-i-am-doing --sanitize-block-erase $k
                                if [[ $? -ne 0 ]];then
                                        continue
                                fi
                        fi
                fi
        	done
	done
	sleep 120
fi
}

direct_ssd_erase(){
ls /dev/sd* |grep -Ewv "sda[0-9*]|sda" >direct_disk_map
if [ -s direct_disk_map ];then
	for k in `cat direct_disk_map`
	do
        IS_SSD=`smartctl -i $k |grep "Rotation Rate" |awk -F":" '{print $NF}' |sed 's/^\s*//g'`
        Model=`smartctl -i $k |grep "Device Model" |awk -F":" '{print $NF}' |sed 's/\s*//g'`
        FW_Ver=`smartctl -i $k |grep "Firmware Version" |awk -F":" '{print $NF}' |sed 's/\s*//g'`
		for m in `seq 1 $(cat ssd_upd_list |wc -l)`
		do
		match_model=`sed -n "$m"p ssd_upd_list |awk '{print $1}'`
                match_fw_ver=`sed -n "$m"p ssd_upd_list |awk '{print $2}'`
                if [[ "${IS_SSD}" =~ "Solid State Device" ]];then
                        if [[ "${Model}" =~ "${match_model}" ]] && [[ "${FW_Ver}" == "${match_fw_ver}" ]];then
                                echo -e "\033[31m $k start to erase ${Model} sata ssd \033[0m"
                                hdparm --yes-i-know-what-i-am-doing --sanitize-block-erase $k
                                if [[ $? -ne 0 ]];then
                                        continue
                                fi
                        fi
                fi
        	done
	done
	sleep 120
fi
}

dell_megaraid_ssd_erase(){
for i in `/opt/MegaRAID/perccli/perccli64 show all |grep -A 10 "Ctl Model" |grep -B 10 "Ctl=Controller Index" |sed -n '/^-/,/-$/p' |grep -v "^-" |awk '{print $1}' |sed '/^\s*$/d'`
do
        /opt/MegaRAID/perccli/perccli64 /c$i/vall show |sed -n '/DG\/VD/,/Cac=CacheCade/p' |sed -n '/-/,/-$/p' |sed -e '1d' -e '$d' |awk '{print $1}' |awk -F"/" '{print $1}' > ${i}_dell_lsi_vd_map
        for j in `grep -v "0" ${i}_dell_lsi_vd_map`
	do
        	/opt/MegaRAID/perccli/perccli64 /c$i/v$j delete >/dev/null 2>&1
        done
	/opt/MegaRAID/perccli/perccli64 /c$i set jbod=on >/dev/null 2>&1
        /opt/MegaRAID/perccli/perccli64 /c$i/eall/sall set jbod > /dev/null 2>&1
done
sleep 5
lsscsi -g |grep ATA > dell_lsi_disk_map
for k in `awk '{print $NF}' dell_lsi_disk_map`
do
        IS_SSD=`smartctl -i $k |grep "Rotation Rate" |awk -F":" '{print $NF}' |sed 's/^\s*//g'`
        Model=`smartctl -i $k |grep "Device Model" |awk -F":" '{print $NF}' |sed 's/\s*//g'`
        FW_Ver=`smartctl -i $k |grep "Firmware Version" |awk -F":" '{print $NF}' |sed 's/\s*//g'`
        for m in `seq 1 $(cat ssd_upd_list |wc -l)`
        do
                match_model=`sed -n "$m"p ssd_upd_list |awk '{print $1}'`
                match_fw_ver=`sed -n "$m"p ssd_upd_list |awk '{print $2}'`
                if [[ "${IS_SSD}" =~ "Solid State Device" ]];then
                        if [[ "${Model}" =~ "${match_model}" ]] && [[ "${FW_Ver}" == "${match_fw_ver}" ]];then
                                echo -e "\033[31m $k start to erase ${Model} sata ssd \033[0m"
                                hdparm --yes-i-know-what-i-am-doing --sanitize-block-erase $k
                                if [[ $? -ne 0 ]];then
                                        continue
                                fi
                        fi
                fi
        done
done
sleep 120
for n in `/opt/MegaRAID/perccli/perccli64 show all |grep -A 10 "Ctl Model" |grep -B 10 "Ctl=Controller Index" |sed -n '/^-/,/-$/p' |grep -v "^-" |awk '{print $1}' |sed '/^\s*$/d'`
do
	/opt/MegaRAID/perccli/perccli64 /c$n/eall/sall set good force >/dev/null 2>&1
        /opt/MegaRAID/perccli/perccli64 /c$n set jbod=off >/dev/null 2>&1
done
}

pmc_aacraid_ssd_erase(){
lsscsi -g |grep ATA > pmc_disk_map
for j in `awk '{print $NF}' pmc_disk_map`
do
	Disk_SN=`smartctl -i $j |awk '/Serial Number/{print $NF}' |sed 's/\s*//g'`
	for os_sn in `arcconf getconfig 1 ld 0  |grep -E "Segment|Present" |awk '{print $NF}' |sed 's/\s*//g'`
        do
                if [[ ${Disk_SN} == ${os_sn} ]];then
                        echo $j >> os_disk_map
                fi
        done
done
for k in `awk '{print $NF}' pmc_disk_map |grep -v -f os_disk_map`
do
        IS_SSD=`smartctl -i $k |grep "Rotation Rate" |awk -F":" '{print $NF}' |sed 's/^\s*//g'`
        Model=`smartctl -i $k |grep "Device Model" |awk -F":" '{print $NF}' |sed 's/\s*//g'`
        FW_Ver=`smartctl -i $k |grep "Firmware Version" |awk -F":" '{print $NF}' |sed 's/\s*//g'`
        for m in `seq 1 $(cat ssd_upd_list |wc -l)`
        do
                match_model=`sed -n "$m"p ssd_upd_list |awk '{print $1}'`
                match_fw_ver=`sed -n "$m"p ssd_upd_list |awk '{print $2}'`
                if [[ "${IS_SSD}" =~ "Solid State Device" ]];then
                        if [[ "${Model}" =~ "${match_model}" ]] && [[ "${FW_Ver}" == "${match_fw_ver}" ]];then
                                echo -e "\033[31m $k start to erase ${Model} sata ssd \033[0m"
                                hdparm --yes-i-know-what-i-am-doing --sanitize-block-erase $k
                                if [[ $? -ne 0 ]];then
                                        continue
                                fi
                        fi
                fi
        done
done
sleep 120
}

pmc_smartpqi_ssd_erase(){
rm -rf pmc_smartpqi_all_map os_disk_slot_map *_os_disk_slot_map *_erased_slot_map
for controller_id in `arcconf list |grep "Controllers found" |awk '{print $NF}'`
do
	arcconf getconfig ${controller_id} pd |grep -A10 "Device is a Hard drive" |grep -E "Reported Channel,Device|Model" |awk '{print $NF}' |sed 'N;s/\n/ /' >> pmc_smartpqi_all_map
done	
for i in $(seq 0 `expr $(cat pmc_smartpqi_all_map |wc -l) - 1`)
do
        Channel_ID=$(sed -n "$(($i+1))"p pmc_smartpqi_all_map |awk '{print $1}' |awk -F"(" '{print $1}' |awk -F"," '{print $1}')
	Device_ID=$(sed -n "$(($i+1))"p pmc_smartpqi_all_map |awk '{print $1}' |awk -F"(" '{print $1}' |awk -F"," '{print $2}')
        Disk_SN=`smartctl -i -d cciss,$i /dev/sda |awk '/Serial Number/{print $NF}' |sed 's/\s*//g'`
        for os_sn in `arcconf getconfig 1 ld 0  |grep -E "Segment|Present" |awk '{print $NF}' |sed 's/\s*//g'`
        do
                if [[ ${Disk_SN} == ${os_sn} ]];then
                        echo -e "${Channel_ID},${Device_ID}" >> os_disk_slot_map
                fi
        done
done
awk -F"(" '{print $1}' pmc_smartpqi_all_map |grep -v -f os_disk_slot_map > remove_os_slot_map
if [ -s remove_os_slot_map ];then
	for controller_id in `arcconf list |grep "Controllers found" |awk '{print $NF}'`
	do
		for j in $(seq 0 `expr $(cat remove_os_slot_map |wc -l) - 1`)
		do
        	IS_SSD=`smartctl -i -d cciss,$j /dev/sda |grep "Rotation Rate" |awk -F":" '{print $NF}' |sed 's/^\s*//g'`
        	Model=`smartctl -i -d cciss,$j /dev/sda |grep "Device Model" |awk -F":" '{print $NF}' |sed 's/\s*//g'`
        	FW_Ver=`smartctl -i -d cciss,$j /dev/sda |grep "Firmware Version" |awk -F":" '{print $NF}' |sed 's/\s*//g'`
		New_Channel_ID=$(sed -n "$(($j+1))"p remove_os_slot_map |awk -F"," '{print $1}')
		New_Device_ID=$(sed -n "$(($j+1))"p remove_os_slot_map |awk -F"," '{print $2}')
        		for k in `seq 1 $(cat ssd_upd_list |wc -l)`
        		do
               		match_model=`sed -n "$k"p ssd_upd_list |awk '{print $1}'`
                	match_fw_ver=`sed -n "$k"p ssd_upd_list |awk '{print $2}'`
                	if [[ "${IS_SSD}" =~ "Solid State Device" ]];then
                        	if [[ "${Model}" =~ "${match_model}" ]] && [[ "${FW_Ver}" == "${match_fw_ver}" ]];then
                                	echo -e "\033[31m ${New_Channel_ID},${New_Device_ID} start to erase ${Model} sata ssd \033[0m"
					#H3C arcconf tool to erase ssd connected in PMC smartpqi RAID/HBA card
                               		arcconf task start ${controller_id} device ${New_Channel_ID} ${New_Device_ID} secureerase pattern 5 noprompt
                                	if [[ $? -ne 0 ]];then
                                        	continue
                                	fi
					#sleep 20
					echo -e "${New_Channel_ID} ${New_Device_ID}" >> ${controller_id}_erased_slot_map
                        	fi
                	fi
			done
        	done
		sleep 120
		if [ -s ${controller_id}_erased_slot_map ];then
			cat ${controller_id}_erased_slot_map |sort -u > ${controller_id}_real_erased_slot_map
			for l in $(seq 1 `cat ${controller_id}_erased_slot_map |wc -l`)
        		do
                		Erased_Channel_ID=`sed -n "$l"p ${controller_id}_real_erased_slot_map |awk '{print $1}'`
                		Erased_Slot_ID=`sed -n "$l"p ${controller_id}_real_erased_slot_map |awk '{print $2}'`
                		#H3C arcconf tool to return original state until erase ssd complete
                		arcconf setstate ${controller_id} device ${Erased_Channel_ID} ${Erased_Slot_ID} eed noprompt
        		done
		fi
	done
fi
}

#os_version=`uname -r |awk -F"el" '{print $2}' |awk -F"." '{print $1}'`
which hdparm >/dev/null 2>&1
if [ $? -ne 0 ];then
	yum install -y hdparm
else
	if [[ $(hdparm -V |awk '{print $2}') != "v9.58" ]];then
		yum update -y hdparm
	fi
fi
lspci |grep -Ei "lsi|broadcom|pmc|adaptec" |grep -i sas > bdm_pmc_map
if [ -s bdm_pmc_map ];then
	for i in `seq 1 $(cat bdm_pmc_map |wc -l)`
	do
	bus_id=`sed -n "$i"p bdm_pmc_map |awk '{print $1}'`
	sas_description=`echo $(sed -n "$i"p bdm_pmc_map) |tr 'A-Z' 'a-z'`
	if [[ ${sas_description} =~ "adpatec" ]];then
		if [[ "$(arcconf  |grep Version |awk '{print $5}')" !=  "3.02.00" ]];then
                        rpm -Uvh Arcconf-3.02-23597.x86_64.rpm
			echo y |mv /usr/Arcconf/arcconf /usr/sbin/arcconf
                fi
	fi
	driver_type=`lspci -i pci.ids -s ${bus_id} -v |grep "Kernel driver in use" |awk '{print $NF}' |sed 's/\s*//'`
	if [[ "${driver_type}" = "megaraid_sas" ]];then
		Manufacturer=$(dmidecode -s system-manufacturer |tr 'A-Z' 'a-z')
		if [[ "${Manufacturer}" =~ "dell" ]];then
			dell_megaraid_ssd_upd
			dell_megaraid_ssd_erase
		else 
			lsi_megaraid_ssd_upd
			lsi_megaraid_ssd_erase
		fi
	elif [[ "${driver_type}" = "mpt2sas" ]] || [[ "${driver_type}" = "mpt3sas" ]];then
			lsi_mptsas_ssd_upd
			lsi_mptsas_ssd_erase
	elif [[ "${driver_type}" = "aacraid" ]];then 
			pmc_aacraid_ssd_upd
			pmc_aacraid_ssd_erase
	elif [[ "${driver_type}" = "smartpqi" ]];then
			pmc_smartpqi_ssd_upd
                	pmc_smartpqi_ssd_erase
	fi
	done
else
		direct_ssd_upd
		direct_ssd_erase
fi	
