#!/sbin/bash

cd /tmp
export `cat /onappstore/onappstore.conf | grep hostid`
export `cat /onappstore/onappstore.conf | grep numdisksperctlr`
export Numdisks=`expr ${numdisksperctlr} - 1`
export NumCtlrs=`ls -1 /onappstore/VMconfigs/NODE*-STORAGENODE* | wc -l`

rm -f STORAGE* CONTROLLER* DEVMAP devlist
for i in `dmsetup ls | grep STORAGE | awk '{print $1}'`; do minor=`dmsetup ls | grep ${i} | sed 's/)//g' | sed 's/:/ /g' | awk '{print $3}'`; dev=`ls /sys/block/dm-${minor}/slaves`; if [ -z ${dev} ]; then dev="zero"; fi; echo "${i} /dev/${dev}" >> devlist; done

for i in `seq 0 ${NumCtlrs}`; do for j in `seq 0 ${Numdisks}`; do grep STORAGEDEV${i}_SLOT${j} devlist > /dev/null; if [ $? == 0 ]; then echo "slot:${j}:`grep STORAGEDEV${i}_SLOT${j} devlist | awk '{print $2}'`">> STORAGECONTROLLER-${i}; fi; done; done

for i in `ls -1 /dev/sd*`; do export `onapp_scsi_id $i`; echo "disk:${ID_SERIAL}_${ID_SCSI_SERIAL}:$i:" >> ./DEVMAP; done

for j in `seq 1 ${NumCtlrs}`; do slot=0; for i in vda vdb vdc vdd; do if [ ${slot} -lt ${numdisksperctlr} ]; then echo "slot${slot} `ssh -o StrictHostKeyChecking=no 10.200.${hostid}.$j mount | grep $i | sed 's/-/ /g' | awk '{print $4}'`" >> CONTROLLER${j}; fi; slot=`expr $slot + 1`; done; done

for i in STORAGECONTROLLER*; do j=`echo $i | sed 's/-/ /g'| awk '{print $2}'`; C=`expr $j + 1`; for k in `seq 0 ${Numdisks}`; do dev=`grep $k $i | sed 's/:/ /g' | awk '{print $3}'`; nodeid=`grep "slot${k}" CONTROLLER${C} | awk '{print $2}'`; entry=`grep ${dev} DEVMAP`; echo "${entry}${nodeid}" >> ./STORAGEDEVMAP; done; done

rm -f CONTROLLER* DEVMAP devlist
