forked from avocado-framework/avocado-vt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qemu_io.py
203 lines (166 loc) · 6.68 KB
/
qemu_io.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
import re
import aexpect
from avocado.utils import process
from virttest import utils_misc
from virttest import error_context
from virttest.compat_52lts import decode_to_text
class QemuIOParamError(Exception):
"""
Parameter Error for qemu-io command
"""
pass
class QemuIO(object):
"""
A class for execute qemu-io command
"""
def __init__(self, test, params, image_name, blkdebug_cfg="",
prompt=r"qemu-io>\s*$", log_filename=None, io_options="",
log_func=None):
self.type = ""
if log_filename:
log_filename += "-" + utils_misc.generate_random_string(4)
self.output_func = utils_misc.log_line
self.output_params = (log_filename,)
else:
self.output_func = None
self.output_params = ()
self.output_prefix = ""
self.prompt = prompt
self.blkdebug_cfg = blkdebug_cfg
self.qemu_io_cmd = utils_misc.get_qemu_io_binary(params)
self.io_options = io_options
self.run_command = False
self.image_name = image_name
self.blkdebug_cfg = blkdebug_cfg
self.log_func = log_func
def get_cmd_line(self, ignore_option=[], essential_option=[],
forbid_option=[]):
"""
Generate the command line for qemu-io from the parameters
:params ignore_option: list for the options should not in command
:params essential_option: list for the essential options
:params forbid_option: list for the option should not in command
:return: qemu-io command line
"""
essential_flag = False
qemu_io_cmd = self.qemu_io_cmd
if self.io_options:
for io_option in re.split(",", self.io_options):
if io_option in ignore_option:
pass
elif io_option in forbid_option:
raise QemuIOParamError
else:
if not essential_flag and io_option in essential_option:
essential_flag = True
if len(io_option) == 1:
qemu_io_cmd += " -%s" % io_option
else:
qemu_io_cmd += " --%s" % io_option
if essential_option and not essential_flag:
raise QemuIOParamError
if self.image_name:
qemu_io_cmd += " "
if self.blkdebug_cfg:
qemu_io_cmd += "blkdebug:%s:" % self.blkdebug_cfg
qemu_io_cmd += self.image_name
return qemu_io_cmd
def cmd_output(self, command):
"""
Run a command in qemu-io
"""
pass
def close(self):
"""
Clean up
"""
pass
class QemuIOShellSession(QemuIO):
"""
Use a shell session to execute qemu-io command
"""
def __init__(self, test, params, image_name, blkdebug_cfg="",
prompt=r"qemu+-io>\s*$", log_filename=None, io_options="",
log_func=None):
QemuIO.__init__(self, test, params, image_name, blkdebug_cfg, prompt,
log_filename, io_options, log_func)
self.type = "shell"
forbid_option = ["h", "help", "V", "version", "c", "cmd"]
self.qemu_io_cmd = self.get_cmd_line(forbid_option=forbid_option)
self.create_session = True
self.session = None
@error_context.context_aware
def cmd_output(self, command, timeout=60):
"""
Get output from shell session. If the create flag is True, init the
shell session and set the create flag to False.
:param command: command to execute in qemu-io
:param timeout: timeout for execute the command
"""
qemu_io_cmd = self.qemu_io_cmd
prompt = self.prompt
output_func = self.output_func
output_params = self.output_params
output_prefix = self.output_prefix
if self.create_session:
error_context.context(
"Running command: %s" % qemu_io_cmd, self.log_func)
self.session = aexpect.ShellSession(qemu_io_cmd, echo=True,
prompt=prompt,
output_func=output_func,
output_params=output_params,
output_prefix=output_prefix)
# Record the command line in log file
if self.output_func:
params = self.output_params + (qemu_io_cmd, )
self.output_func(*params)
self.create_session = False
# Get the reaction from session
self.session.cmd_output("\n")
error_context.context("Executing command: %s" % command, self.log_func)
return self.session.cmd_output(command, timeout=timeout)
def close(self):
"""
Close the shell session for qemu-io
"""
if not self.create_session:
self.session.close()
class QemuIOSystem(QemuIO):
"""
Run qemu-io with a command line which will return immediately
"""
def __init__(self, test, params, image_name, blkdebug_cfg="",
prompt=r"qemu-io>\s*$", log_filename=None, io_options="",
log_func=None):
QemuIO.__init__(self, test, params, image_name, blkdebug_cfg, prompt,
log_filename, io_options, log_func)
ignore_option = ["c", "cmd"]
essential_option = ["h", "help", "V", "version", "c", "cmd"]
self.qemu_io_cmd = self.get_cmd_line(ignore_option=ignore_option,
essential_option=essential_option)
@error_context.context_aware
def cmd_output(self, command, timeout=60):
"""
Get output from system_output. Add the command to the qemu-io command
line with -c and record the output in the log file.
:param command: command to execute in qemu-io
:param timeout: timeout for execute the command
"""
qemu_io_cmd = self.qemu_io_cmd
if command:
qemu_io_cmd += " -c '%s'" % command
error_context.context(
"Running command: %s" % qemu_io_cmd, self.log_func)
output = decode_to_text(process.system_output(qemu_io_cmd, timeout=timeout))
# Record command line in log file
if self.output_func:
params = self.output_params + (qemu_io_cmd,)
self.output_func(*params)
params = self.output_params + (output,)
self.output_func(*params)
return output
def close(self):
"""
To keep the the same interface with QemuIOShellSession
"""
pass