summaryrefslogtreecommitdiff
path: root/extras/path_id/path_id
blob: a7d650bf423b62b46cfd188d3b31c9814d370c53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
#!/bin/sh

# provide the shortest possible unique hardware path to a device
# for the Linux Persistent Device Naming scheme
#
# Copyright (C) 2005-2006 SUSE Linux Products GmbH
# Author:
#	Hannes Reinecke <hare@suse.de>
#
#	This program is free software; you can redistribute it and/or modify it
#	under the terms of the GNU General Public License as published by the
#	Free Software Foundation version 2 of the License.

SYSFS=/sys
RESULT=1
TYPE=
OPWD="`pwd`"
full_sysfs_path=
full_sysfs_device_path=

if [ -z "$DEVPATH" -a -z "$1" ] ; then
	exit 1
fi

if [ -z "$DEVPATH" ] ; then
	case "$1" in
		$SYSFS/*)
			DEVPATH="${1#$SYSFS}"
			;;
		*)
			DEVPATH=$1
			;;
	esac
fi

if [ ! -e $SYSFS$DEVPATH/dev ] ; then
	exit 1
fi

case "$DEVPATH" in
	/devices/*)
		cd "$SYSFS$DEVPATH/subsystem";
		TYPE="`pwd -P`"
		cd "$OPWD"
		TYPE="${TYPE##*/}"
		;;
	/class/*)
		TYPE="${DEVPATH#/class/}"
		TYPE="${TYPE%%/*}"
		;;
	/block/*)
		TYPE=block
		;;
	*)
		exit 1
		;;
esac

get_port_offset () {
	local type offset port
	type=$1
	offset=$2
	for i in $type[0-9]* ; do
		: i $i
		port="${i#$type}"
		if [ "$port" -lt "$offset" ] ; then offset=$port ; fi
	done
	echo $offset
}

handle_pci () {
	local DEV=$1
	cd -P $1
	DEV=${PWD}
	pci_id=${DEV##*/}
	host_dev_path=$DEV
	while [ ! -z "$host_dev_path" ] ; do
		case "$host_dev_path" in
			*/pci[0-9]*)
				host_dev_path=${host_dev_path%/*}
				;;
			*)
				break
				;;
		esac
	done
	d="pci-$pci_id-$d"
	D="$host_dev_path"
	RESULT=0
}

handle_platform () {
	local DEV=$1
	cd -P $1
	DEV=${PWD}
	platform_id=${DEV##*/}
	host_dev_path=$DEV
	while [ ! -z "$host_dev_path" ] ; do
		case "$host_dev_path" in
			*/platform*)
				host_dev_path=${host_dev_path%/*}
				;;
			*)
				break
				;;
		esac
	done
	if [ "$d" ]; then
		d="platform-$platform_id-$d"
	else
		d="platform-$platform_id"
	fi
	D="$host_dev_path"
	RESULT=0
}

handle_serio () {
	local DEV=$1
	cd -P $1
	DEV=${PWD}
	serio_id=${DEV##*/serio}
	host_dev_path=$DEV
	while [ ! -z "$host_dev_path" ] ; do
		case "$host_dev_path" in
			*/serio*)
				host_dev_path=${host_dev_path%/*}
				;;
			*)
				break
				;;
		esac
	done
	if [ "$d" ]; then
		d="serio-$serio_id-$d"
	else
		d="serio-$serio_id"
	fi
	D="$host_dev_path"
	RESULT=0
}

handle_ide () {
	: handle_ide $*
	local DEV=$1
	local port idedev idecontroller
	# IDE
	: DEV $DEV
	port=${DEV##*/}
	idedev=${DEV%/*}
	idecontroller=${idedev%/*}
	# port info if the controller has more than one interface
	port="${port#ide}"
	: port $port d $d
	: idedev $idedev kernel_port $port
	case "${port#*.}" in
		0)
			channel=0
			;;
		1)
			channel=1
			;;
		*)
			echo "Error: $idedev is neither master or slave" >&2
			;;
	esac
	cd $idecontroller
	offset="`get_port_offset ide ${port%.*}`"
	cd "$OPWD"
	:  port offset $offset
	port=$((${port%.*} - $offset))
	if [ "$d" ] ; then
		d="ide-${port}:$channel-$d"
	else
		d="ide-${port}:$channel"
	fi
	D=$idecontroller
	RESULT=0
}

handle_scsi () {
	: handle_scsi $*
	local DEV=$1
	local cil controller_port controller_dev
	# SCSI device
	cil="${DEV##*/}"
	cil="${cil#*:}"
	target_dev=${DEV%/*}
	target_id=${target_dev##*/target}
	cd "$target_dev"
	target_num=0
	for tid in ${target_id}* ; do
		target_num=$(( $target_num + 1 ))
	done
	controller_port=${target_dev%/*}
	controller_dev="${controller_port%/*}"
	: controller_dev $controller_dev
	: controller_port $controller_port
	# a host controller may have more than one interface/port
	controller_port="${controller_port##*/host}"
	#
	cd "$controller_dev"
	controller_offset=$(get_port_offset host $controller_port)
	cd "$OPWD"
	controller_port=$(( $controller_port - $controller_offset))
	scsi_id="scsi-${controller_port}:${cil}"
	if [ "$d" ] ; then
		d="${scsi_id}-$d"
	else
		d="$scsi_id"
	fi
	D="$controller_dev"
	RESULT=0
}

