[PATCH 24/26] admin: add 'daemon-shutdown' command

Daniel P. Berrangé posted 26 patches 1 week, 5 days ago
[PATCH 24/26] admin: add 'daemon-shutdown' command
Posted by Daniel P. Berrangé 1 week, 5 days ago
The daemons are wired up to shutdown in responsible to UNIX process
signals, as well as in response to login1 dbus signals, or loss of
desktop session. The latter two options can optionally preserve state
(ie running VMs).

In non-systemd environments, as well as for testing, it would be useful
to have a way to trigger shutdown with state preservation more directly.

Thus a new admin protocol API is introduced

  virAdmConnectDaemonShutdown

which will trigger a daemon shutdown, and preserve running VMs if the
VIR_DAEMON_SHUTDOWN_PRESERVE flag is set.

It has a corresponding 'virt-admin daemon-shutdown [--preserve]' command
binding.

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 include/libvirt/libvirt-admin.h     | 13 +++++++++
 src/admin/admin_protocol.x          | 11 +++++++-
 src/admin/admin_server_dispatch.c   | 13 +++++++++
 src/admin/libvirt-admin.c           | 33 +++++++++++++++++++++++
 src/admin/libvirt_admin_public.syms |  5 ++++
 src/rpc/virnetdaemon.c              |  4 +++
 tools/virt-admin.c                  | 41 +++++++++++++++++++++++++++++
 7 files changed, 119 insertions(+), 1 deletion(-)

diff --git a/include/libvirt/libvirt-admin.h b/include/libvirt/libvirt-admin.h
index ae4703f89b..1926202e97 100644
--- a/include/libvirt/libvirt-admin.h
+++ b/include/libvirt/libvirt-admin.h
@@ -484,6 +484,19 @@ int virAdmConnectSetDaemonTimeout(virAdmConnectPtr conn,
                                   unsigned int timeout,
                                   unsigned int flags);
 
+/**
+ * virAdmConnectDaemonShutdownFlags:
+ *
+ * Since: 11.0.0
+ */
+typedef enum {
+    /* Preserve state before shutting down daemon (Since: 11.0.0) */
+    VIR_DAEMON_SHUTDOWN_PRESERVE = (1 << 0),
+} virAdmConnectDaemonShutdownFlags;
+
+int virAdmConnectDaemonShutdown(virAdmConnectPtr conn,
+                                unsigned int flags);
+
 # ifdef __cplusplus
 }
 # endif
diff --git a/src/admin/admin_protocol.x b/src/admin/admin_protocol.x
index f3130efd2d..cf5707e62e 100644
--- a/src/admin/admin_protocol.x
+++ b/src/admin/admin_protocol.x
@@ -219,6 +219,10 @@ struct admin_connect_set_daemon_timeout_args {
     unsigned int flags;
 };
 
+struct admin_connect_daemon_shutdown_args {
+    unsigned int flags;
+};
+
 /* Define the program number, protocol version and procedure numbers here. */
 const ADMIN_PROGRAM = 0x06900690;
 const ADMIN_PROTOCOL_VERSION = 1;
@@ -334,5 +338,10 @@ enum admin_procedure {
     /**
      * @generate: both
      */
-    ADMIN_PROC_CONNECT_SET_DAEMON_TIMEOUT = 19
+    ADMIN_PROC_CONNECT_SET_DAEMON_TIMEOUT = 19,
+
+    /**
+     * @generate: both
+     */
+    ADMIN_PROC_CONNECT_DAEMON_SHUTDOWN = 20
 };
diff --git a/src/admin/admin_server_dispatch.c b/src/admin/admin_server_dispatch.c
index ae8a8d4fa6..092cc790e6 100644
--- a/src/admin/admin_server_dispatch.c
+++ b/src/admin/admin_server_dispatch.c
@@ -474,6 +474,19 @@ adminConnectSetDaemonTimeout(virNetDaemon *dmn,
     return virNetDaemonAutoShutdown(dmn, timeout);
 }
 
