Skip to content

Commit eb70273

Browse files
committed
Extract FileMode from host path if possible
Signed-off-by: Evan Lezar <[email protected]>
1 parent bc9ec77 commit eb70273

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

internal/edits/device.go

+22
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@
1717
package edits
1818

1919
import (
20+
"os"
21+
2022
"tags.cncf.io/container-device-interface/pkg/cdi"
2123
"tags.cncf.io/container-device-interface/specs-go"
2224

25+
"github.com/opencontainers/runc/libcontainer/devices"
26+
2327
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover"
2428
)
2529

@@ -49,13 +53,31 @@ func (d device) toSpec() (*specs.DeviceNode, error) {
4953
// Since the behaviour for HostPath == "" and HostPath == Path are equivalent, we clear HostPath
5054
// if it is equal to Path to ensure compatibility with the widest range of specs.
5155
hostPath := d.HostPath
56+
5257
if hostPath == d.Path {
5358
hostPath = ""
5459
}
5560
s := specs.DeviceNode{
5661
HostPath: hostPath,
5762
Path: d.Path,
63+
FileMode: d.getFileMode(),
5864
}
5965

6066
return &s, nil
6167
}
68+
69+
// getFileMode returns the filemode of the host device node associated with the discovered device.
70+
// If this fails, a nil filemode is returned.
71+
func (d device) getFileMode() *os.FileMode {
72+
path := d.HostPath
73+
if path == "" {
74+
path = d.Path
75+
}
76+
dn, err := devices.DeviceFromPath(path, "rwm")
77+
if err != nil {
78+
// return nil, fmt.Errorf("failed to get device information for %q: %w", path, err)
79+
return nil
80+
}
81+
82+
return &dn.FileMode
83+
}

0 commit comments

Comments
 (0)