File block-npiv of Package xen

#!/bin/bash

# Usage: block-npiv [add npiv | remove dev]

dir=$(dirname "$0")
. "$dir/block-npiv-common.sh"
. "$dir/block-common.sh"

#set -x
#command=$1

case "$command" in
  add)
    # Params is one big arg, with fields separated by hyphens:
    # single path: 
    #    FABRIC-VPWWPN-VPWWNN-TGTWWPN-LUN#
    # multipath: 
    #    {FABRIC1.FABRIC2}-{VPWWPN1.VPWWPN2.VPWWPN3}-VPWWNN-TGTWWPN-LUN#
    #    arg 2 - Fabric Name
    #    arg 3 - VPORT's WWPN
    #    arg 4 - VPORT's WWNN
    #    arg 5 - Target's WWPN
    #    arg 6 - LUN # on Target
    # no wwn contains a leading 0x - it is a 16 character hex value
    # You may want to optionally pick a specific adapter ?
    par=`xenstore-read $XENBUS_PATH/params` || true
    #par=$2
    NPIVARGS=$par;
    LUN=${NPIVARGS##*-*-*-*-}; NPIVARGS=${NPIVARGS%-*}
    if test $LUN = $NPIVARGS ; then exit 1; fi
    TGTWWPN=${NPIVARGS##*-*-*-}; NPIVARGS=${NPIVARGS%-*}
    if test $TGTWWPN = $NPIVARGS ; then exit 1; fi
    VPORTWWNN=${NPIVARGS##*-*-}; NPIVARGS=${NPIVARGS%-*}
    if test $VPORTWWNN = $NPIVARGS ; then exit 1; fi
    VPORTWWPNS=${NPIVARGS##*-}; NPIVARGS=${NPIVARGS%-*}
    if test $VPORTWWPNS = $NPIVARGS ; then exit 1; fi
    FABRICNMS=$NPIVARGS

    # Ensure we compare everything using lower-case hex characters
    TGTWWPN=`echo $TGTWWPN | tr A-Z a-z`
    VPORTWWPNS=`echo $VPORTWWPNS | tr A-Z a-z |sed 's/[{.}]/ /g'`
    VPORTWWNN=`echo $VPORTWWNN | tr A-Z a-z` 
    FABRICNMS=`echo $FABRICNMS | tr A-Z a-z |sed 's/[{.}]/ /g'`


    claim_lock "npiv"
    paths=0
    for FABRICNM in $FABRICNMS; do
      for VPORTWWPN in $VPORTWWPNS; do
        find_vhost $VPORTWWPN $FABRICNM
        if test -z "$vhost" ; then
          create_vport $FABRICNM $VPORTWWPN $VPORTWWNN
          if [ $? -ne 0 ] ; then exit 2; fi
          sleep 8
          find_vhost $VPORTWWPN $FABRICNM
          if test -z "$vhost" ; then exit 3; fi
        fi
        find_sdev $vhost $TGTWWPN $LUN
        if test -z "$dev"; then
          echo "- - -" > /sys/class/scsi_host/$vhost/scan
          sleep 2
          find_sdev $vhost $TGTWWPN $LUN
        fi
        if test -z "$dev"; then
          exit 4
	fi
        paths=$(($paths+1))
      done
    done
    release_lock "npiv"

    if test $paths -gt 1; then
      xenstore-write $XENBUS_PATH/multipath 1
      /etc/init.d/multipathd start
      if test $? -ne 0 ; then exit 4; fi
      dm=`multipath -l /dev/$dev | grep dm | cut -f2 -d' '`
    else
      xenstore-write $XENBUS_PATH/multipath 0
      dm=$dev
    fi
    
    if test ! -z "$dm"; then
      xenstore-write $XENBUS_PATH/node /dev/$dm
      write_dev /dev/$dm
      exit 0
    fi

    exit 4
    ;;

  remove)
    node=`xenstore-read $XENBUS_PATH/node` || true
    multipath=`xenstore-read $XENBUS_PATH/multipath` || true
    # this is really screwy. the first delete of a lun will
    # terminate the entire vport (all luns)
    if test $multipath = 1; then
      par=`xenstore-read $XENBUS_PATH/params` || true
      NPIVARGS=$par;
      FABRICNMS=${NPIVARGS%%-*}; NPIVARGS=${NPIVARGS#*-}
      VPORTWWPNS=${NPIVARGS%%-*}
      VPORTWWPNS=`echo $VPORTWWPNS | tr A-Z a-z |sed 's/[{.}]/ /g'`
      FABRICNMS=`echo $FABRICNMS | tr A-Z a-z |sed 's/[{.}]/ /g'`
      for FABRICNM in $FABRICNMS; do
        for VPORTWWPN in $VPORTWWPNS; do
          find_vhost $VPORTWWPN $FABRICNM
          if test -z "$vhost" ; then exit 5; fi
          flush_nodes_on_vhost $vhost
          delete_vhost $vhost
        done
      done
    else
      dev=$node; dev=${dev#/dev/}
      find_vhost_from_dev $dev
      if test -z "$vhost" ; then exit 5; fi
      flush_nodes_on_vhost $vhost
      delete_vhost $vhost
    fi

    exit 0
    ;;
esac
openSUSE Build Service is sponsored by