#
# Storwize non-disruptive volume move between cluster members (eg move volume between different io groups)
# This version is for QLogic HBAs with normal path count is 4 and when both cluster members have access its 8.
# 
# If you have different HBAs (like Emulex cards, it needs rescan-scsi-bus.sh --issue-lip type scanning to notice new paths) 
# or different path counts, then modify values in this script where needed.
#
# Version: 1.1.1
# Dated: 20.1.2016
# (c) Pekka Panula / Sofor Oy
# License: GPLv2
#
# Get SR to migrate, case sensitive!
if [ -z "$1" ]
then
	echo "Usage: $0 <SR name, case sensitive>"
	exit
fi

SR_NAME="$1"

# set new max multipath count for SR
DEFAULT_MAX=4

# my FQDN hostname
HOSTNAME=$(hostname)

# get SCSIid
SCSI_ID=$(xe pbd-list sr-name-label=${SR_NAME} |grep SCSIid|head -n 1|cut -d":" -f3|xargs)

# SCSI_ID="3600507680281034b08000000000000d7"

echo "Migrating SR: ${SR_NAME}, SCSIid: ${SCSI_ID}"

   done=0
 siirto=0
  start=1

function set_max_paths {

   local SR_NAME=$1
   local     MAX=$2

   if [ -z "$MAX" ]
   then
	echo "Max multipath count missing, assuming ${DEFAULT_MAX}"
	MAX=${DEFAULT_MAX}
   fi

   HOST_UUID=$(xe host-list hostname=${HOSTNAME} --minimal)

   if [ -z "$HOST_UUID" ]
   then
	echo "Cant find host uuid"
   fi

   echo "uuid for this xenserver host: $HOST_UUID"

   PBD_UUID=$(xe pbd-list sr-name-label=${SR_NAME} host-uuid=${HOST_UUID} --minimal)

   if [ -z "$PBD_UUID" ]
   then
	echo "Cant find PBD for SR ${SR_NAME}"
   fi

    echo "PBD uuid for ${SR_NAME} for this XenServer host: $PBD_UUID"

   # get SCSIid
   SCSI_ID=$(xe pbd-list sr-name-label=${SR_NAME} |grep SCSIid|head -n 1|cut -d":" -f3|xargs)

   if [ -z "$SCSI_ID" ]
   then
	echo "Cant find SCSI id"
   fi

   echo "SCSI id for ${SR_NAME} is ${SCSI_ID}"

   MULTIPATH_VALUE=$(xe pbd-param-get uuid=${PBD_UUID} param-name=other-config param-key=mpath-${SCSI_ID})

   if [ -z "MULTIPATH_VALUE" ] 
   then
	echo "Cant find multipath values for PBD"
   fi

   echo "Current multipath value for SR for this host: ${MULTIPATH_VALUE}"

   COUNT=$(echo $MULTIPATH_VALUE|cut -f 1 -d"," | sed 's/[^0-9]*//g')

   if [ -z "$COUNT" ]
   then
	echo "Cant find multipath count"
   fi

   echo "Multipath count: $COUNT"

   NEW_VALUE="[${COUNT}, ${MAX}]"

   echo "Setting new multipath values for count and max paths: $NEW_VALUE"

   xe pbd-param-set uuid=${PBD_UUID} other-config:mpath-${SCSI_ID}="${NEW_VALUE}"

   echo "Done"
}

function scsi_bus_scan() {
#	echo "- - -" >/sys/class/scsi_host/host0/scan
#	echo "- - -" >/sys/class/scsi_host/host1/scan
	rescan-scsi-bus.sh >/dev/null
	sleep 2
}

function path_rescan {
	scsi_bus_scan
	multipath -r >/dev/null && sleep 2 && multipathd -k"reconfigure" >/dev/null
}

while [ $done != "1" ]
do
	echo "Scanning SCSI Bus..."
	scsi_bus_scan

	# get count, do not count failed paths
	count=$(multipathd -k"show paths format \"%w %t %o %T\""|grep ${SCSI_ID}|grep -v failed|wc -l)
#	count=$(</tmp/count.txt)

	echo "SR Multipath count: $count"

	case "$count" in
	    "4" )
	        if [ $start == "0" ]; 
		then
			echo "Path scan because 8 -> 4 paths..." 
			path_rescan
			set_max_paths $SR_NAME "4"
			done=1
		fi
		
		;;
	    "8" )
		if [ $siirto == "0" ];
		then
			echo "Path scan because 4 -> 8 paths..." 
			path_rescan
			start=0 
			siirto=1
		fi
		;;
	esac

	echo -n "." 
	sleep 10
done

echo "Removing all faulty block devices..."

for faulty_block in $(multipathd -k"show paths format \"%d %t %o %T\""|grep faulty|cut -d" " -f1)
do
  echo "Faulty device: ${faulty_block}, removing..."
  echo 1 >/sys/block/${faulty_block}/device/delete
done

echo "Migration done."
