Browse code

Update libcontainer dep to v1.0.1 Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@docker.com> (github: crosbymichael)

Michael Crosby authored on 2014/06/20 07:23:19
Showing 11 changed files
... ...
@@ -61,4 +61,4 @@ mv tmp-tar src/code.google.com/p/go/src/pkg/archive/tar
61 61
 
62 62
 clone git github.com/godbus/dbus v1
63 63
 clone git github.com/coreos/go-systemd v2
64
-clone git github.com/docker/libcontainer 77ffd49dfedbc78a7cd4cb7a50c7446cf118725f
64
+clone git github.com/docker/libcontainer v1.0.1
... ...
@@ -9,3 +9,4 @@ install:
9 9
 script:
10 10
     - bash "$DOCKER_PATH/hack/make/validate-dco"
11 11
     - bash "$DOCKER_PATH/hack/make/validate-gofmt"
12
+    - go test
... ...
@@ -36,7 +36,7 @@ that feature *on top of* libcontainer.
36 36
 ### Discuss your design on the mailing list
37 37
 
38 38
 We recommend discussing your plans [on the mailing
39
-list](https://groups.google.com/forum/?fromgroups#!forum/docker-dev)
39
+list](https://groups.google.com/forum/?fromgroups#!forum/libcontainer)
40 40
 before starting to code - especially for more ambitious contributions.
41 41
 This gives other contributors a chance to point you in the right
42 42
 direction, give feedback on your design, and maybe point out if someone
... ...
@@ -112,12 +112,12 @@ and that all the tests pass.
112 112
 libcontainer maintainers use LGTM (looks good to me) in comments on the code review
113 113
 to indicate acceptance.
114 114
 
115
-A change requires LGTMs from at lease one maintainer of each
116
-component affected. For example, if a change affects `netlink/` and `security/`, it
117
-needs at least one LGTM from the maintainers of `netlink/` AND, separately, at
118
-least one LGTM from the maintainers of `security/`.
115
+A change requires LGTMs from at lease two maintainers. One of those must come from
116
+a maintainer of the component affected. For example, if a change affects `netlink/`
117
+and `security`, it needs at least one LGTM from a maintainer of each. Maintainers
118
+only need one LGTM as presumably they LGTM their own change.
119 119
 
120
-For more details see [MAINTAINERS.md](hack/MAINTAINERS.md)
120
+For more details see [MAINTAINERS.md](MAINTAINERS.md)
121 121
 
122 122
 ### Sign your work
123 123
 
... ...
@@ -63,7 +63,7 @@ All decisions are pull requests, and the relevant maintainers make
63 63
 decisions by accepting or refusing the pull request. Review and acceptance
64 64
 by anyone is denoted by adding a comment in the pull request: `LGTM`. 
65 65
 However, only currently listed `MAINTAINERS` are counted towards the required
66
-majority.
66
+two LGTMs.
67 67
 
68 68
 libcontainer follows the timeless, highly efficient and totally unfair system
69 69
 known as [Benevolent dictator for life](http://en.wikipedia.org/wiki/Benevolent_Dictator_for_Life), with Michael Crosby in the role of BDFL.
... ...
@@ -84,6 +84,7 @@ func (s *cpuacctGroup) GetStats(d *data, stats *cgroups.Stats) error {
84 84
 	if err != nil {
85 85
 		return err
86 86
 	}
87
+	stats.CpuStats.CpuUsage.TotalUsage = lastUsage
87 88
 	stats.CpuStats.CpuUsage.PercpuUsage = percpuUsage
88 89
 	stats.CpuStats.CpuUsage.UsageInKernelmode = (kernelModeUsage * nanosecondsInSecond) / clockTicks
89 90
 	stats.CpuStats.CpuUsage.UsageInUsermode = (userModeUsage * nanosecondsInSecond) / clockTicks
... ...
@@ -13,8 +13,10 @@ type CpuUsage struct {
13 13
 	// percentage of available CPUs currently being used.
14 14
 	PercentUsage uint64 `json:"percent_usage,omitempty"`
15 15
 	// nanoseconds of cpu time consumed over the last 100 ms.
16
-	CurrentUsage uint64   `json:"current_usage,omitempty"`
17
-	PercpuUsage  []uint64 `json:"percpu_usage,omitempty"`
16
+	CurrentUsage uint64 `json:"current_usage,omitempty"`
17
+	// total nanoseconds of cpu time consumed
18
+	TotalUsage  uint64   `json:"total_usage,omitempty"`
19
+	PercpuUsage []uint64 `json:"percpu_usage,omitempty"`
18 20
 	// Time spent by tasks of the cgroup in kernel mode. Units: nanoseconds.
19 21
 	UsageInKernelmode uint64 `json:"usage_in_kernelmode"`
20 22
 	// Time spent by tasks of the cgroup in user mode. Units: nanoseconds.
... ...
@@ -1,107 +1,213 @@
1 1
 {
2
-  "namespaces": {
3
-    "NEWNET": true,
4
-    "NEWPID": true,
5
-    "NEWIPC": true,
6
-    "NEWUTS": true,
7
-    "NEWNS": true
8
-  },
9
-  "networks": [
10
-    {
11
-      "gateway": "localhost",
12
-      "type": "loopback",
13
-      "address": "127.0.0.1/0",
14
-      "mtu": 1500
2
+    "capabilities": [
3
+        "CHOWN",
4
+        "DAC_OVERRIDE",
5
+        "FOWNER",
6
+        "MKNOD",
7
+        "NET_RAW",
8
+        "SETGID",
9
+        "SETUID",
10
+        "SETFCAP",
11
+        "SETPCAP",
12
+        "NET_BIND_SERVICE",
13
+        "SYS_CHROOT",
14
+        "KILL"
15
+    ],
16
+    "cgroups": {
17
+        "allowed_devices": [
18
+            {
19
+                "cgroup_permissions": "m",
20
+                "major_number": -1,
21
+                "minor_number": -1,
22
+                "type": 99
23
+            },
24
+            {
25
+                "cgroup_permissions": "m",
26
+                "major_number": -1,
27
+                "minor_number": -1,
28
+                "type": 98
29
+            },
30
+            {
31
+                "cgroup_permissions": "rwm",
32
+                "major_number": 5,
33
+                "minor_number": 1,
34
+                "path": "/dev/console",
35
+                "type": 99
36
+            },
37
+            {
38
+                "cgroup_permissions": "rwm",
39
+                "major_number": 4,
40
+                "path": "/dev/tty0",
41
+                "type": 99
42
+            },
43
+            {
44
+                "cgroup_permissions": "rwm",
45
+                "major_number": 4,
46
+                "minor_number": 1,
47
+                "path": "/dev/tty1",
48
+                "type": 99
49
+            },
50
+            {
51
+                "cgroup_permissions": "rwm",
52
+                "major_number": 136,
53
+                "minor_number": -1,
54
+                "type": 99
55
+            },
56
+            {
57
+                "cgroup_permissions": "rwm",
58
+                "major_number": 5,
59
+                "minor_number": 2,
60
+                "type": 99
61
+            },
62
+            {
63
+                "cgroup_permissions": "rwm",
64
+                "major_number": 10,
65
+                "minor_number": 200,
66
+                "type": 99
67
+            },
68
+            {
69
+                "cgroup_permissions": "rwm",
70
+                "file_mode": 438,
71
+                "major_number": 1,
72
+                "minor_number": 3,
73
+                "path": "/dev/null",
74
+                "type": 99
75
+            },
76
+            {
77
+                "cgroup_permissions": "rwm",
78
+                "file_mode": 438,
79
+                "major_number": 1,
80
+                "minor_number": 5,
81
+                "path": "/dev/zero",
82
+                "type": 99
83
+            },
84
+            {
85
+                "cgroup_permissions": "rwm",
86
+                "file_mode": 438,
87
+                "major_number": 1,
88
+                "minor_number": 7,
89
+                "path": "/dev/full",
90
+                "type": 99
91
+            },
92
+            {
93
+                "cgroup_permissions": "rwm",
94
+                "file_mode": 438,
95
+                "major_number": 5,
96
+                "path": "/dev/tty",
97
+                "type": 99
98
+            },
99
+            {
100
+                "cgroup_permissions": "rwm",
101
+                "file_mode": 438,
102
+                "major_number": 1,
103
+                "minor_number": 9,
104
+                "path": "/dev/urandom",
105
+                "type": 99
106
+            },
107
+            {
108
+                "cgroup_permissions": "rwm",
109
+                "file_mode": 438,
110
+                "major_number": 1,
111
+                "minor_number": 8,
112
+                "path": "/dev/random",
113
+                "type": 99
114
+            }
115
+        ],
116
+        "name": "docker-koye",
117
+        "parent": "docker"
15 118
     },
16
-    {
17
-      "gateway": "172.17.42.1",
18
-      "context": {
19
-        "prefix": "veth",
20
-        "bridge": "docker0"
21
-      },
22
-      "type": "veth",
23
-      "address": "172.17.42.2/16",
24
-      "mtu": 1500
25
-    }
26
-  ],
27
-  "routes": [
28
-    {
29
-      "gateway": "172.17.42.1",
30
-      "interface_name": "eth0"
119
+    "context": {
120
+        "mount_label": "",
121
+        "process_label": "",
122
+        "restrictions": "true"
31 123
     },
32
-    {
33
-      "destination": "192.168.0.0/24",
34
-      "interface_name": "eth0"
35
-    }
36
-  ],
37
-  "capabilities": [
38
-    "MKNOD"
39
-  ],
40
-  "cgroups": {
41
-    "name": "docker-koye",
42
-    "parent": "docker"
43
-  },
44
-  "hostname": "koye",
45
-  "environment": [
46
-    "HOME=/",
47
-    "PATH=PATH=$PATH:/bin:/usr/bin:/sbin:/usr/sbin",
48
-    "container=docker",
49
-    "TERM=xterm-256color"
50
-  ],
51
-  "tty": true,
52
-  "mounts": [
53
-    {
54
-      "type": "devtmpfs"
55
-    }
56
-  ],
57
-  "device_nodes": [
58
-		{
59
-			"path":  "/dev/null",
60
-			"type":        99,
61
-			"major_number": 1,
62
-			"minor_number": 3,
63
-			"cgroup_permissions": "rwm",
64
-			"file_mode": 438
65
-		},
66
-		{
67
-			"path":  "/dev/zero",
68
-			"type":        99,
69
-			"major_number": 1,
70
-			"minor_number": 5,
71
-			"cgroup_permissions": "rwm",
72
-			"file_mode": 438
73
-		},
74
-		{
75
-			"path":  "/dev/full",
76
-			"type":        99,
77
-			"major_number": 1,
78
-			"minor_number": 7,
79
-			"cgroup_permissions": "rwm",
80
-			"file_mode": 438
81
-		},
82
-		{
83
-			"path":  "/dev/tty",
84
-			"type":        99,
85
-			"major_number": 5,
86
-			"minor_number": 0,
87
-			"cgroup_permissions": "rwm",
88
-			"file_mode": 438
89
-		},
90
-		{
91
-			"path":  "/dev/urandom",
92
-			"type":        99,
93
-			"major_number": 1,
94
-			"minor_number": 9,
95
-			"cgroup_permissions": "rwm",
96
-			"file_mode": 438
97
-		},
98
-		{
99
-			"path":  "/dev/random",
100
-			"type":        99,
101
-			"major_number": 1,
102
-			"minor_number": 8,
103
-			"cgroup_permissions": "rwm",
104
-			"file_mode": 438
105
-		}
106
-  ]
124
+    "device_nodes": [
125
+        {
126
+            "cgroup_permissions": "rwm",
127
+            "major_number": 10,
128
+            "minor_number": 229,
129
+            "path": "/dev/fuse",
130
+            "type": 99
131
+        },
132
+        {
133
+            "cgroup_permissions": "rwm",
134
+            "file_mode": 438,
135
+            "major_number": 1,
136
+            "minor_number": 3,
137
+            "path": "/dev/null",
138
+            "type": 99
139
+        },
140
+        {
141
+            "cgroup_permissions": "rwm",
142
+            "file_mode": 438,
143
+            "major_number": 1,
144
+            "minor_number": 5,
145
+            "path": "/dev/zero",
146
+            "type": 99
147
+        },
148
+        {
149
+            "cgroup_permissions": "rwm",
150
+            "file_mode": 438,
151
+            "major_number": 1,
152
+            "minor_number": 7,
153
+            "path": "/dev/full",
154
+            "type": 99
155
+        },
156
+        {
157
+            "cgroup_permissions": "rwm",
158
+            "file_mode": 438,
159
+            "major_number": 5,
160
+            "path": "/dev/tty",
161
+            "type": 99
162
+        },
163
+        {
164
+            "cgroup_permissions": "rwm",
165
+            "file_mode": 438,
166
+            "major_number": 1,
167
+            "minor_number": 9,
168
+            "path": "/dev/urandom",
169
+            "type": 99
170
+        },
171
+        {
172
+            "cgroup_permissions": "rwm",
173
+            "file_mode": 438,
174
+            "major_number": 1,
175
+            "minor_number": 8,
176
+            "path": "/dev/random",
177
+            "type": 99
178
+        }
179
+    ],
180
+    "environment": [
181
+        "HOME=/",
182
+        "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
183
+        "HOSTNAME=2d388ea3bd92",
184
+        "TERM=xterm"
185
+    ],
186
+    "hostname": "koye",
187
+    "namespaces": {
188
+        "NEWIPC": true,
189
+        "NEWNET": true,
190
+        "NEWNS": true,
191
+        "NEWPID": true,
192
+        "NEWUTS": true
193
+    },
194
+    "networks": [
195
+        {
196
+            "address": "127.0.0.1/0",
197
+            "gateway": "localhost",
198
+            "mtu": 1500,
199
+            "type": "loopback"
200
+        },
201
+        {
202
+            "address": "172.17.0.101/16",
203
+            "context": {
204
+                "bridge": "docker0",
205
+                "prefix": "veth"
206
+            },
207
+            "gateway": "172.17.42.1",
208
+            "mtu": 1500,
209
+            "type": "veth"
210
+        }
211
+    ],
212
+    "tty": true
107 213
 }
... ...
@@ -37,11 +37,6 @@ func TestContainerJsonFormat(t *testing.T) {
37 37
 		t.Fail()
38 38
 	}
39 39
 
40
-	if len(container.Routes) != 2 {
41
-		t.Log("should have found 2 routes")
42
-		t.Fail()
43
-	}
44
-
45 40
 	if !container.Namespaces["NEWNET"] {
46 41
 		t.Log("namespaces should contain NEWNET")
47 42
 		t.Fail()
... ...
@@ -62,8 +57,8 @@ func TestContainerJsonFormat(t *testing.T) {
62 62
 		t.Fail()
63 63
 	}
64 64
 
65
-	if contains("SYS_CHROOT", container.Capabilities) {
66
-		t.Log("capabilities mask should not contain SYS_CHROOT")
65
+	if !contains("SYS_CHROOT", container.Capabilities) {
66
+		t.Log("capabilities mask should contain SYS_CHROOT")
67 67
 		t.Fail()
68 68
 	}
69 69
 }
... ...
@@ -21,14 +21,8 @@ func ExecIn(container *libcontainer.Container, nspid int, args []string) error {
21 21
 		return err
22 22
 	}
23 23
 
24
-	// TODO(vmarmol): Move this to the container JSON.
25
-	processLabel, err := label.GetPidCon(nspid)
26
-	if err != nil {
27
-		return err
28
-	}
29
-
30 24
 	// Enter the namespace and then finish setup
31
-	finalArgs := []string{os.Args[0], "nsenter", strconv.Itoa(nspid), processLabel, string(containerJson)}
25
+	finalArgs := []string{os.Args[0], "nsenter", "--nspid", strconv.Itoa(nspid), "--containerjson", string(containerJson), "--"}
32 26
 	finalArgs = append(finalArgs, args...)
33 27
 	if err := system.Execv(finalArgs[0], finalArgs[0:], os.Environ()); err != nil {
34 28
 		return err
... ...
@@ -37,7 +31,7 @@ func ExecIn(container *libcontainer.Container, nspid int, args []string) error {
37 37
 }
38 38
 
39 39
 // NsEnter is run after entering the namespace.
40
-func NsEnter(container *libcontainer.Container, processLabel string, nspid int, args []string) error {
40
+func NsEnter(container *libcontainer.Container, nspid int, args []string) error {
41 41
 	// clear the current processes env and replace it with the environment
42 42
 	// defined on the container
43 43
 	if err := LoadContainerEnvironment(container); err != nil {
... ...
@@ -46,9 +40,13 @@ func NsEnter(container *libcontainer.Container, processLabel string, nspid int,
46 46
 	if err := FinalizeNamespace(container); err != nil {
47 47
 		return err
48 48
 	}
49
-	if err := label.SetProcessLabel(processLabel); err != nil {
50
-		return err
49
+
50
+	if process_label, ok := container.Context["process_label"]; ok {
51
+		if err := label.SetProcessLabel(process_label); err != nil {
52
+			return err
53
+		}
51 54
 	}
55
+
52 56
 	if err := system.Execv(args[0], args[0:], container.Env); err != nil {
53 57
 		return err
54 58
 	}
... ...
@@ -15,6 +15,7 @@ package namespaces
15 15
 #include <sys/stat.h>
16 16
 #include <sys/types.h>
17 17
 #include <unistd.h>
18
+#include <getopt.h>
18 19
 
19 20
 static const kBufSize = 256;
20 21
 
... ...
@@ -64,6 +65,10 @@ int setns(int fd, int nstype) {
64 64
 #endif
65 65
 #endif
66 66
 
67
+void print_usage() {
68
+	fprintf(stderr, "<binary> nsenter --nspid <pid> --containerjson <container_json> -- cmd1 arg1 arg2...\n");
69
+}
70
+
67 71
 void nsenter() {
68 72
 	int argc;
69 73
 	char **argv;
... ...
@@ -79,11 +84,40 @@ void nsenter() {
79 79
 		fprintf(stderr, "nsenter: Incorrect usage, not enough arguments\n");
80 80
 		exit(1);
81 81
 	}
82
-	pid_t init_pid = strtol(argv[2], NULL, 10);
82
+
83
+	static const struct option longopts[] = {
84
+		{ "nspid",         required_argument, NULL, 'n' },
85
+		{ "containerjson", required_argument, NULL, 'c' },
86
+		{ NULL,            0,                 NULL,  0  }
87
+	};
88
+
89
+	int c;
90
+	pid_t init_pid = -1;
91
+	char *init_pid_str = NULL;
92
+	char *container_json = NULL;
93
+	while ((c = getopt_long_only(argc, argv, "n:s:c:", longopts, NULL)) != -1) {
94
+		switch (c) {
95
+		case 'n':
96
+			init_pid_str = optarg;
97
+			break;
98
+		case 'c':
99
+			container_json = optarg;
100
+			break;
101
+		}
102
+	}
103
+
104
+	if (container_json == NULL || init_pid_str == NULL) {
105
+		print_usage();
106
+		exit(1);
107
+	}
108
+
109
+	init_pid = strtol(init_pid_str, NULL, 10);
83 110
 	if (errno != 0 || init_pid <= 0) {
84
-		fprintf(stderr, "nsenter: Failed to parse PID from \"%s\" with error: \"%s\"\n", argv[2], strerror(errno));
111
+		fprintf(stderr, "nsenter: Failed to parse PID from \"%s\" with error: \"%s\"\n", init_pid_str, strerror(errno));
112
+		print_usage();
85 113
 		exit(1);
86 114
 	}
115
+
87 116
 	argc -= 3;
88 117
 	argv += 3;
89 118
 
... ...
@@ -2,7 +2,6 @@ package main
2 2
 
3 3
 import (
4 4
 	"log"
5
-	"strconv"
6 5
 
7 6
 	"github.com/codegangsta/cli"
8 7
 	"github.com/docker/libcontainer/namespaces"
... ...
@@ -12,29 +11,30 @@ var nsenterCommand = cli.Command{
12 12
 	Name:   "nsenter",
13 13
 	Usage:  "init process for entering an existing namespace",
14 14
 	Action: nsenterAction,
15
+	Flags: []cli.Flag{
16
+		cli.IntFlag{Name: "nspid"},
17
+		cli.StringFlag{Name: "containerjson"},
18
+	},
15 19
 }
16 20
 
17 21
 func nsenterAction(context *cli.Context) {
18 22
 	args := context.Args()
19
-	if len(args) < 4 {
20
-		log.Fatalf("incorrect usage: <pid> <process label> <container JSON> <cmd>...")
21
-	}
22 23
 
23
-	container, err := loadContainerFromJson(args.Get(2))
24
-	if err != nil {
25
-		log.Fatalf("unable to load container: %s", err)
24
+	if len(args) == 0 {
25
+		args = []string{"/bin/bash"}
26 26
 	}
27 27
 
28
-	nspid, err := strconv.Atoi(args.Get(0))
28
+	container, err := loadContainerFromJson(context.String("containerjson"))
29 29
 	if err != nil {
30
-		log.Fatalf("unable to read pid: %s from %q", err, args.Get(0))
30
+		log.Fatalf("unable to load container: %s", err)
31 31
 	}
32 32
 
33
+	nspid := context.Int("nspid")
33 34
 	if nspid <= 0 {
34 35
 		log.Fatalf("cannot enter into namespaces without valid pid: %q", nspid)
35 36
 	}
36 37
 
37
-	if err := namespaces.NsEnter(container, args.Get(1), nspid, args[3:]); err != nil {
38
+	if err := namespaces.NsEnter(container, nspid, args); err != nil {
38 39
 		log.Fatalf("failed to nsenter: %s", err)
39 40
 	}
40 41
 }