-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathzfcp_san_disc
330 lines (294 loc) · 7.02 KB
/
zfcp_san_disc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/bin/bash
#
# zfcp_san_disc
#
# Outputs a list of zFCP WWPNs or LUNs
#
# Usage:
# zfcp_san_disc [-h | -W | -L -p <WWPN> ] -b <BUS_ID>
#
# Return codes
# 1 zFCP sysfs directory not available
# 2 Invalid command line parameter
# 3 Too many commands used
# 4 Error retrieving HBA list
# 5 Bus ID not found
# 6 Error retrieving Port list
# 7 WWPN not found
# 8 Bus ID sysfs directory not available
# 9 WWPN sysfs directory not available/unable to add port to Bus ID
# 10 Error retrieving LUN list
# 11 HBA API device not available
#
START_DIR=`pwd`
SCRIPT_NAME=`basename $0`
cd `dirname $0`
SCRIPT_DIR=`pwd`
cd "${START_DIR}"
FCP_SYS_DIR='/sys/bus/ccw/drivers/zfcp'
# Commands available
LIST_WWPN='-W'
LIST_LUN='-L'
COMMAND=''
BUSID=''
WWPN=''
echo_err()
{
echo "$SCRIPT_NAME: $1" 1>&2
}
usage()
{
echo "$0 [-h | $LIST_WWPN | $LIST_LUN -p <WWPN>] -b <BUS_ID>" 1>&2
echo 1>&2
echo "Commands:" 1>&2
echo " $LIST_WWPN List WWPNs for the given BUS_ID" 1>&2
echo " $LIST_LUN List LUNs for the given BUS_ID and WWPN" 1>&2
echo " -h This usage information" 1>&2
echo "Options:" 1>&2
echo " -b BUSID Bus ID to use for listing" 1>&2
echo " -p WWPN WWPN to use for listing" 1>&2
}
list_lun()
{
local PRINT_WWPN
local BUSID_DIR
local WWPN_DIR
local ADDED_PORT
}
deactivate()
{
local ccw=$1
echo 0 > /sys/bus/ccw/devices/$ccw/online
}
lun_remove()
{
local syspath=$1
local lun=$2
echo "$lun" > $syspath/unit_remove
}
sg_remove()
{
local sg=$1
local sgnum
sgnum=${sg#/dev/sg}
: deactivate /sys/class/scsi_generic/sg$sgnum/device/delete
echo 1 > /sys/class/scsi_generic/sg$sgnum/device/delete
udevadm settle
}
while [ $# -gt 0 ]
do
case "$1" in
-b* )
if [ "$1" == "-b" ]
then
shift
BUSID="$1"
else
BUSID="${1:2}"
fi
BUSID=`echo $BUSID | tr A-F a-f`
;;
-p* )
if [ "$1" == "-p" ]
then
shift
WWPN="$1"
else
WWPN="${1:2}"
fi
WWPN=`echo $WWPN | tr A-FX a-fx`
;;
"$LIST_WWPN"|"$LIST_LUN" )
if [ -z "$COMMAND" -o "$1" == "$COMMAND" ]
then
COMMAND=$1
else
echo_err "You have already specified the $COMMAND command, and cannot use the $1 command also."
exit 3
fi
;;
-h )
usage
exit 0
;;
* )
usage
echo_err "Unknown command line parameter : $1"
exit 2
;;
esac
shift
done
if [ -z "$BUSID" ] ; then
echo_err "No bus ID given"
exit 2
fi
if [ -z "$COMMAND" ] ; then
echo_err "Please specify either -W or -L"
exit 2
fi
if [ ! -d /sys/bus/ccw/devices/$BUSID ] ; then
echo_err "Unknown bus ID $BUSID"
exit 2
fi
read devtype < /sys/bus/ccw/devices/$BUSID/devtype
read cutype < /sys/bus/ccw/devices/$BUSID/cutype
if [ "$cutype" != "1731/03" ] ; then
echo_err "Bus ID $BUSID is not an zfcp adapter"
exit 2
fi
if [ "$devtype" != "1732/03" -a "$devtype" != "1732/04" ] ; then
echo_err "Bus ID $BUSID is not an zfcp adapter"
exit 2
fi
# Now we're sure we're dealing with zfcp devices
if [ ! -d "$FCP_SYS_DIR" ] ; then
modprobe zfcp
fi
[ "$COMMAND" == "$LIST_LUN" -a -z "$WWPN" ] && usage && exit 2
read online < /sys/bus/ccw/devices/$BUSID/online
if [ "$online" != 1 ] ; then
# Activate adapter
echo 1 > /sys/bus/ccw/devices/$BUSID/online
read online < /sys/bus/ccw/devices/$BUSID/online
if [ "$online" != 1 ] ; then
echo_err "Cannot activate zfcp adapter at $BUSID"
exit 2
fi
trapcmd="deactivate $BUSID"
trap "$trapcmd" EXIT
fi
for loop in 1 2 3 4 5 ; do
read status < /sys/bus/ccw/devices/$BUSID/status
(( $status & 0x10000000 )) && break;
done
read wwpn_status < /sys/bus/ccw/devices/$BUSID/status
if !(( $wwpn_status & 0x10000000 )) ; then
echo_err "Adapter activation failed, status $wwpn_status"
exit 3
fi
for host in /sys/bus/ccw/devices/$BUSID/host* ; do
if [ -d $host ] ; then
hba_num=${host##*host}
fi
done
if [ -z "$hba_num" ] ; then
echo_err "No SCSI host allocated"
exit 3
fi
if [ "$COMMAND" == "$LIST_WWPN" ]
then
for PRINT_WWPN in /sys/bus/ccw/devices/$BUSID/0x*
do
test -d $PRINT_WWPN && echo ${PRINT_WWPN##*/}
done
exit 0
elif [ "$COMMAND" != "$LIST_LUN" ]
then
exit 1
fi
ERR=0
read allow_lun_scan < /sys/module/zfcp/parameters/allow_lun_scan
if [ "$allow_lun_scan" = "Y" ] ; then
read port_type < /sys/class/fc_host/host${hba_num}/port_type
if [ "$port_type" = "NPIV VPORT" ] ; then
skip_activation=1
fi
fi
if [ -z "$skip_activation" ] ; then
WWPN_DIR=/sys/bus/ccw/devices/$BUSID/$WWPN
if [ ! -d "${WWPN_DIR}" ]
then
echo_err "port $WWPN not found on zfcp $BUSID"
exit 9
fi
# Activate WLUN
if [ ! -d $WWPN_DIR/0xc101000000000000 ] ; then
echo 0xc101000000000000 > $WWPN_DIR/unit_add
orig_trapcmd="$trapcmd"
trapcmd="lun_remove $WWPN_DIR 0xc101000000000000; $trapcmd"
trap "$trapcmd" EXIT
activated=1
# Wait for udev to catch up
udevadm settle
sleep 1
fi
# Find corresponding sg device
sgdev=$(lsscsi -t -g $hba_num:-:-:49409 | sed -n "s/.* fc:$WWPN.* \(\/dev\/sg[0-9]*\)[[:blank:]]*$/\1/p")
if [ -c "$sgdev" ] ; then
if sg_luns $sgdev > /dev/null 2>&1 ; then
LUN_LIST=`sg_luns $sgdev | sed -n 's/^ \(.*\)/\1/p'`
trapcmd="sg_remove $sgdev; $trapcmd"
trap "$trapcmd" EXIT
wlun=1
else
wlun=
fi
fi
if [ -z "$wlun" ] ; then
if [ -n "$activated" ] ; then
trapcmd=$orig_trapcmd
trap "$trapcmd" EXIT
lun_remove $WWPN_DIR 0xc101000000000000
activated=
fi
# Activate LUN 0
if [ ! -d $WWPN_DIR/0x0000000000000000 ] ; then
echo 0 > $WWPN_DIR/unit_add
orig_trapcmd=$trapcmd
trapcmd="lun_remove $WWPN_DIR 0x0000000000000000; $trapcmd"
trap "$trapcmd" EXIT
activated=1
# Wait for udev to catch up
udevadm settle
sleep 1
fi
# Find corresponding sg device
sgdev=$(lsscsi -t -g $hba_num:-:-:0 | sed -n "s/.* fc:$WWPN.* \(\/dev\/sg[^[:blank:]]*\)[[:blank:]]*$/\1/p")
if [ -c "$sgdev" ] ; then
if sg_luns $sgdev > /dev/null 2>&1 ; then
LUN_LIST=`sg_luns $sgdev | sed -n 's/^ \(.*\)/\1/p'`
fi
if [ -n "$activated" ] ; then
trapcmd="sg_remove $sgdev; $trapcmd"
trap "$trapcmd" EXIT
fi
else
echo_err "Unable to activate LUN 0"
trap "$trapcmd" EXIT
lun_remove $WWPN_DIR 0x0000000000000000
activated=
sgdev=
ERR=10
fi
fi
for LUN in $LUN_LIST ; do
echo 0x$LUN
done
exit $ERR
else
for loop in 1 2 3 4 5 ; do
if [ -n "$(ls -d /sys/class/fc_remote_ports/rport-${hba_num}:* 2>/dev/null)" ] ; then
break
else
sleep 1
fi
done
if [ -z "$(ls -d /sys/class/fc_remote_ports/rport-${hba_num}:* 2>/dev/null)" ]; then
echo "The remote Fiber Channel port has not become available. Exiting"
exit 1
fi
for rport in /sys/class/fc_remote_ports/rport-${hba_num}:* ; do
[ -f ${rport}/port_name ] || continue
read port_name < ${rport}/port_name
if [ "$port_name" = "$WWPN" ] ; then
for t in ${rport}/device/target* ; do
[ -f ${t}/uevent ] || continue
targetid=${t#*target}
targetid=${targetid##*:}
break
done
fi
done
lsscsi -xx ${hba_num}:0:${targetid}:- | sed -n "s/\[${hba_num}:0:${targetid}:\(0x[0-9a-f]*\)\].*/\1/p"
fi