create.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // +build linux,cgo
  2. package native
  3. import (
  4. "errors"
  5. "fmt"
  6. "net"
  7. "path/filepath"
  8. "strings"
  9. "syscall"
  10. "github.com/docker/docker/daemon/execdriver"
  11. "github.com/docker/docker/pkg/symlink"
  12. "github.com/docker/libcontainer/apparmor"
  13. "github.com/docker/libcontainer/configs"
  14. "github.com/docker/libcontainer/devices"
  15. "github.com/docker/libcontainer/utils"
  16. )
  17. // createContainer populates and configures the container type with the
  18. // data provided by the execdriver.Command
  19. func (d *driver) createContainer(c *execdriver.Command) (*configs.Config, error) {
  20. container := execdriver.InitContainer(c)
  21. if err := d.createIpc(container, c); err != nil {
  22. return nil, err
  23. }
  24. if err := d.createPid(container, c); err != nil {
  25. return nil, err
  26. }
  27. if err := d.createNetwork(container, c); err != nil {
  28. return nil, err
  29. }
  30. if c.ProcessConfig.Privileged {
  31. // clear readonly for /sys
  32. for i := range container.Mounts {
  33. if container.Mounts[i].Destination == "/sys" {
  34. container.Mounts[i].Flags &= ^syscall.MS_RDONLY
  35. }
  36. }
  37. container.ReadonlyPaths = nil
  38. container.MaskPaths = nil
  39. if err := d.setPrivileged(container); err != nil {
  40. return nil, err
  41. }
  42. } else {
  43. if err := d.setCapabilities(container, c); err != nil {
  44. return nil, err
  45. }
  46. }
  47. if c.AppArmorProfile != "" {
  48. container.AppArmorProfile = c.AppArmorProfile
  49. }
  50. if err := execdriver.SetupCgroups(container, c); err != nil {
  51. return nil, err
  52. }
  53. if err := d.setupMounts(container, c); err != nil {
  54. return nil, err
  55. }
  56. if err := d.setupLabels(container, c); err != nil {
  57. return nil, err
  58. }
  59. d.setupRlimits(container, c)
  60. return container, nil
  61. }
  62. func generateIfaceName() (string, error) {
  63. for i := 0; i < 10; i++ {
  64. name, err := utils.GenerateRandomName("veth", 7)
  65. if err != nil {
  66. continue
  67. }
  68. if _, err := net.InterfaceByName(name); err != nil {
  69. if strings.Contains(err.Error(), "no such") {
  70. return name, nil
  71. }
  72. return "", err
  73. }
  74. }
  75. return "", errors.New("Failed to find name for new interface")
  76. }
  77. func (d *driver) createNetwork(container *configs.Config, c *execdriver.Command) error {
  78. if c.Network.HostNetworking {
  79. container.Namespaces.Remove(configs.NEWNET)
  80. return nil
  81. }
  82. container.Networks = []*configs.Network{
  83. {
  84. Type: "loopback",
  85. },
  86. }
  87. iName, err := generateIfaceName()
  88. if err != nil {
  89. return err
  90. }
  91. if c.Network.Interface != nil {
  92. vethNetwork := configs.Network{
  93. Name: "eth0",
  94. HostInterfaceName: iName,
  95. Mtu: c.Network.Mtu,
  96. Address: fmt.Sprintf("%s/%d", c.Network.Interface.IPAddress, c.Network.Interface.IPPrefixLen),
  97. MacAddress: c.Network.Interface.MacAddress,
  98. Gateway: c.Network.Interface.Gateway,
  99. Type: "veth",
  100. Bridge: c.Network.Interface.Bridge,
  101. }
  102. if c.Network.Interface.GlobalIPv6Address != "" {
  103. vethNetwork.IPv6Address = fmt.Sprintf("%s/%d", c.Network.Interface.GlobalIPv6Address, c.Network.Interface.GlobalIPv6PrefixLen)
  104. vethNetwork.IPv6Gateway = c.Network.Interface.IPv6Gateway
  105. }
  106. container.Networks = append(container.Networks, &vethNetwork)
  107. }
  108. if c.Network.ContainerID != "" {
  109. d.Lock()
  110. active := d.activeContainers[c.Network.ContainerID]
  111. d.Unlock()
  112. if active == nil {
  113. return fmt.Errorf("%s is not a valid running container to join", c.Network.ContainerID)
  114. }
  115. state, err := active.State()
  116. if err != nil {
  117. return err
  118. }
  119. container.Namespaces.Add(configs.NEWNET, state.NamespacePaths[configs.NEWNET])
  120. }
  121. return nil
  122. }
  123. func (d *driver) createIpc(container *configs.Config, c *execdriver.Command) error {
  124. if c.Ipc.HostIpc {
  125. container.Namespaces.Remove(configs.NEWIPC)
  126. return nil
  127. }
  128. if c.Ipc.ContainerID != "" {
  129. d.Lock()
  130. active := d.activeContainers[c.Ipc.ContainerID]
  131. d.Unlock()
  132. if active == nil {
  133. return fmt.Errorf("%s is not a valid running container to join", c.Ipc.ContainerID)
  134. }
  135. state, err := active.State()
  136. if err != nil {
  137. return err
  138. }
  139. container.Namespaces.Add(configs.NEWIPC, state.NamespacePaths[configs.NEWIPC])
  140. }
  141. return nil
  142. }
  143. func (d *driver) createPid(container *configs.Config, c *execdriver.Command) error {
  144. if c.Pid.HostPid {
  145. container.Namespaces.Remove(configs.NEWPID)
  146. return nil
  147. }
  148. return nil
  149. }
  150. func (d *driver) setPrivileged(container *configs.Config) (err error) {
  151. container.Capabilities = execdriver.GetAllCapabilities()
  152. container.Cgroups.AllowAllDevices = true
  153. hostDevices, err := devices.HostDevices()
  154. if err != nil {
  155. return err
  156. }
  157. container.Devices = hostDevices
  158. if apparmor.IsEnabled() {
  159. container.AppArmorProfile = "unconfined"
  160. }
  161. return nil
  162. }
  163. func (d *driver) setCapabilities(container *configs.Config, c *execdriver.Command) (err error) {
  164. container.Capabilities, err = execdriver.TweakCapabilities(container.Capabilities, c.CapAdd, c.CapDrop)
  165. return err
  166. }
  167. func (d *driver) setupRlimits(container *configs.Config, c *execdriver.Command) {
  168. if c.Resources == nil {
  169. return
  170. }
  171. for _, rlimit := range c.Resources.Rlimits {
  172. container.Rlimits = append(container.Rlimits, configs.Rlimit{
  173. Type: rlimit.Type,
  174. Hard: rlimit.Hard,
  175. Soft: rlimit.Soft,
  176. })
  177. }
  178. }
  179. func (d *driver) setupMounts(container *configs.Config, c *execdriver.Command) error {
  180. userMounts := make(map[string]struct{})
  181. for _, m := range c.Mounts {
  182. userMounts[m.Destination] = struct{}{}
  183. }
  184. // Filter out mounts that are overriden by user supplied mounts
  185. var defaultMounts []*configs.Mount
  186. for _, m := range container.Mounts {
  187. if _, ok := userMounts[m.Destination]; !ok {
  188. defaultMounts = append(defaultMounts, m)
  189. }
  190. }
  191. container.Mounts = defaultMounts
  192. for _, m := range c.Mounts {
  193. dest, err := symlink.FollowSymlinkInScope(filepath.Join(c.Rootfs, m.Destination), c.Rootfs)
  194. if err != nil {
  195. return err
  196. }
  197. flags := syscall.MS_BIND | syscall.MS_REC
  198. if !m.Writable {
  199. flags |= syscall.MS_RDONLY
  200. }
  201. if m.Slave {
  202. flags |= syscall.MS_SLAVE
  203. }
  204. container.Mounts = append(container.Mounts, &configs.Mount{
  205. Source: m.Source,
  206. Destination: dest,
  207. Device: "bind",
  208. Flags: flags,
  209. })
  210. }
  211. return nil
  212. }
  213. func (d *driver) setupLabels(container *configs.Config, c *execdriver.Command) error {
  214. container.ProcessLabel = c.ProcessLabel
  215. container.MountLabel = c.MountLabel
  216. return nil
  217. }