create.go 5.9 KB

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