Browse code

Change root_maxkeys

Most modern distros have the limit for the maximum root keys at 1000000
but some do not. Because we are creating a new key for each container
we need to bump this up as the older distros are having this limit at
200.

Using 1000000 as the limit because that is that most distros are setting
this to now. If someone has this value configured over that we do not
change it.

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>

Michael Crosby authored on 2016/06/02 09:29:06
Showing 3 changed files
... ...
@@ -387,6 +387,11 @@ func (daemon *Daemon) IsSwarmCompatible() error {
387 387
 func NewDaemon(config *Config, registryService registry.Service, containerdRemote libcontainerd.Remote) (daemon *Daemon, err error) {
388 388
 	setDefaultMtu(config)
389 389
 
390
+	// Ensure that we have a correct root key limit for launching containers.
391
+	if err := ModifyRootKeyLimit(); err != nil {
392
+		logrus.Warnf("unable to modify root key limit, number of containers could be limitied by this quota: %v", err)
393
+	}
394
+
390 395
 	// Ensure we have compatible and valid configuration options
391 396
 	if err := verifyDaemonSettings(config); err != nil {
392 397
 		return nil, err
393 398
new file mode 100644
... ...
@@ -0,0 +1,59 @@
0
+// +build linux
1
+
2
+package daemon
3
+
4
+import (
5
+	"fmt"
6
+	"io/ioutil"
7
+	"os"
8
+	"strconv"
9
+	"strings"
10
+)
11
+
12
+const (
13
+	rootKeyFile   = "/proc/sys/kernel/keys/root_maxkeys"
14
+	rootBytesFile = "/proc/sys/kernel/keys/root_maxbytes"
15
+	rootKeyLimit  = 1000000
16
+	// it is standard configuration to allocate 25 bytes per key
17
+	rootKeyByteMultiplier = 25
18
+)
19
+
20
+// ModifyRootKeyLimit checks to see if the root key limit is set to
21
+// at least 1000000 and changes it to that limit along with the maxbytes
22
+// allocated to the keys at a 25 to 1 multiplier.
23
+func ModifyRootKeyLimit() error {
24
+	value, err := readRootKeyLimit(rootKeyFile)
25
+	if err != nil {
26
+		return err
27
+	}
28
+	if value < rootKeyLimit {
29
+		return setRootKeyLimit(rootKeyLimit)
30
+	}
31
+	return nil
32
+}
33
+
34
+func setRootKeyLimit(limit int) error {
35
+	keys, err := os.OpenFile(rootKeyFile, os.O_WRONLY, 0)
36
+	if err != nil {
37
+		return err
38
+	}
39
+	defer keys.Close()
40
+	if _, err := fmt.Fprintf(keys, "%d", limit); err != nil {
41
+		return err
42
+	}
43
+	bytes, err := os.OpenFile(rootBytesFile, os.O_WRONLY, 0)
44
+	if err != nil {
45
+		return err
46
+	}
47
+	defer bytes.Close()
48
+	_, err = fmt.Fprintf(bytes, "%d", limit*rootKeyByteMultiplier)
49
+	return err
50
+}
51
+
52
+func readRootKeyLimit(path string) (int, error) {
53
+	data, err := ioutil.ReadFile(path)
54
+	if err != nil {
55
+		return -1, err
56
+	}
57
+	return strconv.Atoi(strings.Trim(string(data), "\n"))
58
+}
0 59
new file mode 100644
... ...
@@ -0,0 +1,8 @@
0
+// +build !linux
1
+
2
+package daemon
3
+
4
+// ModifyRootKeyLimit is an noop on unsupported platforms.
5
+func ModifyRootKeyLimit() error {
6
+	return nil
7
+}