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

hypervisor: introduce helper for autostart

There's a common pattern for autostart of iterating over VMs, acquiring
a lock and ref count, then checking the autostart & is-active flags.
Wrap this all up into a helper method.

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 18:53:28 +00:00
parent 8a6088221a
commit c0cdbecdaa
3 changed files with 59 additions and 0 deletions

View File

@ -29,9 +29,12 @@
#include "viraccessapicheck.h"
#include "datatypes.h"
#include "driver.h"
#include "virlog.h"
#define VIR_FROM_THIS VIR_FROM_DOMAIN
VIR_LOG_INIT("hypervisor.domain_driver");
char *
virDomainDriverGenerateRootHash(const char *drivername,
const char *root)
@ -652,3 +655,41 @@ virDomainDriverGetIOThreadsConfig(virDomainDef *targetDef,
return ret;
}
static int
virDomainDriverAutoStartOne(virDomainObj *vm,
void *opaque)
{
virDomainDriverAutoStartConfig *cfg = opaque;
virObjectLock(vm);
virObjectRef(vm);
VIR_DEBUG("Autostart %s: autostart=%d",
vm->def->name, vm->autostart);
if (vm->autostart && !virDomainObjIsActive(vm)) {
virResetLastError();
cfg->callback(vm, cfg->opaque);
}
virDomainObjEndAPI(&vm);
virResetLastError();
return 0;
}
void
virDomainDriverAutoStart(virDomainObjList *domains,
virDomainDriverAutoStartConfig *cfg)
{
bool autostart;
VIR_DEBUG("Run autostart stateDir=%s", cfg->stateDir);
if (virDriverShouldAutostart(cfg->stateDir, &autostart) < 0 ||
!autostart) {
VIR_DEBUG("Autostart already processed");
return;
}
virDomainObjListForEach(domains, false, virDomainDriverAutoStartOne, cfg);
}

View File

@ -23,6 +23,7 @@
#include "node_device_conf.h"
#include "virhostdev.h"
#include "virpci.h"
#include "virdomainobjlist.h"
char *
virDomainDriverGenerateRootHash(const char *drivername,
@ -71,3 +72,19 @@ int virDomainDriverDelIOThreadCheck(virDomainDef *def,
int virDomainDriverGetIOThreadsConfig(virDomainDef *targetDef,
virDomainIOThreadInfoPtr **info,
unsigned int bitmap_size);
/*
* Will be called with 'vm' locked and ref count held,
* which will be released when this returns.
*/
typedef void (*virDomainDriverAutoStartCallback)(virDomainObj *vm,
void *opaque);
typedef struct _virDomainDriverAutoStartConfig {
const char *stateDir;
virDomainDriverAutoStartCallback callback;
void *opaque;
} virDomainDriverAutoStartConfig;
void virDomainDriverAutoStart(virDomainObjList *domains,
virDomainDriverAutoStartConfig *cfg);

View File

@ -1647,6 +1647,7 @@ virDomainCgroupSetupVcpuBW;
# hypervisor/domain_driver.h
virDomainDriverAddIOThreadCheck;
virDomainDriverAutoStart;
virDomainDriverDelIOThreadCheck;
virDomainDriverGenerateMachineName;
virDomainDriverGenerateRootHash;