2018-02-05 16:05:59 -05:00
|
|
|
package sysinfo // import "github.com/docker/docker/pkg/sysinfo"
|
2015-05-14 12:05:14 -04:00
|
|
|
|
|
|
|
import (
|
2016-01-20 22:20:59 -05:00
|
|
|
"fmt"
|
2015-05-14 12:05:14 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
2015-07-16 19:00:55 -04:00
|
|
|
"github.com/opencontainers/runc/libcontainer/cgroups"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-05-23 10:22:32 -04:00
|
|
|
"golang.org/x/sys/unix"
|
2015-05-14 12:05:14 -04:00
|
|
|
)
|
|
|
|
|
2016-01-20 22:20:59 -05:00
|
|
|
func findCgroupMountpoints() (map[string]string, error) {
|
2016-09-29 14:02:38 -04:00
|
|
|
cgMounts, err := cgroups.GetCgroupMounts(false)
|
2016-01-20 22:20:59 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to parse cgroup information: %v", err)
|
|
|
|
}
|
|
|
|
mps := make(map[string]string)
|
|
|
|
for _, m := range cgMounts {
|
|
|
|
for _, ss := range m.Subsystems {
|
|
|
|
mps[ss] = m.Mountpoint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mps, nil
|
|
|
|
}
|
|
|
|
|
2019-02-03 14:51:04 -05:00
|
|
|
type infoCollector func(info *SysInfo, cgMounts map[string]string) (warnings []string)
|
|
|
|
|
2015-08-06 07:54:48 -04:00
|
|
|
// New returns a new SysInfo, using the filesystem to detect which features
|
|
|
|
// the kernel supports. If `quiet` is `false` warnings are printed in logs
|
|
|
|
// whenever an error occurs or misconfigurations are present.
|
2015-05-14 12:05:14 -04:00
|
|
|
func New(quiet bool) *SysInfo {
|
2019-02-03 14:51:04 -05:00
|
|
|
var ops []infoCollector
|
|
|
|
var warnings []string
|
2015-05-14 12:05:14 -04:00
|
|
|
sysInfo := &SysInfo{}
|
2016-01-20 22:20:59 -05:00
|
|
|
cgMounts, err := findCgroupMountpoints()
|
|
|
|
if err != nil {
|
2019-02-03 14:51:04 -05:00
|
|
|
logrus.Warn(err)
|
2016-01-20 22:20:59 -05:00
|
|
|
} else {
|
2019-02-03 14:51:04 -05:00
|
|
|
ops = append(ops, []infoCollector{
|
|
|
|
applyMemoryCgroupInfo,
|
|
|
|
applyCPUCgroupInfo,
|
|
|
|
applyBlkioCgroupInfo,
|
|
|
|
applyCPUSetCgroupInfo,
|
|
|
|
applyPIDSCgroupInfo,
|
|
|
|
applyDevicesCgroupInfo,
|
|
|
|
}...)
|
|
|
|
}
|
|
|
|
|
|
|
|
ops = append(ops, []infoCollector{
|
|
|
|
applyNetworkingInfo,
|
|
|
|
applyAppArmorInfo,
|
|
|
|
applySeccompInfo,
|
2018-12-14 18:07:19 -05:00
|
|
|
applyCgroupNsInfo,
|
2019-02-03 14:51:04 -05:00
|
|
|
}...)
|
|
|
|
|
|
|
|
for _, o := range ops {
|
|
|
|
w := o(sysInfo, cgMounts)
|
|
|
|
warnings = append(warnings, w...)
|
|
|
|
}
|
|
|
|
if !quiet {
|
|
|
|
for _, w := range warnings {
|
|
|
|
logrus.Warn(w)
|
2018-10-15 03:52:53 -04:00
|
|
|
}
|
2015-05-14 12:05:14 -04:00
|
|
|
}
|
2015-06-16 22:36:20 -04:00
|
|
|
return sysInfo
|
|
|
|
}
|
|
|
|
|
2019-02-03 14:51:04 -05:00
|
|
|
// applyMemoryCgroupInfo reads the memory information from the memory cgroup mount point.
|
|
|
|
func applyMemoryCgroupInfo(info *SysInfo, cgMounts map[string]string) []string {
|
|
|
|
var warnings []string
|
2016-01-20 22:20:59 -05:00
|
|
|
mountPoint, ok := cgMounts["memory"]
|
|
|
|
if !ok {
|
2019-02-03 14:51:04 -05:00
|
|
|
warnings = append(warnings, "Your kernel does not support cgroup memory limit")
|
|
|
|
return warnings
|
2015-05-14 12:05:14 -04:00
|
|
|
}
|
2019-02-03 14:51:04 -05:00
|
|
|
info.MemoryLimit = ok
|
2015-05-14 12:05:14 -04:00
|
|
|
|
2019-02-03 14:51:04 -05:00
|
|
|
info.SwapLimit = cgroupEnabled(mountPoint, "memory.memsw.limit_in_bytes")
|
|
|
|
if !info.SwapLimit {
|
|
|
|
warnings = append(warnings, "Your kernel does not support swap memory limit")
|
2015-05-14 12:05:14 -04:00
|
|
|
}
|
2019-02-03 14:51:04 -05:00
|
|
|
info.MemoryReservation = cgroupEnabled(mountPoint, "memory.soft_limit_in_bytes")
|
|
|
|
if !info.MemoryReservation {
|
|
|
|
warnings = append(warnings, "Your kernel does not support memory reservation")
|
2015-09-23 02:02:45 -04:00
|
|
|
}
|
2019-02-03 14:51:04 -05:00
|
|
|
info.OomKillDisable = cgroupEnabled(mountPoint, "memory.oom_control")
|
|
|
|
if !info.OomKillDisable {
|
|
|
|
warnings = append(warnings, "Your kernel does not support oom control")
|
2015-06-16 21:19:11 -04:00
|
|
|
}
|
2019-02-03 14:51:04 -05:00
|
|
|
info.MemorySwappiness = cgroupEnabled(mountPoint, "memory.swappiness")
|
|
|
|
if !info.MemorySwappiness {
|
|
|
|
warnings = append(warnings, "Your kernel does not support memory swappiness")
|
2015-07-12 03:46:33 -04:00
|
|
|
}
|
2019-02-03 14:51:04 -05:00
|
|
|
info.KernelMemory = cgroupEnabled(mountPoint, "memory.kmem.limit_in_bytes")
|
|
|
|
if !info.KernelMemory {
|
|
|
|
warnings = append(warnings, "Your kernel does not support kernel memory limit")
|
2015-08-19 11:56:55 -04:00
|
|
|
}
|
2019-02-03 14:51:04 -05:00
|
|
|
info.KernelMemoryTCP = cgroupEnabled(mountPoint, "memory.kmem.tcp.limit_in_bytes")
|
|
|
|
if !info.KernelMemoryTCP {
|
|
|
|
warnings = append(warnings, "Your kernel does not support kernel memory TCP limit")
|
2018-05-11 15:46:11 -04:00
|
|
|
}
|
2015-06-16 22:36:20 -04:00
|
|
|
|
2019-02-03 14:51:04 -05:00
|
|
|
return warnings
|
2015-06-16 22:36:20 -04:00
|
|
|
}
|
|
|
|
|
2019-02-03 14:51:04 -05:00
|
|
|
// applyCPUCgroupInfo reads the cpu information from the cpu cgroup mount point.
|
|
|
|
func applyCPUCgroupInfo(info *SysInfo, cgMounts map[string]string) []string {
|
|
|
|
var warnings []string
|
2016-01-20 22:20:59 -05:00
|
|
|
mountPoint, ok := cgMounts["cpu"]
|
|
|
|
if !ok {
|
2019-02-03 14:51:04 -05:00
|
|
|
warnings = append(warnings, "Unable to find cpu cgroup in mounts")
|
|
|
|
return warnings
|
2015-06-16 21:19:11 -04:00
|
|
|
}
|
|
|
|
|
2019-02-03 14:51:04 -05:00
|
|
|
info.CPUShares = cgroupEnabled(mountPoint, "cpu.shares")
|
|
|
|
if !info.CPUShares {
|
|
|
|
warnings = append(warnings, "Your kernel does not support cgroup cpu shares")
|
2015-08-05 10:35:18 -04:00
|
|
|
}
|
|
|
|
|
2019-02-03 14:51:04 -05:00
|
|
|
info.CPUCfsPeriod = cgroupEnabled(mountPoint, "cpu.cfs_period_us")
|
|
|
|
if !info.CPUCfsPeriod {
|
|
|
|
warnings = append(warnings, "Your kernel does not support cgroup cfs period")
|
2015-05-14 12:05:14 -04:00
|
|
|
}
|
|
|
|
|
2019-02-03 14:51:04 -05:00
|
|
|
info.CPUCfsQuota = cgroupEnabled(mountPoint, "cpu.cfs_quota_us")
|
|
|
|
if !info.CPUCfsQuota {
|
|
|
|
warnings = append(warnings, "Your kernel does not support cgroup cfs quotas")
|
2015-05-14 12:05:14 -04:00
|
|
|
}
|
2016-06-07 15:05:43 -04:00
|
|
|
|
2019-02-03 14:51:04 -05:00
|
|
|
info.CPURealtimePeriod = cgroupEnabled(mountPoint, "cpu.rt_period_us")
|
|
|
|
if !info.CPURealtimePeriod {
|
|
|
|
warnings = append(warnings, "Your kernel does not support cgroup rt period")
|
2016-06-07 15:05:43 -04:00
|
|
|
}
|
|
|
|
|
2019-02-03 14:51:04 -05:00
|
|
|
info.CPURealtimeRuntime = cgroupEnabled(mountPoint, "cpu.rt_runtime_us")
|
|
|
|
if !info.CPURealtimeRuntime {
|
|
|
|
warnings = append(warnings, "Your kernel does not support cgroup rt runtime")
|
2016-06-07 15:05:43 -04:00
|
|
|
}
|
|
|
|
|
2019-02-03 14:51:04 -05:00
|
|
|
return warnings
|
2015-06-16 22:36:20 -04:00
|
|
|
}
|
2015-05-14 12:05:14 -04:00
|
|
|
|
2019-02-03 14:51:04 -05:00
|
|
|
// applyBlkioCgroupInfo reads the blkio information from the blkio cgroup mount point.
|
|
|
|
func applyBlkioCgroupInfo(info *SysInfo, cgMounts map[string]string) []string {
|
|
|
|
var warnings []string
|
2016-01-20 22:20:59 -05:00
|
|
|
mountPoint, ok := cgMounts["blkio"]
|
|
|
|
if !ok {
|
2019-02-03 14:51:04 -05:00
|
|
|
warnings = append(warnings, "Unable to find blkio cgroup in mounts")
|
|
|
|
return warnings
|
2015-08-05 10:35:18 -04:00
|
|
|
}
|
|
|
|
|
2019-02-03 14:51:04 -05:00
|
|
|
info.BlkioWeight = cgroupEnabled(mountPoint, "blkio.weight")
|
|
|
|
if !info.BlkioWeight {
|
|
|
|
warnings = append(warnings, "Your kernel does not support cgroup blkio weight")
|
2015-08-05 10:35:18 -04:00
|
|
|
}
|
2015-06-11 20:34:20 -04:00
|
|
|
|
2019-02-03 14:51:04 -05:00
|
|
|
info.BlkioWeightDevice = cgroupEnabled(mountPoint, "blkio.weight_device")
|
|
|
|
if !info.BlkioWeightDevice {
|
|
|
|
warnings = append(warnings, "Your kernel does not support cgroup blkio weight_device")
|
2015-06-11 20:34:20 -04:00
|
|
|
}
|
2015-07-08 07:06:48 -04:00
|
|
|
|
2019-02-03 14:51:04 -05:00
|
|
|
info.BlkioReadBpsDevice = cgroupEnabled(mountPoint, "blkio.throttle.read_bps_device")
|
|
|
|
if !info.BlkioReadBpsDevice {
|
|
|
|
warnings = append(warnings, "Your kernel does not support cgroup blkio throttle.read_bps_device")
|
2015-07-08 07:06:48 -04:00
|
|
|
}
|
|
|
|
|
2019-02-03 14:51:04 -05:00
|
|
|
info.BlkioWriteBpsDevice = cgroupEnabled(mountPoint, "blkio.throttle.write_bps_device")
|
|
|
|
if !info.BlkioWriteBpsDevice {
|
|
|
|
warnings = append(warnings, "Your kernel does not support cgroup blkio throttle.write_bps_device")
|
2015-07-08 07:06:48 -04:00
|
|
|
}
|
2019-02-03 14:51:04 -05:00
|
|
|
info.BlkioReadIOpsDevice = cgroupEnabled(mountPoint, "blkio.throttle.read_iops_device")
|
|
|
|
if !info.BlkioReadIOpsDevice {
|
|
|
|
warnings = append(warnings, "Your kernel does not support cgroup blkio throttle.read_iops_device")
|
2015-07-08 07:06:48 -04:00
|
|
|
}
|
|
|
|
|
2019-02-03 14:51:04 -05:00
|
|
|
info.BlkioWriteIOpsDevice = cgroupEnabled(mountPoint, "blkio.throttle.write_iops_device")
|
|
|
|
if !info.BlkioWriteIOpsDevice {
|
|
|
|
warnings = append(warnings, "Your kernel does not support cgroup blkio throttle.write_iops_device")
|
2015-06-11 20:34:20 -04:00
|
|
|
}
|
2019-02-03 14:51:04 -05:00
|
|
|
|
|
|
|
return warnings
|
2015-08-05 10:35:18 -04:00
|
|
|
}
|
|
|
|
|
2019-02-03 14:51:04 -05:00
|
|
|
// applyCPUSetCgroupInfo reads the cpuset information from the cpuset cgroup mount point.
|
|
|
|
func applyCPUSetCgroupInfo(info *SysInfo, cgMounts map[string]string) []string {
|
|
|
|
var warnings []string
|
2016-01-20 22:20:59 -05:00
|
|
|
mountPoint, ok := cgMounts["cpuset"]
|
|
|
|
if !ok {
|
2019-02-03 14:51:04 -05:00
|
|
|
warnings = append(warnings, "Unable to find cpuset cgroup in mounts")
|
|
|
|
return warnings
|
2015-08-05 10:35:18 -04:00
|
|
|
}
|
2019-02-03 14:51:04 -05:00
|
|
|
info.Cpuset = ok
|
|
|
|
|
|
|
|
var err error
|
2015-08-05 10:35:18 -04:00
|
|
|
|
2015-09-08 14:40:55 -04:00
|
|
|
cpus, err := ioutil.ReadFile(path.Join(mountPoint, "cpuset.cpus"))
|
|
|
|
if err != nil {
|
2019-02-03 14:51:04 -05:00
|
|
|
return warnings
|
2015-09-08 14:40:55 -04:00
|
|
|
}
|
2019-02-03 14:51:04 -05:00
|
|
|
info.Cpus = strings.TrimSpace(string(cpus))
|
2015-09-08 14:40:55 -04:00
|
|
|
|
|
|
|
mems, err := ioutil.ReadFile(path.Join(mountPoint, "cpuset.mems"))
|
|
|
|
if err != nil {
|
2019-02-03 14:51:04 -05:00
|
|
|
return warnings
|
2015-09-08 14:40:55 -04:00
|
|
|
}
|
2019-02-03 14:51:04 -05:00
|
|
|
info.Mems = strings.TrimSpace(string(mems))
|
2015-09-08 14:40:55 -04:00
|
|
|
|
2019-02-03 14:51:04 -05:00
|
|
|
return warnings
|
2015-08-05 10:35:18 -04:00
|
|
|
}
|
|
|
|
|
2019-02-03 14:51:04 -05:00
|
|
|
// applyPIDSCgroupInfo reads the pids information from the pids cgroup mount point.
|
|
|
|
func applyPIDSCgroupInfo(info *SysInfo, _ map[string]string) []string {
|
|
|
|
var warnings []string
|
2018-12-06 13:49:51 -05:00
|
|
|
_, err := cgroups.FindCgroupMountpoint("", "pids")
|
2015-12-15 14:15:43 -05:00
|
|
|
if err != nil {
|
2019-02-03 14:51:04 -05:00
|
|
|
warnings = append(warnings, err.Error())
|
|
|
|
return warnings
|
|
|
|
}
|
|
|
|
info.PidsLimit = true
|
|
|
|
return warnings
|
|
|
|
}
|
|
|
|
|
|
|
|
// applyDevicesCgroupInfo reads the pids information from the devices cgroup mount point.
|
|
|
|
func applyDevicesCgroupInfo(info *SysInfo, cgMounts map[string]string) []string {
|
|
|
|
var warnings []string
|
|
|
|
_, ok := cgMounts["devices"]
|
|
|
|
info.CgroupDevicesEnabled = ok
|
|
|
|
return warnings
|
|
|
|
}
|
|
|
|
|
|
|
|
// applyNetworkingInfo adds networking information to the info.
|
|
|
|
func applyNetworkingInfo(info *SysInfo, _ map[string]string) []string {
|
|
|
|
var warnings []string
|
|
|
|
info.IPv4ForwardingDisabled = !readProcBool("/proc/sys/net/ipv4/ip_forward")
|
|
|
|
info.BridgeNFCallIPTablesDisabled = !readProcBool("/proc/sys/net/bridge/bridge-nf-call-iptables")
|
|
|
|
info.BridgeNFCallIP6TablesDisabled = !readProcBool("/proc/sys/net/bridge/bridge-nf-call-ip6tables")
|
|
|
|
return warnings
|
|
|
|
}
|
|
|
|
|
|
|
|
// applyAppArmorInfo adds AppArmor information to the info.
|
|
|
|
func applyAppArmorInfo(info *SysInfo, _ map[string]string) []string {
|
|
|
|
var warnings []string
|
|
|
|
if _, err := os.Stat("/sys/kernel/security/apparmor"); !os.IsNotExist(err) {
|
|
|
|
if _, err := ioutil.ReadFile("/sys/kernel/security/apparmor/profiles"); err == nil {
|
|
|
|
info.AppArmor = true
|
2015-12-15 14:15:43 -05:00
|
|
|
}
|
|
|
|
}
|
2019-02-03 14:51:04 -05:00
|
|
|
return warnings
|
|
|
|
}
|
2015-12-15 14:15:43 -05:00
|
|
|
|
2018-12-14 18:07:19 -05:00
|
|
|
// applyCgroupNsInfo adds cgroup namespace information to the info.
|
|
|
|
func applyCgroupNsInfo(info *SysInfo, _ map[string]string) []string {
|
|
|
|
var warnings []string
|
|
|
|
if _, err := os.Stat("/proc/self/ns/cgroup"); !os.IsNotExist(err) {
|
|
|
|
info.CgroupNamespaces = true
|
|
|
|
}
|
|
|
|
return warnings
|
|
|
|
}
|
|
|
|
|
2019-02-03 14:51:04 -05:00
|
|
|
// applySeccompInfo checks if Seccomp is supported, via CONFIG_SECCOMP.
|
|
|
|
func applySeccompInfo(info *SysInfo, _ map[string]string) []string {
|
|
|
|
var warnings []string
|
|
|
|
// Check if Seccomp is supported, via CONFIG_SECCOMP.
|
|
|
|
if err := unix.Prctl(unix.PR_GET_SECCOMP, 0, 0, 0, 0); err != unix.EINVAL {
|
|
|
|
// Make sure the kernel has CONFIG_SECCOMP_FILTER.
|
|
|
|
if err := unix.Prctl(unix.PR_SET_SECCOMP, unix.SECCOMP_MODE_FILTER, 0, 0, 0); err != unix.EINVAL {
|
|
|
|
info.Seccomp = true
|
|
|
|
}
|
2015-12-15 14:15:43 -05:00
|
|
|
}
|
2019-02-03 14:51:04 -05:00
|
|
|
return warnings
|
2015-12-15 14:15:43 -05:00
|
|
|
}
|
|
|
|
|
2015-06-16 22:36:20 -04:00
|
|
|
func cgroupEnabled(mountPoint, name string) bool {
|
|
|
|
_, err := os.Stat(path.Join(mountPoint, name))
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func readProcBool(path string) bool {
|
|
|
|
val, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(string(val)) == "1"
|
2015-05-14 12:05:14 -04:00
|
|
|
}
|