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_usb_storage () {
292 : handle_block_usb_storage $*
297 while [ ! -z "$controller_dev" ] ; do
298 case "$controller_dev" in
300 controller_dev="${controller_dev%/*}"
305 : controller_dev $controller_dev
307 # usb-storage devs have a serial number, hopefully unique
309 if [ -f $controller_dev/../serial ] ; then
310 serial="`sed -e 's@^[ -]\{1,\}\|[ -]\{1,\}$@@g;s@[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789-]@@g' < $controller_dev/../serial`"
311 : serial XXX_${serial}_XXX
313 serial="`echo $serial | sed -e 's@[ 0]\{1,\}@@g'`"
315 if [ -z "$serial" ] ; then
316 # no serial, broken device
317 # has eventually binary junk in vpd
319 if [ -f $controller_dev/../product ] ; then
320 product="`sed -e 's@^[ -]\{1,\}\|[ -]\{1,\}$@@g;s@[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789-]@@g' < $controller_dev/../product`"
322 if [ -f $controller_dev/../manufacturer ] ; then
323 manufacturer="`sed -e 's@^[ -]\{1,\}\|[ -]\{1,\}$@@g;s@[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789-]@@g' < $controller_dev/../manufacturer`"
325 if [ -z "$product" -o -z "$manufacturer" ] ; then
326 read idvendor < $controller_dev/../idVendor
327 read idproduct < $controller_dev/../idProduct
328 identifier="0x${idvendor}-0x${idproduct}"
330 identifier="${manufacturer}-${product}"
332 d="usb-${identifier}"
339 full_sysfs_path="$SYSFS$DEVPATH"
340 if [ -L $full_sysfs_path/subsystem ]; then
341 # new sysfs block layout
342 full_sysfs_path="${full_sysfs_path%/*}"
343 cd "$full_sysfs_path/subsystem";
346 subsys="${subsys##*/}"
347 if [ "$subsys" == "block" ]; then
348 # parent is "block", it's a partition, move one up
349 full_sysfs_path="${full_sysfs_path%/*}"
353 # old sysfs block layout
354 if [ ! -L $full_sysfs_path/device ] ; then
355 if [ -f $full_sysfs_path/range ] ; then return ; fi
356 full_sysfs_path="${full_sysfs_path%/*}"
357 : full_sysfs_path "$full_sysfs_path"
358 if [ ! -L $full_sysfs_path/device -o ! -f $full_sysfs_path/dev ] ; then
362 cd $full_sysfs_path/device
364 full_sysfs_device_path="`pwd -P`"
366 D=$full_sysfs_device_path
368 */ide[0-9]/[0-9].[0-9]*|*/ide[0-9][0-9]/[0-9][0-9].[0-9]*)
369 handle_block_ide "$D"
371 */usb[0-9]*/[0-9]*/host[0-9]*/[0-9]*:[0-9]*:[0-9]*:[0-9]*)
372 handle_block_usb_storage "$D"
375 if [ -r $full_sysfs_device_path/wwpn ]; then
376 read wwpn < $full_sysfs_device_path/wwpn
378 if [ -r $full_sysfs_device_path/fcp_lun ]; then
379 read lun < $full_sysfs_device_path/fcp_lun
381 if [ -r $full_sysfs_device_path/hba_id ]; then
382 read bus_id < $full_sysfs_device_path/hba_id
384 if [ "$bus_id" -a "$wwpn" -a "$lun" ]; then
386 d="ccw-$bus_id-zfcp-$wwpn:$lun"
396 */rport-[0-9]*:[0-9]*-[0-9]*/*)
399 */host[0-9]*/[0-9]*:[0-9]*:[0-9]*:[0-9]*)
400 # check for ieee1394 sbp2
401 if test -f $D/ieee1394_id ; then
402 read ieee1394_id < $D/ieee1394_id
403 d="`echo ieee1394-${ieee1394_id} | sed -e 's@:@-@g'`"
406 handle_block_scsi "$D"