Browse code

Use mount to determine the cgroup mountpoint

Guillaume J. Charmes authored on 2013/04/19 13:57:58
Showing 2 changed files
... ...
@@ -305,11 +305,16 @@ func NewRuntime() (*Runtime, error) {
305 305
 		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())
306 306
 	}
307 307
 
308
-	_, err1 := ioutil.ReadFile("/sys/fs/cgroup/memory/memory.limit_in_bytes")
309
-	_, err2 := ioutil.ReadFile("/sys/fs/cgroup/memory/memory.soft_limit_in_bytes")
308
+	cgroupMemoryMountpoint, err := FindCgroupMountpoint("memory")
309
+	if err != nil {
310
+		return nil, err
311
+	}
312
+
313
+	_, err1 := ioutil.ReadFile(path.Join(cgroupMemoryMountpoint, "/memory.limit_in_bytes"))
314
+	_, err2 := ioutil.ReadFile(path.Join(cgroupMemoryMountpoint, "memory.soft_limit_in_bytes"))
310 315
 	runtime.capabilities.MemoryLimit = err1 == nil && err2 == nil
311 316
 
312
-	_, err = ioutil.ReadFile("/sys/fs/cgroup/memory/memeory.memsw.limit_in_bytes")
317
+	_, err = ioutil.ReadFile(path.Join(cgroupMemoryMountpoint, "memeory.memsw.limit_in_bytes"))
313 318
 	runtime.capabilities.SwapLimit = err == nil
314 319
 
315 320
 	return runtime, nil
... ...
@@ -12,6 +12,7 @@ import (
12 12
 	"os"
13 13
 	"os/exec"
14 14
 	"path/filepath"
15
+	"regexp"
15 16
 	"runtime"
16 17
 	"strconv"
17 18
 	"strings"
... ...
@@ -478,3 +479,20 @@ func CompareKernelVersion(a, b *KernelVersionInfo) int {
478 478
 	}
479 479
 	return 0
480 480
 }
481
+
482
+func FindCgroupMountpoint(cgroupType string) (string, error) {
483
+	output, err := exec.Command("mount").CombinedOutput()
484
+	if err != nil {
485
+		return "", err
486
+	}
487
+
488
+	reg := regexp.MustCompile(`^cgroup on (.*) type cgroup \(.*` + cgroupType + `[,\)]`)
489
+	for _, line := range strings.Split(string(output), "\n") {
490
+		r := reg.FindStringSubmatch(line)
491
+		if len(r) == 2 {
492
+			return r[1], nil
493
+		}
494
+		fmt.Printf("line: %s (%d)\n", line, len(r))
495
+	}
496
+	return "", fmt.Errorf("cgroup mountpoint not found")
497
+}