mirror of
				https://github.com/linuxkit/linuxkit.git
				synced 2025-11-04 04:50:17 +00:00 
			
		
		
		
	- 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>
		
			
				
	
	
		
			79 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			79 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package main
 | 
						|
 | 
						|
import (
 | 
						|
	"bytes"
 | 
						|
	"flag"
 | 
						|
	"fmt"
 | 
						|
	"io/ioutil"
 | 
						|
	"log"
 | 
						|
	"os"
 | 
						|
	"path/filepath"
 | 
						|
	"strings"
 | 
						|
)
 | 
						|
 | 
						|
var (
 | 
						|
	configDir string
 | 
						|
	sysctlDir string
 | 
						|
)
 | 
						|
 | 
						|
func init() {
 | 
						|
	flag.StringVar(&configDir, "configDir", "/etc/sysctl.d", "directory with config files")
 | 
						|
	flag.StringVar(&sysctlDir, "sysctlDir", "/proc/sys", "mount point for sysctls")
 | 
						|
}
 | 
						|
 | 
						|
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(sysctlLineTrimmed, "=")
 | 
						|
	if len(sysctlLineKV) != 2 {
 | 
						|
		return fmt.Errorf("Cannot parse %s", sysctlLineTrimmed)
 | 
						|
	}
 | 
						|
	// trim any extra whitespace
 | 
						|
	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)
 | 
						|
	}
 | 
						|
	defer file.Close()
 | 
						|
	_, err = file.Write([]byte(sysctlValue))
 | 
						|
	if err != nil {
 | 
						|
		return fmt.Errorf("Cannot write to %s: %s", sysctlFile, err)
 | 
						|
	}
 | 
						|
	return nil
 | 
						|
}
 | 
						|
 | 
						|
func splitKv(r rune) bool {
 | 
						|
	return r == '.' || r == '/'
 | 
						|
}
 | 
						|
 | 
						|
func main() {
 | 
						|
	flag.Parse()
 | 
						|
 | 
						|
	files, err := ioutil.ReadDir(configDir)
 | 
						|
	if err != nil {
 | 
						|
		log.Fatalf("Cannot read directory %s: %s", configDir, err)
 | 
						|
	}
 | 
						|
 | 
						|
	for _, file := range files {
 | 
						|
		contents, err := ioutil.ReadFile(filepath.Join(configDir, file.Name()))
 | 
						|
		if err != nil {
 | 
						|
			log.Fatalf("Cannot read file %s: %s", file.Name(), err)
 | 
						|
		}
 | 
						|
		lines := bytes.Split(contents, []byte("\n"))
 | 
						|
		for _, line := range lines {
 | 
						|
			if len(line) == 0 {
 | 
						|
				continue
 | 
						|
			}
 | 
						|
			err = sysctl(line)
 | 
						|
			if err != nil {
 | 
						|
				log.Println(fmt.Errorf("WARN: %v", err))
 | 
						|
			}
 | 
						|
		}
 | 
						|
	}
 | 
						|
}
 |