lib: Add get_default_hv to backend operations

Add a generic way for backends to report the default hypervisor
(ie. QEMU) back to the main code.

The direct implemention reflects the current way that the hypervisor
is chosen at configure time (see m4/guestfs-qemu.m4).

For the libvirt backend, we are already getting this from libvirt
domcapabilities, so we can just return that field.

Note this may return NULL (roughly "no data"), particularly in the
libvirt case because that requires us to have launched the appliance
already.
This commit is contained in:
Richard W.M. Jones
2026-03-17 14:44:53 +00:00
committed by rwmjones
parent 389c6b7583
commit ce4bfa5d08
3 changed files with 31 additions and 0 deletions

View File

@@ -331,6 +331,11 @@ struct backend_ops {
*/
char *(*create_cow_overlay) (guestfs_h *g, void *data, struct drive *drv);
/* Get the default hypervisor (qemu), if the user does not override
* this by calling guestfs_set_hv or setting LIBGUESTFS_HV.
*/
const char *(*get_default_hv) (guestfs_h *g, void *data);
/* Launch and shut down. */
int (*launch) (guestfs_h *g, void *data, const char *arg);
int (*shutdown) (guestfs_h *g, void *data, int check_for_errors);

View File

@@ -441,6 +441,23 @@ free_pid_path:
return rc;
}
/* Pick a default, arch-specific qemu. */
static const char *
get_default_hv_direct (guestfs_h *g, void *datav)
{
if (host_cpu[0] == 'i' && strchr ("3456", host_cpu[1]) &&
host_cpu[2] == '8' && host_cpu[3] == '6' && host_cpu[4] == '\0')
return "qemu-system-i386";
else if (STRPREFIX (host_cpu, "arm"))
return "qemu-system-arm";
else if (STREQ (host_cpu, "powerpc64") ||
STREQ (host_cpu, "powerpc64le") ||
STREQ (host_cpu, "ppc64le"))
return "qemu-system-ppc64";
else
return "qemu-system-" host_cpu;
}
static int
launch_direct (guestfs_h *g, void *datav, const char *arg)
{
@@ -1105,6 +1122,7 @@ max_disks_direct (guestfs_h *g, void *datav)
static struct backend_ops backend_direct_ops = {
.data_size = sizeof (struct backend_direct_data),
.create_cow_overlay = create_cow_overlay_direct,
.get_default_hv = get_default_hv_direct,
.launch = launch_direct,
.shutdown = shutdown_direct,
.get_pid = get_pid_direct,

View File

@@ -305,6 +305,13 @@ create_cow_overlay_libvirt (guestfs_h *g, void *datav, struct drive *drv)
return overlay;
}
static const char *
get_default_hv_libvirt (guestfs_h *g, void *datav)
{
struct backend_libvirt_data *data = datav;
return data->default_qemu;
}
static int
launch_libvirt (guestfs_h *g, void *datav, const char *libvirt_uri)
{
@@ -2211,6 +2218,7 @@ max_disks_libvirt (guestfs_h *g, void *datav)
static struct backend_ops backend_libvirt_ops = {
.data_size = sizeof (struct backend_libvirt_data),
.create_cow_overlay = create_cow_overlay_libvirt,
.get_default_hv = get_default_hv_libvirt,
.launch = launch_libvirt,
.shutdown = shutdown_libvirt,
.max_disks = max_disks_libvirt,