handle_firewire () {
	: handle_firewire $*
	local DEV=$1
	if [ -f "$D/ieee1394_id" ] ; then
		read ieee1394_id < $D/ieee1394_id
	fi
	if [ -z "$ieee1394_id" ] ; then
		: no IEEE1394 ID
		RESULT=1
		return
	fi
	fw_host_dev=${DEV%/fw-host*}
	# IEEE1394 devices are always endpoints
	d="ieee1394-0x$ieee1394_id"
	D="$fw_host_dev"
	RESULT=0
}

handle_fc () {
	: handle_fc $*
	local DEV=$1
	local cil controller_port controller_dev
	# SCSI-FC device
	fc_tgt_hcil="${DEV##*/}"
	fc_tgt_lun="${fc_tgt_hcil##*:}"
	fc_tgt_path="${DEV%/*}"
	fc_tgt_num="${fc_tgt_path##*/}"
	fc_tgt_dev="${fc_tgt_path}/fc_transport:${fc_tgt_num}"
	if [ -e "$fc_tgt_dev/port_name" ]; then
		read wwpn < $fc_tgt_dev/port_name
	fi
	if [ -z "$wwpn" ] ; then
		: no WWPN
		D=
		RESULT=1
		return
	fi
	# Linux currently knows about 32bit luns
	tmp_lun3=$(printf "%04x" $(($fc_tgt_lun & 0xFFFF)))
	tmp_lun2=$(printf "%04x" $(( ($fc_tgt_lun >> 16) & 0xFFFF)))
	tmp_lun1="0000"
	tmp_lun0="0000"
	if (($fc_tgt_lun == 0)) ; then
		lun="0x0000000000000000"
	else
		lun="0x${tmp_lun3}${tmp_lun2}${tmp_lun1}${tmp_lun0}"
	fi
	controller_dev="${fc_tgt_path%/host[0-9]*}"
	# FC devices are always endpoints
	d="fc-${wwpn}:${lun}"
	D="$controller_dev"
	RESULT=0
}

