[lxc-devel] [lxc/master] Introducing lxc.console.buffer.rotate

duguhaotian on Github lxc-bot at linuxcontainers.org
Thu Jan 18 08:01:27 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 337 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180118/478fe914/attachment.bin>
-------------- next part --------------
From d819e20b8bfc765d7a34cbb1a25528cfe956a8f8 Mon Sep 17 00:00:00 2001
From: duguhaotian <duguhaotian at gmail.com>
Date: Thu, 18 Jan 2018 11:33:11 +0800
Subject: [PATCH 1/2] [confile] add lxc.console.buffer.rotate

Signed-off-by: duguhaotian <duguhaotian at gmail.com>
---
 doc/lxc.container.conf.sgml.in | 14 ++++++++++++++
 src/lxc/conf.h                 |  3 +++
 src/lxc/confile.c              | 35 +++++++++++++++++++++++++++++++++++
 3 files changed, 52 insertions(+)

diff --git a/doc/lxc.container.conf.sgml.in b/doc/lxc.container.conf.sgml.in
index d106c9635..77b82b206 100644
--- a/doc/lxc.container.conf.sgml.in
+++ b/doc/lxc.container.conf.sgml.in
@@ -832,6 +832,20 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
           </listitem>
         </varlistentry>
 
+        <varlistentry>
+          <term>
+            <option>lxc.console.buffer.rotate</option>
+          </term>
+          <listitem>
+            <para>
+              Whether to rotate the buffer logfile specified in
+              <option>lxc.console.buffer.logfile</option>. When ringbuffer is full, 
+              will to rotate the logfile and dump the contents of the in-memory 
+              ringbuffer to disk, and clear ringbuffer. 
+            </para>
+          </listitem>
+        </varlistentry>
+
         <varlistentry>
           <term>
             <option>lxc.console.buffer.logfile</option>
diff --git a/src/lxc/conf.h b/src/lxc/conf.h
index 1146a1d4f..e408a092b 100644
--- a/src/lxc/conf.h
+++ b/src/lxc/conf.h
@@ -184,6 +184,9 @@ struct lxc_console {
 		/* fd to the log file for the ringbuffer */
 		int buffer_log_file_fd;
 
+		/* whether to roate buffer log file */
+		unsigned int buffer_rotate;
+
 		/* the in-memory ringbuffer */
 		struct lxc_ringbuf ringbuf;
 	};
diff --git a/src/lxc/confile.c b/src/lxc/confile.c
index 3deec58bf..150aac8e8 100644
--- a/src/lxc/confile.c
+++ b/src/lxc/confile.c
@@ -85,6 +85,7 @@ lxc_config_define(cgroup_dir);
 lxc_config_define(console_logfile);
 lxc_config_define(console_rotate);
 lxc_config_define(console_buffer_logfile);
+lxc_config_define(console_buffer_rotate);
 lxc_config_define(console_buffer_size);
 lxc_config_define(console_path);
 lxc_config_define(environment);
