3 # provide the shortest possible unique hardware path to a device
4 # for the Linux Persistent Device Naming scheme
6 # Copyright (C) 2005-2006 SUSE Linux Products GmbH
8 # Hannes Reinecke <hare@suse.de>
10 # This program is free software; you can redistribute it and/or modify it
11 # under the terms of the GNU General Public License as published by the
12 # Free Software Foundation version 2 of the License.
19 full_sysfs_device_path=
21 if [ -z "$DEVPATH" -a -z "$1" ] ; then
25 if [ -z "$DEVPATH" ] ; then
36 if [ ! -e $SYSFS$DEVPATH/dev ] ; then
42 cd "$SYSFS$DEVPATH/subsystem";
48 TYPE="${DEVPATH#/class/}"
60 local type offset port
63 for i in $type[0-9]* ; do
66 if [ "$port" -lt "$offset" ] ; then offset=$port ; fi
78 # cciss devices don't have a separate sysfs node
79 for blk_link in block*; do
80 if [ -L "$blk_link" ]; then
83 d=cciss-${blk_link#*cciss\!}
88 while [ ! -z "$host_dev_path" ] ; do
89 case "$host_dev_path" in
91 host_dev_path=${host_dev_path%/*}
107 platform_id=${DEV##*/}
109 while [ ! -z "$host_dev_path" ] ; do
110 case "$host_dev_path" in
112 host_dev_path=${host_dev_path%/*}
120 d="platform-$platform_id-$d"
122 d="platform-$platform_id"
132 serio_id=${DEV##*/serio}
134 while [ ! -z "$host_dev_path" ] ; do
135 case "$host_dev_path" in
137 host_dev_path=${host_dev_path%/*}
145 d="serio-$serio_id-$d"
156 local port idedev idecontroller
161 idecontroller=${idedev%/*}
162 # port info if the controller has more than one interface
165 : idedev $idedev kernel_port $port
174 echo "Error: $idedev is neither master or slave" >&2
178 offset="`get_port_offset ide ${port%.*}`"
180 : port offset $offset
181 port=$((${port%.*} - $offset))
183 d="ide-${port}:$channel-$d"
185 d="ide-${port}:$channel"
194 local cil controller_port controller_dev
199 target_id=${target_dev##*/target}
202 for tid in ${target_id}* ; do
203 target_num=$(( $target_num + 1 ))
205 controller_port=${target_dev%/*}
206 controller_dev="${controller_port%/*}"
207 : controller_dev $controller_dev
208 : controller_port $controller_port
209 # a host controller may have more than one interface/port
210 controller_port="${controller_port##*/host}"
213 controller_offset=$(get_port_offset host $controller_port)
215 controller_port=$(( $controller_port - $controller_offset))
216 scsi_id="scsi-${controller_port}:${cil}"
229 if [ -f "$D/ieee1394_id" ] ; then
230 read ieee1394_id < $D/ieee1394_id
232 if [ -z "$ieee1394_id" ] ; then
237 fw_host_dev=${DEV%/fw-host*}
238 # IEEE1394 devices are always endpoints
239 d="ieee1394-0x$ieee1394_id"
247 local cil controller_port controller_dev
249 fc_tgt_hcil="${DEV##*/}"
250 fc_tgt_lun="${fc_tgt_hcil##*:}"
251 fc_tgt_path="${DEV%/*}"
252 fc_tgt_num="${fc_tgt_path##*/}"
253 fc_tgt_dev="${fc_tgt_path}/fc_transport:${fc_tgt_num}"
254 if [ -e "$fc_tgt_dev/port_name" ]; then
255 read wwpn < $fc_tgt_dev/port_name
257 if [ -z "$wwpn" ] ; then
263 # Linux currently knows about 32bit luns
264 tmp_lun3=$(printf "%04x" $(($fc_tgt_lun & 0xFFFF)))
265 tmp_lun2=$(printf "%04x" $(( ($fc_tgt_lun >> 16) & 0xFFFF)))
268 if (($fc_tgt_lun == 0)) ; then
269 lun="0x0000000000000000"
271 lun="0x${tmp_lun3}${tmp_lun2}${tmp_lun1}${tmp_lun0}"
273 controller_dev="${fc_tgt_path%/host[0-9]*}"
274 # FC devices are always endpoints
275 d="fc-${wwpn}:${lun}"
283 local cil adapter controller_dev
285 sas_host_path="${DEV%%/port*}"
286 sas_phy_path="${DEV#*/host*/}"
287 sas_phy_path="${sas_phy_path%%/target*}"
288 sas_phy_id="${sas_phy_path%%/*}"
289 sas_phy_id="${sas_phy_id##*port-}"
290 sas_port_id="${sas_phy_path%%/end_device*}"
291 sas_port_id="${sas_port_id##*port-}"
292 sas_end_id="${sas_phy_path##*end_device-}"
293 sas_phy_dev="/sys/class/sas_phy/phy-${sas_phy_id}"
294 if [ -e "$sas_phy_dev/sas_address" ]; then
295 read phy_address < $sas_phy_dev/sas_address
296 read phy_id < $sas_phy_dev/phy_identifier
298 if [ -z "$phy_address" ] ; then
299 : no initiator address
304 sas_port_dev="/sys/class/sas_port/port-${sas_port_id}"
305 if [ -e "$sas_port_dev/num_phys" ] ; then
306 read phy_port < $sas_port_dev/num_phys
308 if [ -z "$phy_port" ] ; then
309 : no initiator address
314 sas_phy_address="$phy_address:$phy_port:$phy_id"
315 sas_end_dev="/sys/class/sas_device/end_device-${sas_end_id}"
316 if [ -e "$sas_end_dev/sas_address" ]; then
317 read end_address < $sas_end_dev/sas_address
318 read end_id < $sas_end_dev/phy_identifier
320 if [ -z "$end_address" ] ; then
321 : no initiator address
326 sas_end_address="$end_address:$end_id"
327 controller_dev="${sas_host_path%/host[0-9]*}"
328 # SAS devices are always endpoints
329 d="sas-${sas_phy_address}-${sas_end_address}"
336 local iscsi_session_dir
337 local iscsi_session iscsi_session_path
338 local iscsi_connection iscsi_connection_path
341 iscsi_session_dir="${DEV%%/target*}"
342 iscsi_session="${iscsi_session_dir##*/}"
343 iscsi_session_path=/sys/class/iscsi_session/${iscsi_session}
344 if [ ! -d "$iscsi_session_path" ] ; then
345 : no iSCSI session path
349 # Currently we're not doing MC/S
350 for conn in ${iscsi_session_dir}/connection* ; do
351 iscsi_conn_num=${conn##*:}
352 if [ "$iscsi_conn_num" = '0' ] ; then
353 iscsi_connection=$(basename $conn)
356 if [ -z "$iscsi_connection" ] ; then
357 : no iSCSI connection found
361 iscsi_connection_path=/sys/class/iscsi_connection/${iscsi_connection}
362 if [ ! -d "$iscsi_connection_path" ] ; then
363 : no iSCSI connection path
367 if [ -e "${iscsi_session_path}/targetname" ]; then
368 read iscsi_tgtname < ${iscsi_session_path}/targetname
370 if [ -z "$iscsi_tgtname" ] ; then
371 : No iSCSI Targetname
375 if [ -e "${iscsi_connection_path}/persistent_address" ] ; then
376 read iscsi_address < ${iscsi_connection_path}/persistent_address
378 if [ -z "$iscsi_address" ] ; then
379 : No iSCSI Target address
383 if [ -e "${iscsi_connection_path}/persistent_port" ] ; then
384 read iscsi_port < ${iscsi_connection_path}/persistent_port
386 iscsi_scsi_lun="${DEV##*:}"
387 d="ip-${iscsi_address}:${iscsi_port}-iscsi-${iscsi_tgtname}-lun-${iscsi_scsi_lun}"
397 port_num=${port_id#*-}
399 while [ ! -z "$host_dev_path" ] ; do
400 case "$host_dev_path" in
402 usb_host_path=$host_dev_path
403 host_dev_path="${host_dev_path%/*}"
410 : host_dev_path $host_dev_path
411 usb_host_num=${usb_host_path##*/usb}
414 usb_host_offset=$(get_port_offset usb $usb_host_num)
415 usb_host_port=$(($usb_host_num - $usb_host_offset))
418 d="usb-$usb_host_port:$port_num-${d}"
420 d="usb-$usb_host_port:$port_num"
427 full_sysfs_path="$SYSFS$DEVPATH"
431 if [ -L $full_sysfs_path/subsystem ]; then
432 full_sysfs_path="${full_sysfs_path%/*}"
433 cd "$full_sysfs_path/subsystem";
436 subsys="${subsys##*/}"
437 if [ "$subsys" = "block" ]; then
438 # parent is "block", it's a partition, move one up
439 full_sysfs_path="${full_sysfs_path%/*}"
446 if [ ! -L $full_sysfs_path/device ] ; then
447 if [ -f $full_sysfs_path/range ] ; then return ; fi
448 full_sysfs_path="${full_sysfs_path%/*}"
449 : full_sysfs_path "$full_sysfs_path"
450 if [ ! -L $full_sysfs_path/device -o ! -f $full_sysfs_path/dev ] ; then
454 cd $full_sysfs_path/device
457 full_sysfs_device_path="`pwd -P`"
459 D=$full_sysfs_device_path
460 while [ ! -z "$D" ] ; do
462 */ide[0-9]/[0-9].[0-9]*|*/ide[0-9][0-9]/[0-9][0-9].[0-9]*)
466 if [ -r $full_sysfs_device_path/wwpn ]; then
467 read wwpn < $full_sysfs_device_path/wwpn
469 if [ -r $full_sysfs_device_path/fcp_lun ]; then
470 read lun < $full_sysfs_device_path/fcp_lun
472 if [ -r $full_sysfs_device_path/hba_id ]; then
473 read bus_id < $full_sysfs_device_path/hba_id
475 if [ "$bus_id" -a "$wwpn" -a "$lun" ]; then
477 d="ccw-$bus_id-zfcp-$wwpn:$lun"
488 */rport-[0-9]*:[0-9]*-[0-9]*/*)
491 */end_device-[0-9]*:[0-9]*:[0-9]*/*)
501 */host[0-9]*/[0-9]*:[0-9]*:[0-9]*:[0-9]*)
504 */usb[0-9]*/[0-9]*/*)
526 if [ "$TYPE" = "scsi_tape" ] ; then
527 devname=${full_sysfs_path##*/}
528 rewind="${devname%%st*}"
529 mode="${devname##*st}"
545 d="$d-${rewind}st${mode}"