mirror of
https://github.com/linuxkit/linuxkit.git
synced 2025-08-01 06:58:56 +00:00
However, do not try to change memory cgroups from it; this needs to be in `init`. Signed-off-by: Justin Cormack <justin.cormack@docker.com>
73 lines
1.6 KiB
Go
73 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"flag"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
var (
|
|
configDir string
|
|
sysfsDir string
|
|
)
|
|
|
|
func init() {
|
|
flag.StringVar(&configDir, "configDir", "/etc/sysfs.d", "directory with config files")
|
|
flag.StringVar(&sysfsDir, "sysfsDir", "/sys", "mount point for sysfs")
|
|
}
|
|
|
|
func sysfs(line []byte) error {
|
|
// parse line into a string of expected form X/Y/Z=VALUE
|
|
sysfsLineKV := strings.Split(string(line[:]), "=")
|
|
if len(sysfsLineKV) != 2 {
|
|
if len(sysfsLineKV) >= 1 && len(sysfsLineKV[0]) >= 1 && strings.Trim(sysfsLineKV[0], " ")[:1] == "#" {
|
|
return nil
|
|
}
|
|
return fmt.Errorf("Cannot parse %s", string(line))
|
|
}
|
|
// trim any extra whitespace
|
|
sysfsSetting, sysfsValue := strings.Trim(sysfsLineKV[0], " "), strings.Trim(sysfsLineKV[1], " ")
|
|
sysfsFile := filepath.Join(sysfsDir, sysfsSetting)
|
|
file, err := os.OpenFile(sysfsFile, os.O_WRONLY, 0)
|
|
if err != nil {
|
|
return fmt.Errorf("Cannot open %s: %s", sysfsFile, err)
|
|
}
|
|
defer file.Close()
|
|
_, err = file.Write([]byte(sysfsValue))
|
|
if err != nil {
|
|
return fmt.Errorf("Cannot write to %s: %s", sysfsFile, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
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 = sysfs(line)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
}
|