[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [RFC] Shutdown event script for xl toolstack

Hi guys,

Previously I had a series of out of tree patches to xend that added a
shutdown event callback on domain death that would call a script with
the reason why the domain shutdown.
Basically analagous to this:
/etc/xen/scripts/shutdown-event $event $domname $domid

I would like to add similar functionality to xl and attempt to
contribute it back because the feature is incredibly useful for event
based scripting.
Many Xen system admins before me have resorted to hacking it into xend
because it's so useful for monitoring and automation. :P
Similar things can be done with xenstore-watch or the xenstore C API
but they lose information because the only thing communicated to
xenstore is intent.
If a domain crashes for instance it is basically just destroyed and
GC'd, nothing is ever written to xenstore - the path is just rm'd.
The point of this feature is to take the guesswork out of event
handling of xen domains.

My general idea is to add a config option for a path to an event
handler script:
on_shutdown_event = "/etc/xen/scripts/shutdown-event"

Create the event during handle_domain_death in xl_cmdimpl.c and fire
the script once shutdown reason and action has been parsed.

I implemented a hacky version to illustrate my point but I would like
some advice on how to do this properly and what tools/framework within
libxl I could leverage to make it cleaner.

A quick overview of the following would help immensely:
Where to add in a new config option and what steps it goes through to
get to libxl_domain_config.
How to exec an external script safely, can I use usual fork/exec or
should I be using libxl__exec or libxl_spawn*?
Best place/way to get the event data, atm handle_domain_death looks
like an easy target but there might be more elegant ways..

Patch to show what I want to do is below, but it's nasty and only
serves to illustrate my intented outcome.

I will probably write up some wiki articles to help others that want
to go down this path too. :)

Thanks in advance and kind regards,


diff --git a/tools/hotplug/Linux/Makefile b/tools/hotplug/Linux/Makefile
index 5401592..6b3a825 100644
--- a/tools/hotplug/Linux/Makefile
+++ b/tools/hotplug/Linux/Makefile
@@ -22,6 +22,7 @@ XEN_SCRIPTS += vtpm vtpm-delete
 XEN_SCRIPTS += xen-hotplug-cleanup
 XEN_SCRIPTS += external-device-migrate
 XEN_SCRIPTS += vscsi
+XEN_SCRIPTS += shutdown-event
 XEN_SCRIPT_DATA = xen-script-common.sh locking.sh logging.sh
 XEN_SCRIPT_DATA += xen-hotplug-common.sh xen-network-common.sh vif-common.sh
 XEN_SCRIPT_DATA += block-common.sh vtpm-common.sh vtpm-hotplug-common.sh
diff --git a/tools/hotplug/Linux/shutdown-event
new file mode 100644
index 0000000..3f49d16
--- /dev/null
+++ b/tools/hotplug/Linux/shutdown-event
@@ -0,0 +1,19 @@
+case $event in
+    poweroff)
+        # Handle action on poweroff shutdown here
+        ;;
+    reboot)
+        # Handle action on reboot shutdown here
+        ;;
+    crash)
+        # Handle action on crash shutdown here
+        ;;
+    watchdog)
+        # Handle action on watchdog shutdown here
+        ;;
+    unknown)
+        # Handle unknown shutdown reason here
+        ;;
diff --git a/tools/libxl/xl_cmdimpl.c b/tools/libxl/xl_cmdimpl.c
index 2df96b8..94cc4c5 100644
--- a/tools/libxl/xl_cmdimpl.c
+++ b/tools/libxl/xl_cmdimpl.c
@@ -1160,31 +1160,40 @@ skip_vfb:

+#define SHUTDOWN_EVENT_SCRIPT "/etc/xen/scripts/shutdown-event %s %s %d"
 /* Returns 1 if domain should be restarted, 2 if domain should be
renamed then restarted  */
 static int handle_domain_death(libxl_ctx *ctx, uint32_t domid,
libxl_event *event,
                               libxl_domain_config *d_config,
libxl_dominfo *info)
-    int restart = 0;
+    int restart = 0, ret;
    enum libxl_action_on_shutdown action;
+    char event_name[10];
+    char event_cmd[100];

    switch (info->shutdown_reason) {
    case SHUTDOWN_poweroff:
        action = d_config->on_poweroff;
+        strcpy(event_name,"poweroff");
    case SHUTDOWN_reboot:
        action = d_config->on_reboot;
+        strcpy(event_name,"reboot");
    case SHUTDOWN_suspend:
        return 0;
    case SHUTDOWN_crash:
        action = d_config->on_crash;
+        strcpy(event_name,"crash");
    case SHUTDOWN_watchdog:
        action = d_config->on_watchdog;
+        strcpy(event_name,"watchdog");
        LOG("Unknown shutdown reason code %d. Destroying domain.",
        action = LIBXL_ACTION_DESTROY;
+        strcpy(event_name,"unknown");

    LOG("Action for shutdown reason code %d is %s",
info->shutdown_reason, action_on_shutdown_names[action]);
@@ -1230,6 +1239,8 @@ static int handle_domain_death(libxl_ctx *ctx,
uint32_t domid, libxl_event *even

+    snprintf(event_cmd, 100, SHUTDOWN_EVENT_SCRIPT, event_name,
d_config->c_info.name, domid);
+    ret = system(event_cmd);
    return restart;

Founder | Director | VP Research
Orion Virtualisation Solutions | www.orionvm.com.au | Phone: 1300 56
99 52 | Mobile: 0428 754 846

Xen-devel mailing list



Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.