2014-01-15 22:36:13 +00:00
|
|
|
package sysinfo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
2015-04-23 03:11:23 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2014-05-14 22:21:44 +00:00
|
|
|
|
2015-03-26 22:22:04 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2014-06-11 02:58:15 +00:00
|
|
|
"github.com/docker/libcontainer/cgroups"
|
2014-01-15 22:36:13 +00:00
|
|
|
)
|
|
|
|
|
2015-03-27 03:05:07 +00:00
|
|
|
// SysInfo stores information about which features a kernel supports.
|
2014-01-15 22:36:13 +00:00
|
|
|
type SysInfo struct {
|
|
|
|
MemoryLimit bool
|
|
|
|
SwapLimit bool
|
2015-04-08 08:58:59 +00:00
|
|
|
CpuCfsPeriod bool
|
2015-04-20 15:16:47 +00:00
|
|
|
CpuCfsQuota bool
|
2014-01-15 22:36:13 +00:00
|
|
|
IPv4ForwardingDisabled bool
|
|
|
|
AppArmor bool
|
2015-02-26 11:53:55 +00:00
|
|
|
OomKillDisable bool
|
2014-01-15 22:36:13 +00:00
|
|
|
}
|
|
|
|
|
2015-03-27 17:55:22 +00:00
|
|
|
// New returns a new SysInfo, using the filesystem to detect which features the kernel supports.
|
2014-01-15 22:36:13 +00:00
|
|
|
func New(quiet bool) *SysInfo {
|
|
|
|
sysInfo := &SysInfo{}
|
|
|
|
if cgroupMemoryMountpoint, err := cgroups.FindCgroupMountpoint("memory"); err != nil {
|
|
|
|
if !quiet {
|
2015-04-15 00:16:00 +00:00
|
|
|
logrus.Warnf("Your kernel does not support cgroup memory limit: %v", err)
|
2014-01-15 22:36:13 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-04-15 00:16:00 +00:00
|
|
|
// If memory cgroup is mounted, MemoryLimit is always enabled.
|
|
|
|
sysInfo.MemoryLimit = true
|
2014-01-15 22:36:13 +00:00
|
|
|
|
2015-04-15 00:16:00 +00:00
|
|
|
_, err1 := ioutil.ReadFile(path.Join(cgroupMemoryMountpoint, "memory.memsw.limit_in_bytes"))
|
|
|
|
sysInfo.SwapLimit = err1 == nil
|
2014-01-15 22:36:13 +00:00
|
|
|
if !sysInfo.SwapLimit && !quiet {
|
2015-04-15 00:16:00 +00:00
|
|
|
logrus.Warn("Your kernel does not support swap memory limit.")
|
2014-01-15 22:36:13 +00:00
|
|
|
}
|
2015-02-26 11:53:55 +00:00
|
|
|
|
|
|
|
_, err = ioutil.ReadFile(path.Join(cgroupMemoryMountpoint, "memory.oom_control"))
|
|
|
|
sysInfo.OomKillDisable = err == nil
|
|
|
|
if !sysInfo.OomKillDisable && !quiet {
|
|
|
|
logrus.Warnf("Your kernel does not support oom control.")
|
|
|
|
}
|
2014-01-15 22:36:13 +00:00
|
|
|
}
|
|
|
|
|
2015-04-20 15:16:47 +00:00
|
|
|
if cgroupCpuMountpoint, err := cgroups.FindCgroupMountpoint("cpu"); err != nil {
|
|
|
|
if !quiet {
|
2015-04-22 00:15:00 +00:00
|
|
|
logrus.Warnf("%v", err)
|
2015-04-20 15:16:47 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-04-08 08:58:59 +00:00
|
|
|
_, err := ioutil.ReadFile(path.Join(cgroupCpuMountpoint, "cpu.cfs_period_us"))
|
|
|
|
sysInfo.CpuCfsPeriod = err == nil
|
|
|
|
if !sysInfo.CpuCfsPeriod && !quiet {
|
2015-05-12 10:02:30 +00:00
|
|
|
logrus.Warn("Your kernel does not support cgroup cfs period")
|
2015-04-08 08:58:59 +00:00
|
|
|
}
|
|
|
|
_, err = ioutil.ReadFile(path.Join(cgroupCpuMountpoint, "cpu.cfs_quota_us"))
|
|
|
|
sysInfo.CpuCfsQuota = err == nil
|
2015-04-20 15:16:47 +00:00
|
|
|
if !sysInfo.CpuCfsQuota && !quiet {
|
2015-04-22 00:15:00 +00:00
|
|
|
logrus.Warn("Your kernel does not support cgroup cfs quotas")
|
2015-04-20 15:16:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-23 03:11:23 +00:00
|
|
|
// Checek if ipv4_forward is disabled.
|
|
|
|
if data, err := ioutil.ReadFile("/proc/sys/net/ipv4/ip_forward"); os.IsNotExist(err) {
|
|
|
|
sysInfo.IPv4ForwardingDisabled = true
|
|
|
|
} else {
|
|
|
|
if enabled, _ := strconv.Atoi(strings.TrimSpace(string(data))); enabled == 0 {
|
|
|
|
sysInfo.IPv4ForwardingDisabled = true
|
|
|
|
} else {
|
|
|
|
sysInfo.IPv4ForwardingDisabled = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-27 03:22:05 +00:00
|
|
|
// Check if AppArmor is supported.
|
2014-01-15 22:36:13 +00:00
|
|
|
if _, err := os.Stat("/sys/kernel/security/apparmor"); os.IsNotExist(err) {
|
|
|
|
sysInfo.AppArmor = false
|
|
|
|
} else {
|
|
|
|
sysInfo.AppArmor = true
|
|
|
|
}
|
2015-04-14 00:38:34 +00:00
|
|
|
|
|
|
|
// Check if Devices cgroup is mounted, it is hard requirement for container security.
|
|
|
|
if _, err := cgroups.FindCgroupMountpoint("devices"); err != nil {
|
|
|
|
logrus.Fatalf("Error mounting devices cgroup: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-01-15 22:36:13 +00:00
|
|
|
return sysInfo
|
|
|
|
}
|