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]
144 # exit with error to avoid processing usage message by a udev rule
151 MAPPED_SLOT
=$
(awk -v linux_slot
="$LINUX_SLOT" -v channel
="$CHANNEL" \
152 '$1 == "slot" && $2 == linux_slot && \
153 ($4 ~ "^"channel"$" || $4 ~ /^$/) { print $3; exit}' $CONFIG)
154 if [ -z "$MAPPED_SLOT" ] ; then
155 MAPPED_SLOT
=$LINUX_SLOT
157 printf "%d" "${MAPPED_SLOT}"
167 MAPPED_CHAN
=$
(awk -v port
="$PORT" \
168 '$1 == "channel" && $2 == port \
169 { print $3; exit }' $CONFIG)
172 MAPPED_CHAN
=$
(awk -v pciID
="$PCI_ID" -v port
="$PORT" \
173 '$1 == "channel" && $2 == pciID && $3 == port \
177 printf "%s" "${MAPPED_CHAN}"
185 while [ $i -le $count ] ; do
186 d
=$
(eval echo '$'{$i})
187 id
=$
(cat "/sys/class/enclosure/${d}/id")
188 ENCL_ID
="${ENCL_ID} $id"
194 for uuid
in ${ENCL_ID}; do
197 for count
in ${UNIQ_ENCL_ID}; do
198 if [ $count = $uuid ]; then
204 if [ $found -eq 0 ]; then
205 UNIQ_ENCL_ID
="${UNIQ_ENCL_ID} $uuid"
210 # map_jbod explainer: The bsg driver knows the difference between a SAS
211 # expander and fanout expander. Use hostX instance along with top-level
212 # (whole enclosure) expander instances in /sys/class/enclosure and
213 # matching a field in an array of expanders, using the index of the
214 # matched array field as the enclosure instance, thereby making jbod IDs
215 # dynamic. Avoids reliance on high overhead userspace commands like
216 # multipath and lsscsi and instead uses existing sysfs data. $HOSTCHAN
217 # variable derived from devpath gymnastics in sas_handler() function.
219 DEVEXP
=$
(ls -l "/sys/block/$DEV/device/" |
grep enclos |
awk -F/ '{print $(NF-1) }')
222 # Use "set --" to create index values (Arrays)
223 set -- $
(ls -l /sys
/class
/enclosure |
grep -v "^total" |
awk '{print $9}')
224 # Get count of total elements
228 # Build JBODs (enclosure) id from sys/class/enclosure/<dev>/id
229 get_encl_id
"$JBOD_ITEM"
230 # Different expander instances for each paths.
231 # Filter out and keep only unique id.
234 # Identify final 'mapped jbod'
236 for count
in ${UNIQ_ENCL_ID}; do
239 while [ $i -le $JBOD_COUNT ] ; do
240 d
=$
(eval echo '$'{$i})
241 id
=$
(cat "/sys/class/enclosure/${d}/id")
242 if [ "$d" = "$DEVEXP" ] && [ $id = $count ] ; then
250 printf "%d" "${MAPPED_JBOD}"
254 if [ -z "$PHYS_PER_PORT" ] ; then
255 PHYS_PER_PORT
=$
(awk '$1 == "phys_per_port" \
256 {print $2; exit}' $CONFIG)
258 PHYS_PER_PORT
=${PHYS_PER_PORT:-4}
260 if ! echo "$PHYS_PER_PORT" |
grep -q -E '^[0-9]+$' ; then
261 echo "Error: phys_per_port value $PHYS_PER_PORT is non-numeric"
265 if [ -z "$MULTIPATH_MODE" ] ; then
266 MULTIPATH_MODE
=$
(awk '$1 == "multipath" \
267 {print $2; exit}' $CONFIG)
270 if [ -z "$MULTIJBOD_MODE" ] ; then
271 MULTIJBOD_MODE
=$
(awk '$1 == "multijbod" \
272 {print $2; exit}' $CONFIG)
275 # Use first running component device if we're handling a dm-mpath device
276 if [ "$MULTIPATH_MODE" = "yes" ] ; then
277 # If udev didn't tell us the UUID via DM_NAME, check /dev/mapper
278 if [ -z "$DM_NAME" ] ; then
279 DM_NAME
=$
(ls -l --full-time /dev
/mapper |
280 grep "$DEV"$ |
awk '{print $9}')
283 # For raw disks udev exports DEVTYPE=partition when
284 # handling partitions, and the rules can be written to
285 # take advantage of this to append a -part suffix. For
286 # dm devices we get DEVTYPE=disk even for partitions so
287 # we have to append the -part suffix directly in the
289 if [ "$DEVTYPE" != "partition" ] ; then
290 # Match p[number], remove the 'p' and prepend "-part"
291 PART
=$
(echo "$DM_NAME" |
292 awk 'match($0,/p[0-9]+$/) {print "-part"substr($0,RSTART+1,RLENGTH-1)}')
295 # Strip off partition information.
296 DM_NAME
=$
(echo "$DM_NAME" |
sed 's/p[0-9][0-9]*$//')
297 if [ -z "$DM_NAME" ] ; then
301 # Utilize DM device name to gather subordinate block devices
302 # using sysfs to avoid userspace utilities
304 # If our DEVNAME is something like /dev/dm-177, then we may be
305 # able to get our DMDEV from it.
306 DMDEV
=$
(echo $DEVNAME |
sed 's;/dev/;;g')
307 if [ ! -e /sys
/block
/$DMDEV/slaves
/* ] ; then
308 # It's not there, try looking in /dev/mapper
309 DMDEV
=$
(ls -l --full-time /dev
/mapper |
grep $DM_NAME |
310 awk '{gsub("../", " "); print $NF}')
313 # Use sysfs pointers in /sys/block/dm-X/slaves because using
314 # userspace tools creates lots of overhead and should be avoided
315 # whenever possible. Use awk to isolate lowest instance of
316 # sd device member in dm device group regardless of string
318 DEV
=$
(ls "/sys/block/$DMDEV/slaves" |
awk '
319 { len=sprintf ("%20s",length($0)); gsub(/ /,0,str); a[NR]=len "_" $0; }
322 print substr(a[1],22)
325 if [ -z "$DEV" ] ; then
330 if echo "$DEV" |
grep -q ^
/devices
/ ; then
333 sys_path
=$
(udevadm info
-q path
-p "/sys/block/$DEV" 2>/dev
/null
)
336 # Use positional parameters as an ad-hoc array
337 set -- $
(echo "$sys_path" |
tr / ' ')
341 # Get path up to /sys/.../hostX
344 while [ $i -le "$num_dirs" ] ; do
345 d
=$
(eval echo '$'{$i})
346 scsi_host_dir
="$scsi_host_dir/$d"
347 echo "$d" |
grep -q -E '^host[0-9]+$' && break
351 # Lets grab the SAS host channel number and save it for JBOD sorting later
352 HOSTCHAN
=$
(echo "$d" |
awk -F/ '{ gsub("host","",$NF); print $NF}')
354 if [ $i = "$num_dirs" ] ; then
358 PCI_ID
=$
(eval echo '$'{$
((i
-1))} |
awk -F: '{print $2":"$3}')
360 # In sas_switch mode, the directory four levels beneath
361 # /sys/.../hostX contains symlinks to phy devices that reveal
362 # the switch port number. In sas_direct mode, the phy links one
363 # directory down reveal the HBA port.
364 port_dir
=$scsi_host_dir
367 "sas_switch") j
=$
((i
+ 4)) ;;
368 "sas_direct") j
=$
((i
+ 1)) ;;
373 while [ $i -le $j ] ; do
374 port_dir
="$port_dir/$(eval echo '$'{$i})"
378 PHY
=$
(ls -vd "$port_dir"/phy
* 2>/dev
/null |
head -1 |
awk -F: '{print $NF}')
379 if [ -z "$PHY" ] ; then
382 PORT
=$
((PHY
/ PHYS_PER_PORT
))
384 # Look in /sys/.../sas_device/end_device-X for the bay_identifier
386 end_device_dir
=$port_dir
388 while [ $i -lt "$num_dirs" ] ; do
389 d
=$
(eval echo '$'{$i})
390 end_device_dir
="$end_device_dir/$d"
391 if echo "$d" |
grep -q '^end_device' ; then
392 end_device_dir
="$end_device_dir/sas_device/$d"
398 # Add 'mix' slot type for environments where dm-multipath devices
399 # include end-devices connected via SAS expanders or direct connection
400 # to SAS HBA. A mixed connectivity environment such as pool devices
401 # contained in a SAS JBOD and spare drives or log devices directly
402 # connected in a server backplane without expanders in the I/O path.
407 SLOT
=$
(cat "$end_device_dir/bay_identifier" 2>/dev
/null
)
410 if [ $
(cat "$end_device_dir/bay_identifier" 2>/dev
/null
) ] ; then
411 SLOT
=$
(cat "$end_device_dir/bay_identifier" 2>/dev
/null
)
413 SLOT
=$
(cat "$end_device_dir/phy_identifier" 2>/dev
/null
)
417 SLOT
=$
(cat "$end_device_dir/phy_identifier" 2>/dev
/null
)
420 d
=$
(eval echo '$'{$i})
421 SLOT
=$
(echo "$d" |
sed -e 's/^.*://')
425 d
=$
(eval echo '$'{$i})
426 SLOT
=$
(echo "$d" |
sed -e 's/^.*://')
430 d
=$
(eval echo '$'{$i})
431 SLOT
=$
(echo "$d" |
sed -e 's/^.*://')
434 # look for this SAS path in all SCSI Enclosure Services
436 sas_address
=$
(cat "$end_device_dir/sas_address" 2>/dev
/null
)
437 enclosures
=$
(lsscsi
-g | \
438 sed -n -e '/enclosu/s/^.* \([^ ][^ ]*\) *$/\1/p')
439 for enclosure
in $enclosures; do
440 set -- $
(sg_ses
-p aes
"$enclosure" | \
441 awk "/device slot number:/{slot=\$12} \
442 /SAS address: $sas_address/\
445 if [ -n "$SLOT" ] ; then
451 if [ -z "$SLOT" ] ; then
455 if [ "$MULTIJBOD_MODE" = "yes" ] ; then
456 CHAN
=$
(map_channel
"$PCI_ID" "$PORT")
457 SLOT
=$
(map_slot
"$SLOT" "$CHAN")
458 JBOD
=$
(map_jbod
"$DEV")
460 if [ -z "$CHAN" ] ; then
463 echo "${CHAN}"-"${JBOD}"-"${SLOT}${PART}"
465 CHAN
=$
(map_channel
"$PCI_ID" "$PORT")
466 SLOT
=$
(map_slot
"$SLOT" "$CHAN")
468 if [ -z "$CHAN" ] ; then
471 echo "${CHAN}${SLOT}${PART}"
476 if [ -z "$FIRST_BAY_NUMBER" ] ; then
477 FIRST_BAY_NUMBER
=$
(awk '$1 == "first_bay_number" \
478 {print $2; exit}' $CONFIG)
480 FIRST_BAY_NUMBER
=${FIRST_BAY_NUMBER:-0}
482 if [ -z "$PHYS_PER_PORT" ] ; then
483 PHYS_PER_PORT
=$
(awk '$1 == "phys_per_port" \
484 {print $2; exit}' $CONFIG)
486 PHYS_PER_PORT
=${PHYS_PER_PORT:-4}
488 if ! echo "$PHYS_PER_PORT" |
grep -q -E '^[0-9]+$' ; then
489 echo "Error: phys_per_port value $PHYS_PER_PORT is non-numeric"
493 if [ -z "$MULTIPATH_MODE" ] ; then
494 MULTIPATH_MODE
=$
(awk '$1 == "multipath" \
495 {print $2; exit}' $CONFIG)
498 # Use first running component device if we're handling a dm-mpath device
499 if [ "$MULTIPATH_MODE" = "yes" ] ; then
500 # If udev didn't tell us the UUID via DM_NAME, check /dev/mapper
501 if [ -z "$DM_NAME" ] ; then
502 DM_NAME
=$
(ls -l --full-time /dev
/mapper |
503 grep "$DEV"$ |
awk '{print $9}')
506 # For raw disks udev exports DEVTYPE=partition when
507 # handling partitions, and the rules can be written to
508 # take advantage of this to append a -part suffix. For
509 # dm devices we get DEVTYPE=disk even for partitions so
510 # we have to append the -part suffix directly in the
512 if [ "$DEVTYPE" != "partition" ] ; then
513 # Match p[number], remove the 'p' and prepend "-part"
514 PART
=$
(echo "$DM_NAME" |
515 awk 'match($0,/p[0-9]+$/) {print "-part"substr($0,RSTART+1,RLENGTH-1)}')
518 # Strip off partition information.
519 DM_NAME
=$
(echo "$DM_NAME" |
sed 's/p[0-9][0-9]*$//')
520 if [ -z "$DM_NAME" ] ; then
524 # Get the raw scsi device name from multipath -ll. Strip off
525 # leading pipe symbols to make field numbering consistent.
526 DEV
=$
(multipath
-ll "$DM_NAME" |
527 awk '/running/{gsub("^[|]"," "); print $3 ; exit}')
528 if [ -z "$DEV" ] ; then
533 if echo "$DEV" |
grep -q ^
/devices
/ ; then
536 sys_path
=$
(udevadm info
-q path
-p "/sys/block/$DEV" 2>/dev
/null
)
539 # expect sys_path like this, for example:
540 # /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
542 # Use positional parameters as an ad-hoc array
543 set -- $
(echo "$sys_path" |
tr / ' ')
547 # Get path up to /sys/.../hostX
550 while [ $i -le "$num_dirs" ] ; do
551 d
=$
(eval echo '$'{$i})
552 scsi_host_dir
="$scsi_host_dir/$d"
554 echo "$d" |
grep -q -E '^host[0-9]+$' && break
558 if [ $i = "$num_dirs" ] ; then
562 PCI_ID
=$
(eval echo '$'{$
((i
-1))} |
awk -F: '{print $2":"$3}')
564 # In scsi mode, the directory two levels beneath
565 # /sys/.../hostX reveals the port and slot.
566 port_dir
=$scsi_host_dir
570 while [ $i -le $j ] ; do
571 port_dir
="$port_dir/$(eval echo '$'{$i})"
575 set -- $
(echo "$port_dir" |
sed -e 's/^.*:\([^:]*\):\([^:]*\)$/\1 \2/')
577 SLOT
=$
(($2 + FIRST_BAY_NUMBER
))
579 if [ -z "$SLOT" ] ; then
583 CHAN
=$
(map_channel
"$PCI_ID" "$PORT")
584 SLOT
=$
(map_slot
"$SLOT" "$CHAN")
586 if [ -z "$CHAN" ] ; then
589 echo "${CHAN}${SLOT}${PART}"
592 # Figure out the name for the enclosure symlink
593 enclosure_handler
() {
594 # We get all the info we need from udev's DEVPATH variable:
596 # DEVPATH=/sys/devices/pci0000:00/0000:00:03.0/0000:05:00.0/host0/subsystem/devices/0:0:0:0/scsi_generic/sg0
598 # Get the enclosure ID ("0:0:0:0")
602 if [ ! -d "/sys/class/enclosure/$ENC" ] ; then
603 # Not an enclosure, bail out
607 # Get the long sysfs device path to our enclosure. Looks like:
608 # /devices/pci0000:00/0000:00:03.0/0000:05:00.0/host0/port-0:0/ ... /enclosure/0:0:0:0
610 ENC_DEVICE
=$
(readlink
"/sys/class/enclosure/$ENC")
612 # Grab the full path to the hosts port dir:
613 # /devices/pci0000:00/0000:00:03.0/0000:05:00.0/host0/port-0:0
614 PORT_DIR
=$
(echo "$ENC_DEVICE" |
grep -Eo '.+host[0-9]+/port-[0-9]+:[0-9]+')
616 # Get the port number
617 PORT_ID
=$
(echo "$PORT_DIR" |
grep -Eo "[0-9]+$")
619 # The PCI directory is two directories up from the port directory
620 # /sys/devices/pci0000:00/0000:00:03.0/0000:05:00.0
621 PCI_ID_LONG
="$(readlink -m "/sys
/$PORT_DIR/..
/..
")"
622 PCI_ID_LONG
="${PCI_ID_LONG##*/}"
624 # Strip down the PCI address from 0000:05:00.0 to 05:00.0
625 PCI_ID
="${PCI_ID_LONG#[0-9]*:}"
627 # Name our device according to vdev_id.conf (like "L0" or "U1").
628 NAME
=$
(awk "/channel/{if (\$1 == \"channel\" && \$2 == \"$PCI_ID\" && \
629 \$3 == \"$PORT_ID\") {print \$4\$3}}" $CONFIG)
635 # Special handling is needed to correctly append a -part suffix
636 # to partitions of device mapper devices. The DEVTYPE attribute
637 # is normally set to "disk" instead of "partition" in this case,
638 # so the udev rules won't handle that for us as they do for
639 # "plain" block devices.
641 # For example, we may have the following links for a device and its
644 # /dev/disk/by-id/dm-name-isw_dibgbfcije_ARRAY0 -> ../../dm-0
645 # /dev/disk/by-id/dm-name-isw_dibgbfcije_ARRAY0p1 -> ../../dm-1
646 # /dev/disk/by-id/dm-name-isw_dibgbfcije_ARRAY0p2 -> ../../dm-3
648 # and the following alias in vdev_id.conf.
650 # alias A0 dm-name-isw_dibgbfcije_ARRAY0
652 # The desired outcome is for the following links to be created
653 # without having explicitly defined aliases for the partitions.
655 # /dev/disk/by-vdev/A0 -> ../../dm-0
656 # /dev/disk/by-vdev/A0-part1 -> ../../dm-1
657 # /dev/disk/by-vdev/A0-part2 -> ../../dm-3
659 # Warning: The following grep pattern will misidentify whole-disk
660 # devices whose names end with 'p' followed by a string of
661 # digits as partitions, causing alias creation to fail. This
662 # ambiguity seems unavoidable, so devices using this facility
663 # must not use such names.
665 if echo "$DM_NAME" |
grep -q -E 'p[0-9][0-9]*$' ; then
666 if [ "$DEVTYPE" != "partition" ] ; then
667 # Match p[number], remove the 'p' and prepend "-part"
668 DM_PART
=$
(echo "$DM_NAME" |
669 awk 'match($0,/p[0-9]+$/) {print "-part"substr($0,RSTART+1,RLENGTH-1)}')
673 # DEVLINKS attribute must have been populated by already-run udev rules.
674 for link
in $DEVLINKS ; do
675 # Remove partition information to match key of top-level device.
676 if [ -n "$DM_PART" ] ; then
677 link
=$
(echo "$link" |
sed 's/p[0-9][0-9]*$//')
679 # Check both the fully qualified and the base name of link.
680 for l
in $link ${link##*/} ; do
681 if [ ! -z "$l" ]; then
682 alias=$
(awk -v var
="$l" '($1 == "alias") && \
684 { print $2; exit }' $CONFIG)
685 if [ -n "$alias" ] ; then
686 echo "${alias}${DM_PART}"
695 while getopts 'c:d:eg:jmp:h' OPTION
; do
704 # When udev sees a scsi_generic device, it calls this script with -e to
705 # create the enclosure device symlinks only. We also need
706 # "enclosure_symlinks yes" set in vdev_id.config to actually create the
708 ENCLOSURE_MODE
=$
(awk '{if ($1 == "enclosure_symlinks") \
709 print $2}' "$CONFIG")
711 if [ "$ENCLOSURE_MODE" != "yes" ] ; then
719 PHYS_PER_PORT
=${OPTARG}
733 if [ ! -r "$CONFIG" ] ; then
734 echo "Error: Config file \"$CONFIG\" not found"
738 if [ -z "$DEV" ] && [ -z "$ENCLOSURE_MODE" ] ; then
739 echo "Error: missing required option -d"
743 if [ -z "$TOPOLOGY" ] ; then
744 TOPOLOGY
=$
(awk '($1 == "topology") {print $2; exit}' "$CONFIG")
747 if [ -z "$BAY" ] ; then
748 BAY
=$
(awk '($1 == "slot") {print $2; exit}' "$CONFIG")
751 TOPOLOGY
=${TOPOLOGY:-sas_direct}
753 # Should we create /dev/by-enclosure symlinks?
754 if [ "$ENCLOSURE_MODE" = "yes" ] && [ "$TOPOLOGY" = "sas_direct" ] ; then
755 ID_ENCLOSURE
=$
(enclosure_handler
)
756 if [ -z "$ID_ENCLOSURE" ] ; then
760 # Just create the symlinks to the enclosure devices and then exit.
761 ENCLOSURE_PREFIX
=$
(awk '/enclosure_symlinks_prefix/{print $2}' "$CONFIG")
762 if [ -z "$ENCLOSURE_PREFIX" ] ; then
763 ENCLOSURE_PREFIX
="enc"
765 echo "ID_ENCLOSURE=$ID_ENCLOSURE"
766 echo "ID_ENCLOSURE_PATH=by-enclosure/$ENCLOSURE_PREFIX-$ID_ENCLOSURE"
770 # First check if an alias was defined for this device.
771 ID_VDEV
=$
(alias_handler
)
773 if [ -z "$ID_VDEV" ] ; then
776 sas_direct|sas_switch
)
777 ID_VDEV
=$
(sas_handler
)
780 ID_VDEV
=$
(scsi_handler
)
783 echo "Error: unknown topology $TOPOLOGY"
789 if [ -n "$ID_VDEV" ] ; then
790 echo "ID_VDEV=${ID_VDEV}"
791 echo "ID_VDEV_PATH=disk/by-vdev/${ID_VDEV}"