chiark / gitweb /
fix NAME="" logic
[elogind.git] / extras / path_id / path_id
index 3f8efd63ac3dc8ac6e4423e8042cdd7880cb37d0..d21dea70117b8fb91fe28fc80f42d504aedc73af 100755 (executable)
 #      This program is free software; you can redistribute it and/or modify it
 #      under the terms of the GNU General Public License as published by the
 #      Free Software Foundation version 2 of the License.
-#
-# to be called from a udev rule to return the name for a symlink
-#      DEVPATH=<devpath>; path_id
-#      path_id <devpath>
-
-# examples for all block devices on a system:
-#      for i in `find /sys/class/block`; do DEVPATH="`echo $i | sed -e 's@^/sys\|/dev@@g'`"; path_id; done
 
 SYSFS=/sys
 RESULT=1
@@ -81,6 +74,17 @@ handle_pci () {
        DEV=${PWD}
        pci_id=${DEV##*/}
        host_dev_path=$DEV
+
+       # cciss devices don't have a separate sysfs node
+       for blk_link in block*; do
+           if [ -L "$blk_link" ]; then
+               case "$blk_link" in
+                   *cciss*)
+                       d=cciss-${blk_link#*cciss\!}
+                       ;;
+               esac
+           fi
+       done
        while [ ! -z "$host_dev_path" ] ; do
                case "$host_dev_path" in
                        */pci[0-9]*)
@@ -91,7 +95,61 @@ handle_pci () {
                                ;;
                esac
        done
-       d="pci-$pci_id-$d"
+       if [ "$d" ]; then
+               d="pci-$pci_id-$d"
+       else
+               d="pci-$pci_id"
+       fi
+       D="$host_dev_path"
+       RESULT=0
+}
+
+handle_platform () {
+       local DEV=$1
+       cd -P $1
+       DEV=${PWD}
+       platform_id=${DEV##*/}
+       host_dev_path=$DEV
+       while [ ! -z "$host_dev_path" ] ; do
+               case "$host_dev_path" in
+                       */platform*)
+                               host_dev_path=${host_dev_path%/*}
+                               ;;
+                       *)
+                               break
+                               ;;
+               esac
+       done
+       if [ "$d" ]; then
+               d="platform-$platform_id-$d"
+       else
+               d="platform-$platform_id"
+       fi
+       D="$host_dev_path"
+       RESULT=0
+}
+
+handle_serio () {
+       local DEV=$1
+       cd -P $1
+       DEV=${PWD}
+       serio_id=${DEV##*/serio}
+       host_dev_path=$DEV
+       while [ ! -z "$host_dev_path" ] ; do
+               case "$host_dev_path" in
+                       */serio*)
+                               host_dev_path=${host_dev_path%/*}
+                               ;;
+                       *)
+                               break
+                               ;;
+               esac
+       done
+       if [ "$d" ]; then
+               d="serio-$serio_id-$d"
+       else
+               d="serio-$serio_id"
+       fi
        D="$host_dev_path"
        RESULT=0
 }
@@ -170,7 +228,7 @@ handle_scsi () {
 }
 
 handle_firewire () {
-       :handle_firewire $*
+       : handle_firewire $*
        local DEV=$1
        if [ -f "$D/ieee1394_id" ] ; then
                read ieee1394_id < $D/ieee1394_id
@@ -196,12 +254,13 @@ handle_fc () {
        fc_tgt_lun="${fc_tgt_hcil##*:}"
        fc_tgt_path="${DEV%/*}"
        fc_tgt_num="${fc_tgt_path##*/}"
-       fc_tgt_dev="${fc_tgt_path}/fc_transport:${fc_tgt_num}"
+       fc_tgt_dev="${fc_tgt_path}/fc_transport/${fc_tgt_num}"
        if [ -e "$fc_tgt_dev/port_name" ]; then
                read wwpn < $fc_tgt_dev/port_name
        fi
        if [ -z "$wwpn" ] ; then
                : no WWPN
+               D=
                RESULT=1
                return
        fi
@@ -226,42 +285,115 @@ handle_sas () {
        : handle_sas $*
        local DEV=$1
        local cil adapter controller_dev
+       local lun
+       lun=${DEV##*:}
        # SAS device
-       sas_host_path="${DEV%%/phy*}"
-       sas_phy_path="${DEV#*/host*/}"
-       sas_phy_path="${sas_phy_path%%/target*}"
-       sas_phy_id="${sas_phy_path%%/*}"
-       sas_rphy_id="${sas_phy_path##*/}"
-       sas_phy_dev="${sas_host_path}/${sas_phy_id}/sas_phy:${sas_phy_id}"
-       if [ -e "$sas_phy_dev/sas_address" ]; then
-               read phy_address < $sas_phy_dev/sas_address
-               read phy_port < $sas_phy_dev/port_identifier
-               read phy_id < $sas_phy_dev/phy_identifier
-       fi
-       if [ -z "$phy_address" ] ; then
+       sas_end_path="${DEV%%/target*}"
+       sas_host_path="${sas_end_path%%/port*}"
+       sas_phy_path="${sas_end_path#*/host*/}"
+       sas_phy_path="${sas_phy_path%%/*}"
+       sas_phy_path="${sas_host_path}/${sas_phy_path}"
+
+       sas_phy_id=255
+       for phy in $sas_phy_path/phy-*/sas_phy/phy-* ; do
+               if [ -d "$phy" ] ; then
+                       read phy_id < $phy/phy_identifier
+                       if [ $phy_id -lt $sas_phy_id ]; then
+                               sas_phy_id=$phy_id
+                       fi
+               fi
+       done
+
+       if [ $sas_phy_id -eq 255 ] ; then
                : no initiator address
+               D=
                RESULT=1
                return
        fi
-       sas_phy_address="$phy_address:$phy_port:$phy_id"
-       sas_rphy_dev="${sas_host_path}/${sas_phy_path}/sas_rphy:${sas_rphy_id}"
-       if [ -e "$sas_rphy_dev/sas_address" ]; then
-               read rphy_address < $sas_rphy_dev/sas_address
-               read rphy_id < $sas_rphy_dev/phy_identifier
+
+       sas_port_id="${sas_phy_path##*/port-}"
+       sas_port_dev="/sys/class/sas_port/port-${sas_port_id}"
+       if [ -e "$sas_port_dev/num_phys" ] ; then
+               read phy_port < $sas_port_dev/num_phys
        fi
-       if [ -z "$rphy_address" ] ; then
-               : no initiator address
+
+       sas_end_id="${sas_end_path##*end_device-}"
+       sas_end_dev="/sys/class/sas_device/end_device-${sas_end_id}"
+       if [ -e "$sas_end_dev/sas_address" ]; then
+               read end_address < $sas_end_dev/sas_address
+               read end_id < $sas_end_dev/phy_identifier
+       fi
+       if [ -z "$end_address" ] ; then
+               : no end device address
+               D=
                RESULT=1
                return
        fi
-       sas_rphy_address="$rphy_address:$rphy_id"
+       sas_end_address="$end_address:$end_id"
        controller_dev="${sas_host_path%/host[0-9]*}"
        # SAS devices are always endpoints
-       d="sas-${sas_phy_address}-${sas_rphy_address}"
+       d="sas-phy${sas_phy_id}:${phy_port}-${sas_end_address}-lun$lun"
        D="$controller_dev"
        RESULT=0
 }
 
+handle_iscsi() {
+       local DEV=$1
+       local iscsi_session_dir
+       local iscsi_session iscsi_session_path
+       local iscsi_connection iscsi_connection_path
+       local iscsi_scsi_lun
+       # iSCSI device
+       iscsi_session_dir="${DEV%%/target*}"
+       iscsi_session="${iscsi_session_dir##*/}"
+       iscsi_session_path=/sys/class/iscsi_session/${iscsi_session}
+       if [ ! -d "$iscsi_session_path" ] ; then
+           : no iSCSI session path
+           RESULT=1
+           return
+       fi
+       # Currently we're not doing MC/S
+       for conn in ${iscsi_session_dir}/connection* ; do
+           iscsi_conn_num=${conn##*:}
+           if [ "$iscsi_conn_num" = '0' ] ; then
+               iscsi_connection=$(basename $conn)
+           fi
+       done
+       if [ -z "$iscsi_connection" ] ; then
+           : no iSCSI connection found
+           RESULT=1
+           return
+       fi
+       iscsi_connection_path=/sys/class/iscsi_connection/${iscsi_connection}
+       if [ ! -d "$iscsi_connection_path" ] ; then
+           : no iSCSI connection path
+           RESULT=1
+           return
+       fi
+       if [ -e "${iscsi_session_path}/targetname" ]; then
+           read iscsi_tgtname < ${iscsi_session_path}/targetname
+       fi
+       if [ -z "$iscsi_tgtname" ] ; then
+           : No iSCSI Targetname
+           RESULT=1
+           return
+       fi
+       if [ -e "${iscsi_connection_path}/persistent_address" ] ; then
+           read iscsi_address < ${iscsi_connection_path}/persistent_address
+       fi
+       if [ -z "$iscsi_address" ] ; then
+           : No iSCSI Target address
+           RESULT=1
+           return
+       fi
+       if [ -e "${iscsi_connection_path}/persistent_port" ] ; then
+           read iscsi_port < ${iscsi_connection_path}/persistent_port
+       fi
+       iscsi_scsi_lun="${DEV##*:}"
+       d="ip-${iscsi_address}:${iscsi_port}-iscsi-${iscsi_tgtname}-lun-${iscsi_scsi_lun}"
+       RESULT=0
+}
+
 handle_usb () {
 : handle_usb $*
        local DEV=$1
@@ -299,32 +431,47 @@ handle_usb () {
 
 handle_device () {
        full_sysfs_path="$SYSFS$DEVPATH"
-       if [ -L $full_sysfs_path/subsystem ]; then
-               # new sysfs block layout
-               full_sysfs_path="${full_sysfs_path%/*}"
-               cd "$full_sysfs_path/subsystem";
-               subsys="`pwd -P`"
-               cd "$OPWD"
-               subsys="${subsys##*/}"
-               if [ "$subsys" == "block" ]; then
-                       # parent is "block", it's a partition, move one up
+       case "$DEVPATH" in
+               /devices/*)
                        full_sysfs_path="${full_sysfs_path%/*}"
-               fi
-               cd $full_sysfs_path
-       else
-               # old sysfs block layout
-               if [ ! -L $full_sysfs_path/device ] ; then
-                       if [ -f $full_sysfs_path/range ] ; then return ; fi
-                       full_sysfs_path="${full_sysfs_path%/*}"
-                       : full_sysfs_path "$full_sysfs_path"
-                       if [ ! -L $full_sysfs_path/device -o ! -f $full_sysfs_path/dev ] ; then
-                               return
+                       # skip parent device of the same subsystem
+                       if [ -L $full_sysfs_path/subsystem ]; then
+                               cd "$full_sysfs_path/subsystem";
+                               subsys="`pwd -P`"
+                               cd "$OPWD"
+                               subsys="${subsys##*/}"
+                               if [ "$subsys" = "$TYPE" ]; then
+                                       : skip same subsystem parent
+                                       full_sysfs_path="${full_sysfs_path%/*}"
+                               fi
                        fi
-               fi
-               cd $full_sysfs_path/device
-       fi
+                       # skip subsystem directory
+                       subsys="${full_sysfs_path##*/}"
+                       if [ "$subsys" = "$TYPE" ]; then
+                               : skip subsystem directory
+                               full_sysfs_path="${full_sysfs_path%/*}"
+                       fi
+                       cd $full_sysfs_path
+                       ;;
+               *)
+                       # old sysfs layout
+                       if [ ! -L $full_sysfs_path/device ]; then
+                               full_sysfs_path="${full_sysfs_path%/*}"
+                               : full_sysfs_path "$full_sysfs_path"
+                               if [ ! -L $full_sysfs_path/device -o ! -f $full_sysfs_path/dev ]; then
+                                       return
+                               fi
+                       fi
+                       if [ -L $full_sysfs_path/device/device ]; then
+                               cd $full_sysfs_path/device/device
+                       else
+                               cd $full_sysfs_path/device
+                       fi
+                       ;;
+       esac
        full_sysfs_device_path="`pwd -P`"
        cd "$OPWD"
+
        D=$full_sysfs_device_path
        while [ ! -z "$D" ] ; do
                case "$D" in
@@ -357,21 +504,34 @@ handle_device () {
                        */rport-[0-9]*:[0-9]*-[0-9]*/*)
                                handle_fc "$D"
                                ;;
-                       */phy-[0-9]*:[0-9]*/*)
+                       */end_device-[0-9]*:[0-9]*:[0-9]*/*)
                                handle_sas "$D"
                                ;;
                        */fw-host[0-9]*/*)
                                handle_firewire "$D"
                                ;;
+                       */session[0-9]*/*)
+                               handle_iscsi "$D"
+                               D=
+                               ;;
                        */host[0-9]*/[0-9]*:[0-9]*:[0-9]*:[0-9]*)
                                handle_scsi "$D"
                                ;;
+                       */ttyUSB*)
+                               D=${D%/ttyUSB*}
+                               ;;
                        */usb[0-9]*/[0-9]*/*)
                                handle_usb "$D"
                                ;;
                        */pci[0-9]*:[0-9]*)
                                handle_pci "$D"
                                ;;
+                       */serio[0-9]*)
+                               handle_serio "$D"
+                               ;;
+                       */platform/*)
+                               handle_platform "$D"
+                               ;;
                        */devices)
                                D=
                                ;;
@@ -382,7 +542,7 @@ handle_device () {
                                ;;
                esac
        done
-       if [ "$TYPE" == "scsi_tape" ] ; then
+       if [ "$TYPE" = "scsi_tape" ] ; then
                devname=${full_sysfs_path##*/}
                rewind="${devname%%st*}"
                mode="${devname##*st}"
@@ -398,7 +558,7 @@ handle_device () {
                                ;;
                        *)
                                mode=""
-                          ;;
+                               ;;
                esac
                if [ "$d" ]; then
                        d="$d-${rewind}st${mode}"
@@ -406,22 +566,9 @@ handle_device () {
        fi
 }
 
-case "$TYPE" in
-       block)
-               handle_device
-               echo "ID_PATH=$d"
-               ;;
-       scsi_tape)
-               handle_device
-               echo "ID_PATH=$d"
-               ;;
-       input)
-               handle_device
-               echo "ID_PATH=$d"
-               ;;
-       *)
-               RESULT=1
-               ;;
-esac
-
-exit $RESULT
+handle_device
+if [ -z "$d" ]; then
+       exit 1
+fi
+echo "ID_PATH=$d"
+exit 0