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
126 while [ ! -z "$host_dev_path" ] ; do
127 case "$host_dev_path" in
129 host_dev_path=${host_dev_path%/*}
140 handle_block_ide () {
141 : handle_block_ide $*
143 local port idedev idecontroller
149 idecontroller=${idedev%/*}
150 # port info if the controller has more than one interface
153 : idedev $idedev kernel_port $port
162 echo "Error: $idedev is neither master or slave" >&2
166 offset="`get_port_offset ide ${port%.*}`"
168 : port offset $offset
169 port=$((${port%.*} - $offset))
171 d="ide-${port}:$channel-$d"
173 d="ide-${port}:$channel"
179 handle_block_scsi () {
180 : handle_block_scsi $*
182 local cil controller_port controller_dev
187 target_id=${target_dev##*/target}
190 for tid in ${target_id}* ; do
191 target_num=$(( $target_num + 1 ))
194 controller_port=${target_dev%/*}
195 controller_dev="${controller_port%/*}"
197 : controller_dev $controller_dev
198 : controller_port $controller_port
199 # a host controller may have more than one interface/port
200 controller_port="${controller_port##*/host}"
203 controller_offset=$(get_port_offset host $controller_port)
205 controller_port=$(( $controller_port - $controller_offset))
206 scsi_id="scsi-${controller_port}:${cil}"
218 handle_block_firewire () {
219 :handle_block_firewire $*
222 if [ -f "$D/ieee1394_id" ] ; then
223 read ieee1394_id < $D/ieee1394_id
226 if [ -z "$ieee1394_id" ] ; then
232 fw_host_dev=${DEV%/fw-host*}
233 # IEEE1394 devices are always endpoints
234 d="ieee1394-0x$ieee1394_id"
243 local cil controller_port controller_dev
245 fc_tgt_hcil="${DEV##*/}"
246 fc_tgt_lun="${fc_tgt_hcil##*:}"
247 fc_tgt_path="${DEV%/*}"
248 fc_tgt_num="${fc_tgt_path##*/}"
249 fc_tgt_dev="${fc_tgt_path}/fc_transport:${fc_tgt_num}"
250 if [ -e "$fc_tgt_dev/port_name" ]; then
251 read wwpn < $fc_tgt_dev/port_name
253 if [ -z "$wwpn" ] ; then
258 # Linux currently knows about 32bit luns
259 tmp_lun3=$(printf "%04x" $(($fc_tgt_lun & 0xFFFF)))
260 tmp_lun2=$(printf "%04x" $(( ($fc_tgt_lun >> 16) & 0xFFFF)))
264 if (($fc_tgt_lun == 0)) ; then
265 lun="0x0000000000000000"
267 lun="0x${tmp_lun3}${tmp_lun2}${tmp_lun1}${tmp_lun0}"
269 controller_dev="${fc_tgt_path%/host[0-9]*}"
271 # FC devices are always endpoints
272 d="fc-${wwpn}:${lun}"
277 handle_block_sas () {
278 : handle_block_sas $*
280 local cil adapter controller_dev
282 sas_host_path="${DEV%%/phy*}"
283 sas_phy_path="${DEV#*/host*/}"
284 sas_phy_path="${sas_phy_path%%/target*}"
285 sas_phy_id="${sas_phy_path%%/*}"
286 sas_rphy_id="${sas_phy_path##*/}"
287 sas_phy_dev="${sas_host_path}/${sas_phy_id}/sas_phy:${sas_phy_id}"
288 if [ -e "$sas_phy_dev/sas_address" ]; then
289 read phy_address < $sas_phy_dev/sas_address
290 read phy_port < $sas_phy_dev/port_identifier
291 read phy_id < $sas_phy_dev/phy_identifier
293 if [ -z "$phy_address" ] ; then
294 : no initiator address
298 sas_phy_address="$phy_address:$phy_port:$phy_id"
299 sas_rphy_dev="${sas_host_path}/${sas_phy_id}/${sas_rphy_id}/sas_rphy:${sas_rphy_id}"
300 if [ -e "$sas_rphy_dev/sas_address" ]; then
301 read rphy_address < $sas_rphy_dev/sas_address
302 read rphy_id < $sas_rphy_dev/phy_identifier
304 if [ -z "$rphy_address" ] ; then
305 : no initiator address
309 sas_rphy_address="$rphy_address:$rphy_id"
311 controller_dev="${sas_host_path%/host[0-9]*}"
313 # SAS devices are always endpoints
314 d="sas-${sas_phy_address}-${sas_rphy_address}"
326 port_num=${port_id#*-}
328 while [ ! -z "$host_dev_path" ] ; do
329 case "$host_dev_path" in
331 usb_host_path=$host_dev_path
332 host_dev_path="${host_dev_path%/*}"
337 : host_dev_path $host_dev_path
338 usb_host_num=${usb_host_path##*/usb}
341 usb_host_offset=$(get_port_offset usb $usb_host_num)
342 usb_host_port=$(($usb_host_num - $usb_host_offset))
346 d="usb-$usb_host_port:$port_num-${d}"
348 d="usb-$usb_host_port:$port_num"
356 full_sysfs_path="$SYSFS$DEVPATH"
357 if [ -L $full_sysfs_path/subsystem ]; then
358 # new sysfs block layout
359 full_sysfs_path="${full_sysfs_path%/*}"
360 cd "$full_sysfs_path/subsystem";
363 subsys="${subsys##*/}"
364 if [ "$subsys" == "block" ]; then
365 # parent is "block", it's a partition, move one up
366 full_sysfs_path="${full_sysfs_path%/*}"
370 # old sysfs block layout
371 if [ ! -L $full_sysfs_path/device ] ; then
372 if [ -f $full_sysfs_path/range ] ; then return ; fi
373 full_sysfs_path="${full_sysfs_path%/*}"
374 : full_sysfs_path "$full_sysfs_path"
375 if [ ! -L $full_sysfs_path/device -o ! -f $full_sysfs_path/dev ] ; then
379 cd $full_sysfs_path/device
381 full_sysfs_device_path="`pwd -P`"
383 D=$full_sysfs_device_path
384 while [ ! -z "$D" ] ; do
386 */ide[0-9]/[0-9].[0-9]*|*/ide[0-9][0-9]/[0-9][0-9].[0-9]*)
387 handle_block_ide "$D"
390 if [ -r $full_sysfs_device_path/wwpn ]; then
391 read wwpn < $full_sysfs_device_path/wwpn
393 if [ -r $full_sysfs_device_path/fcp_lun ]; then
394 read lun < $full_sysfs_device_path/fcp_lun
396 if [ -r $full_sysfs_device_path/hba_id ]; then
397 read bus_id < $full_sysfs_device_path/hba_id
399 if [ "$bus_id" -a "$wwpn" -a "$lun" ]; then
401 d="ccw-$bus_id-zfcp-$wwpn:$lun"
412 */rport-[0-9]*:[0-9]*-[0-9]*/*)
415 */phy-[0-9]*:[0-9]*/*)
416 handle_block_sas "$D"
419 handle_block_firewire "$D"
421 */host[0-9]*/[0-9]*:[0-9]*:[0-9]*:[0-9]*)
422 handle_block_scsi "$D"
424 */usb[0-9]*/[0-9]*/*)
441 if [ "$TYPE" == "scsi_tape" ] ; then
442 devname=${full_sysfs_path##*/}
443 rewind="${devname%%st*}"
444 mode="${devname##*st}"
460 d="$d-${rewind}st${mode}"
475 handle_usb $SYSFS$DEVPATH/device