3 # Low-level QEMU shell on top of QMP.
5 # Copyright (C) 2009, 2010 Red Hat Inc.
8 # Luiz Capitulino <lcapitulino@redhat.com>
10 # This work is licensed under the terms of the GNU GPL, version 2. See
11 # the COPYING file in the top-level directory.
17 # # qemu [...] -qmp unix:./qmp-sock,server
21 # $ qmp-shell ./qmp-sock
23 # Commands have the following format:
25 # < command-name > [ arg-name1=arg1 ] ... [ arg-nameN=argN ]
29 # (QEMU) device_add driver=e1000 id=net1
33 # key=value pairs also support Python or JSON object literal subset notations,
34 # without spaces. Dictionaries/objects {} are supported as are arrays [].
36 # example-command arg-name1={'key':'value','obj'={'prop':"value"}}
38 # Both JSON and Python formatting should work, including both styles of
39 # string literal quotes. Both paradigms of literal values should work,
40 # including null/true/false for JSON and None/True/False for Python.
43 # Transactions have the following multi-line format:
46 # action-name1 [ arg-name1=arg1 ] ... [arg-nameN=argN ]
48 # action-nameN [ arg-name1=arg1 ] ... [arg-nameN=argN ]
51 # One line transactions are also supported:
53 # transaction( action-name1 ... )
58 # TRANS> block-dirty-bitmap-add node=drive0 name=bitmap1
59 # TRANS> block-dirty-bitmap-clear node=drive0 name=bitmap0
64 # Use the -v and -p options to activate the verbose and pretty-print options,
65 # which will echo back the properly formatted JSON-compliant QMP that is being
66 # sent to QEMU, which is useful for debugging and documentation generation.
77 class QMPCompleter(list):
78 def complete(self
, text
, state
):
80 if cmd
.startswith(text
):
86 class QMPShellError(Exception):
89 class QMPShellBadPort(QMPShellError
):
92 class FuzzyJSON(ast
.NodeTransformer
):
93 '''This extension of ast.NodeTransformer filters literal "true/false/null"
94 values in an AST and replaces them by proper "True/False/None" values that
95 Python can properly evaluate.'''
96 def visit_Name(self
, node
):
99 if node
.id == 'false':
101 if node
.id == 'null':
105 # TODO: QMPShell's interface is a bit ugly (eg. _fill_completion() and
106 # _execute_cmd()). Let's design a better one.
107 class QMPShell(qmp
.QEMUMonitorProtocol
):
108 def __init__(self
, address
, pretty
=False):
109 super(QMPShell
, self
).__init
__(self
.__get
_address
(address
))
110 self
._greeting
= None
111 self
._completer
= None
112 self
._pretty
= pretty
113 self
._transmode
= False
114 self
._actions
= list()
115 self
._histfile
= os
.path
.join(os
.path
.expanduser('~'),
116 '.qmp-shell_history')
118 def __get_address(self
, arg
):
120 Figure out if the argument is in the port:host form, if it's not it's
121 probably a file path.
123 addr
= arg
.split(':')
128 raise QMPShellBadPort
129 return ( addr
[0], port
)
133 def _fill_completion(self
):
134 cmds
= self
.cmd('query-commands')
135 if cmds
.has_key('error'):
137 for cmd
in cmds
['return']:
138 self
._completer
.append(cmd
['name'])
140 def __completer_setup(self
):
141 self
._completer
= QMPCompleter()
142 self
._fill
_completion
()
143 readline
.set_history_length(1024)
144 readline
.set_completer(self
._completer
.complete
)
145 readline
.parse_and_bind("tab: complete")
146 # XXX: default delimiters conflict with some command names (eg. query-),
147 # clearing everything as it doesn't seem to matter
148 readline
.set_completer_delims('')
150 readline
.read_history_file(self
._histfile
)
151 except Exception as e
:
152 if isinstance(e
, IOError) and e
.errno
== errno
.ENOENT
:
153 # File not found. No problem.
156 print "Failed to read history '%s'; %s" % (self
._histfile
, e
)
157 atexit
.register(self
.__save
_history
)
159 def __save_history(self
):
161 readline
.write_history_file(self
._histfile
)
162 except Exception as e
:
163 print "Failed to save history file '%s'; %s" % (self
._histfile
, e
)
165 def __parse_value(self
, val
):
171 if val
.lower() == 'true':
173 if val
.lower() == 'false':
175 if val
.startswith(('{', '[')):
176 # Try first as pure JSON:
178 return json
.loads(val
)
181 # Try once again as FuzzyJSON:
183 st
= ast
.parse(val
, mode
='eval')
184 return ast
.literal_eval(FuzzyJSON().visit(st
))
191 def __cli_expr(self
, tokens
, parent
):
193 (key
, sep
, val
) = arg
.partition('=')
195 raise QMPShellError("Expected a key=value pair, got '%s'" % arg
)
197 value
= self
.__parse
_value
(val
)
198 optpath
= key
.split('.')
200 for p
in optpath
[:-1]:
202 d
= parent
.get(p
, {})
203 if type(d
) is not dict:
204 raise QMPShellError('Cannot use "%s" as both leaf and non-leaf key' % '.'.join(curpath
))
207 if optpath
[-1] in parent
:
208 if type(parent
[optpath
[-1]]) is dict:
209 raise QMPShellError('Cannot use "%s" as both leaf and non-leaf key' % '.'.join(curpath
))
211 raise QMPShellError('Cannot set "%s" multiple times' % key
)
212 parent
[optpath
[-1]] = value
214 def __build_cmd(self
, cmdline
):
216 Build a QMP input object from a user provided command-line in the
219 < command-name > [ arg-name1=arg1 ] ... [ arg-nameN=argN ]
221 cmdargs
= cmdline
.split()
223 # Transactional CLI entry/exit:
224 if cmdargs
[0] == 'transaction(':
225 self
._transmode
= True
227 elif cmdargs
[0] == ')' and self
._transmode
:
228 self
._transmode
= False
230 raise QMPShellError("Unexpected input after close of Transaction sub-shell")
231 qmpcmd
= { 'execute': 'transaction',
232 'arguments': { 'actions': self
._actions
} }
233 self
._actions
= list()
236 # Nothing to process?
240 # Parse and then cache this Transactional Action
243 action
= { 'type': cmdargs
[0], 'data': {} }
244 if cmdargs
[-1] == ')':
247 self
.__cli
_expr
(cmdargs
[1:], action
['data'])
248 self
._actions
.append(action
)
249 return self
.__build
_cmd
(')') if finalize
else None
251 # Standard command: parse and return it to be executed.
252 qmpcmd
= { 'execute': cmdargs
[0], 'arguments': {} }
253 self
.__cli
_expr
(cmdargs
[1:], qmpcmd
['arguments'])
256 def _print(self
, qmp
):
260 jsobj
= json
.dumps(qmp
, indent
=indent
)
263 def _execute_cmd(self
, cmdline
):
265 qmpcmd
= self
.__build
_cmd
(cmdline
)
266 except Exception as e
:
267 print 'Error while parsing command line: %s' % e
268 print 'command format: <command-name> ',
269 print '[arg-name1=arg1] ... [arg-nameN=argN]'
271 # For transaction mode, we may have just cached the action:
276 resp
= self
.cmd_obj(qmpcmd
)
283 def connect(self
, negotiate
):
284 self
._greeting
= super(QMPShell
, self
).connect(negotiate
)
285 self
.__completer
_setup
()
287 def show_banner(self
, msg
='Welcome to the QMP low-level shell!'):
289 if not self
._greeting
:
292 version
= self
._greeting
['QMP']['version']['qemu']
293 print 'Connected to QEMU %d.%d.%d\n' % (version
['major'],version
['minor'],version
['micro'])
295 def get_prompt(self
):
300 def read_exec_command(self
, prompt
):
302 Read and execute a command.
304 @return True if execution was ok, return False if disconnected.
307 cmdline
= raw_input(prompt
)
312 for ev
in self
.get_events():
317 return self
._execute
_cmd
(cmdline
)
319 def set_verbosity(self
, verbose
):
320 self
._verbose
= verbose
322 class HMPShell(QMPShell
):
323 def __init__(self
, address
):
324 QMPShell
.__init
__(self
, address
)
327 def __cmd_completion(self
):
328 for cmd
in self
.__cmd
_passthrough
('help')['return'].split('\r\n'):
329 if cmd
and cmd
[0] != '[' and cmd
[0] != '\t':
330 name
= cmd
.split()[0] # drop help text
333 if name
.find('|') != -1:
334 # Command in the form 'foobar|f' or 'f|foobar', take the
336 opt
= name
.split('|')
341 self
._completer
.append(name
)
342 self
._completer
.append('help ' + name
) # help completion
344 def __info_completion(self
):
345 for cmd
in self
.__cmd
_passthrough
('info')['return'].split('\r\n'):
347 self
._completer
.append('info ' + cmd
.split()[1])
349 def __other_completion(self
):
351 self
._completer
.append('help info')
353 def _fill_completion(self
):
354 self
.__cmd
_completion
()
355 self
.__info
_completion
()
356 self
.__other
_completion
()
358 def __cmd_passthrough(self
, cmdline
, cpu_index
= 0):
359 return self
.cmd_obj({ 'execute': 'human-monitor-command', 'arguments':
360 { 'command-line': cmdline
,
361 'cpu-index': cpu_index
} })
363 def _execute_cmd(self
, cmdline
):
364 if cmdline
.split()[0] == "cpu":
365 # trap the cpu command, it requires special setting
367 idx
= int(cmdline
.split()[1])
368 if not 'return' in self
.__cmd
_passthrough
('info version', idx
):
369 print 'bad CPU index'
371 self
.__cpu
_index
= idx
373 print 'cpu command takes an integer argument'
375 resp
= self
.__cmd
_passthrough
(cmdline
, self
.__cpu
_index
)
379 assert 'return' in resp
or 'error' in resp
382 if len(resp
['return']) > 0:
383 print resp
['return'],
386 print '%s: %s' % (resp
['error']['class'], resp
['error']['desc'])
389 def show_banner(self
):
390 QMPShell
.show_banner(self
, msg
='Welcome to the HMP shell!')
393 sys
.stderr
.write('ERROR: %s\n' % msg
)
396 def fail_cmdline(option
=None):
398 sys
.stderr
.write('ERROR: bad command-line option \'%s\'\n' % option
)
399 sys
.stderr
.write('qmp-shell [ -v ] [ -p ] [ -H ] [ -N ] < UNIX socket path> | < TCP address:port >\n')
400 sys
.stderr
.write(' -v Verbose (echo command sent and received)\n')
401 sys
.stderr
.write(' -p Pretty-print JSON\n')
402 sys
.stderr
.write(' -H Use HMP interface\n')
403 sys
.stderr
.write(' -N Skip negotiate (for qemu-ga)\n')
415 for arg
in sys
.argv
[1:]:
432 qemu
= QMPShell(arg
, pretty
)
437 except QMPShellBadPort
:
438 die('bad port number in command-line')
441 qemu
.connect(negotiate
)
442 except qmp
.QMPConnectError
:
443 die('Didn\'t get QMP greeting message')
444 except qmp
.QMPCapabilitiesError
:
445 die('Could not negotiate capabilities')
447 die('Could not connect to %s' % addr
)
450 qemu
.set_verbosity(verbose
)
451 while qemu
.read_exec_command(qemu
.get_prompt()):
455 if __name__
== '__main__':