]> git.proxmox.com Git - mirror_zfs.git/blob - cmd/vdev_id/vdev_id
4c7b270a85442ce23787d7359af2e1b81d7fa54f
[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 -g Storage network topology [default="$TOPOLOGY"]
108 -m Run in multipath mode
109 -p number of phy's per switch port [default=$PHYS_PER_PORT]
110 -h show this summary
111 EOF
112 exit 0
113 }
114
115 map_slot() {
116 local LINUX_SLOT=$1
117 local CHANNEL=$2
118 local MAPPED_SLOT=
119
120 MAPPED_SLOT=`awk "\\$1 == \"slot\" && \\$2 == ${LINUX_SLOT} && \
121 \\$4 ~ /^${CHANNEL}$|^$/ { print \\$3; exit }" $CONFIG`
122 if [ -z "$MAPPED_SLOT" ] ; then
123 MAPPED_SLOT=$LINUX_SLOT
124 fi
125 printf "%d" ${MAPPED_SLOT}
126 }
127
128 map_channel() {
129 local MAPPED_CHAN=
130 local PCI_ID=$1
131 local PORT=$2
132
133 case $TOPOLOGY in
134 "sas_switch")
135 MAPPED_CHAN=`awk "\\$1 == \"channel\" && \\$2 == ${PORT} \
136 { print \\$3; exit }" $CONFIG`
137 ;;
138 "sas_direct"|"scsi")
139 MAPPED_CHAN=`awk "\\$1 == \"channel\" && \
140 \\$2 == \"${PCI_ID}\" && \\$3 == ${PORT} \
141 { print \\$4; exit }" $CONFIG`
142 ;;
143 esac
144 printf "%s" ${MAPPED_CHAN}
145 }
146
147 sas_handler() {
148 if [ -z "$PHYS_PER_PORT" ] ; then
149 PHYS_PER_PORT=`awk "\\$1 == \"phys_per_port\" \
150 {print \\$2; exit}" $CONFIG`
151 fi
152 PHYS_PER_PORT=${PHYS_PER_PORT:-4}
153 if ! echo $PHYS_PER_PORT | grep -q -E '^[0-9]+$' ; then
154 echo "Error: phys_per_port value $PHYS_PER_PORT is non-numeric"
155 exit 1
156 fi
157
158 if [ -z "$MULTIPATH_MODE" ] ; then
159 MULTIPATH_MODE=`awk "\\$1 == \"multipath\" \
160 {print \\$2; exit}" $CONFIG`
161 fi
162
163 # Use first running component device if we're handling a dm-mpath device
164 if [ "$MULTIPATH_MODE" = "yes" ] ; then
165 # If udev didn't tell us the UUID via DM_NAME, check /dev/mapper
166 if [ -z "$DM_NAME" ] ; then
167 DM_NAME=`ls -l --full-time /dev/mapper |
168 awk "/\/$DEV$/{print \\$9}"`
169 fi
170
171 # For raw disks udev exports DEVTYPE=partition when
172 # handling partitions, and the rules can be written to
173 # take advantage of this to append a -part suffix. For
174 # dm devices we get DEVTYPE=disk even for partitions so
175 # we have to append the -part suffix directly in the
176 # helper.
177 if [ "$DEVTYPE" != "partition" ] ; then
178 PART=`echo $DM_NAME | awk -Fp '/p/{print "-part"$2}'`
179 fi
180
181 # Strip off partition information.
182 DM_NAME=`echo $DM_NAME | sed 's/p[0-9][0-9]*$//'`
183 if [ -z "$DM_NAME" ] ; then
184 return
185 fi
186
187 # Get the raw scsi device name from multipath -ll. Strip off
188 # leading pipe symbols to make field numbering consistent.
189 DEV=`multipath -ll $DM_NAME |
190 awk '/running/{gsub("^[|]"," "); print $3 ; exit}'`
191 if [ -z "$DEV" ] ; then
192 return
193 fi
194 fi
195
196 if echo $DEV | grep -q ^/devices/ ; then
197 sys_path=$DEV
198 else
199 sys_path=`udevadm info -q path -p /sys/block/$DEV 2>/dev/null`
200 fi
201
202 # Use positional parameters as an ad-hoc array
203 set -- $(echo "$sys_path" | tr / ' ')
204 num_dirs=$#
205 scsi_host_dir="/sys"
206
207 # Get path up to /sys/.../hostX
208 i=1
209 while [ $i -le $num_dirs ] ; do
210 d=$(eval echo \${$i})
211 scsi_host_dir="$scsi_host_dir/$d"
212 echo $d | grep -q -E '^host[0-9]+$' && break
213 i=$(($i + 1))
214 done
215
216 if [ $i = $num_dirs ] ; then
217 return
218 fi
219
220 PCI_ID=$(eval echo \${$(($i -1))} | awk -F: '{print $2":"$3}')
221
222 # In sas_switch mode, the directory four levels beneath
223 # /sys/.../hostX contains symlinks to phy devices that reveal
224 # the switch port number. In sas_direct mode, the phy links one
225 # directory down reveal the HBA port.
226 port_dir=$scsi_host_dir
227 case $TOPOLOGY in
228 "sas_switch") j=$(($i + 4)) ;;
229 "sas_direct") j=$(($i + 1)) ;;
230 esac
231
232 i=$(($i + 1))
233 while [ $i -le $j ] ; do
234 port_dir="$port_dir/$(eval echo \${$i})"
235 i=$(($i + 1))
236 done
237
238 PHY=`ls -d $port_dir/phy* 2>/dev/null | head -1 | awk -F: '{print $NF}'`
239 if [ -z "$PHY" ] ; then
240 PHY=0
241 fi
242 PORT=$(( $PHY / $PHYS_PER_PORT ))
243
244 # Look in /sys/.../sas_device/end_device-X for the bay_identifier
245 # attribute.
246 end_device_dir=$port_dir
247 while [ $i -lt $num_dirs ] ; do
248 d=$(eval echo \${$i})
249 end_device_dir="$end_device_dir/$d"
250 if echo $d | grep -q '^end_device' ; then
251 end_device_dir="$end_device_dir/sas_device/$d"
252 break
253 fi
254 i=$(($i + 1))
255 done
256
257 SLOT=
258 case $BAY in
259 "bay")
260 SLOT=`cat $end_device_dir/bay_identifier 2>/dev/null`
261 ;;
262 "phy")
263 SLOT=`cat $end_device_dir/phy_identifier 2>/dev/null`
264 ;;
265 "port")
266 d=$(eval echo \${$i})
267 SLOT=`echo $d | sed -e 's/^.*://'`
268 ;;
269 "id")
270 i=$(($i + 1))
271 d=$(eval echo \${$i})
272 SLOT=`echo $d | sed -e 's/^.*://'`
273 ;;
274 "lun")
275 i=$(($i + 2))
276 d=$(eval echo \${$i})
277 SLOT=`echo $d | sed -e 's/^.*://'`
278 ;;
279 "ses")
280 # look for this SAS path in all SCSI Enclosure Services
281 # (SES) enclosures
282 sas_address=`cat $end_device_dir/sas_address 2>/dev/null`
283 enclosures=`lsscsi -g | \
284 sed -n -e '/enclosu/s/^.* \([^ ][^ ]*\) *$/\1/p'`
285 for enclosure in $enclosures; do
286 set -- $(sg_ses -p aes $enclosure | \
287 awk "/device slot number:/{slot=\$12} \
288 /SAS address: $sas_address/\
289 {print slot}")
290 SLOT=$1
291 if [ -n "$SLOT" ] ; then
292 break
293 fi
294 done
295 ;;
296 esac
297 if [ -z "$SLOT" ] ; then
298 return
299 fi
300
301 CHAN=`map_channel $PCI_ID $PORT`
302 SLOT=`map_slot $SLOT $CHAN`
303 if [ -z "$CHAN" ] ; then
304 return
305 fi
306 echo ${CHAN}${SLOT}${PART}
307 }
308
309 scsi_handler() {
310 if [ -z "$FIRST_BAY_NUMBER" ] ; then
311 FIRST_BAY_NUMBER=`awk "\\$1 == \"first_bay_number\" \
312 {print \\$2; exit}" $CONFIG`
313 fi
314 FIRST_BAY_NUMBER=${FIRST_BAY_NUMBER:-0}
315
316 if [ -z "$PHYS_PER_PORT" ] ; then
317 PHYS_PER_PORT=`awk "\\$1 == \"phys_per_port\" \
318 {print \\$2; exit}" $CONFIG`
319 fi
320 PHYS_PER_PORT=${PHYS_PER_PORT:-4}
321 if ! echo $PHYS_PER_PORT | grep -q -E '^[0-9]+$' ; then
322 echo "Error: phys_per_port value $PHYS_PER_PORT is non-numeric"
323 exit 1
324 fi
325
326 if [ -z "$MULTIPATH_MODE" ] ; then
327 MULTIPATH_MODE=`awk "\\$1 == \"multipath\" \
328 {print \\$2; exit}" $CONFIG`
329 fi
330
331 # Use first running component device if we're handling a dm-mpath device
332 if [ "$MULTIPATH_MODE" = "yes" ] ; then
333 # If udev didn't tell us the UUID via DM_NAME, check /dev/mapper
334 if [ -z "$DM_NAME" ] ; then
335 DM_NAME=`ls -l --full-time /dev/mapper |
336 awk "/\/$DEV$/{print \\$9}"`
337 fi
338
339 # For raw disks udev exports DEVTYPE=partition when
340 # handling partitions, and the rules can be written to
341 # take advantage of this to append a -part suffix. For
342 # dm devices we get DEVTYPE=disk even for partitions so
343 # we have to append the -part suffix directly in the
344 # helper.
345 if [ "$DEVTYPE" != "partition" ] ; then
346 PART=`echo $DM_NAME | awk -Fp '/p/{print "-part"$2}'`
347 fi
348
349 # Strip off partition information.
350 DM_NAME=`echo $DM_NAME | sed 's/p[0-9][0-9]*$//'`
351 if [ -z "$DM_NAME" ] ; then
352 return
353 fi
354
355 # Get the raw scsi device name from multipath -ll. Strip off
356 # leading pipe symbols to make field numbering consistent.
357 DEV=`multipath -ll $DM_NAME |
358 awk '/running/{gsub("^[|]"," "); print $3 ; exit}'`
359 if [ -z "$DEV" ] ; then
360 return
361 fi
362 fi
363
364 if echo $DEV | grep -q ^/devices/ ; then
365 sys_path=$DEV
366 else
367 sys_path=`udevadm info -q path -p /sys/block/$DEV 2>/dev/null`
368 fi
369
370 # expect sys_path like this, for example:
371 # /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
372
373 # Use positional parameters as an ad-hoc array
374 set -- $(echo "$sys_path" | tr / ' ')
375 num_dirs=$#
376 scsi_host_dir="/sys"
377
378 # Get path up to /sys/.../hostX
379 i=1
380 while [ $i -le $num_dirs ] ; do
381 d=$(eval echo \${$i})
382 scsi_host_dir="$scsi_host_dir/$d"
383 echo $d | grep -q -E '^host[0-9]+$' && break
384 i=$(($i + 1))
385 done
386
387 if [ $i = $num_dirs ] ; then
388 return
389 fi
390
391 PCI_ID=$(eval echo \${$(($i -1))} | awk -F: '{print $2":"$3}')
392
393 # In scsi mode, the directory two levels beneath
394 # /sys/.../hostX reveals the port and slot.
395 port_dir=$scsi_host_dir
396 j=$(($i + 2))
397
398 i=$(($i + 1))
399 while [ $i -le $j ] ; do
400 port_dir="$port_dir/$(eval echo \${$i})"
401 i=$(($i + 1))
402 done
403
404 set -- $(echo $port_dir | sed -e 's/^.*:\([^:]*\):\([^:]*\)$/\1 \2/')
405 PORT=$1
406 SLOT=$(($2 + $FIRST_BAY_NUMBER))
407
408 if [ -z "$SLOT" ] ; then
409 return
410 fi
411
412 CHAN=`map_channel $PCI_ID $PORT`
413 SLOT=`map_slot $SLOT $CHAN`
414 if [ -z "$CHAN" ] ; then
415 return
416 fi
417 echo ${CHAN}${SLOT}${PART}
418 }
419
420 alias_handler () {
421 # Special handling is needed to correctly append a -part suffix
422 # to partitions of device mapper devices. The DEVTYPE attribute
423 # is normally set to "disk" instead of "partition" in this case,
424 # so the udev rules won't handle that for us as they do for
425 # "plain" block devices.
426 #
427 # For example, we may have the following links for a device and its
428 # partitions,
429 #
430 # /dev/disk/by-id/dm-name-isw_dibgbfcije_ARRAY0 -> ../../dm-0
431 # /dev/disk/by-id/dm-name-isw_dibgbfcije_ARRAY0p1 -> ../../dm-1
432 # /dev/disk/by-id/dm-name-isw_dibgbfcije_ARRAY0p2 -> ../../dm-3
433 #
434 # and the following alias in vdev_id.conf.
435 #
436 # alias A0 dm-name-isw_dibgbfcije_ARRAY0
437 #
438 # The desired outcome is for the following links to be created
439 # without having explicitly defined aliases for the partitions.
440 #
441 # /dev/disk/by-vdev/A0 -> ../../dm-0
442 # /dev/disk/by-vdev/A0-part1 -> ../../dm-1
443 # /dev/disk/by-vdev/A0-part2 -> ../../dm-3
444 #
445 # Warning: The following grep pattern will misidentify whole-disk
446 # devices whose names end with 'p' followed by a string of
447 # digits as partitions, causing alias creation to fail. This
448 # ambiguity seems unavoidable, so devices using this facility
449 # must not use such names.
450 local DM_PART=
451 if echo $DM_NAME | grep -q -E 'p[0-9][0-9]*$' ; then
452 if [ "$DEVTYPE" != "partition" ] ; then
453 DM_PART=`echo $DM_NAME | awk -Fp '/p/{print "-part"$2}'`
454 fi
455 fi
456
457 # DEVLINKS attribute must have been populated by already-run udev rules.
458 for link in $DEVLINKS ; do
459 # Remove partition information to match key of top-level device.
460 if [ -n "$DM_PART" ] ; then
461 link=`echo $link | sed 's/p[0-9][0-9]*$//'`
462 fi
463 # Check both the fully qualified and the base name of link.
464 for l in $link `basename $link` ; do
465 alias=`awk "\\$1 == \"alias\" && \\$3 == \"${l}\" \
466 { print \\$2; exit }" $CONFIG`
467 if [ -n "$alias" ] ; then
468 echo ${alias}${DM_PART}
469 return
470 fi
471 done
472 done
473 }
474
475 while getopts 'c:d:g:mp:h' OPTION; do
476 case ${OPTION} in
477 c)
478 CONFIG=${OPTARG}
479 ;;
480 d)
481 DEV=${OPTARG}
482 ;;
483 g)
484 TOPOLOGY=$OPTARG
485 ;;
486 p)
487 PHYS_PER_PORT=${OPTARG}
488 ;;
489 m)
490 MULTIPATH_MODE=yes
491 ;;
492 h)
493 usage
494 ;;
495 esac
496 done
497
498 if [ ! -r $CONFIG ] ; then
499 exit 0
500 fi
501
502 if [ -z "$DEV" ] ; then
503 echo "Error: missing required option -d"
504 exit 1
505 fi
506
507 if [ -z "$TOPOLOGY" ] ; then
508 TOPOLOGY=`awk "\\$1 == \"topology\" {print \\$2; exit}" $CONFIG`
509 fi
510
511 if [ -z "$BAY" ] ; then
512 BAY=`awk "\\$1 == \"slot\" {print \\$2; exit}" $CONFIG`
513 fi
514
515 # First check if an alias was defined for this device.
516 ID_VDEV=`alias_handler`
517
518 if [ -z "$ID_VDEV" ] ; then
519 BAY=${BAY:-bay}
520 TOPOLOGY=${TOPOLOGY:-sas_direct}
521 case $TOPOLOGY in
522 sas_direct|sas_switch)
523 ID_VDEV=`sas_handler`
524 ;;
525 scsi)
526 ID_VDEV=`scsi_handler`
527 ;;
528 *)
529 echo "Error: unknown topology $TOPOLOGY"
530 exit 1
531 ;;
532 esac
533 fi
534
535 if [ -n "$ID_VDEV" ] ; then
536 echo "ID_VDEV=${ID_VDEV}"
537 echo "ID_VDEV_PATH=disk/by-vdev/${ID_VDEV}"
538 fi