Skip to content

Commit f557996

Browse files
committed
Add flag to allow getting all mounts for cgroups subsystems
Signed-off-by: Mrunal Patel <[email protected]>
1 parent b1e602e commit f557996

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

libcontainer/cgroups/utils.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func (m Mount) GetThisCgroupDir(cgroups map[string]string) (string, error) {
139139
return getControllerPath(m.Subsystems[0], cgroups)
140140
}
141141

142-
func getCgroupMountsHelper(ss map[string]bool, mi io.Reader) ([]Mount, error) {
142+
func getCgroupMountsHelper(ss map[string]bool, mi io.Reader, all bool) ([]Mount, error) {
143143
res := make([]Mount, 0, len(ss))
144144
scanner := bufio.NewScanner(mi)
145145
numFound := 0
@@ -166,7 +166,9 @@ func getCgroupMountsHelper(ss map[string]bool, mi io.Reader) ([]Mount, error) {
166166
} else {
167167
m.Subsystems = append(m.Subsystems, opt)
168168
}
169-
numFound++
169+
if !all {
170+
numFound++
171+
}
170172
}
171173
res = append(res, m)
172174
}
@@ -176,23 +178,25 @@ func getCgroupMountsHelper(ss map[string]bool, mi io.Reader) ([]Mount, error) {
176178
return res, nil
177179
}
178180

179-
func GetCgroupMounts() ([]Mount, error) {
181+
// GetCgroupMounts returns the mounts for the cgroup subsystems.
182+
// all indicates whether to return just the first instance or all the mounts.
183+
func GetCgroupMounts(all bool) ([]Mount, error) {
180184
f, err := os.Open("/proc/self/mountinfo")
181185
if err != nil {
182186
return nil, err
183187
}
184188
defer f.Close()
185189

186-
all, err := ParseCgroupFile("/proc/self/cgroup")
190+
allSubsystems, err := ParseCgroupFile("/proc/self/cgroup")
187191
if err != nil {
188192
return nil, err
189193
}
190194

191195
allMap := make(map[string]bool)
192-
for s := range all {
196+
for s := range allSubsystems {
193197
allMap[s] = true
194198
}
195-
return getCgroupMountsHelper(allMap, f)
199+
return getCgroupMountsHelper(allMap, f, all)
196200
}
197201

198202
// GetAllSubsystems returns all the cgroup subsystems supported by the kernel

libcontainer/cgroups/utils_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func TestGetCgroupMounts(t *testing.T) {
134134
}
135135
for _, td := range testTable {
136136
mi := bytes.NewBufferString(td.mountInfo)
137-
cgMounts, err := getCgroupMountsHelper(td.subsystems, mi)
137+
cgMounts, err := getCgroupMountsHelper(td.subsystems, mi, false)
138138
if err != nil {
139139
t.Fatal(err)
140140
}
@@ -187,7 +187,7 @@ func BenchmarkGetCgroupMounts(b *testing.B) {
187187
b.StopTimer()
188188
mi := bytes.NewBufferString(fedoraMountinfo)
189189
b.StartTimer()
190-
if _, err := getCgroupMountsHelper(subsystems, mi); err != nil {
190+
if _, err := getCgroupMountsHelper(subsystems, mi, false); err != nil {
191191
b.Fatal(err)
192192
}
193193
}

libcontainer/rootfs_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ func mountToRootfs(m *configs.Mount, rootfs, mountLabel string) error {
270270
}
271271

272272
func getCgroupMounts(m *configs.Mount) ([]*configs.Mount, error) {
273-
mounts, err := cgroups.GetCgroupMounts()
273+
mounts, err := cgroups.GetCgroupMounts(false)
274274
if err != nil {
275275
return nil, err
276276
}

0 commit comments

Comments
 (0)