4 # this script should provide the shortest possible unique hardware path
8 # Horrible script. Handle with extreme care.
10 # to be called from udev
11 # DEVPATH=/block/sda/sda3 $0 (or similar)
16 # for i in `find /sys/block -name dev` ;do DEVPATH="`echo $i | sed -e 's@^/sys\|/dev@@g'`" $0 ; done
20 # /block/sr0 -> /devices/pci0002:30/0002:30:0c.0/host0/0:0:1:0
21 # result: pci-0002:30:0c.0-scsi-0:0:1:0
23 # /block/sda -> /devices/pci0002:30/0002:30:0c.0/host0/0:0:4:0
24 # result: pci-0002:30:0c.0-scsi-0:0:4:0
25 # SATA disk, 4 channels per controller
26 # /block/sda -> /devices/pci0001:00/0001:00:07.0/0001:05:0c.0/host0/0:0:0:0
27 # result: pci-0001:05:0c.0-scsi-0:0:0:0
29 # /block/hda -> /devices/pci0002:02/0002:02:0d.0/ide0/0.0
30 # result: pci-0002:02:0d.0-ide-0.0
31 # IDE cdrom on a Mac ASIC:
32 # /block/hdc -> /devices/pci0001:01/0001:01:17.0/0.80000000:mac-io/0.00020000:ata-3/ide1/1.0
33 # result: mac-io_ata-3_master
34 # IDE cdrom on a Mac ASIC, with ide-scsi:
35 # /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
36 # result: mac-io_ata-4_slave
38 # USB CDrom drive without 'serial' number:
39 # reusing 'product' and 'manufacturer' string, if available
40 # /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
41 # result: usb-storage-odd-Freecom-USIDERev930:0:0:0
43 # devices may have several interfaces on one PCI device, like IDE:
44 # pci-0001:00:04.0_ide1-master
45 # pci-0001:00:04.0_ide2-master
46 # pci-0001:00:04.0_ide2-slave
47 # they are marked as ports, it is expected that the driver shows
48 # ide1 even if there is nothing connected to either master or slave
51 # match order is important.
52 # first IDE to find ide-scsi devices.
58 PATH=/lib/klibc/bin #/usr:/usr/sbin:/usr/bin:/sbin:/bin
66 if $(pwd -P > /dev/null 2>&1); then
71 full_sysfs_class_path=
72 full_sysfs_device_path=
74 if [ -z "$DEVPATH" -a -z "$1" ] ; then
78 if [ -z "$DEVPATH" ] ; then
89 if [ ! -d $SYSFS$DEVPATH ] ; then
92 if [ ! -f $SYSFS$DEVPATH/dev ] ; then
101 TYPE="${DEVPATH#/class/}"
114 local type offset port
117 for i in $type[0-9]* ; do
120 if [ "$port" -lt "$offset" ] ; then offset=$port ; fi
122 if [ "$port" != "0" ] ; then
123 echo $(($2 - $offset))
127 handle_block_ide () {
128 : handle_block_ide $*
130 local port idedev idecontroller
135 # remove ide-scsi part, leave only channel info
136 */ide[0-9]*/host[0-9]*)
137 while [ ! -z "$d" ] ; do
151 while [ ! -z "$d" ] ; do
164 # port info if the controller has more than one interface
167 : idedev $idedev kernel_port $port
168 case "${idedev##*.}" in
176 echo "Error: $idedev is neither master or slave" >&2
181 d="`echo $d | sed -e 's@^.*:mac-io[^:]\+:\([^/]\+\).*@mac-io_\1@'`"
185 ifname=${full_sysfs_class_path##*/}
186 set -- `sed -n "/$ifname/p" /var/lib/pcmcia/stab`
191 # d="`echo $d | sed -e 's@^.*/\([^/]\{1,\}\)/.@pci-\1@'`"
196 port="`get_port ide $port`"
198 : hardware_port $port
199 if [ -z "$port" ] ; then
200 d="${d}-ide-0:$channel"
202 d="${d}-ide-${port}:$channel"
208 handle_block_scsi () {
209 : handle_block_scsi $*
211 local cil controller_port controller_dev
216 while [ ! -z "$controller_dev" ] ; do
217 case "$controller_dev" in
219 controller_port=$controller_dev
220 controller_dev="${controller_dev%/*}"
225 : controller_dev $controller_dev
226 : controller_port $controller_port
227 # a host controller may have more than one interface/port
228 controller_port="${controller_port##*/}"
229 controller_port="${controller_port##host}"
231 case "$controller_dev" in
232 # grand central, old powermacs
234 adapter="`echo $controller_dev | sed -e 's@/[^/]\{1,\}$@@;s@^.*/@@;s@^.*:@@'`"
238 adapter="${controller_dev##*/}"
243 controller_port="`get_port host $controller_port`"
246 if [ -z "$controller_port" ] ; then
249 d="${d}-scsi-${controller_port}:${cil}"
253 handle_block_usb_storage () {
254 : handle_block_usb_storage $*
259 while [ ! -z "$controller_dev" ] ; do
260 case "$controller_dev" in
262 controller_dev="${controller_dev%/*}"
267 : controller_dev $controller_dev
269 # usb-storage devs have a serial number, hopefully unique
271 if [ -f $controller_dev/../serial ] ; then
272 serial="`sed -e 's@^[ -]\{1,\}\|[ -]\{1,\}$@@g;s@[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_0123456789]@@g' < $controller_dev/../serial`"
273 : serial XXX_${serial}_XXX
275 serial="`echo $serial | sed -e 's@[ 0]\{1,\}@@g'`"
277 if [ -z "$serial" ] ; then
278 # no serial, broken device
279 # has eventually binary junk in vpd
281 if [ -f $controller_dev/../product ] ; then
282 product="`sed -e 's@^[ -]\{1,\}\|[ -]\{1,\}$@@g;s@[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_0123456789]@@g' < $controller_dev/../product`"
284 if [ -f $controller_dev/../manufacturer ] ; then
285 manufacturer="`sed -e 's@^[ -]\{1,\}\|[ -]\{1,\}$@@g;s@[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_0123456789]@@g' < $controller_dev/../manufacturer`"
287 if [ -z "$product" -o -z "$manufacturer" ] ; then
288 read idvendor < $controller_dev/../idVendor
289 read idproduct < $controller_dev/../idProduct
290 identifier="0x${idvendor}-0x${idproduct}"
292 identifier="${manufacturer}-${product}"
294 d="usb-${identifier}"
303 full_sysfs_class_path="$SYSFS$DEVPATH"
304 if [ ! -f $full_sysfs_class_path/dev ] ; then return ; fi
305 # the main device has (hopefully) a symlink to the real device
306 # a partition is a subdir of the main (raw) device
307 if [ ! -L $full_sysfs_class_path/device ] ; then
308 if [ -f $full_sysfs_class_path/range ] ; then return ; fi
309 full_sysfs_class_path="${full_sysfs_class_path%/*}"
310 : full_sysfs_class_path "$full_sysfs_class_path"
311 if [ ! -L $full_sysfs_class_path/device -o ! -f $full_sysfs_class_path/dev ] ; then
315 cd $full_sysfs_class_path/device
316 full_sysfs_device_path="`$pwd_cmd`"
318 D=$full_sysfs_device_path
320 */ide[0-9]/[0-9].[0-9]*|*/ide[0-9][0-9]/[0-9][0-9].[0-9]*)
321 handle_block_ide "$D"
323 */usb[0-9]*/[0-9]*/host[0-9]*/[0-9]*:[0-9]*:[0-9]*:[0-9]*)
324 handle_block_usb_storage "$D"
327 if [ -r $full_sysfs_device_path/wwpn ]; then
328 read wwpn < $full_sysfs_device_path/wwpn
330 if [ -r $full_sysfs_device_path/fcp_lun ]; then
331 read lun < $full_sysfs_device_path/fcp_lun
333 if [ -r $full_sysfs_device_path/hba_id ]; then
334 read bus_id < $full_sysfs_device_path/hba_id
336 if [ "$bus_id" -a "$wwpn" -a "$lun" ]; then
338 d="ccw-$bus_id-zfcp-$wwpn:$lun"
348 */host[0-9]*/[0-9]*:[0-9]*:[0-9]*:[0-9]*)
349 # check for ieee1394 sbp2
350 if test -f $D/ieee1394_id ; then
351 read ieee1394_id < $D/ieee1394_id
352 d="`echo ieee1394-${ieee1394_id} | sed -e 's@:@-@g'`"
355 handle_block_scsi "$D"
366 # look for a partition
367 if [ "$full_sysfs_class_path" != "$SYSFS$DEVPATH" ] ; then
368 dp="`echo $SYSFS$DEVPATH | sed -e 's@^/.*/@@;s@^[^0-9]\{1,\}@@;s@.*_@@'`"