path_id.sh revision 185ea6a76d3f3a9e9708239d2302bd98e00460fa
# provide the shortest possible unique hardware path to a device
# for the Linux Persistent Device Naming scheme
#
# Copyright (C) 2005-2006 SUSE Linux Products GmbH
# Author:
# Hannes Reinecke <hare@suse.de>
#
# under the terms of the GNU General Public License as published by the
# Free Software Foundation version 2 of the License.
RESULT=1
TYPE=
if [ -z "$DEVPATH" -a -z "$1" ] ; then
exit 1
fi
if [ -z "$DEVPATH" ] ; then
case "$1" in
$SYSFS/*)
;;
*)
DEVPATH=$1
;;
esac
fi
exit 1
fi
/devices/*)
cd "$OPWD"
TYPE="${TYPE##*/}"
;;
/class/*)
TYPE="${DEVPATH#/class/}"
TYPE="${TYPE%%/*}"
;;
/block/*)
;;
*)
exit 1
;;
esac
type=$1
offset=$2
: i $i
done
echo $offset
}
local DEV=$1
cd -P $1
# cciss devices don't have a separate sysfs node
*cciss*)
;;
esac
fi
done
*/pci[0-9]*)
host_dev_path=${host_dev_path%/*}
;;
*)
break
;;
esac
done
if [ "$d" ]; then
d="pci-$pci_id-$d"
else
d="pci-$pci_id"
fi
D="$host_dev_path"
RESULT=0
}
local DEV=$1
cd -P $1
platform_id=${DEV##*/}
*/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
}
local DEV=$1
cd -P $1
*/vbd*)
host_dev_path=${host_dev_path%/*}
;;
*)
break
;;
esac
done
if [ "$d" ]; then
d="xen-$vbd_id-$d"
else
d="xen-$vbd_id"
fi
D="$host_dev_path"
RESULT=0
}
local DEV=$1
cd -P $1
*/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
}
: handle_ide $*
local DEV=$1
local port idedev idecontroller
# IDE
idecontroller=${idedev%/*}
# port info if the controller has more than one interface
port="${port#ide}"
0)
channel=0
;;
1)
channel=1
;;
*)
;;
esac
cd "$OPWD"
if [ "$d" ] ; then
d="ide-${port}:$channel-$d"
else
d="ide-${port}:$channel"
fi
RESULT=0
}
: handle_scsi $*
local DEV=$1
local cil controller_port controller_dev
# SCSI device
cil="${DEV##*/}"
cil="${cil#*:}"
target_dev=${DEV%/*}
target_id=${target_dev##*/target}
cd "$target_dev"
done
controller_port=${target_dev%/*}
controller_dev="${controller_port%/*}"
controller_port="${controller_port##*/host}"
#
cd "$controller_dev"
cd "$OPWD"
scsi_id="scsi-${controller_port}:${cil}"
if [ "$d" ] ; then
d="${scsi_id}-$d"
else
d="$scsi_id"
fi
D="$controller_dev"
RESULT=0
}
: handle_firewire $*
local DEV=$1
read ieee1394_id < $D/ieee1394_id
fi
RESULT=1
return
fi
fw_host_dev=${DEV%/fw-host*}
# IEEE1394 devices are always endpoints
d="ieee1394-0x$ieee1394_id"
D="$fw_host_dev"
RESULT=0
}
: handle_fc $*
local DEV=$1
local cil controller_port controller_dev
# SCSI-FC device
fc_tgt_hcil="${DEV##*/}"
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}"
read wwpn < $fc_tgt_dev/port_name
fi
D=
RESULT=1
return
fi
# Linux currently knows about 32bit luns
tmp_lun1="0000"
tmp_lun0="0000"
lun="0x0000000000000000"
else
lun="0x${tmp_lun3}${tmp_lun2}${tmp_lun1}${tmp_lun0}"
fi
controller_dev="${fc_tgt_path%/host[0-9]*}"
# FC devices are always endpoints
d="fc-${wwpn}:${lun}"
D="$controller_dev"
RESULT=0
}
: handle_sas $*
local DEV=$1
local cil adapter controller_dev
local lun
# SAS device
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
read phy_id < $phy/phy_identifier
if [ $phy_id -lt $sas_phy_id ]; then
fi
fi
done
if [ $sas_phy_id -eq 255 ] ; then
D=
RESULT=1
return
fi
sas_port_id="${sas_phy_path##*/port-}"
sas_port_dev="/sys/class/sas_port/port-${sas_port_id}"
read phy_port < $sas_port_dev/num_phys
fi
sas_end_id="${sas_end_path##*end_device-}"
sas_end_dev="/sys/class/sas_device/end_device-${sas_end_id}"
read end_address < $sas_end_dev/sas_address
read end_id < $sas_end_dev/phy_identifier
fi
D=
RESULT=1
return
fi
controller_dev="${sas_host_path%/host[0-9]*}"
# SAS devices are always endpoints
d="sas-phy${sas_phy_id}:${phy_port}-${sas_end_address}-lun$lun"
D="$controller_dev"
RESULT=0
}
local DEV=$1
local iscsi_session_dir
local iscsi_scsi_lun
# iSCSI device
iscsi_session_dir="${DEV%%/target*}"
iscsi_session="${iscsi_session_dir##*/}"
RESULT=1
return
fi
# Currently we're not doing MC/S
for conn in ${iscsi_session_dir}/connection* ; do
iscsi_conn_num=${conn##*:}
fi
done
RESULT=1
return
fi
RESULT=1
return
fi
if [ -e "${iscsi_session_path}/targetname" ]; then
read iscsi_tgtname < ${iscsi_session_path}/targetname
fi
RESULT=1
return
fi
if [ -e "${iscsi_connection_path}/persistent_address" ] ; then
fi
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 $*
local DEV=$1
cd -P $1
*/usb*)
host_dev_path="${host_dev_path%/*}"
;;
*)
break
;;
esac
done
usb_host_num=${usb_host_path##*/usb}
cd "$host_dev_path"
cd "$OPWD"
if [ "$d" ] ; then
d="usb-$usb_host_port:$port_num-${d}"
else
d="usb-$usb_host_port:$port_num"
fi
D="$host_dev_path"
RESULT=0
}
/devices/*)
full_sysfs_path="${full_sysfs_path%/*}"
# skip parent device of the same subsystem
if [ -L $full_sysfs_path/subsystem ]; then
cd "$OPWD"
subsys="${subsys##*/}"
full_sysfs_path="${full_sysfs_path%/*}"
fi
fi
# skip subsystem directory
subsys="${full_sysfs_path##*/}"
full_sysfs_path="${full_sysfs_path%/*}"
fi
;;
*)
# old sysfs layout
if [ ! -L $full_sysfs_path/device ]; then
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
else
fi
;;
esac
cd "$OPWD"
while [ ! -z "$D" ] ; do
handle_ide "$D"
;;
*/css0/*)
if [ -r $full_sysfs_device_path/wwpn ]; then
read wwpn < $full_sysfs_device_path/wwpn
fi
if [ -r $full_sysfs_device_path/fcp_lun ]; then
read lun < $full_sysfs_device_path/fcp_lun
fi
if [ -r $full_sysfs_device_path/hba_id ]; then
read bus_id < $full_sysfs_device_path/hba_id
fi
# S/390 zfcp adapter
RESULT=0
else
# DASD devices
bus="ccw"
adapter=${D##*/}
RESULT=0
fi
D=
;;
*/rport-[0-9]*:[0-9]*-[0-9]*/*)
handle_fc "$D"
;;
*/end_device-[0-9]*:[0-9]*:[0-9]*/*)
handle_sas "$D"
;;
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"
;;
*/vbd-[0-9]*)
handle_xen "$D"
;;
*/devices)
D=
;;
*)
RESULT=1
return
;;
esac
done
devname=${full_sysfs_path##*/}
rewind="${devname%%st*}"
mode="${devname##*st}"
*l)
mode="l"
;;
*m)
mode="m"
;;
*a)
mode="a"
;;
*)
mode=""
;;
esac
if [ "$d" ]; then
d="$d-${rewind}st${mode}"
fi
fi
}
if [ -z "$d" ]; then
exit 1
fi
echo "ID_PATH=$d"
exit 0