create.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package native
  2. import (
  3. "fmt"
  4. "os"
  5. "os/exec"
  6. "path/filepath"
  7. "github.com/dotcloud/docker/daemon/execdriver"
  8. "github.com/dotcloud/docker/daemon/execdriver/native/configuration"
  9. "github.com/dotcloud/docker/daemon/execdriver/native/template"
  10. "github.com/dotcloud/docker/pkg/apparmor"
  11. "github.com/dotcloud/docker/pkg/libcontainer"
  12. "github.com/dotcloud/docker/pkg/libcontainer/mount/nodes"
  13. )
  14. // createContainer populates and configures the container type with the
  15. // data provided by the execdriver.Command
  16. func (d *driver) createContainer(c *execdriver.Command) (*libcontainer.Container, error) {
  17. container := template.New()
  18. container.Hostname = getEnv("HOSTNAME", c.Env)
  19. container.Tty = c.Tty
  20. container.User = c.User
  21. container.WorkingDir = c.WorkingDir
  22. container.Env = c.Env
  23. container.Cgroups.Name = c.ID
  24. // check to see if we are running in ramdisk to disable pivot root
  25. container.NoPivotRoot = os.Getenv("DOCKER_RAMDISK") != ""
  26. container.Context["restrictions"] = "true"
  27. if err := d.createNetwork(container, c); err != nil {
  28. return nil, err
  29. }
  30. if c.Privileged {
  31. if err := d.setPrivileged(container); err != nil {
  32. return nil, err
  33. }
  34. }
  35. if err := d.setupCgroups(container, c); err != nil {
  36. return nil, err
  37. }
  38. if err := d.setupMounts(container, c); err != nil {
  39. return nil, err
  40. }
  41. if err := d.setupLabels(container, c); err != nil {
  42. return nil, err
  43. }
  44. cmds := make(map[string]*exec.Cmd)
  45. d.Lock()
  46. for k, v := range d.activeContainers {
  47. cmds[k] = v.cmd
  48. }
  49. d.Unlock()
  50. if err := configuration.ParseConfiguration(container, cmds, c.Config["native"]); err != nil {
  51. return nil, err
  52. }
  53. return container, nil
  54. }
  55. func (d *driver) createNetwork(container *libcontainer.Container, c *execdriver.Command) error {
  56. if c.Network.HostNetworking {
  57. container.Namespaces["NEWNET"] = false
  58. return nil
  59. }
  60. container.Networks = []*libcontainer.Network{
  61. {
  62. Mtu: c.Network.Mtu,
  63. Address: fmt.Sprintf("%s/%d", "127.0.0.1", 0),
  64. Gateway: "localhost",
  65. Type: "loopback",
  66. Context: libcontainer.Context{},
  67. },
  68. }
  69. if c.Network.Interface != nil {
  70. vethNetwork := libcontainer.Network{
  71. Mtu: c.Network.Mtu,
  72. Address: fmt.Sprintf("%s/%d", c.Network.Interface.IPAddress, c.Network.Interface.IPPrefixLen),
  73. Gateway: c.Network.Interface.Gateway,
  74. Type: "veth",
  75. Context: libcontainer.Context{
  76. "prefix": "veth",
  77. "bridge": c.Network.Interface.Bridge,
  78. },
  79. }
  80. container.Networks = append(container.Networks, &vethNetwork)
  81. }
  82. if c.Network.ContainerID != "" {
  83. d.Lock()
  84. active := d.activeContainers[c.Network.ContainerID]
  85. d.Unlock()
  86. if active == nil || active.cmd.Process == nil {
  87. return fmt.Errorf("%s is not a valid running container to join", c.Network.ContainerID)
  88. }
  89. cmd := active.cmd
  90. nspath := filepath.Join("/proc", fmt.Sprint(cmd.Process.Pid), "ns", "net")
  91. container.Networks = append(container.Networks, &libcontainer.Network{
  92. Type: "netns",
  93. Context: libcontainer.Context{
  94. "nspath": nspath,
  95. },
  96. })
  97. }
  98. return nil
  99. }
  100. func (d *driver) setPrivileged(container *libcontainer.Container) (err error) {
  101. container.Capabilities = libcontainer.GetAllCapabilities()
  102. container.Cgroups.DeviceAccess = true
  103. delete(container.Context, "restrictions")
  104. container.OptionalDeviceNodes = nil
  105. if container.RequiredDeviceNodes, err = nodes.GetHostDeviceNodes(); err != nil {
  106. return err
  107. }
  108. if apparmor.IsEnabled() {
  109. container.Context["apparmor_profile"] = "unconfined"
  110. }
  111. return nil
  112. }
  113. func (d *driver) setupCgroups(container *libcontainer.Container, c *execdriver.Command) error {
  114. if c.Resources != nil {
  115. container.Cgroups.CpuShares = c.Resources.CpuShares
  116. container.Cgroups.Memory = c.Resources.Memory
  117. container.Cgroups.MemoryReservation = c.Resources.Memory
  118. container.Cgroups.MemorySwap = c.Resources.MemorySwap
  119. container.Cgroups.CpusetCpus = c.Resources.Cpuset
  120. }
  121. return nil
  122. }
  123. func (d *driver) setupMounts(container *libcontainer.Container, c *execdriver.Command) error {
  124. for _, m := range c.Mounts {
  125. container.Mounts = append(container.Mounts, libcontainer.Mount{
  126. Type: "bind",
  127. Source: m.Source,
  128. Destination: m.Destination,
  129. Writable: m.Writable,
  130. Private: m.Private,
  131. })
  132. }
  133. return nil
  134. }
  135. func (d *driver) setupLabels(container *libcontainer.Container, c *execdriver.Command) error {
  136. container.Context["process_label"] = c.Config["process_label"][0]
  137. container.Context["mount_label"] = c.Config["mount_label"][0]
  138. return nil
  139. }