2009-11-27 03:59:09 +03:00
|
|
|
#!/usr/bin/python
|
|
|
|
#
|
2010-10-27 23:57:51 +04:00
|
|
|
# Low-level QEMU shell on top of QMP.
|
2009-11-27 03:59:09 +03:00
|
|
|
#
|
2010-10-27 23:57:51 +04:00
|
|
|
# Copyright (C) 2009, 2010 Red Hat Inc.
|
2009-11-27 03:59:09 +03:00
|
|
|
#
|
|
|
|
# Authors:
|
|
|
|
# Luiz Capitulino <lcapitulino@redhat.com>
|
|
|
|
#
|
|
|
|
# This work is licensed under the terms of the GNU GPL, version 2. See
|
|
|
|
# the COPYING file in the top-level directory.
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
#
|
|
|
|
# Start QEMU with:
|
|
|
|
#
|
2010-10-27 23:57:51 +04:00
|
|
|
# # qemu [...] -qmp unix:./qmp-sock,server
|
2009-11-27 03:59:09 +03:00
|
|
|
#
|
|
|
|
# Run the shell:
|
|
|
|
#
|
2010-10-27 23:57:51 +04:00
|
|
|
# $ qmp-shell ./qmp-sock
|
2009-11-27 03:59:09 +03:00
|
|
|
#
|
|
|
|
# Commands have the following format:
|
|
|
|
#
|
2010-10-27 23:57:51 +04:00
|
|
|
# < command-name > [ arg-name1=arg1 ] ... [ arg-nameN=argN ]
|
2009-11-27 03:59:09 +03:00
|
|
|
#
|
|
|
|
# For example:
|
|
|
|
#
|
2010-10-27 23:57:51 +04:00
|
|
|
# (QEMU) device_add driver=e1000 id=net1
|
|
|
|
# {u'return': {}}
|
|
|
|
# (QEMU)
|
2009-11-27 03:59:09 +03:00
|
|
|
|
|
|
|
import qmp
|
|
|
|
import readline
|
2010-10-27 23:57:51 +04:00
|
|
|
import sys
|
2009-11-27 03:59:09 +03:00
|
|
|
|
2010-10-27 23:57:51 +04:00
|
|
|
class QMPCompleter(list):
|
|
|
|
def complete(self, text, state):
|
|
|
|
for cmd in self:
|
|
|
|
if cmd.startswith(text):
|
|
|
|
if not state:
|
|
|
|
return cmd
|
|
|
|
else:
|
|
|
|
state -= 1
|
2009-11-27 03:59:09 +03:00
|
|
|
|
2010-10-27 23:57:51 +04:00
|
|
|
class QMPShellError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class QMPShellBadPort(QMPShellError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
# TODO: QMPShell's interface is a bit ugly (eg. _fill_completion() and
|
|
|
|
# _execute_cmd()). Let's design a better one.
|
|
|
|
class QMPShell(qmp.QEMUMonitorProtocol):
|
|
|
|
def __init__(self, address):
|
|
|
|
qmp.QEMUMonitorProtocol.__init__(self, self.__get_address(address))
|
|
|
|
self._greeting = None
|
|
|
|
self._completer = None
|
|
|
|
|
|
|
|
def __get_address(self, arg):
|
|
|
|
"""
|
|
|
|
Figure out if the argument is in the port:host form, if it's not it's
|
|
|
|
probably a file path.
|
|
|
|
"""
|
|
|
|
addr = arg.split(':')
|
|
|
|
if len(addr) == 2:
|
|
|
|
try:
|
|
|
|
port = int(addr[1])
|
|
|
|
except ValueError:
|
|
|
|
raise QMPShellBadPort
|
|
|
|
return ( addr[0], port )
|
|
|
|
# socket path
|
|
|
|
return arg
|
|
|
|
|
|
|
|
def _fill_completion(self):
|
|
|
|
for cmd in self.cmd('query-commands')['return']:
|
|
|
|
self._completer.append(cmd['name'])
|
|
|
|
|
|
|
|
def __completer_setup(self):
|
|
|
|
self._completer = QMPCompleter()
|
|
|
|
self._fill_completion()
|
|
|
|
readline.set_completer(self._completer.complete)
|
|
|
|
readline.parse_and_bind("tab: complete")
|
|
|
|
# XXX: default delimiters conflict with some command names (eg. query-),
|
|
|
|
# clearing everything as it doesn't seem to matter
|
|
|
|
readline.set_completer_delims('')
|
|
|
|
|
|
|
|
def __build_cmd(self, cmdline):
|
|
|
|
"""
|
|
|
|
Build a QMP input object from a user provided command-line in the
|
|
|
|
following format:
|
|
|
|
|
|
|
|
< command-name > [ arg-name1=arg1 ] ... [ arg-nameN=argN ]
|
|
|
|
"""
|
|
|
|
cmdargs = cmdline.split()
|
|
|
|
qmpcmd = { 'execute': cmdargs[0], 'arguments': {} }
|
|
|
|
for arg in cmdargs[1:]:
|
|
|
|
opt = arg.split('=')
|
|
|
|
try:
|
|
|
|
value = int(opt[1])
|
|
|
|
except ValueError:
|
|
|
|
value = opt[1]
|
|
|
|
qmpcmd['arguments'][opt[0]] = value
|
|
|
|
return qmpcmd
|
|
|
|
|
|
|
|
def _execute_cmd(self, cmdline):
|
|
|
|
try:
|
|
|
|
qmpcmd = self.__build_cmd(cmdline)
|
|
|
|
except:
|
|
|
|
print 'command format: <command-name> ',
|
|
|
|
print '[arg-name1=arg1] ... [arg-nameN=argN]'
|
|
|
|
return True
|
|
|
|
resp = self.cmd_obj(qmpcmd)
|
|
|
|
if resp is None:
|
|
|
|
print 'Disconnected'
|
|
|
|
return False
|
|
|
|
print resp
|
|
|
|
return True
|
|
|
|
|
|
|
|
def connect(self):
|
|
|
|
self._greeting = qmp.QEMUMonitorProtocol.connect(self)
|
|
|
|
self.__completer_setup()
|
2009-11-27 03:59:09 +03:00
|
|
|
|
2010-10-27 23:57:51 +04:00
|
|
|
def show_banner(self, msg='Welcome to the QMP low-level shell!'):
|
|
|
|
print msg
|
|
|
|
version = self._greeting['QMP']['version']['qemu']
|
|
|
|
print 'Connected to QEMU %d.%d.%d\n' % (version['major'],version['minor'],version['micro'])
|
2009-11-27 03:59:09 +03:00
|
|
|
|
2010-10-27 23:57:51 +04:00
|
|
|
def read_exec_command(self, prompt):
|
|
|
|
"""
|
|
|
|
Read and execute a command.
|
2009-11-27 03:59:09 +03:00
|
|
|
|
2010-10-27 23:57:51 +04:00
|
|
|
@return True if execution was ok, return False if disconnected.
|
|
|
|
"""
|
2009-11-27 03:59:09 +03:00
|
|
|
try:
|
2010-10-27 23:57:51 +04:00
|
|
|
cmdline = raw_input(prompt)
|
2009-11-27 03:59:09 +03:00
|
|
|
except EOFError:
|
|
|
|
print
|
2010-10-27 23:57:51 +04:00
|
|
|
return False
|
|
|
|
if cmdline == '':
|
|
|
|
for ev in self.get_events():
|
|
|
|
print ev
|
|
|
|
self.clear_events()
|
|
|
|
return True
|
2009-11-27 03:59:09 +03:00
|
|
|
else:
|
2010-10-27 23:57:51 +04:00
|
|
|
return self._execute_cmd(cmdline)
|
|
|
|
|
|
|
|
def die(msg):
|
|
|
|
sys.stderr.write('ERROR: %s\n' % msg)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
def fail_cmdline(option=None):
|
|
|
|
if option:
|
|
|
|
sys.stderr.write('ERROR: bad command-line option \'%s\'\n' % option)
|
|
|
|
sys.stderr.write('qemu-shell [ -H ] < UNIX socket path> | < TCP address:port >\n')
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
def main():
|
|
|
|
try:
|
|
|
|
if len(sys.argv) == 2:
|
|
|
|
qemu = QMPShell(sys.argv[1])
|
|
|
|
else:
|
|
|
|
fail_cmdline()
|
|
|
|
except QMPShellBadPort:
|
|
|
|
die('bad port number in command-line')
|
|
|
|
|
|
|
|
try:
|
|
|
|
qemu.connect()
|
|
|
|
except qmp.QMPConnectError:
|
|
|
|
die('Didn\'t get QMP greeting message')
|
|
|
|
except qmp.QMPCapabilitiesError:
|
|
|
|
die('Could not negotiate capabilities')
|
|
|
|
except qemu.error:
|
|
|
|
die('Could not connect to %s' % sys.argv[1])
|
|
|
|
|
|
|
|
qemu.show_banner()
|
|
|
|
while qemu.read_exec_command('(QEMU) '):
|
|
|
|
pass
|
|
|
|
qemu.close()
|
2009-11-27 03:59:09 +03:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|