]> git.proxmox.com Git - mirror_zfs.git/blob - cmd/vdev_id/vdev_id
Add enclosure_symlinks option to vdev_id
[mirror_zfs.git] / cmd / vdev_id / vdev_id
1 #!/bin/sh
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 #
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 #
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
35 # slot bay
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 #
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
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
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
91 PATH=/bin:/sbin:/usr/bin:/usr/sbin
92 CONFIG=/etc/zfs/vdev_id.conf
93 PHYS_PER_PORT=
94 DEV=
95 MULTIPATH=
96 TOPOLOGY=
97 BAY=
98
99 usage() {
100 cat << EOF
101 Usage: vdev_id [-h]
102 vdev_id <-d device> [-c config_file] [-p phys_per_port]
103 [-g sas_direct|sas_switch|scsi] [-m]
104
105 -c specify name of alernate config file [default=$CONFIG]
106 -d specify basename of device (i.e. sda)
107 -e Create enclose device symlinks only (/dev/by-enclosure)
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
112 EOF
113 exit 0
114 }
115
116 map_slot() {
117 local LINUX_SLOT=$1
118 local CHANNEL=$2
119 local MAPPED_SLOT=
120
121 MAPPED_SLOT=`awk "\\$1 == \"slot\" && \\$2 == ${LINUX_SLOT} && \
122 \\$4 ~ /^${CHANNEL}$|^$/ { print \\$3; exit }" $CONFIG`
123 if [ -z "$MAPPED_SLOT" ] ; then
124 MAPPED_SLOT=$LINUX_SLOT
125 fi
126 printf "%d" ${MAPPED_SLOT}
127 }
128
129 map_channel() {
130 local MAPPED_CHAN=
131 local PCI_ID=$1
132 local PORT=$2
133
134 case $TOPOLOGY in
135 "sas_switch")
136 MAPPED_CHAN=`awk "\\$1 == \"channel\" && \\$2 == ${PORT} \
137 { print \\$3; exit }" $CONFIG`
138 ;;
139 "sas_direct"|"scsi")
140 MAPPED_CHAN=`awk "\\$1 == \"channel\" && \
141 \\$2 == \"${PCI_ID}\" && \\$3 == ${PORT} \
142 { print \\$4; exit }" $CONFIG`
143 ;;
144 esac
145 printf "%s" ${MAPPED_CHAN}
146 }
147
148 sas_handler() {
149 if [ -z "$PHYS_PER_PORT" ] ; then
150 PHYS_PER_PORT=`awk "\\$1 == \"phys_per_port\" \
151 {print \\$2; exit}" $CONFIG`
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
160 MULTIPATH_MODE=`awk "\\$1 == \"multipath\" \
161 {print \\$2; exit}" $CONFIG`
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
188 # Get the raw scsi device name from multipath -ll. Strip off
189 # leading pipe symbols to make field numbering consistent.
190 DEV=`multipath -ll $DM_NAME |
191 awk '/running/{gsub("^[|]"," "); print $3 ; exit}'`
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
241 PHY=0
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
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 ;;
266 "port")
267 d=$(eval echo \${$i})
268 SLOT=`echo $d | sed -e 's/^.*://'`
269 ;;
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 ;;
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 ;;
297 esac
298 if [ -z "$SLOT" ] ; then
299 return
300 fi
301
302 CHAN=`map_channel $PCI_ID $PORT`
303 SLOT=`map_slot $SLOT $CHAN`
304 if [ -z "$CHAN" ] ; then
305 return
306 fi
307 echo ${CHAN}${SLOT}${PART}
308 }
309
310 scsi_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
421 # Figure out the name for the enclosure symlink
422 enclosure_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
460 alias_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
505 alias=`awk "\\$1 == \"alias\" && \\$3 == \"${l}\" \
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
515 while getopts 'c:d:eg:mp:h' OPTION; do
516 case ${OPTION} in
517 c)
518 CONFIG=${OPTARG}
519 ;;
520 d)
521 DEV=${OPTARG}
522 ;;
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 ;;
533 g)
534 TOPOLOGY=$OPTARG
535 ;;
536 p)
537 PHYS_PER_PORT=${OPTARG}
538 ;;
539 m)
540 MULTIPATH_MODE=yes
541 ;;
542 h)
543 usage
544 ;;
545 esac
546 done
547
548 if [ ! -r $CONFIG ] ; then
549 exit 0
550 fi
551
552 if [ -z "$DEV" -a -z "$ENCLOSURE_MODE" ] ; then
553 echo "Error: missing required option -d"
554 exit 1
555 fi
556
557 if [ -z "$TOPOLOGY" ] ; then
558 TOPOLOGY=`awk "\\$1 == \"topology\" {print \\$2; exit}" $CONFIG`
559 fi
560
561 if [ -z "$BAY" ] ; then
562 BAY=`awk "\\$1 == \"slot\" {print \\$2; exit}" $CONFIG`
563 fi
564
565 TOPOLOGY=${TOPOLOGY:-sas_direct}
566
567 # Should we create /dev/by-enclosure symlinks?
568 if [ "$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
582 fi
583
584 # First check if an alias was defined for this device.
585 ID_VDEV=`alias_handler`
586
587 if [ -z "$ID_VDEV" ] ; then
588 BAY=${BAY:-bay}
589 case $TOPOLOGY in
590 sas_direct|sas_switch)
591 ID_VDEV=`sas_handler`
592 ;;
593 scsi)
594 ID_VDEV=`scsi_handler`
595 ;;
596 *)
597 echo "Error: unknown topology $TOPOLOGY"
598 exit 1
599 ;;
600 esac
601 fi
602
603 if [ -n "$ID_VDEV" ] ; then
604 echo "ID_VDEV=${ID_VDEV}"
605 echo "ID_VDEV_PATH=disk/by-vdev/${ID_VDEV}"
606 fi