create.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // +build linux,cgo
  2. package native
  3. import (
  4. "fmt"
  5. "os/exec"
  6. "path/filepath"
  7. "github.com/docker/docker/daemon/execdriver"
  8. "github.com/docker/libcontainer"
  9. "github.com/docker/libcontainer/apparmor"
  10. "github.com/docker/libcontainer/devices"
  11. "github.com/docker/libcontainer/mount"
  12. "github.com/docker/libcontainer/security/capabilities"
  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.Config, error) {
  17. container := execdriver.InitContainer(c)
  18. if err := d.createIpc(container, c); err != nil {
  19. return nil, err
  20. }
  21. if err := d.createPid(container, c); err != nil {
  22. return nil, err
  23. }
  24. if err := d.createNetwork(container, c); err != nil {
  25. return nil, err
  26. }
  27. if c.ProcessConfig.Privileged {
  28. if err := d.setPrivileged(container); err != nil {
  29. return nil, err
  30. }
  31. } else {
  32. if err := d.setCapabilities(container, c); err != nil {
  33. return nil, err
  34. }
  35. }
  36. if c.AppArmorProfile != "" {
  37. container.AppArmorProfile = c.AppArmorProfile
  38. }
  39. if err := execdriver.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. return container, nil
  55. }
  56. func (d *driver) createNetwork(container *libcontainer.Config, c *execdriver.Command) error {
  57. if c.Network.HostNetworking {
  58. container.Namespaces.Remove(libcontainer.NEWNET)
  59. return nil
  60. }
  61. container.Networks = []*libcontainer.Network{
  62. {
  63. Mtu: c.Network.Mtu,
  64. Address: fmt.Sprintf("%s/%d", "127.0.0.1", 0),
  65. Gateway: "localhost",
  66. Type: "loopback",
  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. MacAddress: c.Network.Interface.MacAddress,
  74. Gateway: c.Network.Interface.Gateway,
  75. Type: "veth",
  76. Bridge: c.Network.Interface.Bridge,
  77. VethPrefix: "veth",
  78. }
  79. if c.Network.Interface.GlobalIPv6Address != "" {
  80. vethNetwork.IPv6Address = fmt.Sprintf("%s/%d", c.Network.Interface.GlobalIPv6Address, c.Network.Interface.GlobalIPv6PrefixLen)
  81. vethNetwork.IPv6Gateway = c.Network.Interface.IPv6Gateway
  82. }
  83. container.Networks = append(container.Networks, &vethNetwork)
  84. }
  85. if c.Network.ContainerID != "" {
  86. d.Lock()
  87. active := d.activeContainers[c.Network.ContainerID]
  88. d.Unlock()
  89. if active == nil || active.cmd.Process == nil {
  90. return fmt.Errorf("%s is not a valid running container to join", c.Network.ContainerID)
  91. }
  92. cmd := active.cmd
  93. nspath := filepath.Join("/proc", fmt.Sprint(cmd.Process.Pid), "ns", "net")
  94. container.Namespaces.Add(libcontainer.NEWNET, nspath)
  95. }
  96. return nil
  97. }
  98. func (d *driver) createIpc(container *libcontainer.Config, c *execdriver.Command) error {
  99. if c.Ipc.HostIpc {
  100. container.Namespaces.Remove(libcontainer.NEWIPC)
  101. return nil
  102. }
  103. if c.Ipc.ContainerID != "" {
  104. d.Lock()
  105. active := d.activeContainers[c.Ipc.ContainerID]
  106. d.Unlock()
  107. if active == nil || active.cmd.Process == nil {
  108. return fmt.Errorf("%s is not a valid running container to join", c.Ipc.ContainerID)
  109. }
  110. cmd := active.cmd
  111. container.Namespaces.Add(libcontainer.NEWIPC, filepath.Join("/proc", fmt.Sprint(cmd.Process.Pid), "ns", "ipc"))
  112. }
  113. return nil
  114. }
  115. func (d *driver) createPid(container *libcontainer.Config, c *execdriver.Command) error {
  116. if c.Pid.HostPid {
  117. container.Namespaces.Remove(libcontainer.NEWPID)
  118. return nil
  119. }
  120. return nil
  121. }
  122. func (d *driver) setPrivileged(container *libcontainer.Config) (err error) {
  123. container.Capabilities = capabilities.GetAllCapabilities()
  124. container.Cgroups.AllowAllDevices = true
  125. hostDeviceNodes, err := devices.GetHostDeviceNodes()
  126. if err != nil {
  127. return err
  128. }
  129. container.MountConfig.DeviceNodes = hostDeviceNodes
  130. container.RestrictSys = false
  131. if apparmor.IsEnabled() {
  132. container.AppArmorProfile = "unconfined"
  133. }
  134. return nil
  135. }
  136. func (d *driver) setCapabilities(container *libcontainer.Config, c *execdriver.Command) (err error) {
  137. container.Capabilities, err = execdriver.TweakCapabilities(container.Capabilities, c.CapAdd, c.CapDrop)
  138. return err
  139. }
  140. func (d *driver) setupMounts(container *libcontainer.Config, c *execdriver.Command) error {
  141. for _, m := range c.Mounts {
  142. container.MountConfig.Mounts = append(container.MountConfig.Mounts, &mount.Mount{
  143. Type: "bind",
  144. Source: m.Source,
  145. Destination: m.Destination,
  146. Writable: m.Writable,
  147. Private: m.Private,
  148. Slave: m.Slave,
  149. })
  150. }
  151. return nil
  152. }
  153. func (d *driver) setupLabels(container *libcontainer.Config, c *execdriver.Command) error {
  154. container.ProcessLabel = c.ProcessLabel
  155. container.MountConfig.MountLabel = c.MountLabel
  156. return nil
  157. }