The following pull request was submitted through Github.
It can be accessed and reviewed at: https://github.com/lxc/lxcfs/pull/271

This e-mail was sent by the LXC bot, direct replies will not reach the author
unless they happen to be subscribed to this list.

=== Description (from pull-request) ===
Signed-off-by: Dieter Maier <d...@web.de>

It concerns the combination of a system with activated swap and docker. When I use lxcfs to limit the memory resources of a container, I always have swap.. Which I don't want.

docker run -it -m 256m --memory-swap 256m \
      -v /var/lib/lxcfs/proc/cpuinfo:/proc/cpuinfo:rw \
      -v /var/lib/lxcfs/proc/diskstats:/proc/diskstats:rw \
      -v /var/lib/lxcfs/proc/meminfo:/proc/meminfo:rw \
      -v /var/lib/lxcfs/proc/stat:/proc/stat:rw \
      -v /var/lib/lxcfs/proc/swaps:/proc/swaps:rw \
      -v /var/lib/lxcfs/proc/uptime:/proc/uptime:rw \
      ubuntu:18.04 /bin/bash

That, too, is not satisfactory:
docker run -it -m 256m \
      -v /var/lib/lxcfs/proc/cpuinfo:/proc/cpuinfo:rw \
      ....

I can force with the parameter "-u" that the swap values in the "meminfo" are always 0.

To use this parameter I use "fuse_get_context()->private_data".

It is a very special use case, but I wanted to at least suggest it.

I haven't programmed a C for a long time, so please take a critical look at it.

It works very well with Ubuntu. Unfortunately not with Alpine yet. I tested it with "free".

I developed the customization to use several mongodb on one docker host, which unfortunately have swap enabled. This works very well.
From fe2549f18f95abd42bf412887e595a5de675f3e0 Mon Sep 17 00:00:00 2001
From: dm <dm@localhost>
Date: Mon, 18 Feb 2019 06:17:30 +0100
Subject: [PATCH] option to disable swap in meminfo

Signed-off-by: Dieter Maier <d...@web.de>
---
 README.md  | 17 +++++++++++++++++
 bindings.c | 35 +++++++++++++++++++++++++----------
 bindings.h |  4 ++++
 lxcfs.c    | 20 ++++++++++++++++----
 4 files changed, 62 insertions(+), 14 deletions(-)

diff --git a/README.md b/README.md
index 4c1434a..93e7b37 100644
--- a/README.md
+++ b/README.md
@@ -60,3 +60,20 @@ send `SIGUSR1` to the pid of the running `LXCFS` process. 
This can be as simple
 as doing:
 
     kill -s USR1 $(pidof lxcfs)
