-
Notifications
You must be signed in to change notification settings - Fork 1
/
DroboIOctl.py
376 lines (295 loc) · 10.4 KB
/
DroboIOctl.py
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
"""
copyright:
Drobo Utils Copyright (C) 2008,2009 Peter Silva ([email protected])
Drobo Utils comes with ABSOLUTELY NO WARRANTY; For details type see the file
named COPYING in the root of the source directory tree.
"""
from ctypes import *
from fcntl import ioctl
import Drobo
import struct
def hexdump(label,data):
i=0
print("%s %03x:" % (label, i), end=' ')
for bb in data:
print("%02x" % ord(bb), end=' ')
i=i+1
if (i % 16) == 0:
print()
print("%s %03x:" % (label,i), end=' ')
print()
class sg_io_hdr(Structure):
"""
do ioctl's using Linux generic SCSI interface.
all of this comes from /usr/include/scsi/sg.h
"""
SG_DXFER_TO_DEV=-2
SG_DXFER_FROM_DEV=-3
SG_IO = 0x2285
SG_GET_VERSION_NUM = 0x2282
# see include/scsi/scsi.h for more values
SAM_STAT_GOOD = 0x00
SAM_STAT_CHECK_CONDITION = 0x02
_fields_ = [ ("interface_id", c_int ),
("dxfer_direction", c_int),
("cmd_len", c_ubyte),
("mx_sb_len", c_ubyte),
("iovec_count", c_ushort),
("dxfer_len", c_int),
("dxferp", c_char_p), # ought to be void...
("cmdp", c_char_p),
("sbp", c_char_p),
("timeout", c_uint),
("flags", c_uint),
("pack_id", c_int),
("usr_ptr", c_char_p), # ought to be void...
("status", c_ubyte),
("masked_status", c_ubyte),
("msg_status", c_ubyte),
("sb_len_wr", c_ubyte),
("host_status", c_ushort),
("driver_status", c_ushort),
("resid", c_int),
("duration", c_uint),
("info", c_uint) ]
def __init__(self):
self.interface_id=ord('S')
self.dxfer_direction=0
self.cmd_len=0
self.mx_sb_len=0
self.iovec_count=0
self.dxfer_len=0
self.dxferp=None
self.cmdp=None
self.timeout=20000 # milliseconds
#self.timeout=4000 # milliseconds
self.flags=0
self.pack_id=0
self.usr_ptr=None
self.status=0
self.masked_status=0
self.msg_status=0
self.sb_len_wr=0
self.host_status=0
self.driver_status=0
self.resid=0
self.duration=0
self.info=0
class DroboIOctl:
def __init__(self,char_dev_file,readwrite=1,debugflags=1):
self.char_dev_file=char_dev_file
self.sg_fd=open(char_dev_file,'w')
self.debug=debugflags
def version(self):
"""
"""
sfmt="l"
k=create_string_buffer(struct.calcsize(sfmt))
if ioctl(self.sg_fd, sg_io_hdr.SG_GET_VERSION_NUM, k, True) < 0 :
print("%s is not an sg device, or old sg driver\n" % char_dev_file)
num=struct.unpack(sfmt,k)
return num[0]
def closefd(self):
if self.sg_fd > 0:
self.sg_fd.close()
self.sg_fd=-1
pass
def identifyLUN(self):
"""
issue a SCSI Identify LUN query, return the result.
printf("%s: scsi%d channel=%d id=%d lun=%d", file_namep, host_no,
(my_idlun.dev_id >> 16) & 0xff, my_idlun.dev_id & 0xff,
(my_idlun.dev_id >> 8) & 0xff);
"""
SCSI_IOCTL_GET_IDLUN = 0x5382
SCSI_IOCTL_GET_BUS_NUMBER = 0x5386
fmt=">bbbbl"
idlun = create_string_buffer(struct.calcsize(fmt))
i= ioctl( self.sg_fd, SCSI_IOCTL_GET_IDLUN, idlun, True)
if i < 0:
print("Drobo get_mode_page SG_IO ioctl error")
return None
(channel, lun, id, host, host_unique_id ) = struct.unpack(fmt, idlun)
#print "%s: scsi%d channel=%d id=%d lun=%d" % ( self.char_dev_file, host, \
# channel, id, lun )
#bog standard inquiry mcb
fmt="8s8s16s"
hoholen=struct.calcsize(fmt)
mcb=struct.pack("6B", 0x12, 0, 0, 0, hoholen, 0 )
# len ought to be 96
hoho=self.get_sub_page(hoholen,mcb,0,self.debug)
(dunno1,vendor,product) = struct.unpack(fmt,hoho)
return ( host, channel, id, lun, vendor )
def get_sub_page(self, sz, mcb, out, DEBUG):
"""
ioctl to retrieve a sub-page from the Drobo.
required arguments:
sz : length of buffer to be returned.
if the ioctl indicates a residual amount
control_block : some scsi control block thingum...
pass transparently through to ioctl/SG
out : choose direction of xfer. out= to device.
debug : if 1,then print debugging output (lots of it.)
"""
io_hdr=sg_io_hdr()
if out:
io_hdr.dxfer_direction=sg_io_hdr.SG_DXFER_TO_DEV
else:
io_hdr.dxfer_direction=sg_io_hdr.SG_DXFER_FROM_DEV
if self.debug & Drobo.DBG_HWDialog:
hexdump("mcb", mcb)
io_hdr.cmd_len = len(mcb)
io_hdr.cmdp = mcb
sense_buffer = create_string_buffer(64)
self.mx_sb_len = len(sense_buffer)
io_hdr.sbp=cast(sense_buffer,c_char_p)
io_hdr.sb_len_wr = 0 # initialize just in case...
page_buffer=create_string_buffer(sz)
io_hdr.dxfer_len = sz
io_hdr.dxferp = cast(page_buffer,c_char_p)
if self.debug & Drobo.DBG_HWDialog:
print("4 before ioctl, sense_buffer_len=", io_hdr.mx_sb_len)
i=ioctl(self.sg_fd, sg_io_hdr.SG_IO, io_hdr, True)
if self.debug & Drobo.DBG_HWDialog:
print("5 after ioctl, result=%d status: %d driver_status: %d host_status: %d sb_len_wr: %d resid: %d" % \
( i, io_hdr.status, io_hdr.driver_status, \
io_hdr.host_status, io_hdr.sb_len_wr, io_hdr.resid ))
if i < 0:
raise IOError("Drobo get_mode_page SG_IO ioctl error")
if io_hdr.status != io_hdr.SAM_STAT_GOOD:
raise IOError("io_hdr status is: %x" % io_hdr.status)
if io_hdr.resid > 0:
retsz = sz - io_hdr.resid
else:
retsz = sz
if self.debug & Drobo.DBG_HWDialog:
hexdump("page_buffer", page_buffer)
print("the length is: ", retsz)
return page_buffer[0:retsz]
def put_sub_page(self, mcb, buffer, DEBUG ):
"""
ioctl to write using a sub-page to the Drobo.
required arguments:
modepageblock -
buffer
DEBUG
return the number of bytes written.
"""
io_hdr=sg_io_hdr()
io_hdr.dxfer_direction=sg_io_hdr.SG_DXFER_TO_DEV
io_hdr.status=99;
io_hdr.cmd_len = len(mcb)
io_hdr.cmdp = mcb
sense_buffer = create_string_buffer(32)
io_hdr.mx_sb_len = len(sense_buffer)
#io_hdr.sbp=addressof(sense_buffer)
io_hdr.sbp=cast(sense_buffer,c_char_p)
size = len(buffer)
data2write = create_string_buffer(buffer,size)
io_hdr.dxfer_len = size
#io_hdr.dxferp = addressof(data2write)
io_hdr.dxferp = cast(data2write,c_char_p)
#these are set by ioctl... initializing just in case.
io_hdr.sb_len_wr=0;
io_hdr.resid=0;
io_hdr.status=0;
#iohp = cast(pointer(io_hdr), c_void_ptr).value
i=ioctl(self.sg_fd, sg_io_hdr.SG_IO, io_hdr, True)
if self.debug & Drobo.DBG_HWDialog:
print("put_sub_page, 5 after ioctl, result=", i)
print("status: ", io_hdr.status)
print("driver_status: ", io_hdr.driver_status)
print("host_status: ", io_hdr.host_status)
print("sb_len_wr: ", io_hdr.sb_len_wr)
print("resid: ", io_hdr.resid)
if (i< 0) :
print(" get_mode_page SG_IO ioctl error")
return None
if (io_hdr.status != 0 ) and (io_hdr.status != 2) :
print("oh no! io_hdr status is: %x\n" % io_hdr.status)
return None
if io_hdr.resid > 0:
size = size - io_hdr.resid
return size
import os
def drobolunlist(debugflags=0,vendor="Drobo"):
"""
return a list of attached Drobo devices, like so
[ [lun0, lun1, lun2], [lun0, lun1, lun2] ]
inspired by sg_scan.c (part of sg3_utils), sample output line:
/dev/sdh: scsi41 channel=0 id=0 lun=0 [em]
TRUSTED Mass Storage 1.00 [rmb=0 cmdq=0 pqual=0 pdev=0x0]
whose logic is encapsulated in the idenfityLUN call.
"""
devdir="/dev"
devices=[]
lundevs=[]
previousdev=""
p=os.listdir(devdir)
p.sort() # to ensure luns in ascending order.
for potential in p:
if potential.startswith("sd") and len(potential) == 3:
dev_file = devdir + '/' + potential
try:
if debugflags & Drobo.DBG_Detection:
print("examining: ", dev_file)
pdio = DroboIOctl( dev_file )
except:
if debugflags & Drobo.DBG_Detection:
print("rejected: failed to construct LUN pdio")
continue
try:
id = pdio.identifyLUN()
except:
if debugflags & Drobo.DBG_Detection:
print("rejected: failed to identify LUN")
pdio.closefd()
continue
if debugflags & Drobo.DBG_Detection:
print("id: ", id)
thisdev="%02d%02d%02d" % (id[0], id[1], id[2])
if ( id[4].lower().startswith("trusted") or \
id[4].lower().startswith("drobo") or \
id[4].lower().startswith(vendor.lower()) ): # you have a Drobo!
if debugflags & Drobo.DBG_Detection:
print("found a Drobo")
if thisdev == previousdev : # multi-lun drobo...
if debugflags & Drobo.DBG_Detection:
print("appending to lundevs...")
lundevs.append( dev_file )
else:
if lundevs != []:
devices.append(lundevs)
if debugflags & Drobo.DBG_Detection:
print("appending new lundevs to devices:", devices)
lundevs=[dev_file]
else:
if debugflags & Drobo.DBG_Detection:
print("rejected: vendor is %s (not from DRI)" % id[4])
previousdev=thisdev
pdio.closefd()
if lundevs != []:
devices.append(lundevs)
if debugflags & Drobo.DBG_Detection:
print("returning list: ", devices)
return devices
# unit testing...
if __name__ == "__main__":
import struct # only for unit testing...
valid_device="/dev/sdg"
#valid mcb: 5a 00 3a 01 00 00 00 00 14 00
valid_mcb=struct.pack(">BBBBBBBBBB", 0x5a, 0, 0x3a, 1, 0, 0, 0, 0, 0x14, 0 )
dmp = DroboIOctl(valid_device)
print("version", dmp.version())
print("identifyLUN", dmp.identifyLUN())
print("doing a sub_page")
hoho=dmp.get_sub_page(20,valid_mcb,0,4)
# the 4 byte header on the returned sense buffer: (122, 1, 20)
# cfg: (4, 16, 1099511557632)
#hexdump("hoho", hoho)
fmt=">BBHBBBQBHH"
print(struct.calcsize(fmt))
print(struct.unpack(fmt,hoho))
dmp.closefd()
print('hunt...')
print(drobolunlist())