33package libpod
44
55import (
6- "errors"
76 "fmt"
87 "io/fs"
98 "os"
@@ -30,9 +29,7 @@ import (
3029 "golang.org/x/sys/unix"
3130)
3231
33- var (
34- bindOptions = []string {define .TypeBind , "rprivate" }
35- )
32+ var bindOptions = []string {define .TypeBind , "rprivate" }
3633
3734func (c * Container ) mountSHM (shmOptions string ) error {
3835 contextType := "context"
@@ -267,11 +264,6 @@ func (c *Container) setupSystemd(mounts []spec.Mount, g generate.Generator) erro
267264 g .AddMount (tmpfsMnt )
268265 }
269266
270- unified , err := cgroups .IsCgroup2UnifiedMode ()
271- if err != nil {
272- return err
273- }
274-
275267 hasCgroupNs := false
276268 for _ , ns := range c .config .Spec .Linux .Namespaces {
277269 if ns .Type == spec .CgroupNamespace {
@@ -280,69 +272,25 @@ func (c *Container) setupSystemd(mounts []spec.Mount, g generate.Generator) erro
280272 }
281273 }
282274
283- if unified {
284- g .RemoveMount ("/sys/fs/cgroup" )
275+ g .RemoveMount ("/sys/fs/cgroup" )
285276
286- var systemdMnt spec.Mount
287- if hasCgroupNs {
288- systemdMnt = spec.Mount {
289- Destination : "/sys/fs/cgroup" ,
290- Type : "cgroup" ,
291- Source : "cgroup" ,
292- Options : []string {"private" , "rw" },
293- }
294- } else {
295- systemdMnt = spec.Mount {
296- Destination : "/sys/fs/cgroup" ,
297- Type : define .TypeBind ,
298- Source : "/sys/fs/cgroup" ,
299- Options : []string {define .TypeBind , "private" , "rw" },
300- }
277+ var systemdMnt spec.Mount
278+ if hasCgroupNs {
279+ systemdMnt = spec.Mount {
280+ Destination : "/sys/fs/cgroup" ,
281+ Type : "cgroup" ,
282+ Source : "cgroup" ,
283+ Options : []string {"private" , "rw" },
301284 }
302- g .AddMount (systemdMnt )
303285 } else {
304- hasSystemdMount := MountExists (mounts , "/sys/fs/cgroup/systemd" )
305- if hasCgroupNs && ! hasSystemdMount {
306- return errors .New ("cgroup namespace is not supported with cgroup v1 and systemd mode" )
307- }
308- mountOptions := []string {define .TypeBind , "rprivate" }
309-
310- if ! hasSystemdMount {
311- skipMount := hasSystemdMount
312- var statfs unix.Statfs_t
313- if err := unix .Statfs ("/sys/fs/cgroup/systemd" , & statfs ); err != nil {
314- if errors .Is (err , os .ErrNotExist ) {
315- // If the mount is missing on the host, we cannot bind mount it so
316- // just skip it.
317- skipMount = true
318- }
319- mountOptions = append (mountOptions , "nodev" , "noexec" , "nosuid" )
320- } else {
321- if statfs .Flags & unix .MS_NODEV == unix .MS_NODEV {
322- mountOptions = append (mountOptions , "nodev" )
323- }
324- if statfs .Flags & unix .MS_NOEXEC == unix .MS_NOEXEC {
325- mountOptions = append (mountOptions , "noexec" )
326- }
327- if statfs .Flags & unix .MS_NOSUID == unix .MS_NOSUID {
328- mountOptions = append (mountOptions , "nosuid" )
329- }
330- if statfs .Flags & unix .MS_RDONLY == unix .MS_RDONLY {
331- mountOptions = append (mountOptions , "ro" )
332- }
333- }
334- if ! skipMount {
335- systemdMnt := spec.Mount {
336- Destination : "/sys/fs/cgroup/systemd" ,
337- Type : define .TypeBind ,
338- Source : "/sys/fs/cgroup/systemd" ,
339- Options : mountOptions ,
340- }
341- g .AddMount (systemdMnt )
342- g .AddLinuxMaskedPaths ("/sys/fs/cgroup/systemd/release_agent" )
343- }
286+ systemdMnt = spec.Mount {
287+ Destination : "/sys/fs/cgroup" ,
288+ Type : define .TypeBind ,
289+ Source : "/sys/fs/cgroup" ,
290+ Options : []string {define .TypeBind , "private" , "rw" },
344291 }
345292 }
293+ g .AddMount (systemdMnt )
346294
347295 return nil
348296}
@@ -385,16 +333,12 @@ func isRootlessCgroupSet(cgroup string) bool {
385333}
386334
387335func (c * Container ) expectPodCgroup () (bool , error ) {
388- unified , err := cgroups .IsCgroup2UnifiedMode ()
389- if err != nil {
390- return false , err
391- }
392336 cgroupManager := c .CgroupManager ()
393337 switch {
394338 case c .config .NoCgroups :
395339 return false , nil
396340 case cgroupManager == config .SystemdCgroupsManager :
397- return ! rootless . IsRootless () || unified , nil
341+ return true , nil
398342 case cgroupManager == config .CgroupfsCgroupsManager :
399343 return ! rootless .IsRootless (), nil
400344 default :
@@ -404,10 +348,6 @@ func (c *Container) expectPodCgroup() (bool, error) {
404348
405349// Get cgroup path in a format suitable for the OCI spec
406350func (c * Container ) getOCICgroupPath () (string , error ) {
407- unified , err := cgroups .IsCgroup2UnifiedMode ()
408- if err != nil {
409- return "" , err
410- }
411351 cgroupManager := c .CgroupManager ()
412352 switch {
413353 case c .config .NoCgroups :
@@ -425,7 +365,7 @@ func (c *Container) getOCICgroupPath() (string, error) {
425365 systemdCgroups := fmt .Sprintf ("%s:libpod:%s" , path .Base (c .config .CgroupParent ), c .ID ())
426366 logrus .Debugf ("Setting Cgroups for container %s to %s" , c .ID (), systemdCgroups )
427367 return systemdCgroups , nil
428- case (rootless .IsRootless () && (cgroupManager == config .CgroupfsCgroupsManager || ! unified )):
368+ case (rootless .IsRootless () && (cgroupManager == config .CgroupfsCgroupsManager )):
429369 if c .config .CgroupParent == "" || ! isRootlessCgroupSet (c .config .CgroupParent ) {
430370 return "" , nil
431371 }
0 commit comments