[libvirt] [PATCH] daemon: Allow overriding NOFILES ulimit for the daemon as well

Michal Privoznik mprivozn at redhat.com
Thu Jan 5 10:16:32 UTC 2012


One of my latest patches (d8db0f9690) created support for setting
the limit for the maximum of opened files by qemu user. However,
since libvirtd keeps one FD opened per domain (well, for qemu at least)
it will likely hit this limit on huge scenarios.
---
 daemon/libvirtd.aug  |    1 +
 daemon/libvirtd.c    |   37 +++++++++++++++++++++++++++++++++++++
 daemon/libvirtd.conf |    9 +++++++++
 3 files changed, 47 insertions(+), 0 deletions(-)

diff --git a/daemon/libvirtd.aug b/daemon/libvirtd.aug
index 9d78bd7..278099b 100644
--- a/daemon/libvirtd.aug
+++ b/daemon/libvirtd.aug
@@ -58,6 +58,7 @@ module Libvirtd =
                         | int_entry "max_requests"
                         | int_entry "max_client_requests"
                         | int_entry "prio_workers"
+                        | int_entry "max_files"
 
    let logging_entry = int_entry "log_level"
                      | str_entry "log_filters"
diff --git a/daemon/libvirtd.c b/daemon/libvirtd.c
index b1b542b..34bdd39 100644
--- a/daemon/libvirtd.c
+++ b/daemon/libvirtd.c
@@ -139,6 +139,8 @@ struct daemonConfig {
     int max_requests;
     int max_client_requests;
 
+    int max_files;
+
     int log_level;
     char *log_filters;
     char *log_outputs;
@@ -1081,6 +1083,8 @@ daemonConfigLoad(struct daemonConfig *data,
     GET_CONF_INT (conf, filename, max_requests);
     GET_CONF_INT (conf, filename, max_client_requests);
 
+    GET_CONF_INT (conf, filename, max_files);
+
     GET_CONF_INT (conf, filename, audit_level);
     GET_CONF_INT (conf, filename, audit_logging);
 
@@ -1198,6 +1202,34 @@ static int daemonStateInit(virNetServerPtr srv)
     return 0;
 }
 
+/*
+ * daemonSetProcessLimits:
+ * @config: where to take limits from
+ *
+ * Set both soft and hard limits set in config file.
+ *
+ * Returns 0 on success, -1 otherwise.
+ */
+static int
+daemonSetProcessLimits(struct daemonConfig *config)
+{
+    struct rlimit rlim;
+
+    if (config->max_files > 0) {
+        /* Max number of opened files is one greater than
+         * actual limit. See man setrlimit */
+        rlim.rlim_cur = rlim.rlim_max = config->max_files + 1;
+        if (setrlimit(RLIMIT_NOFILE, &rlim) < 0) {
+            virReportSystemError(errno,
+                                 _("cannot set max opened files to %d"),
+                                 config->max_files);
+            return -1;
+        }
+    }
+
+    return 0;
+}
+
 /* Print command-line usage. */
 static void
 daemonUsage(const char *argv0, bool privileged)
@@ -1429,6 +1461,11 @@ int main(int argc, char **argv) {
         exit(EXIT_FAILURE);
     }
 
+    if (daemonSetProcessLimits(config) < 0) {
+        /* Helper reported error */
+        exit(EXIT_FAILURE);
+    }
+
     if (godaemon) {
         char ebuf[1024];
 
diff --git a/daemon/libvirtd.conf b/daemon/libvirtd.conf
index 3eab2be..70d5294 100644
--- a/daemon/libvirtd.conf
+++ b/daemon/libvirtd.conf
@@ -279,6 +279,15 @@
 # and max_workers parameter
 #max_client_requests = 5
 
+# If max_files is set to positive integer, the maximum number
+# of opened files for the daemon is set to given value. This
+# can be used to override host system default value.
+# Please keep in mind that in some drivers (like qemu) this
+# will affect the upper bound of domains which can run simultaneously.
+# That's because the daemon keeps one file opened per domain - monitor.
+# Well, at least one.
+#max_files = 0
+
 #################################################################
 #
 # Logging controls
-- 
1.7.3.4




More information about the libvir-list mailing list