]> git.proxmox.com Git - mirror_zfs.git/blame - cmd/vdev_id/vdev_id
ZTS events_002: Improve speed and reliability
[mirror_zfs.git] / cmd / vdev_id / vdev_id
CommitLineData
a6ef9522 1#!/bin/sh
821b6834
NB
2#
3# vdev_id: udev helper to generate user-friendly names for JBOD disks
4#
5# This script parses the file /etc/zfs/vdev_id.conf to map a
6# physical path in a storage topology to a channel name. The
7# channel name is combined with a disk enclosure slot number to
8# create an alias that reflects the physical location of the drive.
9# This is particularly helpful when it comes to tasks like replacing
10# failed drives. Slot numbers may also be re-mapped in case the
11# default numbering is unsatisfactory. The drive aliases will be
12# created as symbolic links in /dev/disk/by-vdev.
13#
2957f38d
NB
14# The currently supported topologies are sas_direct and sas_switch.
15# A multipath mode is supported in which dm-mpath devices are
16# handled by examining the first-listed running component disk. In
17# multipath mode the configuration file should contain a channel
18# definition with the same name for each path to a given enclosure.
19#
20# The alias keyword provides a simple way to map already-existing
21# device symlinks to more convenient names. It is suitable for
22# small, static configurations or for sites that have some automated
23# way to generate the mapping file.
24#
821b6834
NB
25#
26# Some example configuration files are given below.
27
28# #
29# # Example vdev_id.conf - sas_direct.
30# #
31#
32# multipath no
33# topology sas_direct
34# phys_per_port 4
bba365cf 35# slot bay
821b6834
NB
36#
37# # PCI_ID HBA PORT CHANNEL NAME
38# channel 85:00.0 1 A
39# channel 85:00.0 0 B
40# channel 86:00.0 1 C
41# channel 86:00.0 0 D
42#
09d0b30f
NB
43# # Custom mapping for Channel A
44#
45# # Linux Mapped
46# # Slot Slot Channel
47# slot 1 7 A
48# slot 2 10 A
49# slot 3 3 A
50# slot 4 6 A
51#
52# # Default mapping for B, C, and D
53# slot 1 4
54# slot 2 2
55# slot 3 1
56# slot 4 3
821b6834
NB
57
58# #
59# # Example vdev_id.conf - sas_switch
60# #
61#
62# topology sas_switch
63#
64# # SWITCH PORT CHANNEL NAME
65# channel 1 A
66# channel 2 B
67# channel 3 C
68# channel 4 D
69
70# #
71# # Example vdev_id.conf - multipath
72# #
73#
74# multipath yes
75#
76# # PCI_ID HBA PORT CHANNEL NAME
77# channel 85:00.0 1 A
78# channel 85:00.0 0 B
79# channel 86:00.0 1 A
80# channel 86:00.0 0 B
81
677cdf78
AH
82# #
83# # Example vdev_id.conf - multipath / multijbod-daisychaining
84# #
85#
86# multipath yes
87# multijbod yes
88#
89# # PCI_ID HBA PORT CHANNEL NAME
90# channel 85:00.0 1 A
91# channel 85:00.0 0 B
92# channel 86:00.0 1 A
93# channel 86:00.0 0 B
94
95# #
96# # Example vdev_id.conf - multipath / mixed
97# #
98#
99# multipath yes
100# slot mix
101#
102# # PCI_ID HBA PORT CHANNEL NAME
103# channel 85:00.0 3 A
104# channel 85:00.0 2 B
105# channel 86:00.0 3 A
106# channel 86:00.0 2 B
107# channel af:00.0 0 C
108# channel af:00.0 1 C
109
2957f38d
NB
110# #
111# # Example vdev_id.conf - alias
112# #
113#
114# # by-vdev
115# # name fully qualified or base name of device link
116# alias d1 /dev/disk/by-id/wwn-0x5000c5002de3b9ca
117# alias d2 wwn-0x5000c5002def789e
118
821b6834
NB
119PATH=/bin:/sbin:/usr/bin:/usr/sbin
120CONFIG=/etc/zfs/vdev_id.conf
121PHYS_PER_PORT=
122DEV=
821b6834 123TOPOLOGY=
bba365cf 124BAY=
677cdf78
AH
125ENCL_ID=""
126UNIQ_ENCL_ID=""
821b6834
NB
127
128usage() {
129 cat << EOF
130Usage: vdev_id [-h]
131 vdev_id <-d device> [-c config_file] [-p phys_per_port]
269db7a4 132 [-g sas_direct|sas_switch|scsi] [-m]
821b6834 133
ad0b23b1 134 -c specify name of an alternative config file [default=$CONFIG]
821b6834 135 -d specify basename of device (i.e. sda)
c66401fa 136 -e Create enclose device symlinks only (/dev/by-enclosure)
821b6834
NB
137 -g Storage network topology [default="$TOPOLOGY"]
138 -m Run in multipath mode
677cdf78 139 -j Run in multijbod mode
821b6834
NB
140 -p number of phy's per switch port [default=$PHYS_PER_PORT]
141 -h show this summary
142EOF
143 exit 0
144}
145
146map_slot() {
64025fa3
BB
147 LINUX_SLOT=$1
148 CHANNEL=$2
821b6834 149
677cdf78
AH
150 MAPPED_SLOT=$(awk '$1 == "slot" && $2 == "${LINUX_SLOT}" && \
151 $4 ~ /^${CHANNEL}$|^$/ { print $3; exit}' $CONFIG)
821b6834
NB
152 if [ -z "$MAPPED_SLOT" ] ; then
153 MAPPED_SLOT=$LINUX_SLOT
154 fi
677cdf78 155 printf "%d" "${MAPPED_SLOT}"
821b6834
NB
156}
157
158map_channel() {
64025fa3
BB
159 MAPPED_CHAN=
160 PCI_ID=$1
161 PORT=$2
821b6834
NB
162
163 case $TOPOLOGY in
164 "sas_switch")
677cdf78
AH
165 MAPPED_CHAN=$(awk -v port="$PORT" \
166 '$1 == "channel" && $2 == ${PORT} \
167 { print $3; exit }' $CONFIG)
821b6834 168 ;;
269db7a4 169 "sas_direct"|"scsi")
677cdf78
AH
170 MAPPED_CHAN=$(awk -v pciID="$PCI_ID" -v port="$PORT" \
171 '$1 == "channel" && $2 == pciID && $3 == port \
172 {print $4}' $CONFIG)
821b6834
NB
173 ;;
174 esac
677cdf78
AH
175 printf "%s" "${MAPPED_CHAN}"
176}
177
178get_encl_id() {
179 set -- $(echo $1)
180 count=$#
181
182 i=1
183 while [ $i -le $count ] ; do
184 d=$(eval echo '$'{$i})
185 id=$(cat "/sys/class/enclosure/${d}/id")
186 ENCL_ID="${ENCL_ID} $id"
187 i=$((i + 1))
188 done
189}
190
191get_uniq_encl_id() {
192 for uuid in ${ENCL_ID}; do
193 found=0
194
195 for count in ${UNIQ_ENCL_ID}; do
196 if [ $count = $uuid ]; then
197 found=1
198 break
199 fi
200 done
201
202 if [ $found -eq 0 ]; then
203 UNIQ_ENCL_ID="${UNIQ_ENCL_ID} $uuid"
204 fi
205 done
206}
207
208# map_jbod explainer: The bsg driver knows the difference between a SAS
209# expander and fanout expander. Use hostX instance along with top-level
210# (whole enclosure) expander instances in /sys/class/enclosure and
211# matching a field in an array of expanders, using the index of the
212# matched array field as the enclosure instance, thereby making jbod IDs
213# dynamic. Avoids reliance on high overhead userspace commands like
214# multipath and lsscsi and instead uses existing sysfs data. $HOSTCHAN
215# variable derived from devpath gymnastics in sas_handler() function.
216map_jbod() {
217 DEVEXP=$(ls -l "/sys/block/$DEV/device/" | grep enclos | awk -F/ '{print $(NF-1) }')
218 DEV=$1
219
220 # Use "set --" to create index values (Arrays)
221 set -- $(ls -l /sys/class/enclosure | grep -v "^total" | awk '{print $9}')
222 # Get count of total elements
223 JBOD_COUNT=$#
224 JBOD_ITEM=$*
225
226 # Build JBODs (enclosure) id from sys/class/enclosure/<dev>/id
227 get_encl_id "$JBOD_ITEM"
228 # Different expander instances for each paths.
229 # Filter out and keep only unique id.
230 get_uniq_encl_id
231
232 # Identify final 'mapped jbod'
233 j=0
234 for count in ${UNIQ_ENCL_ID}; do
235 i=1
236 j=$((j + 1))
237 while [ $i -le $JBOD_COUNT ] ; do
238 d=$(eval echo '$'{$i})
239 id=$(cat "/sys/class/enclosure/${d}/id")
240 if [ "$d" = "$DEVEXP" ] && [ $id = $count ] ; then
241 MAPPED_JBOD=$j
242 break
243 fi
244 i=$((i + 1))
245 done
246 done
247
248 printf "%d" "${MAPPED_JBOD}"
821b6834
NB
249}
250
2957f38d
NB
251sas_handler() {
252 if [ -z "$PHYS_PER_PORT" ] ; then
677cdf78
AH
253 PHYS_PER_PORT=$(awk '$1 == "phys_per_port" \
254 {print $2; exit}' $CONFIG)
2957f38d
NB
255 fi
256 PHYS_PER_PORT=${PHYS_PER_PORT:-4}
677cdf78
AH
257
258 if ! echo "$PHYS_PER_PORT" | grep -q -E '^[0-9]+$' ; then
2957f38d
NB
259 echo "Error: phys_per_port value $PHYS_PER_PORT is non-numeric"
260 exit 1
261 fi
262
263 if [ -z "$MULTIPATH_MODE" ] ; then
677cdf78
AH
264 MULTIPATH_MODE=$(awk '$1 == "multipath" \
265 {print $2; exit}' $CONFIG)
266 fi
267
268 if [ -z "$MULTIJBOD_MODE" ] ; then
269 MULTIJBOD_MODE=$(awk '$1 == "multijbod" \
270 {print $2; exit}' $CONFIG)
2957f38d
NB
271 fi
272
273 # Use first running component device if we're handling a dm-mpath device
274 if [ "$MULTIPATH_MODE" = "yes" ] ; then
275 # If udev didn't tell us the UUID via DM_NAME, check /dev/mapper
276 if [ -z "$DM_NAME" ] ; then
677cdf78
AH
277 DM_NAME=$(ls -l --full-time /dev/mapper |
278 grep "$DEV"$ | awk '{print $9}')
2957f38d
NB
279 fi
280
281 # For raw disks udev exports DEVTYPE=partition when
282 # handling partitions, and the rules can be written to
283 # take advantage of this to append a -part suffix. For
284 # dm devices we get DEVTYPE=disk even for partitions so
285 # we have to append the -part suffix directly in the
286 # helper.
287 if [ "$DEVTYPE" != "partition" ] ; then
3ee4e6d8
TH
288 # Match p[number], remove the 'p' and prepend "-part"
289 PART=$(echo "$DM_NAME" |
290 awk 'match($0,/p[0-9]+$/) {print "-part"substr($0,RSTART+1,RLENGTH-1)}')
2957f38d
NB
291 fi
292
293 # Strip off partition information.
677cdf78 294 DM_NAME=$(echo "$DM_NAME" | sed 's/p[0-9][0-9]*$//')
2957f38d
NB
295 if [ -z "$DM_NAME" ] ; then
296 return
297 fi
298
677cdf78
AH
299 # Utilize DM device name to gather subordinate block devices
300 # using sysfs to avoid userspace utilities
301 DMDEV=$(ls -l --full-time /dev/mapper | grep $DM_NAME |
302 awk '{gsub("../", " "); print $NF}')
303
304 # Use sysfs pointers in /sys/block/dm-X/slaves because using
305 # userspace tools creates lots of overhead and should be avoided
306 # whenever possible. Use awk to isolate lowest instance of
307 # sd device member in dm device group regardless of string
308 # length.
309 DEV=$(ls "/sys/block/$DMDEV/slaves" | awk '
310 { len=sprintf ("%20s",length($0)); gsub(/ /,0,str); a[NR]=len "_" $0; }
311 END {
312 asort(a)
313 print substr(a[1],22)
314 }')
315
2957f38d
NB
316 if [ -z "$DEV" ] ; then
317 return
318 fi
319 fi
320
677cdf78 321 if echo "$DEV" | grep -q ^/devices/ ; then
2957f38d
NB
322 sys_path=$DEV
323 else
677cdf78 324 sys_path=$(udevadm info -q path -p "/sys/block/$DEV" 2>/dev/null)
2957f38d
NB
325 fi
326
327 # Use positional parameters as an ad-hoc array
328 set -- $(echo "$sys_path" | tr / ' ')
329 num_dirs=$#
330 scsi_host_dir="/sys"
331
332 # Get path up to /sys/.../hostX
333 i=1
677cdf78
AH
334
335 while [ $i -le "$num_dirs" ] ; do
336 d=$(eval echo '$'{$i})
2957f38d 337 scsi_host_dir="$scsi_host_dir/$d"
677cdf78
AH
338 echo "$d" | grep -q -E '^host[0-9]+$' && break
339 i=$((i + 1))
2957f38d
NB
340 done
341
677cdf78
AH
342 # Lets grab the SAS host channel number and save it for JBOD sorting later
343 HOSTCHAN=$(echo "$d" | awk -F/ '{ gsub("host","",$NF); print $NF}')
344
345 if [ $i = "$num_dirs" ] ; then
2957f38d
NB
346 return
347 fi
348
677cdf78 349 PCI_ID=$(eval echo '$'{$((i -1))} | awk -F: '{print $2":"$3}')
2957f38d
NB
350
351 # In sas_switch mode, the directory four levels beneath
352 # /sys/.../hostX contains symlinks to phy devices that reveal
353 # the switch port number. In sas_direct mode, the phy links one
354 # directory down reveal the HBA port.
355 port_dir=$scsi_host_dir
677cdf78 356
2957f38d 357 case $TOPOLOGY in
677cdf78
AH
358 "sas_switch") j=$((i + 4)) ;;
359 "sas_direct") j=$((i + 1)) ;;
2957f38d
NB
360 esac
361
677cdf78
AH
362 i=$((i + 1))
363
2957f38d 364 while [ $i -le $j ] ; do
677cdf78
AH
365 port_dir="$port_dir/$(eval echo '$'{$i})"
366 i=$((i + 1))
2957f38d
NB
367 done
368
677cdf78 369 PHY=$(ls -d "$port_dir"/phy* 2>/dev/null | head -1 | awk -F: '{print $NF}')
2957f38d 370 if [ -z "$PHY" ] ; then
d49d9c2b 371 PHY=0
2957f38d 372 fi
677cdf78 373 PORT=$((PHY / PHYS_PER_PORT))
2957f38d
NB
374
375 # Look in /sys/.../sas_device/end_device-X for the bay_identifier
376 # attribute.
377 end_device_dir=$port_dir
677cdf78
AH
378
379 while [ $i -lt "$num_dirs" ] ; do
380 d=$(eval echo '$'{$i})
2957f38d 381 end_device_dir="$end_device_dir/$d"
677cdf78 382 if echo "$d" | grep -q '^end_device' ; then
2957f38d
NB
383 end_device_dir="$end_device_dir/sas_device/$d"
384 break
385 fi
677cdf78 386 i=$((i + 1))
2957f38d
NB
387 done
388
677cdf78
AH
389 # Add 'mix' slot type for environments where dm-multipath devices
390 # include end-devices connected via SAS expanders or direct connection
391 # to SAS HBA. A mixed connectivity environment such as pool devices
392 # contained in a SAS JBOD and spare drives or log devices directly
393 # connected in a server backplane without expanders in the I/O path.
bba365cf 394 SLOT=
677cdf78 395
bba365cf
AB
396 case $BAY in
397 "bay")
677cdf78
AH
398 SLOT=$(cat "$end_device_dir/bay_identifier" 2>/dev/null)
399 ;;
400 "mix")
401 if [ $(cat "$end_device_dir/bay_identifier" 2>/dev/null) ] ; then
402 SLOT=$(cat "$end_device_dir/bay_identifier" 2>/dev/null)
403 else
404 SLOT=$(cat "$end_device_dir/phy_identifier" 2>/dev/null)
405 fi
bba365cf
AB
406 ;;
407 "phy")
677cdf78 408 SLOT=$(cat "$end_device_dir/phy_identifier" 2>/dev/null)
bba365cf 409 ;;
d49d9c2b 410 "port")
677cdf78
AH
411 d=$(eval echo '$'{$i})
412 SLOT=$(echo "$d" | sed -e 's/^.*://')
d49d9c2b 413 ;;
bba365cf 414 "id")
677cdf78
AH
415 i=$((i + 1))
416 d=$(eval echo '$'{$i})
417 SLOT=$(echo "$d" | sed -e 's/^.*://')
bba365cf
AB
418 ;;
419 "lun")
677cdf78
AH
420 i=$((i + 2))
421 d=$(eval echo '$'{$i})
422 SLOT=$(echo "$d" | sed -e 's/^.*://')
bba365cf 423 ;;
993669a7
SG
424 "ses")
425 # look for this SAS path in all SCSI Enclosure Services
426 # (SES) enclosures
677cdf78
AH
427 sas_address=$(cat "$end_device_dir/sas_address" 2>/dev/null)
428 enclosures=$(lsscsi -g | \
429 sed -n -e '/enclosu/s/^.* \([^ ][^ ]*\) *$/\1/p')
993669a7 430 for enclosure in $enclosures; do
677cdf78 431 set -- $(sg_ses -p aes "$enclosure" | \
993669a7
SG
432 awk "/device slot number:/{slot=\$12} \
433 /SAS address: $sas_address/\
434 {print slot}")
435 SLOT=$1
436 if [ -n "$SLOT" ] ; then
437 break
438 fi
439 done
440 ;;
bba365cf 441 esac
2957f38d
NB
442 if [ -z "$SLOT" ] ; then
443 return
444 fi
445
677cdf78
AH
446 if [ "$MULTIJBOD_MODE" = "yes" ] ; then
447 CHAN=$(map_channel "$PCI_ID" "$PORT")
448 SLOT=$(map_slot "$SLOT" "$CHAN")
449 JBOD=$(map_jbod "$DEV")
450
451 if [ -z "$CHAN" ] ; then
452 return
453 fi
454 echo "${CHAN}"-"${JBOD}"-"${SLOT}${PART}"
455 else
456 CHAN=$(map_channel "$PCI_ID" "$PORT")
457 SLOT=$(map_slot "$SLOT" "$CHAN")
458
459 if [ -z "$CHAN" ] ; then
460 return
461 fi
462 echo "${CHAN}${SLOT}${PART}"
2957f38d 463 fi
2957f38d
NB
464}
465
269db7a4
SG
466scsi_handler() {
467 if [ -z "$FIRST_BAY_NUMBER" ] ; then
677cdf78
AH
468 FIRST_BAY_NUMBER=$(awk '$1 == "first_bay_number" \
469 {print $2; exit}' $CONFIG)
269db7a4
SG
470 fi
471 FIRST_BAY_NUMBER=${FIRST_BAY_NUMBER:-0}
472
473 if [ -z "$PHYS_PER_PORT" ] ; then
677cdf78
AH
474 PHYS_PER_PORT=$(awk '$1 == "phys_per_port" \
475 {print $2; exit}' $CONFIG)
269db7a4
SG
476 fi
477 PHYS_PER_PORT=${PHYS_PER_PORT:-4}
677cdf78
AH
478
479 if ! echo "$PHYS_PER_PORT" | grep -q -E '^[0-9]+$' ; then
269db7a4
SG
480 echo "Error: phys_per_port value $PHYS_PER_PORT is non-numeric"
481 exit 1
482 fi
483
484 if [ -z "$MULTIPATH_MODE" ] ; then
677cdf78
AH
485 MULTIPATH_MODE=$(awk '$1 == "multipath" \
486 {print $2; exit}' $CONFIG)
269db7a4
SG
487 fi
488
489 # Use first running component device if we're handling a dm-mpath device
490 if [ "$MULTIPATH_MODE" = "yes" ] ; then
491 # If udev didn't tell us the UUID via DM_NAME, check /dev/mapper
492 if [ -z "$DM_NAME" ] ; then
677cdf78
AH
493 DM_NAME=$(ls -l --full-time /dev/mapper |
494 grep "$DEV"$ | awk '{print $9}')
269db7a4
SG
495 fi
496
497 # For raw disks udev exports DEVTYPE=partition when
498 # handling partitions, and the rules can be written to
499 # take advantage of this to append a -part suffix. For
500 # dm devices we get DEVTYPE=disk even for partitions so
501 # we have to append the -part suffix directly in the
502 # helper.
503 if [ "$DEVTYPE" != "partition" ] ; then
3ee4e6d8
TH
504 # Match p[number], remove the 'p' and prepend "-part"
505 PART=$(echo "$DM_NAME" |
506 awk 'match($0,/p[0-9]+$/) {print "-part"substr($0,RSTART+1,RLENGTH-1)}')
269db7a4
SG
507 fi
508
509 # Strip off partition information.
677cdf78 510 DM_NAME=$(echo "$DM_NAME" | sed 's/p[0-9][0-9]*$//')
269db7a4
SG
511 if [ -z "$DM_NAME" ] ; then
512 return
513 fi
514
515 # Get the raw scsi device name from multipath -ll. Strip off
516 # leading pipe symbols to make field numbering consistent.
677cdf78
AH
517 DEV=$(multipath -ll "$DM_NAME" |
518 awk '/running/{gsub("^[|]"," "); print $3 ; exit}')
269db7a4
SG
519 if [ -z "$DEV" ] ; then
520 return
521 fi
522 fi
523
677cdf78 524 if echo "$DEV" | grep -q ^/devices/ ; then
269db7a4
SG
525 sys_path=$DEV
526 else
677cdf78 527 sys_path=$(udevadm info -q path -p "/sys/block/$DEV" 2>/dev/null)
269db7a4
SG
528 fi
529
530 # expect sys_path like this, for example:
531 # /devices/pci0000:00/0000:00:0b.0/0000:09:00.0/0000:0a:05.0/0000:0c:00.0/host3/target3:1:0/3:1:0:21/block/sdv
532
533 # Use positional parameters as an ad-hoc array
534 set -- $(echo "$sys_path" | tr / ' ')
535 num_dirs=$#
536 scsi_host_dir="/sys"
537
538 # Get path up to /sys/.../hostX
539 i=1
677cdf78
AH
540
541 while [ $i -le "$num_dirs" ] ; do
542 d=$(eval echo '$'{$i})
269db7a4 543 scsi_host_dir="$scsi_host_dir/$d"
677cdf78
AH
544
545 echo "$d" | grep -q -E '^host[0-9]+$' && break
546 i=$((i + 1))
269db7a4
SG
547 done
548
677cdf78 549 if [ $i = "$num_dirs" ] ; then
269db7a4
SG
550 return
551 fi
552
677cdf78 553 PCI_ID=$(eval echo '$'{$((i -1))} | awk -F: '{print $2":"$3}')
269db7a4
SG
554
555 # In scsi mode, the directory two levels beneath
556 # /sys/.../hostX reveals the port and slot.
557 port_dir=$scsi_host_dir
677cdf78 558 j=$((i + 2))
269db7a4 559
677cdf78 560 i=$((i + 1))
269db7a4 561 while [ $i -le $j ] ; do
677cdf78
AH
562 port_dir="$port_dir/$(eval echo '$'{$i})"
563 i=$((i + 1))
269db7a4
SG
564 done
565
677cdf78 566 set -- $(echo "$port_dir" | sed -e 's/^.*:\([^:]*\):\([^:]*\)$/\1 \2/')
269db7a4 567 PORT=$1
677cdf78 568 SLOT=$(($2 + FIRST_BAY_NUMBER))
269db7a4
SG
569
570 if [ -z "$SLOT" ] ; then
571 return
572 fi
573
677cdf78
AH
574 CHAN=$(map_channel "$PCI_ID" "$PORT")
575 SLOT=$(map_slot "$SLOT" "$CHAN")
576
269db7a4
SG
577 if [ -z "$CHAN" ] ; then
578 return
579 fi
677cdf78 580 echo "${CHAN}${SLOT}${PART}"
269db7a4
SG
581}
582
c66401fa
TH
583# Figure out the name for the enclosure symlink
584enclosure_handler () {
585 # We get all the info we need from udev's DEVPATH variable:
586 #
587 # DEVPATH=/sys/devices/pci0000:00/0000:00:03.0/0000:05:00.0/host0/subsystem/devices/0:0:0:0/scsi_generic/sg0
588
589 # Get the enclosure ID ("0:0:0:0")
590 ENC=$(basename $(readlink -m "/sys/$DEVPATH/../.."))
677cdf78 591 if [ ! -d "/sys/class/enclosure/$ENC" ] ; then
c66401fa
TH
592 # Not an enclosure, bail out
593 return
594 fi
595
596 # Get the long sysfs device path to our enclosure. Looks like:
597 # /devices/pci0000:00/0000:00:03.0/0000:05:00.0/host0/port-0:0/ ... /enclosure/0:0:0:0
598
677cdf78 599 ENC_DEVICE=$(readlink "/sys/class/enclosure/$ENC")
c66401fa
TH
600
601 # Grab the full path to the hosts port dir:
602 # /devices/pci0000:00/0000:00:03.0/0000:05:00.0/host0/port-0:0
677cdf78 603 PORT_DIR=$(echo "$ENC_DEVICE" | grep -Eo '.+host[0-9]+/port-[0-9]+:[0-9]+')
c66401fa
TH
604
605 # Get the port number
677cdf78 606 PORT_ID=$(echo "$PORT_DIR" | grep -Eo "[0-9]+$")
c66401fa
TH
607
608 # The PCI directory is two directories up from the port directory
609 # /sys/devices/pci0000:00/0000:00:03.0/0000:05:00.0
610 PCI_ID_LONG=$(basename $(readlink -m "/sys/$PORT_DIR/../.."))
611
612 # Strip down the PCI address from 0000:05:00.0 to 05:00.0
613 PCI_ID=$(echo "$PCI_ID_LONG" | sed -r 's/^[0-9]+://g')
614
615 # Name our device according to vdev_id.conf (like "L0" or "U1").
677cdf78
AH
616 NAME=$(awk '/channel/{if ($1 == "channel" && $2 == "$PCI_ID" && \
617 $3 == "$PORT_ID") {print ${4}int(count[$4])}; count[$4]++}' $CONFIG)
c66401fa
TH
618
619 echo "${NAME}"
620}
621
2957f38d
NB
622alias_handler () {
623 # Special handling is needed to correctly append a -part suffix
624 # to partitions of device mapper devices. The DEVTYPE attribute
625 # is normally set to "disk" instead of "partition" in this case,
626 # so the udev rules won't handle that for us as they do for
627 # "plain" block devices.
628 #
629 # For example, we may have the following links for a device and its
630 # partitions,
631 #
632 # /dev/disk/by-id/dm-name-isw_dibgbfcije_ARRAY0 -> ../../dm-0
633 # /dev/disk/by-id/dm-name-isw_dibgbfcije_ARRAY0p1 -> ../../dm-1
634 # /dev/disk/by-id/dm-name-isw_dibgbfcije_ARRAY0p2 -> ../../dm-3
635 #
636 # and the following alias in vdev_id.conf.
637 #
638 # alias A0 dm-name-isw_dibgbfcije_ARRAY0
639 #
640 # The desired outcome is for the following links to be created
641 # without having explicitly defined aliases for the partitions.
642 #
643 # /dev/disk/by-vdev/A0 -> ../../dm-0
644 # /dev/disk/by-vdev/A0-part1 -> ../../dm-1
645 # /dev/disk/by-vdev/A0-part2 -> ../../dm-3
646 #
647 # Warning: The following grep pattern will misidentify whole-disk
648 # devices whose names end with 'p' followed by a string of
649 # digits as partitions, causing alias creation to fail. This
650 # ambiguity seems unavoidable, so devices using this facility
651 # must not use such names.
64025fa3 652 DM_PART=
677cdf78 653 if echo "$DM_NAME" | grep -q -E 'p[0-9][0-9]*$' ; then
2957f38d 654 if [ "$DEVTYPE" != "partition" ] ; then
3ee4e6d8
TH
655 # Match p[number], remove the 'p' and prepend "-part"
656 DM_PART=$(echo "$DM_NAME" |
657 awk 'match($0,/p[0-9]+$/) {print "-part"substr($0,RSTART+1,RLENGTH-1)}')
2957f38d
NB
658 fi
659 fi
660
661 # DEVLINKS attribute must have been populated by already-run udev rules.
662 for link in $DEVLINKS ; do
663 # Remove partition information to match key of top-level device.
664 if [ -n "$DM_PART" ] ; then
677cdf78 665 link=$(echo "$link" | sed 's/p[0-9][0-9]*$//')
2957f38d
NB
666 fi
667 # Check both the fully qualified and the base name of link.
677cdf78
AH
668 for l in $link $(basename "$link") ; do
669 if [ ! -z "$l" ]; then
670 alias=$(awk -v var="$l" '($1 == "alias") && \
671 ($3 == var) \
672 { print $2; exit }' $CONFIG)
673 if [ -n "$alias" ] ; then
674 echo "${alias}${DM_PART}"
675 return
676 fi
2957f38d
NB
677 fi
678 done
679 done
680}
681
677cdf78
AH
682# main
683while getopts 'c:d:eg:jmp:h' OPTION; do
821b6834
NB
684 case ${OPTION} in
685 c)
2957f38d
NB
686 CONFIG=${OPTARG}
687 ;;
821b6834 688 d)
2957f38d
NB
689 DEV=${OPTARG}
690 ;;
c66401fa
TH
691 e)
692 # When udev sees a scsi_generic device, it calls this script with -e to
693 # create the enclosure device symlinks only. We also need
694 # "enclosure_symlinks yes" set in vdev_id.config to actually create the
695 # symlink.
677cdf78
AH
696 ENCLOSURE_MODE=$(awk '{if ($1 == "enclosure_symlinks") \
697 print $2}' "$CONFIG")
698
c66401fa
TH
699 if [ "$ENCLOSURE_MODE" != "yes" ] ; then
700 exit 0
701 fi
702 ;;
821b6834
NB
703 g)
704 TOPOLOGY=$OPTARG
705 ;;
706 p)
707 PHYS_PER_PORT=${OPTARG}
708 ;;
677cdf78
AH
709 j)
710 MULTIJBOD_MODE=yes
711 ;;
821b6834
NB
712 m)
713 MULTIPATH_MODE=yes
714 ;;
821b6834
NB
715 h)
716 usage
717 ;;
718 esac
719done
720
677cdf78 721if [ ! -r "$CONFIG" ] ; then
e2870fb2 722 echo "Error: Config file \"$CONFIG\" not found"
821b6834
NB
723 exit 0
724fi
725
64025fa3 726if [ -z "$DEV" ] && [ -z "$ENCLOSURE_MODE" ] ; then
821b6834
NB
727 echo "Error: missing required option -d"
728 exit 1
729fi
730
731if [ -z "$TOPOLOGY" ] ; then
677cdf78 732 TOPOLOGY=$(awk '($1 == "topology") {print $2; exit}' "$CONFIG")
821b6834 733fi
a6ef9522 734
bba365cf 735if [ -z "$BAY" ] ; then
677cdf78 736 BAY=$(awk '($1 == "slot") {print $2; exit}' "$CONFIG")
bba365cf
AB
737fi
738
c66401fa
TH
739TOPOLOGY=${TOPOLOGY:-sas_direct}
740
741# Should we create /dev/by-enclosure symlinks?
64025fa3 742if [ "$ENCLOSURE_MODE" = "yes" ] && [ "$TOPOLOGY" = "sas_direct" ] ; then
c66401fa
TH
743 ID_ENCLOSURE=$(enclosure_handler)
744 if [ -z "$ID_ENCLOSURE" ] ; then
745 exit 0
746 fi
747
748 # Just create the symlinks to the enclosure devices and then exit.
677cdf78 749 ENCLOSURE_PREFIX=$(awk '/enclosure_symlinks_prefix/{print $2}' "$CONFIG")
c66401fa
TH
750 if [ -z "$ENCLOSURE_PREFIX" ] ; then
751 ENCLOSURE_PREFIX="enc"
752 fi
753 echo "ID_ENCLOSURE=$ID_ENCLOSURE"
754 echo "ID_ENCLOSURE_PATH=by-enclosure/$ENCLOSURE_PREFIX-$ID_ENCLOSURE"
755 exit 0
756fi
757
2957f38d 758# First check if an alias was defined for this device.
677cdf78 759ID_VDEV=$(alias_handler)
821b6834 760
2957f38d 761if [ -z "$ID_VDEV" ] ; then
bba365cf 762 BAY=${BAY:-bay}
2957f38d
NB
763 case $TOPOLOGY in
764 sas_direct|sas_switch)
677cdf78 765 ID_VDEV=$(sas_handler)
2957f38d 766 ;;
269db7a4 767 scsi)
677cdf78 768 ID_VDEV=$(scsi_handler)
269db7a4 769 ;;
2957f38d
NB
770 *)
771 echo "Error: unknown topology $TOPOLOGY"
772 exit 1
773 ;;
774 esac
821b6834
NB
775fi
776
2957f38d
NB
777if [ -n "$ID_VDEV" ] ; then
778 echo "ID_VDEV=${ID_VDEV}"
779 echo "ID_VDEV_PATH=disk/by-vdev/${ID_VDEV}"
821b6834 780fi