-
Notifications
You must be signed in to change notification settings - Fork 1
/
hba_util.py
175 lines (139 loc) · 5.69 KB
/
hba_util.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
#!/usr/bin/python
from commands import getstatusoutput
from misc_lib import run_command_list
class HBA:
def __init__(self, name = None):
self.sys_name = name
self.tested = 0
self.bdev_list_dir = {}
def test_bdev(self):
if not len(self.bdev_list_dir):
return
for (_, bdev_list) in self.bdev_list_dir.items():
i = 0
while i < len(bdev_list):
bdev = bdev_list[i]
cmd = ' '.join(['swapon -s | grep', bdev])
(status, info) = getstatusoutput(cmd)
if not status or len(info):
print 'it used as swap: %s' % (bdev)
del bdev_list[i]
continue
cmd = ' '.join(['mount', '|', 'grep', bdev])
(status, _) = getstatusoutput(cmd)
if status: # not mounted
cmd = ' '.join(['cat /proc/partitions',
'|', 'grep', bdev, '|', 'wc -l'])
(status, info) = getstatusoutput(cmd)
if status or (int(info) > 1):
print 'it has partiations: %s' % (bdev)
del bdev_list[i]
continue
cmd = ' '.join(['dd if=/dev/zero of=/dev/' + bdev,
'bs=1M count=100 oflag=direct 2>&1',
'|', 'grep MB'])
(status, _) = getstatusoutput(cmd)
if status:
print 'cannot write: %s' % (bdev)
del bdev_list[i]
continue
i += 1
else:
print 'it mounted: %s' % (bdev)
del bdev_list[i]
print 'find %s' % (bdev_list)
self.tested = 1
def get_bdev(self):
base = '/sys/bus/pci/drivers/' + self.sys_name
cmd = ' '.join(['ls', base, '|', 'grep :'])
(status, host_ids) = getstatusoutput(cmd)
if status:
print '%s : %s' % (str(status), host_ids)
return
for host_id in host_ids.split():
host_id_base = ''.join([base, '/', host_id])
cmd = ' '.join(['ls', host_id_base, '|', 'grep host'])
(status, host) = getstatusoutput(cmd)
if status:
print '%s : %s' % (str(status), host)
return
bdev_list = []
host_base = ''.join([host_id_base, '/', host])
cmd = ' '.join(['ls', host_base, '|', 'grep target'])
(status, target_ids) = getstatusoutput(cmd)
if status:
print '%s : %s' % (str(status), target_ids)
return
for target_id in target_ids.split():
target_id_base = ''.join([host_base, '/', target_id])
cmd = ' '.join(['ls', target_id_base, '|', 'grep :'])
(status, bdev_ids) = getstatusoutput(cmd)
if status:
print '%s : %s' % (str(status), bdev_ids)
return
for bdev_id in bdev_ids.split():
bdev_id_base = ''.join([target_id_base, '/',
bdev_id, '/block'])
cmd = ' '.join(['ls', bdev_id_base, '|', 'grep sd'])
(status, bdev) = getstatusoutput(cmd)
if status:
#print '%s : %s' % (str(status), bdev)
continue
bdev_list.append(bdev)
self.bdev_list_dir[host] = bdev_list
if not self.tested:
self.test_bdev()
def print_bdev_list(self):
if not len(self.bdev_list_dir):
self.get_bdev()
for host, bdevs in self.bdev_list_dir.items():
print 'host: %s' % (host)
for bdev in bdevs:
print '\t%s' % (bdev)
def get_bdev_balanced(self, cnt):
if not len(self.bdev_list_dir):
return
chose_list = []
hba_cnt = len(self.bdev_list_dir)
avg = cnt / hba_cnt
rem = cnt % hba_cnt
for i in range(0, avg):
for (_, bdev_list) in self.bdev_list_dir.items():
if i < len(bdev_list):
chose_list.append(bdev_list[i])
for i in range(avg, avg + rem):
for (_, bdev_list) in self.bdev_list_dir.items():
if len(bdev_list) <= i:
continue
chose_list.append(bdev_list[i])
if len(chose_list) < cnt:
print 'need %d bdevs' % (cnt)
self.print_bdev_list()
return chose_list
def find_hba_name(self):
hba_names = ['mv64xx', 'mptspi']
for hba in hba_names:
base = '/sys/bus/pci/drivers/'
cmd = ' '.join(['ls', base, '|', 'grep', hba])
(status, output) = getstatusoutput(cmd)
if status or not len(output):
continue
else:
self.sys_name = hba
return self.sys_name
def cleanup_dev_list(self, bdevs):
for dev_name in bdevs:
dev_path = '/dev/' + dev_name
self.__cleanup_dev(dev_path)
def cleanup_all_dev(self):
if not len(self.bdev_list_dir):
return
for _, bdevs in self.bdev_list_dir.items():
for dev_name in bdevs:
dev_path = '/dev/' + dev_name
self.__cleanup_dev(dev_path)
def __cleanup_dev(self, dev):
print 'clean %s' % (dev)
cmd = ''.join(['dd if=/dev/zero of=', dev, ' bs=1M count=100'])
cmds = [cmd]
run_command_list(cmds)