#!/bin/bash
which nvme &>/dev/null
if [ $? -ne 0 ];then
	yum install nvme-cli -y
fi
for DEV in `nvme list |egrep "\bNS8510|\bNS8610" |awk '{print $1}' |awk -F"/" '{print $3}' |sed 's/n1$//g'`;do
	FW_Ver=`smartctl -a /dev/$DEV |awk '/Firmware Version/{print $NF}' |sed 's/\s*//g'`
	if [[ "$FW_Ver" == "HUT70040" ]] && [[ "$FW_Ver" != "MB4S4A17" ]];then
		./inspurmgr fw-download -f firmware.tar /dev/$DEV
		sleep 1
		./inspurmgr fw-activate -s 0x0 -a 0x01 /dev/$DEV
		sleep 3
		./inspurmgr reset /dev/$DEV
		sleep 1
	fi
	LBA_Size=`smartctl -a /dev/$DEV |awk '/Namespace 1 Formatted LBA Size/{print $NF}' |sed 's/\s*//g'`
        if [[ "$LBA_Size" != "512" ]];then
                nvme format -s 1 -l 0 /dev/$DEV
		sleep 1
                nvme reset /dev/$DEV
                sleep 1
        fi
done
