Skip to content

Extract FileMode from host path if possible #960

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions internal/edits/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@
package edits

import (
"os"

"tags.cncf.io/container-device-interface/pkg/cdi"
"tags.cncf.io/container-device-interface/specs-go"

"github.com/opencontainers/runc/libcontainer/devices"

"github.com/NVIDIA/nvidia-container-toolkit/internal/discover"
)

Expand Down Expand Up @@ -49,13 +53,31 @@ func (d device) toSpec() (*specs.DeviceNode, error) {
// Since the behaviour for HostPath == "" and HostPath == Path are equivalent, we clear HostPath
// if it is equal to Path to ensure compatibility with the widest range of specs.
hostPath := d.HostPath

if hostPath == d.Path {
hostPath = ""
}
s := specs.DeviceNode{
HostPath: hostPath,
Path: d.Path,
FileMode: d.getFileMode(),
}

return &s, nil
}

// getFileMode returns the filemode of the host device node associated with the discovered device.
// If this fails, a nil filemode is returned.
func (d device) getFileMode() *os.FileMode {
path := d.HostPath
if path == "" {
path = d.Path
}
dn, err := devices.DeviceFromPath(path, "rwm")
if err != nil {
// return nil, fmt.Errorf("failed to get device information for %q: %w", path, err)
return nil
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To what will nil translate to in the spec? => 0 ?

Copy link
Member Author

@elezar elezar Mar 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nil would be the current behaviour. In the case of runc this is filled in by the runtime and read from the host. In the case of kata, this would be 0 as you've observed.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The interface is the CDI spec, so how does runc know this value is nil? It is not encoded in the CDI spec.

Copy link
Member Author

@elezar elezar Mar 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, maybe runc is not the correct reference here. For regular devices, a higher-level runtime such as containerd also extracts the FileMode (see https://github.com/containerd/containerd/blob/9deb0b8f748ee038dfd48c51d7aee6b55e34e9c1/pkg/oci/utils_unix.go#L142). This is not done for CDI devices since the code from https://github.com/cncf-tags/container-device-interface/blob/012c8be38d9444f08b7e6eeedbcddd733adf8632/pkg/cdi/container-edits_unix.go#L60 is used for this instead.

This means that for OCI devices created from a CDI spec, this field is nil if unspecified, whereas for "regular" devices this field is populated from the device node on the host.

@klihub do you know why we elected to not add information other than the Major and Minor numbers to the devices in CDI?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@elezar Do you mean what is the reason for not filling in any other missing information wrt. the CDI Spec DeviceNode than type, major, and minor ? TBH, I don't recall any specifics, but I suspect it's only because that's the minimum working implementation.

}

return &dn.FileMode
}
174 changes: 174 additions & 0 deletions vendor/github.com/opencontainers/runc/libcontainer/devices/device.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ github.com/google/uuid
github.com/moby/sys/symlink
# github.com/opencontainers/runc v1.2.5
## explicit; go 1.22
github.com/opencontainers/runc/libcontainer/devices
github.com/opencontainers/runc/libcontainer/dmz
github.com/opencontainers/runc/libcontainer/system
github.com/opencontainers/runc/libcontainer/utils
Expand Down