]> git.proxmox.com Git - mirror_zfs-debian.git/blob - cmd/vdev_id/vdev_id
d2781972f75cf84e6bb0fc2a6d95aae93737cab4
[mirror_zfs-debian.git] / cmd / vdev_id / vdev_id
1 #!/bin/bash
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 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.
20
21 #
22 # Some example configuration files are given below.
23
24 # #
25 # # Example vdev_id.conf - sas_direct.
26 # #
27 #
28 # multipath no
29 # topology sas_direct
30 # phys_per_port 4
31 #
32 # # PCI_ID HBA PORT CHANNEL NAME
33 # channel 85:00.0 1 A
34 # channel 85:00.0 0 B
35 # channel 86:00.0 1 C
36 # channel 86:00.0 0 D
37 #
38 # # Linux Mapped
39 # # Slot Slot
40 # slot 1 7
41 # slot 2 10
42 # slot 3 3
43 # slot 4 6
44 # slot 5 2
45 # slot 6 8
46 # slot 7 1
47 # slot 8 4
48 # slot 9 9
49 # slot 10 5
50
51 # #
52 # # Example vdev_id.conf - sas_switch
53 # #
54 #
55 # topology sas_switch
56 #
57 # # SWITCH PORT CHANNEL NAME
58 # channel 1 A
59 # channel 2 B
60 # channel 3 C
61 # channel 4 D
62
63 # #
64 # # Example vdev_id.conf - multipath
65 # #
66 #
67 # multipath yes
68 #
69 # # PCI_ID HBA PORT CHANNEL NAME
70 # channel 85:00.0 1 A
71 # channel 85:00.0 0 B
72 # channel 86:00.0 1 A
73 # channel 86:00.0 0 B
74
75 PATH=/bin:/sbin:/usr/bin:/usr/sbin
76 CONFIG=/etc/zfs/vdev_id.conf
77 PHYS_PER_PORT=
78 DEV=
79 SLOT_MAP=
80 CHANNEL_MAP=
81 MULTIPATH=
82 TOPOLOGY=
83 declare -i i j
84
85 usage() {
86 cat << EOF
87 Usage: vdev_id [-h]
88 vdev_id <-d device> [-c config_file] [-p phys_per_port]
89 [-g sas_direct|sas_switch] [-m]
90
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]
96 -h show this summary
97 EOF
98 exit 0
99 }
100
101 map_slot() {
102 local LINUX_SLOT=$1
103 local MAPPED_SLOT=
104
105 MAPPED_SLOT=`awk "/^slot / && \\$2 == ${LINUX_SLOT} \
106 { print \\$3; exit }" $CONFIG`
107 if [ -z "$MAPPED_SLOT" ] ; then
108 MAPPED_SLOT=$LINUX_SLOT
109 fi
110 printf "%d" ${MAPPED_SLOT}
111 }
112
113 map_channel() {
114 local MAPPED_CHAN=
115 local PCI_ID=$1
116 local PORT=$2
117
118 case $TOPOLOGY in
119 "sas_switch")
120 MAPPED_CHAN=`awk "/^channel / && \\$2 == ${PORT} \
121 { print \\$3; exit }" $CONFIG`
122 ;;
123 "sas_direct")
124 MAPPED_CHAN=`awk "/^channel / && \\$2 == \"${PCI_ID}\" && \
125 \\$3 == ${PORT} { print \\$4; exit }" \
126 $CONFIG`
127 ;;
128 esac
129 printf "%s" ${MAPPED_CHAN}
130 }
131
132 while getopts 'c:s:d:g:mp:h' OPTION; do
133 case ${OPTION} in
134 c)
135 CONFIG=`readlink -e ${OPTARG}`
136 ;;
137 d)
138 DEV=${OPTARG}
139 ;;
140 g)
141 TOPOLOGY=$OPTARG
142 ;;
143 p)
144 PHYS_PER_PORT=${OPTARG}
145 ;;
146 m)
147 MULTIPATH_MODE=yes
148 ;;
149 s)
150 SLOT_MAP=`readlink -e ${OPTARG}`
151 if [ ! -r $SLOT_MAP ] ; then
152 echo "Error: $SLOT_MAP is nonexistant or unreadable"
153 exit 1
154 fi
155 ;;
156 h)
157 usage
158 ;;
159 esac
160 done
161
162 if [ ! -r $CONFIG ] ; then
163 exit 0
164 fi
165
166 if [ -z "$DEV" ] ; then
167 echo "Error: missing required option -d"
168 exit 1
169 fi
170
171 if [ -z "$TOPOLOGY" ] ; then
172 TOPOLOGY=`awk "/^topology /{print \\$2; exit}" $CONFIG`
173 fi
174 TOPOLOGY=${TOPOLOGY:-sas_direct}
175 case $TOPOLOGY in
176 sas_direct|sas_switch)
177 ;;
178 *)
179 echo "Error: unknown topology $TOPOLOGY"
180 exit 1
181 ;;
182 esac
183
184 if [ -z "$PHYS_PER_PORT" ] ; then
185 PHYS_PER_PORT=`awk "/^phys_per_port /{print \\$2; exit}" $CONFIG`
186 fi
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"
190 exit 1
191 fi
192
193 if [ -z "$MULTIPATH_MODE" ] ; then
194 MULTIPATH_MODE=`awk "/^multipath /{print \\$2; exit}" $CONFIG`
195 fi
196
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}"`
203 fi
204
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}'`
211 fi
212
213 # Strip off partition information.
214 DM_NAME=`echo $DM_NAME | sed 's/p[0-9][0-9]*$//'`
215 if [ -z "$DM_NAME" ] ; then
216 exit 0
217 fi
218
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
222 exit 0
223 fi
224 fi
225
226 if echo $DEV | grep -q ^/devices/ ; then
227 sys_path=$DEV
228 else
229 sys_path=`udevadm info -q path -p /sys/block/$DEV 2>/dev/null`
230 fi
231
232 dirs=(`echo "$sys_path" | tr / ' '`)
233 scsi_host_dir="/sys"
234
235 # Get path up to /sys/.../hostX
236 for (( i=0; i<${#dirs[*]}; i++ )); do
237 d=${dirs[$i]}
238 scsi_host_dir="$scsi_host_dir/$d"
239 echo $d | egrep -q -e '^host[0-9]+$' && break
240 done
241
242 if [ $i = ${#dirs[*]} ] ; then
243 exit 0
244 fi
245
246 PCI_ID=`echo ${dirs[$(( $i - 1 ))]} | awk -F: '{print $2":"$3}'`
247
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
252 case $TOPOLOGY in
253 "sas_switch") j=$(($i+4)) ;;
254 "sas_direct") j=$(($i + 1)) ;;
255 esac
256 for (( i++; i<=$j; i++ )); do
257 port_dir="$port_dir/${dirs[$i]}"
258 done
259
260 PHY=`ls -d $port_dir/phy* 2>/dev/null | head -1 | awk -F: '{print $NF}'`
261 if [ -z "$PHY" ] ; then
262 exit 0
263 fi
264 PORT=$(( $PHY / $PHYS_PER_PORT ))
265
266 # Look in /sys/.../sas_device/end_device-X for the bay_identifier
267 # attribute.
268 end_device_dir=$port_dir
269 for (( ; i<${#dirs[*]} ; i++ )); do
270 d=${dirs[$i]}
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"
274 break
275 fi
276 done
277
278 SLOT=`cat $end_device_dir/bay_identifier 2>/dev/null`
279 if [ -z "$SLOT" ] ; then
280 exit 0
281 fi
282
283 SLOT=`map_slot $SLOT`
284 CHAN=`map_channel $PCI_ID $PORT`
285 if [ -z "$CHAN" ] ; then
286 exit 0
287 fi
288 ID_VDEV=${CHAN}${SLOT}${PART}
289
290 echo "ID_VDEV=${ID_VDEV}"
291 echo "ID_VDEV_PATH=disk/by-vdev/${ID_VDEV}"