This eliminates some duplicated code patterns aross drivers.
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
#include "virportallocator.h"
#include "conf/domain_capabilities.h"
#include "virutil.h"
+#include "domain_driver.h"
#include "bhyve_conf.h"
#include "bhyve_device.h"
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());
- }
- }
- return ret;
-}
-static void
-bhyveAutostartDomains(struct _bhyveConn *driver)
-{
- virDomainObjListForEach(driver->domains, false, bhyveAutostartDomain, driver);
+ 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());
+ }
}
/**
virStateInhibitCallback callback G_GNUC_UNUSED,
void *opaque G_GNUC_UNUSED)
{
- bool autostart = true;
+ virDomainDriverAutoStartConfig autostartCfg;
if (root != NULL) {
virReportError(VIR_ERR_INVALID_ARG, "%s",
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;
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;
}
{
libxlDriverConfig *cfg;
g_autofree char *driverConf = NULL;
- bool autostart = true;
+ virDomainDriverAutoStartConfig autostartCfg;
if (root != NULL) {
virReportError(VIR_ERR_INVALID_ARG, "%s",
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,
void *opaque)
{
virLXCDriverConfig *cfg = NULL;
- bool autostart = true;
const char *defsecmodel;
+ virDomainDriverAutoStartConfig autostartCfg;
if (root != NULL) {
virReportError(VIR_ERR_INVALID_ARG, "%s",
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;
}
-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);
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);
}
unsigned int cleanupFlags);
void virLXCProcessAutostartAll(virLXCDriver *driver);
+void virLXCProcessAutostartDomain(virDomainObj *vm,
+ void *opaque);
int virLXCProcessReconnectAll(virLXCDriver *driver,
virDomainObjList *doms);
-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);
}
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);
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;