+    
+## Using with Docker
+
+```
+docker run -it -m 256m --memory-swap 256m \
+      -v /var/lib/lxcfs/proc/cpuinfo:/proc/cpuinfo:rw \
+      -v /var/lib/lxcfs/proc/diskstats:/proc/diskstats:rw \
+      -v /var/lib/lxcfs/proc/meminfo:/proc/meminfo:rw \
+      -v /var/lib/lxcfs/proc/stat:/proc/stat:rw \
+      -v /var/lib/lxcfs/proc/swaps:/proc/swaps:rw \
+      -v /var/lib/lxcfs/proc/uptime:/proc/uptime:rw \
+      ubuntu:18.04 /bin/bash
+ ```
+ 
+ In a system with swap enabled, the parameter "-n" can be used to set all 
values in "meminfo" that refer to the swap to 0.
+ 
+ sudo lxcfs -u /var/lib/lxcfs
diff --git a/bindings.c b/bindings.c
index 097ca81..a90f349 100644
--- a/bindings.c
+++ b/bindings.c
@@ -3437,6 +3437,7 @@ static int proc_meminfo_read(char *buf, size_t size, 
off_t offset,
                struct fuse_file_info *fi)
 {
        struct fuse_context *fc = fuse_get_context();
+       struct lxcfs_opts *opts = (struct lxcfs_opts *) 
fuse_get_context()->private_data;
        struct file_info *d = (struct file_info *)fi->fh;
        char *cg;
        char *memusage_str = NULL, *memstat_str = NULL,
@@ -3517,18 +3518,32 @@ static int proc_meminfo_read(char *buf, size_t size, 
off_t offset,
                } else if (startswith(line, "MemAvailable:")) {
                        snprintf(lbuf, 100, "MemAvailable:   %8lu kB\n", 
memlimit - memusage + cached);
                        printme = lbuf;
-               } else if (startswith(line, "SwapTotal:") && memswlimit > 0) {
-                       sscanf(line+sizeof("SwapTotal:")-1, "%lu", 
&hostswtotal);
-                       if (hostswtotal < memswlimit)
-                               memswlimit = hostswtotal;
-                       snprintf(lbuf, 100, "SwapTotal:      %8lu kB\n", 
memswlimit);
-                       printme = lbuf;
-               } else if (startswith(line, "SwapFree:") && memswlimit > 0 && 
memswusage > 0) {
-                       unsigned long swaptotal = memswlimit,
+               } else if (startswith(line, "SwapTotal:")) {
+                        if (memswlimit > 0) {
+                            sscanf(line+sizeof("SwapTotal:")-1, "%lu", 
&hostswtotal);
+                            if (hostswtotal < memswlimit)
+                                    memswlimit = hostswtotal;
+                            snprintf(lbuf, 100, "SwapTotal:      %8lu kB\n", 
memswlimit);
+                            printme = lbuf;
+                        }
+                        if (opts->swap_off == true) {
+                            memswlimit = 0;
+                            snprintf(lbuf, 100, "SwapTotal:      %8lu kB\n", 
memswlimit);
+                            printme = lbuf;
+                        }
+               } else if (startswith(line, "SwapFree:")) {
+                        if (memswlimit > 0 && memswusage > 0) {
+                            unsigned long swaptotal = memswlimit,
                                        swapusage = memswusage - memusage,
                                        swapfree = swapusage < swaptotal ? 
swaptotal - swapusage : 0;
-                       snprintf(lbuf, 100, "SwapFree:       %8lu kB\n", 
swapfree);
-                       printme = lbuf;
+                            snprintf(lbuf, 100, "SwapFree:       %8lu kB\n", 
swapfree);
+                            printme = lbuf;
+                        }
+                        if (opts->swap_off == true) {
+                            unsigned long swapfree = 0;
+                            snprintf(lbuf, 100, "SwapFree:       %8lu kB\n", 
swapfree);
+                            printme = lbuf;
+                        }
                } else if (startswith(line, "Slab:")) {
                        snprintf(lbuf, 100, "Slab:        %8lu kB\n", 0UL);
                        printme = lbuf;
diff --git a/bindings.h b/bindings.h
index f1a4627..2f76861 100644
--- a/bindings.h
+++ b/bindings.h
@@ -7,6 +7,10 @@
 #define BASEDIR RUNTIME_PATH "/lxcfs/controllers"
 #define ROOTDIR RUNTIME_PATH "/lxcfs/root"
 
+struct lxcfs_opts {
+    bool swap_off;
+};
+
 extern int cg_write(const char *path, const char *buf, size_t size, off_t 
offset,
             struct fuse_file_info *fi);
 extern int cg_mkdir(const char *path, mode_t mode);
diff --git a/lxcfs.c b/lxcfs.c
index 76b3ea2..953266b 100644
--- a/lxcfs.c
+++ b/lxcfs.c
@@ -792,9 +792,10 @@ static void usage()
 {
        fprintf(stderr, "Usage:\n");
        fprintf(stderr, "\n");
-       fprintf(stderr, "lxcfs [-f|-d] -l [-p pidfile] mountpoint\n");
+       fprintf(stderr, "lxcfs [-f|-d] -u -l -n [-p pidfile] mountpoint\n");
        fprintf(stderr, "  -f running foreground by default; -d enable debug 
output \n");
        fprintf(stderr, "  -l use loadavg \n");
+        fprintf(stderr, "  -u no swap \n");
        fprintf(stderr, "  Default pidfile is %s/lxcfs.pid\n", RUNTIME_PATH);
        fprintf(stderr, "lxcfs -h\n");
        exit(1);
@@ -903,7 +904,15 @@ int main(int argc, char *argv[])
         */
        int nargs = 5, cnt = 0;
        char *newargv[6];
-
+       
+        struct lxcfs_opts *opts;
+       opts = malloc(sizeof(struct lxcfs_opts));
+       if (opts == NULL) {
+               fprintf(stderr, "Error setting options: %m\n");
+               goto out;
+       }
+        opts->swap_off = false;
+        
        /* accomodate older init scripts */
        swallow_arg(&argc, argv, "-s");
        swallow_arg(&argc, argv, "-f");
@@ -911,6 +920,9 @@ int main(int argc, char *argv[])
        if (swallow_arg(&argc, argv, "-l")) {
                load_use = true;
        }
+        if (swallow_arg(&argc, argv, "-u")) {
+               opts->swap_off = true;
+       }
        if (swallow_option(&argc, argv, "-o", &v)) {
                /* Parse multiple values */
                for (; (token = strtok_r(v, ",", &saveptr)); v = NULL) {
@@ -929,7 +941,7 @@ int main(int argc, char *argv[])
        }
        if (swallow_option(&argc, argv, "-p", &v))
                pidfile = v;
-
+        
        if (argc == 2  && strcmp(argv[1], "--version") == 0) {
                fprintf(stderr, "%s\n", VERSION);
                exit(EXIT_SUCCESS);
@@ -967,7 +979,7 @@ int main(int argc, char *argv[])
        if (load_use && start_loadavg() != 0)
                goto out;
 
-       if (!fuse_main(nargs, newargv, &lxcfs_ops, NULL))
+       if (!fuse_main(nargs, newargv, &lxcfs_ops, opts))
                ret = EXIT_SUCCESS;
        if (load_use)
                stop_loadavg();
_______________________________________________
lxc-devel mailing list
lxc-devel@lists.linuxcontainers.org
http://lists.linuxcontainers.org/listinfo/lxc-devel

Reply via email to