driver_unix.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. // +build !windows
  2. package execdriver
  3. import (
  4. "encoding/json"
  5. "io/ioutil"
  6. "os"
  7. "path/filepath"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "github.com/docker/docker/daemon/execdriver/native/template"
  12. "github.com/docker/docker/pkg/idtools"
  13. "github.com/docker/docker/pkg/mount"
  14. "github.com/docker/docker/pkg/ulimit"
  15. "github.com/opencontainers/runc/libcontainer"
  16. "github.com/opencontainers/runc/libcontainer/cgroups/fs"
  17. "github.com/opencontainers/runc/libcontainer/configs"
  18. blkiodev "github.com/opencontainers/runc/libcontainer/configs"
  19. )
  20. // Mount contains information for a mount operation.
  21. type Mount struct {
  22. Source string `json:"source"`
  23. Destination string `json:"destination"`
  24. Writable bool `json:"writable"`
  25. Private bool `json:"private"`
  26. Slave bool `json:"slave"`
  27. Data string `json:"data"`
  28. }
  29. // Resources contains all resource configs for a driver.
  30. // Currently these are all for cgroup configs.
  31. type Resources struct {
  32. CommonResources
  33. // Fields below here are platform specific
  34. BlkioWeightDevice []*blkiodev.WeightDevice `json:"blkio_weight_device"`
  35. BlkioThrottleReadBpsDevice []*blkiodev.ThrottleDevice `json:"blkio_throttle_read_bps_device"`
  36. BlkioThrottleWriteBpsDevice []*blkiodev.ThrottleDevice `json:"blkio_throttle_write_bps_device"`
  37. MemorySwap int64 `json:"memory_swap"`
  38. KernelMemory int64 `json:"kernel_memory"`
  39. CPUQuota int64 `json:"cpu_quota"`
  40. CpusetCpus string `json:"cpuset_cpus"`
  41. CpusetMems string `json:"cpuset_mems"`
  42. CPUPeriod int64 `json:"cpu_period"`
  43. Rlimits []*ulimit.Rlimit `json:"rlimits"`
  44. OomKillDisable bool `json:"oom_kill_disable"`
  45. MemorySwappiness int64 `json:"memory_swappiness"`
  46. }
  47. // ProcessConfig is the platform specific structure that describes a process
  48. // that will be run inside a container.
  49. type ProcessConfig struct {
  50. CommonProcessConfig
  51. // Fields below here are platform specific
  52. Privileged bool `json:"privileged"`
  53. User string `json:"user"`
  54. Console string `json:"-"` // dev/console path
  55. }
  56. // Ipc settings of the container
  57. // It is for IPC namespace setting. Usually different containers
  58. // have their own IPC namespace, however this specifies to use
  59. // an existing IPC namespace.
  60. // You can join the host's or a container's IPC namespace.
  61. type Ipc struct {
  62. ContainerID string `json:"container_id"` // id of the container to join ipc.
  63. HostIpc bool `json:"host_ipc"`
  64. }
  65. // Pid settings of the container
  66. // It is for PID namespace setting. Usually different containers
  67. // have their own PID namespace, however this specifies to use
  68. // an existing PID namespace.
  69. // Joining the host's PID namespace is currently the only supported
  70. // option.
  71. type Pid struct {
  72. HostPid bool `json:"host_pid"`
  73. }
  74. // UTS settings of the container
  75. // It is for UTS namespace setting. Usually different containers
  76. // have their own UTS namespace, however this specifies to use
  77. // an existing UTS namespace.
  78. // Joining the host's UTS namespace is currently the only supported
  79. // option.
  80. type UTS struct {
  81. HostUTS bool `json:"host_uts"`
  82. }
  83. // Network settings of the container
  84. type Network struct {
  85. Mtu int `json:"mtu"`
  86. ContainerID string `json:"container_id"` // id of the container to join network.
  87. NamespacePath string `json:"namespace_path"`
  88. HostNetworking bool `json:"host_networking"`
  89. }
  90. // Command wraps an os/exec.Cmd to add more metadata
  91. type Command struct {
  92. CommonCommand
  93. // Fields below here are platform specific
  94. AllowedDevices []*configs.Device `json:"allowed_devices"`
  95. AppArmorProfile string `json:"apparmor_profile"`
  96. AutoCreatedDevices []*configs.Device `json:"autocreated_devices"`
  97. CapAdd []string `json:"cap_add"`
  98. CapDrop []string `json:"cap_drop"`
  99. CgroupParent string `json:"cgroup_parent"` // The parent cgroup for this command.
  100. GIDMapping []idtools.IDMap `json:"gidmapping"`
  101. GroupAdd []string `json:"group_add"`
  102. Ipc *Ipc `json:"ipc"`
  103. OomScoreAdj int `json:"oom_score_adj"`
  104. Pid *Pid `json:"pid"`
  105. ReadonlyRootfs bool `json:"readonly_rootfs"`
  106. RemappedRoot *User `json:"remap_root"`
  107. SeccompProfile string `json:"seccomp_profile"`
  108. UIDMapping []idtools.IDMap `json:"uidmapping"`
  109. UTS *UTS `json:"uts"`
  110. }
  111. // InitContainer is the initialization of a container config.
  112. // It returns the initial configs for a container. It's mostly
  113. // defined by the default template.
  114. func InitContainer(c *Command) *configs.Config {
  115. container := template.New()
  116. container.Hostname = getEnv("HOSTNAME", c.ProcessConfig.Env)
  117. container.Cgroups.Name = c.ID
  118. container.Cgroups.AllowedDevices = c.AllowedDevices
  119. container.Devices = c.AutoCreatedDevices
  120. container.Rootfs = c.Rootfs
  121. container.Readonlyfs = c.ReadonlyRootfs
  122. container.RootPropagation = mount.RPRIVATE
  123. // check to see if we are running in ramdisk to disable pivot root
  124. container.NoPivotRoot = os.Getenv("DOCKER_RAMDISK") != ""
  125. // Default parent cgroup is "docker". Override if required.
  126. if c.CgroupParent != "" {
  127. container.Cgroups.Parent = c.CgroupParent
  128. }
  129. return container
  130. }
  131. func getEnv(key string, env []string) string {
  132. for _, pair := range env {
  133. parts := strings.SplitN(pair, "=", 2)
  134. if parts[0] == key {
  135. return parts[1]
  136. }
  137. }
  138. return ""
  139. }
  140. // SetupCgroups setups cgroup resources for a container.
  141. func SetupCgroups(container *configs.Config, c *Command) error {
  142. if c.Resources != nil {
  143. container.Cgroups.CpuShares = c.Resources.CPUShares
  144. container.Cgroups.Memory = c.Resources.Memory
  145. container.Cgroups.MemoryReservation = c.Resources.MemoryReservation
  146. container.Cgroups.MemorySwap = c.Resources.MemorySwap
  147. container.Cgroups.KernelMemory = c.Resources.KernelMemory
  148. container.Cgroups.CpusetCpus = c.Resources.CpusetCpus
  149. container.Cgroups.CpusetMems = c.Resources.CpusetMems
  150. container.Cgroups.CpuPeriod = c.Resources.CPUPeriod
  151. container.Cgroups.CpuQuota = c.Resources.CPUQuota
  152. container.Cgroups.BlkioWeight = c.Resources.BlkioWeight
  153. container.Cgroups.BlkioWeightDevice = c.Resources.BlkioWeightDevice
  154. container.Cgroups.BlkioThrottleReadBpsDevice = c.Resources.BlkioThrottleReadBpsDevice
  155. container.Cgroups.BlkioThrottleWriteBpsDevice = c.Resources.BlkioThrottleWriteBpsDevice
  156. container.Cgroups.OomKillDisable = c.Resources.OomKillDisable
  157. container.Cgroups.MemorySwappiness = c.Resources.MemorySwappiness
  158. }
  159. return nil
  160. }
  161. // Returns the network statistics for the network interfaces represented by the NetworkRuntimeInfo.
  162. func getNetworkInterfaceStats(interfaceName string) (*libcontainer.NetworkInterface, error) {
  163. out := &libcontainer.NetworkInterface{Name: interfaceName}
  164. // This can happen if the network runtime information is missing - possible if the
  165. // container was created by an old version of libcontainer.
  166. if interfaceName == "" {
  167. return out, nil
  168. }
  169. type netStatsPair struct {
  170. // Where to write the output.
  171. Out *uint64
  172. // The network stats file to read.
  173. File string
  174. }
  175. // 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.
  176. netStats := []netStatsPair{
  177. {Out: &out.RxBytes, File: "tx_bytes"},
  178. {Out: &out.RxPackets, File: "tx_packets"},
  179. {Out: &out.RxErrors, File: "tx_errors"},
  180. {Out: &out.RxDropped, File: "tx_dropped"},
  181. {Out: &out.TxBytes, File: "rx_bytes"},
  182. {Out: &out.TxPackets, File: "rx_packets"},
  183. {Out: &out.TxErrors, File: "rx_errors"},
  184. {Out: &out.TxDropped, File: "rx_dropped"},
  185. }
  186. for _, netStat := range netStats {
  187. data, err := readSysfsNetworkStats(interfaceName, netStat.File)
  188. if err != nil {
  189. return nil, err
  190. }
  191. *(netStat.Out) = data
  192. }
  193. return out, nil
  194. }
  195. // Reads the specified statistics available under /sys/class/net/<EthInterface>/statistics
  196. func readSysfsNetworkStats(ethInterface, statsFile string) (uint64, error) {
  197. data, err := ioutil.ReadFile(filepath.Join("/sys/class/net", ethInterface, "statistics", statsFile))
  198. if err != nil {
  199. return 0, err
  200. }
  201. return strconv.ParseUint(strings.TrimSpace(string(data)), 10, 64)
  202. }
  203. // Stats collects all the resource usage information from a container.
  204. func Stats(containerDir string, containerMemoryLimit int64, machineMemory int64) (*ResourceStats, error) {
  205. f, err := os.Open(filepath.Join(containerDir, "state.json"))
  206. if err != nil {
  207. return nil, err
  208. }
  209. defer f.Close()
  210. type network struct {
  211. Type string
  212. HostInterfaceName string
  213. }
  214. state := struct {
  215. CgroupPaths map[string]string `json:"cgroup_paths"`
  216. Networks []network
  217. }{}
  218. if err := json.NewDecoder(f).Decode(&state); err != nil {
  219. return nil, err
  220. }
  221. now := time.Now()
  222. mgr := fs.Manager{Paths: state.CgroupPaths}
  223. cstats, err := mgr.GetStats()
  224. if err != nil {
  225. return nil, err
  226. }
  227. stats := &libcontainer.Stats{CgroupStats: cstats}
  228. // if the container does not have any memory limit specified set the
  229. // limit to the machines memory
  230. memoryLimit := containerMemoryLimit
  231. if memoryLimit == 0 {
  232. memoryLimit = machineMemory
  233. }
  234. for _, iface := range state.Networks {
  235. switch iface.Type {
  236. case "veth":
  237. istats, err := getNetworkInterfaceStats(iface.HostInterfaceName)
  238. if err != nil {
  239. return nil, err
  240. }
  241. stats.Interfaces = append(stats.Interfaces, istats)
  242. }
  243. }
  244. return &ResourceStats{
  245. Stats: stats,
  246. Read: now,
  247. MemoryLimit: memoryLimit,
  248. }, nil
  249. }
  250. // User contains the uid and gid representing a Unix user
  251. type User struct {
  252. UID int `json:"root_uid"`
  253. GID int `json:"root_gid"`
  254. }
  255. // ExitStatus provides exit reasons for a container.
  256. type ExitStatus struct {
  257. // The exit code with which the container exited.
  258. ExitCode int
  259. // Whether the container encountered an OOM.
  260. OOMKilled bool
  261. }