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.
14 # to be called from a udev rule to return the name for a symlink
15 # DEVPATH=<devpath>; path_id
18 # examples for all block devices on a system:
19 # for i in `find /sys/class/block`; do DEVPATH="`echo $i | sed -e 's@^/sys\|/dev@@g'`"; path_id; done
26 full_sysfs_device_path=
28 if [ -z "$DEVPATH" -a -z "$1" ] ; then
32 if [ -z "$DEVPATH" ] ; then
43 if [ ! -e $SYSFS$DEVPATH/dev ] ; then
49 cd "$SYSFS$DEVPATH/subsystem";
55 TYPE="${DEVPATH#/class/}"
67 local type offset port
70 for i in $type[0-9]* ; do
73 if [ "$port" -lt "$offset" ] ; then offset=$port ; fi
84 while [ ! -z "$host_dev_path" ] ; do
85 case "$host_dev_path" in
87 host_dev_path=${host_dev_path%/*}
102 local port idedev idecontroller
107 idecontroller=${idedev%/*}
108 # port info if the controller has more than one interface
111 : idedev $idedev kernel_port $port
120 echo "Error: $idedev is neither master or slave" >&2
124 offset="`get_port_offset ide ${port%.*}`"
126 : port offset $offset
127 port=$((${port%.*} - $offset))
129 d="ide-${port}:$channel-$d"
131 d="ide-${port}:$channel"
140 local cil controller_port controller_dev
145 target_id=${target_dev##*/target}
148 for tid in ${target_id}* ; do
149 target_num=$(( $target_num + 1 ))
151 controller_port=${target_dev%/*}
152 controller_dev="${controller_port%/*}"
153 : controller_dev $controller_dev
154 : controller_port $controller_port
155 # a host controller may have more than one interface/port
156 controller_port="${controller_port##*/host}"
159 controller_offset=$(get_port_offset host $controller_port)
161 controller_port=$(( $controller_port - $controller_offset))
162 scsi_id="scsi-${controller_port}:${cil}"
175 if [ -f "$D/ieee1394_id" ] ; then
176 read ieee1394_id < $D/ieee1394_id
178 if [ -z "$ieee1394_id" ] ; then
183 fw_host_dev=${DEV%/fw-host*}
184 # IEEE1394 devices are always endpoints
185 d="ieee1394-0x$ieee1394_id"
193 local cil controller_port controller_dev
195 fc_tgt_hcil="${DEV##*/}"
196 fc_tgt_lun="${fc_tgt_hcil##*:}"
197 fc_tgt_path="${DEV%/*}"
198 fc_tgt_num="${fc_tgt_path##*/}"
199 fc_tgt_dev="${fc_tgt_path}/fc_transport:${fc_tgt_num}"
200 if [ -e "$fc_tgt_dev/port_name" ]; then
201 read wwpn < $fc_tgt_dev/port_name
203 if [ -z "$wwpn" ] ; then
208 # Linux currently knows about 32bit luns
209 tmp_lun3=$(printf "%04x" $(($fc_tgt_lun & 0xFFFF)))
210 tmp_lun2=$(printf "%04x" $(( ($fc_tgt_lun >> 16) & 0xFFFF)))
213 if (($fc_tgt_lun == 0)) ; then
214 lun="0x0000000000000000"
216 lun="0x${tmp_lun3}${tmp_lun2}${tmp_lun1}${tmp_lun0}"
218 controller_dev="${fc_tgt_path%/host[0-9]*}"
219 # FC devices are always endpoints
220 d="fc-${wwpn}:${lun}"
228 local cil adapter controller_dev
230 sas_host_path="${DEV%%/phy*}"
231 sas_phy_path="${DEV#*/host*/}"
232 sas_phy_path="${sas_phy_path%%/target*}"
233 sas_phy_id="${sas_phy_path%%/*}"
234 sas_rphy_id="${sas_phy_path##*/}"
235 sas_phy_dev="${sas_host_path}/${sas_phy_id}/sas_phy:${sas_phy_id}"
236 if [ -e "$sas_phy_dev/sas_address" ]; then
237 read phy_address < $sas_phy_dev/sas_address
238 read phy_port < $sas_phy_dev/port_identifier
239 read phy_id < $sas_phy_dev/phy_identifier
241 if [ -z "$phy_address" ] ; then
242 : no initiator address
246 sas_phy_address="$phy_address:$phy_port:$phy_id"
247 sas_rphy_dev="${sas_host_path}/${sas_phy_id}/${sas_rphy_id}/sas_rphy:${sas_rphy_id}"
248 if [ -e "$sas_rphy_dev/sas_address" ]; then
249 read rphy_address < $sas_rphy_dev/sas_address
250 read rphy_id < $sas_rphy_dev/phy_identifier
252 if [ -z "$rphy_address" ] ; then
253 : no initiator address
257 sas_rphy_address="$rphy_address:$rphy_id"
258 controller_dev="${sas_host_path%/host[0-9]*}"
259 # SAS devices are always endpoints
260 d="sas-${sas_phy_address}-${sas_rphy_address}"
271 port_num=${port_id#*-}
273 while [ ! -z "$host_dev_path" ] ; do
274 case "$host_dev_path" in
276 usb_host_path=$host_dev_path
277 host_dev_path="${host_dev_path%/*}"
284 : host_dev_path $host_dev_path
285 usb_host_num=${usb_host_path##*/usb}
288 usb_host_offset=$(get_port_offset usb $usb_host_num)
289 usb_host_port=$(($usb_host_num - $usb_host_offset))
292 d="usb-$usb_host_port:$port_num-${d}"
294 d="usb-$usb_host_port:$port_num"
301 full_sysfs_path="$SYSFS$DEVPATH"
302 if [ -L $full_sysfs_path/subsystem ]; then
303 # new sysfs block layout
304 full_sysfs_path="${full_sysfs_path%/*}"
305 cd "$full_sysfs_path/subsystem";
308 subsys="${subsys##*/}"
309 if [ "$subsys" == "block" ]; then
310 # parent is "block", it's a partition, move one up
311 full_sysfs_path="${full_sysfs_path%/*}"
315 # old sysfs block layout
316 if [ ! -L $full_sysfs_path/device ] ; then
317 if [ -f $full_sysfs_path/range ] ; then return ; fi
318 full_sysfs_path="${full_sysfs_path%/*}"
319 : full_sysfs_path "$full_sysfs_path"
320 if [ ! -L $full_sysfs_path/device -o ! -f $full_sysfs_path/dev ] ; then
324 cd $full_sysfs_path/device
326 full_sysfs_device_path="`pwd -P`"
328 D=$full_sysfs_device_path
329 while [ ! -z "$D" ] ; do
331 */ide[0-9]/[0-9].[0-9]*|*/ide[0-9][0-9]/[0-9][0-9].[0-9]*)
335 if [ -r $full_sysfs_device_path/wwpn ]; then
336 read wwpn < $full_sysfs_device_path/wwpn
338 if [ -r $full_sysfs_device_path/fcp_lun ]; then
339 read lun < $full_sysfs_device_path/fcp_lun
341 if [ -r $full_sysfs_device_path/hba_id ]; then
342 read bus_id < $full_sysfs_device_path/hba_id
344 if [ "$bus_id" -a "$wwpn" -a "$lun" ]; then
346 d="ccw-$bus_id-zfcp-$wwpn:$lun"
357 */rport-[0-9]*:[0-9]*-[0-9]*/*)
360 */phy-[0-9]*:[0-9]*/*)
366 */host[0-9]*/[0-9]*:[0-9]*:[0-9]*:[0-9]*)
369 */usb[0-9]*/[0-9]*/*)
385 if [ "$TYPE" == "scsi_tape" ] ; then
386 devname=${full_sysfs_path##*/}
387 rewind="${devname%%st*}"
388 mode="${devname##*st}"
404 d="$d-${rewind}st${mode}"