handle_sas () {
	: handle_sas $*
	local DEV=$1
	local cil adapter controller_dev
	# SAS device
	sas_host_path="${DEV%%/port*}"
	sas_phy_path="${DEV#*/host*/}"
	sas_phy_path="${sas_phy_path%%/target*}"
	sas_phy_id="${sas_phy_path%%/*}"
	sas_phy_id="${sas_phy_id##*port-}"
	sas_port_id="${sas_phy_path%%/end_device*}"
	sas_port_id="${sas_port_id##*port-}"
	sas_end_id="${sas_phy_path##*end_device-}"
	sas_phy_dev="/sys/class/sas_phy/phy-${sas_phy_id}"
	if [ -e "$sas_phy_dev/sas_address" ]; then
		read phy_address < $sas_phy_dev/sas_address
		read phy_id < $sas_phy_dev/phy_identifier
	fi
	if [ -z "$phy_address" ] ; then
		: no initiator address
		D=
		RESULT=1
		return
	fi
	sas_port_dev="/sys/class/sas_port/port-${sas_port_id}"
	if [ -e "$sas_port_dev/num_phys" ] ; then
		read phy_port < $sas_port_dev/num_phys
	fi
	if [ -z "$phy_port" ] ; then
		: no initiator address
		D=
		RESULT=1
		return
	fi
	sas_phy_address="$phy_address:$phy_port:$phy_id"
	sas_end_dev="/sys/class/sas_device/end_device-${sas_end_id}"
	if [ -e "$sas_end_dev/sas_address" ]; then
		read end_address < $sas_end_dev/sas_address
		read end_id < $sas_end_dev/phy_identifier
	fi
	if [ -z "$end_address" ] ; then
		: no initiator address
		D=
		RESULT=1
		return
	fi
	sas_end_address="$end_address:$end_id"
	controller_dev="${sas_host_path%/host[0-9]*}"
	# SAS devices are always endpoints
	d="sas-${sas_phy_address}-${sas_end_address}"
	D="$controller_dev"
	RESULT=0
}

