[libvirt] [PATCH] [6/6] Add script hook support to the LXC driver

Daniel Veillard veillard at redhat.com
Fri Mar 26 15:46:20 UTC 2010


Add script hook support to the LXC driver

Right now this implements only 2 basic hooks:
- before the lxc control process is being launched
- after the lxc control process is terminated
the XML description of the domain is passed to the hook script stdin
/etc/libvirt/hook/lxc

* src/lxc/lxc_driver.c: implement synchronous script hooks for LXC
  at domain startup and end

diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c
index ba13065..1697363 100644
--- a/src/lxc/lxc_driver.c
+++ b/src/lxc/lxc_driver.c
@@ -49,6 +49,7 @@
 #include "nodeinfo.h"
 #include "uuid.h"
 #include "stats_linux.h"
+#include "hooks.h"
 
 
 #define VIR_FROM_THIS VIR_FROM_LXC
@@ -722,6 +723,16 @@ static int lxcVmCleanup(lxc_driver_t *driver,
         DEBUG("container exited with rc: %d", rc);
     }
 
+    /* now that we know it's stopped call the hook if present */
+    if (virHookPresent(VIR_HOOK_DRIVER_LXC)) {
+        char *xml = virDomainDefFormat(vm->def, 0);
+
+        /* we can't stop the operation even if the script raised an error */
+        virHookCall(VIR_HOOK_DRIVER_LXC, vm->def->name,
+                    VIR_HOOK_LXC_OP_STOPPED, VIR_HOOK_SUBOP_END, NULL, xml);
+        VIR_FREE(xml);
+    }
+
     virEventRemoveHandle(priv->monitorWatch);
     close(priv->monitor);
 
@@ -1120,6 +1131,22 @@ static int lxcControllerStart(lxc_driver_t *driver,
 
     FD_SET(appPty, &keepfd);
 
+    /* now that we know it is about to start call the hook if present */
+    if (virHookPresent(VIR_HOOK_DRIVER_LXC)) {
+        char *xml = virDomainDefFormat(vm->def, 0);
+        int hookret;
+
+        hookret = virHookCall(VIR_HOOK_DRIVER_LXC, vm->def->name,
+                    VIR_HOOK_LXC_OP_START, VIR_HOOK_SUBOP_BEGIN, NULL, xml);
+        VIR_FREE(xml);
+
+        /*
+         * If the script raised an error abort the launch
+         */
+        if (hookret < 0)
+            goto cleanup;
+    }
+
     if (virExec(largv, lenv, &keepfd, &child,
                 -1, &logfd, &logfd,
                 VIR_EXEC_NONE) < 0)

-- 
Daniel Veillard      | libxml Gnome XML XSLT toolkit  http://xmlsoft.org/
daniel at veillard.com  | Rpmfind RPM search engine http://rpmfind.net/
http://veillard.com/ | virtualization library  http://libvirt.org/




More information about the libvir-list mailing list