hypervisor: Move QEMU log context to hypervisor

While doing so, also drop QEMU specific arguments from
domainLogContextNew() and replace them with hypervisor agnostic
ones.

Signed-off-by: Praveen K Paladugu <praveenkpaladugu@gmail.com>
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Praveen K Paladugu 2024-12-19 08:45:52 -06:00 committed by Michal Privoznik
parent 94b393dd6e
commit 334d2f604c
11 changed files with 146 additions and 125 deletions

View File

@ -94,6 +94,7 @@ src/hyperv/hyperv_util.c
src/hyperv/hyperv_wmi.c
src/hypervisor/domain_cgroup.c
src/hypervisor/domain_driver.c
src/hypervisor/domain_logcontext.c
src/hypervisor/domain_interface.c
src/hypervisor/virhostdev.c
src/interface/interface_backend_netcf.c
@ -180,7 +181,6 @@ src/qemu/qemu_hostdev.c
src/qemu/qemu_hotplug.c
src/qemu/qemu_interface.c
src/qemu/qemu_interop_config.c
src/qemu/qemu_logcontext.c
src/qemu/qemu_migration.c
src/qemu/qemu_migration_cookie.c
src/qemu/qemu_migration_params.c

View File

@ -1,5 +1,5 @@
/*
* qemu_logcontext.c: QEMU log context
* domain_logcontext.c: Domain log context
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@ -18,20 +18,22 @@
#include <config.h>
#include "qemu_logcontext.h"
#include "domain_logcontext.h"
#include "viralloc.h"
#include "virlog.h"
#include "virstring.h"
#include "virutil.h"
#include "virfile.h"
#include "virerror.h"
#include <fcntl.h>
#define VIR_FROM_THIS VIR_FROM_QEMU
#define VIR_FROM_THIS VIR_FROM_DOMAIN
VIR_LOG_INIT("qemu.qemu_logcontext");
VIR_LOG_INIT("domain.domain_logcontext");
struct _qemuLogContext {
struct _domainLogContext {
GObject parent;
int writefd;
@ -42,62 +44,64 @@ struct _qemuLogContext {
virLogManager *manager;
};
G_DEFINE_TYPE(qemuLogContext, qemu_log_context, G_TYPE_OBJECT);
G_DEFINE_TYPE(domainLogContext, domain_log_context, G_TYPE_OBJECT);
static void
qemuLogContextFinalize(GObject *obj);
domainLogContextFinalize(GObject *obj);
static void
qemu_log_context_init(qemuLogContext *logctxt G_GNUC_UNUSED)
domain_log_context_init(domainLogContext *logctxt G_GNUC_UNUSED)
{
}
static void
qemu_log_context_class_init(qemuLogContextClass *klass)
domain_log_context_class_init(domainLogContextClass *klass)
{
GObjectClass *obj = G_OBJECT_CLASS(klass);
obj->finalize = qemuLogContextFinalize;
obj->finalize = domainLogContextFinalize;
}
static void
qemuLogContextFinalize(GObject *object)
domainLogContextFinalize(GObject *object)
{
qemuLogContext *ctxt = QEMU_LOG_CONTEXT(object);
domainLogContext *ctxt = DOMAIN_LOG_CONTEXT(object);
VIR_DEBUG("ctxt=%p", ctxt);
virLogManagerFree(ctxt->manager);
VIR_FREE(ctxt->path);
VIR_FORCE_CLOSE(ctxt->writefd);
VIR_FORCE_CLOSE(ctxt->readfd);
G_OBJECT_CLASS(qemu_log_context_parent_class)->finalize(object);
G_OBJECT_CLASS(domain_log_context_parent_class)->finalize(object);
}
qemuLogContext *
qemuLogContextNew(virQEMUDriver *driver,
domainLogContext *
domainLogContextNew(bool stdioLogD,
char *logDir,
const char *driver_name,
virDomainObj *vm,
bool privileged,
const char *basename)
{
g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
qemuLogContext *ctxt = QEMU_LOG_CONTEXT(g_object_new(QEMU_TYPE_LOG_CONTEXT, NULL));
domainLogContext *ctxt = DOMAIN_LOG_CONTEXT(g_object_new(DOMAIN_TYPE_LOG_CONTEXT, NULL));
VIR_DEBUG("Context new %p stdioLogD=%d", ctxt, cfg->stdioLogD);
VIR_DEBUG("Context new %p stdioLogD=%d", ctxt, stdioLogD);
ctxt->writefd = -1;
ctxt->readfd = -1;
ctxt->path = g_strdup_printf("%s/%s.log", cfg->logDir, basename);
ctxt->path = g_strdup_printf("%s/%s.log", logDir, basename);
if (cfg->stdioLogD) {
ctxt->manager = virLogManagerNew(driver->privileged);
if (stdioLogD) {
ctxt->manager = virLogManagerNew(privileged);
if (!ctxt->manager)
goto error;
ctxt->writefd = virLogManagerDomainOpenLogFile(ctxt->manager,
"qemu",
driver_name,
vm->def->uuid,
vm->def->name,
ctxt->path,
@ -121,7 +125,7 @@ qemuLogContextNew(virQEMUDriver *driver,
/* For unprivileged startup we must truncate the file since
* we can't rely on logrotate. We don't use O_TRUNC since
* it is better for SELinux policy if we truncate afterwards */
if (!driver->privileged &&
if (!privileged &&
ftruncate(ctxt->writefd, 0) < 0) {
virReportSystemError(errno, _("failed to truncate %1$s"),
ctxt->path);
@ -155,7 +159,7 @@ qemuLogContextNew(virQEMUDriver *driver,
int
qemuLogContextWrite(qemuLogContext *ctxt,
domainLogContextWrite(domainLogContext *ctxt,
const char *fmt, ...)
{
va_list argptr;
@ -186,7 +190,7 @@ qemuLogContextWrite(qemuLogContext *ctxt,
ssize_t
qemuLogContextRead(qemuLogContext *ctxt,
domainLogContextRead(domainLogContext *ctxt,
char **msg)
{
char *buf;
@ -238,7 +242,7 @@ qemuLogContextRead(qemuLogContext *ctxt,
/**
* qemuLogContextFilter: Read and filter log for relevant messages
* domainLogContextFilter: Read and filter log for relevant messages
* @ctxt: the domain log context
* @msg: pointer to buffer to store the read messages in
* @max: maximum length of the message returned in @msg after filtering
@ -249,7 +253,7 @@ qemuLogContextRead(qemuLogContext *ctxt,
* after a new line if possible.
*/
int
qemuLogContextReadFiltered(qemuLogContext *ctxt,
domainLogContextReadFiltered(domainLogContext *ctxt,
char **msg,
size_t max)
{
@ -259,7 +263,7 @@ qemuLogContextReadFiltered(qemuLogContext *ctxt,
size_t skip;
ssize_t got;
if ((got = qemuLogContextRead(ctxt, &buf)) < 0)
if ((got = domainLogContextRead(ctxt, &buf)) < 0)
return -1;
/* Filter out debug messages from intermediate libvirt process */
@ -302,14 +306,14 @@ qemuLogContextReadFiltered(qemuLogContext *ctxt,
int
qemuLogContextGetWriteFD(qemuLogContext *ctxt)
domainLogContextGetWriteFD(domainLogContext *ctxt)
{
return ctxt->writefd;
}
void
qemuLogContextMarkPosition(qemuLogContext *ctxt)
domainLogContextMarkPosition(domainLogContext *ctxt)
{
if (ctxt->manager)
virLogManagerDomainGetLogFilePosition(ctxt->manager,
@ -323,7 +327,7 @@ qemuLogContextMarkPosition(qemuLogContext *ctxt)
virLogManager *
qemuLogContextGetManager(qemuLogContext *ctxt)
domainLogContextGetManager(domainLogContext *ctxt)
{
return ctxt->manager;
}

View File

@ -0,0 +1,45 @@
/*
* domain_logcontext.h: Domain log context
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <glib-object.h>
#include "logging/log_manager.h"
#include "virconftypes.h"
#include "domain_conf.h"
#define DOMAIN_TYPE_LOG_CONTEXT domain_log_context_get_type()
G_DECLARE_FINAL_TYPE(domainLogContext, domain_log_context, DOMAIN, LOG_CONTEXT, GObject);
domainLogContext *domainLogContextNew(bool stdioLogD,
char *logDir,
const char *driver_name,
virDomainObj *vm,
bool privileged,
const char *basename);
int domainLogContextWrite(domainLogContext *ctxt,
const char *fmt, ...) G_GNUC_PRINTF(2, 3);
ssize_t domainLogContextRead(domainLogContext *ctxt,
char **msg);
int domainLogContextReadFiltered(domainLogContext *ctxt,
char **msg,
size_t max);
int domainLogContextGetWriteFD(domainLogContext *ctxt);
void domainLogContextMarkPosition(domainLogContext *ctxt);
virLogManager *domainLogContextGetManager(domainLogContext *ctxt);

View File

@ -2,6 +2,7 @@ hypervisor_sources = [
'domain_cgroup.c',
'domain_driver.c',
'domain_interface.c',
'domain_logcontext.c',
'virclosecallbacks.c',
'virhostdev.c',
]

View File

@ -1665,6 +1665,12 @@ virDomainInterfaceStopDevice;
virDomainInterfaceStopDevices;
virDomainInterfaceVportRemove;
# hypervisor/domain_logcontext.h
domainLogContextGetWriteFD;
domainLogContextMarkPosition;
domainLogContextNew;
domainLogContextReadFiltered;
domainLogContextWrite;
# hypervisor/virclosecallbacks.h
virCloseCallbacksDomainAdd;

View File

@ -22,7 +22,6 @@ qemu_driver_sources = [
'qemu_hotplug.c',
'qemu_interface.c',
'qemu_interop_config.c',
'qemu_logcontext.c',
'qemu_migration.c',
'qemu_migration_cookie.c',
'qemu_migration_params.c',

View File

@ -5399,7 +5399,7 @@ static void G_GNUC_PRINTF(5, 6)
qemuDomainObjTaintMsg(virQEMUDriver *driver,
virDomainObj *obj,
virDomainTaintFlags taint,
qemuLogContext *logCtxt,
domainLogContext *logCtxt,
const char *fmt, ...)
{
virErrorPtr orig_err = NULL;
@ -5452,7 +5452,7 @@ qemuDomainObjTaintMsg(virQEMUDriver *driver,
goto cleanup;
if (logCtxt) {
rc = qemuLogContextWrite(logCtxt,
rc = domainLogContextWrite(logCtxt,
"%s: Domain id=%d is tainted: %s%s%s%s\n",
timestamp,
obj->def->id,
@ -5478,7 +5478,7 @@ qemuDomainObjTaintMsg(virQEMUDriver *driver,
void qemuDomainObjTaint(virQEMUDriver *driver,
virDomainObj *obj,
virDomainTaintFlags taint,
qemuLogContext *logCtxt)
domainLogContext *logCtxt)
{
qemuDomainObjTaintMsg(driver, obj, taint, logCtxt, NULL);
qemuDomainSaveStatus(obj);
@ -5487,7 +5487,7 @@ void qemuDomainObjTaint(virQEMUDriver *driver,
static void
qemuDomainObjCheckMachineTaint(virQEMUDriver *driver,
virDomainObj *obj,
qemuLogContext *logCtxt)
domainLogContext *logCtxt)
{
qemuDomainObjPrivate *priv = obj->privateData;
virQEMUCaps *qemuCaps = priv->qemuCaps;
@ -5505,7 +5505,7 @@ qemuDomainObjCheckMachineTaint(virQEMUDriver *driver,
static void
qemuDomainObjCheckCPUTaint(virQEMUDriver *driver,
virDomainObj *obj,
qemuLogContext *logCtxt,
domainLogContext *logCtxt,
bool incomingMigration)
{
qemuDomainObjPrivate *priv = obj->privateData;
@ -5537,7 +5537,7 @@ qemuDomainObjCheckCPUTaint(virQEMUDriver *driver,
void qemuDomainObjCheckTaint(virQEMUDriver *driver,
virDomainObj *obj,
qemuLogContext *logCtxt,
domainLogContext *logCtxt,
bool incomingMigration)
{
size_t i;
@ -5593,7 +5593,7 @@ void qemuDomainObjCheckTaint(virQEMUDriver *driver,
void qemuDomainObjCheckDiskTaint(virQEMUDriver *driver,
virDomainObj *obj,
virDomainDiskDef *disk,
qemuLogContext *logCtxt)
domainLogContext *logCtxt)
{
if (disk->rawio == VIR_TRISTATE_BOOL_YES)
qemuDomainObjTaint(driver, obj, VIR_DOMAIN_TAINT_HIGH_PRIVILEGES,
@ -5610,7 +5610,7 @@ void qemuDomainObjCheckDiskTaint(virQEMUDriver *driver,
void qemuDomainObjCheckHostdevTaint(virQEMUDriver *driver,
virDomainObj *obj,
virDomainHostdevDef *hostdev,
qemuLogContext *logCtxt)
domainLogContext *logCtxt)
{
if (!virHostdevIsSCSIDevice(hostdev))
return;
@ -5623,7 +5623,7 @@ void qemuDomainObjCheckHostdevTaint(virQEMUDriver *driver,
void qemuDomainObjCheckNetTaint(virQEMUDriver *driver,
virDomainObj *obj,
virDomainNetDef *net,
qemuLogContext *logCtxt)
domainLogContext *logCtxt)
{
/* script is only useful for NET_TYPE_ETHERNET (qemu) and
* NET_TYPE_BRIDGE (xen), but could be (incorrectly) specified for

View File

@ -26,13 +26,13 @@
#include "virperf.h"
#include "domain_addr.h"
#include "domain_conf.h"
#include "domain_logcontext.h"
#include "qemu_monitor.h"
#include "qemu_agent.h"
#include "qemu_blockjob.h"
#include "qemu_domainjob.h"
#include "qemu_conf.h"
#include "qemu_capabilities.h"
#include "qemu_logcontext.h"
#include "qemu_migration_params.h"
#include "qemu_nbdkit.h"
#include "qemu_slirp.h"
@ -641,24 +641,24 @@ char *qemuDomainDefFormatLive(virQEMUDriver *driver,
void qemuDomainObjTaint(virQEMUDriver *driver,
virDomainObj *obj,
virDomainTaintFlags taint,
qemuLogContext *logCtxt);
domainLogContext *logCtxt);
void qemuDomainObjCheckTaint(virQEMUDriver *driver,
virDomainObj *obj,
qemuLogContext *logCtxt,
domainLogContext *logCtxt,
bool incomingMigration);
void qemuDomainObjCheckDiskTaint(virQEMUDriver *driver,
virDomainObj *obj,
virDomainDiskDef *disk,
qemuLogContext *logCtxt);
domainLogContext *logCtxt);
void qemuDomainObjCheckHostdevTaint(virQEMUDriver *driver,
virDomainObj *obj,
virDomainHostdevDef *disk,
qemuLogContext *logCtxt);
domainLogContext *logCtxt);
void qemuDomainObjCheckNetTaint(virQEMUDriver *driver,
virDomainObj *obj,
virDomainNetDef *net,
qemuLogContext *logCtxt);
domainLogContext *logCtxt);
int qemuDomainLogAppendMessage(virQEMUDriver *driver,
virDomainObj *vm,

View File

@ -1,41 +0,0 @@
/*
* qemu_logcontext.h: QEMU log context
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <glib-object.h>
#include "qemu_conf.h"
#include "logging/log_manager.h"
#define QEMU_TYPE_LOG_CONTEXT qemu_log_context_get_type()
G_DECLARE_FINAL_TYPE(qemuLogContext, qemu_log_context, QEMU, LOG_CONTEXT, GObject);
qemuLogContext *qemuLogContextNew(virQEMUDriver *driver,
virDomainObj *vm,
const char *basename);
int qemuLogContextWrite(qemuLogContext *ctxt,
const char *fmt, ...) G_GNUC_PRINTF(2, 3);
ssize_t qemuLogContextRead(qemuLogContext *ctxt,
char **msg);
int qemuLogContextReadFiltered(qemuLogContext *ctxt,
char **msg,
size_t max);
int qemuLogContextGetWriteFD(qemuLogContext *ctxt);
void qemuLogContextMarkPosition(qemuLogContext *ctxt);
virLogManager *qemuLogContextGetManager(qemuLogContext *ctxt);

View File

@ -1177,6 +1177,7 @@ qemuNbdkitProcessStart(qemuNbdkitProcess *proc,
virQEMUDriver *driver)
{
g_autoptr(virCommand) cmd = NULL;
g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
int rc;
int exitstatus = 0;
g_autofree char *errbuf = NULL;
@ -1185,7 +1186,7 @@ qemuNbdkitProcessStart(qemuNbdkitProcess *proc,
g_autofree char *uristring = NULL;
g_autofree char *basename = g_strdup_printf("%s-nbdkit-%i", vm->def->name, proc->source->id);
int logfd = -1;
g_autoptr(qemuLogContext) logContext = NULL;
g_autoptr(domainLogContext) logContext = NULL;
#if WITH_NBDKIT
struct nbd_handle *nbd = NULL;
#endif
@ -1200,12 +1201,15 @@ qemuNbdkitProcessStart(qemuNbdkitProcess *proc,
if (!(cmd = qemuNbdkitProcessBuildCommand(proc)))
return -1;
if (!(logContext = qemuLogContextNew(driver, vm, basename))) {
if (!(logContext = domainLogContextNew(cfg->stdioLogD, cfg->logDir,
QEMU_DRIVER_NAME,
vm, driver->privileged,
basename))) {
virLastErrorPrefixMessage("%s", _("can't connect to virtlogd"));
return -1;
}
logfd = qemuLogContextGetWriteFD(logContext);
logfd = domainLogContextGetWriteFD(logContext);
VIR_DEBUG("starting nbdkit process for %s", qemuBlockStorageSourceGetStorageNodename(proc->source));
virCommandSetErrorFD(cmd, &logfd);
@ -1283,7 +1287,7 @@ qemuNbdkitProcessStart(qemuNbdkitProcess *proc,
if ((uri = qemuBlockStorageSourceGetURI(proc->source)))
uristring = virURIFormat(uri);
if (qemuLogContextReadFiltered(logContext, &errbuf, 1024) < 0)
if (domainLogContextReadFiltered(logContext, &errbuf, 1024) < 0)
VIR_WARN("Unable to read from nbdkit log");
virReportError(VIR_ERR_OPERATION_FAILED,

View File

@ -1840,7 +1840,7 @@ qemuProcessMonitorReportLogError(qemuMonitor *mon,
static void
qemuProcessMonitorLogFree(void *opaque)
{
qemuLogContext *logCtxt = opaque;
domainLogContext *logCtxt = opaque;
g_clear_object(&logCtxt);
}
@ -1866,7 +1866,7 @@ static int
qemuConnectMonitor(virQEMUDriver *driver,
virDomainObj *vm,
int asyncJob,
qemuLogContext *logCtxt,
domainLogContext *logCtxt,
bool reconnect)
{
qemuDomainObjPrivate *priv = vm->privateData;
@ -1918,13 +1918,13 @@ qemuConnectMonitor(virQEMUDriver *driver,
static int
qemuProcessReportLogError(qemuLogContext *logCtxt,
qemuProcessReportLogError(domainLogContext *logCtxt,
const char *msgprefix)
{
g_autofree char *logmsg = NULL;
/* assume that 1024 chars of qemu log is the right balance */
if (qemuLogContextReadFiltered(logCtxt, &logmsg, 1024) < 0)
if (domainLogContextReadFiltered(logCtxt, &logmsg, 1024) < 0)
return -1;
virResetLastError();
@ -1943,7 +1943,7 @@ qemuProcessMonitorReportLogError(qemuMonitor *mon G_GNUC_UNUSED,
const char *msg,
void *opaque)
{
qemuLogContext *logCtxt = opaque;
domainLogContext *logCtxt = opaque;
qemuProcessReportLogError(logCtxt, msg);
}
@ -2244,7 +2244,7 @@ static int
qemuProcessWaitForMonitor(virQEMUDriver *driver,
virDomainObj *vm,
int asyncJob,
qemuLogContext *logCtxt)
domainLogContext *logCtxt)
{
int ret = -1;
g_autoptr(GHashTable) info = NULL;
@ -4740,7 +4740,7 @@ static void
qemuLogOperation(virDomainObj *vm,
const char *msg,
virCommand *cmd,
qemuLogContext *logCtxt)
domainLogContext *logCtxt)
{
g_autofree char *timestamp = NULL;
qemuDomainObjPrivate *priv = vm->privateData;
@ -4754,7 +4754,7 @@ qemuLogOperation(virDomainObj *vm,
if ((timestamp = virTimeStringNow()) == NULL)
return;
if (qemuLogContextWrite(logCtxt,
if (domainLogContextWrite(logCtxt,
"%s: %s %s, qemu version: %d.%d.%d%s, kernel: %s, hostname: %s\n",
timestamp, msg, VIR_LOG_VERSION_STRING,
(qemuVersion / 1000000) % 1000,
@ -4767,7 +4767,7 @@ qemuLogOperation(virDomainObj *vm,
if (cmd) {
g_autofree char *args = virCommandToString(cmd, true);
qemuLogContextWrite(logCtxt, "%s\n", args);
domainLogContextWrite(logCtxt, "%s\n", args);
}
}
@ -7775,7 +7775,7 @@ qemuProcessLaunch(virConnectPtr conn,
int ret = -1;
int rv;
int logfile = -1;
g_autoptr(qemuLogContext) logCtxt = NULL;
g_autoptr(domainLogContext) logCtxt = NULL;
qemuDomainObjPrivate *priv = vm->privateData;
g_autoptr(virCommand) cmd = NULL;
struct qemuProcessHookData hookData;
@ -7825,11 +7825,14 @@ qemuProcessLaunch(virConnectPtr conn,
hookData.cfg = cfg;
VIR_DEBUG("Creating domain log file");
if (!(logCtxt = qemuLogContextNew(driver, vm, vm->def->name))) {
if (!(logCtxt = domainLogContextNew(cfg->stdioLogD, cfg->logDir,
QEMU_DRIVER_NAME,
vm, driver->privileged,
vm->def->name))) {
virLastErrorPrefixMessage("%s", _("can't connect to virtlogd"));
goto cleanup;
}
logfile = qemuLogContextGetWriteFD(logCtxt);
logfile = domainLogContextGetWriteFD(logCtxt);
if (qemuProcessGenID(vm, flags) < 0)
goto cleanup;
@ -7865,7 +7868,7 @@ qemuProcessLaunch(virConnectPtr conn,
qemuDomainObjCheckTaint(driver, vm, logCtxt, incoming != NULL);
qemuLogContextMarkPosition(logCtxt);
domainLogContextMarkPosition(logCtxt);
if (qemuProcessEnableDomainNamespaces(driver, vm) < 0)
goto cleanup;