summaryrefslogtreecommitdiff
path: root/execdriver/native
diff options
context:
space:
mode:
Diffstat (limited to 'execdriver/native')
-rw-r--r--execdriver/native/default_template.go90
-rw-r--r--execdriver/native/driver.go251
-rw-r--r--execdriver/native/info.go21
-rw-r--r--execdriver/native/term.go42
4 files changed, 0 insertions, 404 deletions
diff --git a/execdriver/native/default_template.go b/execdriver/native/default_template.go
deleted file mode 100644
index 6e7d597b7b..0000000000
--- a/execdriver/native/default_template.go
+++ /dev/null
@@ -1,90 +0,0 @@
-package native
-
-import (
- "fmt"
- "github.com/dotcloud/docker/execdriver"
- "github.com/dotcloud/docker/pkg/cgroups"
- "github.com/dotcloud/docker/pkg/libcontainer"
- "os"
-)
-
-// createContainer populates and configures the container type with the
-// data provided by the execdriver.Command
-func createContainer(c *execdriver.Command) *libcontainer.Container {
- container := getDefaultTemplate()
-
- container.Hostname = getEnv("HOSTNAME", c.Env)
- container.Tty = c.Tty
- container.User = c.User
- container.WorkingDir = c.WorkingDir
- container.Env = c.Env
-
- if c.Network != nil {
- container.Networks = []*libcontainer.Network{
- {
- Mtu: c.Network.Mtu,
- Address: fmt.Sprintf("%s/%d", c.Network.IPAddress, c.Network.IPPrefixLen),
- Gateway: c.Network.Gateway,
- Type: "veth",
- Context: libcontainer.Context{
- "prefix": "veth",
- "bridge": c.Network.Bridge,
- },
- },
- }
- }
-
- container.Cgroups.Name = c.ID
- if c.Privileged {
- container.Capabilities = nil
- container.Cgroups.DeviceAccess = true
- container.Context["apparmor_profile"] = "unconfined"
- }
- if c.Resources != nil {
- container.Cgroups.CpuShares = c.Resources.CpuShares
- container.Cgroups.Memory = c.Resources.Memory
- container.Cgroups.MemorySwap = c.Resources.MemorySwap
- }
- // check to see if we are running in ramdisk to disable pivot root
- container.NoPivotRoot = os.Getenv("DOCKER_RAMDISK") != ""
-
- return container
-}
-
-// getDefaultTemplate returns the docker default for
-// the libcontainer configuration file
-func getDefaultTemplate() *libcontainer.Container {
- return &libcontainer.Container{
- Capabilities: libcontainer.Capabilities{
- libcontainer.GetCapability("SETPCAP"),
- libcontainer.GetCapability("SYS_MODULE"),
- libcontainer.GetCapability("SYS_RAWIO"),
- libcontainer.GetCapability("SYS_PACCT"),
- libcontainer.GetCapability("SYS_ADMIN"),
- libcontainer.GetCapability("SYS_NICE"),
- libcontainer.GetCapability("SYS_RESOURCE"),
- libcontainer.GetCapability("SYS_TIME"),
- libcontainer.GetCapability("SYS_TTY_CONFIG"),
- libcontainer.GetCapability("MKNOD"),
- libcontainer.GetCapability("AUDIT_WRITE"),
- libcontainer.GetCapability("AUDIT_CONTROL"),
- libcontainer.GetCapability("MAC_OVERRIDE"),
- libcontainer.GetCapability("MAC_ADMIN"),
- libcontainer.GetCapability("NET_ADMIN"),
- },
- Namespaces: libcontainer.Namespaces{
- libcontainer.GetNamespace("NEWNS"),
- libcontainer.GetNamespace("NEWUTS"),
- libcontainer.GetNamespace("NEWIPC"),
- libcontainer.GetNamespace("NEWPID"),
- libcontainer.GetNamespace("NEWNET"),
- },
- Cgroups: &cgroups.Cgroup{
- Parent: "docker",
- DeviceAccess: false,
- },
- Context: libcontainer.Context{
- "apparmor_profile": "docker-default",
- },
- }
-}
diff --git a/execdriver/native/driver.go b/execdriver/native/driver.go
deleted file mode 100644
index 452e802523..0000000000
--- a/execdriver/native/driver.go
+++ /dev/null
@@ -1,251 +0,0 @@
-package native
-
-import (
- "encoding/json"
- "fmt"
- "github.com/dotcloud/docker/execdriver"
- "github.com/dotcloud/docker/pkg/cgroups"
- "github.com/dotcloud/docker/pkg/libcontainer"
- "github.com/dotcloud/docker/pkg/libcontainer/apparmor"
- "github.com/dotcloud/docker/pkg/libcontainer/nsinit"
- "github.com/dotcloud/docker/pkg/system"
- "io/ioutil"
- "os"
- "os/exec"
- "path/filepath"
- "strconv"
- "strings"
- "syscall"
-)
-
-const (
- DriverName = "native"
- Version = "0.1"
-)
-
-func init() {
- execdriver.RegisterInitFunc(DriverName, func(args *execdriver.InitArgs) error {
- var (
- container *libcontainer.Container
- ns = nsinit.NewNsInit(&nsinit.DefaultCommandFactory{}, &nsinit.DefaultStateWriter{args.Root})
- )
- f, err := os.Open(filepath.Join(args.Root, "container.json"))
- if err != nil {
- return err
- }
- if err := json.NewDecoder(f).Decode(&container); err != nil {
- f.Close()
- return err
- }
- f.Close()
-
- cwd, err := os.Getwd()
- if err != nil {
- return err
- }
- syncPipe, err := nsinit.NewSyncPipeFromFd(0, uintptr(args.Pipe))
- if err != nil {
- return err
- }
- if err := ns.Init(container, cwd, args.Console, syncPipe, args.Args); err != nil {
- return err
- }
- return nil
- })
-}
-
-type driver struct {
- root string
-}
-
-func NewDriver(root string) (*driver, error) {
- if err := os.MkdirAll(root, 0700); err != nil {
- return nil, err
- }
- if err := apparmor.InstallDefaultProfile(); err != nil {
- return nil, err
- }
- return &driver{
- root: root,
- }, nil
-}
-
-func (d *driver) Run(c *execdriver.Command, pipes *execdriver.Pipes, startCallback execdriver.StartCallback) (int, error) {
- if err := d.validateCommand(c); err != nil {
- return -1, err
- }
- var (
- term nsinit.Terminal
- container = createContainer(c)
- factory = &dockerCommandFactory{c: c, driver: d}
- stateWriter = &dockerStateWriter{
- callback: startCallback,
- c: c,
- dsw: &nsinit.DefaultStateWriter{filepath.Join(d.root, c.ID)},
- }
- ns = nsinit.NewNsInit(factory, stateWriter)
- args = append([]string{c.Entrypoint}, c.Arguments...)
- )
- if err := d.createContainerRoot(c.ID); err != nil {
- return -1, err
- }
- defer d.removeContainerRoot(c.ID)
-
- if c.Tty {
- term = &dockerTtyTerm{
- pipes: pipes,
- }
- } else {
- term = &dockerStdTerm{
- pipes: pipes,
- }
- }
- c.Terminal = term
- if err := d.writeContainerFile(container, c.ID); err != nil {
- return -1, err
- }
- return ns.Exec(container, term, args)
-}
-
-func (d *driver) Kill(p *execdriver.Command, sig int) error {
- err := syscall.Kill(p.Process.Pid, syscall.Signal(sig))
- d.removeContainerRoot(p.ID)
- return err
-}
-
-func (d *driver) Info(id string) execdriver.Info {
- return &info{
- ID: id,
- driver: d,
- }
-}
-
-func (d *driver) Name() string {
- return fmt.Sprintf("%s-%s", DriverName, Version)
-}
-
-// TODO: this can be improved with our driver
-// there has to be a better way to do this
-func (d *driver) GetPidsForContainer(id string) ([]int, error) {
- pids := []int{}
-
- subsystem := "devices"
- cgroupRoot, err := cgroups.FindCgroupMountpoint(subsystem)
- if err != nil {
- return pids, err
- }
- cgroupDir, err := cgroups.GetThisCgroupDir(subsystem)
- if err != nil {
- return pids, err
- }
-
- filename := filepath.Join(cgroupRoot, cgroupDir, id, "tasks")
- if _, err := os.Stat(filename); os.IsNotExist(err) {
- filename = filepath.Join(cgroupRoot, cgroupDir, "docker", id, "tasks")
- }
-
- output, err := ioutil.ReadFile(filename)
- if err != nil {
- return pids, err
- }
- for _, p := range strings.Split(string(output), "\n") {
- if len(p) == 0 {
- continue
- }
- pid, err := strconv.Atoi(p)
- if err != nil {
- return pids, fmt.Errorf("Invalid pid '%s': %s", p, err)
- }
- pids = append(pids, pid)
- }
- return pids, nil
-}
-
-func (d *driver) writeContainerFile(container *libcontainer.Container, id string) error {
- data, err := json.Marshal(container)
- if err != nil {
- return err
- }
- return ioutil.WriteFile(filepath.Join(d.root, id, "container.json"), data, 0655)
-}
-
-func (d *driver) createContainerRoot(id string) error {
- return os.MkdirAll(filepath.Join(d.root, id), 0655)
-}
-
-func (d *driver) removeContainerRoot(id string) error {
- return os.RemoveAll(filepath.Join(d.root, id))
-}
-
-func (d *driver) validateCommand(c *execdriver.Command) error {
- // we need to check the Config of the command to make sure that we
- // do not have any of the lxc-conf variables
- for _, conf := range c.Config {
- if strings.Contains(conf, "lxc") {
- return fmt.Errorf("%s is not supported by the native driver", conf)
- }
- }
- return nil
-}
-
-func getEnv(key string, env []string) string {
- for _, pair := range env {
- parts := strings.Split(pair, "=")
- if parts[0] == key {
- return parts[1]
- }
- }
- return ""
-}
-
-type dockerCommandFactory struct {
- c *execdriver.Command
- driver *driver
-}
-
-// createCommand will return an exec.Cmd with the Cloneflags set to the proper namespaces
-// defined on the container's configuration and use the current binary as the init with the
-// args provided
-func (d *dockerCommandFactory) Create(container *libcontainer.Container, console string, syncFile *os.File, args []string) *exec.Cmd {
- // we need to join the rootfs because nsinit will setup the rootfs and chroot
- initPath := filepath.Join(d.c.Rootfs, d.c.InitPath)
-
- d.c.Path = initPath
- d.c.Args = append([]string{
- initPath,
- "-driver", DriverName,
- "-console", console,
- "-pipe", "3",
- "-root", filepath.Join(d.driver.root, d.c.ID),
- "--",
- }, args...)
-
- // set this to nil so that when we set the clone flags anything else is reset
- d.c.SysProcAttr = nil
- system.SetCloneFlags(&d.c.Cmd, uintptr(nsinit.GetNamespaceFlags(container.Namespaces)))
- d.c.ExtraFiles = []*os.File{syncFile}
-
- d.c.Env = container.Env
- d.c.Dir = d.c.Rootfs
-
- return &d.c.Cmd
-}
-
-type dockerStateWriter struct {
- dsw nsinit.StateWriter
- c *execdriver.Command
- callback execdriver.StartCallback
-}
-
-func (d *dockerStateWriter) WritePid(pid int) error {
- d.c.ContainerPid = pid
- err := d.dsw.WritePid(pid)
- if d.callback != nil {
- d.callback(d.c)
- }
- return err
-}
-
-func (d *dockerStateWriter) DeletePid() error {
- return d.dsw.DeletePid()
-}
diff --git a/execdriver/native/info.go b/execdriver/native/info.go
deleted file mode 100644
index aef2f85c6b..0000000000
--- a/execdriver/native/info.go
+++ /dev/null
@@ -1,21 +0,0 @@
-package native
-
-import (
- "os"
- "path/filepath"
-)
-
-type info struct {
- ID string
- driver *driver
-}
-
-// IsRunning is determined by looking for the
-// pid file for a container. If the file exists then the
-// container is currently running
-func (i *info) IsRunning() bool {
- if _, err := os.Stat(filepath.Join(i.driver.root, i.ID, "pid")); err == nil {
- return true
- }
- return false
-}
diff --git a/execdriver/native/term.go b/execdriver/native/term.go
deleted file mode 100644
index ec69820f75..0000000000
--- a/execdriver/native/term.go
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- These types are wrappers around the libcontainer Terminal interface so that
- we can resuse the docker implementations where possible.
-*/
-package native
-
-import (
- "github.com/dotcloud/docker/execdriver"
- "io"
- "os"
- "os/exec"
-)
-
-type dockerStdTerm struct {
- execdriver.StdConsole
- pipes *execdriver.Pipes
-}
-
-func (d *dockerStdTerm) Attach(cmd *exec.Cmd) error {
- return d.AttachPipes(cmd, d.pipes)
-}
-
-func (d *dockerStdTerm) SetMaster(master *os.File) {
- // do nothing
-}
-
-type dockerTtyTerm struct {
- execdriver.TtyConsole
- pipes *execdriver.Pipes
-}
-
-func (t *dockerTtyTerm) Attach(cmd *exec.Cmd) error {
- go io.Copy(t.pipes.Stdout, t.MasterPty)
- if t.pipes.Stdin != nil {
- go io.Copy(t.MasterPty, t.pipes.Stdin)
- }
- return nil
-}
-
-func (t *dockerTtyTerm) SetMaster(master *os.File) {
- t.MasterPty = master
-}