forked from autotest/virt-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiscsi.py
295 lines (250 loc) · 9.28 KB
/
iscsi.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
"""
Basic iscsi support for Linux host with the help of commands
iscsiadm and tgtadm.
This include the basic operates such as login and get device name by
target name. And it can support the real iscsi access and emulated
iscsi in localhost then access it.
"""
import re, os, logging
from autotest.client import os_dep
from autotest.client.shared import utils, error
def iscsi_get_sessions():
"""
Get the iscsi sessions activated
"""
cmd = "iscsiadm --mode session"
output = utils.system_output(cmd, ignore_status=True)
pattern = r"(\d+\.\d+\.\d+\.\d+):\d+,\d+\s+([\w\.\-:\d]+)"
sessions = []
if "No active sessions" not in output:
sessions = re.findall(pattern, output)
return sessions
def iscsi_get_nodes():
"""
Get the iscsi nodes
"""
cmd = "iscsiadm --mode node"
output = utils.system_output(cmd)
pattern = r"(\d+\.\d+\.\d+\.\d+):\d+,\d+\s+([\w\.\-:\d]+)"
nodes = []
if "No records found" not in output:
nodes = re.findall(pattern, output)
return nodes
def iscsi_login(target_name):
"""
Login to a target with the target name
@param target_name: Name of the target
"""
cmd = "iscsiadm --mode node --login --targetname %s" % target_name
output = utils.system_output(cmd)
target_login = ""
if "successful" in output:
target_login = target_name
return target_login
def iscsi_logout(target_name=None):
"""
Logout from a target. If the target name is not set then logout all
targets.
@params target_name: Name of the target.
"""
if target_name:
cmd = "iscsiadm --mode node --logout -T %s" % target_name
else:
cmd = "iscsiadm --mode node --logout all"
output = utils.system_output(cmd)
target_logout = ""
if "successful" in output:
target_logout = target_name
return target_logout
def iscsi_discover(portal_ip):
"""
Query from iscsi server for available targets
@param portal_ip: Ip for iscsi server
"""
cmd = "iscsiadm -m discovery -t sendtargets -p %s" % portal_ip
output = utils.system_output(cmd, ignore_status=True)
session = ""
if "Invalid" in output:
logging.debug(output)
else:
session = output
return session
class Iscsi(object):
"""
Basic iscsi support class. Will handle the emulated iscsi export and
access to both real iscsi and emulated iscsi device.
"""
def __init__(self, params, root_dir="/tmp"):
os_dep.command("iscsiadm")
self.target = params.get("target")
if params.get("portal_ip"):
self.portal_ip = params.get("portal_ip")
else:
self.portal_ip = utils.system_output("hostname")
if params.get("iscsi_thread_id"):
self.id = params.get("iscsi_thread_id")
else:
self.id = utils.generate_random_string(4)
self.initiator = params.get("initiator")
if params.get("emulated_image"):
self.initiator = None
os_dep.command("tgtadm")
emulated_image = params.get("emulated_image")
self.emulated_image = os.path.join(root_dir, emulated_image)
self.emulated_id = ""
self.emulated_size = params.get("image_size")
self.unit = self.emulated_size[-1].upper()
self.emulated_size = self.emulated_size[:-1]
self.export_flag = False
# maps K,M,G,T => (count, bs)
emulated_size = {'K': (1, 1),
'M': (1, 1024),
'G': (1024, 1024),
'T': (1024, 1048576),
}
if emulated_size.has_key(self.unit):
block_size = emulated_size[self.unit][1]
size = int(self.emulated_size) * emulated_size[self.unit][0]
self.create_cmd = ("dd if=/dev/zero of=%s count=%s bs=%sK"
% (self.emulated_image, size, block_size))
def logged_in(self):
"""
Check if the session is login or not.
"""
sessions = iscsi_get_sessions()
login = False
for i in sessions:
if i[1] == self.target:
login = True
break
return login
def portal_visible(self):
"""
Check if the portal can be found or not.
"""
return bool(re.findall("%s$" % self.target,
iscsi_discover(self.portal_ip), re.M))
def login(self):
"""
Login session for both real iscsi device and emulated iscsi. Include
env check and setup.
"""
login_flag = False
if self.portal_visible():
login_flag = True
elif self.initiator:
logging.debug("Try to update iscsi initiatorname")
cmd = "mv /etc/iscsi/initiatorname.iscsi "
cmd += "/etc/iscsi/initiatorname.iscsi-%s" % self.id
utils.system(cmd)
fd = open("/etc/iscsi/initiatorname.iscsi", 'w')
fd.write("InitiatorName=%s" % self.initiator)
fd.close()
utils.system("service iscsid restart")
if self.portal_visible():
login_flag = True
elif self.emulated_image:
self.export_target()
utils.system("service iscsid restart")
if self.portal_visible():
login_flag = True
if login_flag:
iscsi_login(self.target)
def get_device_name(self):
"""
Get device name from the target name.
"""
cmd = "iscsiadm -m session -P 3"
device_name = ""
if self.logged_in():
output = utils.system_output(cmd)
pattern = r"Target:\s+%s\n.*?disk\s+(\w+)\s+" % self.target
if re.findall(pattern, output, re.S):
device_name = re.findall(pattern, output, re.S)[0]
device_name = "/dev/%s" % device_name
else:
logging.debug("Can not find taget after login")
else:
logging.debug("Session is not login yet.")
return device_name
def get_target_id(self):
"""
Get target id from image name. Only works for emulated iscsi device
"""
cmd = "tgtadm --lld iscsi --mode target --op show"
target_info = utils.system_output(cmd)
target_id = ""
for line in re.split("\n", target_info):
if re.findall("Target\s+(\d+)", line):
target_id= re.findall("Target\s+(\d+)", line)[0]
elif re.findall("Backing store path:\s+(/+.+)", line):
if self.emulated_image in line:
break
else:
target_id = ""
return target_id
def export_target(self):
"""
Export target in localhost for emulated iscsi
"""
if not os.path.isfile(self.emulated_image):
utils.system(self.create_cmd)
cmd = "tgtadm --lld iscsi --mode target --op show"
try:
output = utils.system_output(cmd)
except error.CmdError:
utils.system("service tgtd restart")
output = utils.system_output(cmd)
if not re.findall("%s$" % self.target, output, re.M):
logging.debug("Need to export target in host")
output = utils.system_output(cmd)
used_id = re.findall("Target\s+(\d+)", output)
emulated_id = 1
while str(emulated_id) in used_id:
emulated_id += 1
self.emulated_id = str(emulated_id)
cmd = "tgtadm --mode target --op new --tid %s" % self.emulated_id
cmd += " --lld iscsi --targetname %s" % self.target
utils.system(cmd)
cmd = "tgtadm --mode logicalunit --op new "
cmd += "--tid %s --lld iscsi --lun 1 " % self.emulated_id
cmd += "--backing-store %s" % self.emulated_image
utils.system(cmd)
cmd = "tgtadm --lld iscsi --op bind --mode target "
cmd += "--tid %s -I ALL" % self.emulated_id
utils.system(cmd)
self.export_flag = True
else:
self.emulated_id = re.findall("Target\s+(\d+):\s+%s$" %\
self.target, output)
def delete_target(self):
"""
Delete target from host.
"""
cmd = "tgtadm --lld iscsi --mode target --op show"
output = utils.system_output(cmd)
if re.findall("%s$" % self.target, output, re.M):
if self.emulated_id:
cmd = "tgtadm --lld iscsi --mode target --op delete "
cmd += "--tid %s" % self.emulated_id
utils.system(cmd)
def logout(self):
"""
Logout from target.
"""
if self.logged_in():
iscsi_logout(self.target)
def cleanup(self):
"""
Clean up env after iscsi used.
"""
if self.logged_in():
self.logout()
if os.path.isfile("/etc/iscsi/initiatorname.iscsi-%s" % self.id):
cmd = " mv /etc/iscsi/initiatorname.iscsi-%s" % self.id
cmd += " /etc/iscsi/initiatorname.iscsi"
utils.system(cmd)
cmd = "service iscsid restart"
utils.system(cmd)
if self.export_flag:
self.delete_target()