From cf380e22898f70f5782bcea8b0d22027ff7d86af Mon Sep 17 00:00:00 2001
From: AlexChen <alex.chen@huawei.com>
Date: Wed, 20 Oct 2021 11:07:34 +0800
Subject: [PATCH] hotpatch: implement hotpatch virsh api

Signed-off-by: Hao Wang <wanghao232@huawei.com>
Signed-off-by: Bihong Yu <yubihong@huawei.com>
Signed-off-by: AlexChen <alex.chen@huawei.com>
---
 tools/virsh-domain.c | 78 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 78 insertions(+)

diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index f643bd403e..813be4a0db 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -14326,6 +14326,78 @@ cmdGuestInfo(vshControl *ctl, const vshCmd *cmd)
     return ret;
 }
 
+/*
+ * "hotpatch" command
+ */
+static const vshCmdInfo info_hotpatch[] = {
+    {.name = "help",
+     .data = N_("Manage hotpatch of a live domain")
+    },
+    {.name = "desc",
+     .data = N_("Manage hotpatch of a live domain")
+    },
+    {.name = NULL}
+};
+
+static const vshCmdOptDef opts_hotpatch[] = {
+    VIRSH_COMMON_OPT_DOMAIN_FULL(0),
+    {.name = "action",
+     .type = VSH_OT_DATA,
+     .flags = VSH_OFLAG_REQ,
+     .help = N_("hotpatch action, choose from <apply>, <unapply> and <query>")
+    },
+    {.name = "patch",
+     .type = VSH_OT_STRING,
+     .help = N_("the absolute path of the hotpatch file, mandatory when action=apply")
+    },
+    {.name = "id",
+     .type = VSH_OT_STRING,
+     .help = N_("the unique id of the target patch, mandatory when action=unapply")
+    },
+    {.name = NULL}
+};
+
+VIR_ENUM_DECL(virDomainHotpatchAction);
+VIR_ENUM_IMPL(virDomainHotpatchAction,
+              VIR_DOMAIN_HOTPATCH_LAST,
+              "none",
+              "apply",
+              "unapply",
+              "query");
+
+static bool
+cmdHotpatch(vshControl *ctl,
+            const vshCmd *cmd)
+{
+    g_autoptr(virshDomain) dom = NULL;
+    const char *patch = NULL;
+    const char *id = NULL;
+    const char *actionstr = NULL;
+    int action = -1;
+    g_autofree char *ret = NULL;
+
+    if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
+        return false;
+
+    if (vshCommandOptStringReq(ctl, cmd, "action", &actionstr) < 0)
+        return false;
+
+    if (actionstr)
+        action = virDomainHotpatchActionTypeFromString(actionstr);
+
+    if (vshCommandOptStringReq(ctl, cmd, "patch", &patch) < 0)
+        return false;
+
+    if (vshCommandOptStringReq(ctl, cmd, "id", &id) < 0)
+        return false;
+
+    if (!(ret = virDomainHotpatchManage(dom, action, patch, id, 0)))
+        return false;
+
+    vshPrint(ctl, _("%s"), ret);
+    return true;
+}
+
 const vshCmdDef domManagementCmds[] = {
     {.name = "attach-device",
      .handler = cmdAttachDevice,
@@ -14953,5 +15025,11 @@ const vshCmdDef domManagementCmds[] = {
      .info = info_guestinfo,
      .flags = 0
     },
+    {.name = "hotpatch",
+     .handler = cmdHotpatch,
+     .opts = opts_hotpatch,
+     .info = info_hotpatch,
+     .flags = 0
+    },
     {.name = NULL}
 };
-- 
2.27.0

