]> git.proxmox.com Git - mirror_zfs-debian.git/blob - cmd/vdev_id/vdev_id
Imported Upstream version 0.6.5.3
[mirror_zfs-debian.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] [-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")
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 -l. Strip off
188 # leading pipe symbols to make field numbering consistent.
189 DEV=`multipath -l $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 return
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 "id")
266 i=$(($i + 1))
267 d=$(eval echo \${$i})
268 SLOT=`echo $d | sed -e 's/^.*://'`
269 ;;
270 "lun")
271 i=$(($i + 2))
272 d=$(eval echo \${$i})
273 SLOT=`echo $d | sed -e 's/^.*://'`
274 ;;
275 esac
276 if [ -z "$SLOT" ] ; then
277 return
278 fi
279
280 CHAN=`map_channel $PCI_ID $PORT`
281 SLOT=`map_slot $SLOT $CHAN`
282 if [ -z "$CHAN" ] ; then
283 return
284 fi
285 echo ${CHAN}${SLOT}${PART}
286 }
287
288 alias_handler () {
289 # Special handling is needed to correctly append a -part suffix
290 # to partitions of device mapper devices. The DEVTYPE attribute
291 # is normally set to "disk" instead of "partition" in this case,
292 # so the udev rules won't handle that for us as they do for
293 # "plain" block devices.
294 #
295 # For example, we may have the following links for a device and its
296 # partitions,
297 #
298 # /dev/disk/by-id/dm-name-isw_dibgbfcije_ARRAY0 -> ../../dm-0
299 # /dev/disk/by-id/dm-name-isw_dibgbfcije_ARRAY0p1 -> ../../dm-1
300 # /dev/disk/by-id/dm-name-isw_dibgbfcije_ARRAY0p2 -> ../../dm-3
301 #
302 # and the following alias in vdev_id.conf.
303 #
304 # alias A0 dm-name-isw_dibgbfcije_ARRAY0
305 #
306 # The desired outcome is for the following links to be created
307 # without having explicitly defined aliases for the partitions.
308 #
309 # /dev/disk/by-vdev/A0 -> ../../dm-0
310 # /dev/disk/by-vdev/A0-part1 -> ../../dm-1
311 # /dev/disk/by-vdev/A0-part2 -> ../../dm-3
312 #
313 # Warning: The following grep pattern will misidentify whole-disk
314 # devices whose names end with 'p' followed by a string of
315 # digits as partitions, causing alias creation to fail. This
316 # ambiguity seems unavoidable, so devices using this facility
317 # must not use such names.
318 local DM_PART=
319 if echo $DM_NAME | grep -q -E 'p[0-9][0-9]*$' ; then
320 if [ "$DEVTYPE" != "partition" ] ; then
321 DM_PART=`echo $DM_NAME | awk -Fp '/p/{print "-part"$2}'`
322 fi
323 fi
324
325 # DEVLINKS attribute must have been populated by already-run udev rules.
326 for link in $DEVLINKS ; do
327 # Remove partition information to match key of top-level device.
328 if [ -n "$DM_PART" ] ; then
329 link=`echo $link | sed 's/p[0-9][0-9]*$//'`
330 fi
331 # Check both the fully qualified and the base name of link.
332 for l in $link `basename $link` ; do
333 alias=`awk "\\$1 == \"alias\" && \\$3 == \"${l}\" \
334 { print \\$2; exit }" $CONFIG`
335 if [ -n "$alias" ] ; then
336 echo ${alias}${DM_PART}
337 return
338 fi
339 done
340 done
341 }
342
343 while getopts 'c:d:g:mp:h' OPTION; do
344 case ${OPTION} in
345 c)
346 CONFIG=${OPTARG}
347 ;;
348 d)
349 DEV=${OPTARG}
350 ;;
351 g)
352 TOPOLOGY=$OPTARG
353 ;;
354 p)
355 PHYS_PER_PORT=${OPTARG}
356 ;;
357 m)
358 MULTIPATH_MODE=yes
359 ;;
360 h)
361 usage
362 ;;
363 esac
364 done
365
366 if [ ! -r $CONFIG ] ; then
367 exit 0
368 fi
369
370 if [ -z "$DEV" ] ; then
371 echo "Error: missing required option -d"
372 exit 1
373 fi
374
375 if [ -z "$TOPOLOGY" ] ; then
376 TOPOLOGY=`awk "\\$1 == \"topology\" {print \\$2; exit}" $CONFIG`
377 fi
378
379 if [ -z "$BAY" ] ; then
380 BAY=`awk "\\$1 == \"slot\" {print \\$2; exit}" $CONFIG`
381 fi
382
383 # First check if an alias was defined for this device.
384 ID_VDEV=`alias_handler`
385
386 if [ -z "$ID_VDEV" ] ; then
387 BAY=${BAY:-bay}
388 TOPOLOGY=${TOPOLOGY:-sas_direct}
389 case $TOPOLOGY in
390 sas_direct|sas_switch)
391 ID_VDEV=`sas_handler`
392 ;;
393 *)
394 echo "Error: unknown topology $TOPOLOGY"
395 exit 1
396 ;;
397 esac
398 fi
399
400 if [ -n "$ID_VDEV" ] ; then
401 echo "ID_VDEV=${ID_VDEV}"
402 echo "ID_VDEV_PATH=disk/by-vdev/${ID_VDEV}"
403 fi