chiark / gitweb /
update Debian rules
[elogind.git] / extras / path_id / path_id
1 #!/bin/sh
2
3 # provide the shortest possible unique hardware path to a device
4 # for the Linux Persistent Device Naming scheme
5 #
6 # Copyright (C) 2005-2006 SUSE Linux Products GmbH
7 # Author:
8 #       Hannes Reinecke <hare@suse.de>
9 #
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.
13 #
14 # to be called from a udev rule to return the name for a symlink
15 #       DEVPATH=<devpath>; path_id
16 #       path_id <devpath>
17
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
20
21 SYSFS=/sys
22 RESULT=1
23 TYPE=
24 OPWD="`pwd`"
25 full_sysfs_path=
26 full_sysfs_device_path=
27
28 if [ -z "$DEVPATH" -a -z "$1" ] ; then
29         exit 1
30 fi
31
32 if [ -z "$DEVPATH" ] ; then
33         case "$1" in
34                 $SYSFS/*)
35                         DEVPATH="${1#$SYSFS}"
36                         ;;
37                 *)
38                         DEVPATH=$1
39                         ;;
40         esac
41 fi
42
43 if [ ! -e $SYSFS$DEVPATH/dev ] ; then
44         exit 1
45 fi
46
47 case "$DEVPATH" in
48         /devices/*)
49                 cd "$SYSFS$DEVPATH/subsystem";
50                 TYPE="`pwd -P`"
51                 cd "$OPWD"
52                 TYPE="${TYPE##*/}"
53                 ;;
54         /class/*)
55                 TYPE="${DEVPATH#/class/}"
56                 TYPE="${TYPE%%/*}"
57                 ;;
58         /block/*)
59                 TYPE=block
60                 ;;
61         *)
62                 exit 1
63                 ;;
64 esac
65
66 get_port_offset () {
67         local type offset port
68         type=$1
69         offset=$2
70         for i in $type[0-9]* ; do
71                 : i $i
72                 port="${i#$type}"
73                 if [ "$port" -lt "$offset" ] ; then offset=$port ; fi
74         done
75         echo $offset
76 }
77
78 handle_pci () {
79         local DEV=$1
80         cd -P $1
81         DEV=${PWD}
82         pci_id=${DEV##*/}
83         host_dev_path=$DEV
84         while [ ! -z "$host_dev_path" ] ; do
85                 case "$host_dev_path" in
86                         */pci[0-9]*)
87                                 host_dev_path=${host_dev_path%/*}
88                                 ;;
89                         *)
90                                 break
91                                 ;;
92                 esac
93         done
94         d="pci-$pci_id-$d"
95         D="$host_dev_path"
96         RESULT=0
97 }
98
99 handle_ide () {
100         : handle_ide $*
101         local DEV=$1
102         local port idedev idecontroller
103         # IDE
104         : DEV $DEV
105         port=${DEV##*/}
106         idedev=${DEV%/*}
107         idecontroller=${idedev%/*}
108         # port info if the controller has more than one interface
109         port="${port#ide}"
110         : port $port d $d
111         : idedev $idedev kernel_port $port
112         case "${port#*.}" in
113                 0)
114                         channel=0
115                         ;;
116                 1)
117                         channel=1
118                         ;;
119                 *)
120                         echo "Error: $idedev is neither master or slave" >&2
121                         ;;
122         esac
123         cd $idecontroller
124         offset="`get_port_offset ide ${port%.*}`"
125         cd "$OPWD"
126         :  port offset $offset
127         port=$((${port%.*} - $offset))
128         if [ "$d" ] ; then
129                 d="ide-${port}:$channel-$d"
130         else
131                 d="ide-${port}:$channel"
132         fi
133         D=$idecontroller
134         RESULT=0
135 }
136
137 handle_scsi () {
138         : handle_scsi $*
139         local DEV=$1
140         local cil controller_port controller_dev
141         # SCSI device
142         cil="${DEV##*/}"
143         cil="${cil#*:}"
144         target_dev=${DEV%/*}
145         target_id=${target_dev##*/target}
146         cd "$target_dev"
147         target_num=0
148         for tid in ${target_id}* ; do
149                 target_num=$(( $target_num + 1 ))
150         done
151         controller_port=${target_dev%/*}
152         controller_dev="${controller_port%/*}"
153         : controller_dev $controller_dev
154         : controller_port $controller_port
155         # a host controller may have more than one interface/port
156         controller_port="${controller_port##*/host}"
157         #
158         cd "$controller_dev"
159         controller_offset=$(get_port_offset host $controller_port)
160         cd "$OPWD"
161         controller_port=$(( $controller_port - $controller_offset))
162         scsi_id="scsi-${controller_port}:${cil}"
163         if [ "$d" ] ; then
164                 d="${scsi_id}-$d"
165         else
166                 d="$scsi_id"
167         fi
168         D="$controller_dev"
169         RESULT=0
170 }
171
172 handle_firewire () {
173         :handle_firewire $*
174         local DEV=$1
175         if [ -f "$D/ieee1394_id" ] ; then
176                 read ieee1394_id < $D/ieee1394_id
177         fi
178         if [ -z "$ieee1394_id" ] ; then
179                 : no IEEE1394 ID
180                 RESULT=1
181                 return
182         fi
183         fw_host_dev=${DEV%/fw-host*}
184         # IEEE1394 devices are always endpoints
185         d="ieee1394-0x$ieee1394_id"
186         D="$fw_host_dev"
187         RESULT=0
188 }
189
190 handle_fc () {
191         : handle_fc $*
192         local DEV=$1
193         local cil controller_port controller_dev
194         # SCSI-FC device
195         fc_tgt_hcil="${DEV##*/}"
196         fc_tgt_lun="${fc_tgt_hcil##*:}"
197         fc_tgt_path="${DEV%/*}"
198         fc_tgt_num="${fc_tgt_path##*/}"
199         fc_tgt_dev="${fc_tgt_path}/fc_transport:${fc_tgt_num}"
200         if [ -e "$fc_tgt_dev/port_name" ]; then
201                 read wwpn < $fc_tgt_dev/port_name
202         fi
203         if [ -z "$wwpn" ] ; then
204                 : no WWPN
205                 RESULT=1
206                 return
207         fi
208         # Linux currently knows about 32bit luns
209         tmp_lun3=$(printf "%04x" $(($fc_tgt_lun & 0xFFFF)))
210         tmp_lun2=$(printf "%04x" $(( ($fc_tgt_lun >> 16) & 0xFFFF)))
211         tmp_lun1="0000"
212         tmp_lun0="0000"
213         if (($fc_tgt_lun == 0)) ; then
214                 lun="0x0000000000000000"
215         else
216                 lun="0x${tmp_lun3}${tmp_lun2}${tmp_lun1}${tmp_lun0}"
217         fi
218         controller_dev="${fc_tgt_path%/host[0-9]*}"
219         # FC devices are always endpoints
220         d="fc-${wwpn}:${lun}"
221         D="$controller_dev"
222         RESULT=0
223 }
224
225 handle_sas () {
226         : handle_sas $*
227         local DEV=$1
228         local cil adapter controller_dev
229         # SAS device
230         sas_host_path="${DEV%%/phy*}"
231         sas_phy_path="${DEV#*/host*/}"
232         sas_phy_path="${sas_phy_path%%/target*}"
233         sas_phy_id="${sas_phy_path%%/*}"
234         sas_rphy_id="${sas_phy_path##*/}"
235         sas_phy_dev="${sas_host_path}/${sas_phy_id}/sas_phy:${sas_phy_id}"
236         if [ -e "$sas_phy_dev/sas_address" ]; then
237                 read phy_address < $sas_phy_dev/sas_address
238                 read phy_port < $sas_phy_dev/port_identifier
239                 read phy_id < $sas_phy_dev/phy_identifier
240         fi
241         if [ -z "$phy_address" ] ; then
242                 : no initiator address
243                 RESULT=1
244                 return
245         fi
246         sas_phy_address="$phy_address:$phy_port:$phy_id"
247         sas_rphy_dev="${sas_host_path}/${sas_phy_id}/${sas_rphy_id}/sas_rphy:${sas_rphy_id}"
248         if [ -e "$sas_rphy_dev/sas_address" ]; then
249                 read rphy_address < $sas_rphy_dev/sas_address
250                 read rphy_id < $sas_rphy_dev/phy_identifier
251         fi
252         if [ -z "$rphy_address" ] ; then
253                 : no initiator address
254                 RESULT=1
255                 return
256         fi
257         sas_rphy_address="$rphy_address:$rphy_id"
258         controller_dev="${sas_host_path%/host[0-9]*}"
259         # SAS devices are always endpoints
260         d="sas-${sas_phy_address}-${sas_rphy_address}"
261         D="$controller_dev"
262         RESULT=0
263 }
264
265 handle_usb () {
266 : handle_usb $*
267         local DEV=$1
268         cd -P $1
269         DEV=${PWD}
270         port_id=${DEV##*/}
271         port_num=${port_id#*-}
272         host_dev_path=$DEV
273         while [ ! -z "$host_dev_path" ] ; do
274                 case "$host_dev_path" in
275                         */usb*)
276                                 usb_host_path=$host_dev_path
277                                 host_dev_path="${host_dev_path%/*}"
278                                 ;;
279                         *)
280                                 break
281                                 ;;
282                 esac
283         done
284         : host_dev_path $host_dev_path
285         usb_host_num=${usb_host_path##*/usb}
286
287         cd "$host_dev_path"
288         usb_host_offset=$(get_port_offset usb $usb_host_num)
289         usb_host_port=$(($usb_host_num - $usb_host_offset))
290         cd "$OPWD"
291         if [ "$d" ] ; then
292                 d="usb-$usb_host_port:$port_num-${d}"
293         else
294                 d="usb-$usb_host_port:$port_num"
295         fi
296         D="$host_dev_path"
297         RESULT=0
298 }
299
300 handle_device () {
301         full_sysfs_path="$SYSFS$DEVPATH"
302         if [ -L $full_sysfs_path/subsystem ]; then
303                 # new sysfs block layout
304                 full_sysfs_path="${full_sysfs_path%/*}"
305                 cd "$full_sysfs_path/subsystem";
306                 subsys="`pwd -P`"
307                 cd "$OPWD"
308                 subsys="${subsys##*/}"
309                 if [ "$subsys" == "block" ]; then
310                         # parent is "block", it's a partition, move one up
311                         full_sysfs_path="${full_sysfs_path%/*}"
312                 fi
313                 cd $full_sysfs_path
314         else
315                 # old sysfs block layout
316                 if [ ! -L $full_sysfs_path/device ] ; then
317                         if [ -f $full_sysfs_path/range ] ; then return ; fi
318                         full_sysfs_path="${full_sysfs_path%/*}"
319                         : full_sysfs_path "$full_sysfs_path"
320                         if [ ! -L $full_sysfs_path/device -o ! -f $full_sysfs_path/dev ] ; then
321                                 return
322                         fi
323                 fi
324                 cd $full_sysfs_path/device
325         fi
326         full_sysfs_device_path="`pwd -P`"
327         cd "$OPWD"
328         D=$full_sysfs_device_path
329         while [ ! -z "$D" ] ; do
330                 case "$D" in
331                         */ide[0-9]/[0-9].[0-9]*|*/ide[0-9][0-9]/[0-9][0-9].[0-9]*)
332                                 handle_ide "$D"
333                                 ;;
334                         */css0/*)
335                                 if [ -r $full_sysfs_device_path/wwpn ]; then
336                                         read wwpn < $full_sysfs_device_path/wwpn
337                                 fi
338                                 if [ -r $full_sysfs_device_path/fcp_lun ]; then
339                                         read lun < $full_sysfs_device_path/fcp_lun
340                                 fi
341                                 if [ -r $full_sysfs_device_path/hba_id ]; then
342                                         read bus_id < $full_sysfs_device_path/hba_id
343                                 fi
344                                 if [ "$bus_id" -a "$wwpn" -a "$lun" ]; then
345                                         # S/390 zfcp adapter
346                                         d="ccw-$bus_id-zfcp-$wwpn:$lun"
347                                         RESULT=0
348                                 else
349                                         # DASD devices
350                                         bus="ccw"
351                                         adapter=${D##*/}
352                                         d="$bus-$adapter"
353                                         RESULT=0
354                                 fi
355                                 D=
356                                 ;;
357                         */rport-[0-9]*:[0-9]*-[0-9]*/*)
358                                 handle_fc "$D"
359                                 ;;
360                         */phy-[0-9]*:[0-9]*/*)
361                                 handle_sas "$D"
362                                 ;;
363                         */fw-host[0-9]*/*)
364                                 handle_firewire "$D"
365                                 ;;
366                         */host[0-9]*/[0-9]*:[0-9]*:[0-9]*:[0-9]*)
367                                 handle_scsi "$D"
368                                 ;;
369                         */usb[0-9]*/[0-9]*/*)
370                                 handle_usb "$D"
371                                 ;;
372                         */pci[0-9]*:[0-9]*)
373                                 handle_pci "$D"
374                                 ;;
375                         */devices)
376                                 D=
377                                 ;;
378                         *)
379                                 : not handled
380                                 RESULT=1
381                                 return
382                                 ;;
383                 esac
384         done
385         if [ "$TYPE" == "scsi_tape" ] ; then
386                 devname=${full_sysfs_path##*/}
387                 rewind="${devname%%st*}"
388                 mode="${devname##*st}"
389                 case "$mode" in
390                         *l)
391                                 mode="l"
392                                 ;;
393                         *m)
394                                 mode="m"
395                                 ;;
396                         *a)
397                                 mode="a"
398                                 ;;
399                         *)
400                                 mode=""
401                            ;;
402                 esac
403                 if [ "$d" ]; then
404                         d="$d-${rewind}st${mode}"
405                 fi
406         fi
407 }
408
409 case "$TYPE" in
410         block)
411                 handle_device
412                 echo "ID_PATH=$d"
413                 ;;
414         scsi_tape)
415                 handle_device
416                 echo "ID_PATH=$d"
417                 ;;
418         input)
419                 handle_device
420                 echo "ID_PATH=$d"
421                 ;;
422         *)
423                 RESULT=1
424                 ;;
425 esac
426
427 exit $RESULT