2d6102
#!/bin/bash
2d6102
#
2d6102
# fc_wwpn_id
2d6102
#
2d6102
# Generates device node names links based on FC WWPN
2d6102
# Copyright (c) 2016 Hannes Reinecke, SUSE Linux GmbH
2d6102
#
2d6102
# This program is free software; you can redistribute it and/or modify it
2d6102
# under the terms of the GNU General Public License as published by the
2d6102
# Free Software Foundation version 2 of the License.
2d6102
#
2d6102
2d6102
DEVPATH=$1
2d6102
SCSIPATH=$(cd -P "/sys$DEVPATH/device" || exit; echo "$PWD")
2d6102
2d6102
d=$SCSIPATH
2d6102
[ -d "$d/scsi_disk" ] || exit 0
2d6102
target_lun=${d##*:}
2d6102
2d6102
while [ -n "$d" ] ; do
2d6102
    d=${d%/*}
2d6102
    e=${d##*/}
2d6102
    case "$e" in
2d6102
	rport*)
2d6102
	    rport=$e
2d6102
	    rport_dir="/sys/class/fc_remote_ports/$rport"
2d6102
	    if [ -d "$rport_dir" ] ; then
2d6102
		rport_wwpn=$(cat "$rport_dir/port_name")
2d6102
	    fi
2d6102
	    ;;
2d6102
	host*)
2d6102
	    host=$e
2d6102
	    host_dir="/sys/class/fc_host/$host"
2d6102
	    if [ -d "$host_dir" ] ; then
2d6102
		host_wwpn=$(cat "$host_dir/port_name")
2d6102
		break;
2d6102
	    fi
2d6102
    esac
2d6102
done
2d6102
2d6102
if [ -n "$rport_wwpn" ] || [ -n "$host_wwpn" ] ; then
2d6102
    echo "FC_TARGET_LUN=$target_lun"
2d6102
fi
2d6102
2d6102
if [ -n "$rport_wwpn" ] ; then
2d6102
    echo "FC_TARGET_WWPN=$rport_wwpn"
2d6102
fi
2d6102
2d6102
if [ -n "$host_wwpn" ] ; then
2d6102
    echo "FC_INITIATOR_WWPN=$host_wwpn"
2d6102
fi