| ... | ... |
@@ -124,3 +124,125 @@ func parseCgroupFile(subsystem string, r io.Reader) (string, error) {
|
| 124 | 124 |
func writeFile(dir, file, data string) error {
|
| 125 | 125 |
return ioutil.WriteFile(filepath.Join(dir, file), []byte(data), 0700) |
| 126 | 126 |
} |
| 127 |
+ |
|
| 128 |
+func (c *Cgroup) Apply(pid int) error {
|
|
| 129 |
+ // We have two implementation of cgroups support, one is based on |
|
| 130 |
+ // systemd and the dbus api, and one is based on raw cgroup fs operations |
|
| 131 |
+ // following the pre-single-writer model docs at: |
|
| 132 |
+ // http://www.freedesktop.org/wiki/Software/systemd/PaxControlGroups/ |
|
| 133 |
+ // |
|
| 134 |
+ // we can pick any subsystem to find the root |
|
| 135 |
+ cgroupRoot, err := FindCgroupMountpoint("memory")
|
|
| 136 |
+ if err != nil {
|
|
| 137 |
+ return err |
|
| 138 |
+ } |
|
| 139 |
+ cgroupRoot = filepath.Dir(cgroupRoot) |
|
| 140 |
+ |
|
| 141 |
+ if _, err := os.Stat(cgroupRoot); err != nil {
|
|
| 142 |
+ return fmt.Errorf("cgroups fs not found")
|
|
| 143 |
+ } |
|
| 144 |
+ if err := c.setupDevices(cgroupRoot, pid); err != nil {
|
|
| 145 |
+ return err |
|
| 146 |
+ } |
|
| 147 |
+ if err := c.setupMemory(cgroupRoot, pid); err != nil {
|
|
| 148 |
+ return err |
|
| 149 |
+ } |
|
| 150 |
+ if err := c.setupCpu(cgroupRoot, pid); err != nil {
|
|
| 151 |
+ return err |
|
| 152 |
+ } |
|
| 153 |
+ return nil |
|
| 154 |
+} |
|
| 155 |
+ |
|
| 156 |
+func (c *Cgroup) setupDevices(cgroupRoot string, pid int) (err error) {
|
|
| 157 |
+ if !c.DeviceAccess {
|
|
| 158 |
+ dir, err := c.Join(cgroupRoot, "devices", pid) |
|
| 159 |
+ if err != nil {
|
|
| 160 |
+ return err |
|
| 161 |
+ } |
|
| 162 |
+ |
|
| 163 |
+ defer func() {
|
|
| 164 |
+ if err != nil {
|
|
| 165 |
+ os.RemoveAll(dir) |
|
| 166 |
+ } |
|
| 167 |
+ }() |
|
| 168 |
+ |
|
| 169 |
+ if err := writeFile(dir, "devices.deny", "a"); err != nil {
|
|
| 170 |
+ return err |
|
| 171 |
+ } |
|
| 172 |
+ |
|
| 173 |
+ allow := []string{
|
|
| 174 |
+ // /dev/null, zero, full |
|
| 175 |
+ "c 1:3 rwm", |
|
| 176 |
+ "c 1:5 rwm", |
|
| 177 |
+ "c 1:7 rwm", |
|
| 178 |
+ |
|
| 179 |
+ // consoles |
|
| 180 |
+ "c 5:1 rwm", |
|
| 181 |
+ "c 5:0 rwm", |
|
| 182 |
+ "c 4:0 rwm", |
|
| 183 |
+ "c 4:1 rwm", |
|
| 184 |
+ |
|
| 185 |
+ // /dev/urandom,/dev/random |
|
| 186 |
+ "c 1:9 rwm", |
|
| 187 |
+ "c 1:8 rwm", |
|
| 188 |
+ |
|
| 189 |
+ // /dev/pts/ - pts namespaces are "coming soon" |
|
| 190 |
+ "c 136:* rwm", |
|
| 191 |
+ "c 5:2 rwm", |
|
| 192 |
+ |
|
| 193 |
+ // tuntap |
|
| 194 |
+ "c 10:200 rwm", |
|
| 195 |
+ } |
|
| 196 |
+ |
|
| 197 |
+ for _, val := range allow {
|
|
| 198 |
+ if err := writeFile(dir, "devices.allow", val); err != nil {
|
|
| 199 |
+ return err |
|
| 200 |
+ } |
|
| 201 |
+ } |
|
| 202 |
+ } |
|
| 203 |
+ return nil |
|
| 204 |
+} |
|
| 205 |
+ |
|
| 206 |
+func (c *Cgroup) setupMemory(cgroupRoot string, pid int) (err error) {
|
|
| 207 |
+ if c.Memory != 0 || c.MemorySwap != 0 {
|
|
| 208 |
+ dir, err := c.Join(cgroupRoot, "memory", pid) |
|
| 209 |
+ if err != nil {
|
|
| 210 |
+ return err |
|
| 211 |
+ } |
|
| 212 |
+ defer func() {
|
|
| 213 |
+ if err != nil {
|
|
| 214 |
+ os.RemoveAll(dir) |
|
| 215 |
+ } |
|
| 216 |
+ }() |
|
| 217 |
+ |
|
| 218 |
+ if c.Memory != 0 {
|
|
| 219 |
+ if err := writeFile(dir, "memory.limit_in_bytes", strconv.FormatInt(c.Memory, 10)); err != nil {
|
|
| 220 |
+ return err |
|
| 221 |
+ } |
|
| 222 |
+ if err := writeFile(dir, "memory.soft_limit_in_bytes", strconv.FormatInt(c.Memory, 10)); err != nil {
|
|
| 223 |
+ return err |
|
| 224 |
+ } |
|
| 225 |
+ } |
|
| 226 |
+ if c.MemorySwap != 0 {
|
|
| 227 |
+ if err := writeFile(dir, "memory.memsw.limit_in_bytes", strconv.FormatInt(c.MemorySwap, 10)); err != nil {
|
|
| 228 |
+ return err |
|
| 229 |
+ } |
|
| 230 |
+ } |
|
| 231 |
+ } |
|
| 232 |
+ return nil |
|
| 233 |
+} |
|
| 234 |
+ |
|
| 235 |
+func (c *Cgroup) setupCpu(cgroupRoot string, pid int) (err error) {
|
|
| 236 |
+ // We always want to join the cpu group, to allow fair cpu scheduling |
|
| 237 |
+ // on a container basis |
|
| 238 |
+ dir, err := c.Join(cgroupRoot, "cpu", pid) |
|
| 239 |
+ if err != nil {
|
|
| 240 |
+ return err |
|
| 241 |
+ } |
|
| 242 |
+ if c.CpuShares != 0 {
|
|
| 243 |
+ if err := writeFile(dir, "cpu.shares", strconv.FormatInt(c.CpuShares, 10)); err != nil {
|
|
| 244 |
+ return err |
|
| 245 |
+ } |
|
| 246 |
+ } |
|
| 247 |
+ return nil |
|
| 248 |
+} |
| 127 | 249 |
deleted file mode 100644 |
| ... | ... |
@@ -1,140 +0,0 @@ |
| 1 |
-package cgroup |
|
| 2 |
- |
|
| 3 |
-import ( |
|
| 4 |
- "fmt" |
|
| 5 |
- "github.com/dotcloud/docker/pkg/cgroups" |
|
| 6 |
- "github.com/dotcloud/docker/pkg/libcontainer" |
|
| 7 |
- "io/ioutil" |
|
| 8 |
- "os" |
|
| 9 |
- "path/filepath" |
|
| 10 |
- "strconv" |
|
| 11 |
-) |
|
| 12 |
- |
|
| 13 |
-func ApplyCgroup(container *libcontainer.Container, pid int) (err error) {
|
|
| 14 |
- if container.Cgroups == nil {
|
|
| 15 |
- return nil |
|
| 16 |
- } |
|
| 17 |
- |
|
| 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 |
- cgroupRoot, err := cgroups.FindCgroupMountpoint("memory")
|
|
| 25 |
- if err != nil {
|
|
| 26 |
- return err |
|
| 27 |
- } |
|
| 28 |
- cgroupRoot = filepath.Dir(cgroupRoot) |
|
| 29 |
- if _, err := os.Stat(cgroupRoot); err != nil {
|
|
| 30 |
- return fmt.Errorf("cgroups fs not found")
|
|
| 31 |
- } |
|
| 32 |
- if err := setupDevices(container, cgroupRoot, pid); err != nil {
|
|
| 33 |
- return err |
|
| 34 |
- } |
|
| 35 |
- if err := setupMemory(container, cgroupRoot, pid); err != nil {
|
|
| 36 |
- return err |
|
| 37 |
- } |
|
| 38 |
- if err := setupCpu(container, cgroupRoot, pid); err != nil {
|
|
| 39 |
- return err |
|
| 40 |
- } |
|
| 41 |
- return nil |
|
| 42 |
-} |
|
| 43 |
- |
|
| 44 |
-func setupDevices(container *libcontainer.Container, cgroupRoot string, pid int) (err error) {
|
|
| 45 |
- if !container.Cgroups.DeviceAccess {
|
|
| 46 |
- dir, err := container.Cgroups.Join(cgroupRoot, "devices", pid) |
|
| 47 |
- if err != nil {
|
|
| 48 |
- return err |
|
| 49 |
- } |
|
| 50 |
- |
|
| 51 |
- defer func() {
|
|
| 52 |
- if err != nil {
|
|
| 53 |
- os.RemoveAll(dir) |
|
| 54 |
- } |
|
| 55 |
- }() |
|
| 56 |
- |
|
| 57 |
- if err := writeFile(dir, "devices.deny", "a"); err != nil {
|
|
| 58 |
- return err |
|
| 59 |
- } |
|
| 60 |
- |
|
| 61 |
- allow := []string{
|
|
| 62 |
- // /dev/null, zero, full |
|
| 63 |
- "c 1:3 rwm", |
|
| 64 |
- "c 1:5 rwm", |
|
| 65 |
- "c 1:7 rwm", |
|
| 66 |
- |
|
| 67 |
- // consoles |
|
| 68 |
- "c 5:1 rwm", |
|
| 69 |
- "c 5:0 rwm", |
|
| 70 |
- "c 4:0 rwm", |
|
| 71 |
- "c 4:1 rwm", |
|
| 72 |
- |
|
| 73 |
- // /dev/urandom,/dev/random |
|
| 74 |
- "c 1:9 rwm", |
|
| 75 |
- "c 1:8 rwm", |
|
| 76 |
- |
|
| 77 |
- // /dev/pts/ - pts namespaces are "coming soon" |
|
| 78 |
- "c 136:* rwm", |
|
| 79 |
- "c 5:2 rwm", |
|
| 80 |
- |
|
| 81 |
- // tuntap |
|
| 82 |
- "c 10:200 rwm", |
|
| 83 |
- } |
|
| 84 |
- |
|
| 85 |
- for _, val := range allow {
|
|
| 86 |
- if err := writeFile(dir, "devices.allow", val); err != nil {
|
|
| 87 |
- return err |
|
| 88 |
- } |
|
| 89 |
- } |
|
| 90 |
- } |
|
| 91 |
- return nil |
|
| 92 |
-} |
|
| 93 |
- |
|
| 94 |
-func setupMemory(container *libcontainer.Container, cgroupRoot string, pid int) (err error) {
|
|
| 95 |
- if container.Cgroups.Memory != 0 || container.Cgroups.MemorySwap != 0 {
|
|
| 96 |
- dir, err := container.Cgroups.Join(cgroupRoot, "memory", pid) |
|
| 97 |
- if err != nil {
|
|
| 98 |
- return err |
|
| 99 |
- } |
|
| 100 |
- defer func() {
|
|
| 101 |
- if err != nil {
|
|
| 102 |
- os.RemoveAll(dir) |
|
| 103 |
- } |
|
| 104 |
- }() |
|
| 105 |
- |
|
| 106 |
- if container.Cgroups.Memory != 0 {
|
|
| 107 |
- if err := writeFile(dir, "memory.limit_in_bytes", strconv.FormatInt(container.Cgroups.Memory, 10)); err != nil {
|
|
| 108 |
- return err |
|
| 109 |
- } |
|
| 110 |
- if err := writeFile(dir, "memory.soft_limit_in_bytes", strconv.FormatInt(container.Cgroups.Memory, 10)); err != nil {
|
|
| 111 |
- return err |
|
| 112 |
- } |
|
| 113 |
- } |
|
| 114 |
- if container.Cgroups.MemorySwap != 0 {
|
|
| 115 |
- if err := writeFile(dir, "memory.memsw.limit_in_bytes", strconv.FormatInt(container.Cgroups.MemorySwap, 10)); err != nil {
|
|
| 116 |
- return err |
|
| 117 |
- } |
|
| 118 |
- } |
|
| 119 |
- } |
|
| 120 |
- return nil |
|
| 121 |
-} |
|
| 122 |
- |
|
| 123 |
-func setupCpu(container *libcontainer.Container, cgroupRoot string, pid int) (err error) {
|
|
| 124 |
- // We always want to join the cpu group, to allow fair cpu scheduling |
|
| 125 |
- // on a container basis |
|
| 126 |
- dir, err := container.Cgroups.Join(cgroupRoot, "cpu", pid) |
|
| 127 |
- if err != nil {
|
|
| 128 |
- return err |
|
| 129 |
- } |
|
| 130 |
- if container.Cgroups.CpuShares != 0 {
|
|
| 131 |
- if err := writeFile(dir, "cpu.shares", strconv.FormatInt(container.Cgroups.CpuShares, 10)); err != nil {
|
|
| 132 |
- return err |
|
| 133 |
- } |
|
| 134 |
- } |
|
| 135 |
- return nil |
|
| 136 |
-} |
|
| 137 |
- |
|
| 138 |
-func writeFile(dir, file, data string) error {
|
|
| 139 |
- return ioutil.WriteFile(filepath.Join(dir, file), []byte(data), 0700) |
|
| 140 |
-} |
| ... | ... |
@@ -5,7 +5,6 @@ package main |
| 5 | 5 |
import ( |
| 6 | 6 |
"fmt" |
| 7 | 7 |
"github.com/dotcloud/docker/pkg/libcontainer" |
| 8 |
- "github.com/dotcloud/docker/pkg/libcontainer/cgroup" |
|
| 9 | 8 |
"github.com/dotcloud/docker/pkg/libcontainer/network" |
| 10 | 9 |
"github.com/dotcloud/docker/pkg/libcontainer/utils" |
| 11 | 10 |
"github.com/dotcloud/docker/pkg/system" |
| ... | ... |
@@ -41,9 +40,11 @@ func execCommand(container *libcontainer.Container, args []string) (int, error) |
| 41 | 41 |
|
| 42 | 42 |
// Do this before syncing with child so that no children |
| 43 | 43 |
// can escape the cgroup |
| 44 |
- if err := cgroup.ApplyCgroup(container, command.Process.Pid); err != nil {
|
|
| 45 |
- command.Process.Kill() |
|
| 46 |
- return -1, err |
|
| 44 |
+ if container.Cgroups != nil {
|
|
| 45 |
+ if err := container.Cgroups.Apply(command.Process.Pid); err != nil {
|
|
| 46 |
+ command.Process.Kill() |
|
| 47 |
+ return -1, err |
|
| 48 |
+ } |
|
| 47 | 49 |
} |
| 48 | 50 |
|
| 49 | 51 |
if container.Network != nil {
|