From: Roman Bogorodskiy <bogorodskiy(a)gmail.com>
The only supported flag for now is 'autodestroy'. In order to
support 'autodestroy', add support for close callbacks.
Signed-off-by: Michal Privoznik <mprivozn(a)redhat.com>
---
src/bhyve/bhyve_driver.c | 28 ++++++++++++++++++++++++---
src/bhyve/bhyve_process.c | 49 +++++++++++++++++++++++++++++++++++------------
src/bhyve/bhyve_process.h | 7 ++++++-
src/bhyve/bhyve_utils.h | 3 +++
4 files changed, 71 insertions(+), 16 deletions(-)
diff --git a/src/bhyve/bhyve_driver.c b/src/bhyve/bhyve_driver.c
index 1cf3be2..a524d35 100644
--- a/src/bhyve/bhyve_driver.c
+++ b/src/bhyve/bhyve_driver.c
@@ -178,6 +178,9 @@ bhyveConnectOpen(virConnectPtr conn,
static int
bhyveConnectClose(virConnectPtr conn)
{
+ bhyveConnPtr privconn = conn->privateData;
+
+ virCloseCallbacksRun(privconn->closeCallbacks, conn, privconn->domains,
privconn);
conn->privateData = NULL;
return 0;
@@ -530,16 +533,23 @@ cleanup:
}
static int
-bhyveDomainCreate(virDomainPtr dom)
+bhyveDomainCreateWithFlags(virDomainPtr dom,
+ unsigned int flags)
{
bhyveConnPtr privconn = dom->conn->privateData;
virDomainObjPtr vm;
+ unsigned int start_flags = 0;
int ret = -1;
+ virCheckFlags(VIR_DOMAIN_START_AUTODESTROY, -1);
+
+ if (flags & VIR_DOMAIN_START_AUTODESTROY)
+ start_flags |= VIR_BHYVE_PROCESS_START_AUTODESTROY;
+
if (!(vm = bhyveDomObjFromDomain(dom)))
goto cleanup;
- if (virDomainCreateEnsureACL(dom->conn, vm->def) < 0)
+ if (virDomainCreateWithFlagsEnsureACL(dom->conn, vm->def) < 0)
goto cleanup;
if (virDomainObjIsActive(vm)) {
@@ -549,7 +559,8 @@ bhyveDomainCreate(virDomainPtr dom)
}
ret = virBhyveProcessStart(dom->conn, privconn, vm,
- VIR_DOMAIN_RUNNING_BOOTED);
+ VIR_DOMAIN_RUNNING_BOOTED,
+ start_flags);
cleanup:
virObjectUnlock(vm);
@@ -557,6 +568,12 @@ cleanup:
}
static int
+bhyveDomainCreate(virDomainPtr dom)
+{
+ return bhyveDomainCreateWithFlags(dom, 0);
+}
+
+static int
bhyveDomainDestroy(virDomainPtr dom)
{
bhyveConnPtr privconn = dom->conn->privateData;
@@ -629,6 +646,7 @@ bhyveStateCleanup(void)
virObjectUnref(bhyve_driver->domains);
virObjectUnref(bhyve_driver->caps);
virObjectUnref(bhyve_driver->xmlopt);
+ virObjectUnref(bhyve_driver->closeCallbacks);
virMutexDestroy(&bhyve_driver->lock);
VIR_FREE(bhyve_driver);
@@ -655,6 +673,9 @@ bhyveStateInitialize(bool priveleged ATTRIBUTE_UNUSED,
return -1;
}
+ if (!(bhyve_driver->closeCallbacks = virCloseCallbacksNew()))
+ goto cleanup;
+
if (!(bhyve_driver->caps = bhyveBuildCapabilities()))
goto cleanup;
@@ -773,6 +794,7 @@ static virDriver bhyveDriver = {
.connectListDefinedDomains = bhyveConnectListDefinedDomains, /* 1.2.2 */
.connectNumOfDefinedDomains = bhyveConnectNumOfDefinedDomains, /* 1.2.2 */
.domainCreate = bhyveDomainCreate, /* 1.2.2 */
+ .domainCreateWithFlags = bhyveDomainCreateWithFlags, /* 1.2.3 */
.domainDestroy = bhyveDomainDestroy, /* 1.2.2 */
.domainLookupByUUID = bhyveDomainLookupByUUID, /* 1.2.2 */
.domainLookupByName = bhyveDomainLookupByName, /* 1.2.2 */
diff --git a/src/bhyve/bhyve_process.c b/src/bhyve/bhyve_process.c
index f5433f3..423185a 100644
--- a/src/bhyve/bhyve_process.c
+++ b/src/bhyve/bhyve_process.c
@@ -46,11 +46,29 @@
VIR_LOG_INIT("bhyve.bhyve_process");
+static virDomainObjPtr
+bhyveProcessAutoDestroy(virDomainObjPtr vm,
+ virConnectPtr conn ATTRIBUTE_UNUSED,
+ void *opaque)
+{
+ bhyveConnPtr driver = opaque;
+
+ virBhyveProcessStop(driver, vm, VIR_DOMAIN_SHUTOFF_DESTROYED);
+
+ if (!vm->persistent) {
+ virDomainObjListRemove(driver->domains, vm);
+ vm = NULL;
+ }
+
+ return vm;
+}
+
int
virBhyveProcessStart(virConnectPtr conn,
bhyveConnPtr driver,
virDomainObjPtr vm,
- virDomainRunningReason reason)
+ virDomainRunningReason reason,
+ unsigned int flags)
{
char *logfile = NULL;
int logfd = -1;
@@ -121,21 +139,25 @@ virBhyveProcessStart(virConnectPtr conn,
/* Now we can start the domain */
VIR_DEBUG("Starting domain '%s'", vm->def->name);
- ret = virCommandRun(cmd, NULL);
+ if (virCommandRun(cmd, NULL) < 0)
+ goto cleanup;
- if (ret == 0) {
- if (virPidFileReadPath(privconn->pidfile, &vm->pid) < 0) {
- virReportError(VIR_ERR_INTERNAL_ERROR,
- _("Domain %s didn't show up"),
vm->def->name);
- goto cleanup;
- }
-
- vm->def->id = vm->pid;
- virDomainObjSetState(vm, VIR_DOMAIN_RUNNING, reason);
- } else {
+ if (virPidFileReadPath(privconn->pidfile, &vm->pid) < 0) {
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Domain %s didn't show up"),
vm->def->name);
goto cleanup;
}
+ if (flags & VIR_BHYVE_PROCESS_START_AUTODESTROY &&
+ virCloseCallbacksSet(driver->closeCallbacks, vm,
+ conn, bhyveProcessAutoDestroy) < 0)
+ goto cleanup;
+
+ vm->def->id = vm->pid;
+ virDomainObjSetState(vm, VIR_DOMAIN_RUNNING, reason);
+
+ ret = 0;
+
cleanup:
if (ret < 0) {
virCommandPtr destroy_cmd;
@@ -203,6 +225,9 @@ virBhyveProcessStop(bhyveConnPtr driver,
ret = 0;
+ virCloseCallbacksUnset(driver->closeCallbacks, vm,
+ bhyveProcessAutoDestroy);
+
virDomainObjSetState(vm, VIR_DOMAIN_SHUTOFF, reason);
vm->pid = -1;
vm->def->id = -1;
diff --git a/src/bhyve/bhyve_process.h b/src/bhyve/bhyve_process.h
index 66548ae..f91504e 100644
--- a/src/bhyve/bhyve_process.h
+++ b/src/bhyve/bhyve_process.h
@@ -27,10 +27,15 @@
int virBhyveProcessStart(virConnectPtr conn,
bhyveConnPtr driver,
virDomainObjPtr vm,
- virDomainRunningReason reason);
+ virDomainRunningReason reason,
+ unsigned int flags);
int virBhyveProcessStop(bhyveConnPtr driver,
virDomainObjPtr vm,
virDomainShutoffReason reason);
+typedef enum {
+ VIR_BHYVE_PROCESS_START_AUTODESTROY = 1 << 0,
+} bhyveProcessStartFlags;
+
#endif /* __BHYVE_PROCESS_H__ */
diff --git a/src/bhyve/bhyve_utils.h b/src/bhyve/bhyve_utils.h
index 0810caa..6c76770 100644
--- a/src/bhyve/bhyve_utils.h
+++ b/src/bhyve/bhyve_utils.h
@@ -26,6 +26,7 @@
# include "domain_conf.h"
# include "configmake.h"
# include "virthread.h"
+# include "virclosecallbacks.h"
# define BHYVE_AUTOSTART_DIR SYSCONFDIR "/libvirt/bhyve/autostart"
# define BHYVE_CONFIG_DIR SYSCONFDIR "/libvirt/bhyve"
@@ -38,6 +39,8 @@ struct _bhyveConn {
virCapsPtr caps;
virDomainXMLOptionPtr xmlopt;
char *pidfile;
+
+ virCloseCallbacksPtr closeCallbacks;
};
typedef struct _bhyveConn bhyveConn;
--
1.9.0