diff --git a/libpod/container_inspect.go b/libpod/container_inspect.go index 5d759ecc22..e893d6fb3b 100644 --- a/libpod/container_inspect.go +++ b/libpod/container_inspect.go @@ -653,7 +653,7 @@ func (c *Container) GetDevices(priv bool, ctrSpec spec.Spec, deviceNodes map[str for _, dev := range ctrSpec.Linux.Devices { key := fmt.Sprintf("%d:%d", dev.Major, dev.Minor) if deviceNodes == nil { - nodes, err := util.FindDeviceNodes() + nodes, err := util.FindDeviceNodes(false) if err != nil { return nil, err } @@ -678,7 +678,7 @@ func blkioDeviceThrottle(deviceNodes map[string]string, devs []spec.LinuxThrottl for _, dev := range devs { key := fmt.Sprintf("%d:%d", dev.Major, dev.Minor) if deviceNodes == nil { - nodes, err := util.FindDeviceNodes() + nodes, err := util.FindDeviceNodes(true) if err != nil { return nil, err } diff --git a/libpod/container_inspect_linux.go b/libpod/container_inspect_linux.go index e8fd37c05c..ea7a391d96 100644 --- a/libpod/container_inspect_linux.go +++ b/libpod/container_inspect_linux.go @@ -89,7 +89,7 @@ func (c *Container) platformInspectContainerHostConfig(ctrSpec *spec.Spec, hostC continue } if deviceNodes == nil { - nodes, err := util.FindDeviceNodes() + nodes, err := util.FindDeviceNodes(true) if err != nil { return err } diff --git a/pkg/util/utils_linux.go b/pkg/util/utils_linux.go index 9130d1f404..d113ad802a 100644 --- a/pkg/util/utils_linux.go +++ b/pkg/util/utils_linux.go @@ -33,7 +33,8 @@ func GetContainerPidInformationDescriptors() ([]string, error) { // [major:minor] is the device's major and minor numbers formatted as, for // example, 2:0 and path is the path to the device node. // Symlinks to nodes are ignored. -func FindDeviceNodes() (map[string]string, error) { +// If onlyBlockDevices is specified, character devices are ignored. +func FindDeviceNodes(onlyBlockDevices bool) (map[string]string, error) { nodes := make(map[string]string) err := filepath.WalkDir("/dev", func(path string, d fs.DirEntry, err error) error { if err != nil { @@ -44,7 +45,13 @@ func FindDeviceNodes() (map[string]string, error) { } // If we aren't a device node, do nothing. - if d.Type()&(os.ModeDevice|os.ModeCharDevice) == 0 { + if d.Type()&os.ModeDevice == 0 { + return nil + } + + // Ignore character devices, because it is not possible to set limits on them. + // os.ModeCharDevice is usable only when os.ModeDevice is set. + if onlyBlockDevices && d.Type()&os.ModeCharDevice != 0 { return nil } diff --git a/pkg/util/utils_unsupported.go b/pkg/util/utils_unsupported.go index 8a77e189da..7885423b46 100644 --- a/pkg/util/utils_unsupported.go +++ b/pkg/util/utils_unsupported.go @@ -5,6 +5,6 @@ package util import "errors" // FindDeviceNodes is not implemented anywhere except Linux. -func FindDeviceNodes() (map[string]string, error) { +func FindDeviceNodes(onlyBlockDevices bool) (map[string]string, error) { return nil, errors.New("not supported on non-Linux OSes") } diff --git a/test/apiv2/20-containers.at b/test/apiv2/20-containers.at index 7b8c5a6bf0..fa4c6ae46f 100644 --- a/test/apiv2/20-containers.at +++ b/test/apiv2/20-containers.at @@ -710,15 +710,15 @@ t GET libpod/containers/$cname/json 200 \ .ImageName=$IMAGE \ .Name=$cname -if root; then +if root && test -e /dev/nullb0; then podman run -dt --name=updateCtr alpine echo '{ "Memory":{"Limit":500000}, "CPU":{"Shares":123}, - "DeviceReadBPs": [{ "Path": "/dev/zero", "Rate": 10485760 }], - "DeviceWriteBPs": [{ "Path": "/dev/zero", "Rate": 31457280 }], - "DeviceReadIOPs": [{ "Path": "/dev/zero", "Rate": 2000 }], - "DeviceWriteIOPs": [{ "Path": "/dev/zero", "Rate": 4000 }] + "DeviceReadBPs": [{ "Path": "/dev/nullb0", "Rate": 10485760 }], + "DeviceWriteBPs": [{ "Path": "/dev/nullb0", "Rate": 31457280 }], + "DeviceReadIOPs": [{ "Path": "/dev/nullb0", "Rate": 2000 }], + "DeviceWriteIOPs": [{ "Path": "/dev/nullb0", "Rate": 4000 }] }' >${TMPD}/update.json t POST libpod/containers/updateCtr/update ${TMPD}/update.json 201 @@ -734,25 +734,25 @@ if root; then BlkioDeviceReadBps_expected='[ { - "Path": "/dev/zero", + "Path": "/dev/nullb0", "Rate": 10485760 } ]' BlkioDeviceWriteBPs_expected='[ { - "Path": "/dev/zero", + "Path": "/dev/nullb0", "Rate": 31457280 } ]' BlkioDeviceReadIOPs_expected='[ { - "Path": "/dev/zero", + "Path": "/dev/nullb0", "Rate": 2000 } ]' BlkioDeviceWriteIOPs_expected='[ { - "Path": "/dev/zero", + "Path": "/dev/nullb0", "Rate": 4000 } ]'