From fc31076c23b9eff091f21e3c708f0b0f587ea91e Mon Sep 17 00:00:00 2001 From: Alexander Morozov Date: Fri, 17 Jul 2015 11:41:54 -0700 Subject: [PATCH] Substract source mount from cgroup dir This is needed because for nested containers cgroups. Without this patch they creating unnecessary intermediate cgroup like: /sys/fs/cgroup/memory/system.slice/docker-9409d9f0b68fb9e9d7d532d5b3f35e7c7f9cca1312af392ae3b28436f1f2998f.scope/system.slice/docker-9409d9f0b68fb9e9d7d532d5b3f35e7c7f9cca1312af392ae3b28436f1f2998f.scope/docker/908ebcc9c13584a14322ec070bd971e0de62f126c0cd95c079acdb99990ad3a3 It is because in /proc/self/cgroup we see paths from host, and they don't exist in container. Signed-off-by: Alexander Morozov --- libcontainer/cgroups/fs/apply_raw.go | 12 ++++++++---- libcontainer/cgroups/utils.go | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/libcontainer/cgroups/fs/apply_raw.go b/libcontainer/cgroups/fs/apply_raw.go index 3d099d86..1954357b 100644 --- a/libcontainer/cgroups/fs/apply_raw.go +++ b/libcontainer/cgroups/fs/apply_raw.go @@ -235,16 +235,20 @@ func getCgroupData(c *configs.Cgroup, pid int) (*data, error) { }, nil } -func (raw *data) parent(subsystem, mountpoint string) (string, error) { +func (raw *data) parent(subsystem, mountpoint, src string) (string, error) { initPath, err := cgroups.GetInitCgroupDir(subsystem) if err != nil { return "", err } - return filepath.Join(mountpoint, initPath), nil + relDir, err := filepath.Rel(src, initPath) + if err != nil { + return "", err + } + return filepath.Join(mountpoint, relDir), nil } func (raw *data) path(subsystem string) (string, error) { - mnt, err := cgroups.FindCgroupMountpoint(subsystem) + mnt, src, err := cgroups.FindCgroupMountpointAndSource(subsystem) // If we didn't mount the subsystem, there is no point we make the path. if err != nil { return "", err @@ -255,7 +259,7 @@ func (raw *data) path(subsystem string) (string, error) { return filepath.Join(raw.root, subsystem, raw.cgroup), nil } - parent, err := raw.parent(subsystem, mnt) + parent, err := raw.parent(subsystem, mnt, src) if err != nil { return "", err } diff --git a/libcontainer/cgroups/utils.go b/libcontainer/cgroups/utils.go index f58e717f..92438a07 100644 --- a/libcontainer/cgroups/utils.go +++ b/libcontainer/cgroups/utils.go @@ -42,6 +42,28 @@ func FindCgroupMountpoint(subsystem string) (string, error) { return "", NewNotFoundError(subsystem) } +func FindCgroupMountpointAndSource(subsystem string) (string, string, error) { + f, err := os.Open("/proc/self/mountinfo") + if err != nil { + return "", "", err + } + scanner := bufio.NewScanner(f) + for scanner.Scan() { + txt := scanner.Text() + fields := strings.Split(txt, " ") + for _, opt := range strings.Split(fields[len(fields)-1], ",") { + if opt == subsystem { + return fields[4], fields[3], nil + } + } + } + if err := scanner.Err(); err != nil { + return "", "", err + } + + return "", "", NewNotFoundError(subsystem) +} + func FindCgroupMountpointDir() (string, error) { mounts, err := mount.GetMounts() if err != nil {