pkg/cgroups Add GetCgroupMounts() and GetAllSubsystems()

This lists all currently mounted cgroups and all supported cgroup
subsystems on the machine.

Docker-DCO-1.1-Signed-off-by: Alexander Larsson <alexl@redhat.com> (github: alexlarsson)
This commit is contained in:
Alexander Larsson 2014-05-07 16:48:54 +02:00
parent 124aba2f15
commit e0e0da9e28
1 changed files with 75 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package cgroups
import ( import (
"bufio" "bufio"
"fmt"
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
@ -30,6 +31,80 @@ func FindCgroupMountpoint(subsystem string) (string, error) {
return "", ErrNotFound return "", ErrNotFound
} }
type Mount struct {
Mountpoint string
Subsystems []string
}
func (m Mount) GetThisCgroupDir() (string, error) {
if len(m.Subsystems) == 0 {
return "", fmt.Errorf("no subsystem for mount")
}
return GetThisCgroupDir(m.Subsystems[0])
}
func GetCgroupMounts() ([]Mount, error) {
mounts, err := mount.GetMounts()
if err != nil {
return nil, err
}
all, err := GetAllSubsystems()
if err != nil {
return nil, err
}
allMap := make(map[string]bool)
for _, s := range all {
allMap[s] = true
}
res := []Mount{}
for _, mount := range mounts {
if mount.Fstype == "cgroup" {
m := Mount{Mountpoint: mount.Mountpoint}
for _, opt := range strings.Split(mount.VfsOpts, ",") {
if strings.HasPrefix(opt, "name=") {
m.Subsystems = append(m.Subsystems, opt)
}
if allMap[opt] {
m.Subsystems = append(m.Subsystems, opt)
}
}
res = append(res, m)
}
}
return res, nil
}
// Returns all the cgroup subsystems supported by the kernel
func GetAllSubsystems() ([]string, error) {
f, err := os.Open("/proc/cgroups")
if err != nil {
return nil, err
}
defer f.Close()
subsystems := []string{}
s := bufio.NewScanner(f)
for s.Scan() {
if err := s.Err(); err != nil {
return nil, err
}
text := s.Text()
if text[0] != '#' {
parts := strings.Fields(text)
if len(parts) > 4 && parts[3] != "0" {
subsystems = append(subsystems, parts[0])
}
}
}
return subsystems, nil
}
// Returns the relative path to the cgroup docker is running in. // Returns the relative path to the cgroup docker is running in.
func GetThisCgroupDir(subsystem string) (string, error) { func GetThisCgroupDir(subsystem string) (string, error) {
f, err := os.Open("/proc/self/cgroup") f, err := os.Open("/proc/self/cgroup")