1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2025-03-20 06:50:22 +03:00

src: convert drivers over to use new autostart helper

This eliminates some duplicated code patterns aross drivers.

Reviewed-by: Peter Krempa <pkrempa@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrangé 2024-12-16 19:26:13 +00:00
parent c0cdbecdaa
commit c23554fc34
6 changed files with 52 additions and 110 deletions

View File

@ -54,6 +54,7 @@
#include "virportallocator.h"
#include "conf/domain_capabilities.h"
#include "virutil.h"
#include "domain_driver.h"
#include "bhyve_conf.h"
#include "bhyve_device.h"
@ -70,30 +71,19 @@ VIR_LOG_INIT("bhyve.bhyve_driver");
struct _bhyveConn *bhyve_driver = NULL;
static int
static void
bhyveAutostartDomain(virDomainObj *vm, void *opaque)
{
bhyveConn *driver = opaque;
int ret = 0;
VIR_LOCK_GUARD lock = virObjectLockGuard(vm);
if (vm->autostart && !virDomainObjIsActive(vm)) {
virResetLastError();
ret = virBhyveProcessStart(driver, NULL, vm,
VIR_DOMAIN_RUNNING_BOOTED, 0);
if (ret < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed to autostart VM '%1$s': %2$s"),
vm->def->name, virGetLastErrorMessage());
}
ret = virBhyveProcessStart(driver, NULL, vm,
VIR_DOMAIN_RUNNING_BOOTED, 0);
if (ret < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed to autostart VM '%1$s': %2$s"),
vm->def->name, virGetLastErrorMessage());
}
return ret;
}
static void
bhyveAutostartDomains(struct _bhyveConn *driver)
{
virDomainObjListForEach(driver->domains, false, bhyveAutostartDomain, driver);
}
/**
@ -1169,7 +1159,7 @@ bhyveStateInitialize(bool privileged,
virStateInhibitCallback callback G_GNUC_UNUSED,
void *opaque G_GNUC_UNUSED)
{
bool autostart = true;
virDomainDriverAutoStartConfig autostartCfg;
if (root != NULL) {
virReportError(VIR_ERR_INVALID_ARG, "%s",
@ -1254,11 +1244,12 @@ bhyveStateInitialize(bool privileged,
virBhyveProcessReconnectAll(bhyve_driver);
if (virDriverShouldAutostart(BHYVE_STATE_DIR, &autostart) < 0)
goto cleanup;
if (autostart)
bhyveAutostartDomains(bhyve_driver);
autostartCfg = (virDomainDriverAutoStartConfig) {
.stateDir = BHYVE_STATE_DIR,
.callback = bhyveAutostartDomain,
.opaque = bhyve_driver,
};
virDomainDriverAutoStart(bhyve_driver->domains, &autostartCfg);
return VIR_DRV_STATE_INIT_COMPLETE;

View File

@ -315,36 +315,22 @@ libxlDomObjFromDomain(virDomainPtr dom)
return vm;
}
static int
static void
libxlAutostartDomain(virDomainObj *vm,
void *opaque)
{
libxlDriverPrivate *driver = opaque;
int ret = -1;
virObjectRef(vm);
virObjectLock(vm);
virResetLastError();
if (virDomainObjBeginJob(vm, VIR_JOB_MODIFY) < 0)
goto cleanup;
return;
if (vm->autostart && !virDomainObjIsActive(vm) &&
libxlDomainStartNew(driver, vm, false) < 0) {
if (libxlDomainStartNew(driver, vm, false) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed to autostart VM '%1$s': %2$s"),
vm->def->name, virGetLastErrorMessage());
goto endjob;
}
ret = 0;
endjob:
virDomainObjEndJob(vm);
cleanup:
virDomainObjEndAPI(&vm);
return ret;
}
@ -654,7 +640,7 @@ libxlStateInitialize(bool privileged,
{
libxlDriverConfig *cfg;
g_autofree char *driverConf = NULL;
bool autostart = true;
virDomainDriverAutoStartConfig autostartCfg;
if (root != NULL) {
virReportError(VIR_ERR_INVALID_ARG, "%s",
@ -807,14 +793,12 @@ libxlStateInitialize(bool privileged,
NULL, NULL) < 0)
goto error;
if (virDriverShouldAutostart(cfg->stateDir, &autostart) < 0)
goto error;
if (autostart) {
virDomainObjListForEach(libxl_driver->domains, false,
libxlAutostartDomain,
libxl_driver);
}
autostartCfg = (virDomainDriverAutoStartConfig) {
.stateDir = cfg->stateDir,
.callback = libxlAutostartDomain,
.opaque = libxl_driver,
};
virDomainDriverAutoStart(libxl_driver->domains, &autostartCfg);
virDomainObjListForEach(libxl_driver->domains, false,
libxlDomainManagedSaveLoad,

View File

@ -1402,8 +1402,8 @@ lxcStateInitialize(bool privileged,
void *opaque)
{
virLXCDriverConfig *cfg = NULL;
bool autostart = true;
const char *defsecmodel;
virDomainDriverAutoStartConfig autostartCfg;
if (root != NULL) {
virReportError(VIR_ERR_INVALID_ARG, "%s",
@ -1499,11 +1499,12 @@ lxcStateInitialize(bool privileged,
NULL, NULL) < 0)
goto cleanup;
if (virDriverShouldAutostart(cfg->stateDir, &autostart) < 0)
goto cleanup;
if (autostart)
virLXCProcessAutostartAll(lxc_driver);
autostartCfg = (virDomainDriverAutoStartConfig) {
.stateDir = cfg->stateDir,
.callback = virLXCProcessAutostartDomain,
.opaque = NULL,
};
virDomainDriverAutoStart(lxc_driver->domains, &autostartCfg);
return VIR_DRV_STATE_INIT_COMPLETE;

View File

@ -1562,19 +1562,14 @@ int virLXCProcessStart(virLXCDriver * driver,
}
static int
void
virLXCProcessAutostartDomain(virDomainObj *vm,
void *opaque G_GNUC_UNUSED)
{
VIR_LOCK_GUARD lock = virObjectLockGuard(vm);
virLXCDomainObjPrivate *priv = vm->privateData;
virObjectEvent *event;
int rc = 0;
if (!vm->autostart ||
virDomainObjIsActive(vm))
return 0;
rc = virLXCProcessStart(priv->driver, vm, 0, NULL, NULL, VIR_DOMAIN_RUNNING_BOOTED);
virDomainAuditStart(vm, "booted", rc >= 0);
@ -1582,22 +1577,13 @@ virLXCProcessAutostartDomain(virDomainObj *vm,
VIR_ERROR(_("Failed to autostart VM '%1$s': %2$s"),
vm->def->name,
virGetLastErrorMessage());
return -1;
return;
}
event = virDomainEventLifecycleNewFromObj(vm,
VIR_DOMAIN_EVENT_STARTED,
VIR_DOMAIN_EVENT_STARTED_BOOTED);
virObjectEventStateQueue(priv->driver->domainEventState, event);
return 0;
}
void
virLXCProcessAutostartAll(virLXCDriver *driver)
{
virDomainObjListForEach(driver->domains, false, virLXCProcessAutostartDomain, NULL);
}

View File

@ -34,6 +34,8 @@ int virLXCProcessStop(virLXCDriver *driver,
unsigned int cleanupFlags);
void virLXCProcessAutostartAll(virLXCDriver *driver);
void virLXCProcessAutostartDomain(virDomainObj *vm,
void *opaque);
int virLXCProcessReconnectAll(virLXCDriver *driver,
virDomainObjList *doms);

View File

@ -167,52 +167,29 @@ qemuDomObjFromSnapshot(virDomainSnapshotPtr snapshot)
static int
static void
qemuAutostartDomain(virDomainObj *vm,
void *opaque)
{
virQEMUDriver *driver = opaque;
int flags = 0;
g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
int ret = -1;
if (cfg->autoStartBypassCache)
flags |= VIR_DOMAIN_START_BYPASS_CACHE;
virObjectLock(vm);
virObjectRef(vm);
virResetLastError();
if (vm->autostart &&
!virDomainObjIsActive(vm)) {
if (qemuProcessBeginJob(vm, VIR_DOMAIN_JOB_OPERATION_START,
flags) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed to start job on VM '%1$s': %2$s"),
vm->def->name, virGetLastErrorMessage());
goto cleanup;
}
if (qemuProcessBeginJob(vm, VIR_DOMAIN_JOB_OPERATION_START,
flags) < 0)
return;
if (qemuDomainObjStart(NULL, driver, vm, flags,
VIR_ASYNC_JOB_START) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed to autostart VM '%1$s': %2$s"),
if (qemuDomainObjStart(NULL, driver, vm, flags,
VIR_ASYNC_JOB_START) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed to autostart VM '%1$s': %2$s"),
vm->def->name, virGetLastErrorMessage());
}
qemuProcessEndJob(vm);
}
ret = 0;
cleanup:
virDomainObjEndAPI(&vm);
return ret;
}
static void
qemuAutostartDomains(virQEMUDriver *driver)
{
virDomainObjListForEach(driver->domains, false, qemuAutostartDomain, driver);
qemuProcessEndJob(vm);
}
@ -557,10 +534,10 @@ qemuStateInitialize(bool privileged,
virQEMUDriverConfig *cfg;
uid_t run_uid = -1;
gid_t run_gid = -1;
bool autostart = true;
size_t i;
const char *defsecmodel = NULL;
g_autoptr(virIdentity) identity = virIdentityGetCurrent();
virDomainDriverAutoStartConfig autostartCfg;
qemu_driver = g_new0(virQEMUDriver, 1);
@ -906,11 +883,12 @@ qemuStateInitialize(bool privileged,
qemuProcessReconnectAll(qemu_driver);
if (virDriverShouldAutostart(cfg->stateDir, &autostart) < 0)
goto error;
if (autostart)
qemuAutostartDomains(qemu_driver);
autostartCfg = (virDomainDriverAutoStartConfig) {
.stateDir = cfg->stateDir,
.callback = qemuAutostartDomain,
.opaque = qemu_driver,
};
virDomainDriverAutoStart(qemu_driver->domains, &autostartCfg);
return VIR_DRV_STATE_INIT_COMPLETE;