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 # example for all block devices on a system:
19 # for i in `find /sys/block -name dev` ;do DEVPATH="`echo $i | sed -e 's@^/sys\|/dev@@g'`" path_id ; done
23 # /block/sr0 -> /devices/pci0002:30/0002:30:0c.0/host0/0:0:1:0
24 # result: pci-0002:30:0c.0-scsi-0:0:1:0
26 # /block/sda -> /devices/pci0002:30/0002:30:0c.0/host0/0:0:4:0
27 # result: pci-0002:30:0c.0-scsi-0:0:4:0
28 # SATA disk, 4 channels per controller
29 # /block/sda -> /devices/pci0001:00/0001:00:07.0/0001:05:0c.0/host0/0:0:0:0
30 # result: pci-0001:05:0c.0-scsi-0:0:0:0
32 # /block/hda -> /devices/pci0002:02/0002:02:0d.0/ide0/0.0
33 # result: pci-0002:02:0d.0-ide-0.0
34 # IDE cdrom on a Mac ASIC:
35 # /block/hdc -> /devices/pci0001:01/0001:01:17.0/0.80000000:mac-io/0.00020000:ata-3/ide1/1.0
36 # result: mac-io_ata-3_master
37 # IDE cdrom on a Mac ASIC, with ide-scsi:
38 # /block/sr0 -> /devices/pci0001:01/0001:01:17.0/0.80000000:mac-io/0.0001f000:ata-4/ide0/0.1/host2/2:0:0:0
39 # result: mac-io_ata-4_slave
41 # USB CDrom drive without 'serial' number:
42 # reusing 'product' and 'manufacturer' string, if available
43 # /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
44 # result: usb-storage-odd-Freecom-USIDERev930:0:0:0
46 # devices may have several interfaces on one PCI device, like IDE:
47 # pci-0001:00:04.0_ide1-master
48 # pci-0001:00:04.0_ide2-master
49 # pci-0001:00:04.0_ide2-slave
50 # they are marked as ports, it is expected that the driver shows
51 # ide1 even if there is nothing connected to either master or slave
54 # match order is important.
55 # first IDE to find ide-scsi devices.
67 if $(pwd -P > /dev/null 2>&1); then
72 full_sysfs_class_path=
73 full_sysfs_device_path=
75 if [ -z "$DEVPATH" -a -z "$1" ] ; then
79 if [ -z "$DEVPATH" ] ; then
90 if [ ! -d $SYSFS$DEVPATH ] ; then
93 if [ ! -f $SYSFS$DEVPATH/dev ] ; then
102 TYPE="${DEVPATH#/class/}"
115 local type offset port
118 for i in $type[0-9]* ; do
121 if [ "$port" -lt "$offset" ] ; then offset=$port ; fi
123 if [ "$port" != "0" ] ; then
124 echo $(($2 - $offset))
128 handle_block_ide () {
129 : handle_block_ide $*
131 local port idedev idecontroller
136 # remove ide-scsi part, leave only channel info
137 */ide[0-9]*/host[0-9]*)
138 while [ ! -z "$d" ] ; do
152 while [ ! -z "$d" ] ; do
165 # port info if the controller has more than one interface
168 : idedev $idedev kernel_port $port
169 case "${idedev##*.}" in
177 echo "Error: $idedev is neither master or slave" >&2
182 d="`echo $d | sed -e 's@^.*:mac-io[^:]\+:\([^/]\+\).*@mac-io_\1@'`"
186 ifname=${full_sysfs_class_path##*/}
187 set -- `sed -n "/$ifname/p" /var/lib/pcmcia/stab`
192 # d="`echo $d | sed -e 's@^.*/\([^/]\{1,\}\)/.@pci-\1@'`"
197 port="`get_port ide $port`"
199 : hardware_port $port
200 if [ -z "$port" ] ; then
201 d="${d}-ide-0:$channel"
203 d="${d}-ide-${port}:$channel"
209 handle_block_scsi () {
210 : handle_block_scsi $*
212 local cil controller_port controller_dev
217 while [ ! -z "$controller_dev" ] ; do
218 case "$controller_dev" in
220 controller_port=$controller_dev
221 controller_dev="${controller_dev%/*}"
226 : controller_dev $controller_dev
227 : controller_port $controller_port
228 # a host controller may have more than one interface/port
229 controller_port="${controller_port##*/}"
230 controller_port="${controller_port##host}"
232 case "$controller_dev" in
233 # grand central, old powermacs
235 adapter="`echo $controller_dev | sed -e 's@/[^/]\{1,\}$@@;s@^.*/@@;s@^.*:@@'`"
240 adapter="${controler_dev##*/}"
244 adapter="${controller_dev##*/}"
249 controller_port="`get_port host $controller_port`"
252 if [ -z "$controller_port" ] ; then
255 d="${d}-scsi-${controller_port}:${cil}"
259 handle_block_usb_storage () {
260 : handle_block_usb_storage $*
265 while [ ! -z "$controller_dev" ] ; do
266 case "$controller_dev" in
268 controller_dev="${controller_dev%/*}"
273 : controller_dev $controller_dev
275 # usb-storage devs have a serial number, hopefully unique
277 if [ -f $controller_dev/../serial ] ; then
278 serial="`sed -e 's@^[ -]\{1,\}\|[ -]\{1,\}$@@g;s@[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789-]@@g' < $controller_dev/../serial`"
279 : serial XXX_${serial}_XXX
281 serial="`echo $serial | sed -e 's@[ 0]\{1,\}@@g'`"
283 if [ -z "$serial" ] ; then
284 # no serial, broken device
285 # has eventually binary junk in vpd
287 if [ -f $controller_dev/../product ] ; then
288 product="`sed -e 's@^[ -]\{1,\}\|[ -]\{1,\}$@@g;s@[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789-]@@g' < $controller_dev/../product`"
290 if [ -f $controller_dev/../manufacturer ] ; then
291 manufacturer="`sed -e 's@^[ -]\{1,\}\|[ -]\{1,\}$@@g;s@[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789-]@@g' < $controller_dev/../manufacturer`"
293 if [ -z "$product" -o -z "$manufacturer" ] ; then
294 read idvendor < $controller_dev/../idVendor
295 read idproduct < $controller_dev/../idProduct
296 identifier="0x${idvendor}-0x${idproduct}"
298 identifier="${manufacturer}-${product}"
300 d="usb-${identifier}"
309 full_sysfs_class_path="$SYSFS$DEVPATH"
310 if [ ! -f $full_sysfs_class_path/dev ] ; then return ; fi
311 # the main device has (hopefully) a symlink to the real device
312 # a partition is a subdir of the main (raw) device
313 if [ ! -L $full_sysfs_class_path/device ] ; then
314 if [ -f $full_sysfs_class_path/range ] ; then return ; fi
315 full_sysfs_class_path="${full_sysfs_class_path%/*}"
316 : full_sysfs_class_path "$full_sysfs_class_path"
317 if [ ! -L $full_sysfs_class_path/device -o ! -f $full_sysfs_class_path/dev ] ; then
321 cd $full_sysfs_class_path/device
322 full_sysfs_device_path="`$pwd_cmd`"
324 D=$full_sysfs_device_path
326 */ide[0-9]/[0-9].[0-9]*|*/ide[0-9][0-9]/[0-9][0-9].[0-9]*)
327 handle_block_ide "$D"
329 */usb[0-9]*/[0-9]*/host[0-9]*/[0-9]*:[0-9]*:[0-9]*:[0-9]*)
330 handle_block_usb_storage "$D"
333 if [ -r $full_sysfs_device_path/wwpn ]; then
334 read wwpn < $full_sysfs_device_path/wwpn
336 if [ -r $full_sysfs_device_path/fcp_lun ]; then
337 read lun < $full_sysfs_device_path/fcp_lun
339 if [ -r $full_sysfs_device_path/hba_id ]; then
340 read bus_id < $full_sysfs_device_path/hba_id
342 if [ "$bus_id" -a "$wwpn" -a "$lun" ]; then
344 d="ccw-$bus_id-zfcp-$wwpn:$lun"
354 */host[0-9]*/[0-9]*:[0-9]*:[0-9]*:[0-9]*)
355 # check for ieee1394 sbp2
356 if test -f $D/ieee1394_id ; then
357 read ieee1394_id < $D/ieee1394_id
358 d="`echo ieee1394-${ieee1394_id} | sed -e 's@:@-@g'`"
361 handle_block_scsi "$D"
372 # look for a partition
373 if [ "$full_sysfs_class_path" != "$SYSFS$DEVPATH" ] ; then
374 dp="`echo $SYSFS$DEVPATH | sed -e 's@^/.*/@@;s@^[^0-9]\{1,\}@@;s@.*_@@'`"