mirror of
https://github.com/opencontainers/runc.git
synced 2025-10-20 22:19:42 +08:00
libcontainer/cgroups: rm FindCgroupMountpointDir
This function is cgroupv1-specific, is only used once, and its name is very close to the name of another function, FindCgroupMountpoint. Inline it into the (only) caller. Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This commit is contained in:
@@ -3,9 +3,11 @@
|
|||||||
package fs
|
package fs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/opencontainers/runc/libcontainer/cgroups"
|
"github.com/opencontainers/runc/libcontainer/cgroups"
|
||||||
@@ -88,10 +90,43 @@ func getCgroupRoot() (string, error) {
|
|||||||
return cgroupRoot, nil
|
return cgroupRoot, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
root, err := cgroups.FindCgroupMountpointDir()
|
f, err := os.Open("/proc/self/mountinfo")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
var root string
|
||||||
|
scanner := bufio.NewScanner(f)
|
||||||
|
for scanner.Scan() {
|
||||||
|
text := scanner.Text()
|
||||||
|
fields := strings.Split(text, " ")
|
||||||
|
// Safe as mountinfo encodes mountpoints with spaces as \040.
|
||||||
|
index := strings.Index(text, " - ")
|
||||||
|
postSeparatorFields := strings.Fields(text[index+3:])
|
||||||
|
numPostFields := len(postSeparatorFields)
|
||||||
|
|
||||||
|
// This is an error as we can't detect if the mount is for "cgroup"
|
||||||
|
if numPostFields == 0 {
|
||||||
|
return "", fmt.Errorf("mountinfo: found no fields post '-' in %q", text)
|
||||||
|
}
|
||||||
|
|
||||||
|
if postSeparatorFields[0] == "cgroup" {
|
||||||
|
// Check that the mount is properly formatted.
|
||||||
|
if numPostFields < 3 {
|
||||||
|
return "", fmt.Errorf("Error found less than 3 fields post '-' in %q", text)
|
||||||
|
}
|
||||||
|
|
||||||
|
root = filepath.Dir(fields[4])
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := scanner.Err(); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if root == "" {
|
||||||
|
return "", errors.New("no cgroup mount found in mountinfo")
|
||||||
|
}
|
||||||
|
|
||||||
if _, err := os.Stat(root); err != nil {
|
if _, err := os.Stat(root); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
@@ -117,43 +117,6 @@ func isSubsystemAvailable(subsystem string) bool {
|
|||||||
return avail
|
return avail
|
||||||
}
|
}
|
||||||
|
|
||||||
func FindCgroupMountpointDir() (string, error) {
|
|
||||||
f, err := os.Open("/proc/self/mountinfo")
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
scanner := bufio.NewScanner(f)
|
|
||||||
for scanner.Scan() {
|
|
||||||
text := scanner.Text()
|
|
||||||
fields := strings.Split(text, " ")
|
|
||||||
// Safe as mountinfo encodes mountpoints with spaces as \040.
|
|
||||||
index := strings.Index(text, " - ")
|
|
||||||
postSeparatorFields := strings.Fields(text[index+3:])
|
|
||||||
numPostFields := len(postSeparatorFields)
|
|
||||||
|
|
||||||
// This is an error as we can't detect if the mount is for "cgroup"
|
|
||||||
if numPostFields == 0 {
|
|
||||||
return "", fmt.Errorf("Found no fields post '-' in %q", text)
|
|
||||||
}
|
|
||||||
|
|
||||||
if postSeparatorFields[0] == "cgroup" || postSeparatorFields[0] == "cgroup2" {
|
|
||||||
// Check that the mount is properly formatted.
|
|
||||||
if numPostFields < 3 {
|
|
||||||
return "", fmt.Errorf("Error found less than 3 fields post '-' in %q", text)
|
|
||||||
}
|
|
||||||
|
|
||||||
return filepath.Dir(fields[4]), nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err := scanner.Err(); err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
return "", NewNotFoundError("cgroup")
|
|
||||||
}
|
|
||||||
|
|
||||||
type Mount struct {
|
type Mount struct {
|
||||||
Mountpoint string
|
Mountpoint string
|
||||||
Root string
|
Root string
|
||||||
|
Reference in New Issue
Block a user