Skip to content

Commit bf6a78c

Browse files
authored
Merge pull request #3842 from kolyshkin/rm-warning
libct/cg/sd: use systemd version when generating device properties
2 parents 3e76cc4 + d7208f5 commit bf6a78c

File tree

4 files changed

+18
-24
lines changed

4 files changed

+18
-24
lines changed

libcontainer/cgroups/devices/systemd.go

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717

1818
// systemdProperties takes the configured device rules and generates a
1919
// corresponding set of systemd properties to configure the devices correctly.
20-
func systemdProperties(r *configs.Resources) ([]systemdDbus.Property, error) {
20+
func systemdProperties(r *configs.Resources, sdVer int) ([]systemdDbus.Property, error) {
2121
if r.SkipDevices {
2222
return nil, nil
2323
}
@@ -78,9 +78,10 @@ func systemdProperties(r *configs.Resources) ([]systemdDbus.Property, error) {
7878
// trickery to convert things:
7979
//
8080
// * Concrete rules with non-wildcard major/minor numbers have to use
81-
// /dev/{block,char} paths. This is slightly odd because it means
82-
// that we cannot add whitelist rules for devices that don't exist,
83-
// but there's not too much we can do about that.
81+
// /dev/{block,char}/MAJOR:minor paths. Before v240, systemd uses
82+
// stat(2) on such paths to look up device properties, meaning we
83+
// cannot add whitelist rules for devices that don't exist. Since v240,
84+
// device properties are parsed from the path string.
8485
//
8586
// However, path globbing is not support for path-based rules so we
8687
// need to handle wildcards in some other manner.
@@ -128,21 +129,14 @@ func systemdProperties(r *configs.Resources) ([]systemdDbus.Property, error) {
128129
case devices.CharDevice:
129130
entry.Path = fmt.Sprintf("/dev/char/%d:%d", rule.Major, rule.Minor)
130131
}
131-
// systemd will issue a warning if the path we give here doesn't exist.
132-
// Since all of this logic is best-effort anyway (we manually set these
133-
// rules separately to systemd) we can safely skip entries that don't
134-
// have a corresponding path.
135-
if _, err := os.Stat(entry.Path); err != nil {
136-
// Also check /sys/dev so that we don't depend on /dev/{block,char}
137-
// being populated. (/dev/{block,char} is populated by udev, which
138-
// isn't strictly required for systemd). Ironically, this happens most
139-
// easily when starting containerd within a runc created container
140-
// itself.
141-
142-
// We don't bother with securejoin here because we create entry.Path
143-
// right above here, so we know it's safe.
144-
if _, err := os.Stat("/sys" + entry.Path); err != nil {
145-
logrus.Warnf("skipping device %s for systemd: %s", entry.Path, err)
132+
if sdVer < 240 {
133+
// Old systemd versions use stat(2) on path to find out device major:minor
134+
// numbers and type. If the path doesn't exist, it will not add the rule,
135+
// emitting a warning instead.
136+
// Since all of this logic is best-effort anyway (we manually set these
137+
// rules separately to systemd) we can safely skip entries that don't
138+
// have a corresponding path.
139+
if _, err := os.Stat(entry.Path); err != nil {
146140
continue
147141
}
148142
}

libcontainer/cgroups/systemd/common.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var (
3333
isRunningSystemdOnce sync.Once
3434
isRunningSystemd bool
3535

36-
GenerateDeviceProps func(*configs.Resources) ([]systemdDbus.Property, error)
36+
GenerateDeviceProps func(r *configs.Resources, sdVer int) ([]systemdDbus.Property, error)
3737
)
3838

3939
// NOTE: This function comes from package github.com/coreos/go-systemd/util
@@ -342,13 +342,13 @@ func addCpuset(cm *dbusConnManager, props *[]systemdDbus.Property, cpus, mems st
342342

343343
// generateDeviceProperties takes the configured device rules and generates a
344344
// corresponding set of systemd properties to configure the devices correctly.
345-
func generateDeviceProperties(r *configs.Resources) ([]systemdDbus.Property, error) {
345+
func generateDeviceProperties(r *configs.Resources, cm *dbusConnManager) ([]systemdDbus.Property, error) {
346346
if GenerateDeviceProps == nil {
347347
if len(r.Devices) > 0 {
348348
return nil, cgroups.ErrDevicesUnsupported
349349
}
350350
return nil, nil
351351
}
352352

353-
return GenerateDeviceProps(r)
353+
return GenerateDeviceProps(r, systemdVersion(cm))
354354
}

libcontainer/cgroups/systemd/v1.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ var legacySubsystems = []subsystem{
7575
func genV1ResourcesProperties(r *configs.Resources, cm *dbusConnManager) ([]systemdDbus.Property, error) {
7676
var properties []systemdDbus.Property
7777

78-
deviceProperties, err := generateDeviceProperties(r)
78+
deviceProperties, err := generateDeviceProperties(r, cm)
7979
if err != nil {
8080
return nil, err
8181
}

libcontainer/cgroups/systemd/v2.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func genV2ResourcesProperties(dirPath string, r *configs.Resources, cm *dbusConn
214214
// aren't the end of the world, but it is a bit concerning. However
215215
// it's unclear if systemd removes all eBPF programs attached when
216216
// doing SetUnitProperties...
217-
deviceProperties, err := generateDeviceProperties(r)
217+
deviceProperties, err := generateDeviceProperties(r, cm)
218218
if err != nil {
219219
return nil, err
220220
}

0 commit comments

Comments
 (0)