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 only currently supported topologies are sas_direct and
15 # sas_switch. A multipath mode is supported in which dm-mpath
16 # devices are handled by examining the first-listed running
17 # component disk. In multipath mode the configuration file
18 # should contain a channel definition with the same name for
19 # each path to a given enclosure.
22 # Some example configuration files are given below.
25 # # Example vdev_id.conf - sas_direct.
32 # # PCI_ID HBA PORT CHANNEL NAME
52 # # Example vdev_id.conf - sas_switch
57 # # SWITCH PORT CHANNEL NAME
64 # # Example vdev_id.conf - multipath
69 # # PCI_ID HBA PORT CHANNEL NAME
75 PATH=/bin:/sbin:/usr/bin:/usr/sbin
76 CONFIG=/etc/zfs/vdev_id.conf
87 vdev_id <-d device> [-c config_file] [-p phys_per_port]
88 [-g sas_direct|sas_switch] [-m]
90 -c specify name of alernate config file [default=$CONFIG]
91 -d specify basename of device (i.e. sda)
92 -g Storage network topology [default="$TOPOLOGY"]
93 -m Run in multipath mode
94 -p number of phy's per switch port [default=$PHYS_PER_PORT]
104 MAPPED_SLOT=`awk "/^slot / && \\$2 == ${LINUX_SLOT} \
105 { print \\$3; exit }" $CONFIG`
106 if [ -z "$MAPPED_SLOT" ] ; then
107 MAPPED_SLOT=$LINUX_SLOT
109 printf "%d" ${MAPPED_SLOT}
119 MAPPED_CHAN=`awk "/^channel / && \\$2 == ${PORT} \
120 { print \\$3; exit }" $CONFIG`
123 MAPPED_CHAN=`awk "/^channel / && \\$2 == \"${PCI_ID}\" && \
124 \\$3 == ${PORT} { print \\$4; exit }" \
128 printf "%s" ${MAPPED_CHAN}
131 while getopts 'c:s:d:g:mp:h' OPTION; do
134 CONFIG=`readlink -e ${OPTARG}`
143 PHYS_PER_PORT=${OPTARG}
149 SLOT_MAP=`readlink -e ${OPTARG}`
150 if [ ! -r $SLOT_MAP ] ; then
151 echo "Error: $SLOT_MAP is nonexistant or unreadable"
161 if [ ! -r $CONFIG ] ; then
165 if [ -z "$DEV" ] ; then
166 echo "Error: missing required option -d"
170 if [ -z "$TOPOLOGY" ] ; then
171 TOPOLOGY=`awk "/^topology /{print \\$2; exit}" $CONFIG`
173 TOPOLOGY=${TOPOLOGY:-sas_direct}
175 sas_direct|sas_switch)
178 echo "Error: unknown topology $TOPOLOGY"
183 if [ -z "$PHYS_PER_PORT" ] ; then
184 PHYS_PER_PORT=`awk "/^phys_per_port /{print \\$2; exit}" $CONFIG`
186 PHYS_PER_PORT=${PHYS_PER_PORT:-4}
187 if ! echo $PHYS_PER_PORT | grep -q -E '^[0-9]+$' ; then
188 echo "Error: phys_per_port value $PHYS_PER_PORT is non-numeric"
192 if [ -z "$MULTIPATH_MODE" ] ; then
193 MULTIPATH_MODE=`awk "/^multipath /{print \\$2; exit}" $CONFIG`
196 # Use first running component device if we're handling a dm-mpath device.
197 if [ "$MULTIPATH_MODE" = "yes" ] ; then
198 # If udev didn't tell us the UUID via DM_NAME, find it in /dev/mapper
199 if [ -z "$DM_NAME" ] ; then
200 DM_NAME=`ls -l --full-time /dev/mapper |
201 awk "/\/$DEV$/{print \\$9}"`
204 # For raw disks udev exports DEVTYPE=partition when handling partitions,
205 # and the rules can be written to take advantage of this to append a
206 # -part suffix. For dm devices we get DEVTYPE=disk even for partitions
207 # so we have to append the -part suffix directly in the helper.
208 if [ "$DEVTYPE" != "partition" ] ; then
209 PART=`echo $DM_NAME | awk -Fp '/p/{print "-part"$2}'`
212 # Strip off partition information.
213 DM_NAME=`echo $DM_NAME | sed 's/p[0-9][0-9]*$//'`
214 if [ -z "$DM_NAME" ] ; then
218 # Get the raw scsi device name from multipath -l.
219 DEV=`multipath -l $DM_NAME |awk '/running/{print $3 ; exit}'`
220 if [ -z "$DEV" ] ; then
225 if echo $DEV | grep -q ^/devices/ ; then
228 sys_path=`udevadm info -q path -p /sys/block/$DEV 2>/dev/null`
231 # Use positional parameters as an ad-hoc array
232 set -- $(echo "$sys_path" | tr / ' ')
236 # Get path up to /sys/.../hostX
238 while [ $i -le $num_dirs ] ; do
239 d=$(eval echo \${$i})
240 scsi_host_dir="$scsi_host_dir/$d"
241 echo $d | grep -q -E '^host[0-9]+$' && break
245 if [ $i = $num_dirs ] ; then
249 PCI_ID=$(eval echo \${$(($i -1))} | awk -F: '{print $2":"$3}')
251 # In sas_switch mode, the directory four levels beneath /sys/.../hostX
252 # contains symlinks to phy devices that reveal the switch port number. In
253 # sas_direct mode, the phy links one directory down reveal the HBA port.
254 port_dir=$scsi_host_dir
256 "sas_switch") j=$(($i + 4)) ;;
257 "sas_direct") j=$(($i + 1)) ;;
261 while [ $i -le $j ] ; do
262 port_dir="$port_dir/$(eval echo \${$i})"
266 PHY=`ls -d $port_dir/phy* 2>/dev/null | head -1 | awk -F: '{print $NF}'`
267 if [ -z "$PHY" ] ; then
270 PORT=$(( $PHY / $PHYS_PER_PORT ))
272 # Look in /sys/.../sas_device/end_device-X for the bay_identifier
274 end_device_dir=$port_dir
275 while [ $i -lt $num_dirs ] ; do
276 d=$(eval echo \${$i})
277 end_device_dir="$end_device_dir/$d"
278 if echo $d | grep -q '^end_device' ; then
279 end_device_dir="$end_device_dir/sas_device/$d"
285 SLOT=`cat $end_device_dir/bay_identifier 2>/dev/null`
286 if [ -z "$SLOT" ] ; then
290 SLOT=`map_slot $SLOT`
291 CHAN=`map_channel $PCI_ID $PORT`
292 if [ -z "$CHAN" ] ; then
295 ID_VDEV=${CHAN}${SLOT}${PART}
297 echo "ID_VDEV=${ID_VDEV}"
298 echo "ID_VDEV_PATH=disk/by-vdev/${ID_VDEV}"