Forráskód Böngészése

Use mount to determine the cgroup mountpoint

Guillaume J. Charmes 12 éve
szülő
commit
f3e89fae28
2 módosított fájl, 26 hozzáadás és 3 törlés
  1. 8 3
      runtime.go
  2. 18 0
      utils.go

+ 8 - 3
runtime.go

@@ -305,11 +305,16 @@ func NewRuntime() (*Runtime, error) {
 		log.Printf("WARNING: You are running linux kernel version %s, which might be unstable running docker. Please upgrade your kernel to 3.8.0.", k.String())
 	}
 
-	_, err1 := ioutil.ReadFile("/sys/fs/cgroup/memory/memory.limit_in_bytes")
-	_, err2 := ioutil.ReadFile("/sys/fs/cgroup/memory/memory.soft_limit_in_bytes")
+	cgroupMemoryMountpoint, err := FindCgroupMountpoint("memory")
+	if err != nil {
+		return nil, err
+	}
+
+	_, err1 := ioutil.ReadFile(path.Join(cgroupMemoryMountpoint, "/memory.limit_in_bytes"))
+	_, err2 := ioutil.ReadFile(path.Join(cgroupMemoryMountpoint, "memory.soft_limit_in_bytes"))
 	runtime.capabilities.MemoryLimit = err1 == nil && err2 == nil
 
-	_, err = ioutil.ReadFile("/sys/fs/cgroup/memory/memeory.memsw.limit_in_bytes")
+	_, err = ioutil.ReadFile(path.Join(cgroupMemoryMountpoint, "memeory.memsw.limit_in_bytes"))
 	runtime.capabilities.SwapLimit = err == nil
 
 	return runtime, nil

+ 18 - 0
utils.go

@@ -12,6 +12,7 @@ import (
 	"os"
 	"os/exec"
 	"path/filepath"
+	"regexp"
 	"runtime"
 	"strconv"
 	"strings"
@@ -478,3 +479,20 @@ func CompareKernelVersion(a, b *KernelVersionInfo) int {
 	}
 	return 0
 }
+
+func FindCgroupMountpoint(cgroupType string) (string, error) {
+	output, err := exec.Command("mount").CombinedOutput()
+	if err != nil {
+		return "", err
+	}
+
+	reg := regexp.MustCompile(`^cgroup on (.*) type cgroup \(.*` + cgroupType + `[,\)]`)
+	for _, line := range strings.Split(string(output), "\n") {
+		r := reg.FindStringSubmatch(line)
+		if len(r) == 2 {
+			return r[1], nil
+		}
+		fmt.Printf("line: %s (%d)\n", line, len(r))
+	}
+	return "", fmt.Errorf("cgroup mountpoint not found")
+}