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
37 class QMPCompleter(list):
38 def complete(self
, text
, state
):
40 if cmd
.startswith(text
):
46 class QMPShellError(Exception):
49 class QMPShellBadPort(QMPShellError
):
52 # TODO: QMPShell's interface is a bit ugly (eg. _fill_completion() and
53 # _execute_cmd()). Let's design a better one.
54 class QMPShell(qmp
.QEMUMonitorProtocol
):
55 def __init__(self
, address
):
56 qmp
.QEMUMonitorProtocol
.__init
__(self
, self
.__get
_address
(address
))
58 self
._completer
= None
60 def __get_address(self
, arg
):
62 Figure out if the argument is in the port:host form, if it's not it's
71 return ( addr
[0], port
)
75 def _fill_completion(self
):
76 for cmd
in self
.cmd('query-commands')['return']:
77 self
._completer
.append(cmd
['name'])
79 def __completer_setup(self
):
80 self
._completer
= QMPCompleter()
81 self
._fill
_completion
()
82 readline
.set_completer(self
._completer
.complete
)
83 readline
.parse_and_bind("tab: complete")
84 # XXX: default delimiters conflict with some command names (eg. query-),
85 # clearing everything as it doesn't seem to matter
86 readline
.set_completer_delims('')
88 def __build_cmd(self
, cmdline
):
90 Build a QMP input object from a user provided command-line in the
93 < command-name > [ arg-name1=arg1 ] ... [ arg-nameN=argN ]
95 cmdargs
= cmdline
.split()
96 qmpcmd
= { 'execute': cmdargs
[0], 'arguments': {} }
97 for arg
in cmdargs
[1:]:
103 qmpcmd
['arguments'][opt
[0]] = value
106 def _execute_cmd(self
, cmdline
):
108 qmpcmd
= self
.__build
_cmd
(cmdline
)
110 print 'command format: <command-name> ',
111 print '[arg-name1=arg1] ... [arg-nameN=argN]'
113 resp
= self
.cmd_obj(qmpcmd
)
121 self
._greeting
= qmp
.QEMUMonitorProtocol
.connect(self
)
122 self
.__completer
_setup
()
124 def show_banner(self
, msg
='Welcome to the QMP low-level shell!'):
126 version
= self
._greeting
['QMP']['version']['qemu']
127 print 'Connected to QEMU %d.%d.%d\n' % (version
['major'],version
['minor'],version
['micro'])
129 def read_exec_command(self
, prompt
):
131 Read and execute a command.
133 @return True if execution was ok, return False if disconnected.
136 cmdline
= raw_input(prompt
)
141 for ev
in self
.get_events():
146 return self
._execute
_cmd
(cmdline
)
149 sys
.stderr
.write('ERROR: %s\n' % msg
)
152 def fail_cmdline(option
=None):
154 sys
.stderr
.write('ERROR: bad command-line option \'%s\'\n' % option
)
155 sys
.stderr
.write('qemu-shell [ -H ] < UNIX socket path> | < TCP address:port >\n')
160 if len(sys
.argv
) == 2:
161 qemu
= QMPShell(sys
.argv
[1])
164 except QMPShellBadPort
:
165 die('bad port number in command-line')
169 except qmp
.QMPConnectError
:
170 die('Didn\'t get QMP greeting message')
171 except qmp
.QMPCapabilitiesError
:
172 die('Could not negotiate capabilities')
174 die('Could not connect to %s' % sys
.argv
[1])
177 while qemu
.read_exec_command('(QEMU) '):
181 if __name__
== '__main__':