| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
import os |
| 5 |
import logging |
| 6 |
import libvirt |
| 7 |
import tty |
| 8 |
import termios |
| 9 |
import atexit |
| 10 |
from argparse import ArgumentParser |
| 11 |
from typing import Optional |
| 12 |
|
| 13 |
|
| 14 |
def reset_term() -> None: |
| 15 |
termios.tcsetattr(0, termios.TCSADRAIN, attrs) |
| 16 |
|
| 17 |
|
| 18 |
def error_handler(unused, error) -> None: |
| 19 |
|
| 20 |
if error[0] == libvirt.VIR_ERR_RPC and error[1] == libvirt.VIR_FROM_STREAMS: |
| 21 |
return |
| 22 |
logging.warning(error) |
| 23 |
|
| 24 |
|
| 25 |
class Console(object): |
| 26 |
def __init__(self, uri: str, uuid: str) -> None: |
| 27 |
self.uri = uri |
| 28 |
self.uuid = uuid |
| 29 |
self.connection = libvirt.open(uri) |
| 30 |
self.domain = self.connection.lookupByUUIDString(uuid) |
| 31 |
self.state = self.domain.state(0) |
| 32 |
self.connection.domainEventRegister(lifecycle_callback, self) |
| 33 |
self.stream = None |
| 34 |
self.run_console = True |
| 35 |
self.stdin_watch = -1 |
| 36 |
logging.info("%s initial state %d, reason %d", |
| 37 |
self.uuid, self.state[0], self.state[1]) |
| 38 |
|
| 39 |
|
| 40 |
def check_console(console: Console) -> bool: |
| 41 |
if (console.state[0] == libvirt.VIR_DOMAIN_RUNNING or console.state[0] == libvirt.VIR_DOMAIN_PAUSED): |
| 42 |
if console.stream is None: |
| 43 |
console.stream = console.connection.newStream(libvirt.VIR_STREAM_NONBLOCK) |
| 44 |
console.domain.openConsole(None, console.stream, 0) |
| 45 |
console.stream.eventAddCallback(libvirt.VIR_STREAM_EVENT_READABLE, stream_callback, console) |
| 46 |
else: |
| 47 |
if console.stream: |
| 48 |
console.stream.eventRemoveCallback() |
| 49 |
console.stream = None |
| 50 |
|
| 51 |
return console.run_console |
| 52 |
|
| 53 |
|
| 54 |
def stdin_callback(watch: int, fd: int, events: int, console: Console) -> None: |
| 55 |
readbuf = os.read(fd, 1024) |
| 56 |
if readbuf.startswith(b"\x1d"): |
| 57 |
console.run_console = False |
| 58 |
return |
| 59 |
if console.stream: |
| 60 |
console.stream.send(readbuf) |
| 61 |
|
| 62 |
|
| 63 |
def stream_callback(stream: libvirt.virStream, events: int, console: Console) -> None: |
| 64 |
try: |
| 65 |
assert console.stream |
| 66 |
received_data = console.stream.recv(1024) |
| 67 |
except Exception: |
| 68 |
return |
| 69 |
os.write(0, received_data) |
| 70 |
|
| 71 |
|
| 72 |
def lifecycle_callback(connection: libvirt.virConnect, domain: libvirt.virDomain, event: int, detail: int, console: Console) -> None: |
| 73 |
console.state = console.domain.state(0) |
| 74 |
logging.info("%s transitioned to state %d, reason %d", |
| 75 |
console.uuid, console.state[0], console.state[1]) |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
parser = ArgumentParser(epilog="Example: %(prog)s 'qemu:///system' '32ad945f-7e78-c33a-e96d-39f25e025d81'") |
| 80 |
parser.add_argument("uri") |
| 81 |
parser.add_argument("uuid") |
| 82 |
args = parser.parse_args() |
| 83 |
|
| 84 |
print("Escape character is ^]") |
| 85 |
logging.basicConfig(filename='msg.log', level=logging.DEBUG) |
| 86 |
logging.info("URI: %s", args.uri) |
| 87 |
logging.info("UUID: %s", args.uuid) |
| 88 |
|
| 89 |
libvirt.virEventRegisterDefaultImpl() |
| 90 |
libvirt.registerErrorHandler(error_handler, None) |
| 91 |
|
| 92 |
atexit.register(reset_term) |
| 93 |
attrs = termios.tcgetattr(0) |
| 94 |
tty.setraw(0) |
| 95 |
|
| 96 |
console = Console(args.uri, args.uuid) |
| 97 |
console.stdin_watch = libvirt.virEventAddHandle(0, libvirt.VIR_EVENT_HANDLE_READABLE, stdin_callback, console) |
| 98 |
|
| 99 |
while check_console(console): |
| 100 |
libvirt.virEventRunDefaultImpl() |