Skip to content

Commit 4dae5ae

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

File tree

4 files changed

+316
-0
lines changed

4 files changed

+316
-0
lines changed

internal/edits/device.go

Lines changed: 22 additions & 0 deletions
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+
}

vendor/github.com/opencontainers/runc/libcontainer/devices/device.go

Lines changed: 174 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/opencontainers/runc/libcontainer/devices/device_unix.go

Lines changed: 119 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/modules.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ github.com/google/uuid
3636
github.com/moby/sys/symlink
3737
# github.com/opencontainers/runc v1.2.5
3838
## explicit; go 1.22
39+
github.com/opencontainers/runc/libcontainer/devices
3940
github.com/opencontainers/runc/libcontainer/dmz
4041
github.com/opencontainers/runc/libcontainer/system
4142
github.com/opencontainers/runc/libcontainer/utils

0 commit comments

Comments
 (0)