From eb70273971fcb2b4118fa2ad19a8b8911609f6f6 Mon Sep 17 00:00:00 2001 From: Evan Lezar Date: Wed, 5 Mar 2025 17:30:26 +0200 Subject: [PATCH] Extract FileMode from host path if possible Signed-off-by: Evan Lezar --- internal/edits/device.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/internal/edits/device.go b/internal/edits/device.go index d04df153..f7b3eb08 100644 --- a/internal/edits/device.go +++ b/internal/edits/device.go @@ -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" ) @@ -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 + } + + return &dn.FileMode +}