3 # provide the shortest possible unique hardware path to a block device
4 # for the udev persistent disk device naming scheme
6 # Copyright (C) 2005 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=/block/sda/sda3 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
22 # /class/block/sr0 -> /devices/pci0002:30/0002:30:0c.0/host0/target0:0:0/0:0:1:0
23 # pci-0002:30:0c.0-scsi-0:0:1:0
26 # /class/block/sda -> /devices/pci0002:30/0002:30:0c.0/host0/target0:0:0/0:0:4:0
27 # pci-0002:30:0c.0-scsi-0:0:4:0
29 # SATA disk, 4 channels per controller
30 # /class/block/sda -> /devices/pci0001:00/0001:00:07.0/0001:05:0c.0/host0/target0:0:0/0:0:0:0
31 # pci-0001:05:0c.0-scsi-0:0:0:0
34 # /class/block/hda -> /devices/pci0002:02/0002:02:0d.0/ide0/0.0
35 # pci-0002:02:0d.0-ide-0.0
37 # IDE cdrom on a Mac ASIC:
38 # /class/block/hdc -> /devices/pci0001:01/0001:01:17.0/0.80000000:mac-io/0.00020000:ata-3/ide1/1.0
41 # IDE cdrom on a Mac ASIC, with ide-scsi:
42 # /class/block/sr0 -> /devices/pci0001:01/0001:01:17.0/0.80000000:mac-io/0.0001f000:ata-4/ide0/0.1/host2/target2:0:0/2:0:0:0
45 # USB CDrom drive without 'serial' number:
46 # reusing 'product' and 'manufacturer' string, if available
47 # /class/block/sr0 -> /devices/pci0001:00/0001:00:04.0/0001:02:0b.0/usb4/4-2/4-2:1.0/host4/4:0:0:0
48 # usb-storage-odd-Freecom-USIDERev930:0:0:0
50 # devices may have several interfaces on one PCI device, like IDE:
51 # pci-0001:00:04.0_ide1-master
52 # pci-0001:00:04.0_ide2-master
53 # pci-0001:00:04.0_ide2-slave
54 # they are marked as ports, it is expected that the driver shows
55 # ide1 even if there is nothing connected to either master or slave
58 # match order is important.
59 # first IDE to find ide-scsi devices, then SCSI
60 # first usb-storage, then firewire sbp2, then the rest
67 full_sysfs_device_path=
69 if [ -z "$DEVPATH" -a -z "$1" ] ; then
73 if [ -z "$DEVPATH" ] ; then
84 if [ ! -e $SYSFS$DEVPATH/dev ] ; then
90 cd "$SYSFS$DEVPATH/subsystem";
96 TYPE="${DEVPATH#/class/}"
108 local type offset port
111 for i in $type[0-9]* ; do
114 if [ "$port" -lt "$offset" ] ; then offset=$port ; fi
116 if [ "$port" != "0" ] ; then
117 echo $(($2 - $offset))
121 handle_block_ide () {
122 : handle_block_ide $*
124 local port idedev idecontroller
129 # remove ide-scsi part, leave only channel info
130 */ide[0-9]*/host[0-9]*)
131 while [ ! -z "$d" ] ; do
145 while [ ! -z "$d" ] ; do
158 # port info if the controller has more than one interface
161 : idedev $idedev kernel_port $port
162 case "${idedev##*.}" in
170 echo "Error: $idedev is neither master or slave" >&2
175 d="`echo $d | sed -e 's@^.*:mac-io[^:]\+:\([^/]\+\).*@mac-io_\1@'`"
179 ifname=${full_sysfs_path##*/}
180 set -- `sed -n "/$ifname/p" /var/lib/pcmcia/stab`
185 # d="`echo $d | sed -e 's@^.*/\([^/]\{1,\}\)/.@pci-\1@'`"
190 port="`get_port ide $port`"
192 : hardware_port $port
193 if [ -z "$port" ] ; then
194 d="${d}-ide-0:$channel"
196 d="${d}-ide-${port}:$channel"
201 handle_block_scsi () {
202 : handle_block_scsi $*
204 local cil controller_port controller_dev
209 while [ ! -z "$controller_dev" ] ; do
210 case "$controller_dev" in
212 controller_port=$controller_dev
213 controller_dev="${controller_dev%/*}"
218 : controller_dev $controller_dev
219 : controller_port $controller_port
220 # a host controller may have more than one interface/port
221 controller_port="${controller_port##*/}"
222 controller_port="${controller_port##host}"
224 case "$controller_dev" in
225 # grand central, old powermacs
227 adapter="`echo $controller_dev | sed -e 's@/[^/]\{1,\}$@@;s@^.*/@@;s@^.*:@@'`"
232 adapter="${controller_dev##*/}"
236 adapter="${controller_dev##*/}"
241 controller_port="`get_port host $controller_port`"
244 if [ -z "$controller_port" ] ; then
247 d="${d}-scsi-${controller_port}:${cil}"
254 local cil controller_port controller_dev
256 fc_tgt_hcil="${DEV##*/}"
257 fc_tgt_lun="${fc_tgt_hcil##*:}"
258 fc_tgt_path="${DEV%/*}"
259 fc_tgt_num="${fc_tgt_path##*/}"
260 fc_tgt_dev="${fc_tgt_path}/fc_transport:${fc_tgt_num}"
261 if [ -e "$fc_tgt_dev/port_name" ]; then
262 read wwpn < $fc_tgt_dev/port_name
264 if [ -z "$wwpn" ] ; then
269 # Linux currently knows about 32bit luns
270 tmp_lun3=$(printf "%04x" $(($fc_tgt_lun & 0xFFFF)))
271 tmp_lun2=$(printf "%04x" $(( ($fc_tgt_lun >> 16) & 0xFFFF)))
275 if (($fc_tgt_lun == 0)) ; then
276 lun="0x0000000000000000"
278 lun="0x${tmp_lun3}${tmp_lun2}${tmp_lun1}${tmp_lun0}"
280 controller_dev="${fc_tgt_path%/host[0-9]*}"
281 adapter="${controller_dev##*/}"
284 if [ -z "$controller_port" ] ; then
287 d="${d}-fc-${wwpn}:${lun}"
291 handle_block_sas () {
292 : handle_block_sas $*
294 local cil adapter controller_dev
296 sas_host_path="${DEV%%/phy*}"
297 sas_phy_path="${DEV#*/host*/}"
298 sas_phy_path="${sas_phy_path%%/target*}"
299 sas_phy_id="${sas_phy_path%%/*}"
300 sas_rphy_id="${sas_phy_path##*/}"
301 sas_phy_dev="${sas_host_path}/${sas_phy_id}/sas_phy:${sas_phy_id}"
302 if [ -e "$sas_phy_dev/sas_address" ]; then
303 read phy_address < $sas_phy_dev/sas_address
304 read phy_port < $sas_phy_dev/port_identifier
305 read phy_id < $sas_phy_dev/phy_identifier
307 if [ -z "$phy_address" ] ; then
308 : no initiator address
312 sas_phy_address="$phy_address:$phy_port:$phy_id"
313 sas_rphy_dev="${sas_host_path}/${sas_phy_id}/${sas_rphy_id}/sas_rphy:${sas_rphy_id}"
314 if [ -e "$sas_rphy_dev/sas_address" ]; then
315 read rphy_address < $sas_rphy_dev/sas_address
316 read rphy_id < $sas_rphy_dev/phy_identifier
318 if [ -z "$rphy_address" ] ; then
319 : no initiator address
323 sas_rphy_address="$rphy_address:$rphy_id"
325 controller_dev="${sas_host_path%/host[0-9]*}"
326 adapter="${controller_dev##*/}"
329 d="${d}-sas-${sas_phy_address}-${sas_rphy_address}"
333 handle_block_usb_storage () {
334 : handle_block_usb_storage $*
339 while [ ! -z "$controller_dev" ] ; do
340 case "$controller_dev" in
342 controller_dev="${controller_dev%/*}"
347 : controller_dev $controller_dev
349 # usb-storage devs have a serial number, hopefully unique
351 if [ -f $controller_dev/../serial ] ; then
352 serial="`sed -e 's@^[ -]\{1,\}\|[ -]\{1,\}$@@g;s@[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789-]@@g' < $controller_dev/../serial`"
353 : serial XXX_${serial}_XXX
355 serial="`echo $serial | sed -e 's@[ 0]\{1,\}@@g'`"
357 if [ -z "$serial" ] ; then
358 # no serial, broken device
359 # has eventually binary junk in vpd
361 if [ -f $controller_dev/../product ] ; then
362 product="`sed -e 's@^[ -]\{1,\}\|[ -]\{1,\}$@@g;s@[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789-]@@g' < $controller_dev/../product`"
364 if [ -f $controller_dev/../manufacturer ] ; then
365 manufacturer="`sed -e 's@^[ -]\{1,\}\|[ -]\{1,\}$@@g;s@[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789-]@@g' < $controller_dev/../manufacturer`"
367 if [ -z "$product" -o -z "$manufacturer" ] ; then
368 read idvendor < $controller_dev/../idVendor
369 read idproduct < $controller_dev/../idProduct
370 identifier="0x${idvendor}-0x${idproduct}"
372 identifier="${manufacturer}-${product}"
374 d="usb-${identifier}"
381 full_sysfs_path="$SYSFS$DEVPATH"
382 if [ -L $full_sysfs_path/subsystem ]; then
383 # new sysfs block layout
384 full_sysfs_path="${full_sysfs_path%/*}"
385 cd "$full_sysfs_path/subsystem";
388 subsys="${subsys##*/}"
389 if [ "$subsys" == "block" ]; then
390 # parent is "block", it's a partition, move one up
391 full_sysfs_path="${full_sysfs_path%/*}"
395 # old sysfs block layout
396 if [ ! -L $full_sysfs_path/device ] ; then
397 if [ -f $full_sysfs_path/range ] ; then return ; fi
398 full_sysfs_path="${full_sysfs_path%/*}"
399 : full_sysfs_path "$full_sysfs_path"
400 if [ ! -L $full_sysfs_path/device -o ! -f $full_sysfs_path/dev ] ; then
404 cd $full_sysfs_path/device
406 full_sysfs_device_path="`pwd -P`"
408 D=$full_sysfs_device_path
410 */ide[0-9]/[0-9].[0-9]*|*/ide[0-9][0-9]/[0-9][0-9].[0-9]*)
411 handle_block_ide "$D"
413 */usb[0-9]*/[0-9]*/host[0-9]*/[0-9]*:[0-9]*:[0-9]*:[0-9]*)
414 handle_block_usb_storage "$D"
417 if [ -r $full_sysfs_device_path/wwpn ]; then
418 read wwpn < $full_sysfs_device_path/wwpn
420 if [ -r $full_sysfs_device_path/fcp_lun ]; then
421 read lun < $full_sysfs_device_path/fcp_lun
423 if [ -r $full_sysfs_device_path/hba_id ]; then
424 read bus_id < $full_sysfs_device_path/hba_id
426 if [ "$bus_id" -a "$wwpn" -a "$lun" ]; then
428 d="ccw-$bus_id-zfcp-$wwpn:$lun"
438 */rport-[0-9]*:[0-9]*-[0-9]*/*)
441 */phy-[0-9]*:[0-9]*/*)
442 handle_block_sas "$D"
444 */host[0-9]*/[0-9]*:[0-9]*:[0-9]*:[0-9]*)
445 # check for ieee1394 sbp2
446 if test -f $D/ieee1394_id ; then
447 read ieee1394_id < $D/ieee1394_id
448 d="`echo ieee1394-${ieee1394_id} | sed -e 's@:@-@g'`"
451 handle_block_scsi "$D"