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
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
2022-03-07 18:07:23 -05:00
|
|
|
"sync"
|
2015-05-14 12:05:14 -04:00
|
|
|
|
2022-03-07 18:07:23 -05:00
|
|
|
"github.com/containerd/cgroups"
|
|
|
|
"github.com/containerd/containerd/pkg/seccomp"
|
|
|
|
"github.com/moby/sys/mountinfo"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2015-05-14 12:05:14 -04:00
|
|
|
)
|
|
|
|
|
2022-03-07 18:07:23 -05:00
|
|
|
var (
|
|
|
|
readMountinfoOnce sync.Once
|
|
|
|
readMountinfoErr error
|
|
|
|
cgroupMountinfo []*mountinfo.Info
|
|
|
|
)
|
|
|
|
|
|
|
|
// readCgroupMountinfo returns a list of cgroup v1 mounts (i.e. the ones
|
|
|
|
// with fstype of "cgroup") for the current running process.
|
|
|
|
//
|
|
|
|
// The results are cached (to avoid re-reading mountinfo which is relatively
|
|
|
|
// expensive), so it is assumed that cgroup mounts are not being changed.
|
|
|
|
func readCgroupMountinfo() ([]*mountinfo.Info, error) {
|
|
|
|
readMountinfoOnce.Do(func() {
|
|
|
|
cgroupMountinfo, readMountinfoErr = mountinfo.GetMounts(
|
|
|
|
mountinfo.FSTypeFilter("cgroup"),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
return cgroupMountinfo, readMountinfoErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func findCgroupV1Mountpoints() (map[string]string, error) {
|
|
|
|
mounts, err := readCgroupMountinfo()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
allSubsystems, err := cgroups.ParseCgroupFile("/proc/self/cgroup")
|
2016-01-20 22:20:59 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to parse cgroup information: %v", err)
|
|
|
|
}
|
2022-03-07 18:07:23 -05:00
|
|
|
|
|
|
|
allMap := make(map[string]bool)
|
|
|
|
for s := range allSubsystems {
|
|
|
|
allMap[s] = false
|
|
|
|
}
|
|
|
|
|
2016-01-20 22:20:59 -05:00
|
|
|
mps := make(map[string]string)
|
2022-03-07 18:07:23 -05:00
|
|
|
for _, mi := range mounts {
|
|
|
|
for _, opt := range strings.Split(mi.VFSOptions, ",") {
|
|
|
|
seen, known := allMap[opt]
|
|
|
|
if known && !seen {
|
|
|
|
allMap[opt] = true
|
|
|
|
mps[strings.TrimPrefix(opt, "name=")] = mi.Mountpoint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(mps) >= len(allMap) {
|
|
|
|
break
|
2016-01-20 22:20:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return mps, nil
|
|
|
|
}
|
|
|
|
|
2021-07-14 10:24:03 -04:00
|
|
|
type infoCollector func(info *SysInfo)
|
2019-02-03 14:51:04 -05:00
|
|
|
|
2020-03-10 08:09:25 -04:00
|
|
|
// WithCgroup2GroupPath specifies the cgroup v2 group path to inspect availability
|
|
|
|
// of the controllers.
|
|
|
|
//
|
|
|
|
// WithCgroup2GroupPath is expected to be used for rootless mode with systemd driver.
|
|
|
|
//
|
|
|
|
// e.g. g = "/user.slice/user-1000.slice/user@1000.service"
|
|
|
|
func WithCgroup2GroupPath(g string) Opt {
|
2021-07-14 09:05:12 -04:00
|
|
|
return func(o *SysInfo) {
|
|
|
|
if p := path.Clean(g); p != "" {
|
|
|
|
o.cg2GroupPath = p
|
|
|
|
}
|
2020-03-10 08:09:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-06 07:54:48 -04:00
|
|
|
// New returns a new SysInfo, using the filesystem to detect which features
|
2021-07-14 10:45:02 -04:00
|
|
|
// the kernel supports.
|
|
|
|
func New(options ...Opt) *SysInfo {
|
2022-03-07 18:07:23 -05:00
|
|
|
if cgroups.Mode() == cgroups.Unified {
|
2021-07-14 10:45:02 -04:00
|
|
|
return newV2(options...)
|
2020-03-10 08:09:25 -04:00
|
|
|
}
|
2021-07-14 10:45:02 -04:00
|
|
|
return newV1()
|
2021-07-14 06:36:38 -04:00
|
|
|
}
|
2020-03-10 08:09:25 -04:00
|
|
|
|
2021-07-14 10:45:02 -04:00
|
|
|
func newV1() *SysInfo {
|
2021-07-14 08:28:52 -04:00
|
|
|
var (
|
2021-07-14 10:24:03 -04:00
|
|
|
err error
|
|
|
|
sysInfo = &SysInfo{}
|
2021-07-14 08:28:52 -04:00
|
|
|
)
|
2021-07-14 09:44:38 -04:00
|
|
|
|
|
|
|
ops := []infoCollector{
|
|
|
|
applyNetworkingInfo,
|
|
|
|
applyAppArmorInfo,
|
|
|
|
applySeccompInfo,
|
|
|
|
applyCgroupNsInfo,
|
|
|
|
}
|
|
|
|
|
2022-03-07 18:07:23 -05:00
|
|
|
sysInfo.cgMounts, err = findCgroupV1Mountpoints()
|
2016-01-20 22:20:59 -05:00
|
|
|
if err != nil {
|
2019-02-03 14:51:04 -05:00
|
|
|
logrus.Warn(err)
|
2016-01-20 22:20:59 -05:00
|
|
|
} else {
|
2021-07-14 09:44:38 -04:00
|
|
|
ops = append(ops,
|
2019-02-03 14:51:04 -05:00
|
|
|
applyMemoryCgroupInfo,
|
|
|
|
applyCPUCgroupInfo,
|
|
|
|
applyBlkioCgroupInfo,
|
|
|
|
applyCPUSetCgroupInfo,
|
|
|
|
applyPIDSCgroupInfo,
|
|
|
|
applyDevicesCgroupInfo,
|
2021-07-14 09:44:38 -04:00
|
|
|
)
|
2019-02-03 14:51:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range ops {
|
2021-07-14 10:24:03 -04:00
|
|
|
o(sysInfo)
|
2019-02-03 14:51:04 -05:00
|
|
|
}
|
2015-06-16 22:36:20 -04:00
|
|
|
return sysInfo
|
|
|
|
}
|
|
|
|
|
2020-05-19 19:57:53 -04:00
|
|
|
// applyMemoryCgroupInfo adds the memory cgroup controller information to the info.
|
2021-07-14 10:24:03 -04:00
|
|
|
func applyMemoryCgroupInfo(info *SysInfo) {
|
2021-07-14 08:28:52 -04:00
|
|
|
mountPoint, ok := info.cgMounts["memory"]
|
2016-01-20 22:20:59 -05:00
|
|
|
if !ok {
|
2021-07-14 10:24:03 -04:00
|
|
|
info.Warnings = append(info.Warnings, "Your kernel does not support cgroup memory limit")
|
|
|
|
return
|
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 {
|
2021-07-14 10:24:03 -04:00
|
|
|
info.Warnings = append(info.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 {
|
2021-07-14 10:24:03 -04:00
|
|
|
info.Warnings = append(info.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 {
|
2021-07-14 10:24:03 -04:00
|
|
|
info.Warnings = append(info.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 {
|
2021-07-14 10:24:03 -04:00
|
|
|
info.Warnings = append(info.Warnings, "Your kernel does not support memory swappiness")
|
2015-07-12 03:46:33 -04:00
|
|
|
}
|
2022-02-07 11:09:23 -05:00
|
|
|
|
|
|
|
// Option is deprecated, but still accepted on API < v1.42 with cgroups v1,
|
|
|
|
// so setting the field to allow feature detection.
|
|
|
|
info.KernelMemory = cgroupEnabled(mountPoint, "memory.kmem.limit_in_bytes")
|
|
|
|
|
|
|
|
// Option is deprecated in runc, but still accepted in our API, so setting
|
|
|
|
// the field to allow feature detection, but don't warn if it's missing, to
|
|
|
|
// make the daemon logs a bit less noisy.
|
2019-02-03 14:51:04 -05:00
|
|
|
info.KernelMemoryTCP = cgroupEnabled(mountPoint, "memory.kmem.tcp.limit_in_bytes")
|
2015-06-16 22:36:20 -04:00
|
|
|
}
|
|
|
|
|
2020-05-19 19:57:53 -04:00
|
|
|
// applyCPUCgroupInfo adds the cpu cgroup controller information to the info.
|
2021-07-14 10:24:03 -04:00
|
|
|
func applyCPUCgroupInfo(info *SysInfo) {
|
2021-07-14 08:28:52 -04:00
|
|
|
mountPoint, ok := info.cgMounts["cpu"]
|
2016-01-20 22:20:59 -05:00
|
|
|
if !ok {
|
2021-07-14 10:24:03 -04:00
|
|
|
info.Warnings = append(info.Warnings, "Unable to find cpu cgroup in mounts")
|
|
|
|
return
|
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 {
|
2021-07-14 10:24:03 -04:00
|
|
|
info.Warnings = append(info.Warnings, "Your kernel does not support CPU shares")
|
2015-08-05 10:35:18 -04:00
|
|
|
}
|
|
|
|
|
2020-05-22 17:18:06 -04:00
|
|
|
info.CPUCfs = cgroupEnabled(mountPoint, "cpu.cfs_quota_us")
|
|
|
|
if !info.CPUCfs {
|
2021-07-14 10:24:03 -04:00
|
|
|
info.Warnings = append(info.Warnings, "Your kernel does not support CPU CFS scheduler")
|
2015-05-14 12:05:14 -04:00
|
|
|
}
|
|
|
|
|
2020-05-22 17:18:06 -04:00
|
|
|
info.CPURealtime = cgroupEnabled(mountPoint, "cpu.rt_period_us")
|
|
|
|
if !info.CPURealtime {
|
2021-07-14 10:24:03 -04:00
|
|
|
info.Warnings = append(info.Warnings, "Your kernel does not support CPU realtime scheduler")
|
2016-06-07 15:05:43 -04:00
|
|
|
}
|
2015-06-16 22:36:20 -04:00
|
|
|
}
|
2015-05-14 12:05:14 -04:00
|
|
|
|
2020-05-19 19:57:53 -04:00
|
|
|
// applyBlkioCgroupInfo adds the blkio cgroup controller information to the info.
|
2021-07-14 10:24:03 -04:00
|
|
|
func applyBlkioCgroupInfo(info *SysInfo) {
|
2021-07-14 08:28:52 -04:00
|
|
|
mountPoint, ok := info.cgMounts["blkio"]
|
2016-01-20 22:20:59 -05:00
|
|
|
if !ok {
|
2021-07-14 10:24:03 -04:00
|
|
|
info.Warnings = append(info.Warnings, "Unable to find blkio cgroup in mounts")
|
|
|
|
return
|
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 {
|
2021-07-14 10:24:03 -04:00
|
|
|
info.Warnings = append(info.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 {
|
2021-07-14 10:24:03 -04:00
|
|
|
info.Warnings = append(info.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 {
|
2021-07-14 10:24:03 -04:00
|
|
|
info.Warnings = append(info.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 {
|
2021-07-14 10:24:03 -04:00
|
|
|
info.Warnings = append(info.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 {
|
2021-07-14 10:24:03 -04:00
|
|
|
info.Warnings = append(info.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 {
|
2021-07-14 10:24:03 -04:00
|
|
|
info.Warnings = append(info.Warnings, "Your kernel does not support cgroup blkio throttle.write_iops_device")
|
2015-06-11 20:34:20 -04:00
|
|
|
}
|
2015-08-05 10:35:18 -04:00
|
|
|
}
|
|
|
|
|
2020-05-19 19:57:53 -04:00
|
|
|
// applyCPUSetCgroupInfo adds the cpuset cgroup controller information to the info.
|
2021-07-14 10:24:03 -04:00
|
|
|
func applyCPUSetCgroupInfo(info *SysInfo) {
|
2021-07-14 08:28:52 -04:00
|
|
|
mountPoint, ok := info.cgMounts["cpuset"]
|
2016-01-20 22:20:59 -05:00
|
|
|
if !ok {
|
2021-07-14 10:24:03 -04:00
|
|
|
info.Warnings = append(info.Warnings, "Unable to find cpuset cgroup in mounts")
|
|
|
|
return
|
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
|
|
|
|
2021-08-24 06:10:50 -04:00
|
|
|
cpus, err := os.ReadFile(path.Join(mountPoint, "cpuset.cpus"))
|
2015-09-08 14:40:55 -04:00
|
|
|
if err != nil {
|
2021-07-14 10:24:03 -04:00
|
|
|
return
|
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
|
|
|
|
2021-08-24 06:10:50 -04:00
|
|
|
mems, err := os.ReadFile(path.Join(mountPoint, "cpuset.mems"))
|
2015-09-08 14:40:55 -04:00
|
|
|
if err != nil {
|
2021-07-14 10:24:03 -04:00
|
|
|
return
|
2015-09-08 14:40:55 -04:00
|
|
|
}
|
2019-02-03 14:51:04 -05:00
|
|
|
info.Mems = strings.TrimSpace(string(mems))
|
2015-08-05 10:35:18 -04:00
|
|
|
}
|
|
|
|
|
2020-05-19 19:57:53 -04:00
|
|
|
// applyPIDSCgroupInfo adds whether the pids cgroup controller is available to the info.
|
2021-07-14 10:24:03 -04:00
|
|
|
func applyPIDSCgroupInfo(info *SysInfo) {
|
2021-07-14 08:28:52 -04:00
|
|
|
_, ok := info.cgMounts["pids"]
|
2020-05-19 19:46:44 -04:00
|
|
|
if !ok {
|
2021-07-14 10:24:03 -04:00
|
|
|
info.Warnings = append(info.Warnings, "Unable to find pids cgroup in mounts")
|
|
|
|
return
|
2019-02-03 14:51:04 -05:00
|
|
|
}
|
|
|
|
info.PidsLimit = true
|
|
|
|
}
|
|
|
|
|
2020-05-19 19:57:53 -04:00
|
|
|
// applyDevicesCgroupInfo adds whether the devices cgroup controller is available to the info.
|
2021-07-14 10:24:03 -04:00
|
|
|
func applyDevicesCgroupInfo(info *SysInfo) {
|
2021-07-14 08:28:52 -04:00
|
|
|
_, ok := info.cgMounts["devices"]
|
2019-02-03 14:51:04 -05:00
|
|
|
info.CgroupDevicesEnabled = ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// applyNetworkingInfo adds networking information to the info.
|
2021-07-14 10:24:03 -04:00
|
|
|
func applyNetworkingInfo(info *SysInfo) {
|
2019-02-03 14:51:04 -05:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2020-05-19 19:57:53 -04:00
|
|
|
// applyAppArmorInfo adds whether AppArmor is enabled to the info.
|
2021-07-14 10:24:03 -04:00
|
|
|
func applyAppArmorInfo(info *SysInfo) {
|
2019-02-03 14:51:04 -05:00
|
|
|
if _, err := os.Stat("/sys/kernel/security/apparmor"); !os.IsNotExist(err) {
|
2021-08-24 06:10:50 -04:00
|
|
|
if _, err := os.ReadFile("/sys/kernel/security/apparmor/profiles"); err == nil {
|
2019-02-03 14:51:04 -05:00
|
|
|
info.AppArmor = true
|
2015-12-15 14:15:43 -05:00
|
|
|
}
|
|
|
|
}
|
2019-02-03 14:51:04 -05:00
|
|
|
}
|
2015-12-15 14:15:43 -05:00
|
|
|
|
2020-05-19 19:57:53 -04:00
|
|
|
// applyCgroupNsInfo adds whether cgroupns is enabled to the info.
|
2021-07-14 10:24:03 -04:00
|
|
|
func applyCgroupNsInfo(info *SysInfo) {
|
2018-12-14 18:07:19 -05:00
|
|
|
if _, err := os.Stat("/proc/self/ns/cgroup"); !os.IsNotExist(err) {
|
|
|
|
info.CgroupNamespaces = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 14:51:04 -05:00
|
|
|
// applySeccompInfo checks if Seccomp is supported, via CONFIG_SECCOMP.
|
2021-07-14 10:24:03 -04:00
|
|
|
func applySeccompInfo(info *SysInfo) {
|
2022-03-07 18:07:23 -05:00
|
|
|
info.Seccomp = seccomp.IsEnabled()
|
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 {
|
2021-08-24 06:10:50 -04:00
|
|
|
val, err := os.ReadFile(path)
|
2015-06-16 22:36:20 -04:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(string(val)) == "1"
|
2015-05-14 12:05:14 -04:00
|
|
|
}
|