]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - cmd/vdev_id/vdev_id
vdev_id: Create symlinks even if no /dev/mapper/
[FreeBSD/FreeBSD.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 - 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
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
119 PATH=/bin:/sbin:/usr/bin:/usr/sbin
120 CONFIG=/etc/zfs/vdev_id.conf
121 PHYS_PER_PORT=
122 DEV=
123 TOPOLOGY=
124 BAY=
125 ENCL_ID=""
126 UNIQ_ENCL_ID=""
127
128 usage() {
129         cat << EOF
130 Usage: vdev_id [-h]
131        vdev_id <-d device> [-c config_file] [-p phys_per_port]
132                [-g sas_direct|sas_switch|scsi] [-m]
133
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]
141   -h    show this summary
142 EOF
143         exit 0
144 }
145
146 map_slot() {
147         LINUX_SLOT=$1
148         CHANNEL=$2
149
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
154         fi
155         printf "%d" "${MAPPED_SLOT}"
156 }
157
158 map_channel() {
159         MAPPED_CHAN=
160         PCI_ID=$1
161         PORT=$2
162
163         case $TOPOLOGY in
164                 "sas_switch")
165                 MAPPED_CHAN=$(awk -v port="$PORT" \
166                         '$1 == "channel" && $2 == ${PORT} \
167                         { print $3; exit }' $CONFIG)
168                 ;;
169                 "sas_direct"|"scsi")
170                 MAPPED_CHAN=$(awk -v pciID="$PCI_ID" -v port="$PORT" \
171                         '$1 == "channel" && $2 == pciID && $3 == port \
172                         {print $4}' $CONFIG)
173                 ;;
174         esac
175         printf "%s" "${MAPPED_CHAN}"
176 }
177
178 get_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
191 get_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.
216 map_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}"
249 }
250
251 sas_handler() {
252         if [ -z "$PHYS_PER_PORT" ] ; then
253                 PHYS_PER_PORT=$(awk '$1 == "phys_per_port" \
254                         {print $2; exit}' $CONFIG)
255         fi
256         PHYS_PER_PORT=${PHYS_PER_PORT:-4}
257
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"
260                 exit 1
261         fi
262
263         if [ -z "$MULTIPATH_MODE" ] ; then
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)
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
277                         DM_NAME=$(ls -l --full-time /dev/mapper |
278                                 grep "$DEV"$ | awk '{print $9}')
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
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)}')
291                 fi
292
293                 # Strip off partition information.
294                 DM_NAME=$(echo "$DM_NAME" | sed 's/p[0-9][0-9]*$//')
295                 if [ -z "$DM_NAME" ] ; then
296                         return
297                 fi
298
299                 # Utilize DM device name to gather subordinate block devices
300                 # using sysfs to avoid userspace utilities
301
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}')
309                 fi
310
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
315                 # length.
316                 DEV=$(ls "/sys/block/$DMDEV/slaves" | awk '
317                         { len=sprintf ("%20s",length($0)); gsub(/ /,0,str); a[NR]=len "_" $0; }
318                         END {
319                                 asort(a)
320                                 print substr(a[1],22)
321                         }')
322
323                 if [ -z "$DEV" ] ; then
324                         return
325                 fi
326         fi
327
328         if echo "$DEV" | grep -q ^/devices/ ; then
329                 sys_path=$DEV
330         else
331                 sys_path=$(udevadm info -q path -p "/sys/block/$DEV" 2>/dev/null)
332         fi
333
334         # Use positional parameters as an ad-hoc array
335         set -- $(echo "$sys_path" | tr / ' ')
336         num_dirs=$#
337         scsi_host_dir="/sys"
338
339         # Get path up to /sys/.../hostX
340         i=1
341
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
346                 i=$((i + 1))
347         done
348
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}')
351
352         if [ $i = "$num_dirs" ] ; then
353                 return
354         fi
355
356         PCI_ID=$(eval echo '$'{$((i -1))} | awk -F: '{print $2":"$3}')
357
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
363
364         case $TOPOLOGY in
365                 "sas_switch") j=$((i + 4)) ;;
366                 "sas_direct") j=$((i + 1)) ;;
367         esac
368
369         i=$((i + 1))
370
371         while [ $i -le $j ] ; do
372                 port_dir="$port_dir/$(eval echo '$'{$i})"
373                 i=$((i + 1))
374         done
375
376         PHY=$(ls -d "$port_dir"/phy* 2>/dev/null | head -1 | awk -F: '{print $NF}')
377         if [ -z "$PHY" ] ; then
378                 PHY=0
379         fi
380         PORT=$((PHY / PHYS_PER_PORT))
381
382         # Look in /sys/.../sas_device/end_device-X for the bay_identifier
383         # attribute.
384         end_device_dir=$port_dir
385
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"
391                         break
392                 fi
393                 i=$((i + 1))
394         done
395
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.
401         SLOT=
402
403         case $BAY in
404         "bay")
405                 SLOT=$(cat "$end_device_dir/bay_identifier" 2>/dev/null)
406                 ;;
407         "mix")
408                 if [ $(cat "$end_device_dir/bay_identifier" 2>/dev/null) ] ; then
409                         SLOT=$(cat "$end_device_dir/bay_identifier" 2>/dev/null)
410                 else
411                         SLOT=$(cat "$end_device_dir/phy_identifier" 2>/dev/null)
412                 fi
413                 ;;
414         "phy")
415                 SLOT=$(cat "$end_device_dir/phy_identifier" 2>/dev/null)
416                 ;;
417         "port")
418                 d=$(eval echo '$'{$i})
419                 SLOT=$(echo "$d" | sed -e 's/^.*://')
420                 ;;
421         "id")
422                 i=$((i + 1))
423                 d=$(eval echo '$'{$i})
424                 SLOT=$(echo "$d" | sed -e 's/^.*://')
425                 ;;
426         "lun")
427                 i=$((i + 2))
428                 d=$(eval echo '$'{$i})
429                 SLOT=$(echo "$d" | sed -e 's/^.*://')
430                 ;;
431         "ses")
432                 # look for this SAS path in all SCSI Enclosure Services
433                 # (SES) enclosures
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/\
441                                         {print slot}")
442                         SLOT=$1
443                         if [ -n "$SLOT" ] ; then
444                                 break
445                         fi
446                 done
447                 ;;
448         esac
449         if [ -z "$SLOT" ] ; then
450                 return
451         fi
452
453         if [ "$MULTIJBOD_MODE" = "yes" ] ; then
454                 CHAN=$(map_channel "$PCI_ID" "$PORT")
455                 SLOT=$(map_slot "$SLOT" "$CHAN")
456                 JBOD=$(map_jbod "$DEV")
457
458                 if [ -z "$CHAN" ] ; then
459                         return
460                 fi
461                 echo "${CHAN}"-"${JBOD}"-"${SLOT}${PART}"
462         else
463                 CHAN=$(map_channel "$PCI_ID" "$PORT")
464                 SLOT=$(map_slot "$SLOT" "$CHAN")
465
466                 if [ -z "$CHAN" ] ; then
467                         return
468                 fi
469                 echo "${CHAN}${SLOT}${PART}"
470         fi
471 }
472
473 scsi_handler() {
474         if [ -z "$FIRST_BAY_NUMBER" ] ; then
475                 FIRST_BAY_NUMBER=$(awk '$1 == "first_bay_number" \
476                         {print $2; exit}' $CONFIG)
477         fi
478         FIRST_BAY_NUMBER=${FIRST_BAY_NUMBER:-0}
479
480         if [ -z "$PHYS_PER_PORT" ] ; then
481                 PHYS_PER_PORT=$(awk '$1 == "phys_per_port" \
482                         {print $2; exit}' $CONFIG)
483         fi
484         PHYS_PER_PORT=${PHYS_PER_PORT:-4}
485
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"
488                 exit 1
489         fi
490
491         if [ -z "$MULTIPATH_MODE" ] ; then
492                 MULTIPATH_MODE=$(awk '$1 == "multipath" \
493                         {print $2; exit}' $CONFIG)
494         fi
495
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}')
502                 fi
503
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
509                 # helper.
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)}')
514                 fi
515
516                 # Strip off partition information.
517                 DM_NAME=$(echo "$DM_NAME" | sed 's/p[0-9][0-9]*$//')
518                 if [ -z "$DM_NAME" ] ; then
519                         return
520                 fi
521
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
527                         return
528                 fi
529         fi
530
531         if echo "$DEV" | grep -q ^/devices/ ; then
532                 sys_path=$DEV
533         else
534                 sys_path=$(udevadm info -q path -p "/sys/block/$DEV" 2>/dev/null)
535         fi
536
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
539
540         # Use positional parameters as an ad-hoc array
541         set -- $(echo "$sys_path" | tr / ' ')
542         num_dirs=$#
543         scsi_host_dir="/sys"
544
545         # Get path up to /sys/.../hostX
546         i=1
547
548         while [ $i -le "$num_dirs" ] ; do
549                 d=$(eval echo '$'{$i})
550                 scsi_host_dir="$scsi_host_dir/$d"
551
552                 echo "$d" | grep -q -E '^host[0-9]+$' && break
553                 i=$((i + 1))
554         done
555
556         if [ $i = "$num_dirs" ] ; then
557                 return
558         fi
559
560         PCI_ID=$(eval echo '$'{$((i -1))} | awk -F: '{print $2":"$3}')
561
562         # In scsi mode, the directory two levels beneath
563         # /sys/.../hostX reveals the port and slot.
564         port_dir=$scsi_host_dir
565         j=$((i + 2))
566
567         i=$((i + 1))
568         while [ $i -le $j ] ; do
569                 port_dir="$port_dir/$(eval echo '$'{$i})"
570                 i=$((i + 1))
571         done
572
573         set -- $(echo "$port_dir" | sed -e 's/^.*:\([^:]*\):\([^:]*\)$/\1 \2/')
574         PORT=$1
575         SLOT=$(($2 + FIRST_BAY_NUMBER))
576
577         if [ -z "$SLOT" ] ; then
578                 return
579         fi
580
581         CHAN=$(map_channel "$PCI_ID" "$PORT")
582         SLOT=$(map_slot "$SLOT" "$CHAN")
583
584         if [ -z "$CHAN" ] ; then
585                 return
586         fi
587         echo "${CHAN}${SLOT}${PART}"
588 }
589
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:
593         #
594         # DEVPATH=/sys/devices/pci0000:00/0000:00:03.0/0000:05:00.0/host0/subsystem/devices/0:0:0:0/scsi_generic/sg0
595
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
600                 return
601         fi
602
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
605
606         ENC_DEVICE=$(readlink "/sys/class/enclosure/$ENC")
607
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]+')
611
612         # Get the port number
613         PORT_ID=$(echo "$PORT_DIR" | grep -Eo "[0-9]+$")
614
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/../.."))
618
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')
621
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)
625
626         echo "${NAME}"
627 }
628
629 alias_handler () {
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.
635         #
636         # For example, we may have the following links for a device and its
637         # partitions,
638         #
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
642         #
643         # and the following alias in vdev_id.conf.
644         #
645         #   alias A0 dm-name-isw_dibgbfcije_ARRAY0
646         #
647         # The desired outcome is for the following links to be created
648         # without having explicitly defined aliases for the partitions.
649         #
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
653         #
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.
659         DM_PART=
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)}')
665                 fi
666         fi
667
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]*$//')
673                 fi
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") && \
678                                         ($3 == var) \
679                                         { print $2; exit }' $CONFIG)
680                                 if [ -n "$alias" ] ; then
681                                         echo "${alias}${DM_PART}"
682                                         return
683                                 fi
684                         fi
685                 done
686         done
687 }
688
689 # main
690 while getopts 'c:d:eg:jmp:h' OPTION; do
691         case ${OPTION} in
692         c)
693                 CONFIG=${OPTARG}
694                 ;;
695         d)
696                 DEV=${OPTARG}
697                 ;;
698         e)
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
702         # symlink.
703         ENCLOSURE_MODE=$(awk '{if ($1 == "enclosure_symlinks") \
704                 print $2}' "$CONFIG")
705
706         if [ "$ENCLOSURE_MODE" != "yes" ] ; then
707                 exit 0
708         fi
709                 ;;
710         g)
711                 TOPOLOGY=$OPTARG
712                 ;;
713         p)
714                 PHYS_PER_PORT=${OPTARG}
715                 ;;
716         j)
717                 MULTIJBOD_MODE=yes
718                 ;;
719         m)
720                 MULTIPATH_MODE=yes
721                 ;;
722         h)
723                 usage
724                 ;;
725         esac
726 done
727
728 if [ ! -r "$CONFIG" ] ; then
729         echo "Error: Config file \"$CONFIG\" not found"
730         exit 0
731 fi
732
733 if [ -z "$DEV" ] && [ -z "$ENCLOSURE_MODE" ] ; then
734         echo "Error: missing required option -d"
735         exit 1
736 fi
737
738 if [ -z "$TOPOLOGY" ] ; then
739         TOPOLOGY=$(awk '($1 == "topology") {print $2; exit}' "$CONFIG")
740 fi
741
742 if [ -z "$BAY" ] ; then
743         BAY=$(awk '($1 == "slot") {print $2; exit}' "$CONFIG")
744 fi
745
746 TOPOLOGY=${TOPOLOGY:-sas_direct}
747
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
752                 exit 0
753         fi
754
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"
759         fi
760         echo "ID_ENCLOSURE=$ID_ENCLOSURE"
761         echo "ID_ENCLOSURE_PATH=by-enclosure/$ENCLOSURE_PREFIX-$ID_ENCLOSURE"
762         exit 0
763 fi
764
765 # First check if an alias was defined for this device.
766 ID_VDEV=$(alias_handler)
767
768 if [ -z "$ID_VDEV" ] ; then
769         BAY=${BAY:-bay}
770         case $TOPOLOGY in
771                 sas_direct|sas_switch)
772                         ID_VDEV=$(sas_handler)
773                         ;;
774                 scsi)
775                         ID_VDEV=$(scsi_handler)
776                         ;;
777                 *)
778                         echo "Error: unknown topology $TOPOLOGY"
779                         exit 1
780                         ;;
781         esac
782 fi
783
784 if [ -n "$ID_VDEV" ] ; then
785         echo "ID_VDEV=${ID_VDEV}"
786         echo "ID_VDEV_PATH=disk/by-vdev/${ID_VDEV}"
787 fi