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="${controler_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}"
251 handle_block_usb_storage () {
252 : handle_block_usb_storage $*
257 while [ ! -z "$controller_dev" ] ; do
258 case "$controller_dev" in
260 controller_dev="${controller_dev%/*}"
265 : controller_dev $controller_dev
267 # usb-storage devs have a serial number, hopefully unique
269 if [ -f $controller_dev/../serial ] ; then
270 serial="`sed -e 's@^[ -]\{1,\}\|[ -]\{1,\}$@@g;s@[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789-]@@g' < $controller_dev/../serial`"
271 : serial XXX_${serial}_XXX
273 serial="`echo $serial | sed -e 's@[ 0]\{1,\}@@g'`"
275 if [ -z "$serial" ] ; then
276 # no serial, broken device
277 # has eventually binary junk in vpd
279 if [ -f $controller_dev/../product ] ; then
280 product="`sed -e 's@^[ -]\{1,\}\|[ -]\{1,\}$@@g;s@[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789-]@@g' < $controller_dev/../product`"
282 if [ -f $controller_dev/../manufacturer ] ; then
283 manufacturer="`sed -e 's@^[ -]\{1,\}\|[ -]\{1,\}$@@g;s@[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789-]@@g' < $controller_dev/../manufacturer`"
285 if [ -z "$product" -o -z "$manufacturer" ] ; then
286 read idvendor < $controller_dev/../idVendor
287 read idproduct < $controller_dev/../idProduct
288 identifier="0x${idvendor}-0x${idproduct}"
290 identifier="${manufacturer}-${product}"
292 d="usb-${identifier}"
299 full_sysfs_path="$SYSFS$DEVPATH"
300 if [ -L $full_sysfs_path/subsystem ]; then
301 # new sysfs block layout
302 full_sysfs_path="${full_sysfs_path%/*}"
303 cd "$full_sysfs_path/subsystem";
306 subsys="${subsys##*/}"
307 if [ "$subsys" == "block" ]; then
308 # parent is "block", it's a partition, move one up
309 full_sysfs_path="${full_sysfs_path%/*}"
313 # old sysfs block layout
314 if [ ! -L $full_sysfs_path/device ] ; then
315 if [ -f $full_sysfs_path/range ] ; then return ; fi
316 full_sysfs_path="${full_sysfs_path%/*}"
317 : full_sysfs_path "$full_sysfs_path"
318 if [ ! -L $full_sysfs_path/device -o ! -f $full_sysfs_path/dev ] ; then
322 cd $full_sysfs_path/device
324 full_sysfs_device_path="`pwd -P`"
326 D=$full_sysfs_device_path
328 */ide[0-9]/[0-9].[0-9]*|*/ide[0-9][0-9]/[0-9][0-9].[0-9]*)
329 handle_block_ide "$D"
331 */usb[0-9]*/[0-9]*/host[0-9]*/[0-9]*:[0-9]*:[0-9]*:[0-9]*)
332 handle_block_usb_storage "$D"
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"
356 */host[0-9]*/[0-9]*:[0-9]*:[0-9]*:[0-9]*)
357 # check for ieee1394 sbp2
358 if test -f $D/ieee1394_id ; then
359 read ieee1394_id < $D/ieee1394_id
360 d="`echo ieee1394-${ieee1394_id} | sed -e 's@:@-@g'`"
363 handle_block_scsi "$D"