1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2025-03-30 18:50:18 +03:00

admin: add 'daemon-shutdown' command

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.

Reviewed-by: Peter Krempa <pkrempa@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrangé 2025-01-07 13:25:49 +00:00
parent 94029c9d64
commit 27020d20ee
8 changed files with 132 additions and 1 deletions

View File

@ -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
===============

View File

@ -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

View File

@ -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
};

View File

@ -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,

View File

@ -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;
}

View File

@ -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;

View File

@ -148,6 +148,9 @@ struct admin_connect_set_daemon_timeout_args {
u_int timeout;
u_int flags;
};
struct admin_connect_daemon_shutdown_args {
u_int flags;
};
enum admin_procedure {
ADMIN_PROC_CONNECT_OPEN = 1,
ADMIN_PROC_CONNECT_CLOSE = 2,
@ -168,4 +171,5 @@ enum admin_procedure {
ADMIN_PROC_CONNECT_SET_LOGGING_FILTERS = 17,
ADMIN_PROC_SERVER_UPDATE_TLS_FILES = 18,
ADMIN_PROC_CONNECT_SET_DAEMON_TIMEOUT = 19,
ADMIN_PROC_CONNECT_DAEMON_SHUTDOWN = 20,
};

View File

@ -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}
};