Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for sysbox issue #544 #77

Merged
merged 3 commits into from
May 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package fscommon
package cgroups

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"strings"
"sync"
Expand All @@ -11,6 +14,88 @@ import (
"golang.org/x/sys/unix"
)

// OpenFile opens a cgroup file in a given dir with given flags.
// It is supposed to be used for cgroup files only.
func OpenFile(dir, file string, flags int) (*os.File, error) {
if dir == "" {
return nil, errors.Errorf("no directory specified for %s", file)
}
return openFile(dir, file, flags)
}

// ReadFile reads data from a cgroup file in dir.
// It is supposed to be used for cgroup files only.
func ReadFile(dir, file string) (string, error) {
fd, err := OpenFile(dir, file, unix.O_RDONLY)
if err != nil {
return "", err
}
defer fd.Close()
var buf bytes.Buffer

_, err = buf.ReadFrom(fd)
return buf.String(), err
}

// WriteFile writes data to a cgroup file in dir.
// It is supposed to be used for cgroup files only.
func WriteFile(dir, file, data string) error {
fd, err := OpenFile(dir, file, unix.O_WRONLY)
if err != nil {
return err
}
defer fd.Close()
if err := retryingWriteFile(fd, data); err != nil {
return errors.Wrapf(err, "failed to write %q", data)
}
return nil
}

func CopyFile(source, dest string) error {
var (
srcF *os.File
dstF *os.File
data []byte
err error
)

srcF, err = os.Open(source)
if err != nil {
return fmt.Errorf("failed to open %s: %s", source, err)
}
defer srcF.Close()

dstF, err = os.Open(dest)
if err != nil {
dstF.Close()
return fmt.Errorf("failed to open %s: %s", dest, err)
}
defer dstF.Close()

data, err = ioutil.ReadFile(source)
if err != nil {
return fmt.Errorf("failed to read %s: %s", source, err)
}

err = ioutil.WriteFile(dest, data, 0)
if err != nil {
return fmt.Errorf("failed to read %s: %s", dest, err)
}

return nil
}

func retryingWriteFile(fd *os.File, data string) error {
for {
_, err := fd.Write([]byte(data))
if errors.Is(err, unix.EINTR) {
logrus.Infof("interrupted while writing %s to %s", data, fd.Name())
continue
}
return err
}
}

const (
cgroupfsDir = "/sys/fs/cgroup"
cgroupfsPrefix = cgroupfsDir + "/"
Expand Down Expand Up @@ -61,10 +146,7 @@ func prepareOpenat2() error {

// OpenFile opens a cgroup file in a given dir with given flags.
// It is supposed to be used for cgroup files only.
func OpenFile(dir, file string, flags int) (*os.File, error) {
if dir == "" {
return nil, errors.Errorf("no directory specified for %s", file)
}
func openFile(dir, file string, flags int) (*os.File, error) {
mode := os.FileMode(0)
if TestMode && flags&os.O_WRONLY != 0 {
// "emulate" cgroup fs for unit tests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// +build linux

package fscommon
package cgroups

import (
"fmt"
Expand Down
1 change: 1 addition & 0 deletions libcontainer/cgroups/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var (
&NetPrioGroup{},
&PerfEventGroup{},
&FreezerGroup{},
&RdmaGroup{},
&NameGroup{GroupName: "name=systemd", Join: true},
}
HugePageSizes, _ = cgroups.GetHugePageSize()
Expand Down
41 changes: 41 additions & 0 deletions libcontainer/cgroups/fs/rdma.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package fs

import (
"fmt"
"os"

"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
"github.com/opencontainers/runc/libcontainer/configs"
)

type RdmaGroup struct{}

func (s *RdmaGroup) Name() string {
return "rdma"
}

func (s *RdmaGroup) Apply(path string, d *cgroupData) error {
return join(path, d.pid)
}

func (s *RdmaGroup) Set(path string, cgroup *configs.Cgroup) error {
return fscommon.RdmaSet(path, cgroup)
}

func (s *RdmaGroup) GetStats(path string, stats *cgroups.Stats) error {
return fscommon.RdmaGetStats(path, stats)
}

func (s *RdmaGroup) Clone(source, dest string) error {

if err := fscommon.WriteFile(source, "cgroup.clone_children", "1"); err != nil {
return err
}

if err := os.MkdirAll(dest, 0755); err != nil {
return fmt.Errorf("Failed to create cgroup %s", dest)
}

return nil
}
3 changes: 2 additions & 1 deletion libcontainer/cgroups/fs/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ import (
"path/filepath"
"testing"

"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
"github.com/opencontainers/runc/libcontainer/configs"
)

func init() {
fscommon.TestMode = true
cgroups.TestMode = true
}

type cgroupTestUtil struct {
Expand Down
8 changes: 8 additions & 0 deletions libcontainer/cgroups/fs2/fs2.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ func (m *manager) GetStats() (*cgroups.Stats, error) {
errs = append(errs, err)
}
}
// rdma (since kernel 4.11)
if err := fscommon.RdmaGetStats(m.dirPath, st); err != nil && !os.IsNotExist(err) {
errs = append(errs, err)
}
if len(errs) > 0 && !m.rootless {
return st, errors.Errorf("error while statting cgroup v2: %+v", errs)
}
Expand Down Expand Up @@ -204,6 +208,10 @@ func (m *manager) Set(container *configs.Config) error {
if err := setHugeTlb(m.dirPath, container.Cgroups); err != nil {
return err
}
// rdma (since kernel 4.11)
if err := fscommon.RdmaSet(m.dirPath, container.Cgroups); err != nil {
return err
}
// freezer (since kernel 5.2, pseudo-controller)
if err := setFreezer(m.dirPath, container.Cgroups.Freezer); err != nil {
return err
Expand Down
87 changes: 0 additions & 87 deletions libcontainer/cgroups/fscommon/fscommon.go

This file was deleted.

Loading