Browse code

Move raw cgroups into fs package (filesystem) Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)

Michael Crosby authored on 2014/04/19 13:34:26
Showing 4 changed files
1 1
deleted file mode 100644
... ...
@@ -1,256 +0,0 @@
1
-package cgroups
2
-
3
-import (
4
-	"fmt"
5
-	"os"
6
-	"path/filepath"
7
-	"strconv"
8
-)
9
-
10
-type rawCgroup struct {
11
-	root   string
12
-	cgroup string
13
-}
14
-
15
-func rawApply(c *Cgroup, pid int) (ActiveCgroup, error) {
16
-	// We have two implementation of cgroups support, one is based on
17
-	// systemd and the dbus api, and one is based on raw cgroup fs operations
18
-	// following the pre-single-writer model docs at:
19
-	// http://www.freedesktop.org/wiki/Software/systemd/PaxControlGroups/
20
-	//
21
-	// we can pick any subsystem to find the root
22
-
23
-	cgroupRoot, err := FindCgroupMountpoint("cpu")
24
-	if err != nil {
25
-		return nil, err
26
-	}
27
-	cgroupRoot = filepath.Dir(cgroupRoot)
28
-
29
-	if _, err := os.Stat(cgroupRoot); err != nil {
30
-		return nil, fmt.Errorf("cgroups fs not found")
31
-	}
32
-
33
-	cgroup := c.Name
34
-	if c.Parent != "" {
35
-		cgroup = filepath.Join(c.Parent, cgroup)
36
-	}
37
-
38
-	raw := &rawCgroup{
39
-		root:   cgroupRoot,
40
-		cgroup: cgroup,
41
-	}
42
-	for _, g := range []func(*Cgroup, int) error{
43
-		raw.setupDevices,
44
-		raw.setupMemory,
45
-		raw.setupCpu,
46
-		raw.setupCpuset,
47
-		raw.setupCpuacct,
48
-		raw.setupBlkio,
49
-		raw.setupPerfevent,
50
-		raw.setupFreezer,
51
-	} {
52
-		if err := g(c, pid); err != nil {
53
-			return nil, err
54
-		}
55
-	}
56
-
57
-	return raw, nil
58
-}
59
-
60
-func (raw *rawCgroup) path(subsystem string) (string, error) {
61
-	initPath, err := GetInitCgroupDir(subsystem)
62
-	if err != nil {
63
-		return "", err
64
-	}
65
-	return filepath.Join(raw.root, subsystem, initPath, raw.cgroup), nil
66
-}
67
-
68
-func (raw *rawCgroup) join(subsystem string, pid int) (string, error) {
69
-	path, err := raw.path(subsystem)
70
-	if err != nil {
71
-		return "", err
72
-	}
73
-	if err := os.MkdirAll(path, 0755); err != nil && !os.IsExist(err) {
74
-		return "", err
75
-	}
76
-	if err := writeFile(path, "cgroup.procs", strconv.Itoa(pid)); err != nil {
77
-		return "", err
78
-	}
79
-	return path, nil
80
-}
81
-
82
-func (raw *rawCgroup) setupDevices(c *Cgroup, pid int) (err error) {
83
-	dir, err := raw.join("devices", pid)
84
-	if err != nil {
85
-		return err
86
-	}
87
-	defer func() {
88
-		if err != nil {
89
-			os.RemoveAll(dir)
90
-		}
91
-	}()
92
-
93
-	if !c.DeviceAccess {
94
-
95
-		if err := writeFile(dir, "devices.deny", "a"); err != nil {
96
-			return err
97
-		}
98
-
99
-		allow := []string{
100
-			// allow mknod for any device
101
-			"c *:* m",
102
-			"b *:* m",
103
-
104
-			// /dev/null, zero, full
105
-			"c 1:3 rwm",
106
-			"c 1:5 rwm",
107
-			"c 1:7 rwm",
108
-
109
-			// consoles
110
-			"c 5:1 rwm",
111
-			"c 5:0 rwm",
112
-			"c 4:0 rwm",
113
-			"c 4:1 rwm",
114
-
115
-			// /dev/urandom,/dev/random
116
-			"c 1:9 rwm",
117
-			"c 1:8 rwm",
118
-
119
-			// /dev/pts/ - pts namespaces are "coming soon"
120
-			"c 136:* rwm",
121
-			"c 5:2 rwm",
122
-
123
-			// tuntap
124
-			"c 10:200 rwm",
125
-		}
126
-
127
-		for _, val := range allow {
128
-			if err := writeFile(dir, "devices.allow", val); err != nil {
129
-				return err
130
-			}
131
-		}
132
-	}
133
-	return nil
134
-}
135
-
136
-func (raw *rawCgroup) setupMemory(c *Cgroup, pid int) (err error) {
137
-	dir, err := raw.join("memory", pid)
138
-	// only return an error for memory if it was not specified
139
-	if err != nil && (c.Memory != 0 || c.MemorySwap != 0) {
140
-		return err
141
-	}
142
-	defer func() {
143
-		if err != nil {
144
-			os.RemoveAll(dir)
145
-		}
146
-	}()
147
-
148
-	if c.Memory != 0 || c.MemorySwap != 0 {
149
-		if c.Memory != 0 {
150
-			if err := writeFile(dir, "memory.limit_in_bytes", strconv.FormatInt(c.Memory, 10)); err != nil {
151
-				return err
152
-			}
153
-			if err := writeFile(dir, "memory.soft_limit_in_bytes", strconv.FormatInt(c.Memory, 10)); err != nil {
154
-				return err
155
-			}
156
-		}
157
-		// By default, MemorySwap is set to twice the size of RAM.
158
-		// If you want to omit MemorySwap, set it to `-1'.
159
-		if c.MemorySwap != -1 {
160
-			if err := writeFile(dir, "memory.memsw.limit_in_bytes", strconv.FormatInt(c.Memory*2, 10)); err != nil {
161
-				return err
162
-			}
163
-		}
164
-	}
165
-	return nil
166
-}
167
-
168
-func (raw *rawCgroup) setupCpu(c *Cgroup, pid int) (err error) {
169
-	// We always want to join the cpu group, to allow fair cpu scheduling
170
-	// on a container basis
171
-	dir, err := raw.join("cpu", pid)
172
-	if err != nil {
173
-		return err
174
-	}
175
-	if c.CpuShares != 0 {
176
-		if err := writeFile(dir, "cpu.shares", strconv.FormatInt(c.CpuShares, 10)); err != nil {
177
-			return err
178
-		}
179
-	}
180
-	return nil
181
-}
182
-
183
-func (raw *rawCgroup) setupCpuset(c *Cgroup, pid int) (err error) {
184
-	// we don't want to join this cgroup unless it is specified
185
-	if c.CpusetCpus != "" {
186
-		dir, err := raw.join("cpuset", pid)
187
-		if err != nil && c.CpusetCpus != "" {
188
-			return err
189
-		}
190
-		defer func() {
191
-			if err != nil {
192
-				os.RemoveAll(dir)
193
-			}
194
-		}()
195
-
196
-		if err := writeFile(dir, "cpuset.cpus", c.CpusetCpus); err != nil {
197
-			return err
198
-		}
199
-	}
200
-	return nil
201
-}
202
-
203
-func (raw *rawCgroup) setupCpuacct(c *Cgroup, pid int) error {
204
-	// we just want to join this group even though we don't set anything
205
-	if _, err := raw.join("cpuacct", pid); err != nil && err != ErrNotFound {
206
-		return err
207
-	}
208
-	return nil
209
-}
210
-
211
-func (raw *rawCgroup) setupBlkio(c *Cgroup, pid int) error {
212
-	// we just want to join this group even though we don't set anything
213
-	if _, err := raw.join("blkio", pid); err != nil && err != ErrNotFound {
214
-		return err
215
-	}
216
-	return nil
217
-}
218
-
219
-func (raw *rawCgroup) setupPerfevent(c *Cgroup, pid int) error {
220
-	// we just want to join this group even though we don't set anything
221
-	if _, err := raw.join("perf_event", pid); err != nil && err != ErrNotFound {
222
-		return err
223
-	}
224
-	return nil
225
-}
226
-
227
-func (raw *rawCgroup) setupFreezer(c *Cgroup, pid int) error {
228
-	// we just want to join this group even though we don't set anything
229
-	if _, err := raw.join("freezer", pid); err != nil && err != ErrNotFound {
230
-		return err
231
-	}
232
-	return nil
233
-}
234
-
235
-func (raw *rawCgroup) Cleanup() error {
236
-	get := func(subsystem string) string {
237
-		path, _ := raw.path(subsystem)
238
-		return path
239
-	}
240
-
241
-	for _, path := range []string{
242
-		get("memory"),
243
-		get("devices"),
244
-		get("cpu"),
245
-		get("cpuset"),
246
-		get("cpuacct"),
247
-		get("blkio"),
248
-		get("perf_event"),
249
-		get("freezer"),
250
-	} {
251
-		if path != "" {
252
-			os.RemoveAll(path)
253
-		}
254
-	}
255
-	return nil
256
-}
257 1
new file mode 100644
... ...
@@ -0,0 +1,263 @@
0
+package fs
1
+
2
+import (
3
+	"fmt"
4
+	"io/ioutil"
5
+	"os"
6
+	"path/filepath"
7
+	"strconv"
8
+
9
+	"github.com/dotcloud/docker/pkg/cgroups"
10
+)
11
+
12
+type rawCgroup struct {
13
+	root   string
14
+	cgroup string
15
+}
16
+
17
+func Apply(c *cgroups.Cgroup, pid int) (cgroups.ActiveCgroup, error) {
18
+	// We have two implementation of cgroups support, one is based on
19
+	// systemd and the dbus api, and one is based on raw cgroup fs operations
20
+	// following the pre-single-writer model docs at:
21
+	// http://www.freedesktop.org/wiki/Software/systemd/PaxControlGroups/
22
+	//
23
+	// we can pick any subsystem to find the root
24
+
25
+	cgroupRoot, err := cgroups.FindCgroupMountpoint("cpu")
26
+	if err != nil {
27
+		return nil, err
28
+	}
29
+	cgroupRoot = filepath.Dir(cgroupRoot)
30
+
31
+	if _, err := os.Stat(cgroupRoot); err != nil {
32
+		return nil, fmt.Errorf("cgroups fs not found")
33
+	}
34
+
35
+	cgroup := c.Name
36
+	if c.Parent != "" {
37
+		cgroup = filepath.Join(c.Parent, cgroup)
38
+	}
39
+
40
+	raw := &rawCgroup{
41
+		root:   cgroupRoot,
42
+		cgroup: cgroup,
43
+	}
44
+	for _, g := range []func(*cgroups.Cgroup, int) error{
45
+		raw.setupDevices,
46
+		raw.setupMemory,
47
+		raw.setupCpu,
48
+		raw.setupCpuset,
49
+		raw.setupCpuacct,
50
+		raw.setupBlkio,
51
+		raw.setupPerfevent,
52
+		raw.setupFreezer,
53
+	} {
54
+		if err := g(c, pid); err != nil {
55
+			return nil, err
56
+		}
57
+	}
58
+
59
+	return raw, nil
60
+}
61
+
62
+func (raw *rawCgroup) path(subsystem string) (string, error) {
63
+	initPath, err := cgroups.GetInitCgroupDir(subsystem)
64
+	if err != nil {
65
+		return "", err
66
+	}
67
+	return filepath.Join(raw.root, subsystem, initPath, raw.cgroup), nil
68
+}
69
+
70
+func (raw *rawCgroup) join(subsystem string, pid int) (string, error) {
71
+	path, err := raw.path(subsystem)
72
+	if err != nil {
73
+		return "", err
74
+	}
75
+	if err := os.MkdirAll(path, 0755); err != nil && !os.IsExist(err) {
76
+		return "", err
77
+	}
78
+	if err := writeFile(path, "cgroup.procs", strconv.Itoa(pid)); err != nil {
79
+		return "", err
80
+	}
81
+	return path, nil
82
+}
83
+
84
+func (raw *rawCgroup) setupDevices(c *cgroups.Cgroup, pid int) (err error) {
85
+	dir, err := raw.join("devices", pid)
86
+	if err != nil {
87
+		return err
88
+	}
89
+	defer func() {
90
+		if err != nil {
91
+			os.RemoveAll(dir)
92
+		}
93
+	}()
94
+
95
+	if !c.DeviceAccess {
96
+
97
+		if err := writeFile(dir, "devices.deny", "a"); err != nil {
98
+			return err
99
+		}
100
+
101
+		allow := []string{
102
+			// allow mknod for any device
103
+			"c *:* m",
104
+			"b *:* m",
105
+
106
+			// /dev/null, zero, full
107
+			"c 1:3 rwm",
108
+			"c 1:5 rwm",
109
+			"c 1:7 rwm",
110
+
111
+			// consoles
112
+			"c 5:1 rwm",
113
+			"c 5:0 rwm",
114
+			"c 4:0 rwm",
115
+			"c 4:1 rwm",
116
+
117
+			// /dev/urandom,/dev/random
118
+			"c 1:9 rwm",
119
+			"c 1:8 rwm",
120
+
121
+			// /dev/pts/ - pts namespaces are "coming soon"
122
+			"c 136:* rwm",
123
+			"c 5:2 rwm",
124
+
125
+			// tuntap
126
+			"c 10:200 rwm",
127
+		}
128
+
129
+		for _, val := range allow {
130
+			if err := writeFile(dir, "devices.allow", val); err != nil {
131
+				return err
132
+			}
133
+		}
134
+	}
135
+	return nil
136
+}
137
+
138
+func (raw *rawCgroup) setupMemory(c *cgroups.Cgroup, pid int) (err error) {
139
+	dir, err := raw.join("memory", pid)
140
+	// only return an error for memory if it was not specified
141
+	if err != nil && (c.Memory != 0 || c.MemorySwap != 0) {
142
+		return err
143
+	}
144
+	defer func() {
145
+		if err != nil {
146
+			os.RemoveAll(dir)
147
+		}
148
+	}()
149
+
150
+	if c.Memory != 0 || c.MemorySwap != 0 {
151
+		if c.Memory != 0 {
152
+			if err := writeFile(dir, "memory.limit_in_bytes", strconv.FormatInt(c.Memory, 10)); err != nil {
153
+				return err
154
+			}
155
+			if err := writeFile(dir, "memory.soft_limit_in_bytes", strconv.FormatInt(c.Memory, 10)); err != nil {
156
+				return err
157
+			}
158
+		}
159
+		// By default, MemorySwap is set to twice the size of RAM.
160
+		// If you want to omit MemorySwap, set it to `-1'.
161
+		if c.MemorySwap != -1 {
162
+			if err := writeFile(dir, "memory.memsw.limit_in_bytes", strconv.FormatInt(c.Memory*2, 10)); err != nil {
163
+				return err
164
+			}
165
+		}
166
+	}
167
+	return nil
168
+}
169
+
170
+func (raw *rawCgroup) setupCpu(c *cgroups.Cgroup, pid int) (err error) {
171
+	// We always want to join the cpu group, to allow fair cpu scheduling
172
+	// on a container basis
173
+	dir, err := raw.join("cpu", pid)
174
+	if err != nil {
175
+		return err
176
+	}
177
+	if c.CpuShares != 0 {
178
+		if err := writeFile(dir, "cpu.shares", strconv.FormatInt(c.CpuShares, 10)); err != nil {
179
+			return err
180
+		}
181
+	}
182
+	return nil
183
+}
184
+
185
+func (raw *rawCgroup) setupCpuset(c *cgroups.Cgroup, pid int) (err error) {
186
+	// we don't want to join this cgroup unless it is specified
187
+	if c.CpusetCpus != "" {
188
+		dir, err := raw.join("cpuset", pid)
189
+		if err != nil && c.CpusetCpus != "" {
190
+			return err
191
+		}
192
+		defer func() {
193
+			if err != nil {
194
+				os.RemoveAll(dir)
195
+			}
196
+		}()
197
+
198
+		if err := writeFile(dir, "cpuset.cpus", c.CpusetCpus); err != nil {
199
+			return err
200
+		}
201
+	}
202
+	return nil
203
+}
204
+
205
+func (raw *rawCgroup) setupCpuacct(c *cgroups.Cgroup, pid int) error {
206
+	// we just want to join this group even though we don't set anything
207
+	if _, err := raw.join("cpuacct", pid); err != nil && err != cgroups.ErrNotFound {
208
+		return err
209
+	}
210
+	return nil
211
+}
212
+
213
+func (raw *rawCgroup) setupBlkio(c *cgroups.Cgroup, pid int) error {
214
+	// we just want to join this group even though we don't set anything
215
+	if _, err := raw.join("blkio", pid); err != nil && err != cgroups.ErrNotFound {
216
+		return err
217
+	}
218
+	return nil
219
+}
220
+
221
+func (raw *rawCgroup) setupPerfevent(c *cgroups.Cgroup, pid int) error {
222
+	// we just want to join this group even though we don't set anything
223
+	if _, err := raw.join("perf_event", pid); err != nil && err != cgroups.ErrNotFound {
224
+		return err
225
+	}
226
+	return nil
227
+}
228
+
229
+func (raw *rawCgroup) setupFreezer(c *cgroups.Cgroup, pid int) error {
230
+	// we just want to join this group even though we don't set anything
231
+	if _, err := raw.join("freezer", pid); err != nil && err != cgroups.ErrNotFound {
232
+		return err
233
+	}
234
+	return nil
235
+}
236
+
237
+func (raw *rawCgroup) Cleanup() error {
238
+	get := func(subsystem string) string {
239
+		path, _ := raw.path(subsystem)
240
+		return path
241
+	}
242
+
243
+	for _, path := range []string{
244
+		get("memory"),
245
+		get("devices"),
246
+		get("cpu"),
247
+		get("cpuset"),
248
+		get("cpuacct"),
249
+		get("blkio"),
250
+		get("perf_event"),
251
+		get("freezer"),
252
+	} {
253
+		if path != "" {
254
+			os.RemoveAll(path)
255
+		}
256
+	}
257
+	return nil
258
+}
259
+
260
+func writeFile(dir, file, data string) error {
261
+	return ioutil.WriteFile(filepath.Join(dir, file), []byte(data), 0700)
262
+}
... ...
@@ -3,9 +3,7 @@ package cgroups
3 3
 import (
4 4
 	"bufio"
5 5
 	"io"
6
-	"io/ioutil"
7 6
 	"os"
8
-	"path/filepath"
9 7
 	"strings"
10 8
 
11 9
 	"github.com/dotcloud/docker/pkg/mount"
... ...
@@ -67,7 +65,3 @@ func parseCgroupFile(subsystem string, r io.Reader) (string, error) {
67 67
 	}
68 68
 	return "", ErrNotFound
69 69
 }
70
-
71
-func writeFile(dir, file, data string) error {
72
-	return ioutil.WriteFile(filepath.Join(dir, file), []byte(data), 0700)
73
-}
... ...
@@ -8,6 +8,7 @@ import (
8 8
 	"syscall"
9 9
 
10 10
 	"github.com/dotcloud/docker/pkg/cgroups"
11
+	"github.com/dotcloud/docker/pkg/cgroups/fs"
11 12
 	"github.com/dotcloud/docker/pkg/cgroups/systemd"
12 13
 	"github.com/dotcloud/docker/pkg/libcontainer"
13 14
 	"github.com/dotcloud/docker/pkg/libcontainer/network"
... ...
@@ -104,7 +105,7 @@ func (ns *linuxNs) SetupCgroups(container *libcontainer.Container, nspid int) (c
104 104
 		if systemd.UseSystemd() {
105 105
 			return systemd.Apply(c, nspid)
106 106
 		}
107
-		return rawApply(c, nspid)
107
+		return fs.Apply(c, nspid)
108 108
 	}
109 109
 	return nil, nil
110 110
 }