2014-01-15 22:36:13 +00:00
|
|
|
package sysinfo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
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-20 15:16:47 +00:00
|
|
|
CpuCfsQuota bool
|
2014-01-15 22:36:13 +00:00
|
|
|
IPv4ForwardingDisabled bool
|
|
|
|
AppArmor bool
|
|
|
|
}
|
|
|
|
|
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-22 00:15:00 +00:00
|
|
|
logrus.Warnf("%v", err)
|
2014-01-15 22:36:13 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_, err1 := ioutil.ReadFile(path.Join(cgroupMemoryMountpoint, "memory.limit_in_bytes"))
|
|
|
|
_, err2 := ioutil.ReadFile(path.Join(cgroupMemoryMountpoint, "memory.soft_limit_in_bytes"))
|
|
|
|
sysInfo.MemoryLimit = err1 == nil && err2 == nil
|
|
|
|
if !sysInfo.MemoryLimit && !quiet {
|
2015-04-22 00:15:00 +00:00
|
|
|
logrus.Warn("Your kernel does not support cgroup memory limit.")
|
2014-01-15 22:36:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = ioutil.ReadFile(path.Join(cgroupMemoryMountpoint, "memory.memsw.limit_in_bytes"))
|
|
|
|
sysInfo.SwapLimit = err == nil
|
|
|
|
if !sysInfo.SwapLimit && !quiet {
|
2015-04-22 00:15:00 +00:00
|
|
|
logrus.Warn("Your kernel does not support cgroup swap limit.")
|
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 {
|
|
|
|
_, err1 := ioutil.ReadFile(path.Join(cgroupCpuMountpoint, "cpu.cfs_quota_us"))
|
|
|
|
sysInfo.CpuCfsQuota = err1 == nil
|
|
|
|
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-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
|
|
|
|
}
|