#!/bin/bash
#
# vdev_id: udev helper to generate user-friendly names for JBOD disks
#
# This script parses the file /etc/zfs/vdev_id.conf to map a
# physical path in a storage topology to a channel name.  The
# channel name is combined with a disk enclosure slot number to
# create an alias that reflects the physical location of the drive.
# This is particularly helpful when it comes to tasks like replacing
# failed drives.  Slot numbers may also be re-mapped in case the
# default numbering is unsatisfactory.  The drive aliases will be
# created as symbolic links in /dev/disk/by-vdev.
#
# The only currently supported topologies are sas_direct and
# sas_switch.  A multipath mode is supported in which dm-mpath
# devices are handled by examining the first-listed running
# component disk.  In multipath mode the configuration file
# should contain a channel definition with the same name for
# each path to a given enclosure.

#
# Some example configuration files are given below.

# #
# # Example vdev_id.conf - sas_direct.
# #
#
# multipath     no
# topology      sas_direct
# phys_per_port 4
#
# #       PCI_ID  HBA PORT  CHANNEL NAME
# channel 85:00.0 1         A
# channel 85:00.0 0         B
# channel 86:00.0 1         C
# channel 86:00.0 0         D
#
# #    Linux	Mapped
# #    Slot	Slot
# slot 1        7
# slot 2        10
# slot 3        3
# slot 4        6
# slot 5        2
# slot 6        8
# slot 7        1
# slot 8        4
# slot 9        9
# slot 10       5

# #
# # Example vdev_id.conf - sas_switch
# #
#
# topology      sas_switch
#
# #       SWITCH PORT  CHANNEL NAME
# channel 1            A
# channel 2            B
# channel 3            C
# channel 4            D

# #
# # Example vdev_id.conf - multipath
# #
#
# multipath yes
#
# #       PCI_ID  HBA PORT  CHANNEL NAME
# channel 85:00.0 1         A
# channel 85:00.0 0         B
# channel 86:00.0 1         A
# channel 86:00.0 0         B

PATH=/bin:/sbin:/usr/bin:/usr/sbin
CONFIG=/etc/zfs/vdev_id.conf
PHYS_PER_PORT=
DEV=
SLOT_MAP=
CHANNEL_MAP=
MULTIPATH=
TOPOLOGY=
declare -i i j

usage() {
	cat << EOF
Usage: vdev_id [-h]
       vdev_id <-d device> [-c config_file] [-p phys_per_port]
               [-g sas_direct|sas_switch] [-m]

  -c    specify name of alernate config file [default=$CONFIG]
  -d    specify basename of device (i.e. sda)
  -g    Storage network topology [default="$TOPOLOGY"]
  -m    Run in multipath mode
  -p    number of phy's per switch port [default=$PHYS_PER_PORT]
  -h    show this summary
EOF
	exit 0
}

map_slot() {
	local LINUX_SLOT=$1
	local MAPPED_SLOT=

	MAPPED_SLOT=`awk "/^slot / && \\$2 == ${LINUX_SLOT} \
			{ print \\$3; exit }" $CONFIG`
	if [ -z "$MAPPED_SLOT" ] ; then
		MAPPED_SLOT=$LINUX_SLOT
	fi
	printf "%d" ${MAPPED_SLOT}
}

map_channel() {
	local MAPPED_CHAN=
	local PCI_ID=$1
	local PORT=$2

	case $TOPOLOGY in
		"sas_switch")
		MAPPED_CHAN=`awk "/^channel / && \\$2 == ${PORT} \
			{ print \\$3; exit }" $CONFIG`
		;;
		"sas_direct")
		MAPPED_CHAN=`awk "/^channel / && \\$2 == \"${PCI_ID}\" && \
				\\$3 == ${PORT} { print \\$4; exit }" \
				$CONFIG`
		;;
	esac
	printf "%s" ${MAPPED_CHAN}
}

while getopts 'c:s:d:g:mp:h' OPTION; do
	case ${OPTION} in
	c)
		CONFIG=`readlink -e ${OPTARG}`
	;;
	d)
	DEV=${OPTARG}
	;;
	g)
		TOPOLOGY=$OPTARG
		;;
	p)
		PHYS_PER_PORT=${OPTARG}
		;;
	m)
		MULTIPATH_MODE=yes
		;;
	s)
		SLOT_MAP=`readlink -e ${OPTARG}`
		if [ ! -r $SLOT_MAP ] ; then
			echo "Error: $SLOT_MAP is nonexistant or unreadable"
			exit 1
		fi
		;;
	h)
		usage
		;;
	esac
done

if [ ! -r $CONFIG ] ; then
	exit 0
fi

if [ -z "$DEV" ] ; then
	echo "Error: missing required option -d"
	exit 1
