create.go 4.4 KB

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