parse.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package configuration
  2. import (
  3. "fmt"
  4. "github.com/dotcloud/docker/pkg/libcontainer"
  5. "os/exec"
  6. "path/filepath"
  7. "strconv"
  8. "strings"
  9. )
  10. type Action func(*libcontainer.Container, interface{}, string) error
  11. var actions = map[string]Action{
  12. "cap.add": addCap, // add a cap
  13. "cap.drop": dropCap, // drop a cap
  14. "ns.add": addNamespace, // add a namespace
  15. "ns.drop": dropNamespace, // drop a namespace when cloning
  16. "net.join": joinNetNamespace, // join another containers net namespace
  17. // "net.veth.mac": vethMacAddress, // set the mac address for the veth
  18. "cgroups.cpu_shares": cpuShares, // set the cpu shares
  19. "cgroups.memory": memory, // set the memory limit
  20. "cgroups.memory_swap": memorySwap, // set the memory swap limit
  21. "apparmor_profile": apparmorProfile, // set the apparmor profile to apply
  22. "fs.readonly": readonlyFs, // make the rootfs of the container read only
  23. }
  24. func apparmorProfile(container *libcontainer.Container, context interface{}, value string) error {
  25. container.Context["apparmor_profile"] = value
  26. return nil
  27. }
  28. func cpuShares(container *libcontainer.Container, context interface{}, value string) error {
  29. if container.Cgroups == nil {
  30. return fmt.Errorf("cannot set cgroups when they are disabled")
  31. }
  32. v, err := strconv.ParseInt(value, 0, 64)
  33. if err != nil {
  34. return err
  35. }
  36. container.Cgroups.CpuShares = v
  37. return nil
  38. }
  39. func memory(container *libcontainer.Container, context interface{}, value string) error {
  40. if container.Cgroups == nil {
  41. return fmt.Errorf("cannot set cgroups when they are disabled")
  42. }
  43. v, err := strconv.ParseInt(value, 0, 64)
  44. if err != nil {
  45. return err
  46. }
  47. container.Cgroups.Memory = v
  48. return nil
  49. }
  50. func memorySwap(container *libcontainer.Container, context interface{}, value string) error {
  51. if container.Cgroups == nil {
  52. return fmt.Errorf("cannot set cgroups when they are disabled")
  53. }
  54. v, err := strconv.ParseInt(value, 0, 64)
  55. if err != nil {
  56. return err
  57. }
  58. container.Cgroups.MemorySwap = v
  59. return nil
  60. }
  61. func addCap(container *libcontainer.Container, context interface{}, value string) error {
  62. c := container.CapabilitiesMask.Get(value)
  63. if c == nil {
  64. return fmt.Errorf("%s is not a valid capability", value)
  65. }
  66. c.Enabled = true
  67. return nil
  68. }
  69. func dropCap(container *libcontainer.Container, context interface{}, value string) error {
  70. c := container.CapabilitiesMask.Get(value)
  71. if c == nil {
  72. return fmt.Errorf("%s is not a valid capability", value)
  73. }
  74. c.Enabled = false
  75. return nil
  76. }
  77. func addNamespace(container *libcontainer.Container, context interface{}, value string) error {
  78. ns := container.Namespaces.Get(value)
  79. if ns == nil {
  80. return fmt.Errorf("%s is not a valid namespace", value[1:])
  81. }
  82. ns.Enabled = true
  83. return nil
  84. }
  85. func dropNamespace(container *libcontainer.Container, context interface{}, value string) error {
  86. ns := container.Namespaces.Get(value)
  87. if ns == nil {
  88. return fmt.Errorf("%s is not a valid namespace", value[1:])
  89. }
  90. ns.Enabled = false
  91. return nil
  92. }
  93. func readonlyFs(container *libcontainer.Container, context interface{}, value string) error {
  94. switch value {
  95. case "1", "true":
  96. container.ReadonlyFs = true
  97. default:
  98. container.ReadonlyFs = false
  99. }
  100. return nil
  101. }
  102. func joinNetNamespace(container *libcontainer.Container, context interface{}, value string) error {
  103. var (
  104. running = context.(map[string]*exec.Cmd)
  105. cmd = running[value]
  106. )
  107. if cmd == nil || cmd.Process == nil {
  108. return fmt.Errorf("%s is not a valid running container to join", value)
  109. }
  110. nspath := filepath.Join("/proc", fmt.Sprint(cmd.Process.Pid), "ns", "net")
  111. container.Networks = append(container.Networks, &libcontainer.Network{
  112. Type: "netns",
  113. Context: libcontainer.Context{
  114. "nspath": nspath,
  115. },
  116. })
  117. return nil
  118. }
  119. func vethMacAddress(container *libcontainer.Container, context interface{}, value string) error {
  120. var veth *libcontainer.Network
  121. for _, network := range container.Networks {
  122. if network.Type == "veth" {
  123. veth = network
  124. break
  125. }
  126. }
  127. if veth == nil {
  128. return fmt.Errorf("not veth configured for container")
  129. }
  130. veth.Context["mac"] = value
  131. return nil
  132. }
  133. // configureCustomOptions takes string commands from the user and allows modification of the
  134. // container's default configuration.
  135. //
  136. // format: <key> <...value>
  137. // i.e: cgroup devices.allow *:*
  138. func ParseConfiguration(container *libcontainer.Container, running map[string]*exec.Cmd, opts []string) error {
  139. for _, opt := range opts {
  140. kv := strings.SplitN(opt, "=", 2)
  141. if len(kv) < 2 {
  142. return fmt.Errorf("invalid format for %s", opt)
  143. }
  144. action, exists := actions[kv[0]]
  145. if !exists {
  146. return fmt.Errorf("%s is not a valid option for the native driver", kv[0])
  147. }
  148. if err := action(container, running, kv[1]); err != nil {
  149. return err
  150. }
  151. }
  152. return nil
  153. }