@@ -156,6 +157,7 @@ static struct lxc_config_t config[] = {
 	{ "lxc.cgroup.dir",                false,                  set_config_cgroup_dir,                  get_config_cgroup_dir,                  clr_config_cgroup_dir,                },
 	{ "lxc.cgroup",                    false,                  set_config_cgroup_controller,           get_config_cgroup_controller,           clr_config_cgroup_controller,         },
 	{ "lxc.console.buffer.logfile",    false,                  set_config_console_buffer_logfile,      get_config_console_buffer_logfile,      clr_config_console_buffer_logfile,    },
+	{ "lxc.console.buffer.rotate",	   false,                  set_config_console_buffer_rotate,	   get_config_console_buffer_rotate,       clr_config_console_buffer_rotate,	 },
 	{ "lxc.console.buffer.size",       false,                  set_config_console_buffer_size,         get_config_console_buffer_size,         clr_config_console_buffer_size,       },
 	{ "lxc.console.logfile",           false,                  set_config_console_logfile,             get_config_console_logfile,             clr_config_console_logfile,           },
 	{ "lxc.console.path",              false,                  set_config_console_path,                get_config_console_path,                clr_config_console_path,              },
@@ -1973,6 +1975,26 @@ static int set_config_console_buffer_size(const char *key, const char *value,
 	return 0;
 }
 
+static int set_config_console_buffer_rotate(const char *key, const char *value,
+				     struct lxc_conf *lxc_conf, void *data)
+{
+	if (lxc_config_value_empty(value)) {
+		lxc_conf->console.buffer_rotate = 0;
+		return 0;
+	}
+
+	if (lxc_safe_uint(value, &lxc_conf->console.buffer_rotate) < 0)
+		return -1;
+
+	if (lxc_conf->console.buffer_rotate > 1) {
+		ERROR("The \"lxc.console.buffer.rotate\" config key can only be set "
+		      "to 0 or 1");
+		return -1;
+	}
+
+	return 0;
+}
+
 static int set_config_console_buffer_logfile(const char *key, const char *value,
 					     struct lxc_conf *lxc_conf,
 					     void *data)
@@ -3279,6 +3301,12 @@ static int get_config_console_buffer_size(const char *key, char *retv,
 	return lxc_get_conf_uint64(c, retv, inlen, c->console.buffer_size);
 }
 
+static int get_config_console_buffer_rotate(const char *key, char *retv, int inlen,
+				     struct lxc_conf *c, void *data)
+{
+	return lxc_get_conf_int(c, retv, inlen, c->console.buffer_rotate);
+}
+
 static int get_config_console_buffer_logfile(const char *key, char *retv,
 					     int inlen, struct lxc_conf *c,
 					     void *data)
@@ -3789,6 +3817,13 @@ static inline int clr_config_console_buffer_size(const char *key,
 	return 0;
 }
 
+static inline int clr_config_console_buffer_rotate(const char *key, struct lxc_conf *c,
+					    void *data)
+{
+	c->console.buffer_rotate = 0;
+	return 0;
+}
+
 static inline int clr_config_console_buffer_logfile(const char *key,
 						    struct lxc_conf *c,
 						    void *data)

From 974d9558b29f35df4e7546c0e5b763f35c53b71f Mon Sep 17 00:00:00 2001
From: duguhaotian <duguhaotian at gmail.com>
Date: Thu, 18 Jan 2018 16:11:43 +0800
Subject: [PATCH 2/2] [console] add ringbuffer rotate

Signed-off-by: duguhaotian <duguhaotian at gmail.com>
---
 src/lxc/console.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/src/lxc/console.c b/src/lxc/console.c
index f875f8461..428b43460 100644
--- a/src/lxc/console.c
+++ b/src/lxc/console.c
@@ -209,7 +209,7 @@ int lxc_console_cb_con(int fd, uint32_t events, void *data,
 {
 	struct lxc_console *console = (struct lxc_console *)data;
 	char buf[LXC_CONSOLE_BUFFER_SIZE];
-	int r, w, w_log, w_rbuf;
+	int r, w, w_log, w_rbuf, free;
 
 	w = r = lxc_read_nointr(fd, buf, sizeof(buf));
 	if (r <= 0) {
@@ -231,15 +231,25 @@ int lxc_console_cb_con(int fd, uint32_t events, void *data,
 	if (fd == console->peer)
 		w = lxc_write_nointr(console->master, buf, r);
 
-	w_rbuf = w_log = 0;
+	free = w_rbuf = w_log = 0;
 	if (fd == console->master) {
 		/* write to peer first */
 		if (console->peer >= 0)
 			w = lxc_write_nointr(console->peer, buf, r);
 
 		/* write to console ringbuffer */
-		if (console->buffer_size > 0)
+		if (console->buffer_size > 0) {
+			if (console->buffer_log_file_fd > 0 && console->buffer_rotate) {
+				free = lxc_ringbuf_free(&console->ringbuf);
+				if (r <= console->buffer_size && r > free) {
+					if (lxc_console_write_ringbuffer(console))
+						WARN("when rotate, write ringbuffer into file failed.");
+					else
+						lxc_ringbuf_clear(&console->ringbuf);
+				}
+			}
 			w_rbuf = lxc_ringbuf_write(&console->ringbuf, buf, r);
+		}
 
 		/* write to console log */
 		if (console->log_fd >= 0)


More information about the lxc-devel mailing list