fi

if [ -z "$TOPOLOGY" ] ; then
	TOPOLOGY=`awk "/^topology /{print \\$2; exit}" $CONFIG`
fi
TOPOLOGY=${TOPOLOGY:-sas_direct}
case $TOPOLOGY in
	sas_direct|sas_switch)
		;;
	*)
		echo "Error: unknown topology $TOPOLOGY"
		exit 1
		;;
esac

if [ -z "$PHYS_PER_PORT" ] ; then
	PHYS_PER_PORT=`awk "/^phys_per_port /{print \\$2; exit}" $CONFIG`
fi
PHYS_PER_PORT=${PHYS_PER_PORT:-4}
if ! echo $PHYS_PER_PORT | egrep -q '^[0-9]+$' ; then
	echo "Error: phys_per_port value $PHYS_PER_PORT is non-numeric"
	exit 1
fi

if [ -z "$MULTIPATH_MODE" ] ; then
	MULTIPATH_MODE=`awk "/^multipath /{print \\$2; exit}" $CONFIG`
fi

# Use first running component device if we're handling a dm-mpath device.
if [ "$MULTIPATH_MODE" = "yes" ] ; then
	# If udev didn't tell us the UUID via DM_NAME, find it in /dev/mapper
	if [ -z "$DM_NAME" ] ; then
		DM_NAME=`ls -l --full-time /dev/mapper |
			 awk "/\/$DEV$/{print \\$9}"`
	fi

	# For raw disks udev exports DEVTYPE=partition when handling partitions,
	# and the rules can be written to take advantage of this to append a
	# -part suffix.  For dm devices we get DEVTYPE=disk even for partitions
	# so we have to append the -part suffix directly in the helper.
	if [ "$DEVTYPE" != "partition" ] ; then
		PART=`echo $DM_NAME | awk -Fp '/p/{print "-part"$2}'`
	fi

	# Strip off partition information.
	DM_NAME=`echo $DM_NAME | sed 's/p[0-9][0-9]*$//'`
	if [ -z "$DM_NAME" ] ; then
		exit 0
	fi

	# Get the raw scsi device name from multipath -l.
	DEV=`multipath -l $DM_NAME |awk '/running/{print $3 ; exit}'`
	if [ -z "$DEV" ] ; then
		exit 0
	fi
fi

if echo $DEV | grep -q ^/devices/ ; then
	sys_path=$DEV
else
	sys_path=`udevadm info -q path -p /sys/block/$DEV 2>/dev/null`
fi

dirs=(`echo "$sys_path" | tr / ' '`)
scsi_host_dir="/sys"

# Get path up to /sys/.../hostX
for (( i=0; i<${#dirs[*]}; i++ )); do
	d=${dirs[$i]}
	scsi_host_dir="$scsi_host_dir/$d"
	echo $d | egrep -q -e '^host[0-9]+$' && break
done

if [ $i = ${#dirs[*]} ] ; then
	exit 0
fi

PCI_ID=`echo ${dirs[$(( $i - 1 ))]} | awk -F: '{print $2":"$3}'`

# In sas_switch mode, the directory three levels beneath /sys/.../hostX
# contains symlinks to phy devices that reveal the switch port number.  In
# sas_direct mode, the phy links one directory down reveal the HBA port.
port_dir=$scsi_host_dir
case $TOPOLOGY in
	"sas_switch") j=$(($i+4)) ;;
	"sas_direct") j=$(($i + 1)) ;;
esac
for (( i++; i<=$j; i++ )); do
	port_dir="$port_dir/${dirs[$i]}"
done

PHY=`ls -d $port_dir/phy* 2>/dev/null | head -1 | awk -F: '{print $NF}'`
if [ -z "$PHY" ] ; then
	exit 0
fi
PORT=$(( $PHY / $PHYS_PER_PORT ))

# Look in /sys/.../sas_device/end_device-X for the bay_identifier
# attribute.
end_device_dir=$port_dir
for (( ; i<${#dirs[*]} ; i++ )); do
	d=${dirs[$i]}
	end_device_dir="$end_device_dir/$d"
	if echo $d | egrep -q -e '^end_device' ; then
		end_device_dir="$end_device_dir/sas_device/$d"
		break
	fi
done

SLOT=`cat $end_device_dir/bay_identifier 2>/dev/null`
if [ -z "$SLOT" ] ; then
	exit 0
fi

SLOT=`map_slot $SLOT`
CHAN=`map_channel $PCI_ID $PORT`
if [ -z "$CHAN" ] ; then
	exit 0
fi
ID_VDEV=${CHAN}${SLOT}${PART}

echo "ID_VDEV=${ID_VDEV}"
echo "ID_VDEV_PATH=disk/by-vdev/${ID_VDEV}"
