create.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. for k, v := range d.activeContainers {
  46. cmds[k] = v.cmd
  47. }
  48. if err := configuration.ParseConfiguration(container, cmds, c.Config["native"]); err != nil {
  49. return nil, err
  50. }
  51. return container, nil
  52. }
  53. func (d *driver) createNetwork(container *libcontainer.Container, c *execdriver.Command) error {
  54. if c.Network.HostNetworking {
  55. container.Namespaces["NEWNET"] = false
  56. return nil
  57. }
  58. container.Networks = []*libcontainer.Network{
  59. {
  60. Mtu: c.Network.Mtu,
  61. Address: fmt.Sprintf("%s/%d", "127.0.0.1", 0),
  62. Gateway: "localhost",
  63. Type: "loopback",
  64. Context: libcontainer.Context{},
  65. },
  66. }
  67. if c.Network.Interface != nil {
  68. vethNetwork := libcontainer.Network{
  69. Mtu: c.Network.Mtu,
  70. Address: fmt.Sprintf("%s/%d", c.Network.Interface.IPAddress, c.Network.Interface.IPPrefixLen),
  71. Gateway: c.Network.Interface.Gateway,
  72. Type: "veth",
  73. Context: libcontainer.Context{
  74. "prefix": "veth",
  75. "bridge": c.Network.Interface.Bridge,
  76. },
  77. }
  78. container.Networks = append(container.Networks, &vethNetwork)
  79. }
  80. if c.Network.ContainerID != "" {
  81. active := d.activeContainers[c.Network.ContainerID]
  82. if active == nil || active.cmd.Process == nil {
  83. return fmt.Errorf("%s is not a valid running container to join", c.Network.ContainerID)
  84. }
  85. cmd := active.cmd
  86. nspath := filepath.Join("/proc", fmt.Sprint(cmd.Process.Pid), "ns", "net")
  87. container.Networks = append(container.Networks, &libcontainer.Network{
  88. Type: "netns",
  89. Context: libcontainer.Context{
  90. "nspath": nspath,
  91. },
  92. })
  93. }
  94. return nil
  95. }
  96. func (d *driver) setPrivileged(container *libcontainer.Container) (err error) {
  97. container.Capabilities = libcontainer.GetAllCapabilities()
  98. container.Cgroups.DeviceAccess = true
  99. delete(container.Context, "restrictions")
  100. container.OptionalDeviceNodes = nil
  101. if container.RequiredDeviceNodes, err = nodes.GetHostDeviceNodes(); err != nil {
  102. return err
  103. }
  104. if apparmor.IsEnabled() {
  105. container.Context["apparmor_profile"] = "unconfined"
  106. }
  107. return nil
  108. }
  109. func (d *driver) setupCgroups(container *libcontainer.Container, c *execdriver.Command) error {
  110. if c.Resources != nil {
  111. container.Cgroups.CpuShares = c.Resources.CpuShares
  112. container.Cgroups.Memory = c.Resources.Memory
  113. container.Cgroups.MemoryReservation = c.Resources.Memory
  114. container.Cgroups.MemorySwap = c.Resources.MemorySwap
  115. container.Cgroups.CpusetCpus = c.Resources.Cpuset
  116. }
  117. return nil
  118. }
  119. func (d *driver) setupMounts(container *libcontainer.Container, c *execdriver.Command) error {
  120. for _, m := range c.Mounts {
  121. container.Mounts = append(container.Mounts, libcontainer.Mount{
  122. Type: "bind",
  123. Source: m.Source,
  124. Destination: m.Destination,
  125. Writable: m.Writable,
  126. Private: m.Private,
  127. })
  128. }
  129. return nil
  130. }
  131. func (d *driver) setupLabels(container *libcontainer.Container, c *execdriver.Command) error {
  132. container.Context["process_label"] = c.Config["process_label"][0]
  133. container.Context["mount_label"] = c.Config["mount_label"][0]
  134. return nil
  135. }