|
| 1 | +// +build linux |
| 2 | + |
| 3 | +package rootless |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/opencontainers/runc/libcontainer/cgroups" |
| 9 | + "github.com/opencontainers/runc/libcontainer/cgroups/fs" |
| 10 | + "github.com/opencontainers/runc/libcontainer/configs" |
| 11 | + "github.com/opencontainers/runc/libcontainer/configs/validate" |
| 12 | +) |
| 13 | + |
| 14 | +// TODO: This is copied from libcontainer/cgroups/fs, which duplicates this code |
| 15 | +// needlessly. We should probably export this list. |
| 16 | + |
| 17 | +var subsystems = []subsystem{ |
| 18 | + &fs.CpusetGroup{}, |
| 19 | + &fs.DevicesGroup{}, |
| 20 | + &fs.MemoryGroup{}, |
| 21 | + &fs.CpuGroup{}, |
| 22 | + &fs.CpuacctGroup{}, |
| 23 | + &fs.PidsGroup{}, |
| 24 | + &fs.BlkioGroup{}, |
| 25 | + &fs.HugetlbGroup{}, |
| 26 | + &fs.NetClsGroup{}, |
| 27 | + &fs.NetPrioGroup{}, |
| 28 | + &fs.PerfEventGroup{}, |
| 29 | + &fs.FreezerGroup{}, |
| 30 | + &fs.NameGroup{GroupName: "name=systemd"}, |
| 31 | +} |
| 32 | + |
| 33 | +type subsystem interface { |
| 34 | + // Name returns the name of the subsystem. |
| 35 | + Name() string |
| 36 | + |
| 37 | + // Returns the stats, as 'stats', corresponding to the cgroup under 'path'. |
| 38 | + GetStats(path string, stats *cgroups.Stats) error |
| 39 | +} |
| 40 | + |
| 41 | +// The noop cgroup manager is used for rootless containers, because we currently |
| 42 | +// cannot manage cgroups if we are in a rootless setup. This manager is chosen |
| 43 | +// by factory if we are in rootless mode. We error out if any cgroup options are |
| 44 | +// set in the config -- this may change in the future with upcoming kernel features |
| 45 | +// like the cgroup namespace. |
| 46 | + |
| 47 | +type Manager struct { |
| 48 | + Cgroups *configs.Cgroup |
| 49 | + Paths map[string]string |
| 50 | +} |
| 51 | + |
| 52 | +func (m *Manager) Apply(pid int) error { |
| 53 | + // If there are no cgroup settings, there's nothing to do. |
| 54 | + if m.Cgroups == nil { |
| 55 | + return nil |
| 56 | + } |
| 57 | + |
| 58 | + // We can't set paths. |
| 59 | + // TODO(cyphar): Implement the case where the runner of a rootless container |
| 60 | + // owns their own cgroup, which would allow us to set up a |
| 61 | + // cgroup for each path. |
| 62 | + if m.Cgroups.Paths != nil { |
| 63 | + return fmt.Errorf("cannot change cgroup path in rootless container") |
| 64 | + } |
| 65 | + |
| 66 | + // We load the paths into the manager. |
| 67 | + paths := make(map[string]string) |
| 68 | + for _, sys := range subsystems { |
| 69 | + name := sys.Name() |
| 70 | + |
| 71 | + path, err := cgroups.GetOwnCgroupPath(name) |
| 72 | + if err != nil { |
| 73 | + // Ignore paths we couldn't resolve. |
| 74 | + continue |
| 75 | + } |
| 76 | + |
| 77 | + paths[name] = path |
| 78 | + } |
| 79 | + |
| 80 | + m.Paths = paths |
| 81 | + return nil |
| 82 | +} |
| 83 | + |
| 84 | +func (m *Manager) GetPaths() map[string]string { |
| 85 | + return m.Paths |
| 86 | +} |
| 87 | + |
| 88 | +func (m *Manager) Set(container *configs.Config) error { |
| 89 | + // We have to re-do the validation here, since someone might decide to |
| 90 | + // update a rootless container. |
| 91 | + return validate.New().Validate(container) |
| 92 | +} |
| 93 | + |
| 94 | +func (m *Manager) GetPids() ([]int, error) { |
| 95 | + dir, err := cgroups.GetOwnCgroupPath("devices") |
| 96 | + if err != nil { |
| 97 | + return nil, err |
| 98 | + } |
| 99 | + return cgroups.GetPids(dir) |
| 100 | +} |
| 101 | + |
| 102 | +func (m *Manager) GetAllPids() ([]int, error) { |
| 103 | + dir, err := cgroups.GetOwnCgroupPath("devices") |
| 104 | + if err != nil { |
| 105 | + return nil, err |
| 106 | + } |
| 107 | + return cgroups.GetAllPids(dir) |
| 108 | +} |
| 109 | + |
| 110 | +func (m *Manager) GetStats() (*cgroups.Stats, error) { |
| 111 | + // TODO(cyphar): We can make this work if we figure out a way to allow usage |
| 112 | + // of cgroups with a rootless container. While this doesn't |
| 113 | + // actually require write access to a cgroup directory, the |
| 114 | + // statistics are not useful if they can be affected by |
| 115 | + // non-container processes. |
| 116 | + return nil, fmt.Errorf("cannot get cgroup stats in rootless container") |
| 117 | +} |
| 118 | + |
| 119 | +func (m *Manager) Freeze(state configs.FreezerState) error { |
| 120 | + // TODO(cyphar): We can make this work if we figure out a way to allow usage |
| 121 | + // of cgroups with a rootless container. |
| 122 | + return fmt.Errorf("cannot use freezer cgroup in rootless container") |
| 123 | +} |
| 124 | + |
| 125 | +func (m *Manager) Destroy() error { |
| 126 | + // We don't have to do anything here because we didn't do any setup. |
| 127 | + return nil |
| 128 | +} |
0 commit comments