[libvirt] serial console/events example script

Hi all, A while back I wrote the attached code to demonstrate how to use events and serial console to create a serial console that stays up even when the VM is down. Is it worth adding to the examples? It might need some work, as I am not terribly strong with Python. Dave

On Wed, Feb 15, 2012 at 09:11:32PM -0500, Dave Allan wrote:
Hi all,
A while back I wrote the attached code to demonstrate how to use events and serial console to create a serial console that stays up even when the VM is down. Is it worth adding to the examples? It might need some work, as I am not terribly strong with Python.
Dave
I was just going through some old mail and realized that I don't think this ever got a response. Anybody have any thoughts on it? Dave
#!/usr/bin/python -u import sys, os, logging, libvirt, tty, termios, atexit
def reset_term(): termios.tcsetattr(0, termios.TCSADRAIN, attrs)
def error_handler(unused, error): # The console stream errors on VM shutdown; we don't care if (error[0] == libvirt.VIR_ERR_RPC and error[1] == libvirt.VIR_FROM_STREAMS): return logging.warn(error)
class Console(object): def __init__(self, uri, uuid): self.uri = uri self.uuid = uuid self.connection = libvirt.open(uri) self.domain = self.connection.lookupByUUIDString(uuid) self.state = self.domain.state(0) self.connection.domainEventRegister(lifecycle_callback, self) self.stream = None self.run_console = True logging.info("%s initial state %d, reason %d", self.uuid, self.state[0], self.state[1])
def check_console(console): if (console.state[0] == libvirt.VIR_DOMAIN_RUNNING or console.state[0] == libvirt.VIR_DOMAIN_PAUSED): if console.stream == None: console.stream = console.connection.newStream(libvirt.VIR_STREAM_NONBLOCK) console.domain.openConsole(None, console.stream, 0) console.stream.eventAddCallback(libvirt.VIR_STREAM_EVENT_READABLE, stream_callback, console) else: if console.stream: console.stream.eventRemoveCallback() console.stream = None
return console.run_console
def stdin_callback(watch, fd, events, console): readbuf = os.read(fd, 1024) if readbuf.startswith(""): console.run_console = False return if console.stream: console.stream.send(readbuf)
def stream_callback(stream, events, console): try: received_data = console.stream.recv(1024) except: return os.write(0, received_data)
def lifecycle_callback (connection, domain, event, detail, console): console.state = console.domain.state(0) logging.info("%s transitioned to state %d, reason %d", console.uuid, console.state[0], console.state[1])
# main if len(sys.argv) != 3: print "Usage:", sys.argv[0], "URI UUID" print "for example:", sys.argv[0], "'qemu:///system' '32ad945f-7e78-c33a-e96d-39f25e025d81'" sys.exit(1)
uri = sys.argv[1] uuid = sys.argv[2]
print "Escape character is ^]" logging.basicConfig(filename='msg.log', level=logging.DEBUG) logging.info("URI: %s", uri) logging.info("UUID: %s", uuid)
libvirt.virEventRegisterDefaultImpl() libvirt.registerErrorHandler(error_handler, None)
atexit.register(reset_term) attrs = termios.tcgetattr(0) tty.setraw(0)
console = Console(uri, uuid) console.stdin_watch = libvirt.virEventAddHandle(0, libvirt.VIR_EVENT_HANDLE_READABLE, stdin_callback, console)
while check_console(console): libvirt.virEventRunDefaultImpl()

On Thu, Mar 29, 2012 at 04:55:39PM -0400, Dave Allan wrote:
On Wed, Feb 15, 2012 at 09:11:32PM -0500, Dave Allan wrote:
Hi all,
A while back I wrote the attached code to demonstrate how to use events and serial console to create a serial console that stays up even when the VM is down. Is it worth adding to the examples? It might need some work, as I am not terribly strong with Python.
Dave
I was just going through some old mail and realized that I don't think this ever got a response. Anybody have any thoughts on it?
Add it to the examples/ directory in GIT I say. Daniel -- |: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|

