3 # vdev_id: udev helper to generate user-friendly names for JBOD disks
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.
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.
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.
26 # Some example configuration files are given below.
29 # # Example vdev_id.conf - sas_direct.
37 # # PCI_ID HBA PORT CHANNEL NAME
43 # # Custom mapping for Channel A
52 # # Default mapping for B, C, and D
59 # # Example vdev_id.conf - sas_switch
64 # # SWITCH PORT CHANNEL NAME
71 # # Example vdev_id.conf - multipath
76 # # PCI_ID HBA PORT CHANNEL NAME
83 # # Example vdev_id.conf - multipath / multijbod-daisychaining
89 # # PCI_ID HBA PORT CHANNEL NAME
96 # # Example vdev_id.conf - multipath / mixed
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
111 # # Example vdev_id.conf - alias
115 # # name fully qualified or base name of device link
116 # alias d1 /dev/disk/by-id/wwn-0x5000c5002de3b9ca
117 # alias d2 wwn-0x5000c5002def789e
119 PATH=/bin:/sbin:/usr/bin:/usr/sbin
120 CONFIG=/etc/zfs/vdev_id.conf
131 vdev_id <-d device> [-c config_file] [-p phys_per_port]
132 [-g sas_direct|sas_switch|scsi] [-m]
134 -c specify name of an alternative config file [default=$CONFIG]
135 -d specify basename of device (i.e. sda)
136 -e Create enclose device symlinks only (/dev/by-enclosure)
137 -g Storage network topology [default="$TOPOLOGY"]
138 -m Run in multipath mode
139 -j Run in multijbod mode
140 -p number of phy's per switch port [default=$PHYS_PER_PORT]
150 MAPPED_SLOT=$(awk '$1 == "slot" && $2 == "${LINUX_SLOT}" && \
151 $4 ~ /^${CHANNEL}$|^$/ { print $3; exit}' $CONFIG)
152 if [ -z "$MAPPED_SLOT" ] ; then
153 MAPPED_SLOT=$LINUX_SLOT
155 printf "%d" "${MAPPED_SLOT}"
165 MAPPED_CHAN=$(awk -v port="$PORT" \
166 '$1 == "channel" && $2 == ${PORT} \
167 { print $3; exit }' $CONFIG)
170 MAPPED_CHAN=$(awk -v pciID="$PCI_ID" -v port="$PORT" \
171 '$1 == "channel" && $2 == pciID && $3 == port \
175 printf "%s" "${MAPPED_CHAN}"
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"
192 for uuid in ${ENCL_ID}; do
195 for count in ${UNIQ_ENCL_ID}; do
196 if [ $count = $uuid ]; then
202 if [ $found -eq 0 ]; then
203 UNIQ_ENCL_ID="${UNIQ_ENCL_ID} $uuid"
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.
217 DEVEXP=$(ls -l "/sys/block/$DEV/device/" | grep enclos | awk -F/ '{print $(NF-1) }')
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
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.
232 # Identify final 'mapped jbod'
234 for count in ${UNIQ_ENCL_ID}; do
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
248 printf "%d" "${MAPPED_JBOD}"
252 if [ -z "$PHYS_PER_PORT" ] ; then
253 PHYS_PER_PORT=$(awk '$1 == "phys_per_port" \
254 {print $2; exit}' $CONFIG)
256 PHYS_PER_PORT=${PHYS_PER_PORT:-4}
258 if ! echo "$PHYS_PER_PORT" | grep -q -E '^[0-9]+$' ; then
259 echo "Error: phys_per_port value $PHYS_PER_PORT is non-numeric"
263 if [ -z "$MULTIPATH_MODE" ] ; then
264 MULTIPATH_MODE=$(awk '$1 == "multipath" \
265 {print $2; exit}' $CONFIG)
268 if [ -z "$MULTIJBOD_MODE" ] ; then
269 MULTIJBOD_MODE=$(awk '$1 == "multijbod" \
270 {print $2; exit}' $CONFIG)
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
277 DM_NAME=$(ls -l --full-time /dev/mapper |
278 grep "$DEV"$ | awk '{print $9}')
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
287 if [ "$DEVTYPE" != "partition" ] ; then
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)}')
293 # Strip off partition information.
294 DM_NAME=$(echo "$DM_NAME" | sed 's/p[0-9][0-9]*$//')
295 if [ -z "$DM_NAME" ] ; then
299 # Utilize DM device name to gather subordinate block devices
300 # using sysfs to avoid userspace utilities
302 # If our DEVNAME is something like /dev/dm-177, then we may be
303 # able to get our DMDEV from it.
304 DMDEV=$(echo $DEVNAME | sed 's;/dev/;;g')
305 if [ ! -e /sys/block/$DMDEV/slaves/* ] ; then
306 # It's not there, try looking in /dev/mapper
307 DMDEV=$(ls -l --full-time /dev/mapper | grep $DM_NAME |
308 awk '{gsub("../", " "); print $NF}')
311 # Use sysfs pointers in /sys/block/dm-X/slaves because using
312 # userspace tools creates lots of overhead and should be avoided
313 # whenever possible. Use awk to isolate lowest instance of
314 # sd device member in dm device group regardless of string
316 DEV=$(ls "/sys/block/$DMDEV/slaves" | awk '
317 { len=sprintf ("%20s",length($0)); gsub(/ /,0,str); a[NR]=len "_" $0; }
320 print substr(a[1],22)
323 if [ -z "$DEV" ] ; then
328 if echo "$DEV" | grep -q ^/devices/ ; then
331 sys_path=$(udevadm info -q path -p "/sys/block/$DEV" 2>/dev/null)
334 # Use positional parameters as an ad-hoc array
335 set -- $(echo "$sys_path" | tr / ' ')
339 # Get path up to /sys/.../hostX
342 while [ $i -le "$num_dirs" ] ; do
343 d=$(eval echo '$'{$i})
344 scsi_host_dir="$scsi_host_dir/$d"
345 echo "$d" | grep -q -E '^host[0-9]+$' && break
349 # Lets grab the SAS host channel number and save it for JBOD sorting later
350 HOSTCHAN=$(echo "$d" | awk -F/ '{ gsub("host","",$NF); print $NF}')
352 if [ $i = "$num_dirs" ] ; then
356 PCI_ID=$(eval echo '$'{$((i -1))} | awk -F: '{print $2":"$3}')
358 # In sas_switch mode, the directory four levels beneath
359 # /sys/.../hostX contains symlinks to phy devices that reveal
360 # the switch port number. In sas_direct mode, the phy links one
361 # directory down reveal the HBA port.
362 port_dir=$scsi_host_dir
365 "sas_switch") j=$((i + 4)) ;;
366 "sas_direct") j=$((i + 1)) ;;
371 while [ $i -le $j ] ; do
372 port_dir="$port_dir/$(eval echo '$'{$i})"
376 PHY=$(ls -d "$port_dir"/phy* 2>/dev/null | head -1 | awk -F: '{print $NF}')
377 if [ -z "$PHY" ] ; then
380 PORT=$((PHY / PHYS_PER_PORT))
382 # Look in /sys/.../sas_device/end_device-X for the bay_identifier
384 end_device_dir=$port_dir
386 while [ $i -lt "$num_dirs" ] ; do
387 d=$(eval echo '$'{$i})
388 end_device_dir="$end_device_dir/$d"
389 if echo "$d" | grep -q '^end_device' ; then
390 end_device_dir="$end_device_dir/sas_device/$d"
396 # Add 'mix' slot type for environments where dm-multipath devices
397 # include end-devices connected via SAS expanders or direct connection
398 # to SAS HBA. A mixed connectivity environment such as pool devices
399 # contained in a SAS JBOD and spare drives or log devices directly
400 # connected in a server backplane without expanders in the I/O path.
405 SLOT=$(cat "$end_device_dir/bay_identifier" 2>/dev/null)
408 if [ $(cat "$end_device_dir/bay_identifier" 2>/dev/null) ] ; then
409 SLOT=$(cat "$end_device_dir/bay_identifier" 2>/dev/null)
411 SLOT=$(cat "$end_device_dir/phy_identifier" 2>/dev/null)
415 SLOT=$(cat "$end_device_dir/phy_identifier" 2>/dev/null)
418 d=$(eval echo '$'{$i})
419 SLOT=$(echo "$d" | sed -e 's/^.*://')
423 d=$(eval echo '$'{$i})
424 SLOT=$(echo "$d" | sed -e 's/^.*://')
428 d=$(eval echo '$'{$i})
429 SLOT=$(echo "$d" | sed -e 's/^.*://')
432 # look for this SAS path in all SCSI Enclosure Services
434 sas_address=$(cat "$end_device_dir/sas_address" 2>/dev/null)
435 enclosures=$(lsscsi -g | \
436 sed -n -e '/enclosu/s/^.* \([^ ][^ ]*\) *$/\1/p')
437 for enclosure in $enclosures; do
438 set -- $(sg_ses -p aes "$enclosure" | \
439 awk "/device slot number:/{slot=\$12} \
440 /SAS address: $sas_address/\
443 if [ -n "$SLOT" ] ; then
449 if [ -z "$SLOT" ] ; then
453 if [ "$MULTIJBOD_MODE" = "yes" ] ; then
454 CHAN=$(map_channel "$PCI_ID" "$PORT")
455 SLOT=$(map_slot "$SLOT" "$CHAN")
456 JBOD=$(map_jbod "$DEV")
458 if [ -z "$CHAN" ] ; then
461 echo "${CHAN}"-"${JBOD}"-"${SLOT}${PART}"
463 CHAN=$(map_channel "$PCI_ID" "$PORT")
464 SLOT=$(map_slot "$SLOT" "$CHAN")
466 if [ -z "$CHAN" ] ; then
469 echo "${CHAN}${SLOT}${PART}"
474 if [ -z "$FIRST_BAY_NUMBER" ] ; then
475 FIRST_BAY_NUMBER=$(awk '$1 == "first_bay_number" \
476 {print $2; exit}' $CONFIG)
478 FIRST_BAY_NUMBER=${FIRST_BAY_NUMBER:-0}
480 if [ -z "$PHYS_PER_PORT" ] ; then
481 PHYS_PER_PORT=$(awk '$1 == "phys_per_port" \
482 {print $2; exit}' $CONFIG)
484 PHYS_PER_PORT=${PHYS_PER_PORT:-4}
486 if ! echo "$PHYS_PER_PORT" | grep -q -E '^[0-9]+$' ; then
487 echo "Error: phys_per_port value $PHYS_PER_PORT is non-numeric"
491 if [ -z "$MULTIPATH_MODE" ] ; then
492 MULTIPATH_MODE=$(awk '$1 == "multipath" \
493 {print $2; exit}' $CONFIG)
496 # Use first running component device if we're handling a dm-mpath device
497 if [ "$MULTIPATH_MODE" = "yes" ] ; then
498 # If udev didn't tell us the UUID via DM_NAME, check /dev/mapper
499 if [ -z "$DM_NAME" ] ; then
500 DM_NAME=$(ls -l --full-time /dev/mapper |
501 grep "$DEV"$ | awk '{print $9}')
504 # For raw disks udev exports DEVTYPE=partition when
505 # handling partitions, and the rules can be written to
506 # take advantage of this to append a -part suffix. For
507 # dm devices we get DEVTYPE=disk even for partitions so
508 # we have to append the -part suffix directly in the
510 if [ "$DEVTYPE" != "partition" ] ; then
511 # Match p[number], remove the 'p' and prepend "-part"
512 PART=$(echo "$DM_NAME" |
513 awk 'match($0,/p[0-9]+$/) {print "-part"substr($0,RSTART+1,RLENGTH-1)}')
516 # Strip off partition information.
517 DM_NAME=$(echo "$DM_NAME" | sed 's/p[0-9][0-9]*$//')
518 if [ -z "$DM_NAME" ] ; then
522 # Get the raw scsi device name from multipath -ll. Strip off
523 # leading pipe symbols to make field numbering consistent.
524 DEV=$(multipath -ll "$DM_NAME" |
525 awk '/running/{gsub("^[|]"," "); print $3 ; exit}')
526 if [ -z "$DEV" ] ; then
531 if echo "$DEV" | grep -q ^/devices/ ; then
534 sys_path=$(udevadm info -q path -p "/sys/block/$DEV" 2>/dev/null)
537 # expect sys_path like this, for example:
538 # /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
540 # Use positional parameters as an ad-hoc array
541 set -- $(echo "$sys_path" | tr / ' ')
545 # Get path up to /sys/.../hostX
548 while [ $i -le "$num_dirs" ] ; do
549 d=$(eval echo '$'{$i})
550 scsi_host_dir="$scsi_host_dir/$d"
552 echo "$d" | grep -q -E '^host[0-9]+$' && break
556 if [ $i = "$num_dirs" ] ; then
560 PCI_ID=$(eval echo '$'{$((i -1))} | awk -F: '{print $2":"$3}')
562 # In scsi mode, the directory two levels beneath
563 # /sys/.../hostX reveals the port and slot.
564 port_dir=$scsi_host_dir
568 while [ $i -le $j ] ; do
569 port_dir="$port_dir/$(eval echo '$'{$i})"
573 set -- $(echo "$port_dir" | sed -e 's/^.*:\([^:]*\):\([^:]*\)$/\1 \2/')
575 SLOT=$(($2 + FIRST_BAY_NUMBER))
577 if [ -z "$SLOT" ] ; then
581 CHAN=$(map_channel "$PCI_ID" "$PORT")
582 SLOT=$(map_slot "$SLOT" "$CHAN")
584 if [ -z "$CHAN" ] ; then
587 echo "${CHAN}${SLOT}${PART}"
590 # Figure out the name for the enclosure symlink
591 enclosure_handler () {
592 # We get all the info we need from udev's DEVPATH variable:
594 # DEVPATH=/sys/devices/pci0000:00/0000:00:03.0/0000:05:00.0/host0/subsystem/devices/0:0:0:0/scsi_generic/sg0
596 # Get the enclosure ID ("0:0:0:0")
597 ENC=$(basename $(readlink -m "/sys/$DEVPATH/../.."))
598 if [ ! -d "/sys/class/enclosure/$ENC" ] ; then
599 # Not an enclosure, bail out
603 # Get the long sysfs device path to our enclosure. Looks like:
604 # /devices/pci0000:00/0000:00:03.0/0000:05:00.0/host0/port-0:0/ ... /enclosure/0:0:0:0
606 ENC_DEVICE=$(readlink "/sys/class/enclosure/$ENC")
608 # Grab the full path to the hosts port dir:
609 # /devices/pci0000:00/0000:00:03.0/0000:05:00.0/host0/port-0:0
610 PORT_DIR=$(echo "$ENC_DEVICE" | grep -Eo '.+host[0-9]+/port-[0-9]+:[0-9]+')
612 # Get the port number
613 PORT_ID=$(echo "$PORT_DIR" | grep -Eo "[0-9]+$")
615 # The PCI directory is two directories up from the port directory
616 # /sys/devices/pci0000:00/0000:00:03.0/0000:05:00.0
617 PCI_ID_LONG=$(basename $(readlink -m "/sys/$PORT_DIR/../.."))
619 # Strip down the PCI address from 0000:05:00.0 to 05:00.0
620 PCI_ID=$(echo "$PCI_ID_LONG" | sed -r 's/^[0-9]+://g')
622 # Name our device according to vdev_id.conf (like "L0" or "U1").
623 NAME=$(awk '/channel/{if ($1 == "channel" && $2 == "$PCI_ID" && \
624 $3 == "$PORT_ID") {print ${4}int(count[$4])}; count[$4]++}' $CONFIG)
630 # Special handling is needed to correctly append a -part suffix
631 # to partitions of device mapper devices. The DEVTYPE attribute
632 # is normally set to "disk" instead of "partition" in this case,
633 # so the udev rules won't handle that for us as they do for
634 # "plain" block devices.
636 # For example, we may have the following links for a device and its
639 # /dev/disk/by-id/dm-name-isw_dibgbfcije_ARRAY0 -> ../../dm-0
640 # /dev/disk/by-id/dm-name-isw_dibgbfcije_ARRAY0p1 -> ../../dm-1
641 # /dev/disk/by-id/dm-name-isw_dibgbfcije_ARRAY0p2 -> ../../dm-3
643 # and the following alias in vdev_id.conf.
645 # alias A0 dm-name-isw_dibgbfcije_ARRAY0
647 # The desired outcome is for the following links to be created
648 # without having explicitly defined aliases for the partitions.
650 # /dev/disk/by-vdev/A0 -> ../../dm-0
651 # /dev/disk/by-vdev/A0-part1 -> ../../dm-1
652 # /dev/disk/by-vdev/A0-part2 -> ../../dm-3
654 # Warning: The following grep pattern will misidentify whole-disk
655 # devices whose names end with 'p' followed by a string of
656 # digits as partitions, causing alias creation to fail. This
657 # ambiguity seems unavoidable, so devices using this facility
658 # must not use such names.
660 if echo "$DM_NAME" | grep -q -E 'p[0-9][0-9]*$' ; then
661 if [ "$DEVTYPE" != "partition" ] ; then
662 # Match p[number], remove the 'p' and prepend "-part"
663 DM_PART=$(echo "$DM_NAME" |
664 awk 'match($0,/p[0-9]+$/) {print "-part"substr($0,RSTART+1,RLENGTH-1)}')
668 # DEVLINKS attribute must have been populated by already-run udev rules.
669 for link in $DEVLINKS ; do
670 # Remove partition information to match key of top-level device.
671 if [ -n "$DM_PART" ] ; then
672 link=$(echo "$link" | sed 's/p[0-9][0-9]*$//')
674 # Check both the fully qualified and the base name of link.
675 for l in $link $(basename "$link") ; do
676 if [ ! -z "$l" ]; then
677 alias=$(awk -v var="$l" '($1 == "alias") && \
679 { print $2; exit }' $CONFIG)
680 if [ -n "$alias" ] ; then
681 echo "${alias}${DM_PART}"
690 while getopts 'c:d:eg:jmp:h' OPTION; do
699 # When udev sees a scsi_generic device, it calls this script with -e to
700 # create the enclosure device symlinks only. We also need
701 # "enclosure_symlinks yes" set in vdev_id.config to actually create the
703 ENCLOSURE_MODE=$(awk '{if ($1 == "enclosure_symlinks") \
704 print $2}' "$CONFIG")
706 if [ "$ENCLOSURE_MODE" != "yes" ] ; then
714 PHYS_PER_PORT=${OPTARG}
728 if [ ! -r "$CONFIG" ] ; then
729 echo "Error: Config file \"$CONFIG\" not found"
733 if [ -z "$DEV" ] && [ -z "$ENCLOSURE_MODE" ] ; then
734 echo "Error: missing required option -d"
738 if [ -z "$TOPOLOGY" ] ; then
739 TOPOLOGY=$(awk '($1 == "topology") {print $2; exit}' "$CONFIG")
742 if [ -z "$BAY" ] ; then
743 BAY=$(awk '($1 == "slot") {print $2; exit}' "$CONFIG")
746 TOPOLOGY=${TOPOLOGY:-sas_direct}
748 # Should we create /dev/by-enclosure symlinks?
749 if [ "$ENCLOSURE_MODE" = "yes" ] && [ "$TOPOLOGY" = "sas_direct" ] ; then
750 ID_ENCLOSURE=$(enclosure_handler)
751 if [ -z "$ID_ENCLOSURE" ] ; then
755 # Just create the symlinks to the enclosure devices and then exit.
756 ENCLOSURE_PREFIX=$(awk '/enclosure_symlinks_prefix/{print $2}' "$CONFIG")
757 if [ -z "$ENCLOSURE_PREFIX" ] ; then
758 ENCLOSURE_PREFIX="enc"
760 echo "ID_ENCLOSURE=$ID_ENCLOSURE"
761 echo "ID_ENCLOSURE_PATH=by-enclosure/$ENCLOSURE_PREFIX-$ID_ENCLOSURE"
765 # First check if an alias was defined for this device.
766 ID_VDEV=$(alias_handler)
768 if [ -z "$ID_VDEV" ] ; then
771 sas_direct|sas_switch)
772 ID_VDEV=$(sas_handler)
775 ID_VDEV=$(scsi_handler)
778 echo "Error: unknown topology $TOPOLOGY"
784 if [ -n "$ID_VDEV" ] ; then
785 echo "ID_VDEV=${ID_VDEV}"
786 echo "ID_VDEV_PATH=disk/by-vdev/${ID_VDEV}"