Browse code

Add chroot driver for testing

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

Michael Crosby authored on 2014/01/11 13:22:39
Showing 6 changed files
... ...
@@ -678,18 +678,19 @@ func (container *Container) Start() (err error) {
678 678
 	}
679 679
 
680 680
 	container.process = &execdriver.Process{
681
-		ID:         container.ID,
682
-		Privileged: container.hostConfig.Privileged,
683
-		Rootfs:     root,
684
-		InitPath:   "/.dockerinit",
685
-		Entrypoint: container.Path,
686
-		Arguments:  container.Args,
687
-		WorkingDir: workingDir,
688
-		ConfigPath: container.lxcConfigPath(),
689
-		Network:    en,
690
-		Tty:        container.Config.Tty,
691
-		User:       container.Config.User,
692
-		WaitLock:   make(chan struct{}),
681
+		ID:          container.ID,
682
+		Privileged:  container.hostConfig.Privileged,
683
+		Rootfs:      root,
684
+		InitPath:    "/.dockerinit",
685
+		Entrypoint:  container.Path,
686
+		Arguments:   container.Args,
687
+		WorkingDir:  workingDir,
688
+		ConfigPath:  container.lxcConfigPath(),
689
+		Network:     en,
690
+		Tty:         container.Config.Tty,
691
+		User:        container.Config.User,
692
+		WaitLock:    make(chan struct{}),
693
+		SysInitPath: runtime.sysInitPath,
693 694
 	}
694 695
 	container.process.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
695 696
 
696 697
new file mode 100644
... ...
@@ -0,0 +1,66 @@
0
+package chroot
1
+
2
+import (
3
+	"fmt"
4
+	"github.com/dotcloud/docker/execdriver"
5
+	"io/ioutil"
6
+	"os/exec"
7
+	"path"
8
+	"time"
9
+)
10
+
11
+type driver struct {
12
+}
13
+
14
+func NewDriver() (execdriver.Driver, error) {
15
+	return &driver{}, nil
16
+}
17
+
18
+func (d *driver) Start(c *execdriver.Process) error {
19
+	data, _ := ioutil.ReadFile(c.SysInitPath)
20
+	ioutil.WriteFile(path.Join(c.Rootfs, ".dockerinit"), data, 0644)
21
+	params := []string{
22
+		"chroot",
23
+		c.Rootfs,
24
+		"/.dockerinit",
25
+	}
26
+	// need to mount proc
27
+	params = append(params, c.Entrypoint)
28
+	params = append(params, c.Arguments...)
29
+
30
+	var (
31
+		name = params[0]
32
+		arg  = params[1:]
33
+	)
34
+	aname, err := exec.LookPath(name)
35
+	if err != nil {
36
+		aname = name
37
+	}
38
+	c.Path = aname
39
+	c.Args = append([]string{name}, arg...)
40
+
41
+	if err := c.Start(); err != nil {
42
+		return err
43
+	}
44
+
45
+	go func() {
46
+		if err := c.Wait(); err != nil {
47
+			c.WaitError = err
48
+		}
49
+		close(c.WaitLock)
50
+	}()
51
+
52
+	return nil
53
+}
54
+
55
+func (d *driver) Kill(p *execdriver.Process, sig int) error {
56
+	return p.Process.Kill()
57
+}
58
+
59
+func (d *driver) Wait(id string, duration time.Duration) error {
60
+	panic("No Implemented")
61
+}
62
+
63
+func (d *driver) Version() string {
64
+	return "0.1"
65
+}
... ...
@@ -25,19 +25,20 @@ type Network struct {
25 25
 type Process struct {
26 26
 	exec.Cmd
27 27
 
28
-	ID         string
29
-	Privileged bool
30
-	User       string
31
-	Rootfs     string // root fs of the container
32
-	InitPath   string // dockerinit
33
-	Entrypoint string
34
-	Arguments  []string
35
-	WorkingDir string
36
-	ConfigPath string
37
-	Tty        bool
38
-	Network    *Network // if network is nil then networking is disabled
39
-	WaitLock   chan struct{}
40
-	WaitError  error
28
+	ID          string
29
+	Privileged  bool
30
+	User        string
31
+	Rootfs      string // root fs of the container
32
+	InitPath    string // dockerinit
33
+	Entrypoint  string
34
+	Arguments   []string
35
+	WorkingDir  string
36
+	ConfigPath  string
37
+	Tty         bool
38
+	Network     *Network // if network is nil then networking is disabled
39
+	SysInitPath string
40
+	WaitLock    chan struct{}
41
+	WaitError   error
41 42
 }
42 43
 
43 44
 func (c *Process) Pid() int {
... ...
@@ -88,7 +88,6 @@ func (d *driver) Start(c *execdriver.Process) error {
88 88
 		params = []string{
89 89
 			"unshare", "-m", "--", "/bin/sh", "-c", shellString,
90 90
 		}
91
-
92 91
 	}
93 92
 
94 93
 	params = append(params, "--", c.Entrypoint)
... ...
@@ -6,6 +6,7 @@ import (
6 6
 	"github.com/dotcloud/docker/archive"
7 7
 	"github.com/dotcloud/docker/cgroups"
8 8
 	"github.com/dotcloud/docker/execdriver"
9
+	"github.com/dotcloud/docker/execdriver/chroot"
9 10
 	"github.com/dotcloud/docker/execdriver/lxc"
10 11
 	"github.com/dotcloud/docker/graphdriver"
11 12
 	"github.com/dotcloud/docker/graphdriver/aufs"
... ...
@@ -735,7 +736,12 @@ func NewRuntimeFromDirectory(config *DaemonConfig) (*Runtime, error) {
735 735
 	}
736 736
 
737 737
 	capabilities := NewRuntimeCapabilities(false)
738
-	ed, err := lxc.NewDriver(config.Root, capabilities.AppArmor)
738
+	var ed execdriver.Driver
739
+	if driver := os.Getenv("EXEC_DRIVER"); driver == "lxc" {
740
+		ed, err = lxc.NewDriver(config.Root, capabilities.AppArmor)
741
+	} else {
742
+		ed, err = chroot.NewDriver()
743
+	}
739 744
 	if err != nil {
740 745
 		return nil, err
741 746
 	}
... ...
@@ -182,24 +182,25 @@ func getEnv(args *DockerInitArgs, key string) string {
182 182
 func executeProgram(args *DockerInitArgs) error {
183 183
 	setupEnv(args)
184 184
 
185
-	if err := setupHostname(args); err != nil {
186
-		return err
187
-	}
188
-
189
-	if err := setupNetworking(args); err != nil {
190
-		return err
191
-	}
185
+	if false {
186
+		if err := setupHostname(args); err != nil {
187
+			return err
188
+		}
192 189
 
193
-	if err := setupCapabilities(args); err != nil {
194
-		return err
195
-	}
190
+		if err := setupNetworking(args); err != nil {
191
+			return err
192
+		}
196 193
 
197
-	if err := setupWorkingDirectory(args); err != nil {
198
-		return err
199
-	}
194
+		if err := setupCapabilities(args); err != nil {
195
+			return err
196
+		}
197
+		if err := setupWorkingDirectory(args); err != nil {
198
+			return err
199
+		}
200 200
 
201
-	if err := changeUser(args); err != nil {
202
-		return err
201
+		if err := changeUser(args); err != nil {
202
+			return err
203
+		}
203 204
 	}
204 205
 
205 206
 	path, err := exec.LookPath(args.args[0])