+static int
+adminConnectDaemonShutdown(virNetDaemon *dmn,
+                           unsigned int flags)
+{
+    virCheckFlags(VIR_DAEMON_SHUTDOWN_PRESERVE, -1);
+
+    if (flags & VIR_DAEMON_SHUTDOWN_PRESERVE)
+        virNetDaemonPreserve(dmn);
+
+    virNetDaemonQuit(dmn);
+
+    return 0;
+}
 
 static int
 adminDispatchConnectGetLoggingOutputs(virNetServer *server G_GNUC_UNUSED,
diff --git a/src/admin/libvirt-admin.c b/src/admin/libvirt-admin.c
index 3c756eb376..1e2446135c 100644
--- a/src/admin/libvirt-admin.c
+++ b/src/admin/libvirt-admin.c
@@ -1357,3 +1357,36 @@ virAdmConnectSetDaemonTimeout(virAdmConnectPtr conn,
 
     return ret;
 }
+
+
+/**
+ * virAdmConnectDaemonShutdown:
+ * @conn: pointer to an active admin connection
+ * @flags: optional extra falgs
+ *
+ * Trigger shutdown of the daemon, if @flags includes
+ * VIR_DAEMON_SHUTDOWN_PRESERVE then state will be
+ * preserved before shutting down
+ *
+ * Returns 0 on success, -1 on error.
+ *
+ * Since: 11.0.0
+ */
+int
+virAdmConnectDaemonShutdown(virAdmConnectPtr conn,
+                            unsigned int flags)
+{
+    int ret;
+
+    VIR_DEBUG("conn=%p, flags=0x%x", conn, flags);
+
+    virResetLastError();
+    virCheckAdmConnectReturn(conn, -1);
+
+    if ((ret = remoteAdminConnectDaemonShutdown(conn, flags)) < 0) {
+        virDispatchError(NULL);
+        return -1;
+    }
+
+    return ret;
+}
diff --git a/src/admin/libvirt_admin_public.syms b/src/admin/libvirt_admin_public.syms
index 17930e4fac..d39656bc53 100644
--- a/src/admin/libvirt_admin_public.syms
+++ b/src/admin/libvirt_admin_public.syms
@@ -53,3 +53,8 @@ LIBVIRT_ADMIN_8.6.0 {
     global:
         virAdmConnectSetDaemonTimeout;
 } LIBVIRT_ADMIN_3.0.0;
+
+LIBVIRT_ADMIN_11.0.0 {
+    global:
+        virAdmConnectDaemonShutdown;
+} LIBVIRT_ADMIN_8.6.0;
diff --git a/src/rpc/virnetdaemon.c b/src/rpc/virnetdaemon.c
index d1d7bae569..8cc7af1182 100644
--- a/src/rpc/virnetdaemon.c
+++ b/src/rpc/virnetdaemon.c
@@ -815,8 +815,10 @@ virNetDaemonRun(virNetDaemon *dmn)
         }
     }
 
+    VIR_DEBUG("Main loop exited");
     if (dmn->graceful) {
         virThreadJoin(&shutdownThread);
+        VIR_DEBUG("Graceful shutdown complete");
     } else {
         VIR_WARN("Make forcefull daemon shutdown");
         exit(EXIT_FAILURE);
@@ -946,6 +948,8 @@ virNetDaemonSetShutdownCallbacks(virNetDaemon *dmn,
 {
     VIR_LOCK_GUARD lock = virObjectLockGuard(dmn);
 
+    VIR_DEBUG("Shutdown callbacks preserve=%p prepare=%p wait=%p",
+              preserveCb, prepareCb, waitCb);
     dmn->shutdownPreserveCb = preserveCb;
     dmn->shutdownPrepareCb = prepareCb;
     dmn->shutdownWaitCb = waitCb;
diff --git a/tools/virt-admin.c b/tools/virt-admin.c
index 325b7aa827..54dc36a564 100644
--- a/tools/virt-admin.c
+++ b/tools/virt-admin.c
@@ -1077,6 +1077,41 @@ cmdDaemonTimeout(vshControl *ctl, const vshCmd *cmd)
 }
 
 
+/* --------------------------
+ * Command daemon-shutdown
+ * --------------------------
+ */
+static const vshCmdInfo info_daemon_shutdown = {
+    .help = N_("stop the daemon"),
+    .desc = N_("stop the daemon"),
+};
+
+static const vshCmdOptDef opts_daemon_shutdown[] = {
+    {.name = "preserve",
+     .type = VSH_OT_BOOL,
+     .required = false,
+     .positional = false,
+     .help = N_("preserve state before shutting down"),
+    },
+    {.name = NULL}
+};
+
+static bool
+cmdDaemonShutdown(vshControl *ctl, const vshCmd *cmd)
+{
+    vshAdmControl *priv = ctl->privData;
+    unsigned int flags = 0;
+
+    if (vshCommandOptBool(cmd, "preserve"))
+        flags |= VIR_DAEMON_SHUTDOWN_PRESERVE;
+
+    if (virAdmConnectDaemonShutdown(priv->conn, flags) < 0)
+        return false;
+
+    return true;
+}
+
+
 static void *
 vshAdmConnectionHandler(vshControl *ctl)
 {
@@ -1469,6 +1504,12 @@ static const vshCmdDef managementCmds[] = {
      .info = &info_daemon_timeout,
      .flags = 0
     },
+    {.name = "daemon-shutdown",
+     .handler = cmdDaemonShutdown,
+     .opts = opts_daemon_shutdown,
+     .info = &info_daemon_shutdown,
+     .flags = 0
+    },
     {.name = NULL}
 };
 
-- 
2.47.1