[libvirt] [PATCH 4/4] New virsh commands for managed core dump

Hong Xiang hxiang at linux.vnet.ibm.com
Thu Nov 3 10:23:37 UTC 2011


New commands:
. manageddump
. manageddump-remove
. manageddump-download

* tools/virsh.c: new commands.

Signed-off-by: Hong Xiang <hxiang at linux.vnet.ibm.com>
---
 tools/virsh.c |  200 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 200 insertions(+), 0 deletions(-)

diff --git a/tools/virsh.c b/tools/virsh.c
index 5544a41..13f9e73 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -14006,6 +14006,201 @@ cleanup:
     return ret;
 }
 
+/*
+ * "manageddump" command
+ */
+static const vshCmdInfo info_manageddump[] = {
+    {"help", N_("managed core dump a domain")},
+    {"desc", N_("Managed core dump a domain.")},
+    {NULL, NULL}
+};
+
+static const vshCmdOptDef opts_manageddump[] = {
+    {"live", VSH_OT_BOOL, 0, N_("perform a live core dump if supported")},
+    {"crash", VSH_OT_BOOL, 0, N_("crash the domain after core dump")},
+    {"bypass-cache", VSH_OT_BOOL, 0, N_("avoid file system cache when saving")},
+    {"reset", VSH_OT_BOOL, 0, N_("reset the domain after core dump")},
+    {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, N_("domain name, id or uuid")},
+    {NULL, 0, 0, NULL}
+};
+
+static bool
+cmdManagedDump(vshControl *ctl, const vshCmd *cmd)
+{
+    virDomainPtr dom;
+    const char *name;
+    bool ret = false;
+    unsigned int flags = 0;
+
+    if (!vshConnectionUsability(ctl, ctl->conn))
+        return false;
+
+    if (!(dom = vshCommandOptDomain(ctl, cmd, &name)))
+        return false;
+
+    if (vshCommandOptBool (cmd, "live"))
+        flags |= VIR_DUMP_LIVE;
+    if (vshCommandOptBool (cmd, "crash"))
+        flags |= VIR_DUMP_CRASH;
+    if (vshCommandOptBool(cmd, "bypass-cache"))
+        flags |= VIR_DUMP_BYPASS_CACHE;
+    if (vshCommandOptBool(cmd, "reset"))
+        flags |= VIR_DUMP_RESET;
+
+    if (virDomainManagedCoreDump(dom, flags) < 0) {
+        vshError(ctl, _("Failed to core dump domain %s"), name);
+        goto cleanup;
+    }
+
+    vshPrint(ctl, _("Domain %s dumped\n"), name);
+    ret = true;
+
+cleanup:
+    virDomainFree(dom);
+    return ret;
+}
+
+/*
+ * "manageddump-remove" command
+ */
+static const vshCmdInfo info_manageddumpremove[] = {
+    {"help", N_("Remove managed core dump of a domain")},
+    {"desc", N_("Remove an existing managed core dump from a domain")},
+    {NULL, NULL}
+};
+
+static const vshCmdOptDef opts_manageddumpremove[] = {
+    {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, N_("domain name, id or uuid")},
+    {NULL, 0, 0, NULL}
+};
+
+static bool
+cmdManagedDumpRemove(vshControl *ctl, const vshCmd *cmd)
+{
+    virDomainPtr dom;
+    const char *name;
+    bool ret = false;
+    int hascoredump;
+
+    if (!vshConnectionUsability(ctl, ctl->conn))
+        return false;
+
+    if (!(dom = vshCommandOptDomain(ctl, cmd, &name)))
+        return false;
+
+    hascoredump = virDomainHasManagedCoreDump(dom, 0);
+    if (hascoredump < 0) {
+        vshError(ctl, "%s", _("Failed to check for domain managed core dump"));
+        goto cleanup;
+    }
+
+    if (hascoredump) {
+        if (virDomainManagedCoreDumpRemove(dom, 0) < 0) {
+            vshError(ctl, _("Failed to remove managed core dump for domain %s"),
+                     name);
+            goto cleanup;
+        }
+        else
+            vshPrint(ctl, _("Removed managed core dump for domain %s"), name);
+    }
+    else
+        vshPrint(ctl, _("Domain %s has no managed core dump; removal skipped"),
+                 name);
+
+    ret = true;
+
+cleanup:
+    virDomainFree(dom);
+    return ret;
+}
+
+/*
+ * "manageddump-download" command
+ */
+static const vshCmdInfo info_manageddumpdownload[] = {
+    {"help", N_("Download managed core dump of a domain")},
+    {"desc", N_("Download an existing managed core dump of a domain")},
+    {NULL, NULL}
+};
+
+static const vshCmdOptDef opts_manageddumpdownload[] = {
+    {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, N_("domain name, id or uuid")},
+    {"file", VSH_OT_DATA, VSH_OFLAG_NONE,
+        N_("where to store the downloaded core dump")},
+    {NULL, 0, 0, NULL}
+};
+
+static bool
+cmdManagedDumpDownload(vshControl *ctl, const vshCmd *cmd)
+{
+    virDomainPtr dom;
+    const char *name = NULL;
+    char *file = NULL;
+    int fd = -1;
+    virStreamPtr st = NULL;
+    int ret = false;
+    int hascoredump;
+
+    if (!vshConnectionUsability(ctl, ctl->conn))
+        return false;
+
+    if (!(dom = vshCommandOptDomain(ctl, cmd, &name)))
+        return false;
+
+    hascoredump = virDomainHasManagedCoreDump(dom, 0);
+    if (hascoredump < 0) {
+        vshError(ctl, "%s", _("Failed to check for domain managed core dump"));
+        goto cleanup;
+    } else if(0 == hascoredump) {
+        vshPrint(ctl, _("Domain %s has no managed core dump"), name);
+        goto cleanup;
+    }
+
+    if (vshCommandOptString(cmd, "file", (const char **) &file) < 0) {
+        vshError(ctl, "%s", _("file must not be empty"));
+        goto cleanup;
+    }
+
+    st = virStreamNew(ctl->conn, 0);
+
+    if(virDomainManagedCoreDumpDownload(dom, st, 0)) {
+        vshError(ctl, _("could not download managed core dump of %s"), name);
+        goto cleanup;
+    }
+
+    if ((fd = open(file, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0) {
+        vshError(ctl, _("cannot create file %s"), file);
+        goto cleanup;
+    }
+
+    if (virStreamRecvAll(st, vshStreamSink, &fd) < 0) {
+        vshError(ctl, _("could not receive data from domain %s"), name);
+        goto cleanup;
+    }
+
+    if (VIR_CLOSE(fd) < 0) {
+        vshError(ctl, _("cannot close file %s"), file);
+        goto cleanup;
+    }
+
+    if (virStreamFinish(st) < 0) {
+        vshError(ctl, _("cannot close stream on domain %s"), name);
+        goto cleanup;
+    }
+
+    vshPrint(ctl, _("Managed core dump saved to %s"), file);
+    ret = true;
+
+cleanup:
+    if (!ret)
+        unlink(file);
+    virDomainFree(dom);
+    if (st)
+        virStreamFree(st);
+    VIR_FORCE_CLOSE(fd);
+    return ret;
+}
+
 static const vshCmdDef domManagementCmds[] = {
     {"attach-device", cmdAttachDevice, opts_attach_device,
      info_attach_device, 0},
@@ -14085,6 +14280,11 @@ static const vshCmdDef domManagementCmds[] = {
     {"vcpupin", cmdVcpuPin, opts_vcpupin, info_vcpupin, 0},
     {"version", cmdVersion, opts_version, info_version, 0},
     {"vncdisplay", cmdVNCDisplay, opts_vncdisplay, info_vncdisplay, 0},
+    {"manageddump", cmdManagedDump, opts_manageddump, info_manageddump, 0},
+    {"manageddump-remove", cmdManagedDumpRemove, opts_manageddumpremove,
+     info_manageddumpremove, 0},
+    {"manageddump-download", cmdManagedDumpDownload, opts_manageddumpdownload,
+     info_manageddumpdownload, 0},
     {NULL, NULL, NULL, NULL, 0}
 };
 
-- 
1.7.1




More information about the libvir-list mailing list