sysctl.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package sysctl
  14. import (
  15. "io/ioutil"
  16. "path"
  17. "strconv"
  18. "strings"
  19. )
  20. const (
  21. sysctlBase = "/proc/sys"
  22. // VMOvercommitMemory refers to the sysctl variable responsible for defining
  23. // the memory over-commit policy used by kernel.
  24. VMOvercommitMemory = "vm/overcommit_memory"
  25. // VMPanicOnOOM refers to the sysctl variable responsible for defining
  26. // the OOM behavior used by kernel.
  27. VMPanicOnOOM = "vm/panic_on_oom"
  28. // KernelPanic refers to the sysctl variable responsible for defining
  29. // the timeout after a panic for the kernel to reboot.
  30. KernelPanic = "kernel/panic"
  31. // KernelPanicOnOops refers to the sysctl variable responsible for defining
  32. // the kernel behavior when an oops or BUG is encountered.
  33. KernelPanicOnOops = "kernel/panic_on_oops"
  34. // RootMaxKeys refers to the sysctl variable responsible for defining
  35. // the maximum number of keys that the root user (UID 0 in the root user namespace) may own.
  36. RootMaxKeys = "kernel/keys/root_maxkeys"
  37. // RootMaxBytes refers to the sysctl variable responsible for defining
  38. // the maximum number of bytes of data that the root user (UID 0 in the root user namespace)
  39. // can hold in the payloads of the keys owned by root.
  40. RootMaxBytes = "kernel/keys/root_maxbytes"
  41. // VMOvercommitMemoryAlways represents that kernel performs no memory over-commit handling.
  42. VMOvercommitMemoryAlways = 1
  43. // VMPanicOnOOMInvokeOOMKiller represents that kernel calls the oom_killer function when OOM occurs.
  44. VMPanicOnOOMInvokeOOMKiller = 0
  45. // KernelPanicOnOopsAlways represents that kernel panics on kernel oops.
  46. KernelPanicOnOopsAlways = 1
  47. // KernelPanicRebootTimeout is the timeout seconds after a panic for the kernel to reboot.
  48. KernelPanicRebootTimeout = 10
  49. // RootMaxKeysSetting is the maximum number of keys that the root user (UID 0 in the root user namespace) may own.
  50. // Needed since docker creates a new key per container.
  51. RootMaxKeysSetting = 1000000
  52. // RootMaxBytesSetting is the maximum number of bytes of data that the root user (UID 0 in the root user namespace)
  53. // can hold in the payloads of the keys owned by root.
  54. // Allocate 25 bytes per key * number of MaxKeys.
  55. RootMaxBytesSetting = RootMaxKeysSetting * 25
  56. )
  57. // Interface is an injectable interface for running sysctl commands.
  58. type Interface interface {
  59. // GetSysctl returns the value for the specified sysctl setting
  60. GetSysctl(sysctl string) (int, error)
  61. // SetSysctl modifies the specified sysctl flag to the new value
  62. SetSysctl(sysctl string, newVal int) error
  63. }
  64. // New returns a new Interface for accessing sysctl
  65. func New() Interface {
  66. return &procSysctl{}
  67. }
  68. // procSysctl implements Interface by reading and writing files under /proc/sys
  69. type procSysctl struct {
  70. }
  71. // GetSysctl returns the value for the specified sysctl setting
  72. func (*procSysctl) GetSysctl(sysctl string) (int, error) {
  73. data, err := ioutil.ReadFile(path.Join(sysctlBase, sysctl))
  74. if err != nil {
  75. return -1, err
  76. }
  77. val, err := strconv.Atoi(strings.Trim(string(data), " \n"))
  78. if err != nil {
  79. return -1, err
  80. }
  81. return val, nil
  82. }
  83. // SetSysctl modifies the specified sysctl flag to the new value
  84. func (*procSysctl) SetSysctl(sysctl string, newVal int) error {
  85. return ioutil.WriteFile(path.Join(sysctlBase, sysctl), []byte(strconv.Itoa(newVal)), 0640)
  86. }