handle_iscsi() {
	local DEV=$1
	local iscsi_session_dir
	local iscsi_session iscsi_session_path
	local iscsi_connection iscsi_connection_path
	local iscsi_scsi_lun
	# iSCSI device
	iscsi_session_dir="${DEV%%/target*}"
	iscsi_session="${iscsi_session_dir##*/}"
	iscsi_session_path=/sys/class/iscsi_session/${iscsi_session}
	if [ ! -d "$iscsi_session_path" ] ; then
	    : no iSCSI session path
	    RESULT=1
	    return
	fi
	# Currently we're not doing MC/S
	for conn in ${iscsi_session_dir}/connection* ; do
	    iscsi_conn_num=${conn##*:}
	    if [ "$iscsi_conn_num" = '0' ] ; then
		iscsi_connection=$(basename $conn)
	    fi
	done
	if [ -z "$iscsi_connection" ] ; then
	    : no iSCSI connection found
	    RESULT=1
	    return
	fi
	iscsi_connection_path=/sys/class/iscsi_connection/${iscsi_connection}
	if [ ! -d "$iscsi_connection_path" ] ; then
	    : no iSCSI connection path
	    RESULT=1
	    return
	fi
	if [ -e "${iscsi_session_path}/targetname" ]; then
	    read iscsi_tgtname < ${iscsi_session_path}/targetname
	fi
	if [ -z "$iscsi_tgtname" ] ; then
	    : No iSCSI Targetname
	    RESULT=1
	    return
	fi
	if [ -e "${iscsi_connection_path}/persistent_address" ] ; then
	    read iscsi_address < ${iscsi_connection_path}/persistent_address
	fi
	if [ -z "$iscsi_address" ] ; then
	    : No iSCSI Target address
	    RESULT=1
	    return
	fi
	if [ -e "${iscsi_connection_path}/persistent_port" ] ; then
	    read iscsi_port < ${iscsi_connection_path}/persistent_port
	fi
	iscsi_scsi_lun="${DEV##*:}"
	d="ip-${iscsi_address}:${iscsi_port}-iscsi-${iscsi_tgtname}-lun-${iscsi_scsi_lun}"
	RESULT=0
}

handle_usb () {
: handle_usb $*
	local DEV=$1
	cd -P $1
	DEV=${PWD}
	port_id=${DEV##*/}
	port_num=${port_id#*-}
	host_dev_path=$DEV
	while [ ! -z "$host_dev_path" ] ; do
		case "$host_dev_path" in
			*/usb*)
				usb_host_path=$host_dev_path
				host_dev_path="${host_dev_path%/*}"
				;;
			*)
				break
				;;
		esac
	done
	: host_dev_path $host_dev_path
	usb_host_num=${usb_host_path##*/usb}

	cd "$host_dev_path"
	usb_host_offset=$(get_port_offset usb $usb_host_num)
	usb_host_port=$(($usb_host_num - $usb_host_offset))
	cd "$OPWD"
	if [ "$d" ] ; then
		d="usb-$usb_host_port:$port_num-${d}"
	else
		d="usb-$usb_host_port:$port_num"
	fi
	D="$host_dev_path"
	RESULT=0
}

handle_device () {
	full_sysfs_path="$SYSFS$DEVPATH"
	case "$DEVPATH" in
		/devices/*)
			# new sysfs layout
			if [ -L $full_sysfs_path/subsystem ]; then
				full_sysfs_path="${full_sysfs_path%/*}"
				cd "$full_sysfs_path/subsystem";
				subsys="`pwd -P`"
				cd "$OPWD"
				subsys="${subsys##*/}"
				if [ "$subsys" = "block" ]; then
					# parent is "block", it's a partition, move one up
				full_sysfs_path="${full_sysfs_path%/*}"
				fi
				cd $full_sysfs_path
			fi
			;;
		*)
			# old sysfs layout
			if [ ! -L $full_sysfs_path/device ] ; then
				if [ -f $full_sysfs_path/range ] ; then return ; fi
				full_sysfs_path="${full_sysfs_path%/*}"
				: full_sysfs_path "$full_sysfs_path"
				if [ ! -L $full_sysfs_path/device -o ! -f $full_sysfs_path/dev ] ; then
					return
				fi
			fi
			cd $full_sysfs_path/device
			;;
	esac
	full_sysfs_device_path="`pwd -P`"
	cd "$OPWD"
	D=$full_sysfs_device_path
	while [ ! -z "$D" ] ; do
		case "$D" in
			*/ide[0-9]/[0-9].[0-9]*|*/ide[0-9][0-9]/[0-9][0-9].[0-9]*)
				handle_ide "$D"
				;;
			*/css0/*)
				if [ -r $full_sysfs_device_path/wwpn ]; then
					read wwpn < $full_sysfs_device_path/wwpn
				fi
				if [ -r $full_sysfs_device_path/fcp_lun ]; then
					read lun < $full_sysfs_device_path/fcp_lun
				fi
				if [ -r $full_sysfs_device_path/hba_id ]; then
					read bus_id < $full_sysfs_device_path/hba_id
				fi
				if [ "$bus_id" -a "$wwpn" -a "$lun" ]; then
					# S/390 zfcp adapter
					d="ccw-$bus_id-zfcp-$wwpn:$lun"
					RESULT=0
				else
					# DASD devices
					bus="ccw"
					adapter=${D##*/}
					d="$bus-$adapter"
					RESULT=0
				fi
				D=
				;;
			*/rport-[0-9]*:[0-9]*-[0-9]*/*)
				handle_fc "$D"
				;;
			*/end_device-[0-9]*:[0-9]*:[0-9]*/*)
				handle_sas "$D"
				;;
			*/fw-host[0-9]*/*)
				handle_firewire "$D"
				;;
			*/session[0-9]*/*)
				handle_iscsi "$D"
				D=
				;;
			*/host[0-9]*/[0-9]*:[0-9]*:[0-9]*:[0-9]*)
				handle_scsi "$D"
				;;
			*/usb[0-9]*/[0-9]*/*)
				handle_usb "$D"
				;;
			*/pci[0-9]*:[0-9]*)
				handle_pci "$D"
				;;
			*/serio[0-9]*)
				handle_serio "$D"
				;;
			*/platform/*)
				handle_platform "$D"
				;;
			*/devices)
				D=
				;;
			*)
				: not handled
				RESULT=1
				return
				;;
		esac
	done
	if [ "$TYPE" = "scsi_tape" ] ; then
		devname=${full_sysfs_path##*/}
		rewind="${devname%%st*}"
		mode="${devname##*st}"
		case "$mode" in
			*l)
				mode="l"
				;;
			*m)
				mode="m"
				;;
			*a)
				mode="a"
				;;
			*)
				mode=""
			   ;;
		esac
		if [ "$d" ]; then
			d="$d-${rewind}st${mode}"
		fi
	fi
}

case "$TYPE" in
	block)
		handle_device
		echo "ID_PATH=$d"
		;;
	scsi_tape)
		handle_device
		echo "ID_PATH=$d"
		;;
	input)
		handle_device
		echo "ID_PATH=$d"
		;;
	*)
		RESULT=1
		;;
esac

exit $RESULT