3 # Set BUSES and PORTS to match the topology of your system. As each
4 # port is enumerated it will be assigned the next channel name. The
5 # current script enumerates each port on a bus before moving on to
6 # enumerate the next bus.
8 # Every distribution, version of udev, and type of attached storage
9 # seems to result in slightly different formatting of the by-path
10 # name. For this reason you may need to adjust the parsing below
11 # to suit your needs. This is one of the reasons to use a custom
12 # /etc/zfs/zdev.conf file, it allows the by-path naming convertion
13 # to change and still keep the simple <channel><rank> naming.
16 CONFIG=${CONFIG:-/etc/zfs/zdev.conf}
19 CHANNELS=( A B C D E F G H I J K L M N O P Q R S T U V W X Y Z )
26 Usage: zpool_layout [-th] [-c file] [-b buses] [-p ports] [-n channels] [-m map]
27 -c Alternate config file [default=${CONFIG}]
28 -b Enumerate buses [default="${BUSES[*]}"]
29 -p Enumerate ports [default="${PORTS[*]}"]
30 -n Channel names [default="A..Z"]
31 -t Trigger and wait for udev to settle [default=${TRIGGER}]
32 -m Slot mapping [default=${MAPPING}]
38 while getopts 'c:b:p:n:m:th' OPTION; do
53 MAPPING=`readlink -e ${OPTARG}`
64 # Verify mapping file exists if specified.
65 # Linux-Slot Custom-Slot
66 if [ ${MAPPING} != "linux" ] && [ ! -e ${MAPPING} ]; then
67 echo "Error: Mapping file '${MAPPING}' does not exist"
71 # Save stdout as fd #8, then redirect stdout to the config file.
74 pushd /dev/disk/by-path >/dev/null
80 if [ ${MAPPING} = "linux" ]; then
81 MAPPED_SLOT=${LINUX_SLOT}
83 MAPPED_SLOT=`${AWK} "\\$1 == ${LINUX_SLOT} && !/^#/ \
84 { print \\$2; exit }" $MAPPING`
86 printf "%d" ${MAPPED_SLOT}
89 # Generate comment header.
91 echo "# Custom /dev/disk/by-path to /dev/disk/zpool mapping, "
92 echo "# based of the following physical cable layout."
95 # Generate host port layout table for comment header.
96 echo "# ------------------ Host Port Layout ---------------------"
98 for (( i=0; i<${#BUSES[*]}; i++ )); do
99 printf "%-8d" ${BUSES[$i]}
103 for (( i=0, k=0; i<${#PORTS[*]}; i++ )); do
104 printf "# Port %-2d " ${PORTS[$i]}
106 for (( j=0; j<${#BUSES[*]}; j++, k++ )); do
107 let k=$j*${#PORTS[*]}+$i
108 printf "%-8s" ${CHANNELS[$k]}
114 # Generate channel/disk layout table for comment header.
115 echo "# ----------------- Channel/Disk Layout -------------------"
116 echo "# Channel Disks"
117 for (( i=0, k=0; i<${#BUSES[*]}; i++ )); do
118 for (( j=0; j<${#PORTS[*]}; j++, k++ )); do
119 printf "# %-9s" ${CHANNELS[$k]}
120 ls *:${BUSES[$i]}:*:${PORTS[$j]}* 2>/dev/null | \
121 cut -f7 -d'-' | sort -u -n | tr '\n' ','
127 # Generate mapping from <channel><rank> to by-path name.
128 for (( i=0, k=0; i<${#BUSES[*]}; i++ )); do
129 for (( j=0; j<${#PORTS[*]}; j++, k++ )); do
130 BYPATH=(`ls *:${BUSES[$i]}:*:${PORTS[$j]}* 2>/dev/null | \
131 grep -v part | sort -n -k7 -t'-' | cut -f1-6 -d'-'`)
132 SLOTS=(`ls *:${BUSES[$i]}:*:${PORTS[$j]}* 2>/dev/null | \
133 grep -v part | sort -n -k7 -t'-' | cut -f7 -d'-'`)
136 for (( l=0; l<${#SLOTS[*]}; l++ )); do
137 MAPPED_SLOT=`map_slot ${SLOTS[$l]}`
138 printf "%s%d\t%s-%d\n" \
139 ${CHANNELS[$k]} ${MAPPED_SLOT} \
140 ${BYPATH[$l]} ${SLOTS[$l]} >>${TMP_FILE}
144 echo -n "# Channel ${CHANNELS[$k]}, "
145 echo "Bus ${BUSES[$i]}, Port ${PORTS[$j]}"
146 cat ${TMP_FILE} | sort -n -k2 -t${CHANNELS[$k]}
151 # Restore stdout from fd #8 and close fd #8.
155 if [ ${TRIGGER} = "yes" ]; then
156 udevadm trigger --action=change --subsystem-match=block