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(a)redhat.com>
---
docs/manpages/virt-admin.rst | 13 +++++++++
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 ++++
tools/virt-admin.c | 41 +++++++++++++++++++++++++++++
7 files changed, 128 insertions(+), 1 deletion(-)
diff --git a/docs/manpages/virt-admin.rst b/docs/manpages/virt-admin.rst
index 54a6512ef7..82e9594ba6 100644
--- a/docs/manpages/virt-admin.rst
+++ b/docs/manpages/virt-admin.rst
@@ -326,6 +326,19 @@ Sets the daemon timeout to the value of '--timeout' argument.
Use ``--timeout 0`
to disable auto-shutdown of the daemon.
+daemon-shutdown
+---------------
+
+**Syntax:**
+
+::
+
+ daemon-shutdown [--preserve]
+
+Instruct the daemon to exit gracefully. If the ``--preserve`` flag is given,
+it will save state in the same manner that would be done on a host OS shutdown
+(privileged daemons) or a login session quit (unprivileged daemons).
+
SERVER COMMANDS
===============
diff --git a/include/libvirt/libvirt-admin.h b/include/libvirt/libvirt-admin.h
index ae4703f89b..d5c0a5bc8f 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.2.0
+ */
+typedef enum {
+ /* Preserve state before shutting down daemon (Since: 11.2.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..0eee80f6ac 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)
+ virNetDaemonStop(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..d7efac025f 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.2.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..42feba0329 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.2.0 {
+ global:
+ virAdmConnectDaemonShutdown;
+} LIBVIRT_ADMIN_8.6.0;
diff --git a/tools/virt-admin.c b/tools/virt-admin.c
index 2d63098444..b701ed1fe4 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.48.1