Peter Krempa wrote:

> On Sat, Apr 11, 2026 at 15:35:12 +0200, Roman Bogorodskiy wrote:
> > Implement the domainInterfaceAddresses and domainGetHostname APIs.
> > These APIs could use multiple sources of information, though
> > for bhyve only the 'lease' source is supported.
> 
> These would go better if split to 2 patches.
> 
> 
> > 
> > Signed-off-by: Roman Bogorodskiy <[email protected]>
> > ---
> >  src/bhyve/bhyve_driver.c | 149 +++++++++++++++++++++++++++++++++++++++
> >  1 file changed, 149 insertions(+)
> > 
> > diff --git a/src/bhyve/bhyve_driver.c b/src/bhyve/bhyve_driver.c
> > index a7bd16704e..33a772ccdb 100644
> > --- a/src/bhyve/bhyve_driver.c
> > +++ b/src/bhyve/bhyve_driver.c
> 
> [...]
> 
> > +static int
> > +bhyveDomainGetHostnameLease(virDomainObj *vm,
> > +                            char **hostname)
> > +{
> > +    char macaddr[VIR_MAC_STRING_BUFLEN];
> > +    g_autoptr(virConnect) conn = NULL;
> > +    virNetworkDHCPLeasePtr *leases = NULL;
> > +    int n_leases;
> > +    size_t i, j;
> > +    int ret = -1;
> > +
> > +    if (virDomainObjBeginJob(vm, VIR_JOB_QUERY) < 0)
> > +        return -1;
> > +
> > +    if (virDomainObjCheckActive(vm) < 0)
> > +        goto endjob;
> > +
> > +    if (!(conn = virGetConnectNetwork()))
> > +        goto endjob;
> > +
> > +    for (i = 0; i < vm->def->nnets; i++) {
> > +        g_autoptr(virNetwork) network = NULL;
> > +        virDomainNetDef *net = vm->def->nets[i];
> > +
> > +        if (net->type != VIR_DOMAIN_NET_TYPE_NETWORK)
> > +            continue;
> > +
> > +        virMacAddrFormat(&net->mac, macaddr);
> > +        network = virNetworkLookupByName(conn, net->data.network.name);
> > +
> > +        if (!network)
> > +            goto endjob;
> > +
> > +        if ((n_leases = virNetworkGetDHCPLeases(network, macaddr,
> > +                                                &leases, 0)) < 0)
> > +            goto endjob;
> > +
> > +        for (j = 0; j < n_leases; j++) {
> > +            virNetworkDHCPLeasePtr lease = leases[j];
> > +            if (lease->hostname && !*hostname)
> > +                *hostname = g_strdup(lease->hostname);
> > +
> > +            virNetworkDHCPLeaseFree(lease);
> > +        }
> > +
> > +        VIR_FREE(leases);
> > +
> > +        if (*hostname)
> > +            break;
> > +    }
> > +
> > +    ret = 0;
> > + endjob:
> > +    virDomainObjEndJob(vm);
> > +    return ret;
> > +}
> > +
> > +static char *
> > +bhyveDomainGetHostname(virDomainPtr domain,
> > +                       unsigned int flags)
> > +{
> > +    virDomainObj *vm = NULL;
> > +    char *hostname = NULL;
> > +
> > +    virCheckFlags(VIR_DOMAIN_GET_HOSTNAME_LEASE |
> > +                  VIR_DOMAIN_GET_HOSTNAME_AGENT, NULL);
> 
> IMO you shouldn't allow the _AGENT flag here ...
> 
> 
> > +
> > +    VIR_EXCLUSIVE_FLAGS_RET(VIR_DOMAIN_GET_HOSTNAME_LEASE,
> > +                            VIR_DOMAIN_GET_HOSTNAME_AGENT,
> > +                            NULL);
> > +
> > +    if (!(flags & VIR_DOMAIN_GET_HOSTNAME_AGENT))
> > +        flags |= VIR_DOMAIN_GET_HOSTNAME_LEASE;
> > +
> > +    if (!(vm = bhyveDomObjFromDomain(domain)))
> > +        return NULL;
> > +
> > +    if (virDomainGetHostnameEnsureACL(domain->conn, vm->def) < 0)
> > +        goto cleanup;
> > +
> > +    if (flags & VIR_DOMAIN_GET_HOSTNAME_AGENT) {
> > +        virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
> > +                       _("getting hostname from agent is not supported"));
> > +        goto cleanup;
> 
> ... if you intend to not support it.
> 
> At this point it doesn't matter that much (just one extra error message
> for the translators). I'm working on an API to probe supported flags
> though at which point it would matter as this would signal that the
> _AGENT option is supported.

I intend to support the agent too.

Actually, I'm currently working on implementing a bunch of APIs using
the Qemu guest agent in my private branch, and I decided to submit these
two changes separately to reduce size of the agent series when it's
ready.

> 
> > +    } else if (flags & VIR_DOMAIN_GET_HOSTNAME_LEASE) {
> > +        if (bhyveDomainGetHostnameLease(vm, &hostname) < 0)
> > +            goto cleanup;
> > +    }
> > +
> > +    if (!hostname) {
> > +        virReportError(VIR_ERR_NO_HOSTNAME,
> > +                       _("no hostname found for domain %1$s"),
> > +                       vm->def->name);
> > +        goto cleanup;
> > +    }
> 
> With the handling of _AGENT fixed:
> 
> Reviewed-by: Peter Krempa <[email protected]>
> 

Reply via email to