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
|
|
|
|
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-03-26 22:22:04 +00:00
|
|
|
logrus.Warnf("%s", 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-03-26 22:22:04 +00:00
|
|
|
logrus.Warnf("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-03-26 22:22:04 +00:00
|
|
|
logrus.Warnf("Your kernel does not support cgroup swap limit.")
|
2014-01-15 22:36:13 +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
|
|
|
|
}
|
|
|
|
return sysInfo
|
|
|
|
}
|