]> git.proxmox.com Git - mirror_zfs.git/blame - cmd/vdev_id/vdev_id
Add enclosure_symlinks option to vdev_id
[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
2957f38d
NB
82# #
83# # Example vdev_id.conf - alias
84# #
85#
86# # by-vdev
87# # name fully qualified or base name of device link
88# alias d1 /dev/disk/by-id/wwn-0x5000c5002de3b9ca
89# alias d2 wwn-0x5000c5002def789e
90
821b6834
NB
91PATH=/bin:/sbin:/usr/bin:/usr/sbin
92CONFIG=/etc/zfs/vdev_id.conf
93PHYS_PER_PORT=
94DEV=
821b6834
NB
95MULTIPATH=
96TOPOLOGY=
bba365cf 97BAY=
821b6834
NB
98
99usage() {
100 cat << EOF
101Usage: vdev_id [-h]
102 vdev_id <-d device> [-c config_file] [-p phys_per_port]
269db7a4 103 [-g sas_direct|sas_switch|scsi] [-m]
821b6834
NB
104
105 -c specify name of alernate config file [default=$CONFIG]
106 -d specify basename of device (i.e. sda)
c66401fa 107 -e Create enclose device symlinks only (/dev/by-enclosure)
821b6834
NB
108 -g Storage network topology [default="$TOPOLOGY"]
109 -m Run in multipath mode
110 -p number of phy's per switch port [default=$PHYS_PER_PORT]
111 -h show this summary
112EOF
113 exit 0
114}
115
116map_slot() {
117 local LINUX_SLOT=$1
09d0b30f 118 local CHANNEL=$2
821b6834
NB
119 local MAPPED_SLOT=
120
09d0b30f 121 MAPPED_SLOT=`awk "\\$1 == \"slot\" && \\$2 == ${LINUX_SLOT} && \
2d9d57b0 122 \\$4 ~ /^${CHANNEL}$|^$/ { print \\$3; exit }" $CONFIG`
821b6834
NB
123 if [ -z "$MAPPED_SLOT" ] ; then
124 MAPPED_SLOT=$LINUX_SLOT
125 fi
126 printf "%d" ${MAPPED_SLOT}
127}
128
129map_channel() {
130 local MAPPED_CHAN=
131 local PCI_ID=$1
132 local PORT=$2
133
134 case $TOPOLOGY in
135 "sas_switch")
ba43f456 136 MAPPED_CHAN=`awk "\\$1 == \"channel\" && \\$2 == ${PORT} \
821b6834
NB
137 { print \\$3; exit }" $CONFIG`
138 ;;
269db7a4 139 "sas_direct"|"scsi")
ba43f456
NB
140 MAPPED_CHAN=`awk "\\$1 == \"channel\" && \
141 \\$2 == \"${PCI_ID}\" && \\$3 == ${PORT} \
142 { print \\$4; exit }" $CONFIG`
821b6834
NB
143 ;;
144 esac
145 printf "%s" ${MAPPED_CHAN}
146}
147
2957f38d
NB
148sas_handler() {
149 if [ -z "$PHYS_PER_PORT" ] ; then
ba43f456
NB
150 PHYS_PER_PORT=`awk "\\$1 == \"phys_per_port\" \
151 {print \\$2; exit}" $CONFIG`
2957f38d
NB
152 fi
153 PHYS_PER_PORT=${PHYS_PER_PORT:-4}
154 if ! echo $PHYS_PER_PORT | grep -q -E '^[0-9]+$' ; then
155 echo "Error: phys_per_port value $PHYS_PER_PORT is non-numeric"
156 exit 1
157 fi
158
159 if [ -z "$MULTIPATH_MODE" ] ; then
ba43f456
NB
160 MULTIPATH_MODE=`awk "\\$1 == \"multipath\" \
161 {print \\$2; exit}" $CONFIG`
2957f38d
NB
162 fi
163
164 # Use first running component device if we're handling a dm-mpath device
165 if [ "$MULTIPATH_MODE" = "yes" ] ; then
166 # If udev didn't tell us the UUID via DM_NAME, check /dev/mapper
167 if [ -z "$DM_NAME" ] ; then
168 DM_NAME=`ls -l --full-time /dev/mapper |
169 awk "/\/$DEV$/{print \\$9}"`
170 fi
171
172 # For raw disks udev exports DEVTYPE=partition when
173 # handling partitions, and the rules can be written to
174 # take advantage of this to append a -part suffix. For
175 # dm devices we get DEVTYPE=disk even for partitions so
176 # we have to append the -part suffix directly in the
177 # helper.
178 if [ "$DEVTYPE" != "partition" ] ; then
179 PART=`echo $DM_NAME | awk -Fp '/p/{print "-part"$2}'`
180 fi
181
182 # Strip off partition information.
183 DM_NAME=`echo $DM_NAME | sed 's/p[0-9][0-9]*$//'`
184 if [ -z "$DM_NAME" ] ; then
185 return
186 fi
187
2a152383 188 # Get the raw scsi device name from multipath -ll. Strip off
383efa57 189 # leading pipe symbols to make field numbering consistent.
2a152383 190 DEV=`multipath -ll $DM_NAME |
383efa57 191 awk '/running/{gsub("^[|]"," "); print $3 ; exit}'`
2957f38d
NB
192 if [ -z "$DEV" ] ; then
193 return
194 fi
195 fi
196
197 if echo $DEV | grep -q ^/devices/ ; then
198 sys_path=$DEV
199 else
200 sys_path=`udevadm info -q path -p /sys/block/$DEV 2>/dev/null`
201 fi
202
203 # Use positional parameters as an ad-hoc array
204 set -- $(echo "$sys_path" | tr / ' ')
205 num_dirs=$#
206 scsi_host_dir="/sys"
207
208 # Get path up to /sys/.../hostX
209 i=1
210 while [ $i -le $num_dirs ] ; do
211 d=$(eval echo \${$i})
212 scsi_host_dir="$scsi_host_dir/$d"
213 echo $d | grep -q -E '^host[0-9]+$' && break
214 i=$(($i + 1))
215 done
216
217 if [ $i = $num_dirs ] ; then
218 return
219 fi
220
221 PCI_ID=$(eval echo \${$(($i -1))} | awk -F: '{print $2":"$3}')
222
223 # In sas_switch mode, the directory four levels beneath
224 # /sys/.../hostX contains symlinks to phy devices that reveal
225 # the switch port number. In sas_direct mode, the phy links one
226 # directory down reveal the HBA port.
227 port_dir=$scsi_host_dir
228 case $TOPOLOGY in
229 "sas_switch") j=$(($i + 4)) ;;
230 "sas_direct") j=$(($i + 1)) ;;
231 esac
232
233 i=$(($i + 1))
234 while [ $i -le $j ] ; do
235 port_dir="$port_dir/$(eval echo \${$i})"
236 i=$(($i + 1))
237 done
238
239 PHY=`ls -d $port_dir/phy* 2>/dev/null | head -1 | awk -F: '{print $NF}'`
240 if [ -z "$PHY" ] ; then
d49d9c2b 241 PHY=0
2957f38d
NB
242 fi
243 PORT=$(( $PHY / $PHYS_PER_PORT ))
244
245 # Look in /sys/.../sas_device/end_device-X for the bay_identifier
246 # attribute.
247 end_device_dir=$port_dir
248 while [ $i -lt $num_dirs ] ; do
249 d=$(eval echo \${$i})
250 end_device_dir="$end_device_dir/$d"
251 if echo $d | grep -q '^end_device' ; then
252 end_device_dir="$end_device_dir/sas_device/$d"
253 break
254 fi
255 i=$(($i + 1))
256 done
257
bba365cf
AB
258 SLOT=
259 case $BAY in
260 "bay")
261 SLOT=`cat $end_device_dir/bay_identifier 2>/dev/null`
262 ;;
263 "phy")
264 SLOT=`cat $end_device_dir/phy_identifier 2>/dev/null`
265 ;;
d49d9c2b
DK
266 "port")
267 d=$(eval echo \${$i})
268 SLOT=`echo $d | sed -e 's/^.*://'`
269 ;;
bba365cf
AB
270 "id")
271 i=$(($i + 1))
272 d=$(eval echo \${$i})
273 SLOT=`echo $d | sed -e 's/^.*://'`
274 ;;
275 "lun")
276 i=$(($i + 2))
277 d=$(eval echo \${$i})
278 SLOT=`echo $d | sed -e 's/^.*://'`
279 ;;
993669a7
SG
280 "ses")
281 # look for this SAS path in all SCSI Enclosure Services
282 # (SES) enclosures
283 sas_address=`cat $end_device_dir/sas_address 2>/dev/null`
284 enclosures=`lsscsi -g | \
285 sed -n -e '/enclosu/s/^.* \([^ ][^ ]*\) *$/\1/p'`
286 for enclosure in $enclosures; do
287 set -- $(sg_ses -p aes $enclosure | \
288 awk "/device slot number:/{slot=\$12} \
289 /SAS address: $sas_address/\
290 {print slot}")
291 SLOT=$1
292 if [ -n "$SLOT" ] ; then
293 break
294 fi
295 done
296 ;;
bba365cf 297 esac
2957f38d
NB
298 if [ -z "$SLOT" ] ; then
299 return
300 fi
301
2957f38d 302 CHAN=`map_channel $PCI_ID $PORT`
09d0b30f 303 SLOT=`map_slot $SLOT $CHAN`
2957f38d
NB
304 if [ -z "$CHAN" ] ; then
305 return
306 fi
307 echo ${CHAN}${SLOT}${PART}
308}
309
269db7a4
SG
310scsi_handler() {
311 if [ -z "$FIRST_BAY_NUMBER" ] ; then
312 FIRST_BAY_NUMBER=`awk "\\$1 == \"first_bay_number\" \
313 {print \\$2; exit}" $CONFIG`
314 fi
315 FIRST_BAY_NUMBER=${FIRST_BAY_NUMBER:-0}
316
317 if [ -z "$PHYS_PER_PORT" ] ; then
318 PHYS_PER_PORT=`awk "\\$1 == \"phys_per_port\" \
319 {print \\$2; exit}" $CONFIG`
320 fi
321 PHYS_PER_PORT=${PHYS_PER_PORT:-4}
322 if ! echo $PHYS_PER_PORT | grep -q -E '^[0-9]+$' ; then
323 echo "Error: phys_per_port value $PHYS_PER_PORT is non-numeric"
324 exit 1
325 fi
326
327 if [ -z "$MULTIPATH_MODE" ] ; then
328 MULTIPATH_MODE=`awk "\\$1 == \"multipath\" \
329 {print \\$2; exit}" $CONFIG`
330 fi
331
332 # Use first running component device if we're handling a dm-mpath device
333 if [ "$MULTIPATH_MODE" = "yes" ] ; then
334 # If udev didn't tell us the UUID via DM_NAME, check /dev/mapper
335 if [ -z "$DM_NAME" ] ; then
336 DM_NAME=`ls -l --full-time /dev/mapper |
337 awk "/\/$DEV$/{print \\$9}"`
338 fi
339
340 # For raw disks udev exports DEVTYPE=partition when
341 # handling partitions, and the rules can be written to
342 # take advantage of this to append a -part suffix. For
343 # dm devices we get DEVTYPE=disk even for partitions so
344 # we have to append the -part suffix directly in the
345 # helper.
346 if [ "$DEVTYPE" != "partition" ] ; then
347 PART=`echo $DM_NAME | awk -Fp '/p/{print "-part"$2}'`
348 fi
349
350 # Strip off partition information.
351 DM_NAME=`echo $DM_NAME | sed 's/p[0-9][0-9]*$//'`
352 if [ -z "$DM_NAME" ] ; then
353 return
354 fi
355
356 # Get the raw scsi device name from multipath -ll. Strip off
357 # leading pipe symbols to make field numbering consistent.
358 DEV=`multipath -ll $DM_NAME |
359 awk '/running/{gsub("^[|]"," "); print $3 ; exit}'`
360 if [ -z "$DEV" ] ; then
361 return
362 fi
363 fi
364
365 if echo $DEV | grep -q ^/devices/ ; then
366 sys_path=$DEV
367 else
368 sys_path=`udevadm info -q path -p /sys/block/$DEV 2>/dev/null`
369 fi
370
371 # expect sys_path like this, for example:
372 # /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
373
374 # Use positional parameters as an ad-hoc array
375 set -- $(echo "$sys_path" | tr / ' ')
376 num_dirs=$#
377 scsi_host_dir="/sys"
378
379 # Get path up to /sys/.../hostX
380 i=1
381 while [ $i -le $num_dirs ] ; do
382 d=$(eval echo \${$i})
383 scsi_host_dir="$scsi_host_dir/$d"
384 echo $d | grep -q -E '^host[0-9]+$' && break
385 i=$(($i + 1))
386 done
387
388 if [ $i = $num_dirs ] ; then
389 return
390 fi
391
392 PCI_ID=$(eval echo \${$(($i -1))} | awk -F: '{print $2":"$3}')
393
394 # In scsi mode, the directory two levels beneath
395 # /sys/.../hostX reveals the port and slot.
396 port_dir=$scsi_host_dir
397 j=$(($i + 2))
398
399 i=$(($i + 1))
400 while [ $i -le $j ] ; do
401 port_dir="$port_dir/$(eval echo \${$i})"
402 i=$(($i + 1))
403 done
404
405 set -- $(echo $port_dir | sed -e 's/^.*:\([^:]*\):\([^:]*\)$/\1 \2/')
406 PORT=$1
407 SLOT=$(($2 + $FIRST_BAY_NUMBER))
408
409 if [ -z "$SLOT" ] ; then
410 return
411 fi
412
413 CHAN=`map_channel $PCI_ID $PORT`
414 SLOT=`map_slot $SLOT $CHAN`
415 if [ -z "$CHAN" ] ; then
416 return
417 fi
418 echo ${CHAN}${SLOT}${PART}
419}
420
c66401fa
TH
421# Figure out the name for the enclosure symlink
422enclosure_handler () {
423 # We get all the info we need from udev's DEVPATH variable:
424 #
425 # DEVPATH=/sys/devices/pci0000:00/0000:00:03.0/0000:05:00.0/host0/subsystem/devices/0:0:0:0/scsi_generic/sg0
426
427 # Get the enclosure ID ("0:0:0:0")
428 ENC=$(basename $(readlink -m "/sys/$DEVPATH/../.."))
429 if [ ! -d /sys/class/enclosure/$ENC ] ; then
430 # Not an enclosure, bail out
431 return
432 fi
433
434 # Get the long sysfs device path to our enclosure. Looks like:
435 # /devices/pci0000:00/0000:00:03.0/0000:05:00.0/host0/port-0:0/ ... /enclosure/0:0:0:0
436
437 ENC_DEVICE=$(readlink /sys/class/enclosure/$ENC)
438
439 # Grab the full path to the hosts port dir:
440 # /devices/pci0000:00/0000:00:03.0/0000:05:00.0/host0/port-0:0
441 PORT_DIR=$(echo $ENC_DEVICE | grep -Eo '.+host[0-9]+/port-[0-9]+:[0-9]+')
442
443 # Get the port number
444 PORT_ID=$(echo $PORT_DIR | grep -Eo "[0-9]+$")
445
446 # The PCI directory is two directories up from the port directory
447 # /sys/devices/pci0000:00/0000:00:03.0/0000:05:00.0
448 PCI_ID_LONG=$(basename $(readlink -m "/sys/$PORT_DIR/../.."))
449
450 # Strip down the PCI address from 0000:05:00.0 to 05:00.0
451 PCI_ID=$(echo "$PCI_ID_LONG" | sed -r 's/^[0-9]+://g')
452
453 # Name our device according to vdev_id.conf (like "L0" or "U1").
454 NAME=$(awk "/channel/{if (\$1 == \"channel\" && \$2 == \"$PCI_ID\" && \
455 \$3 == \"$PORT_ID\") {print \$4int(count[\$4])}; count[\$4]++}" $CONFIG)
456
457 echo "${NAME}"
458}
459
2957f38d
NB
460alias_handler () {
461 # Special handling is needed to correctly append a -part suffix
462 # to partitions of device mapper devices. The DEVTYPE attribute
463 # is normally set to "disk" instead of "partition" in this case,
464 # so the udev rules won't handle that for us as they do for
465 # "plain" block devices.
466 #
467 # For example, we may have the following links for a device and its
468 # partitions,
469 #
470 # /dev/disk/by-id/dm-name-isw_dibgbfcije_ARRAY0 -> ../../dm-0
471 # /dev/disk/by-id/dm-name-isw_dibgbfcije_ARRAY0p1 -> ../../dm-1
472 # /dev/disk/by-id/dm-name-isw_dibgbfcije_ARRAY0p2 -> ../../dm-3
473 #
474 # and the following alias in vdev_id.conf.
475 #
476 # alias A0 dm-name-isw_dibgbfcije_ARRAY0
477 #
478 # The desired outcome is for the following links to be created
479 # without having explicitly defined aliases for the partitions.
480 #
481 # /dev/disk/by-vdev/A0 -> ../../dm-0
482 # /dev/disk/by-vdev/A0-part1 -> ../../dm-1
483 # /dev/disk/by-vdev/A0-part2 -> ../../dm-3
484 #
485 # Warning: The following grep pattern will misidentify whole-disk
486 # devices whose names end with 'p' followed by a string of
487 # digits as partitions, causing alias creation to fail. This
488 # ambiguity seems unavoidable, so devices using this facility
489 # must not use such names.
490 local DM_PART=
491 if echo $DM_NAME | grep -q -E 'p[0-9][0-9]*$' ; then
492 if [ "$DEVTYPE" != "partition" ] ; then
493 DM_PART=`echo $DM_NAME | awk -Fp '/p/{print "-part"$2}'`
494 fi
495 fi
496
497 # DEVLINKS attribute must have been populated by already-run udev rules.
498 for link in $DEVLINKS ; do
499 # Remove partition information to match key of top-level device.
500 if [ -n "$DM_PART" ] ; then
501 link=`echo $link | sed 's/p[0-9][0-9]*$//'`
502 fi
503 # Check both the fully qualified and the base name of link.
504 for l in $link `basename $link` ; do
ba43f456 505 alias=`awk "\\$1 == \"alias\" && \\$3 == \"${l}\" \
2957f38d
NB
506 { print \\$2; exit }" $CONFIG`
507 if [ -n "$alias" ] ; then
508 echo ${alias}${DM_PART}
509 return
510 fi
511 done
512 done
513}
514
c66401fa 515while getopts 'c:d:eg:mp:h' OPTION; do
821b6834
NB
516 case ${OPTION} in
517 c)
2957f38d
NB
518 CONFIG=${OPTARG}
519 ;;
821b6834 520 d)
2957f38d
NB
521 DEV=${OPTARG}
522 ;;
c66401fa
TH
523 e)
524 # When udev sees a scsi_generic device, it calls this script with -e to
525 # create the enclosure device symlinks only. We also need
526 # "enclosure_symlinks yes" set in vdev_id.config to actually create the
527 # symlink.
528 ENCLOSURE_MODE=$(awk '{if ($1 == "enclosure_symlinks") print $2}' $CONFIG)
529 if [ "$ENCLOSURE_MODE" != "yes" ] ; then
530 exit 0
531 fi
532 ;;
821b6834
NB
533 g)
534 TOPOLOGY=$OPTARG
535 ;;
536 p)
537 PHYS_PER_PORT=${OPTARG}
538 ;;
539 m)
540 MULTIPATH_MODE=yes
541 ;;
821b6834
NB
542 h)
543 usage
544 ;;
545 esac
546done
547
548if [ ! -r $CONFIG ] ; then
549 exit 0
550fi
551
c66401fa 552if [ -z "$DEV" -a -z "$ENCLOSURE_MODE" ] ; then
821b6834
NB
553 echo "Error: missing required option -d"
554 exit 1
555fi
556
557if [ -z "$TOPOLOGY" ] ; then
ba43f456 558 TOPOLOGY=`awk "\\$1 == \"topology\" {print \\$2; exit}" $CONFIG`
821b6834 559fi
a6ef9522 560
bba365cf
AB
561if [ -z "$BAY" ] ; then
562 BAY=`awk "\\$1 == \"slot\" {print \\$2; exit}" $CONFIG`
563fi
564
c66401fa
TH
565TOPOLOGY=${TOPOLOGY:-sas_direct}
566
567# Should we create /dev/by-enclosure symlinks?
568if [ "$ENCLOSURE_MODE" = "yes" -a "$TOPOLOGY" = "sas_direct" ] ; then
569 ID_ENCLOSURE=$(enclosure_handler)
570 if [ -z "$ID_ENCLOSURE" ] ; then
571 exit 0
572 fi
573
574 # Just create the symlinks to the enclosure devices and then exit.
575 ENCLOSURE_PREFIX=$(awk '/enclosure_symlinks_prefix/{print $2}' $CONFIG)
576 if [ -z "$ENCLOSURE_PREFIX" ] ; then
577 ENCLOSURE_PREFIX="enc"
578 fi
579 echo "ID_ENCLOSURE=$ID_ENCLOSURE"
580 echo "ID_ENCLOSURE_PATH=by-enclosure/$ENCLOSURE_PREFIX-$ID_ENCLOSURE"
581 exit 0
582fi
583
2957f38d
NB
584# First check if an alias was defined for this device.
585ID_VDEV=`alias_handler`
821b6834 586
2957f38d 587if [ -z "$ID_VDEV" ] ; then
bba365cf 588 BAY=${BAY:-bay}
2957f38d
NB
589 case $TOPOLOGY in
590 sas_direct|sas_switch)
591 ID_VDEV=`sas_handler`
592 ;;
269db7a4
SG
593 scsi)
594 ID_VDEV=`scsi_handler`
595 ;;
2957f38d
NB
596 *)
597 echo "Error: unknown topology $TOPOLOGY"
598 exit 1
599 ;;
600 esac
821b6834
NB
601fi
602
2957f38d
NB
603if [ -n "$ID_VDEV" ] ; then
604 echo "ID_VDEV=${ID_VDEV}"
605 echo "ID_VDEV_PATH=disk/by-vdev/${ID_VDEV}"
821b6834 606fi