Browse code

Refactor cgroups file locations Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)

Michael Crosby authored on 2014/04/19 13:14:58
Showing 3 changed files
... ...
@@ -1,14 +1,7 @@
1 1
 package cgroups
2 2
 
3 3
 import (
4
-	"bufio"
5 4
 	"errors"
6
-	"github.com/dotcloud/docker/pkg/mount"
7
-	"io"
8
-	"io/ioutil"
9
-	"os"
10
-	"path/filepath"
11
-	"strings"
12 5
 )
13 6
 
14 7
 var (
... ...
@@ -32,68 +25,7 @@ type ActiveCgroup interface {
32 32
 	Cleanup() error
33 33
 }
34 34
 
35
-// https://www.kernel.org/doc/Documentation/cgroups/cgroups.txt
36
-func FindCgroupMountpoint(subsystem string) (string, error) {
37
-	mounts, err := mount.GetMounts()
38
-	if err != nil {
39
-		return "", err
40
-	}
41
-
42
-	for _, mount := range mounts {
43
-		if mount.Fstype == "cgroup" {
44
-			for _, opt := range strings.Split(mount.VfsOpts, ",") {
45
-				if opt == subsystem {
46
-					return mount.Mountpoint, nil
47
-				}
48
-			}
49
-		}
50
-	}
51
-	return "", ErrNotFound
52
-}
53
-
54
-// Returns the relative path to the cgroup docker is running in.
55
-func GetThisCgroupDir(subsystem string) (string, error) {
56
-	f, err := os.Open("/proc/self/cgroup")
57
-	if err != nil {
58
-		return "", err
59
-	}
60
-	defer f.Close()
61
-
62
-	return parseCgroupFile(subsystem, f)
63
-}
64
-
65
-func GetInitCgroupDir(subsystem string) (string, error) {
66
-	f, err := os.Open("/proc/1/cgroup")
67
-	if err != nil {
68
-		return "", err
69
-	}
70
-	defer f.Close()
71
-
72
-	return parseCgroupFile(subsystem, f)
73
-}
74
-
75
-func parseCgroupFile(subsystem string, r io.Reader) (string, error) {
76
-	s := bufio.NewScanner(r)
77
-	for s.Scan() {
78
-		if err := s.Err(); err != nil {
79
-			return "", err
80
-		}
81
-		text := s.Text()
82
-		parts := strings.Split(text, ":")
83
-		for _, subs := range strings.Split(parts[1], ",") {
84
-			if subs == subsystem {
85
-				return parts[2], nil
86
-			}
87
-		}
88
-	}
89
-	return "", ErrNotFound
90
-}
91
-
92
-func writeFile(dir, file, data string) error {
93
-	return ioutil.WriteFile(filepath.Join(dir, file), []byte(data), 0700)
94
-}
95
-
96
-func (c *Cgroup) Apply(pid int) (ActiveCgroup, error) {
35
+func Apply(c *Cgroup, pid int) (ActiveCgroup, error) {
97 36
 	// We have two implementation of cgroups support, one is based on
98 37
 	// systemd and the dbus api, and one is based on raw cgroup fs operations
99 38
 	// following the pre-single-writer model docs at:
100 39
new file mode 100644
... ...
@@ -0,0 +1,73 @@
0
+package cgroups
1
+
2
+import (
3
+	"bufio"
4
+	"io"
5
+	"io/ioutil"
6
+	"os"
7
+	"path/filepath"
8
+	"strings"
9
+
10
+	"github.com/dotcloud/docker/pkg/mount"
11
+)
12
+
13
+// https://www.kernel.org/doc/Documentation/cgroups/cgroups.txt
14
+func FindCgroupMountpoint(subsystem string) (string, error) {
15
+	mounts, err := mount.GetMounts()
16
+	if err != nil {
17
+		return "", err
18
+	}
19
+
20
+	for _, mount := range mounts {
21
+		if mount.Fstype == "cgroup" {
22
+			for _, opt := range strings.Split(mount.VfsOpts, ",") {
23
+				if opt == subsystem {
24
+					return mount.Mountpoint, nil
25
+				}
26
+			}
27
+		}
28
+	}
29
+	return "", ErrNotFound
30
+}
31
+
32
+// Returns the relative path to the cgroup docker is running in.
33
+func GetThisCgroupDir(subsystem string) (string, error) {
34
+	f, err := os.Open("/proc/self/cgroup")
35
+	if err != nil {
36
+		return "", err
37
+	}
38
+	defer f.Close()
39
+
40
+	return parseCgroupFile(subsystem, f)
41
+}
42
+
43
+func GetInitCgroupDir(subsystem string) (string, error) {
44
+	f, err := os.Open("/proc/1/cgroup")
45
+	if err != nil {
46
+		return "", err
47
+	}
48
+	defer f.Close()
49
+
50
+	return parseCgroupFile(subsystem, f)
51
+}
52
+
53
+func parseCgroupFile(subsystem string, r io.Reader) (string, error) {
54
+	s := bufio.NewScanner(r)
55
+	for s.Scan() {
56
+		if err := s.Err(); err != nil {
57
+			return "", err
58
+		}
59
+		text := s.Text()
60
+		parts := strings.Split(text, ":")
61
+		for _, subs := range strings.Split(parts[1], ",") {
62
+			if subs == subsystem {
63
+				return parts[2], nil
64
+			}
65
+		}
66
+	}
67
+	return "", ErrNotFound
68
+}
69
+
70
+func writeFile(dir, file, data string) error {
71
+	return ioutil.WriteFile(filepath.Join(dir, file), []byte(data), 0700)
72
+}
... ...
@@ -99,7 +99,7 @@ func (ns *linuxNs) Exec(container *libcontainer.Container, term Terminal, args [
99 99
 
100 100
 func (ns *linuxNs) SetupCgroups(container *libcontainer.Container, nspid int) (cgroups.ActiveCgroup, error) {
101 101
 	if container.Cgroups != nil {
102
-		return container.Cgroups.Apply(nspid)
102
+		return cgroups.Apply(container.Cgroups, nspid)
103 103
 	}
104 104
 	return nil, nil
105 105
 }