【问题标题】:Event Callback function called twice for each event in libvirt为 libvirt 中的每个事件调用两次事件回调函数
【发布时间】:2020-08-16 11:03:08
【问题描述】:

newVM 是一个创建新虚拟机的函数。对于每个来宾 VM,事件回调函数将被执行 2 次,尽管计时器回调在每个时间间隔仅正确执行一次。我无法找出相同的原因

eventLoopThread = None
libvirt.virEventRegisterDefaultImpl()

def virEventLoopNativeStart():
    global eventLoopThread
    eventLoopThread = threading.Thread(target=virEventLoopNativeRun, name="libvirtEventLoop")
    eventLoopThread.setDaemon(True)
    eventLoopThread.start()

def domainEventCallback(conn, dom, event, detail, opaque):
    print(" event callback %s %s %s", event, dom.name(), dom.state())

def domainTimeoutCalllback(timer, opaque):
    print(" Timeout ")

def virEventLoopNativeRun():
    while True:
        libvirt.virEventRunDefaultImpl()

virEventLoopNativeStart()
conn = libvirt.open('qemu:///system')
if conn == None:
    print('Failed to open connection to qemu:///system', file=sys.stderr)
    exit(1)
conn.domainEventRegister(domainEventCallback, None)
conn.setKeepAlive(5, 3)

def newVM(xmldata): #xmldata is XML to define guest machine
    try: 
        conn = libvirt.open('qemu:///system')
        if conn == None:
            print('Failed to open connection to qemu:///system', file=sys.stderr)
            exit(1)

        dom = conn.defineXML(xmldata)
        if dom == None:
            print('Failed to define a domain from an XML definition.', file=sys.stderr)
            exit(1)

        if dom.create() < 0:
            print('Can not boot guest domain.', file=sys.stderr)
            exit(1)
    
        libvirt.virEventAddTimeout(runTime, domainTimeoutCalllback, None)
    except libvirt.libvirtError:
        abort(404, "Libvirt could not be configured")


【问题讨论】:

    标签: python virtual-machine qemu libvirt hypervisor


    【解决方案1】:

    在启动过程中多次调用域事件回调是正常的 - 每次都会被赋予不同的参数值。

    【讨论】:

    • 我通过登录控制台检查了事件回调函数中的 dom.state、event、dom.ID、dom.name、timestamp。我为每个事件获得两个条目,时间戳差异小于 0.0001。即使我调用 'virsh destroy domain' 会导致相同日志执行两次的事件回调。
    猜你喜欢
    • 2011-07-07
    • 1970-01-01
    • 2014-01-24
    • 1970-01-01
    • 2015-10-06
    • 2022-08-20
    • 2015-10-29
    • 2016-04-23
    • 1970-01-01
    相关资源
    最近更新 更多