create.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package native
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/dotcloud/docker/pkg/label"
  6. "github.com/dotcloud/docker/pkg/libcontainer"
  7. "github.com/dotcloud/docker/runtime/execdriver"
  8. "github.com/dotcloud/docker/runtime/execdriver/native/configuration"
  9. "github.com/dotcloud/docker/runtime/execdriver/native/template"
  10. )
  11. // createContainer populates and configures the container type with the
  12. // data provided by the execdriver.Command
  13. func (d *driver) createContainer(c *execdriver.Command) (*libcontainer.Container, error) {
  14. container := template.New()
  15. container.Hostname = getEnv("HOSTNAME", c.Env)
  16. container.Tty = c.Tty
  17. container.User = c.User
  18. container.WorkingDir = c.WorkingDir
  19. container.Env = c.Env
  20. container.Cgroups.Name = c.ID
  21. // check to see if we are running in ramdisk to disable pivot root
  22. container.NoPivotRoot = os.Getenv("DOCKER_RAMDISK") != ""
  23. if err := d.createNetwork(container, c); err != nil {
  24. return nil, err
  25. }
  26. if c.Privileged {
  27. if err := d.setPrivileged(container); err != nil {
  28. return nil, err
  29. }
  30. }
  31. if err := d.setupCgroups(container, c); err != nil {
  32. return nil, err
  33. }
  34. if err := d.setupMounts(container, c); err != nil {
  35. return nil, err
  36. }
  37. if err := d.setupLabels(container, c); err != nil {
  38. return nil, err
  39. }
  40. if err := configuration.ParseConfiguration(container, d.activeContainers, c.Config["native"]); err != nil {
  41. return nil, err
  42. }
  43. return container, nil
  44. }
  45. func (d *driver) createNetwork(container *libcontainer.Container, c *execdriver.Command) error {
  46. container.Networks = []*libcontainer.Network{
  47. {
  48. Mtu: c.Network.Mtu,
  49. Address: fmt.Sprintf("%s/%d", "127.0.0.1", 0),
  50. Gateway: "localhost",
  51. Type: "loopback",
  52. Context: libcontainer.Context{},
  53. },
  54. }
  55. if c.Network.Interface != nil {
  56. vethNetwork := libcontainer.Network{
  57. Mtu: c.Network.Mtu,
  58. Address: fmt.Sprintf("%s/%d", c.Network.Interface.IPAddress, c.Network.Interface.IPPrefixLen),
  59. Gateway: c.Network.Interface.Gateway,
  60. Type: "veth",
  61. Context: libcontainer.Context{
  62. "prefix": "veth",
  63. "bridge": c.Network.Interface.Bridge,
  64. },
  65. }
  66. container.Networks = append(container.Networks, &vethNetwork)
  67. }
  68. return nil
  69. }
  70. func (d *driver) setPrivileged(container *libcontainer.Container) error {
  71. for _, c := range container.CapabilitiesMask {
  72. c.Enabled = true
  73. }
  74. container.Cgroups.DeviceAccess = true
  75. container.Context["apparmor_profile"] = "unconfined"
  76. return nil
  77. }
  78. func (d *driver) setupCgroups(container *libcontainer.Container, c *execdriver.Command) error {
  79. if c.Resources != nil {
  80. container.Cgroups.CpuShares = c.Resources.CpuShares
  81. container.Cgroups.Memory = c.Resources.Memory
  82. container.Cgroups.MemorySwap = c.Resources.MemorySwap
  83. }
  84. return nil
  85. }
  86. func (d *driver) setupMounts(container *libcontainer.Container, c *execdriver.Command) error {
  87. for _, m := range c.Mounts {
  88. container.Mounts = append(container.Mounts, libcontainer.Mount{m.Source, m.Destination, m.Writable, m.Private})
  89. }
  90. return nil
  91. }
  92. func (d *driver) setupLabels(container *libcontainer.Container, c *execdriver.Command) error {
  93. labels := c.Config["label"]
  94. if len(labels) > 0 {
  95. process, mount, err := label.GenLabels(labels[0])
  96. if err != nil {
  97. return err
  98. }
  99. container.Context["mount_label"] = mount
  100. container.Context["process_label"] = process
  101. }
  102. return nil
  103. }