From 06d383abe97b94603edb6fd68f318ad1b3ad4685 Mon Sep 17 00:00:00 2001 From: maseman <12774575+maseman@users.noreply.github.com> Date: Thu, 12 Aug 2021 23:34:19 +0000 Subject: [PATCH] qemu-guest-agent network interface query (re)implemented Uses libvirt.DomainInterfaceAddressesSrcAgent in call to virConn.DomainInterfaceAddresses --- libvirt/domain.go | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/libvirt/domain.go b/libvirt/domain.go index f52b7e546..1aba5d535 100644 --- a/libvirt/domain.go +++ b/libvirt/domain.go @@ -154,27 +154,29 @@ func domainGetIfacesInfo(virConn *libvirt.Libvirt, domain libvirt.Domain, rd *sc return []libvirt.DomainInterface{}, nil } + // setup source of interface address information + var addrsrc uint32 qemuAgentEnabled := rd.Get("qemu_agent").(bool) if qemuAgentEnabled { - log.Print("[DEBUG] Not implemented") + addrsrc = uint32(libvirt.DomainInterfaceAddressesSrcAgent) + log.Printf("[DEBUG] qemu-agent used to query interface info") } else { - log.Printf("[DEBUG] qemu-agent is not used") + addrsrc = uint32(libvirt.DomainInterfaceAddressesSrcLease) + log.Printf("[DEBUG] Obtain interface info from dhcp lease file") } - var interfaces []libvirt.DomainInterface // get all the interfaces attached to libvirt networks - log.Print("[DEBUG] no interfaces could be obtained with qemu-agent: falling back to the libvirt API") - - interfaces, err = virConn.DomainInterfaceAddresses(domain, uint32(libvirt.DomainInterfaceAddressesSrcLease), 0) + var interfaces []libvirt.DomainInterface + interfaces, err = virConn.DomainInterfaceAddresses(domain, addrsrc, 0) if err != nil { switch virErr := err.(type) { default: - return interfaces, fmt.Errorf("error retrieving interface addresses: %s", virErr) + return interfaces, fmt.Errorf("error retrieving interface addresses: %w", virErr) case libvirt.Error: - // FIXME ErrorDomain.fromQemu not available in libvirt.Error - // || libvirt.ErrorvirErr.Domain != libvirt.FROM_QEMU { - if virErr.Code != uint32(libvirt.ErrOperationInvalid) { - return interfaces, fmt.Errorf("error retrieving interface addresses: %s", err) + // Agent can be unresponsive if being installed/setup + if addrsrc == uint32(libvirt.DomainInterfaceAddressesSrcLease) && virErr.Code != uint32(libvirt.ErrOperationInvalid) || + addrsrc == uint32(libvirt.DomainInterfaceAddressesSrcAgent) && virErr.Code != uint32(libvirt.ErrAgentUnresponsive) { + return interfaces, fmt.Errorf("Error retrieving interface addresses: %w", err) } } }