[PATCH 10/16] Add hooks for bhyve backend

Ryan Moeller posted 16 patches 4 years, 9 months ago
There is a newer version of this series
[PATCH 10/16] Add hooks for bhyve backend
Posted by Ryan Moeller 4 years, 9 months ago
Signed-off-by: Ryan Moeller <ryan@iXsystems.com>
---
 src/bhyve/bhyve_process.c | 33 +++++++++++++++++++++++++++++++++
 src/util/virhook.c        | 15 +++++++++++++++
 src/util/virhook.h        | 11 +++++++++++
 3 files changed, 59 insertions(+)

diff --git a/src/bhyve/bhyve_process.c b/src/bhyve/bhyve_process.c
index a11f75a894..45162503d4 100644
--- a/src/bhyve/bhyve_process.c
+++ b/src/bhyve/bhyve_process.c
@@ -37,6 +37,7 @@
 #include "bhyve_process.h"
 #include "datatypes.h"
 #include "virerror.h"
+#include "virhook.h"
 #include "virlog.h"
 #include "virfile.h"
 #include "viralloc.h"
@@ -91,6 +92,24 @@ virBhyveFormatDevMapFile(const char *vm_name, char **fn_out)
     *fn_out = g_strdup_printf("%s/grub_bhyve-%s-device.map", BHYVE_STATE_DIR, vm_name);
 }
 
+static int
+bhyveProcessStartHook(virDomainObjPtr vm, virHookBhyveOpType op)
+{
+    if (!virHookPresent(VIR_HOOK_DRIVER_BHYVE))
+        return 0;
+
+    return virHookCall(VIR_HOOK_DRIVER_BHYVE, vm->def->name, op,
+                       VIR_HOOK_SUBOP_BEGIN, NULL, NULL, NULL);
+}
+
+static void
+bhyveProcessStopHook(virDomainObjPtr vm, virHookBhyveOpType op)
+{
+    if (virHookPresent(VIR_HOOK_DRIVER_BHYVE))
+        virHookCall(VIR_HOOK_DRIVER_BHYVE, vm->def->name, op,
+                    VIR_HOOK_SUBOP_END, NULL, NULL, NULL);
+}
+
 int
 virBhyveProcessStart(virConnectPtr conn,
                      virDomainObjPtr vm,
@@ -135,6 +154,10 @@ virBhyveProcessStart(virConnectPtr conn,
     if (bhyveDomainAssignAddresses(vm->def, NULL) < 0)
         goto cleanup;
 
+    /* Run an early hook to setup missing devices. */
+    if (bhyveProcessStartHook(vm, VIR_HOOK_BHYVE_OP_PREPARE) < 0)
+        goto cleanup;
+
     /* Call bhyve to start the VM */
     if (!(cmd = virBhyveProcessBuildBhyveCmd(driver, vm->def, false)))
         goto cleanup;
@@ -176,6 +199,9 @@ virBhyveProcessStart(virConnectPtr conn,
             goto cleanup;
     }
 
+    if (bhyveProcessStartHook(vm, VIR_HOOK_BHYVE_OP_START) < 0)
+        goto cleanup;
+
     /* Now we can start the domain */
     VIR_DEBUG("Starting domain '%s'", vm->def->name);
     if (virCommandRun(cmd, NULL) < 0)
@@ -200,6 +226,9 @@ virBhyveProcessStart(virConnectPtr conn,
                          BHYVE_STATE_DIR) < 0)
         goto cleanup;
 
+    if (bhyveProcessStartHook(vm, VIR_HOOK_BHYVE_OP_STARTED) < 0)
+        goto cleanup;
+
     ret = 0;
 
  cleanup:
@@ -263,6 +292,8 @@ virBhyveProcessStop(bhyveConnPtr driver,
     if ((priv != NULL) && (priv->mon != NULL))
          bhyveMonitorClose(priv->mon);
 
+    bhyveProcessStopHook(vm, VIR_HOOK_BHYVE_OP_STOPPED);
+
     /* Cleanup network interfaces */
     bhyveNetCleanup(vm);
 
@@ -284,6 +315,8 @@ virBhyveProcessStop(bhyveConnPtr driver,
     vm->pid = -1;
     vm->def->id = -1;
 
+    bhyveProcessStopHook(vm, VIR_HOOK_BHYVE_OP_RELEASE);
+
  cleanup:
     virCommandFree(cmd);
 
diff --git a/src/util/virhook.c b/src/util/virhook.c
index 5bdacdd79f..e499841f66 100644
--- a/src/util/virhook.c
+++ b/src/util/virhook.c
@@ -47,6 +47,7 @@ VIR_ENUM_DECL(virHookQemuOp);
 VIR_ENUM_DECL(virHookLxcOp);
 VIR_ENUM_DECL(virHookNetworkOp);
 VIR_ENUM_DECL(virHookLibxlOp);
+VIR_ENUM_DECL(virHookBhyveOp);
 
 VIR_ENUM_IMPL(virHookDriver,
               VIR_HOOK_DRIVER_LAST,
@@ -55,6 +56,7 @@ VIR_ENUM_IMPL(virHookDriver,
               "lxc",
               "network",
               "libxl",
+              "bhyve",
 );
 
 VIR_ENUM_IMPL(virHookDaemonOp,
@@ -115,6 +117,15 @@ VIR_ENUM_IMPL(virHookLibxlOp,
               "reconnect",
 );
 
+VIR_ENUM_IMPL(virHookBhyveOp,
+              VIR_HOOK_BHYVE_OP_LAST,
+              "start",
+              "stopped",
+              "prepare",
+              "release",
+              "started",
+);
+
 static int virHooksFound = -1;
 
 /**
@@ -283,6 +294,10 @@ virHookCall(int driver,
             break;
         case VIR_HOOK_DRIVER_NETWORK:
             opstr = virHookNetworkOpTypeToString(op);
+            break;
+        case VIR_HOOK_DRIVER_BHYVE:
+            opstr = virHookBhyveOpTypeToString(op);
+            break;
     }
     if (opstr == NULL) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
diff --git a/src/util/virhook.h b/src/util/virhook.h
index f91cb87b10..d8237c837e 100644
--- a/src/util/virhook.h
+++ b/src/util/virhook.h
@@ -29,6 +29,7 @@ typedef enum {
     VIR_HOOK_DRIVER_LXC,               /* LXC domains related events */
     VIR_HOOK_DRIVER_NETWORK,           /* network related events */
     VIR_HOOK_DRIVER_LIBXL,             /* Xen libxl domains related events */
+    VIR_HOOK_DRIVER_BHYVE,             /* Bhyve domains related events */
 
     VIR_HOOK_DRIVER_LAST,
 } virHookDriverType;
@@ -97,6 +98,16 @@ typedef enum {
     VIR_HOOK_LIBXL_OP_LAST,
 } virHookLibxlOpType;
 
+typedef enum {
+    VIR_HOOK_BHYVE_OP_START,            /* domain is about to start */
+    VIR_HOOK_BHYVE_OP_STOPPED,          /* domain has stopped */
+    VIR_HOOK_BHYVE_OP_PREPARE,          /* domain startup initiated */
+    VIR_HOOK_BHYVE_OP_RELEASE,          /* domain destruction is over */
+    VIR_HOOK_BHYVE_OP_STARTED,          /* domain has started */
+
+    VIR_HOOK_BHYVE_OP_LAST,
+} virHookBhyveOpType;
+
 int virHookInitialize(void);
 
 int virHookPresent(int driver);
-- 
2.24.1