bhyve: add domainCreateWithFlags support

The only supported flag for now is 'autodestroy'. In order to
support 'autodestroy', add support for close callbacks.
This commit is contained in:
Roman Bogorodskiy 2014-03-18 13:31:48 +04:00
parent 24aa0d1806
commit d4236503ed
4 changed files with 71 additions and 16 deletions

View File

@ -178,6 +178,9 @@ bhyveConnectOpen(virConnectPtr conn,
static int static int
bhyveConnectClose(virConnectPtr conn) bhyveConnectClose(virConnectPtr conn)
{ {
bhyveConnPtr privconn = conn->privateData;
virCloseCallbacksRun(privconn->closeCallbacks, conn, privconn->domains, privconn);
conn->privateData = NULL; conn->privateData = NULL;
return 0; return 0;
@ -530,16 +533,23 @@ cleanup:
} }
static int static int
bhyveDomainCreate(virDomainPtr dom) bhyveDomainCreateWithFlags(virDomainPtr dom,
unsigned int flags)
{ {
bhyveConnPtr privconn = dom->conn->privateData; bhyveConnPtr privconn = dom->conn->privateData;
virDomainObjPtr vm; virDomainObjPtr vm;
unsigned int start_flags = 0;
int ret = -1; 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))) if (!(vm = bhyveDomObjFromDomain(dom)))
goto cleanup; goto cleanup;
if (virDomainCreateEnsureACL(dom->conn, vm->def) < 0) if (virDomainCreateWithFlagsEnsureACL(dom->conn, vm->def) < 0)
goto cleanup; goto cleanup;
if (virDomainObjIsActive(vm)) { if (virDomainObjIsActive(vm)) {
@ -549,13 +559,20 @@ bhyveDomainCreate(virDomainPtr dom)
} }
ret = virBhyveProcessStart(dom->conn, privconn, vm, ret = virBhyveProcessStart(dom->conn, privconn, vm,
VIR_DOMAIN_RUNNING_BOOTED); VIR_DOMAIN_RUNNING_BOOTED,
start_flags);
cleanup: cleanup:
virObjectUnlock(vm); virObjectUnlock(vm);
return ret; return ret;
} }
static int
bhyveDomainCreate(virDomainPtr dom)
{
return bhyveDomainCreateWithFlags(dom, 0);
}
static int static int
bhyveDomainDestroy(virDomainPtr dom) bhyveDomainDestroy(virDomainPtr dom)
{ {
@ -629,6 +646,7 @@ bhyveStateCleanup(void)
virObjectUnref(bhyve_driver->domains); virObjectUnref(bhyve_driver->domains);
virObjectUnref(bhyve_driver->caps); virObjectUnref(bhyve_driver->caps);
virObjectUnref(bhyve_driver->xmlopt); virObjectUnref(bhyve_driver->xmlopt);
virObjectUnref(bhyve_driver->closeCallbacks);
virMutexDestroy(&bhyve_driver->lock); virMutexDestroy(&bhyve_driver->lock);
VIR_FREE(bhyve_driver); VIR_FREE(bhyve_driver);
@ -655,6 +673,9 @@ bhyveStateInitialize(bool priveleged ATTRIBUTE_UNUSED,
return -1; return -1;
} }
if (!(bhyve_driver->closeCallbacks = virCloseCallbacksNew()))
goto cleanup;
if (!(bhyve_driver->caps = bhyveBuildCapabilities())) if (!(bhyve_driver->caps = bhyveBuildCapabilities()))
goto cleanup; goto cleanup;
@ -773,6 +794,7 @@ static virDriver bhyveDriver = {
.connectListDefinedDomains = bhyveConnectListDefinedDomains, /* 1.2.2 */ .connectListDefinedDomains = bhyveConnectListDefinedDomains, /* 1.2.2 */
.connectNumOfDefinedDomains = bhyveConnectNumOfDefinedDomains, /* 1.2.2 */ .connectNumOfDefinedDomains = bhyveConnectNumOfDefinedDomains, /* 1.2.2 */
.domainCreate = bhyveDomainCreate, /* 1.2.2 */ .domainCreate = bhyveDomainCreate, /* 1.2.2 */
.domainCreateWithFlags = bhyveDomainCreateWithFlags, /* 1.2.3 */
.domainDestroy = bhyveDomainDestroy, /* 1.2.2 */ .domainDestroy = bhyveDomainDestroy, /* 1.2.2 */
.domainLookupByUUID = bhyveDomainLookupByUUID, /* 1.2.2 */ .domainLookupByUUID = bhyveDomainLookupByUUID, /* 1.2.2 */
.domainLookupByName = bhyveDomainLookupByName, /* 1.2.2 */ .domainLookupByName = bhyveDomainLookupByName, /* 1.2.2 */

View File

@ -46,11 +46,29 @@
VIR_LOG_INIT("bhyve.bhyve_process"); 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 int
virBhyveProcessStart(virConnectPtr conn, virBhyveProcessStart(virConnectPtr conn,
bhyveConnPtr driver, bhyveConnPtr driver,
virDomainObjPtr vm, virDomainObjPtr vm,
virDomainRunningReason reason) virDomainRunningReason reason,
unsigned int flags)
{ {
char *logfile = NULL; char *logfile = NULL;
int logfd = -1; int logfd = -1;
@ -121,21 +139,25 @@ virBhyveProcessStart(virConnectPtr conn,
/* Now we can start the domain */ /* Now we can start the domain */
VIR_DEBUG("Starting domain '%s'", vm->def->name); 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) {
if (virPidFileReadPath(privconn->pidfile, &vm->pid) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR,
virReportError(VIR_ERR_INTERNAL_ERROR, _("Domain %s didn't show up"), vm->def->name);
_("Domain %s didn't show up"), vm->def->name);
goto cleanup;
}
vm->def->id = vm->pid;
virDomainObjSetState(vm, VIR_DOMAIN_RUNNING, reason);
} else {
goto cleanup; 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: cleanup:
if (ret < 0) { if (ret < 0) {
virCommandPtr destroy_cmd; virCommandPtr destroy_cmd;
@ -203,6 +225,9 @@ virBhyveProcessStop(bhyveConnPtr driver,
ret = 0; ret = 0;
virCloseCallbacksUnset(driver->closeCallbacks, vm,
bhyveProcessAutoDestroy);
virDomainObjSetState(vm, VIR_DOMAIN_SHUTOFF, reason); virDomainObjSetState(vm, VIR_DOMAIN_SHUTOFF, reason);
vm->pid = -1; vm->pid = -1;
vm->def->id = -1; vm->def->id = -1;

View File

@ -27,10 +27,15 @@
int virBhyveProcessStart(virConnectPtr conn, int virBhyveProcessStart(virConnectPtr conn,
bhyveConnPtr driver, bhyveConnPtr driver,
virDomainObjPtr vm, virDomainObjPtr vm,
virDomainRunningReason reason); virDomainRunningReason reason,
unsigned int flags);
int virBhyveProcessStop(bhyveConnPtr driver, int virBhyveProcessStop(bhyveConnPtr driver,
virDomainObjPtr vm, virDomainObjPtr vm,
virDomainShutoffReason reason); virDomainShutoffReason reason);
typedef enum {
VIR_BHYVE_PROCESS_START_AUTODESTROY = 1 << 0,
} bhyveProcessStartFlags;
#endif /* __BHYVE_PROCESS_H__ */ #endif /* __BHYVE_PROCESS_H__ */

View File

@ -26,6 +26,7 @@
# include "domain_conf.h" # include "domain_conf.h"
# include "configmake.h" # include "configmake.h"
# include "virthread.h" # include "virthread.h"
# include "virclosecallbacks.h"
# define BHYVE_AUTOSTART_DIR SYSCONFDIR "/libvirt/bhyve/autostart" # define BHYVE_AUTOSTART_DIR SYSCONFDIR "/libvirt/bhyve/autostart"
# define BHYVE_CONFIG_DIR SYSCONFDIR "/libvirt/bhyve" # define BHYVE_CONFIG_DIR SYSCONFDIR "/libvirt/bhyve"
@ -38,6 +39,8 @@ struct _bhyveConn {
virCapsPtr caps; virCapsPtr caps;
virDomainXMLOptionPtr xmlopt; virDomainXMLOptionPtr xmlopt;
char *pidfile; char *pidfile;
virCloseCallbacksPtr closeCallbacks;
}; };
typedef struct _bhyveConn bhyveConn; typedef struct _bhyveConn bhyveConn;