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
88 vdev_id <-d device> [-c config_file] [-p phys_per_port]
89 [-g sas_direct|sas_switch] [-m]
91 -c specify name of alernate config file [default=$CONFIG]
92 -d specify basename of device (i.e. sda)
93 -g Storage network topology [default="$TOPOLOGY"]
94 -m Run in multipath mode
95 -p number of phy's per switch port [default=$PHYS_PER_PORT]
105 MAPPED_SLOT=`awk "/^slot / && \\$2 == ${LINUX_SLOT} \
106 { print \\$3; exit }" $CONFIG`
107 if [ -z "$MAPPED_SLOT" ] ; then
108 MAPPED_SLOT=$LINUX_SLOT
110 printf "%d" ${MAPPED_SLOT}
120 MAPPED_CHAN=`awk "/^channel / && \\$2 == ${PORT} \
121 { print \\$3; exit }" $CONFIG`
124 MAPPED_CHAN=`awk "/^channel / && \\$2 == \"${PCI_ID}\" && \
125 \\$3 == ${PORT} { print \\$4; exit }" \
129 printf "%s" ${MAPPED_CHAN}
132 while getopts 'c:s:d:g:mp:h' OPTION; do
135 CONFIG=`readlink -e ${OPTARG}`
144 PHYS_PER_PORT=${OPTARG}
150 SLOT_MAP=`readlink -e ${OPTARG}`
151 if [ ! -r $SLOT_MAP ] ; then
152 echo "Error: $SLOT_MAP is nonexistant or unreadable"
162 if [ ! -r $CONFIG ] ; then
166 if [ -z "$DEV" ] ; then
167 echo "Error: missing required option -d"
171 if [ -z "$TOPOLOGY" ] ; then
172 TOPOLOGY=`awk "/^topology /{print \\$2; exit}" $CONFIG`
174 TOPOLOGY=${TOPOLOGY:-sas_direct}
176 sas_direct|sas_switch)
179 echo "Error: unknown topology $TOPOLOGY"
184 if [ -z "$PHYS_PER_PORT" ] ; then
185 PHYS_PER_PORT=`awk "/^phys_per_port /{print \\$2; exit}" $CONFIG`
187 PHYS_PER_PORT=${PHYS_PER_PORT:-4}
188 if ! echo $PHYS_PER_PORT | egrep -q '^[0-9]+$' ; then
189 echo "Error: phys_per_port value $PHYS_PER_PORT is non-numeric"
193 if [ -z "$MULTIPATH_MODE" ] ; then
194 MULTIPATH_MODE=`awk "/^multipath /{print \\$2; exit}" $CONFIG`
197 # Use first running component device if we're handling a dm-mpath device.
198 if [ "$MULTIPATH_MODE" = "yes" ] ; then
199 # If udev didn't tell us the UUID via DM_NAME, find it in /dev/mapper
200 if [ -z "$DM_NAME" ] ; then
201 DM_NAME=`ls -l --full-time /dev/mapper |
202 awk "/\/$DEV$/{print \\$9}"`
205 # For raw disks udev exports DEVTYPE=partition when handling partitions,
206 # and the rules can be written to take advantage of this to append a
207 # -part suffix. For dm devices we get DEVTYPE=disk even for partitions
208 # so we have to append the -part suffix directly in the helper.
209 if [ "$DEVTYPE" != "partition" ] ; then
210 PART=`echo $DM_NAME | awk -Fp '/p/{print "-part"$2}'`
213 # Strip off partition information.
214 DM_NAME=`echo $DM_NAME | sed 's/p[0-9][0-9]*$//'`
215 if [ -z "$DM_NAME" ] ; then
219 # Get the raw scsi device name from multipath -l.
220 DEV=`multipath -l $DM_NAME |awk '/running/{print $3 ; exit}'`
221 if [ -z "$DEV" ] ; then
226 if echo $DEV | grep -q ^/devices/ ; then
229 sys_path=`udevadm info -q path -p /sys/block/$DEV 2>/dev/null`
232 dirs=(`echo "$sys_path" | tr / ' '`)
235 # Get path up to /sys/.../hostX
236 for (( i=0; i<${#dirs[*]}; i++ )); do
238 scsi_host_dir="$scsi_host_dir/$d"
239 echo $d | egrep -q -e '^host[0-9]+$' && break
242 if [ $i = ${#dirs[*]} ] ; then
246 PCI_ID=`echo ${dirs[$(( $i - 1 ))]} | awk -F: '{print $2":"$3}'`
248 # In sas_switch mode, the directory three levels beneath /sys/.../hostX
249 # contains symlinks to phy devices that reveal the switch port number. In
250 # sas_direct mode, the phy links one directory down reveal the HBA port.
251 port_dir=$scsi_host_dir
253 "sas_switch") j=$(($i+4)) ;;
254 "sas_direct") j=$(($i + 1)) ;;
256 for (( i++; i<=$j; i++ )); do
257 port_dir="$port_dir/${dirs[$i]}"
260 PHY=`ls -d $port_dir/phy* 2>/dev/null | head -1 | awk -F: '{print $NF}'`
261 if [ -z "$PHY" ] ; then
264 PORT=$(( $PHY / $PHYS_PER_PORT ))
266 # Look in /sys/.../sas_device/end_device-X for the bay_identifier
268 end_device_dir=$port_dir
269 for (( ; i<${#dirs[*]} ; i++ )); do
271 end_device_dir="$end_device_dir/$d"
272 if echo $d | egrep -q -e '^end_device' ; then
273 end_device_dir="$end_device_dir/sas_device/$d"
278 SLOT=`cat $end_device_dir/bay_identifier 2>/dev/null`
279 if [ -z "$SLOT" ] ; then
283 SLOT=`map_slot $SLOT`
284 CHAN=`map_channel $PCI_ID $PORT`
285 if [ -z "$CHAN" ] ; then
288 ID_VDEV=${CHAN}${SLOT}${PART}
290 echo "ID_VDEV=${ID_VDEV}"
291 echo "ID_VDEV_PATH=disk/by-vdev/${ID_VDEV}"