Skip to content

Commit bdc6b5a

Browse files
committed
Add support for wildcard devices
This change allows the following device node CDI spec to be considered valid: path: "*" type: "c" major: 195 minor: -1 permissions: "rwm" When this is included in the spec, no device nodes are added to the OCI runtime spec, but the linux resources (cgroups) are updated to allow access to all devices in the same way as the Docker `--device-cgroup-rule` flag. This is useful when starting a container that needs to be able to create device nodes that do not exist when it is started. Signed-off-by: Evan Lezar <elezar@nvidia.com>
1 parent db0a0da commit bdc6b5a

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

pkg/cdi/container-edits.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,10 @@ func (d *DeviceNode) Validate() error {
354354
}
355355

356356
func (d *DeviceNode) addToGenerator(specgen *ocigen.Generator, spec *oci.Spec) error {
357+
if d.isWildcardDevice() {
358+
return d.addAsWildcardDevice(specgen)
359+
}
360+
357361
err := d.fillMissingInfo()
358362
if err != nil {
359363
return err
@@ -389,8 +393,33 @@ func (d *DeviceNode) getAccessString() string {
389393
return d.Permissions
390394
}
391395
}
396+
397+
// isWildcardDevice returns whether the device node represents a "wildcard" device.
398+
// Such devices do not cause device nodes to be created in the container, but
399+
// do update the cgroups to allow device access. A wildcard device always has
400+
// the path specified as "*" and setting major or minor numbers to -1 will add
401+
// a cgroup rule that matches all major (or minor) numbers.
402+
func (d *DeviceNode) isWildcardDevice() bool {
403+
return d.Path == "*"
404+
}
405+
406+
func (d *DeviceNode) addAsWildcardDevice(specgen *ocigen.Generator) error {
407+
if d.Type != "b" && d.Type != "c" {
408+
return fmt.Errorf("wildcard device node not supported for device type %v", d.Type)
409+
}
410+
var major *int64
411+
if d.Major != -1 {
412+
major = &d.Major
413+
}
414+
var minor *int64
415+
if d.Minor != -1 {
416+
minor = &d.Minor
417+
}
418+
specgen.AddLinuxResourcesDevice(true, d.Type, major, minor, d.getAccessString())
419+
392420
return nil
393421
}
422+
394423
// Hook is a CDI Spec Hook wrapper, used for validating hooks.
395424
type Hook struct {
396425
*cdi.Hook

0 commit comments

Comments
 (0)