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.
36 # # PCI_ID HBA PORT CHANNEL NAME
42 # # Custom mapping for Channel A
51 # # Default mapping for B, C, and D
58 # # Example vdev_id.conf - sas_switch
63 # # SWITCH PORT CHANNEL NAME
70 # # Example vdev_id.conf - multipath
75 # # PCI_ID HBA PORT CHANNEL NAME
82 # # Example vdev_id.conf - alias
86 # # name fully qualified or base name of device link
87 # alias d1 /dev/disk/by-id/wwn-0x5000c5002de3b9ca
88 # alias d2 wwn-0x5000c5002def789e
90 PATH=/bin:/sbin:/usr/bin:/usr/sbin
91 CONFIG=/etc/zfs/vdev_id.conf
100 vdev_id <-d device> [-c config_file] [-p phys_per_port]
101 [-g sas_direct|sas_switch] [-m]
103 -c specify name of alernate config file [default=$CONFIG]
104 -d specify basename of device (i.e. sda)
105 -g Storage network topology [default="$TOPOLOGY"]
106 -m Run in multipath mode
107 -p number of phy's per switch port [default=$PHYS_PER_PORT]
118 MAPPED_SLOT=`awk "\\$1 == \"slot\" && \\$2 == ${LINUX_SLOT} && \
119 \\$4 ~ /^(${CHANNEL}|)$/ { print \\$3; exit }" $CONFIG`
120 if [ -z "$MAPPED_SLOT" ] ; then
121 MAPPED_SLOT=$LINUX_SLOT
123 printf "%d" ${MAPPED_SLOT}
133 MAPPED_CHAN=`awk "\\$1 == \"channel\" && \\$2 == ${PORT} \
134 { print \\$3; exit }" $CONFIG`
137 MAPPED_CHAN=`awk "\\$1 == \"channel\" && \
138 \\$2 == \"${PCI_ID}\" && \\$3 == ${PORT} \
139 { print \\$4; exit }" $CONFIG`
142 printf "%s" ${MAPPED_CHAN}
146 if [ -z "$PHYS_PER_PORT" ] ; then
147 PHYS_PER_PORT=`awk "\\$1 == \"phys_per_port\" \
148 {print \\$2; exit}" $CONFIG`
150 PHYS_PER_PORT=${PHYS_PER_PORT:-4}
151 if ! echo $PHYS_PER_PORT | grep -q -E '^[0-9]+$' ; then
152 echo "Error: phys_per_port value $PHYS_PER_PORT is non-numeric"
156 if [ -z "$MULTIPATH_MODE" ] ; then
157 MULTIPATH_MODE=`awk "\\$1 == \"multipath\" \
158 {print \\$2; exit}" $CONFIG`
161 # Use first running component device if we're handling a dm-mpath device
162 if [ "$MULTIPATH_MODE" = "yes" ] ; then
163 # If udev didn't tell us the UUID via DM_NAME, check /dev/mapper
164 if [ -z "$DM_NAME" ] ; then
165 DM_NAME=`ls -l --full-time /dev/mapper |
166 awk "/\/$DEV$/{print \\$9}"`
169 # For raw disks udev exports DEVTYPE=partition when
170 # handling partitions, and the rules can be written to
171 # take advantage of this to append a -part suffix. For
172 # dm devices we get DEVTYPE=disk even for partitions so
173 # we have to append the -part suffix directly in the
175 if [ "$DEVTYPE" != "partition" ] ; then
176 PART=`echo $DM_NAME | awk -Fp '/p/{print "-part"$2}'`
179 # Strip off partition information.
180 DM_NAME=`echo $DM_NAME | sed 's/p[0-9][0-9]*$//'`
181 if [ -z "$DM_NAME" ] ; then
185 # Get the raw scsi device name from multipath -l. Strip off
186 # leading pipe symbols to make field numbering consistent.
187 DEV=`multipath -l $DM_NAME |
188 awk '/running/{gsub("^[|]"," "); print $3 ; exit}'`
189 if [ -z "$DEV" ] ; then
194 if echo $DEV | grep -q ^/devices/ ; then
197 sys_path=`udevadm info -q path -p /sys/block/$DEV 2>/dev/null`
200 # Use positional parameters as an ad-hoc array
201 set -- $(echo "$sys_path" | tr / ' ')
205 # Get path up to /sys/.../hostX
207 while [ $i -le $num_dirs ] ; do
208 d=$(eval echo \${$i})
209 scsi_host_dir="$scsi_host_dir/$d"
210 echo $d | grep -q -E '^host[0-9]+$' && break
214 if [ $i = $num_dirs ] ; then
218 PCI_ID=$(eval echo \${$(($i -1))} | awk -F: '{print $2":"$3}')
220 # In sas_switch mode, the directory four levels beneath
221 # /sys/.../hostX contains symlinks to phy devices that reveal
222 # the switch port number. In sas_direct mode, the phy links one
223 # directory down reveal the HBA port.
224 port_dir=$scsi_host_dir
226 "sas_switch") j=$(($i + 4)) ;;
227 "sas_direct") j=$(($i + 1)) ;;
231 while [ $i -le $j ] ; do
232 port_dir="$port_dir/$(eval echo \${$i})"
236 PHY=`ls -d $port_dir/phy* 2>/dev/null | head -1 | awk -F: '{print $NF}'`
237 if [ -z "$PHY" ] ; then
240 PORT=$(( $PHY / $PHYS_PER_PORT ))
242 # Look in /sys/.../sas_device/end_device-X for the bay_identifier
244 end_device_dir=$port_dir
245 while [ $i -lt $num_dirs ] ; do
246 d=$(eval echo \${$i})
247 end_device_dir="$end_device_dir/$d"
248 if echo $d | grep -q '^end_device' ; then
249 end_device_dir="$end_device_dir/sas_device/$d"
255 SLOT=`cat $end_device_dir/bay_identifier 2>/dev/null`
256 if [ -z "$SLOT" ] ; then
260 CHAN=`map_channel $PCI_ID $PORT`
261 SLOT=`map_slot $SLOT $CHAN`
262 if [ -z "$CHAN" ] ; then
265 echo ${CHAN}${SLOT}${PART}
269 # Special handling is needed to correctly append a -part suffix
270 # to partitions of device mapper devices. The DEVTYPE attribute
271 # is normally set to "disk" instead of "partition" in this case,
272 # so the udev rules won't handle that for us as they do for
273 # "plain" block devices.
275 # For example, we may have the following links for a device and its
278 # /dev/disk/by-id/dm-name-isw_dibgbfcije_ARRAY0 -> ../../dm-0
279 # /dev/disk/by-id/dm-name-isw_dibgbfcije_ARRAY0p1 -> ../../dm-1
280 # /dev/disk/by-id/dm-name-isw_dibgbfcije_ARRAY0p2 -> ../../dm-3
282 # and the following alias in vdev_id.conf.
284 # alias A0 dm-name-isw_dibgbfcije_ARRAY0
286 # The desired outcome is for the following links to be created
287 # without having explicitly defined aliases for the partitions.
289 # /dev/disk/by-vdev/A0 -> ../../dm-0
290 # /dev/disk/by-vdev/A0-part1 -> ../../dm-1
291 # /dev/disk/by-vdev/A0-part2 -> ../../dm-3
293 # Warning: The following grep pattern will misidentify whole-disk
294 # devices whose names end with 'p' followed by a string of
295 # digits as partitions, causing alias creation to fail. This
296 # ambiguity seems unavoidable, so devices using this facility
297 # must not use such names.
299 if echo $DM_NAME | grep -q -E 'p[0-9][0-9]*$' ; then
300 if [ "$DEVTYPE" != "partition" ] ; then
301 DM_PART=`echo $DM_NAME | awk -Fp '/p/{print "-part"$2}'`
305 # DEVLINKS attribute must have been populated by already-run udev rules.
306 for link in $DEVLINKS ; do
307 # Remove partition information to match key of top-level device.
308 if [ -n "$DM_PART" ] ; then
309 link=`echo $link | sed 's/p[0-9][0-9]*$//'`
311 # Check both the fully qualified and the base name of link.
312 for l in $link `basename $link` ; do
313 alias=`awk "\\$1 == \"alias\" && \\$3 == \"${l}\" \
314 { print \\$2; exit }" $CONFIG`
315 if [ -n "$alias" ] ; then
316 echo ${alias}${DM_PART}
323 while getopts 'c:d:g:mp:h' OPTION; do
335 PHYS_PER_PORT=${OPTARG}
346 if [ ! -r $CONFIG ] ; then
350 if [ -z "$DEV" ] ; then
351 echo "Error: missing required option -d"
355 if [ -z "$TOPOLOGY" ] ; then
356 TOPOLOGY=`awk "\\$1 == \"topology\" {print \\$2; exit}" $CONFIG`
359 # First check if an alias was defined for this device.
360 ID_VDEV=`alias_handler`
362 if [ -z "$ID_VDEV" ] ; then
363 TOPOLOGY=${TOPOLOGY:-sas_direct}
365 sas_direct|sas_switch)
366 ID_VDEV=`sas_handler`
369 echo "Error: unknown topology $TOPOLOGY"
375 if [ -n "$ID_VDEV" ] ; then
376 echo "ID_VDEV=${ID_VDEV}"
377 echo "ID_VDEV_PATH=disk/by-vdev/${ID_VDEV}"