On 03/29/2012 03:57 PM, Daniel P. Berrange wrote:
On Thu, Mar 29, 2012 at 04:55:39PM -0400, Dave Allan wrote:
On Wed, Feb 15, 2012 at 09:11:32PM -0500, Dave Allan wrote:
Hi all,
A while back I wrote the attached code to demonstrate how to use events and serial console to create a serial console that stays up even when the VM is down. Is it worth adding to the examples? It might need some work, as I am not terribly strong with Python.
Dave
I was just going through some old mail and realized that I don't think this ever got a response. Anybody have any thoughts on it?
Add it to the examples/ directory in GIT I say.
Agreed. Dave, do you need help incorporating it into the actual libvirt.git tree? -- Eric Blake eblake@redhat.com +1-919-301-3266 Libvirt virtualization library http://libvirt.org

On Fri, May 25, 2012 at 08:34:20PM -0600, Eric Blake wrote:
On 03/29/2012 03:57 PM, Daniel P. Berrange wrote:
On Thu, Mar 29, 2012 at 04:55:39PM -0400, Dave Allan wrote:
On Wed, Feb 15, 2012 at 09:11:32PM -0500, Dave Allan wrote:
Hi all,
A while back I wrote the attached code to demonstrate how to use events and serial console to create a serial console that stays up even when the VM is down. Is it worth adding to the examples? It might need some work, as I am not terribly strong with Python.
Dave
I was just going through some old mail and realized that I don't think this ever got a response. Anybody have any thoughts on it?
Add it to the examples/ directory in GIT I say.
Agreed. Dave, do you need help incorporating it into the actual libvirt.git tree?
Thanks, that'd be great--I don't have an r/w tree atm. Dave
-- Eric Blake eblake@redhat.com +1-919-301-3266 Libvirt virtualization library http://libvirt.org

On 05/29/2012 07:30 AM, Dave Allan wrote:
A while back I wrote the attached code to demonstrate how to use events and serial console to create a serial console that stays up even when the VM is down. Is it worth adding to the examples? It might need some work, as I am not terribly strong with Python.
Add it to the examples/ directory in GIT I say.
Agreed. Dave, do you need help incorporating it into the actual libvirt.git tree?
Thanks, that'd be great--I don't have an r/w tree atm.
Done: From bc8296d2e31d1f7a51485d4c564e50da018ad093 Mon Sep 17 00:00:00 2001 From: Dave Allan <dallan@redhat.com> Date: Tue, 29 May 2012 16:49:13 -0600 Subject: [PATCH] examples: add consolecallback example python script A while back I wrote the attached code to demonstrate how to use events and serial console to create a serial console that stays up even when the VM is down. It might need some work, as I am not terribly strong with Python. * examples/python/consolecallback.py: New file. * examples/python/Makefile.am (EXTRA_DIST): Ship it. --- examples/python/Makefile.am | 3 +- examples/python/consolecallback.py | 88 ++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 1 deletions(-) create mode 100644 examples/python/consolecallback.py diff --git a/examples/python/Makefile.am b/examples/python/Makefile.am index d782a28..b04d126 100644 --- a/examples/python/Makefile.am +++ b/examples/python/Makefile.am @@ -1,6 +1,7 @@ -## Copyright (C) 2005-2011 Red Hat, Inc. +## Copyright (C) 2005-2012 Red Hat, Inc. ## See COPYING.LIB for the License of this software EXTRA_DIST= \ README \ + consolecallback.py \ dominfo.py domrestore.py domsave.py domstart.py esxlist.py diff --git a/examples/python/consolecallback.py b/examples/python/consolecallback.py new file mode 100644 index 0000000..23316ce --- /dev/null +++ b/examples/python/consolecallback.py @@ -0,0 +1,88 @@ +#!/usr/bin/env python +# consolecallback - provide a persistent console that survives guest reboots + +import sys, os, logging, libvirt, tty, termios, atexit ... -- Eric Blake eblake@redhat.com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
participants (3)
-
Daniel P. Berrange
-
Dave Allan
-
Eric Blake