Skip to content

Commit ca3e454

Browse files
committed
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 <[email protected]>
1 parent cccfe63 commit ca3e454

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

daemon/daemon.go

+5
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,11 @@ func (daemon *Daemon) IsSwarmCompatible() error {
387387
func NewDaemon(config *Config, registryService registry.Service, containerdRemote libcontainerd.Remote) (daemon *Daemon, err error) {
388388
setDefaultMtu(config)
389389

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+
390395
// Ensure we have compatible and valid configuration options
391396
if err := verifyDaemonSettings(config); err != nil {
392397
return nil, err

daemon/keys.go

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// +build linux
2+
3+
package daemon
4+
5+
import (
6+
"fmt"
7+
"io/ioutil"
8+
"os"
9+
"strconv"
10+
"strings"
11+
)
12+
13+
const (
14+
rootKeyFile = "/proc/sys/kernel/keys/root_maxkeys"
15+
rootBytesFile = "/proc/sys/kernel/keys/root_maxbytes"
16+
rootKeyLimit = 1000000
17+
// it is standard configuration to allocate 25 bytes per key
18+
rootKeyByteMultiplier = 25
19+
)
20+
21+
// ModifyRootKeyLimit checks to see if the root key limit is set to
22+
// at least 1000000 and changes it to that limit along with the maxbytes
23+
// allocated to the keys at a 25 to 1 multiplier.
24+
func ModifyRootKeyLimit() error {
25+
value, err := readRootKeyLimit(rootKeyFile)
26+
if err != nil {
27+
return err
28+
}
29+
if value < rootKeyLimit {
30+
return setRootKeyLimit(rootKeyLimit)
31+
}
32+
return nil
33+
}
34+
35+
func setRootKeyLimit(limit int) error {
36+
keys, err := os.OpenFile(rootKeyFile, os.O_WRONLY, 0)
37+
if err != nil {
38+
return err
39+
}
40+
defer keys.Close()
41+
if _, err := fmt.Fprintf(keys, "%d", limit); err != nil {
42+
return err
43+
}
44+
bytes, err := os.OpenFile(rootBytesFile, os.O_WRONLY, 0)
45+
if err != nil {
46+
return err
47+
}
48+
defer bytes.Close()
49+
_, err = fmt.Fprintf(bytes, "%d", limit*rootKeyByteMultiplier)
50+
return err
51+
}
52+
53+
func readRootKeyLimit(path string) (int, error) {
54+
data, err := ioutil.ReadFile(path)
55+
if err != nil {
56+
return -1, err
57+
}
58+
return strconv.Atoi(strings.Trim(string(data), "\n"))
59+
}

daemon/keys_unsupported.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// +build !linux
2+
3+
package daemon
4+
5+
// ModifyRootKeyLimit is an noop on unsupported platforms.
6+
func ModifyRootKeyLimit() error {
7+
return nil
8+
}

0 commit comments

Comments
 (0)