driver_linux.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package execdriver
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "github.com/docker/docker/daemon/execdriver/native/template"
  11. "github.com/docker/libcontainer"
  12. "github.com/docker/libcontainer/cgroups/fs"
  13. "github.com/docker/libcontainer/configs"
  14. )
  15. func InitContainer(c *Command) *configs.Config {
  16. container := template.New()
  17. container.Hostname = getEnv("HOSTNAME", c.ProcessConfig.Env)
  18. container.Cgroups.Name = c.ID
  19. container.Cgroups.AllowedDevices = c.AllowedDevices
  20. container.Devices = c.AutoCreatedDevices
  21. container.Rootfs = c.Rootfs
  22. container.Readonlyfs = c.ReadonlyRootfs
  23. container.Privatefs = true
  24. // check to see if we are running in ramdisk to disable pivot root
  25. container.NoPivotRoot = os.Getenv("DOCKER_RAMDISK") != ""
  26. // Default parent cgroup is "docker". Override if required.
  27. if c.CgroupParent != "" {
  28. container.Cgroups.Parent = c.CgroupParent
  29. }
  30. return container
  31. }
  32. func getEnv(key string, env []string) string {
  33. for _, pair := range env {
  34. parts := strings.Split(pair, "=")
  35. if parts[0] == key {
  36. return parts[1]
  37. }
  38. }
  39. return ""
  40. }
  41. func SetupCgroups(container *configs.Config, c *Command) error {
  42. if c.Resources != nil {
  43. container.Cgroups.CpuShares = c.Resources.CpuShares
  44. container.Cgroups.Memory = c.Resources.Memory
  45. container.Cgroups.MemoryReservation = c.Resources.Memory
  46. container.Cgroups.MemorySwap = c.Resources.MemorySwap
  47. container.Cgroups.CpusetCpus = c.Resources.CpusetCpus
  48. container.Cgroups.CpusetMems = c.Resources.CpusetMems
  49. container.Cgroups.CpuPeriod = c.Resources.CpuPeriod
  50. container.Cgroups.CpuQuota = c.Resources.CpuQuota
  51. container.Cgroups.BlkioWeight = c.Resources.BlkioWeight
  52. container.Cgroups.OomKillDisable = c.Resources.OomKillDisable
  53. }
  54. return nil
  55. }
  56. // Returns the network statistics for the network interfaces represented by the NetworkRuntimeInfo.
  57. func getNetworkInterfaceStats(interfaceName string) (*libcontainer.NetworkInterface, error) {
  58. out := &libcontainer.NetworkInterface{Name: interfaceName}
  59. // This can happen if the network runtime information is missing - possible if the
  60. // container was created by an old version of libcontainer.
  61. if interfaceName == "" {
  62. return out, nil
  63. }
  64. type netStatsPair struct {
  65. // Where to write the output.
  66. Out *uint64
  67. // The network stats file to read.
  68. File string
  69. }
  70. // Ingress for host veth is from the container. Hence tx_bytes stat on the host veth is actually number of bytes received by the container.
  71. netStats := []netStatsPair{
  72. {Out: &out.RxBytes, File: "tx_bytes"},
  73. {Out: &out.RxPackets, File: "tx_packets"},
  74. {Out: &out.RxErrors, File: "tx_errors"},
  75. {Out: &out.RxDropped, File: "tx_dropped"},
  76. {Out: &out.TxBytes, File: "rx_bytes"},
  77. {Out: &out.TxPackets, File: "rx_packets"},
  78. {Out: &out.TxErrors, File: "rx_errors"},
  79. {Out: &out.TxDropped, File: "rx_dropped"},
  80. }
  81. for _, netStat := range netStats {
  82. data, err := readSysfsNetworkStats(interfaceName, netStat.File)
  83. if err != nil {
  84. return nil, err
  85. }
  86. *(netStat.Out) = data
  87. }
  88. return out, nil
  89. }
  90. // Reads the specified statistics available under /sys/class/net/<EthInterface>/statistics
  91. func readSysfsNetworkStats(ethInterface, statsFile string) (uint64, error) {
  92. data, err := ioutil.ReadFile(filepath.Join("/sys/class/net", ethInterface, "statistics", statsFile))
  93. if err != nil {
  94. return 0, err
  95. }
  96. return strconv.ParseUint(strings.TrimSpace(string(data)), 10, 64)
  97. }
  98. func Stats(containerDir string, containerMemoryLimit int64, machineMemory int64) (*ResourceStats, error) {
  99. f, err := os.Open(filepath.Join(containerDir, "state.json"))
  100. if err != nil {
  101. return nil, err
  102. }
  103. defer f.Close()
  104. type network struct {
  105. Type string
  106. HostInterfaceName string
  107. }
  108. state := struct {
  109. CgroupPaths map[string]string `json:"cgroup_paths"`
  110. Networks []network
  111. }{}
  112. if err := json.NewDecoder(f).Decode(&state); err != nil {
  113. return nil, err
  114. }
  115. now := time.Now()
  116. mgr := fs.Manager{Paths: state.CgroupPaths}
  117. cstats, err := mgr.GetStats()
  118. if err != nil {
  119. return nil, err
  120. }
  121. stats := &libcontainer.Stats{CgroupStats: cstats}
  122. // if the container does not have any memory limit specified set the
  123. // limit to the machines memory
  124. memoryLimit := containerMemoryLimit
  125. if memoryLimit == 0 {
  126. memoryLimit = machineMemory
  127. }
  128. for _, iface := range state.Networks {
  129. switch iface.Type {
  130. case "veth":
  131. istats, err := getNetworkInterfaceStats(iface.HostInterfaceName)
  132. if err != nil {
  133. return nil, err
  134. }
  135. stats.Interfaces = append(stats.Interfaces, istats)
  136. }
  137. }
  138. return &ResourceStats{
  139. Stats: stats,
  140. Read: now,
  141. MemoryLimit: memoryLimit,
  142. }, nil
  143. }