mirror of
https://github.com/linuxkit/linuxkit.git
synced 2025-07-19 09:16:29 +00:00
pkg/sysctl fixes: support commented KV lines, no post-crit KV set skips, support adding sysctl .conf files
- Previously, KV lines which were commented would attempt to be set. Now any commented KV lines will also be ignored. - Comments can start with a hash or semicolon - Splitting KV on both period and forward slash - Some kernels may not have certain features enabled (such as IPv6) in the default etc/sysctl.d/*.conf, and thus pkg/sysctl would only set the KV until the first failure, and then silently skip the rest of the KVs. Now any failure is logged as a WARN, and those lines can now be commented per the above change, as they will be identified. Signed-off-by: Isaac Rodman <isaac@eyz.us>
This commit is contained in:
parent
9e65276160
commit
6ac2ab8924
@ -22,17 +22,19 @@ func init() {
|
||||
}
|
||||
|
||||
func sysctl(line []byte) error {
|
||||
sysctlLineTrimmed := strings.TrimSpace(string(line[:]))
|
||||
// skip any commented lines
|
||||
if len(sysctlLineTrimmed) >= 1 && (sysctlLineTrimmed[:1] == "#" || sysctlLineTrimmed[:1] == ";") {
|
||||
return nil
|
||||
}
|
||||
// parse line into a string of expected form X.Y.Z=VALUE
|
||||
sysctlLineKV := strings.Split(string(line[:]), "=")
|
||||
sysctlLineKV := strings.Split(sysctlLineTrimmed, "=")
|
||||
if len(sysctlLineKV) != 2 {
|
||||
if len(sysctlLineKV) >= 1 && len(sysctlLineKV[0]) >= 1 && strings.Trim(sysctlLineKV[0], " ")[:1] == "#" {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("Cannot parse %s", string(line))
|
||||
return fmt.Errorf("Cannot parse %s", sysctlLineTrimmed)
|
||||
}
|
||||
// trim any extra whitespace
|
||||
sysctlSetting, sysctlValue := strings.Trim(sysctlLineKV[0], " "), strings.Trim(sysctlLineKV[1], " ")
|
||||
sysctlFile := filepath.Join(sysctlDir, filepath.Join(strings.Split(sysctlSetting, ".")...))
|
||||
sysctlSetting, sysctlValue := strings.TrimSpace(sysctlLineKV[0]), strings.TrimSpace(sysctlLineKV[1])
|
||||
sysctlFile := filepath.Join(sysctlDir, filepath.Join(strings.FieldsFunc(sysctlSetting, splitKv)...))
|
||||
file, err := os.OpenFile(sysctlFile, os.O_WRONLY, 0)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Cannot open %s: %s", sysctlFile, err)
|
||||
@ -45,6 +47,10 @@ func sysctl(line []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func splitKv(r rune) bool {
|
||||
return r == '.' || r == '/'
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
@ -65,7 +71,7 @@ func main() {
|
||||
}
|
||||
err = sysctl(line)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Println(fmt.Errorf("WARN: %v", err))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user