[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: [libvirt] Open monitor logs O_APPEND



On Thu, Dec 04, 2008 at 01:26:40PM +0000, Daniel P. Berrange wrote:
> On Thu, Dec 04, 2008 at 02:11:07PM +0100, Guido G?nther wrote:
> > Hi,
> > attached patch is from Debian Bug #507553:
> > 
> > The logfile is opened with O_APPEND (to make logrotate work with
> > copytruncate) and without O_TRUNC (to not lose log information e.g. with
> > stop and start of a VM).
> > 
> > Using collectd or munin to monitor VMs creates quiet a bit of qemu
> > monitor output so the ability to use logrotate makes a lot of sense to
> > me.
> 
> And is there a corresponding logrotate config file from Debian we
> can use ?  If we're going to let log files append, then we need to
> ship a logrotate config by default too.
I've changed the patch to only use O_APPEND as root so we don't fill up
the user's log when running as qemu:///session

> ACK if we add a logrotate config too
New version attached. O.k. to apply?
 -- Guido
>From c0e2c2ac0a62bc2f7907efe03f8f34f117035977 Mon Sep 17 00:00:00 2001
From: =?utf-8?q?Guido=20G=C3=BCnther?= <agx sigxcpu org>
Date: Thu, 4 Dec 2008 17:09:39 +0100
Subject: [PATCH] handle qemu monitor logs via logrotate

therefore use O_APPEND instead of O_CREAT if running as root
---
 qemud/Makefile.am           |   13 ++++++++++++-
 qemud/libvirtd.logrotate.in |    8 ++++++++
 src/qemu_driver.c           |   10 ++++++++--
 3 files changed, 28 insertions(+), 3 deletions(-)
 create mode 100644 qemud/libvirtd.logrotate.in

diff --git a/qemud/Makefile.am b/qemud/Makefile.am
index 15d3717..7b778d8 100644
--- a/qemud/Makefile.am
+++ b/qemud/Makefile.am
@@ -132,7 +132,8 @@ libvirtd_DEPENDENCIES = $(libvirtd_LDADD)
 
 
 default_xml_dest = libvirt/qemu/networks/default.xml
-install-data-local: install-init install-data-sasl install-data-polkit
+install-data-local: install-init install-data-sasl install-data-polkit \
+                    install-logrotate
 	mkdir -p $(DESTDIR)$(sysconfdir)/libvirt/qemu/networks/autostart
 	$(INSTALL_DATA) $(srcdir)/default-network.xml \
 	  $(DESTDIR)$(sysconfdir)/$(default_xml_dest)
@@ -191,6 +192,16 @@ remote_dispatch_localvars.h: $(srcdir)/remote_generate_stubs.pl remote_protocol.
 remote_dispatch_proc_switch.h: $(srcdir)/remote_generate_stubs.pl remote_protocol.x
 	perl -w $(srcdir)/remote_generate_stubs.pl -w $(srcdir)/remote_protocol.x > $@
 
+libvirtd.logrotate: libvirtd.logrotate.in
+	sed						\
+	    -e s!\ localstatedir\@! localstatedir@!g	\
+	    < $< > $ -t
+	mv $ -t $@
+
+install-logrotate: libvirtd.logrotate
+	mkdir -p $(DESTDIR)$(sysconfdir)/logrotate.d/
+	$(INSTALL_DATA) $< $(DESTDIR)$(sysconfdir)/logrotate.d/$<
+
 if LIBVIRT_INIT_SCRIPTS_RED_HAT
 install-init: libvirtd.init
 	mkdir -p $(DESTDIR)$(sysconfdir)/rc.d/init.d
diff --git a/qemud/libvirtd.logrotate.in b/qemud/libvirtd.logrotate.in
new file mode 100644
index 0000000..9b42630
--- /dev/null
+++ b/qemud/libvirtd.logrotate.in
@@ -0,0 +1,8 @@
+ localstatedir@/log/libvirt/qemu/*.log {
+        daily
+        missingok
+        rotate 7
+        compress
+        delaycompress
+        copytruncate
+}
diff --git a/src/qemu_driver.c b/src/qemu_driver.c
index efe780c..0e8acc9 100644
--- a/src/qemu_driver.c
+++ b/src/qemu_driver.c
@@ -798,6 +798,8 @@ static int qemudStartVMDaemon(virConnectPtr conn,
     unsigned int qemuCmdFlags;
     fd_set keepfd;
     const char *emulator;
+    uid_t uid = geteuid();
+    mode_t logmode;
 
     FD_ZERO(&keepfd);
 
@@ -841,8 +843,12 @@ static int qemudStartVMDaemon(virConnectPtr conn,
         return -1;
     }
 
-    if ((vm->logfile = open(logfile, O_CREAT | O_TRUNC | O_WRONLY,
-                            S_IRUSR | S_IWUSR)) < 0) {
+    logmode = O_CREAT | O_WRONLY;
+    if (uid != 0)
+        logmode |= O_TRUNC;
+    else
+        logmode |= O_APPEND;
+    if ((vm->logfile = open(logfile, logmode, S_IRUSR | S_IWUSR)) < 0) {
         qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
                          _("failed to create logfile %s: %s"),
                          logfile, strerror(errno));
-- 
1.6.0.2


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]