+# Figure out the name for the enclosure symlink
+enclosure_handler () {
+ # We get all the info we need from udev's DEVPATH variable:
+ #
+ # DEVPATH=/sys/devices/pci0000:00/0000:00:03.0/0000:05:00.0/host0/subsystem/devices/0:0:0:0/scsi_generic/sg0
+
+ # Get the enclosure ID ("0:0:0:0")
+ ENC=$(basename $(readlink -m "/sys/$DEVPATH/../.."))
+ if [ ! -d /sys/class/enclosure/$ENC ] ; then
+ # Not an enclosure, bail out
+ return
+ fi
+
+ # Get the long sysfs device path to our enclosure. Looks like:
+ # /devices/pci0000:00/0000:00:03.0/0000:05:00.0/host0/port-0:0/ ... /enclosure/0:0:0:0
+
+ ENC_DEVICE=$(readlink /sys/class/enclosure/$ENC)
+
+ # Grab the full path to the hosts port dir:
+ # /devices/pci0000:00/0000:00:03.0/0000:05:00.0/host0/port-0:0
+ PORT_DIR=$(echo $ENC_DEVICE | grep -Eo '.+host[0-9]+/port-[0-9]+:[0-9]+')
+
+ # Get the port number
+ PORT_ID=$(echo $PORT_DIR | grep -Eo "[0-9]+$")
+
+ # The PCI directory is two directories up from the port directory
+ # /sys/devices/pci0000:00/0000:00:03.0/0000:05:00.0
+ PCI_ID_LONG=$(basename $(readlink -m "/sys/$PORT_DIR/../.."))
+
+ # Strip down the PCI address from 0000:05:00.0 to 05:00.0
+ PCI_ID=$(echo "$PCI_ID_LONG" | sed -r 's/^[0-9]+://g')
+
+ # Name our device according to vdev_id.conf (like "L0" or "U1").
+ NAME=$(awk "/channel/{if (\$1 == \"channel\" && \$2 == \"$PCI_ID\" && \
+ \$3 == \"$PORT_ID\") {print \$4int(count[\$4])}; count[\$4]++}" $CONFIG)
+
+ echo "${NAME}"
+}
+