github.com/Heebron/moby@v0.0.0-20221111184709-6eab4f55faf7/libnetwork/osl/kernel/knobs_linux.go (about)

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