github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/libnetwork/osl/kernel/knobs_linux.go (about) 1 package kernel 2 3 import ( 4 "context" 5 "os" 6 "path" 7 "strings" 8 9 "github.com/containerd/log" 10 ) 11 12 // writeSystemProperty writes the value to a path under /proc/sys as determined from the key. 13 // For e.g. net.ipv4.ip_forward translated to /proc/sys/net/ipv4/ip_forward. 14 func writeSystemProperty(key, value string) error { 15 keyPath := strings.ReplaceAll(key, ".", "/") 16 return os.WriteFile(path.Join("/proc/sys", keyPath), []byte(value), 0o644) 17 } 18 19 // readSystemProperty reads the value from the path under /proc/sys and returns it 20 func readSystemProperty(key string) (string, error) { 21 keyPath := strings.ReplaceAll(key, ".", "/") 22 value, err := os.ReadFile(path.Join("/proc/sys", keyPath)) 23 if err != nil { 24 return "", err 25 } 26 return strings.TrimSpace(string(value)), nil 27 } 28 29 // ApplyOSTweaks applies the configuration values passed as arguments 30 func ApplyOSTweaks(osConfig map[string]*OSValue) { 31 for k, v := range osConfig { 32 // read the existing property from disk 33 oldv, err := readSystemProperty(k) 34 if err != nil { 35 log.G(context.TODO()).WithError(err).Errorf("error reading the kernel parameter %s", k) 36 continue 37 } 38 39 if propertyIsValid(oldv, v.Value, v.CheckFn) { 40 // write new prop value to disk 41 if err := writeSystemProperty(k, v.Value); err != nil { 42 log.G(context.TODO()).WithError(err).Errorf("error setting the kernel parameter %s = %s, (leaving as %s)", k, v.Value, oldv) 43 continue 44 } 45 log.G(context.TODO()).Debugf("updated kernel parameter %s = %s (was %s)", k, v.Value, oldv) 46 } 47 } 48 }