github.com/carlanton/docker@v1.8.0-rc1/pkg/sysinfo/sysinfo_linux.go (about)

     1  package sysinfo
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path"
     7  	"strings"
     8  
     9  	"github.com/Sirupsen/logrus"
    10  	"github.com/opencontainers/runc/libcontainer/cgroups"
    11  )
    12  
    13  // New returns a new SysInfo, using the filesystem to detect which features the kernel supports.
    14  func New(quiet bool) *SysInfo {
    15  	sysInfo := &SysInfo{}
    16  	sysInfo.cgroupMemInfo = checkCgroupMem(quiet)
    17  	sysInfo.cgroupCpuInfo = checkCgroupCpu(quiet)
    18  
    19  	_, err := cgroups.FindCgroupMountpoint("devices")
    20  	sysInfo.CgroupDevicesEnabled = err == nil
    21  
    22  	sysInfo.IPv4ForwardingDisabled = !readProcBool("/proc/sys/net/ipv4/ip_forward")
    23  	sysInfo.BridgeNfCallIptablesDisabled = !readProcBool("/proc/sys/net/bridge/bridge-nf-call-iptables")
    24  	sysInfo.BridgeNfCallIp6tablesDisabled = !readProcBool("/proc/sys/net/bridge/bridge-nf-call-ip6tables")
    25  
    26  	// Check if AppArmor is supported.
    27  	if _, err := os.Stat("/sys/kernel/security/apparmor"); !os.IsNotExist(err) {
    28  		sysInfo.AppArmor = true
    29  	}
    30  
    31  	return sysInfo
    32  }
    33  
    34  func checkCgroupMem(quiet bool) *cgroupMemInfo {
    35  	info := &cgroupMemInfo{}
    36  	mountPoint, err := cgroups.FindCgroupMountpoint("memory")
    37  	if err != nil {
    38  		if !quiet {
    39  			logrus.Warnf("Your kernel does not support cgroup memory limit: %v", err)
    40  		}
    41  		return info
    42  	}
    43  	info.MemoryLimit = true
    44  
    45  	info.SwapLimit = cgroupEnabled(mountPoint, "memory.memsw.limit_in_bytes")
    46  	if !quiet && !info.SwapLimit {
    47  		logrus.Warn("Your kernel does not support swap memory limit.")
    48  	}
    49  	info.OomKillDisable = cgroupEnabled(mountPoint, "memory.oom_control")
    50  	if !quiet && !info.OomKillDisable {
    51  		logrus.Warnf("Your kernel does not support oom control.")
    52  	}
    53  	info.MemorySwappiness = cgroupEnabled(mountPoint, "memory.swappiness")
    54  	if !quiet && !info.MemorySwappiness {
    55  		logrus.Warnf("Your kernel does not support memory swappiness.")
    56  	}
    57  
    58  	return info
    59  }
    60  
    61  func checkCgroupCpu(quiet bool) *cgroupCpuInfo {
    62  	info := &cgroupCpuInfo{}
    63  	mountPoint, err := cgroups.FindCgroupMountpoint("cpu")
    64  	if err != nil {
    65  		if !quiet {
    66  			logrus.Warn(err)
    67  		}
    68  		return info
    69  	}
    70  
    71  	info.CpuCfsPeriod = cgroupEnabled(mountPoint, "cpu.cfs_period_us")
    72  	if !quiet && !info.CpuCfsPeriod {
    73  		logrus.Warn("Your kernel does not support cgroup cfs period")
    74  	}
    75  
    76  	info.CpuCfsQuota = cgroupEnabled(mountPoint, "cpu.cfs_quota_us")
    77  	if !quiet && !info.CpuCfsQuota {
    78  		logrus.Warn("Your kernel does not support cgroup cfs quotas")
    79  	}
    80  	return info
    81  }
    82  
    83  func cgroupEnabled(mountPoint, name string) bool {
    84  	_, err := os.Stat(path.Join(mountPoint, name))
    85  	return err == nil
    86  }
    87  
    88  func readProcBool(path string) bool {
    89  	val, err := ioutil.ReadFile(path)
    90  	if err != nil {
    91  		return false
    92  	}
    93  	return strings.TrimSpace(string(val)) == "1"
    94  }